Skip to content

Commit

Permalink
Merge pull request #104 from bird-house/fix-output-drs-filename
Browse files Browse the repository at this point in the history
Fix output drs filename
  • Loading branch information
David Caron authored Mar 12, 2020
2 parents df5c706 + b5bfe49 commit 0846976
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# vim:set ft=dockerfile:
FROM continuumio/miniconda3
MAINTAINER https://github.com/bird-house/finch
LABEL Description="Finch WPS" Vendor="Birdhouse" Version="0.4.0"
LABEL Description="Finch WPS" Vendor="Birdhouse" Version="0.4.1"

# Update Debian system
RUN apt-get update && apt-get install -y \
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
# The short X.Y version.
version = ''
# The full version, including alpha/beta/rc tags.
release = '0.4.0'
release = '0.4.1'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion finch/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

__author__ = """David Huard"""
__email__ = "[email protected]"
__version__ = "__version__ = '0.4.0'"
__version__ = "__version__ = '0.4.1'"
26 changes: 25 additions & 1 deletion finch/processes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ def compute_indices(
output_dataset = xr.Dataset(
data_vars=None, coords=out.coords, attrs=global_attributes
)

# fix frequency of computed output (xclim should handle this)
if output_dataset.attrs.get("frequency") == "day" and "freq" in kwds:
conversions = {
"YS": "yr",
"MS": "mon",
"QS-DEC": "seasonal",
"AS-JUL": "seasonal",
}
output_dataset.attrs["frequency"] = conversions.get(kwds["freq"], "day")

output_dataset[out.name] = out
return output_dataset

Expand All @@ -155,10 +166,14 @@ def drs_filename(ds: xr.Dataset, variable: str = None):
"""
if variable is None:
variable = [k for k, v in ds.variables.items() if len(v.dims) >= 3][0]
variable = variable.replace("_", "-")

# CORDEX example: tas_EUR-11_ICHEC-EC-EARTH_historical_r3i1p1_DMI-HIRHAM5_v1_day
cordex_pattern = "{variable}_{domain}_{driving_model}_{experiment}_{ensemble}_{model}_{version}_{frequency}"

# CMIP5 example: tas_MPI-ESM-LR_historical_r1i1p1
cmip5_pattern = "{variable}_{model}_{experiment}_{ensemble}"

if ds.attrs["project_id"] in ("CORDEX", "EOBS"):
filename = cordex_pattern.format(
variable=variable,
Expand All @@ -183,7 +198,16 @@ def drs_filename(ds: xr.Dataset, variable: str = None):
ensemble=ensemble,
)
else:
raise Exception(f"Unknown project: {ds.attrs['project_id']}")
params = [
variable,
ds.attrs.get("frequency"),
ds.attrs.get("model_id"),
ds.attrs.get("driving_model_id"),
ds.attrs.get("experiment_id", "").replace(",", "+"),
ds.attrs.get("driving_experiment_id", "").replace(",", "+"),
]
params = [k for k in params if k]
filename = "_".join(params)

if "time" in ds:
date_from = ds.time[0].values
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.4.0
current_version = 0.4.1
commit = True
tag = True

Expand Down
9 changes: 9 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ def test_drs_filename():
assert filename == "tasmax_bcc-csm1-1_historical+rcp85_r1i1p1_19500101-19500410.nc"


def test_drs_filename_unknown_project():
ds = xr.open_dataset(
test_data / "bccaqv2_subset_sample/tasmax_bcc-csm1-1_subset.nc"
)
ds.attrs["project_id"] = "unknown"
filename = drs_filename(ds)
assert filename == "tasmax_day_bcc-csm1-1_historical+rcp85_19500101-19500410.nc"


def test_drs_filename_cordex():
ds = xr.open_dataset(test_data / "cordex_subset.nc")
filename = drs_filename(ds)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_wps_xclim_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ def test_processes(client, netcdf_datasets):
date_start = pd.to_datetime(str(ds.time[0].values))
date_end = pd.to_datetime(str(ds.time[-1].values))

expected = f"{output_variable}_{model}_{experiment}_{ensemble}_{date_start:%Y%m%d}-{date_end:%Y%m%d}.nc"
expected = (
f"{output_variable.replace('_', '-')}_"
f"{model}_{experiment}_{ensemble}_"
f"{date_start:%Y%m%d}-{date_end:%Y%m%d}.nc"
)
assert Path(outputs[0]).name == expected


Expand Down Expand Up @@ -128,12 +132,14 @@ def test_heat_wave_frequency_window_thresh_parameters(client, netcdf_datasets):
wps_input_file("tasmax", netcdf_datasets["tasmax"]),
wps_input_file("tasmin", netcdf_datasets["tasmin"]),
wps_literal_input("window", "3"),
wps_literal_input("freq", "YS"),
wps_literal_input("thresh_tasmin", "20 degC"),
wps_literal_input("thresh_tasmax", "25 degC"),
]
outputs = execute_process(client, identifier, inputs)
ds = xr.open_dataset(outputs[0])

assert ds.attrs["frequency"] == "yr"
assert ds.heat_wave_frequency.standard_name == _get_output_standard_name(identifier)


Expand Down

0 comments on commit 0846976

Please sign in to comment.