• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Ouranosinc / xscen / 15004884392

13 May 2025 07:17PM UTC coverage: 87.92%. First build
15004884392

Pull #590

github

web-flow
Merge 236a00d5f into ac65ee676
Pull Request #590: hurs/hurslogit conversion

5 of 8 new or added lines in 1 file covered. (62.5%)

4003 of 4553 relevant lines covered (87.92%)

6.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.5
/src/xscen/xclim_modules/conversions.py
1
"""Conversion functions for when datasets are missing particular variables and that xclim doesn't already implement."""
2

3
from __future__ import annotations  # for xclim 0.37
7✔
4

5
import xarray as xr
7✔
6
from xclim.core.units import convert_units_to, declare_units
7✔
7
from xsdba.processing import from_additive_space, to_additive_space
7✔
8

9

10
@declare_units(prsn="[precipitation]", prlp="[precipitation]")
7✔
11
def precipitation(prsn: xr.DataArray, prlp: xr.DataArray) -> xr.DataArray:
7✔
12
    """Precipitation of all phases.
13

14
    Compute the precipitation flux from all phases by adding solid and liquid precipitation.
15

16
    Parameters
17
    ----------
18
    prsn: xr.DataArray
19
      Solid precipitation flux.
20
    prlp: xr.DataArray
21
      Liquid precipitation flux.
22

23
    Returns
24
    -------
25
    xr.DataArray, [same as prsn]
26
      Surface precipitation flux (all phases)
27
    """
28
    prlp = convert_units_to(prlp, prsn, context="hydro")
7✔
29
    pr = prsn + prlp
7✔
30
    pr.attrs["units"] = prsn.attrs["units"]
7✔
31
    return pr
7✔
32

33

34
@declare_units(dtr="[temperature]", tasmax="[temperature]")
7✔
35
def tasmin_from_dtr(dtr: xr.DataArray, tasmax: xr.DataArray) -> xr.DataArray:
7✔
36
    """Tasmin computed from DTR and tasmax.
37

38
    Tasmin as dtr subtracted from tasmax.
39

40
    Parameters
41
    ----------
42
    dtr: xr.DataArray
43
      Daily temperature range
44
    tasmax: xr.DataArray
45
      Daily maximal temperature.
46

47
    Returns
48
    -------
49
    xr.DataArray, [same as tasmax]
50
      Daily minimum temperature
51
    """
52
    out_units = tasmax.units
7✔
53
    # To prevent strange behaviors that could arise from changing DTR units, we change the units of tasmax to match DTR
54
    tasmax = convert_units_to(tasmax, dtr)
7✔
55
    tasmin = tasmax - dtr
7✔
56
    tasmin.attrs["units"] = dtr.units
7✔
57
    tasmin = convert_units_to(tasmin, out_units)
7✔
58
    return tasmin
7✔
59

60

61
@declare_units(dtr="[temperature]", tasmin="[temperature]")
7✔
62
def tasmax_from_dtr(dtr: xr.DataArray, tasmin: xr.DataArray) -> xr.DataArray:
7✔
63
    """Tasmax computed from DTR and tasmin.
64

65
    Tasmax as dtr added to tasmin.
66

67
    Parameters
68
    ----------
69
    dtr: xr.DataArray
70
      Daily temperature range
71
    tasmin: xr.DataArray
72
      Daily minimal temperature.
73

74
    Returns
75
    -------
76
    xr.DataArray, [same as tasmin]
77
      Daily maximum temperature
78
    """
79
    out_units = tasmin.attrs["units"]
7✔
80
    # To prevent strange behaviors that could arise from changing DTR units, we change the units of tasmin to match DTR
81
    tasmin = convert_units_to(tasmin, dtr)
7✔
82
    tasmax = tasmin + dtr
7✔
83
    tasmax.attrs["units"] = dtr.attrs["units"]
7✔
84
    tasmax = convert_units_to(tasmax, out_units)
7✔
85
    return tasmax
7✔
86

87

88
@declare_units(tasmin="[temperature]", tasmax="[temperature]")
7✔
89
def dtr_from_minmax(tasmin: xr.DataArray, tasmax: xr.DataArray) -> xr.DataArray:
7✔
90
    """DTR computed from tasmin and tasmax.
91

92
    Dtr as tasmin subtracted from tasmax.
93

94
    Parameters
95
    ----------
96
    tasmin: xr.DataArray
97
      Daily minimal temperature.
98
    tasmax: xr.DataArray
99
      Daily maximal temperature.
100

101
    Returns
102
    -------
103
    xr.DataArray
104
      Daily temperature range
105
    """
106
    tasmin = convert_units_to(tasmin, tasmax)
7✔
107
    dtr = tasmax - tasmin
7✔
108
    dtr.attrs["units"] = tasmin.attrs["units"]
7✔
109
    dtr.attrs["units_metadata"] = "temperature: difference"
7✔
110
    return dtr
7✔
111

112

113
@declare_units(hurs="[]")
7✔
114
def hurslogit_from_hurs(hurs: xr.DataArray) -> xr.DataArray:
7✔
115
    """Hurslogit computed from hurs
116

117
    Parameters
118
    ----------
119
    hurs: xr.DataArray
120
      Daily relative humidity
121

122
    Returns
123
    -------
124
    xr.DataArray
125
      Daily relative humidity in logit space
126

127
    Notes
128
    -----
129
    This converts the range of `hurs` from [0,100] to ]-np.inf, np.inf[.
130
    """
NEW
131
    hurs = convert_units_to(hurs, "%")
×
NEW
132
    return to_additive_space(
×
133
        hurs, lower_bound="0 %", upper_bound="100 %", trans="logit"
134
    )
135

136

137
@declare_units(hurslogit="[]")
7✔
138
def hurs_from_hurslogit(hurslogit: xr.DataArray) -> xr.DataArray:
7✔
139
    """Hurslogit computed from hurs
140

141
    Parameters
142
    ----------
143
    hurslogit: xr.DataArray
144
      Daily relative humidity in logit space.
145

146
    Returns
147
    -------
148
    xr.DataArray
149
      Daily relative humidity
150

151
    Notes
152
    -----
153
    This converts the range of `hurslogit` from ]-np.inf, np.inf[ to [0,100].
154
    """
NEW
155
    return from_additive_space(
×
156
        hurslogit, lower_bound="0 %", upper_bound="100 %", trans="logit", units="%"
157
    )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc