def test_tg10p_simple(self, tas_series): i = 366 arr = np.asarray(np.arange(i), 'float') tas = tas_series(arr, start='1/1/2000') tasC = tas.copy() tasC -= K2C tasC.attrs['units'] = 'C' t10 = percentile_doy(tas, per=.1) # create cold spell in june tas[175:180] = 1 tasC[175:180] = 1 - K2C out = temp.tg10p(tas, t10, freq='MS') outC = temp.tg10p(tasC, t10, freq='MS') np.testing.assert_array_equal(out, outC) assert out[0] == 1 assert out[5] == 5 # nan treatment tas[33] = np.nan tasC[33] = np.nan out = temp.tg10p(tas, t10, freq='MS') outC = temp.tg10p(tasC, t10, freq='MS') np.testing.assert_array_equal(out, outC) assert out[0] == 1 assert np.isnan(out[1]) assert out[5] == 5
def test_simple(self, tas_series): a = np.zeros(365) + 17 a[:7] += [-3, -2, -1, 0, 1, 2, 3] da = tas_series(a + K2C) out = xci.heating_degree_days(da) np.testing.assert_array_equal(out[:1], 6) np.testing.assert_array_equal(out[1:], 0)
def test_missing_days(self, tas_series): a = np.arange(360.0) a[5:10] = np.nan ts = tas_series(a) out = checks.missing_any(ts, freq="MS") assert out[0] assert not out[1]
def test_tg90p_simple(self, tas_series): i = 366 arr = np.asarray(np.arange(i), "float") tas = tas_series(arr, start="1/1/2000") tasC = tas.copy() tasC -= K2C tasC.attrs["units"] = "C" t90 = percentile_doy(tas, per=0.1) # create cold spell in june tas[175:180] = 1 tasC[175:180] = 1 - K2C out = atmos.tg90p(tas, t90, freq="MS") outC = atmos.tg90p(tasC, t90, freq="MS") np.testing.assert_array_equal(out, outC) assert out[0] == 30 assert out[1] == 29 assert out[5] == 25 # nan treatment tas[33] = np.nan tasC[33] = np.nan out = atmos.tg90p(tas, t90, freq="MS") outC = atmos.tg90p(tasC, t90, freq="MS") np.testing.assert_array_equal(out, outC) assert out[0] == 30 assert np.isnan(out[1]) assert out[5] == 25
def test_no_start(self, tas_series): tg = np.zeros(365) - 1 tg = tas_series(tg, start='1/1/2000') out = xci.freshet_start(tg) np.testing.assert_equal(out, [ np.nan, ])
def test_attrs(self, tas_series): import datetime as dt a = tas_series(np.arange(360.)) ind = UniIndTemp() txm = ind(a, freq='YS') assert txm.cell_methods == 'time: mean within days time: mean within years' assert '{:%Y-%m-%d %H}'.format(dt.datetime.now()) in txm.attrs['history'] assert txm.name == "tmin0"
def test_simple(self, tas_series): a = np.zeros(365) + K2C a[10:20] -= 15 # 10 days a[40:43] -= 50 # too short -> 0 a[80:100] -= 30 # at the end and beginning ts = tas_series(a) out = temp.cold_spell_days(ts, thresh='-10 C', freq='MS') np.testing.assert_array_equal(out, [10, 0, 12, 8, 0, 0, 0, 0, 0, 0, 0, 0])
def test_temp_unit_conversion(self, tas_series): a = tas_series(np.arange(360.)) ind = UniIndTemp() txk = ind(a, freq='YS') ind.units = 'degC' txc = ind(a, freq='YS') np.testing.assert_array_almost_equal(txk, txc + 273.15)
def test_simple(self, pr_series, tas_series): pr = np.ones(450) pr = pr_series(pr, start='12/1/2000') tas = np.zeros(450) - 1 tas[10:20] += 10 tas = tas_series(tas + K2C, start='12/1/2000') out = xci.winter_rain_ratio(pr, tas=tas) np.testing.assert_almost_equal(out, [10. / (31 + 31 + 28), 0])
def test_single_year(self, tas_series): a = np.zeros(366) + K2C ts = tas_series(a, start='1/1/2000') tt = (ts.time.dt.month >= 5) & (ts.time.dt.month <= 8) offset = np.random.uniform(low=5.5, high=23, size=(tt.sum().values,)) ts[tt] = ts[tt] + offset out = temp.growing_season_length(ts) np.testing.assert_array_equal(out, tt.sum())
def test_convert_units(self, tas_series): a = np.zeros(365) a[10:20] -= 15 # 10 days a[40:43] -= 50 # too short -> 0 a[80:100] -= 30 # at the end and beginning ts = tas_series(a) ts.attrs["units"] = "C" out = atmos.cold_spell_days(ts, thresh="-10 C", freq="MS") np.testing.assert_array_equal(out, [10, 0, 12, 8, 0, 0, 0, 0, 0, 0, 0, 0])
def test_simple(self, tas_series): a = np.zeros(365) a[10:20] -= 15 # 10 days a[40:43] -= 50 # too short -> 0 a[80:100] -= 30 # at the end and beginning da = tas_series(a + K2C) out = xci.cold_spell_days(da, thresh="-10. C", freq="M") np.testing.assert_array_equal(out, [10, 0, 12, 8, 0, 0, 0, 0, 0, 0, 0, 0]) assert out.units == "days"
def test_nan_presence(self, tas_series): a = np.zeros(365) + K2C a[10:20] -= 15 # 10 days a[40:43] -= 50 # too short -> 0 a[80:100] -= 30 # at the end and beginning a[-1] = np.nan ts = tas_series(a) out = atmos.cold_spell_days(ts, thresh="-10 C", freq="MS") np.testing.assert_array_equal( out, [10, 0, 12, 8, 0, 0, 0, 0, 0, 0, 0, np.nan])
def test_simple(self, pr_series, tas_series): pr = np.zeros(100) pr[10:20] = 1 pr = pr_series(pr) tas = np.zeros(100) tas[:14] -= 20 tas[14:] += 10 tas = tas_series(tas + K2C) out = xci.liquid_precip_ratio(pr, tas=tas, freq="M") np.testing.assert_almost_equal(out[:1], [0.6])
def test_convert_units(self, tas_series): a = np.zeros(366) ts = tas_series(a, start="1/1/2000") ts.attrs["units"] = "C" tt = (ts.time.dt.month >= 5) & (ts.time.dt.month <= 8) offset = np.random.uniform(low=5.5, high=23, size=(tt.sum().values, )) ts[tt] = ts[tt] + offset out = atmos.growing_season_length(ts) np.testing.assert_array_equal(out, tt.sum())
def test_simple(self, tas_series, pr_series): tas = np.zeros(30) - 1 pr = np.zeros(30) tas[10] += 5 pr[10] += 2 tas = tas_series(tas + K2C) pr = pr_series(pr / 3600 / 24) out = xci.rain_on_frozen_ground_days(pr, tas, freq="MS") assert out[0] == 1
def test_attrs(self, tas_series): import datetime as dt a = tas_series(np.arange(360.0)) ind = UniIndTemp() txm = ind(a, thresh=5, freq="YS") assert txm.cell_methods == "time: mean within days time: mean within years" assert "{:%Y-%m-%d %H}".format( dt.datetime.now()) in txm.attrs["history"] assert "tmin(da, thresh=5, freq='YS')" in txm.attrs["history"] assert "xclim version: {}.".format(__version__) in txm.attrs["history"] assert txm.name == "tmin5"
def test_consecutive_rain(self, tas_series, pr_series): tas = np.zeros(30) - 1 pr = np.zeros(30) tas[10:16] += 5 pr[10:16] += 5 tas = tas_series(tas + K2C) pr = pr_series(pr) out = xci.rain_on_frozen_ground_days(pr, tas, freq='MS') assert out[0] == 1
def test_tn10p_simple(self, tas_series): i = 366 tas = np.array(range(i)) tas = tas_series(tas, start='1/1/2000') t10 = percentile_doy(tas, per=.1) # create cold spell in june tas[175:180] = 1 out = xci.tn10p(tas, t10, freq='MS') assert out[0] == 1 assert out[5] == 5
def test_small_rain(self, tas_series, pr_series): tas = np.zeros(30) - 1 pr = np.zeros(30) tas[10] += 5 pr[10] += .5 tas = tas_series(tas + K2C) pr = pr_series(pr / 3600 / 24) out = xci.rain_on_frozen_ground_days(pr, tas, freq='MS') assert out[0] == 0
def test_tg90p_simple(self, tas_series): i = 366 tas = np.array(range(i)) tas = tas_series(tas, start="1/1/2000") t90 = percentile_doy(tas, per=0.1) # create cold spell in june tas[175:180] = 1 out = xci.tg90p(tas, t90, freq="MS") assert out[0] == 30 assert out[1] == 29 assert out[5] == 25
def test_nan_presence(self, tas_series): a = np.zeros(366) a[50] = np.nan ts = tas_series(a, start='1/1/2000') ts.attrs['units'] = 'C' tt = (ts.time.dt.month >= 5) & (ts.time.dt.month <= 8) offset = np.random.uniform(low=5.5, high=23, size=(tt.sum().values,)) ts[tt] = ts[tt] + offset out = temp.growing_season_length(ts) np.testing.assert_array_equal(out, [np.nan])
def test_multiyear(self, tas_series): a = np.zeros(366 * 10) ts = tas_series(a, start='1/1/2000') ts.attrs['units'] = 'C' tt = (ts.time.dt.month >= 5) & (ts.time.dt.month <= 8) offset = np.random.uniform(low=5.5, high=23, size=(tt.sum().values,)) ts[tt] = ts[tt] + offset out = temp.growing_season_length(ts) np.testing.assert_array_equal(out[3], tt[0:366].sum().values)
def test_deprecation(self, tas_series): with pytest.warns(FutureWarning): out = utils.convert_units_to(0, units.K) assert out == 273.15 out = utils.convert_units_to(10, units.mm / units.day, context='hydro') assert out == 10 with pytest.warns(FutureWarning): tas = tas_series(np.arange(365), start='1/1/2001') out = ind.tx_days_above(tas, 30) out1 = ind.tx_days_above(tas, '30 degC') out2 = ind.tx_days_above(tas, '303.15 K') np.testing.assert_array_equal(out, out1) np.testing.assert_array_equal(out, out2)
def test_simple(self, tas_series): tg = np.zeros(365) - 1 w = 5 i = 10 tg[i:i + w - 1] += 6 # too short i = 20 tg[i:i + w] += 6 # ok i = 30 tg[i:i + w + 1] += 6 # Second valid condition, should be ignored. tg = tas_series(tg + K2C, start='1/1/2000') out = xci.freshet_start(tg, window=w) assert out[0] == tg.indexes['time'][20].dayofyear
def test_simple(self, tas_series): # test for different growing length # generate 5 years of data a = np.zeros(366 * 2 + 365 * 3) tas = tas_series(a, start='2000/1/1') # 2000 : no growing season # 2001 : growing season all year d1 = '27-12-2000' d2 = '31-12-2001' buffer = tas.sel(time=slice(d1, d2)) tas = tas.where(~tas.time.isin(buffer.time), 280) # 2002 : growing season in June only d1 = '6-1-2002' d2 = '6-10-2002' buffer = tas.sel(time=slice(d1, d2)) tas = tas.where(~tas.time.isin(buffer.time), 280) # # comment: # correct answer should be 10 (i.e. there are 10 days # with tas > 5 degC) but current definition imposes end # of growing season to be equal or later than July 1st. # growing season in Aug only d1 = '8-1-2003' d2 = '8-10-2003' buffer = tas.sel(time=slice(d1, d2)) tas = tas.where(~tas.time.isin(buffer.time), 280) # growing season from June to end of July d1 = '6-1-2004' d2 = '7-31-2004' buffer = tas.sel(time=slice(d1, d2)) tas = tas.where(~tas.time.isin(buffer.time), 280) gsl = xci.growing_season_length(tas) target = [0, 365, 25, 10, 61] np.testing.assert_array_equal(gsl, target)
def test_simple(self, tas_series): tas = tas_series(np.arange(365), start='1/1/2001') p1 = percentile_doy(tas, window=5, per=.5) assert p1.sel(dayofyear=3).data == 2 assert p1.attrs['units'] == 'K'
def test_simple(self, tas_series): a = np.zeros(365) a[0] = 5 # default thresh at 4 da = tas_series(a + K2C) assert xci.growing_degree_days(da)[0] == 1
def test_simple(self, tas_series): ts = tas_series(np.arange(365)) out = utils.threshold_count(ts, '<', 50, 'Y') np.testing.assert_array_equal(out, [50, 0])
def test_simple(self, tas_series): tas = tas_series(np.arange(365), start="1/1/2001") p1 = percentile_doy(tas, window=5, per=0.5) assert p1.sel(dayofyear=3).data == 2 assert p1.attrs["units"] == "K"