wps_QDM¶

wps_QDM is a process that runs the qdm.netcdf.wrapper function of ClimDown package. To get started, first instatiate the client. Here, the client will try to connect to a remote chickadee instance using the url parameter.¶

In [1]:
import os
from birdy import WPSClient
from wps_tools.testing import get_target_url
from importlib.resources import files
from tempfile import NamedTemporaryFile
from netCDF4 import Dataset
from datetime import date

# Ensure we are in the working directory with access to the data
while os.path.basename(os.getcwd()) != "chickadee":
    os.chdir('../')
In [2]:
# NBVAL_IGNORE_OUTPUT
url = get_target_url("chickadee")
print(f"Using chickadee on {url}")
Using chickadee on https://marble-dev01.pcic.uvic.ca/twitcher/ows/proxy/chickadee/wps
In [3]:
chickadee = WPSClient(url)

Help for individual processes can be diplayed using the ? command (ex. bird.process?).¶

In [4]:
# NBVAL_IGNORE_OUTPUT
chickadee.qdm?
Signature:
chickadee.qdm(
    gcm_file,
    obs_file=None,
    varname=None,
    num_cores='4',
    loglevel='INFO',
    units_bool=True,
    n_pr_bool=True,
    tasmax_units='celsius',
    tasmin_units='celsius',
    pr_units='kg m-2 d-1',
    max_gb=1.0,
    start_date=datetime.date(1971, 1, 1),
    end_date=datetime.date(2005, 12, 31),
    multiyear=True,
    expand_multiyear=True,
    multiyear_window_length='30',
    trace=0.005,
    jitter_factor=0.01,
    pr_tau='1001',
    tasmax_tau='101',
    tasmin_tau='101',
    pr_seasonal=True,
    tasmax_seasonal=False,
    tasmin_seasonal=False,
    pr_ratio=True,
    tasmax_ratio=False,
    tasmin_ratio=False,
    out_file=None,
    output_formats=None,
)
Docstring:
High-level wrapper for Quantile Delta Mapping (QDM)

Parameters
----------
gcm_file : ComplexData:mimetype:`application/x-netcdf`, :mimetype:`application/x-ogc-dods`
    Filename of GCM simulations
obs_file : ComplexData:mimetype:`application/x-netcdf`, :mimetype:`application/x-ogc-dods`
    Filename of high-res gridded historical observations
varname : string
    Name of the NetCDF variable to downscale (e.g. 'tasmax')
out_file : string
    Filename to create with the climate imprint outputs
num_cores : {'1', '2', '3', '4'}positiveInteger
    The number of cores to use for parallel execution
loglevel : {'CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET'}string
    Logging level
units_bool : boolean
    Check the input units and convert them to the target output units
n_pr_bool : boolean
    Check for and eliminate negative precipitation values
tasmax_units : string
    Units used for tasmax in output file
tasmin_units : string
    Units used for tasmin in output file
pr_units : string
    Units used for pr in output file
max_gb : float
    Anapproximately how much RAM to use in the chunk I/O loop. It’s best to set this to about 1/3 to 1/4 of what you want the high-water mark to be
start_date : date
    Defines the stat of the calibration period
end_date : date
    Defines the end of the calibration period
multiyear : boolean
    Apply over multi-year chunks - Not recommended to change
expand_multiyear : boolean
    Fold incomplete multi-year block into previous - Not recommended to change
multiyear_window_length : positiveInteger
    Number of years to run if multiyear is true - Not recommended to change
trace : float
    Treat values below trace as left censored - Not recommended to change
jitter_factor : float
    Adds random noise to data to accomodate ties - Not recommended to change
pr_tau : positiveInteger
    Number of empirical quantiles for pr variable (NULL=sample length) - Not recommended to change
tasmax_tau : positiveInteger
    Number of empirical quantiles for tasmax variable (NULL=sample length) - Not recommended to change
tasmin_tau : positiveInteger
    Number of empirical quantiles for tasmin variable (NULL=sample length) - Not recommended to change
pr_seasonal : boolean
    Apply over sliding 3-month windows - Not recommended to change
tasmax_seasonal : boolean
    Apply over sliding 3-month windows - Not recommended to change
tasmin_seasonal : boolean
    Apply over sliding 3-month windows - Not recommended to change
pr_ratio : boolean
    Preserve relative trends in pr ratio variable - Not recommended to change
tasmax_ratio : boolean
    Preserve relative trends in tasmax ratio variable - Not recommended to change
tasmin_ratio : boolean
    Preserve relative trends in tasmin ratio variable - Not recommended to change

Returns
-------
output : ComplexData:mimetype:`application/x-netcdf`
    Output Netcdf File
File:      ~/code/birds/chickadee/</tmp/chickadee-venv/lib/python3.8/site-packages/birdy/client/base.py-3>
Type:      method

We can use the docstring to ensure we provide the appropriate parameters.¶

In [5]:
with NamedTemporaryFile(suffix=".nc", prefix="output_", dir="/tmp", delete=True) as out_file:
    output = chickadee.qdm(
        gcm_file = (files("tests") / "data/CI_expected_output.nc").resolve(),
        obs_file = (files("tests") / "data/tiny_obs.nc").resolve(),
        out_file = out_file.name,
        varname = "tasmax",
        num_cores = 2
    )
# Use asobj=True to access the output file contents as a dataset
output_dataset = output.get(asobj=True)[0]

Once the process has completed we can extract the results and ensure it is what we expected.¶

In [7]:
expected_data = Dataset((files("tests") / "data/QDM_expected_output.nc").resolve())
for key, value in expected_data.dimensions.items():
    assert output_dataset.sizes[key] == len(value)