From f496a037aa655da1053c1e4943b565d2eae5207f Mon Sep 17 00:00:00 2001 From: David Caron Date: Thu, 12 Mar 2020 16:04:31 -0400 Subject: [PATCH 1/4] fix #103 add defaults when `project_id` is unknown --- finch/processes/utils.py | 14 +++++++++++++- tests/test_utils.py | 9 +++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/finch/processes/utils.py b/finch/processes/utils.py index 97d39b66..83e9da92 100644 --- a/finch/processes/utils.py +++ b/finch/processes/utils.py @@ -155,10 +155,13 @@ 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] + # 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, @@ -183,7 +186,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 diff --git a/tests/test_utils.py b/tests/test_utils.py index e388370b..c760954c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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) From 3c6e54f661ecb07bd842deef87f97bcc1532371c Mon Sep 17 00:00:00 2001 From: David Caron Date: Thu, 12 Mar 2020 16:10:30 -0400 Subject: [PATCH 2/4] use dash instead of underscores in drs filenames --- finch/processes/utils.py | 1 + tests/test_wps_xclim_indices.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/finch/processes/utils.py b/finch/processes/utils.py index 83e9da92..12a19f42 100644 --- a/finch/processes/utils.py +++ b/finch/processes/utils.py @@ -155,6 +155,7 @@ 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}" diff --git a/tests/test_wps_xclim_indices.py b/tests/test_wps_xclim_indices.py index d0309a47..4a5efeff 100644 --- a/tests/test_wps_xclim_indices.py +++ b/tests/test_wps_xclim_indices.py @@ -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 From 08793c549948e72507e7e6d8548159996a9f7dd8 Mon Sep 17 00:00:00 2001 From: David Caron Date: Thu, 12 Mar 2020 16:31:03 -0400 Subject: [PATCH 3/4] fix #80 frequency of computed datasets --- finch/processes/utils.py | 11 +++++++++++ tests/test_wps_xclim_indices.py | 2 ++ 2 files changed, 13 insertions(+) diff --git a/finch/processes/utils.py b/finch/processes/utils.py index 12a19f42..ad26cce9 100644 --- a/finch/processes/utils.py +++ b/finch/processes/utils.py @@ -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 diff --git a/tests/test_wps_xclim_indices.py b/tests/test_wps_xclim_indices.py index 4a5efeff..a6137597 100644 --- a/tests/test_wps_xclim_indices.py +++ b/tests/test_wps_xclim_indices.py @@ -132,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) From b5bfe49ba7e470f2eec1950c4de379733f0ab0a9 Mon Sep 17 00:00:00 2001 From: David Caron Date: Thu, 12 Mar 2020 16:39:37 -0400 Subject: [PATCH 4/4] =?UTF-8?q?Bump=20version:=200.4.0=20=E2=86=92=200.4.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- docs/source/conf.py | 2 +- finch/__version__.py | 2 +- setup.cfg | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 711fb1c1..615d9f53 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ diff --git a/docs/source/conf.py b/docs/source/conf.py index e34fb355..529bd13e 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -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. diff --git a/finch/__version__.py b/finch/__version__.py index 702eec1a..1a455200 100644 --- a/finch/__version__.py +++ b/finch/__version__.py @@ -6,4 +6,4 @@ __author__ = """David Huard""" __email__ = "huard.david@ouranos.ca" -__version__ = "__version__ = '0.4.0'" +__version__ = "__version__ = '0.4.1'" diff --git a/setup.cfg b/setup.cfg index da2b6789..bb8df802 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.4.0 +current_version = 0.4.1 commit = True tag = True