wps_climdex_spells¶

WPS wrapper for climdex.pcic spell duration index functions

  • Warm Spell Duration Index (climdex.wsdi) The warm spell duration index is defined as the number of days each year which are part of a "warmspell". A "warm spell" is defined as a sequence of 6 or more days in which the daily maximumtemperature exceeds the 90th percentile of daily maximum temperature for a 5-day running windowsurrounding this day during the baseline period.
  • Cold Spell Duration Index (climdex.csdi) The cold spell duration index is defined as the number of days each year which are part of a "coldspell". A "cold spell" is defined as a sequence of 6 or more days in which the daily minimumtemperature is below the 10th percentile of daily minimum temperature for a 5-day running windowsurrounding this day during the baseline period.
  • Maximum Consecutive Dry Days (climdex.cdd)This function computes the climdex index CDD: the annual maximum length of dry spells, in days. Dry spells are considered to be sequences of days where daily preciptation is less than 1mm per day.
In [1]:
import os
import requests
from birdy import WPSClient
from rpy2 import robjects
from urllib.request import urlretrieve
from importlib.resources import files
from tempfile import NamedTemporaryFile

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

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

In [5]:
# NBVAL_IGNORE_OUTPUT
quail.climdex_spells?
Signature:
quail.climdex_spells(
    climdex_input,
    func=None,
    span_years=False,
    loglevel='INFO',
    output_file='output.rda',
    output_formats=None,
)
Docstring:
Cold (csdi) or warm (wsdi) spell duration index and maximum consecutive dry (cdd) or wet (cwd) days

Parameters
----------
climdex_input : ComplexData:mimetype:`application/x-gzip`
    RDS or Rdata (.rds, .rda, .rdata) file containing R Object of type climdexInput
output_file : string
    Filename to store the output Rdata (extension .rda)
func : {'wsdi', 'csdi', 'cdd', 'cwd'}string
    Compute climdex.wsdi (Warm spell duration index)
span_years : boolean
    Specifies whether spells can cross year boundaries
loglevel : {'CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET'}string
    Logging level

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

Run wps_climdex_spells Process for climdex.wsdi(), Warm Spell Duration Index¶

without spells spanning years

In [6]:
with NamedTemporaryFile(suffix=".rda", prefix="wsdi_", dir="/tmp", delete=True) as output_file:
    output = quail.climdex_spells(
            climdex_input=(files("tests") / "data/climdexInput.rda").resolve(),
            func="wsdi",
            output_file=output_file.name,
        )
wsdi_url = output.get()[0]

with spells spanning years

In [7]:
with NamedTemporaryFile(suffix=".rda", prefix="wsdi_", dir="/tmp", delete=True) as output_file:
    output = quail.climdex_spells(
            climdex_input=(files("tests") / "data/climdexInput.rda").resolve(),
            func="wsdi",
            span_years=True,
            output_file=output_file.name,
        )
wsdi_span_yrs_url = output.get()[0]

Run wps_climdex_spells Process for climdex.csdi(), Cold Spell Duration Index¶

without spells spanning years

In [8]:
with NamedTemporaryFile(suffix=".rda", prefix="csdi_", dir="/tmp", delete=True) as output_file:
    output = quail.climdex_spells(
            climdex_input=(files("tests") / "data/climdexInput.rda").resolve(),
            func="csdi",
            output_file=output_file.name,
        )
csdi_url = output.get()[0]

with spells spanning years

In [9]:
with NamedTemporaryFile(suffix=".rda", prefix="csdi_", dir="/tmp", delete=True) as output_file:
    output = quail.climdex_spells(
            climdex_input=(files("tests") / "data/climdexInput.rda").resolve(),
            func="csdi",
            span_years=True,
            output_file=output_file.name,
        )
csdi_span_yrs_url = output.get()[0]

Run wps_climdex_spells Process for climdex.cdd() Maximum Consecutive Dry Days¶

without spells spanning years

In [10]:
with NamedTemporaryFile(suffix=".rda", prefix="cdd_", dir="/tmp", delete=True) as output_file:
    output = quail.climdex_spells(
            climdex_input=(files("tests") / "data/climdexInput.rda").resolve(),
            func="cdd",
            output_file=output_file.name,
        )
