Exemplo n.º 1
0
def test_wrapped_indicator(tas_series):
    def indice(tas, tas2=None, thresh=0, freq="YS"):
        if tas2 is None:
            out = tas < thresh
        else:
            out = tas < tas2
        out = out.resample(time="YS").sum()
        out.attrs["units"] = "days"
        return out

    ind1 = Daily(
        realm="atmos",
        identifier="test_ind1",
        nvar=1,
        units="days",
        compute=wrapped_partial(indice, tas2=None),
    )

    ind2 = Daily(
        realm="atmos",
        identifier="test_ind2",
        nvar=2,
        units="days",
        compute=wrapped_partial(indice, thresh=None),
    )

    tas = tas_series(np.arange(366), start="2000-01-01")
    tas2 = tas_series(1 + np.arange(366), start="2000-01-01")

    assert ind2(tas, tas2) == 366
    assert ind1(tas, thresh=1111) == 366
Exemplo n.º 2
0
def test_wrapped_partial():
    def func(a, b=1, c=1):
        """Docstring"""
        return (a, b, c)

    newf = wrapped_partial(func, b=2)
    assert list(signature(newf).parameters.keys()) == ["a", "c"]
    assert newf(1) == (1, 2, 1)

    newf = wrapped_partial(func, suggested=dict(c=2), b=2)
    assert list(signature(newf).parameters.keys()) == ["a", "c"]
    assert newf(1) == (1, 2, 2)
    assert newf.__doc__ == func.__doc__

    def func(a, b=1, c=1, **kws):
        """Docstring"""
        return (a, b, c)

    newf = wrapped_partial(func, suggested=dict(c=2), a=2, b=2)
    assert list(signature(newf).parameters.keys()) == ["c", "kws"]
    assert newf() == (2, 2, 2)
Exemplo n.º 3
0
    standard_name="air_temperature",
    long_name="Minimum daily minimum temperature",
    description="{freq} minimum of daily minimum temperature.",
    cell_methods="time: minimum within days time: minimum over days",
    compute=indices.tn_min,
)

daily_temperature_range = TasminTasmax(
    title="Mean of daily temperature range.",
    identifier="dtr",
    units="K",
    standard_name="air_temperature",
    long_name="Mean Diurnal Temperature Range",
    description="{freq} mean diurnal temperature range.",
    cell_methods="time range within days time: mean over days",
    compute=wrapped_partial(indices.daily_temperature_range, op="mean"),
)

max_daily_temperature_range = TasminTasmax(
    title="Maximum of daily temperature range.",
    identifier="dtrmax",
    units="K",
    standard_name="air_temperature",
    long_name="Maximum Diurnal Temperature Range",
    description="{freq} maximum diurnal temperature range.",
    cell_methods="time range within days time: max over days",
    compute=wrapped_partial(indices.daily_temperature_range, op="max"),
)

