Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract frequency from file #27

Merged
merged 2 commits into from
Jun 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 42 additions & 37 deletions ecgtools/parsers/cesm.py
Original file line number Diff line number Diff line change
@@ -9,55 +9,54 @@
from .utilities import extract_attr_with_regex

_STREAMS_DICT = {
'cam.h0': {'component': 'atm', 'frequency': 'month_1'},
'cam.h1': {'component': 'atm', 'frequency': 'day_1'},
'cam.h2': {'component': 'atm', 'frequency': 'hour_6'},
'cam.h3': {'component': 'atm', 'frequency': 'hour_3'},
'cam.h4': {'component': 'atm', 'frequency': 'hour_1'},
'cam.h5': {'component': 'atm', 'frequency': 'subhour_3'},
'cam.h6': {'component': 'atm', 'frequency': 'day_1'},
'cam.h7': {'component': 'atm', 'frequency': 'day_5'},
'cam.h8': {'component': 'atm', 'frequency': 'day_10'},
'clm2.h0': {'component': 'lnd', 'frequency': 'month_1'},
'clm.h1': {'component': 'lnd', 'frequency': 'month_1'},
'clm.h2': {'component': 'lnd', 'frequency': 'month_1'},
'clm.h3': {'component': 'lnd', 'frequency': 'day_365'},
'clm.h4': {'component': 'lnd', 'frequency': 'day_365'},
'clm.h5': {'component': 'lnd', 'frequency': 'day_1'},
'clm.h6': {'component': 'lnd', 'frequency': 'day_1'},
'clm.h7': {'component': 'lnd', 'frequency': 'hour_3'},
'clm.h8': {'component': 'lnd', 'frequency': 'day_1'},
'mosart.h0': {'component': 'rof', 'frequency': 'month_1'},
'mosart.h1': {'component': 'rof', 'frequency': 'day_1'},
'mosart.h2': {'component': 'rof', 'frequency': 'hour_6'},
'mosart.h3': {'component': 'rof', 'frequency': 'hour_3'},
'pop.h': {'component': 'ocn', 'frequency': 'month_1'},
'pop.h.nday1': {'component': 'ocn', 'frequency': 'day_1'},
'pop.h.nyear1': {'component': 'ocn', 'frequency': 'year_1'},
'pop.h.ecosys': {'component': 'ocn', 'frequency': 'month_1'},
'pop.h.ecosys.nday1': {'component': 'ocn', 'frequency': 'day_1'},
'pop.h.ecosys.nyear1': {'component': 'ocn', 'frequency': 'year_1'},
'cice.h': {'component': 'ice', 'frequency': 'month_1'},
'cice.h1': {'component': 'ice', 'frequency': 'day_1'},
'cism.h': {'component': 'glc', 'frequency': 'year_1'},
'cism.h1': {'component': 'glc', 'frequency': 'month_1'},
'ww3.h': {'component': 'wav', 'frequency': 'month_1'},
'cam.h0': {'component': 'atm'},
'cam.h1': {'component': 'atm'},
'cam.h2': {'component': 'atm'},
'cam.h3': {'component': 'atm'},
'cam.h4': {'component': 'atm'},
'cam.h5': {'component': 'atm'},
'cam.h6': {'component': 'atm'},
'cam.h7': {'component': 'atm'},
'cam.h8': {'component': 'atm'},
'clm2.h0': {'component': 'lnd'},
'clm.h1': {'component': 'lnd'},
'clm.h2': {'component': 'lnd'},
'clm.h3': {'component': 'lnd'},
'clm.h4': {'component': 'lnd'},
'clm.h5': {'component': 'lnd'},
'clm.h6': {'component': 'lnd'},
'clm.h7': {'component': 'lnd'},
'clm.h8': {'component': 'lnd'},
'mosart.h0': {'component': 'rof'},
'mosart.h1': {'component': 'rof'},
'mosart.h2': {'component': 'rof'},
'mosart.h3': {'component': 'rof'},
'pop.h': {'component': 'ocn'},
'pop.h.nday1': {'component': 'ocn'},
'pop.h.nyear1': {'component': 'ocn'},
'pop.h.ecosys': {'component': 'ocn'},
'pop.h.ecosys.nday1': {'component': 'ocn'},
'pop.h.ecosys.nyear1': {'component': 'ocn'},
'cice.h': {'component': 'ice'},
'cice.h1': {'component': 'ice'},
'cism.h': {'component': 'glc'},
'cism.h1': {'component': 'glc'},
'ww3.h': {'component': 'wav'},
}


@dataclasses.dataclass
class Stream:
name: str
component: str
frequency: str


# Make sure to sort the streams in reverse, the reverse=True is required so as
# not to confuse `pop.h.ecosys.nday1` and `pop.h.ecosys.nday1` when looping over
# the list of streams in the parsing function

CESM_STREAMS = [
Stream(name=key, component=value['component'], frequency=value['frequency'])
Stream(name=key, component=value['component'])
for key, value in sorted(_STREAMS_DICT.items(), reverse=True)
]

@@ -89,11 +88,16 @@ def parse_cesm_history(file):
info['stream'] = stream.name
z = file.stem.lower().split(extracted_stream)
info['case'] = z[0].strip('.')
info['frequency'] = stream.frequency
info['date'] = z[-1].strip('.')
break
with xr.open_dataset(file, chunks={}, decode_times=False) as ds:
time = ds.cf['T'].name
try:
time = ds.cf['time'].name
except KeyError:
time = ds.cf['T'].name
except:
print('Unable to parse time')
# If missing time bounds, fill with empty string
try:
time_bounds = ds.cf.get_bounds('time').name
except KeyError:
@@ -103,6 +107,7 @@ def parse_cesm_history(file):
for v, da in ds.variables.items()
if time in da.dims and v not in {time, time_bounds}
]
info['frequency'] = ds.attrs['time_period_freq']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @mgrover1! Can we confirm whether this time_period_freq attribute is only available in CESM2+? As far as I can remember from my conversation with @sherimickelson, older versions of CESM didn't output this attribute across all components.

info['variables'] = variables
info['path'] = str(file)
return info