Skip to content

Commit

Permalink
Merge pull request #14 from NASA-IMPACT/granule_id_param
Browse files Browse the repository at this point in the history
Include an idstring parameter to allow arbitrary inuputdir naming.
  • Loading branch information
sharkinsspatial authored Jun 20, 2024
2 parents 35d49ac + aadfff5 commit 1ea40c3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ Generates suite of Vegetation Indices (VI) for HLS Products.
### Generating Vegetation Indices

```plain
vi_generate_indices -i INPUT_DIR -o OUTPUT_DIR
vi_generate_indices -i INPUT_DIR -o OUTPUT_DIR -id ID_STRING
```

where:

- `INPUT_DIR` is expected to be named like
`HLS.{instrument}.{tile_id}.{acquisition_date}.v{version}` and contain L30 or
S30 band tifs.
- `OUTPUT_DIR` is the directory to write VI tifs, and will be created if it does
not already exist. The name of this directory should be named like
`INPUT_DIR`, but with the prefix `HLS` replaced with `HLS-VI`.
- `INPUT_DIR` is expected to contain L30 or S30 band geotiffs.
- `OUTPUT_DIR` is the directory to write VI geotiffs, and will be created if it does
not already exist.
- `ID_STRING` is the HLS granule id basename with a pattern of `HLS.{instrument}.{tile_id}.{acquisition_date}.v{version}`

### Generating CMR Metadata

Expand Down
14 changes: 8 additions & 6 deletions hls_vi/generate_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ class Granule:
data: BandData


def read_granule_bands(input_dir: Path) -> Granule:
id_ = GranuleId.from_string(os.path.basename(input_dir))
def read_granule_bands(input_dir: Path, id_str: str) -> Granule:
id_ = GranuleId.from_string(id_str)

with rasterio.open(input_dir / f"{id_}.Fmask.tif") as tif:
fmask = tif.read(1, masked=False)
Expand Down Expand Up @@ -362,24 +362,26 @@ def parse_args() -> Tuple[Path, Path]:
print(help_text, file=sys.stderr)
sys.exit(2)

input_dir, output_dir = None, None
input_dir, output_dir, id_str = None, None, None

for option, value in options:
if option in ("-i", "--inputdir"):
input_dir = value
elif option in ("-o", "--outputdir"):
output_dir = value
elif option in ("-id", "--idstring"):
id_str = value

if input_dir is None or output_dir is None:
print(help_text, file=sys.stderr)
sys.exit(2)

return Path(input_dir), Path(output_dir)
return Path(input_dir), Path(output_dir), id_str


def main():
input_dir, output_dir = parse_args()
write_granule_indices(output_dir, read_granule_bands(input_dir))
input_dir, output_dir, id_str = parse_args()
write_granule_indices(output_dir, read_granule_bands(input_dir, id_str))


if __name__ == "__main__":
Expand Down
18 changes: 12 additions & 6 deletions tests/test_vi.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,20 @@ def assert_indices_equal(actual_dir: Path, expected_dir: Path):


@pytest.mark.parametrize(
argnames="input_dir",
argnames="input_dir,id_str",
argvalues=[
"tests/fixtures/HLS.L30.T06WVS.2024120T211159.v2.0",
"tests/fixtures/HLS.S30.T13RCN.2024128T173909.v2.0",
],
(
"tests/fixtures/HLS.L30.T06WVS.2024120T211159.v2.0",
"HLS.L30.T06WVS.2024120T211159.v2.0",
),
(
"tests/fixtures/HLS.S30.T13RCN.2024128T173909.v2.0",
"HLS.S30.T13RCN.2024128T173909.v2.0",
),
]
)
def test_generate_indices(input_dir, tmp_path: Path):
write_granule_indices(tmp_path, read_granule_bands(Path(input_dir)))
def test_generate_indices(input_dir, id_str, tmp_path: Path):
write_granule_indices(tmp_path, read_granule_bands(Path(input_dir), id_str))
assert_indices_equal(tmp_path, Path(input_dir.replace("HLS", "HLS-VI")))


Expand Down

0 comments on commit 1ea40c3

Please sign in to comment.