daily_temperature_range_variability = TasminTasmax(
    identifier="dtrvar",
Exemplo n.º 4
0
relative_humidity_from_dewpoint = Converter(
    identifier="rh_fromdewpoint",
    _nvar=2,
    units="%",
    long_name="Relative Humidity",
    standard_name="relative_humidity",
    title="Relative humidity from temperature and dewpoint temperature.",
    description=lambda **kws:
    ("Computed from temperature, and dew point temperature through the "
     "saturation vapor pressures, which were calculated "
     "according to the {method} method.") +
    (" The computation was done in reference to ice for temperatures below {ice_thresh}."
     if kws["ice_thresh"] is not None else ""),
    compute=wrapped_partial(
        indices.relative_humidity,
        huss=None,
        ps=None,
        invalid_values="mask",
    ),
)

relative_humidity = Converter(
    identifier="rh",
    _nvar=3,
    units="%",
    long_name="Relative Humidity",
    standard_name="relative_humidity",
    title="Relative humidity from temperature, pressure and specific humidity.",
    description=lambda **kws:
    ("Computed from temperature, specific humidity and pressure through the "
     "saturation vapor pressure, which was calculated from temperature "
     "according to the {method} method.") +
Exemplo n.º 5
0
    long_name="Average precipitation during Wet Days (SDII)",
    description=
    "{freq} Simple Daily Intensity Index (SDII) : {freq} average precipitation "
    "for days with daily precipitation over {thresh}.",
    cell_methods="",
    compute=indices.daily_pr_intensity,
)

precip_accumulation = Pr(
    identifier="prcptot",
    units="mm",
    standard_name="lwe_thickness_of_precipitation_amount",
    long_name="Total precipitation",
    description="{freq} total precipitation",
    cell_methods="time: sum within days time: sum over days",
    compute=wrapped_partial(indices.precip_accumulation, phase=None),
)

liquid_precip_accumulation = Pr(
    identifier="liquidprcptot",
    units="mm",
    standard_name="lwe_thickness_of_liquid_precipitation_amount",
    long_name="Total liquid precipitation",
    description=
    "{freq} total liquid precipitation, estimated as precipitation when daily average temperature >= 0°C",
    cell_methods="time: sum within days time: sum over days",
    compute=wrapped_partial(indices.precip_accumulation, phase="liquid"),
)

solid_precip_accumulation = Pr(
    identifier="solidprcptot",
Exemplo n.º 6
0
    units="",
    standard_name="{dist} parameters",
    long_name="{dist} distribution parameters",
    description="Parameters of the {dist} distribution",
    title="Distribution parameters fitted over the time dimension.",
    cell_methods="time: fit",
    compute=_fit,
)

doy_qmax = Streamflow(
    identifier="doy_qmax",
    var_name="q{indexer}_doy_qmax",
    long_name="Day of the year of the maximum over {indexer}",
    description="Day of the year of the maximum over {indexer}",
    title="Day of year of the maximum.",
    units="",
    _partial=True,
    compute=wrapped_partial(generic.select_resample_op, op=generic.doymax),
)

doy_qmin = Streamflow(
    identifier="doy_qmin",
    var_name="q{indexer}_doy_qmin",
    long_name="Day of the year of the minimum over {indexer}",
    description="Day of the year of the minimum over {indexer}",
    title="Day of year of the minimum.",
    units="",
    _partial=True,
    compute=wrapped_partial(generic.select_resample_op, op=generic.doymin),
)
Exemplo n.º 7
0
def __build_icclim(mode="warn"):
    from xclim import indices
    from xclim.core.utils import wrapped_partial

    #  ['SD', 'SD1', 'SD5cm', 'SD50cm',

    # TODO : Complete mappings for ICCLIM indices
    mapping = {
        "TG": indices.tg_mean,
        "TX": indices.tx_mean,
        "TN": indices.tn_mean,
        "TG90p": indices.tg90p,
        "TG10p": indices.tg10p,
        "TGx": indices.tg_max,
        "TGn": indices.tg_min,
        "TX90p": indices.tx90p,
        "TX10p": indices.tx10p,
        "TXx": indices.tx_max,
        "TXn": indices.tx_min,
        "TN90p": indices.tn90p,
        "TN10p": indices.tn10p,
        "TNx": indices.tn_max,
        "TNn": indices.tn_min,
        "CSDI": indices.cold_spell_duration_index,
        "SU": indices.tx_days_above,
        "CSU": indices.maximum_consecutive_tx_days,
        "TR": indices.tropical_nights,
        "GD4": wrapped_partial(indices.growing_degree_days, thresh="4 degC"),
        "FD": indices.frost_days,
        "CFD": indices.consecutive_frost_days,
        "GSL": indices.growing_season_length,
        "ID": indices.ice_days,
        "HD17": wrapped_partial(indices.heating_degree_days, thresh="17 degC"),
        "CDD": indices.maximum_consecutive_dry_days,
        "CWD": indices.maximum_consecutive_wet_days,
        "PRCPTOT": indices.precip_accumulation,
        "RR1": indices.wetdays,
        "SDII": wrapped_partial(indices.daily_pr_intensity, thresh="1 mm/day"),
        "ETR": indices.extreme_temperature_range,
        "DTR": indices.daily_temperature_range,
        "vDTR": indices.daily_temperature_range_variability,
        "R10mm": wrapped_partial(indices.wetdays, thresh="10 mm/day"),
        "R20mm": wrapped_partial(indices.wetdays, thresh="20 mm/day"),
        "RX1day": indices.max_1day_precipitation_amount,
        "RX5day": wrapped_partial(indices.max_n_day_precipitation_amount, window=5),
        "WSDI": indices.warm_spell_duration_index,
        "R75p": wrapped_partial(indices.days_over_precip_thresh, thresh="1 mm/day"),
        "R95p": wrapped_partial(indices.days_over_precip_thresh, thresh="1 mm/day"),
        "R99p": wrapped_partial(indices.days_over_precip_thresh, thresh="1 mm/day"),
        "R75pTOT": wrapped_partial(
            indices.fraction_over_precip_thresh, thresh="1 mm/day"
        ),
        "R95pTOT": wrapped_partial(
            indices.fraction_over_precip_thresh, thresh="1 mm/day"
        ),
        "R99pTOT": wrapped_partial(
            indices.fraction_over_precip_thresh, thresh="1 mm/day"
        ),
        # 'SD': None,
        # 'SD1': None,
        # 'SD5cm': None,
        # 'SD50cm': None,
    }

    mod = build_module(
        "xclim.icclim",
        mapping,
        doc="""
            ==============
            ICCLIM indices
            ==============
            The European Climate Assessment & Dataset project (`ECAD`_) defines
            a set of 26 core climate indides. Those have been made accessible
            directly in xclim through their ECAD name for compatibility. Hoewever,
            the methods in this module are only wrappers around the corresponding
            methods of  `xclim.indices`. Note that none of the checks performed by
            the `xclim.utils.Indicator` class (like with `xclim.atmos` indicators)
            are performed in this module.

            .. _ECAD: https://www.ecad.eu/
            """,
        mode=mode,
    )
    return mod
Exemplo n.º 8
0
def __build_anuclim(mode="warn"):
    from xclim import indices
    from xclim.core.utils import wrapped_partial

    mapping = {
        "P1_AnnMeanTemp": ensure_annual(indices.tg_mean),
        "P2_MeanDiurnalRange": ensure_annual(indices.daily_temperature_range),
        "P3_Isothermality": ensure_annual(indices.isothermality),
        "P4_TempSeasonality": indices.temperature_seasonality,
        "P5_MaxTempWarmestPeriod": ensure_annual(indices.tx_max),
        "P6_MinTempColdestPeriod": ensure_annual(indices.tn_min),
        "P7_TempAnnualRange": ensure_annual(indices.extreme_temperature_range),
        "P8_MeanTempWettestQuarter": ensure_annual(
            wrapped_partial(indices.tg_mean_wetdry_quarter, op="wettest")
        ),
        "P9_MeanTempDriestQuarter": ensure_annual(
            wrapped_partial(indices.tg_mean_wetdry_quarter, op="driest")
        ),
        "P10_MeanTempWarmestQuarter": ensure_annual(
            wrapped_partial(indices.tg_mean_warmcold_quarter, op="warmest")
        ),
        "P11_MeanTempColdestQuarter": ensure_annual(
            wrapped_partial(indices.tg_mean_warmcold_quarter, op="coldest")
        ),
        "P12_AnnualPrecip": ensure_annual(indices.prcptot),
        "P13_PrecipWettestPeriod": ensure_annual(
            wrapped_partial(indices.prcptot_wetdry_period, op="wettest")
        ),
        "P14_PrecipDriestPeriod": ensure_annual(
            wrapped_partial(indices.prcptot_wetdry_period, op="driest")
        ),
        "P15_PrecipSeasonality": indices.precip_seasonality,
        "P16_PrecipWettestQuarter": ensure_annual(
            wrapped_partial(indices.prcptot_wetdry_quarter, op="wettest")
        ),
        "P17_PrecipDriestQuarter": ensure_annual(
            wrapped_partial(indices.prcptot_wetdry_quarter, op="driest")
        ),
        "P18_PrecipWarmestQuarter": ensure_annual(
            wrapped_partial(indices.prcptot_warmcold_quarter, op="warmest")
        ),
        "P19_PrecipColdestQuarter": ensure_annual(
            wrapped_partial(indices.prcptot_warmcold_quarter, op="coldest")
        ),
    }

    mod = build_module(
        "xclim.anuclim",
        mapping,
        doc="""
                ==============
                ANUCLIM indices
                ==============

                The ANUCLIM (v6.1) software package' BIOCLIM sub-module produces a set of Bioclimatic
                parameters derived values of temperature and precipitation. The methods in this module
                are wrappers around a subset of corresponding methods of `xclim.indices`. Note that none
                of the checks performed by the `xclim.utils.Indicator` class (like with `xclim.atmos`
                indicators) are performed in this module.

                Futhermore, according to the ANUCLIM user-guide https://fennerschool.anu.edu.au/files/anuclim61.pdf (ch. 6),
                input values should be at a weekly (or monthly) frequency.  However, the xclim.indices
                implementation here will calculate the result with input data of any frequency.

                .. _ANUCLIM: https://fennerschool.anu.edu.au/files/anuclim61.pdf (ch. 6)
                """,
        mode=mode,
    )
    return mod
Exemplo n.º 9
0
    "{freq} maximum precipitation intensity over rolling {window}h window.",
    cell_methods="time: max",
    compute=indices.max_pr_intensity,
    duration="{window}",
    keywords="IDF curves",
)

