Skip to content

Commit 9591b16

Browse files
authored
Merge pull request #177 from nasa/feature/issue-176-grouping-using-day-in-US-central
Issue 176: Modifies grouping algorithm to use day in US Central as the first part of unique grouping identifier.
2 parents ec03521 + 086ede6 commit 9591b16

3 files changed

Lines changed: 66 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## Unreleased
88

9+
- update grouping algorithm to use day in US Central as the first part of unique grouping identifier ([#177](https://github.com/nasa/batchee/pull/177))([**@ank1m**](https://github.com/ank1m))
10+
911
## [1.3.0] - 2024-11-19
1012

1113
### Changed

batcher/tempo_filename_parser.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import logging
3030
import re
3131
from argparse import ArgumentParser
32+
from datetime import datetime
33+
from zoneinfo import ZoneInfo
3234

3335
default_logger = logging.getLogger(__name__)
3436

@@ -45,6 +47,36 @@
4547
)
4648

4749

50+
def get_day_in_us_central(
51+
day_in_granule: str, time_in_granule: str, assume_tz=ZoneInfo("UTC")
52+
) -> str:
53+
"""
54+
Convert a datetime to US Central time (US/Central) and return
55+
a timezone-aware datetime.
56+
57+
Parameters
58+
----------
59+
day_in_granule: str
60+
The day from granule filename
61+
time_in_granule: str
62+
The time from granule filename
63+
assume_tz : timezone, optional (default: UTC)
64+
this is the timezone in which `day_in_granule` and `time_in_granule`
65+
should be interpreted before converting to Central.
66+
67+
Returns
68+
-------
69+
str
70+
The day for datetime converted to US/Central
71+
"""
72+
73+
dt = datetime.strptime(day_in_granule + time_in_granule, "%Y%m%d%H%M%S")
74+
dt = dt.replace(tzinfo=assume_tz)
75+
76+
dt_central = dt.astimezone(ZoneInfo("America/Chicago"))
77+
return dt_central.strftime("%Y%m%d")
78+
79+
4880
def get_batch_indices(filenames: list, logger: logging.Logger = default_logger) -> list[int]:
4981
"""
5082
Returns
@@ -60,7 +92,10 @@ def get_batch_indices(filenames: list, logger: logging.Logger = default_logger)
6092
matches = tempo_granule_filename_pattern.match(name)
6193
if matches:
6294
match_dict = matches.groupdict()
63-
day_and_scans.append((match_dict["day_in_granule"], match_dict["daily_scan_id"]))
95+
day_in_central = get_day_in_us_central(
96+
match_dict["day_in_granule"], match_dict["time_in_granule"]
97+
)
98+
day_and_scans.append((day_in_central, match_dict["daily_scan_id"]))
6499

65100
# Unique day-scans are determined (while keeping the same order). Each will be its own batch.
66101
unique_day_scans: list[tuple[str, str]] = sorted(set(day_and_scans), key=day_and_scans.index)

tests/test_filename_grouping.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,41 @@
22
from unittest.mock import patch
33

44
import batcher.tempo_filename_parser
5-
from batcher.tempo_filename_parser import get_batch_indices
5+
from batcher.tempo_filename_parser import get_batch_indices, get_day_in_us_central
66

77
example_filenames = [
8-
"TEMPO_HCHO_L2_V01_20130701T212354Z_S009G05.nc",
9-
"TEMPO_HCHO_L2_V01_20130701T212953Z_S009G06.nc",
10-
"TEMPO_HCHO_L2_V01_20130701T213553Z_S009G07.nc",
11-
"TEMPO_HCHO_L2_V01_20130701T215955Z_S010G01.nc",
12-
"TEMPO_HCHO_L2_V01_20130701T220554Z_S010G02.nc",
13-
"TEMPO_HCHO_L2_V01_20130701T221154Z_S010G03.nc",
8+
"TEMPO_NO2_L2_V03_20240731T235252Z_S016G04.nc",
9+
"TEMPO_NO2_L2_V03_20240731T235929Z_S016G05.nc",
10+
"TEMPO_NO2_L2_V03_20240801T000606Z_S016G06.nc",
11+
"TEMPO_NO2_L2_V03_20240801T001302Z_S017G01.nc",
12+
"TEMPO_NO2_L2_V03_20240801T001942Z_S017G02.nc",
13+
"TEMPO_NO2_L2_V03_20240801T002619Z_S017G03.nc",
14+
"TEMPO_NO2_L2_V03_20240801T233313Z_S016G01.nc",
15+
"TEMPO_NO2_L2_V03_20240801T233953Z_S016G02.nc",
16+
"TEMPO_NO2_L2_V03_20240801T234630Z_S016G03.nc",
1417
]
1518

1619

20+
def test_timezone_conversion():
21+
utcdates = [filename.split("_")[4] for filename in example_filenames]
22+
days_in_central = [get_day_in_us_central(utcdt[0:8], utcdt[9:15]) for utcdt in utcdates]
23+
assert days_in_central == [
24+
"20240731",
25+
"20240731",
26+
"20240731",
27+
"20240731",
28+
"20240731",
29+
"20240731",
30+
"20240801",
31+
"20240801",
32+
"20240801",
33+
]
34+
35+
1736
def test_grouping():
1837
results = get_batch_indices(example_filenames)
1938

20-
assert results == [0, 0, 0, 1, 1, 1]
39+
assert results == [0, 0, 0, 1, 1, 1, 2, 2, 2]
2140

2241

2342
def test_main_cli():
@@ -27,4 +46,4 @@ def test_main_cli():
2746
with patch.object(sys, "argv", test_args):
2847
grouped_names = batcher.tempo_filename_parser.main()
2948

30-
assert grouped_names == [example_filenames[0:3], example_filenames[3:6]]
49+
assert grouped_names == [example_filenames[0:3], example_filenames[3:6], example_filenames[6:9]]

0 commit comments

Comments
 (0)