cesm_hawc.file_index¶
Find CAM history files and read the dates in their names.
CAM history files are named <case>.cam.hN.YYYY-MM.nc (e.g. monthly
means) or <case>.cam.hN.YYYY-MM-DD-SSSSS.nc, where SSSSS is the
second of the day.
list_files¶
def list_files(directory: str, pattern: str) -> list[str]List the files in a directory that match a glob pattern.
Parameters¶
directory(str) — Directory to search.pattern(str) — Glob pattern, e.g."*.cam.h0.*.nc".
Returns¶
list of str — Matching paths, sorted.
Raises¶
FileNotFoundError— If nothing matches.
filename_date¶
def filename_date(path: str) -> str | NoneRead the date from a CAM history file name.
Parameters¶
path(str) — File path; only the file name is used.
Returns¶
str or None —
"YYYY-MM"for*.YYYY-MM.ncnames,"YYYY-MM-DD"for*.YYYY-MM-DD-SSSSS.ncnames, orNoneif the name has no date.
filename_time¶
def filename_time(path: str) -> pd.Timestamp | NoneChoose a representative time for a CAM history file from its name.
Parameters¶
path(str) — File path; only the file name is used.
Returns¶
pandas.Timestamp or None — The 15th at 12:00 for
*.YYYY-MM.ncnames; otherwise the date plus theSSSSSseconds of day (0 if absent).Noneif the name has no date.
date_in_range¶
def date_in_range(date: str, start: str | None, end: str | None) -> boolCheck whether a file date falls within a date range.
Bounds are compared at date’s own precision, so a month is in range
if any part of it is.
Parameters¶
date(str) —"YYYY-MM"or"YYYY-MM-DD", as returned byfilename_date().start,end(str or None) — Inclusive bounds as"YYYY-MM-DD";Nonemeans unbounded.
Returns¶
bool — True if
dateis within[start, end].
index_by_date¶
def index_by_date(directory: str, pattern: str) -> dict[str, str]Index daily history files by calendar date.
Parameters¶
directory(str) — Directory to search.pattern(str) — Glob pattern, e.g."*.cam.h2.*.nc". Only names ending inYYYY-MM-DD-SSSSS.ncare indexed.
Returns¶
dict of str to str —
{"YYYY-MM-DD": path}.
Raises¶
FileNotFoundError— If nothing matchespattern.
Notes¶
Keeps one file per date: if a date has several (e.g. 12-hourly output),
only the last in sorted order is kept and the rest are dropped without
warning. Use index_by_timestamp() for sub-daily output.
index_by_timestamp¶
def index_by_timestamp(directory: str, pattern: str) -> dict[pd.Timestamp, str]Index history files by date and time of day.
Unlike index_by_date(), every file is kept when there are several
per day (e.g. ...-00000.nc and ...-43200.nc for 12-hourly
output).
Parameters¶
directory(str) — Directory to search.pattern(str) — Glob pattern. Only names ending inYYYY-MM-DD-SSSSS.ncare indexed.
Returns¶
dict of pandas.Timestamp to str —
{date + SSSSS seconds: path}.
Raises¶
FileNotFoundError— If nothing matchespattern.