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.
from birdy import WPSClient
from pkg_resources import resource_filename
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 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('../')
# NBVAL_IGNORE_OUTPUT
url = get_target_url("chickadee")
print(f"Using chickadee on {url}")
Using chickadee on https://docker-dev03.pcic.uvic.ca/twitcher/ows/proxy/chickadee/wps
chickadee = WPSClient(url)
# 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, output_file='output.rda', vector_name='output_vector', ) 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 output_file : string Filename to store the output Rdata (extension .rda) 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: ~/code/birds/chickadee/</tmp/chickadee-venv/lib/python3.8/site-packages/birdy/client/base.py-0> Type: method
with NamedTemporaryFile(
suffix=".rda", prefix="weights_", dir="/tmp", delete=True
) as output_file:
output = chickadee.ca(
gcm_file=resource_filename("tests", "/data/tiny_gcm.nc"),
obs_file=resource_filename("tests", "/data/tiny_obs.nc"),
varname="tasmax",
end_date=date(1972, 12, 31),
output_file=output_file.name,
vector_name="analogues_output"
)
url = output.get()[0]
Access the output with rda_to_vector() or auto_construct_outputs() from wps_tools.output_handling
# NBVAL_IGNORE_OUTPUT
from wps_tools.output_handling import rda_to_vector, auto_construct_outputs
rda_to_vector(url, "analogues_output")
indices |
|
---|---|
weights |
|
# NBVAL_IGNORE_OUTPUT
auto_construct_outputs(output.get())
[R object with classes: ('list',) mapped to: [ListSexpVector, ListSexpVector] indices: <class 'rpy2.rinterface.ListSexpVector'> <rpy2.rinterface.ListSexpVector object at 0x7fa5d6763c40> [RTYPES.VECSXP] weights: <class 'rpy2.rinterface.ListSexpVector'> <rpy2.rinterface.ListSexpVector object at 0x7fa5d6768400> [RTYPES.VECSXP]]
test_analogues(
url, "analogues_output", "analogues.rda", "analogues"
)