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

Add observations parser #65

Merged
merged 13 commits into from
Sep 14, 2021
2 changes: 1 addition & 1 deletion ecgtools/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _filter_files(filelist):
)

def _glob_dir(directory, extension):
return list(directory.rglob(f'*{extension}'))
return sorted(list(directory.rglob(f'*{extension}')))

filelist = joblib.Parallel(n_jobs=self.njobs, verbose=5)(
joblib.delayed(_glob_dir)(directory, self.extension) for directory in self.dirs
Expand Down
46 changes: 46 additions & 0 deletions ecgtools/parsers/observations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pathlib
import traceback
import warnings
mgrover1 marked this conversation as resolved.
Show resolved Hide resolved
from datetime import datetime

import xarray as xr

from ..builder import INVALID_ASSET, TRACEBACK


def parse_amwg_obs(file):
"""Atmospheric observational data stored in"""
file = pathlib.Path(file)
info = {}

try:
stem = file.stem
split = stem.split('_')
source = split[0]
temporal = split[-2]
if len(split[-2]) == 2:
month_number = int(temporal)
time_period = 'monthly'
temporal = datetime(2020, month_number, 1).strftime('%b').upper()
else:
time_period = 'seasonal'

with xr.open_dataset(file, chunks={}, decode_times=False) as ds:
for var in ds:
da = ds[var]
units = da.units
long_name = da.long_name
info = {
'source': source,
'temporal': temporal,
'time_period': time_period,
'variable': var,
'units': units,
'long_name': long_name,
'path': str(path),
mgrover1 marked this conversation as resolved.
Show resolved Hide resolved
}

return info

except Exception:
return {INVALID_ASSET: file, TRACEBACK: traceback.format_exc()}