Skip to content

Commit

Permalink
Add observations parser (#65)
Browse files Browse the repository at this point in the history
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
4 people authored Sep 14, 2021
1 parent 666caad commit b0dae45
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
44 changes: 44 additions & 0 deletions ecgtools/parsers/observations.py
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 added sample_data/cesm_obs/AIRS_01_climo.nc
Binary file not shown.
Binary file added sample_data/cesm_obs/MODIS_ANN_climo.nc
Binary file not shown.
35 changes: 35 additions & 0 deletions tests/test_obs.py
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)

0 comments on commit b0dae45

Please sign in to comment.