Skip to content

Commit

Permalink
Simplier time filters: before/after
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyh committed Apr 10, 2022
1 parent cdbba92 commit 39213c8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
30 changes: 24 additions & 6 deletions eodatasets3/prepare/sentinel_l1_prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,16 @@ def convert(self, value, param, ctx):
),
)
@click.option(
"--limit-newer-than",
help="A {year}-{month} string to limit the scan to datasets newer than that date",
"--after-month",
help="Limit the scan to datasets newer than a given month "
"(expressed as {year}-{month}, eg '2010-01')",
required=False,
type=YearMonth(),
)
@click.option(
"--before-month",
help="Limit the scan to datasets older than the given month "
"(expressed as {year}-{month}, eg '2010-01')",
required=False,
type=YearMonth(),
)
Expand All @@ -444,7 +452,8 @@ def main(
overwrite_existing: bool,
embed_location: Optional[bool],
limit_regions_file: Optional[Path],
limit_newer_than: Optional[Tuple[int, int]],
before_month: Optional[Tuple[int, int]],
after_month: Optional[Tuple[int, int]],
):
if sys.argv[1] == "sentinel-l1c":
warnings.warn(
Expand All @@ -464,7 +473,7 @@ def main(
info = FolderInfo.for_path(input_path)

# Skip regions that are not in the limit?
if limit_regions or limit_newer_than:
if limit_regions or before_month or after_month:
if info is None:
raise ValueError(
f"Cannot filter from non-standard folder layout: {input_path}"
Expand All @@ -476,14 +485,23 @@ def main(
f"Skipping because region {info.region_code!r} is in region filter"
)
continue
if limit_newer_than is not None:
year, month = limit_newer_than

if after_month is not None:
year, month = after_month

if info.year < year or (info.year == year and info.month < month):
logging.debug(
f"Skipping because year {info.year}-{info.month} is older than {year}-{month}"
)
continue
if before_month is not None:
year, month = before_month

if info.year > year or (info.year == year and info.month > month):
logging.debug(
f"Skipping because year {info.year}-{info.month} is newer than {year}-{month}"
)
continue

# The default input_relative path is a parent folder named 'L1C'.
if output_base and (i == 0 and input_relative_to is None):
Expand Down
19 changes: 17 additions & 2 deletions tests/integration/prepare/test_prepare_sentinel_l1.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def test_filter_folder_structure_info(
res = run_prepare_cli(
sentinel_l1_prepare.main,
# After our time period, so it should filter!
"--limit-newer-than",
"--after-month",
"2019-03",
# "Put the output in a different location":
"--output-base",
Expand All @@ -444,12 +444,27 @@ def test_filter_folder_structure_info(
not expected_metadata_path.exists()
), f"Expected dataset to be filtered out! {res.output}"

# Filter the time period
res = run_prepare_cli(
sentinel_l1_prepare.main,
# Before our time period, so it should filter!
"--before-month",
"2018-03",
# "Put the output in a different location":
"--output-base",
output_folder,
input_dataset_path,
)
assert (
not expected_metadata_path.exists()
), f"Expected dataset to be filtered out! {res.output}"

# Now run for real, expect an output.
check_prepare_outputs(
invoke_script=sentinel_l1_prepare.main,
run_args=[
# Before our time period, so it should be fine!
"--limit-newer-than",
"--after-month",
"2018-12",
# "Put the output in a different location":
"--output-base",
Expand Down

0 comments on commit 39213c8

Please sign in to comment.