Skip to content

Commit

Permalink
feat: add read_href_modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Jan 3, 2022
1 parent 288b6af commit 1154a1f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). This project attempts to match the major and minor versions of [stactools](https://github.com/stac-utils/stactools) and increments the patch number as needed.

## [Unreleased]

- Add `read_href_modifier`

## [v0.1.2] - 2021-09-22

### Added
Expand Down
1 change: 1 addition & 0 deletions src/stactools/noaa_c_cap/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

def create_noaa_c_cap_command(cli):
"""Creates the stactools-noaa-c-cap command line utility."""

@cli.group(
"noaa-c-cap",
short_help=("Commands for working with stactools-noaa-c-cap"),
Expand Down
19 changes: 14 additions & 5 deletions src/stactools/noaa_c_cap/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pystac.extensions.label import LabelClasses, LabelExtension, LabelType
from pystac.extensions.scientific import ScientificExtension
from stactools.core import create
from stactools.core.io import ReadHrefModifier

from stactools.noaa_c_cap import Dataset, utils
from stactools.noaa_c_cap.constants import (COLLECTION_CITATION,
Expand Down Expand Up @@ -64,23 +65,31 @@ def create_collection(hrefs: Optional[List[str]] = None) -> Collection:
return collection


def create_item(tiff_href: str, xml_href: Optional[str] = None) -> Item:
def create_item(tiff_href: str,
xml_href: Optional[str] = None,
read_href_modifier: Optional[ReadHrefModifier] = None) -> Item:
"""Creates a STAC Item for the provided C-CAP tiff file.
Args:
tiff_href (str): The HREF pointing to the tiff file.
xml_href (str): The HREF pointing to the xml file.
read_href_modifier (Callable[[str], str]): A function to modify the read href for the file.
Returns:
Item: STAC Item object
"""
return create_item_from_dataset(
Dataset(tiff_href=tiff_href, xml_href=xml_href))
return create_item_from_dataset(Dataset(tiff_href=tiff_href,
xml_href=xml_href),
read_href_modifier=read_href_modifier)


def create_item_from_dataset(dataset: Dataset) -> Item:
def create_item_from_dataset(
dataset: Dataset,
read_href_modifier: Optional[ReadHrefModifier] = None) -> Item:
"""Creates an item from a NOAA C-CAP dataset."""
logger.info(f"Creating STAC item from {dataset.tiff_href}")
item = create.item(dataset.tiff_href)
item = create.item(dataset.tiff_href,
read_href_modifier=read_href_modifier)
item.common_metadata.start_datetime = datetime.datetime(
int(dataset.year), 1, 1, tzinfo=datetime.timezone.utc)
item.common_metadata.end_datetime = datetime.datetime(
Expand Down
1 change: 1 addition & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


class CommandsTest(CliTestCase):

def create_subcommand_functions(self):
return [create_noaa_c_cap_command]

Expand Down
11 changes: 11 additions & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from unittest import TestCase

from stactools.noaa_c_cap import Dataset


class DatasetTest(TestCase):

def test_query_parameters(self) -> None:
Dataset("https://coast.noaa.gov/htdata/raster1/landcover/bulkdownload/"
"30m_lc/conus_1975_ccap_landcover_20200311.tif"
"?a-query-parameter=with_underscores")
1 change: 1 addition & 0 deletions tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@


class TestModule(unittest.TestCase):

def test_version(self):
self.assertIsNotNone(stactools.noaa_c_cap.__version__)
14 changes: 14 additions & 0 deletions tests/test_stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@


class StacTest(unittest.TestCase):

def test_create_collection(self):
hrefs = [
test_data.get_external_data(
Expand Down Expand Up @@ -62,6 +63,19 @@ def test_create_item(self):
self.assertEqual(data.roles, ['data'])
item.validate()

def test_create_item_with_read_href_modifier(self):
did_it = False

def do_it(href: str) -> str:
nonlocal did_it
did_it = True
return href

path = test_data.get_external_data(
'conus_2016_ccap_landcover_20200311.tif')
stac.create_item(path, read_href_modifier=do_it)
assert did_it

def test_create_item_with_xml(self):
tiff_path = test_data.get_external_data(
'conus_2016_ccap_landcover_20200311.tif')
Expand Down

0 comments on commit 1154a1f

Please sign in to comment.