cdd_url = output.get()[0]

with spells spanning years

In [11]:
with NamedTemporaryFile(suffix=".rda", prefix="cdd_", dir="/tmp", delete=True) as output_file:
    output = quail.climdex_spells(
            climdex_input=(files("tests") / "data/climdexInput.rda").resolve(),
            func="cdd",
            span_years=True,
            output_file=output_file.name,
        )
cdd_span_yrs_url = output.get()[0]

Run wps_climdex_spells Process for climdex.cwd() Maximum Consecutive Wet Days with rds input¶

without spells spanning years

In [12]:
with NamedTemporaryFile(suffix=".rda", prefix="cwd_", dir="/tmp", delete=True) as output_file:
    output = quail.climdex_spells(
            climdex_input=(files("tests") / "data/climdexInput.rds").resolve(),
            func="cwd",
            output_file=output_file.name,
        )
cwd_url = output.get()[0]

with spells spanning years and multiple inputs

In [13]:
climdex_inputs = [
    (files("tests") / "data/climdexInput.rds").resolve(),
    (files("tests") / "data/climdexInput.rda").resolve(),
    (files("tests") / "data/climdex_input_multiple.rda").resolve(),
]
with NamedTemporaryFile(suffix=".rda", prefix="cwd_", dir="/tmp", delete=True) as output_file:
    output = quail.climdex_spells(
            climdex_input=climdex_inputs,
            func="cwd",
            span_years=True,
            output_file=output_file.name,
        )
cwd_span_yrs_url = output.get()[0]

Access the output with rda_to_vector or construct_r_out from wps_tools.R

In [14]:
# Eg/ Maximum Consecutive Wet Days with spanning years
cwd = rda_to_vector(cwd_span_yrs_url, "cwd1_ci")
# use print() to see whole vector
print(cwd)
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 
  NA   13   NA   NA   NA    9   12   19    7   11   13    6    9   17    7   10 
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 
  11   NA   NA   13   14   10   10   NA   12   NA   NA   NA   NA   NA    9   14 
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 
  NA    8   NA   NA   14   NA   13   27   15   14   19   NA   15   NA 

In [24]:
construct_r_out([wsdi_url, csdi_url, cdd_url, cwd_url])
Out[24]:
[[R object with classes: ('numeric',) mapped to:
  [     nan, 0.000000,      nan,      nan, ..., 0.000000,      nan, 0.000000,      nan]],
 [R object with classes: ('numeric',) mapped to:
  [     nan, 0.000000,      nan,      nan, ..., 0.000000,      nan, 0.000000,      nan]],
 [R object with classes: ('numeric',) mapped to:
  [     nan, 55.000000,      nan,      nan, ..., 29.000000,      nan, 31.000000,      nan]],
 [R object with classes: ('numeric',) mapped to:
  [     nan, 13.000000,      nan,      nan, ..., 19.000000,      nan, 15.000000,      nan]]]

Test output against expected output¶

In [16]:
test_rda_output(wsdi_url, f"wsdi1_ci", "expected_spells_data.rda", f"expected_wsdi")
In [17]:
test_rda_output(wsdi_span_yrs_url, f"wsdi1_ci", "expected_spells_data.rda", f"expected_wsdi_span_yrs")
In [18]:
test_rda_output(csdi_url, f"csdi1_ci", "expected_spells_data.rda", f"expected_csdi")
In [19]:
test_rda_output(csdi_span_yrs_url, f"csdi1_ci", "expected_spells_data.rda", f"expected_csdi_span_yrs")
In [20]:
test_rda_output(cdd_url, f"cdd1_ci", "expected_spells_data.rda", f"expected_cdd_span_yrs")
In [21]:
test_rda_output(cdd_span_yrs_url, f"cdd1_ci", "expected_spells_data.rda", f"expected_cdd_span_yrs")
In [22]:
test_rda_output(cwd_url, f"cwd1_ci", "expected_spells_data.rda", f"expected_cwd")
In [23]:
test_rda_output(cwd_span_yrs_url, f"cwd1_ci", "expected_spells_data.rda", f"expected_cwd_span_yrs")