From aadfff50211e95a8210d7e247eb5a78550bdf3a2 Mon Sep 17 00:00:00 2001 From: sharkinsspatial Date: Thu, 20 Jun 2024 10:55:11 -0600 Subject: [PATCH] Include an idstring parameter to allow arbitrary inuputdir naming. --- README.md | 12 +++++------- hls_vi/generate_indices.py | 14 ++++++++------ tests/test_vi.py | 18 ++++++++++++------ 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 6b97a06..7e45646 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/hls_vi/generate_indices.py b/hls_vi/generate_indices.py index 44fed5f..064ce9c 100644 --- a/hls_vi/generate_indices.py +++ b/hls_vi/generate_indices.py @@ -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) @@ -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__": diff --git a/tests/test_vi.py b/tests/test_vi.py index 812ac4b..e4be2dd 100644 --- a/tests/test_vi.py +++ b/tests/test_vi.py @@ -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")))