-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Anderson Banihirwe <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxwell Grover <[email protected]>
- Loading branch information
1 parent
666caad
commit b0dae45
Showing
4 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pathlib | ||
import traceback | ||
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(temporal) == 2: | ||
month_number = int(temporal) | ||
time_period = 'monthly' | ||
temporal = datetime(2020, month_number, 1).strftime('%b').upper() | ||
|
||
elif temporal == 'ANN': | ||
time_period = 'annual' | ||
else: | ||
time_period = 'seasonal' | ||
|
||
with xr.open_dataset(file, chunks={}, decode_times=False) as ds: | ||
variable_list = [var for var in ds if 'long_name' in ds[var].attrs] | ||
|
||
info = { | ||
'source': source, | ||
'temporal': temporal, | ||
'time_period': time_period, | ||
'variable': variable_list, | ||
'path': str(file), | ||
} | ||
|
||
return info | ||
|
||
except Exception: | ||
return {INVALID_ASSET: file, TRACEBACK: traceback.format_exc()} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import os | ||
import pathlib | ||
|
||
import pandas as pd | ||
import pytest | ||
|
||
from ecgtools import Builder | ||
from ecgtools.parsers.observations import parse_amwg_obs | ||
|
||
sample_data_dir = pathlib.Path(os.path.dirname(__file__)).parent / 'sample_data' | ||
|
||
df = pd.DataFrame() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'file_path', | ||
[ | ||
sample_data_dir / 'cesm_obs' / 'AIRS_01_climo.nc', | ||
sample_data_dir / 'cesm_obs' / 'MODIS_ANN_climo.nc', | ||
], | ||
) | ||
def test_obs_parser(file_path): | ||
parse_dict = parse_amwg_obs(file_path) | ||
assert isinstance(parse_dict, dict) | ||
assert isinstance(df.append(parse_dict, ignore_index=True), pd.DataFrame) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'file_directory', | ||
[sample_data_dir / 'cesm_obs'], | ||
) | ||
def test_obs_builder(file_directory): | ||
b = Builder(file_directory) | ||
b.build(parse_amwg_obs) | ||
assert isinstance(b.df, pd.DataFrame) |