Skip to content

Commit

Permalink
Merge pull request #15 from kthyng/proto
Browse files Browse the repository at this point in the history
Updates for working prototype!
  • Loading branch information
kthyng authored Sep 15, 2021
2 parents 9f5f955 + d0654ea commit 02c0572
Show file tree
Hide file tree
Showing 19 changed files with 1,641 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.png

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
3 changes: 3 additions & 0 deletions ci/environment-py3.7.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ dependencies:
- python=3.7
############## These will have to be adjusted to your specific project
- cartopy
- cf_xarray
- extract_model
- matplotlib
- numpy
- ocean_data_gateway
- pandas
- scipy
- xarray
Expand Down
3 changes: 3 additions & 0 deletions ci/environment-py3.8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ dependencies:
- python=3.8
############## These will have to be adjusted to your specific project
- cartopy
- cf_xarray
- extract_model
- matplotlib
- numpy
- ocean_data_gateway
- pandas
- scipy
- xarray
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Welcome to ocean-model-skill-assessor's documentation!
.. toctree::
:maxdepth: 3

prototype
Demo_workflows
api


Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies:
- pytest
# Examples (remove and add as needed)
- cartopy
- cf_xarray
- extract_model
- jupyter
- jupyterlab
Expand Down
883 changes: 883 additions & 0 deletions notebooks/Demo_workflows.ipynb

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions notebooks/config_local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
approach: "stations"
loc_model: "https://thredds.cencoos.org/thredds/dodsC/CENCOOS_CA_ROMS_FCST.nc"
axds:
bbox:
criteria:
salt:
name:
"sea_water_practical_salinity$"
erddap:
figname_map: "local_cli.png"
figname_data_prefix: "local_cli_"
local:
filenames:
- "edu_humboldt_tdp.csv"
- "bodega-bay-bml_wts.nc"
only_search: false
only_searchplot: false
parallel: true
readers:
- "local"
run_qc: false
skip_units: true
stations:
time_range:
- "2021-9-1"
- "2021-9-8"
var_def:
salt:
units: "psu"
fail_span:
- -10
- 60
suspect_span:
- -1
- 45
variables:
- "salt"
xarray_kwargs:
chunks:
time:
1
depth:
1
45 changes: 45 additions & 0 deletions notebooks/config_nonlocal.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
approach: "stations"
loc_model: "https://thredds.cencoos.org/thredds/dodsC/CENCOOS_CA_ROMS_FCST.nc"
axds:
bbox:
criteria:
salt:
name:
"sea_water_practical_salinity$"
erddap:
known_server:
"ioos"
figname_map: "nonlocal_cli.png"
figname_data_prefix: "nonlocal_cli_"
local:
only_search: false
only_searchplot: false
parallel: true
readers:
- "erddap"
run_qc: false
skip_units: true
stations:
- "edu_humboldt_tdp"
- "bodega-bay-bml_wts"
time_range:
- "2021-9-1"
- "2021-9-8"
var_def:
salt:
units: "psu"
fail_span:
- -10
- 60
suspect_span:
- -1
- 45
variables:
- "salt"
xarray_kwargs:
chunks:
time:
1
depth:
1
47 changes: 47 additions & 0 deletions notebooks/config_nonlocal_region.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
approach: "region"
loc_model: "https://thredds.cencoos.org/thredds/dodsC/CENCOOS_CA_ROMS_FCST.nc"
axds:
bbox:
- -124.5
- 40
- -123.5
- 42
criteria:
salt:
name:
"sea_water_practical_salinity$"
erddap:
known_server:
"ioos"
figname_map: "nonlocal_cli_region.png"
figname_data_prefix: "nonlocal_cli__region"
local:
only_search: false
only_searchplot: false
parallel: true
readers:
- "erddap"
run_qc: false
skip_units: true
stations:
time_range:
- "2021-9-1"
- "2021-9-8"
var_def:
salt:
units: "psu"
fail_span:
- -10
- 60
suspect_span:
- -1
- 45
variables:
- "salt"
xarray_kwargs:
chunks:
time:
1
depth:
1
78 changes: 78 additions & 0 deletions ocean_model_skill_assessor/CLI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
Command Line Interface.
"""

import argparse

import ocean_data_gateway as odg
import yaml

import ocean_model_skill_assessor as omsa


parser = argparse.ArgumentParser()
parser.add_argument(
"config_file", help="Give path to configuration file. Should be yaml."
)

args = parser.parse_args()
config_file = args.config_file

with open(config_file, "r") as ymlfile:
cfg = yaml.load(ymlfile)

approach = cfg["approach"]
loc_model = cfg["loc_model"]
axds = cfg["axds"]
bbox = cfg["bbox"]
criteria = cfg["criteria"]
erddap = cfg["erddap"]
figname_map = cfg["figname_map"]
figname_data_prefix = cfg["figname_data_prefix"]
local = cfg["local"]
only_search = cfg["only_search"]
only_searchplot = cfg["only_searchplot"]
parallel = cfg["parallel"]
readers = cfg["readers"]
run_qc = cfg["run_qc"]
skip_units = cfg["skip_units"]
stations = cfg["stations"]
time_range = cfg["time_range"]
var_def = cfg["var_def"]
variables = cfg["variables"]
xarray_kwargs = cfg["xarray_kwargs"]

omsa.set_criteria(criteria)

if readers is not None:
input_readers = readers
readers = []
if "local" in input_readers:
readers.append(odg.local)
if "erddap" in input_readers:
readers.append(odg.erddap)
if "axds" in input_readers:
readers.append(odg.axds)

search = omsa.run(
approach=approach,
loc_model=loc_model,
axds=axds,
bbox=bbox,
criteria=criteria,
erddap=erddap,
figname_map=figname_map,
figname_data_prefix=figname_data_prefix,
local=local,
only_search=only_search,
only_searchplot=only_searchplot,
parallel=parallel,
readers=readers,
run_qc=run_qc,
skip_units=skip_units,
time_range=time_range,
stations=stations,
var_def=var_def,
variables=variables,
xarray_kwargs=xarray_kwargs,
)
2 changes: 2 additions & 0 deletions ocean_model_skill_assessor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import ocean_model_skill_assessor.accessor # noqa: F401

from .main import run
from .plot import map, time_series # noqa: F401
from .stats import ( # noqa: F401
compute_bias,
Expand All @@ -17,6 +18,7 @@
compute_root_mean_square_error,
compute_stats,
)
from .utils import set_criteria


try:
Expand Down
46 changes: 46 additions & 0 deletions ocean_model_skill_assessor/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@
Class to facilitate some functions directly on DataFrames.
"""

