wps_ca¶

A process that runs the ca.netcdf.wrapper function from ClimDown.

Constructed Analogue (CA) downscaling algorithm: Starts by spatially aggregating high-resolution gridded observations up to the scale of a GCM. Then it proceeds to bias correcting the GCM based on those observations. Finally, it conducts the search for temporal analogues. This involves taking each timestep in the GCM and searching for the top 30 closest timesteps in the gridded observations. For each of the 30 closest "analogue" timesteps, CA records the integer number of the timestep (indices) and a weight for each of the analogues.

In [1]:
from birdy import WPSClient
from importlib.resources import files
from wps_tools.testing import get_target_url
from tempfile import NamedTemporaryFile
from datetime import date
from urllib.request import urlopen
import requests
import os

from wps_tools.output_handling import auto_construct_outputs
from chickadee.utils import test_analogues

# 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.ca?
Signature:
chickadee.ca(
    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),
    num_analogues='30',
    delta_days='45',
    trimmed_mean=0.0,
    tol=0.1,
    out_file=None,
    vector_name='output_vector',
    output_formats=None,
)
Docstring:
Constructed Analogue (CA) downscaling algorithm

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')
num_cores : {'1', '2', '3', '4'}positiveInteger
    The number of cores to use for parallel execution
out_file : string
    Filename to create with the climate imprint outputs
vector_name : string
    Name to label the output vector
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
num_analogues : positiveInteger
    The number of temporal analogues that the CA algorithm will search for and match. The higher this number, the longer the execution time of the reordering step.
delta_days : positiveInteger
    An integer describing the size of the window on either side of a day - Not recommeded to change
trimmed_mean : float
    The fraction (0 to 0.5) of observations to be trimmed from each extreme of the distribution before the mean is computed - Not recommended to change
tol : float
    Tuning parameter used in ridge regression to calculate weights - Not recommended to change

Returns
-------
rda_output : ComplexData:mimetype:`application/x-gzip`
    Rda file containing R output data
File:      ~/github/pcic/chickadee/</tmp/chickadee-venv/lib/python3.8/site-packages/birdy/client/base.py-0>
Type:      method

Run CA process¶

In [5]:
with NamedTemporaryFile(
    suffix=".rda", prefix="weights_", dir="/tmp", delete=True
) as output_file:
    output = chickadee.ca(
        gcm_file = (files("tests") / "data/tiny_gcm.nc").resolve(),
        obs_file = (files("tests") / "data/tiny_obs.nc").resolve(),
        varname="tasmax", 
        end_date=date(1972, 12, 31),
        out_file=output_file.name,
        vector_name="analogues_output"
    )
url = output.get()[0]

Access the output with auto_construct_outputs from wps_tools.output_handling

In [6]:
# NBVAL_IGNORE_OUTPUT
auto_construct_outputs(output.get())
Out[6]:
['https://marble-dev01.pcic.uvic.ca/wpsoutputs/744ac1c8-303f-11f0-a57a-0242ac120006/weights_fk4bxfne.rda']

Test for expected output¶

In [7]:
test_analogues(
    url, "analogues_output", "analogues.rda", "analogues"
)