precip_accumulation = Pr(
    title="Accumulated total precipitation (solid and liquid)",
    identifier="prcptot",
    units="mm",
    standard_name="lwe_thickness_of_precipitation_amount",
    long_name="Total precipitation",
    description="{freq} total precipitation",
    cell_methods="time: sum within days time: sum over days",
    compute=wrapped_partial(indices.precip_accumulation, tas=None, phase=None),
)

liquid_precip_accumulation = PrTasx(
    title="Accumulated liquid precipitation.",
    identifier="liquidprcptot",
    units="mm",
    standard_name="lwe_thickness_of_liquid_precipitation_amount",
    long_name="Total liquid precipitation",
    description=
    "{freq} total liquid precipitation, estimated as precipitation when temperature >= {thresh}",
    cell_methods="time: sum within days time: sum over days",
    compute=wrapped_partial(
        indices.precip_accumulation, suggested={"tas": _empty},
        phase="liquid"),  # _empty is added to un-optionalize the argument.
)
Exemplo n.º 10
0
    long_name="Daily mean temperature",
    description="Estimated mean temperature from maximum and minimum temperatures",
    cell_methods="time: mean within days",
    compute=indices.tas,
)


wind_speed_from_vector = Converter(
    identifier="sfcWind",
    _nvar=2,
    units="m s-1",
    standard_name="wind_speed",
    description="Wind speed computed as the magnitude of the (uas, vas) vector.",
    long_name="Near-Surface Wind Speed",
    cell_methods="",
    compute=wrapped_partial(indices.uas_vas_2_sfcwind, return_direction=False),
)