import re

import pandas as pd

import ocean_model_skill_assessor


regex = {
"time": {"name": re.compile("\\bt\\b|(time|min|hour|day|week|month|year)[0-9]*")},
"Z": {
"name": re.compile(
"(z|nav_lev|gdep|lv_|[o]*lev|bottom_top|sigma|h(ei)?ght|altitude|depth|"
"isobaric|pres|isotherm)[a-z_]*[0-9]*"
)
},
"Y": {"name": re.compile("y|j|nlat|nj")},
"latitude": {"name": re.compile("y?(nav_lat|lat|gphi)[a-z0-9]*")},
"X": {"name": re.compile("x|i|nlon|ni")},
"longitude": {"name": re.compile("x?(nav_lon|lon|glam)[a-z0-9]*")},
}
regex["T"] = regex["time"]


@pd.api.extensions.register_dataframe_accessor("omsa")
class SkillAssessorAccessor:
"""Class to facilitate some functions directly on DataFrames."""
Expand Down Expand Up @@ -42,3 +60,31 @@ def plot(self, **kwargs):
ocean_model_skill_assessor.time_series.plot(
self.df["obs"], self.df["model"], **kwargs
)


@pd.api.extensions.register_dataframe_accessor("cf")
class cf_pandas:
"""Bring cf-xarray variable and coord/axis identification to pandas!"""

def __init__(self, obj):
"""Initialize with pandas DataFrame."""
self.df = obj

def __getitem__(self, key):
"""This allows for df.cf[varname], etc."""

# combine with regex from cf-xarray just for pandas cf
criteria = ocean_model_skill_assessor.criteria
criteria.update(regex)

results = []
if key in criteria:
for criterion, patterns in criteria[key].items():
results.extend(
list(
set([var for var in self.df.columns if re.match(patterns, var)])
)
)

key = list(set(results))[0]
return self.df[key]
Loading

0 comments on commit 02c0572

Please sign in to comment.