monkey_wrench.date_time._parser module
- class monkey_wrench.date_time._parser.DateTimeParserBase[source]
Bases:
objectA static base class for parsing items, e.g. product IDs or file paths, into datetime objects.
- static _raise_value_error(item: Any) Never[source]
Helper function to raise a
ValueErrorwhen the given item cannot be parsed.
- static parse_by_regex(item: str, regex: str, timezone: ZoneInfo | None = None) datetime[source]
Parse the given item into a datetime object using a regular expression.
- Parameters:
item – The item to parse.
regex – The regular expression to match against.
timezone – The timezone to add to the datetime object. Defaults to
None, which meansUTCwill be used.
- Returns:
The parsed datetime object, if successful.
- Raises:
ValueError – If the given item cannot be parsed.
Example
>>> regex = r"^(19|20\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])_(0\d|1\d|2[0-3])_([0-5]\d)$" >>> DateTimeParserBase.parse_by_regex("20230102_22_30", regex) datetime.datetime(2023, 1, 2, 22, 30, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
- static parse_by_format_string(datetime_string: str, datetime_format_string: str, timezone: ZoneInfo | None = None) datetime[source]
Parse the given datetime string into a datetime object using the given format string.
- Parameters:
datetime_string – The datetime string to parse.
datetime_format_string – The format string using which the parsing is done, e.g.
"%Y%m%d_%H_%M".timezone – The timezone to add to the datetime object. Defaults to
None, which meansUTCwill be used.
- Returns:
The parsed datetime object, if successful.
- Raises:
ValueError – If the given datetime string cannot be parsed.
Example
>>> DateTimeParserBase.parse_by_format_string("20230101_22_30", "%Y%m%d_%H_%M") datetime.datetime(2023, 1, 1, 22, 30, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
- classmethod parse_collection(items: list[Any] | set[Any] | tuple[Any, ...] | Generator) list[datetime] | set[datetime] | tuple[datetime, ...] | Generator[datetime, None, None][source]
Parse the given collection of items into a collection of datetime objects.
- Parameters:
items – The collection (list/set/tuple or generator) of items to parse.
- Returns:
A collection of datetime objects. The type of collection matches the type of the input collection, e.g. a list as input results in a list of datetime objects.
- class monkey_wrench.date_time._parser.SeviriIDParser[source]
Bases:
DateTimeParserBaseStatic parser class for SEVIRI product IDs.
- regex = '[0-9A-Za-z]+-SEVI-[0-9A-Za-z]+-[0-9]+-NA-([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})[0-9]{2}\\.[0-9]+Z-NA'
- class monkey_wrench.date_time._parser.FCIIDParser[source]
Bases:
DateTimeParserBaseStatic parser class for FCI product IDs.
- static parse(fci_product_id: str) datetime[source]
Parse the given FCI product ID into a datetime object.
Example
>>> FCIIDParser.parse( ... "W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-FDHSI-FD--x-x---x_C_EUMT_" ... "20251216091032_IDPFI_OPE_20251216091007_20251216091923_N__O_0056_0000" ... ) datetime.datetime(2025, 12, 16, 9, 10, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> FCIIDParser.parse( ... "W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+FCI-1C-RRAD-HRFI-FD--x-x---x_C_EUMT_" ... "20250102102250_IDPFI_OPE_20250102102007_20250102102924_N__O_0063_0000" ... ) datetime.datetime(2025, 1, 2, 10, 20, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
- class monkey_wrench.date_time._parser.ChimpFilePathParser[source]
Bases:
DateTimeParserBaseStatic parser class for CHIMP-compiliant input and output file paths.
- regex = '[0-9A-Za-z]+_([0-9]{4})([0-9]{2})([0-9]{2})_([0-9]{2})_([0-9]{2})'
- static parse(filepath: Annotated[Path | T, AfterValidator(func=_validate_path)]) datetime[source]
Parse the given filepath into a datetime object.
- Parameters:
filepath – The filepath to parse. It can be either an absolute path or a relative path (e.g. just the base name). For the parsing to be successful, the
filepathmust have the following format:<optional_path><prefix>_<YYYY>_<mm>_<DD>_<HH>_<MM><optional_extension>, where<prefix>is mandatory but can be anything except for an empty string. See the examples below.
Examples
>>> from pathlib import Path >>> >>> # Input is an absolute path of type `Path`. >>> ChimpFilePathParser.parse(Path("/home/user/dir/seviri_20150731_22_12.extension")) datetime.datetime(2015, 7, 31, 22, 12, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is an absolute path of type `Path`. >>> ChimpFilePathParser.parse(Path("/home/user/dir/seviri_20150110_00_01.extension")) datetime.datetime(2015, 1, 10, 0, 1, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is a relative path of type `Path`. >>> ChimpFilePathParser.parse(Path("chimp_20150731_22_12.extension")) datetime.datetime(2015, 7, 31, 22, 12, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is an absolute path of type `str`. >>> ChimpFilePathParser.parse("/home/user/dir/prefix_20150731_22_12.extension") datetime.datetime(2015, 7, 31, 22, 12, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is a relative path of type `str` and does not have an extension. >>> ChimpFilePathParser.parse("seviri_20150731_22_12") datetime.datetime(2015, 7, 31, 22, 12, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is a relative path of type `str` and its extension is numeric, i.e. `72`. >>> ChimpFilePathParser.parse("p_20150731_22_1272") datetime.datetime(2015, 7, 31, 22, 12, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is invalid (missing prefix). The following will raise an exception! >>> # FilePathParser.parse("20150731_22_12")
>>> # Input is invalid (empty prefix). The following will raise an exception! >>> # FilePathParser.parse("_20150731_22_12")
- class monkey_wrench.date_time._parser.HritFilePathParser[source]
Bases:
DateTimeParserBaseStatic parser class for HRIT file paths.
- static parse(filepath: Annotated[Path | T, AfterValidator(func=_validate_path)]) datetime[source]
Parse the given filepath into a datetime object.
- Parameters:
filepath – The HRIT filepath to parse. It can be either an absolute path or a relative path (e.g. just the base name). For the parsing to be successful, the
filepathmust have the following format:<optional_path><optional_prefix><YYYYmmDDHHMM>-__. See the examples below.
Examples
>>> from pathlib import Path >>> >>> # Input is an absolute path of type `Path`. >>> HritFilePathParser.parse( ... Path("/home/user/dir/H-000-MSG3__-MSG3________-WV_073___-000008___-202503041900-__") ... ) datetime.datetime(2025, 3, 4, 19, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is a relative path of type `Path`. >>> HritFilePathParser.parse(Path("H-000-MSG3__-MSG3________-WV_073___-000008___-202503041900-__")) datetime.datetime(2025, 3, 4, 19, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is a relative path of type `str` without a prefix. >>> HritFilePathParser.parse(Path("202503041900-__")) datetime.datetime(2025, 3, 4, 19, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> # Input is invalid as it misses the mandatory trailing `-__`. The following will raise an exception! >>> # HritFilePathParser.parse(Path("202503041900"))