saturation_vapor_pressure = Converter(
    identifier="e_sat",
    _nvar=1,
    units="Pa",
    long_name="Saturation vapor pressure",
    description=lambda **kws: (
        "The saturation vapor pressure was calculated from a temperature "
        "according to the {method} method."
    )
    + (
        " The computation was done in reference to ice for temperatures below {ice_thresh}."
        if kws["ice_thresh"] is not None
Exemplo n.º 11
0
    standard_name="air_temperature",
    long_name="Minimum daily minimum temperature",
    description="{freq} minimum of daily minimum temperature.",
    cell_methods="time: minimum within days time: minimum over days",
    compute=indices.tn_min,
)

daily_temperature_range = TasminTasmax(
    title="Mean of daily temperature range.",
    identifier="dtr",
    units="K",
    standard_name="air_temperature",
    long_name="Mean Diurnal Temperature Range",
    description="{freq} mean diurnal temperature range.",
    cell_methods="time range within days time: mean over days",
    compute=wrapped_partial(indices.daily_temperature_range, op="mean"),
)

max_daily_temperature_range = TasminTasmax(
    title="Maximum of daily temperature range.",
    identifier="dtrmax",
    units="K",
    standard_name="air_temperature",
    long_name="Maximum Diurnal Temperature Range",
    description="{freq} maximum diurnal temperature range.",
    cell_methods="time range within days time: max over days",
    compute=wrapped_partial(indices.daily_temperature_range, op="max"),
)

daily_temperature_range_variability = TasminTasmax(
    identifier="dtrvar",