2929import logging
3030import re
3131from argparse import ArgumentParser
32+ from datetime import datetime
33+ from dateutil import tz
3234
3335default_logger = logging .getLogger (__name__ )
3436
4446 r"(?P<granule_id>G[0-9]{2}).*\.nc"
4547)
4648
49+ def get_day_in_us_central (day_in_granule : str , time_in_granule : str , assume_tz = tz .UTC ) -> str :
50+ """
51+ Convert a datetime to US Central time (US/Central) and return
52+ a timezone-aware datetime.
53+
54+ Parameters
55+ ----------
56+ day_in_granule: str
57+ The day from granule filename
58+ time_in_granule: str
59+ The time from granule filename
60+ assume_tz : timezon, optional (default: UTC)
61+ this is the timezone in which `dt` should be interpreted
62+ before converting to Central.
63+
64+ Returns
65+ -------
66+ str
67+ The day for datetime converted to US/Central
68+ """
69+
70+ dt = datetime .strptime (day_in_granule + time_in_granule , "%Y%m%d%H%M%S" )
71+ dt = dt .replace (tzinfo = assume_tz )
72+
73+ dt_central = dt .astimezone (tz .gettz ('US/Central' ))
74+ return dt_central .strftime ("%Y%m%d" )
75+
4776
4877def get_batch_indices (filenames : list , logger : logging .Logger = default_logger ) -> list [int ]:
4978 """
@@ -60,7 +89,8 @@ def get_batch_indices(filenames: list, logger: logging.Logger = default_logger)
6089 matches = tempo_granule_filename_pattern .match (name )
6190 if matches :
6291 match_dict = matches .groupdict ()
63- day_and_scans .append ((match_dict ["day_in_granule" ], match_dict ["daily_scan_id" ]))
92+ day_in_central = get_day_in_us_central (match_dict ["day_in_granule" ], match_dict ["time_in_granule" ])
93+ day_and_scans .append ((day_in_central , match_dict ["daily_scan_id" ]))
6494
6595 # Unique day-scans are determined (while keeping the same order). Each will be its own batch.
6696 unique_day_scans : list [tuple [str , str ]] = sorted (set (day_and_scans ), key = day_and_scans .index )
0 commit comments