def random_tmax_tmin_setup(length, tasmax_series, tasmin_series): max_values = np.random.uniform(-20, 40, length) min_values = [] for i in range(length): min_values.append(np.random.uniform(-40, max_values[i])) tasmax = tasmax_series(np.add(max_values, K2C)) tasmin = tasmin_series(np.add(min_values, K2C)) return tasmax, tasmin
def test_one_nan_day(self, tasmin_series): a = np.zeros(365) + K2C + 5.0 a[2] -= 20 a[-1] = np.nan ts = tasmin_series(a) out = atmos.consecutive_frost_days(ts) np.testing.assert_array_equal(out, [np.nan])
def test_convert_units_freeze_day(self, tasmin_series): a = np.zeros(365) + 5.0 a[2:5] -= 20 a[6:10] -= 20 ts = tasmin_series(a) ts.attrs['units'] = 'C' out = atmos.consecutive_frost_days(ts) np.testing.assert_array_equal(out, [4])
def test_uniform_freeze_thaw_cycles(self, tasmax_series, tasmin_series): temp_values = np.zeros(365) tasmax, tasmin = ( tasmax_series(temp_values + 5 + K2C), tasmin_series(temp_values - 5 + K2C), ) ft = xci.daily_freezethaw_cycles(tasmax, tasmin, freq="YS") np.testing.assert_array_equal([np.sum(ft)], [365])
def test_1d(self, tasmax_series, tasmin_series): tn1 = np.zeros(366) tx1 = np.zeros(366) tn1[:10] = np.array([20, 23, 23, 23, 23, 21, 23, 23, 23, 23]) tx1[:10] = np.array([29, 31, 31, 31, 29, 31, 31, 31, 31, 31]) tn = tasmin_series(tn1 + K2C, start="1/1/2000") tx = tasmax_series(tx1 + K2C, start="1/1/2000") tnC = tasmin_series(tn1, start="1/1/2000") tnC.attrs["units"] = "C" txC = tasmax_series(tx1, start="1/1/2000") txC.attrs["units"] = "C" hwf = atmos.heat_wave_max_length(tn, tx, thresh_tasmin="22 C", thresh_tasmax="30 C") hwfC = atmos.heat_wave_max_length(tnC, txC, thresh_tasmin="22 C", thresh_tasmax="30 C") np.testing.assert_array_equal(hwf, hwfC) np.testing.assert_allclose(hwf.values[:1], 4) hwf = atmos.heat_wave_max_length(tn, tx, thresh_tasmin="20 C", thresh_tasmax="30 C", window=4) np.testing.assert_allclose(hwf.values[:1], 5) # one long hw hwf = atmos.heat_wave_max_length(tn, tx, thresh_tasmin="10 C", thresh_tasmax="10 C") np.testing.assert_allclose(hwf.values[:1], 10) # no hw hwf = atmos.heat_wave_max_length(tn, tx, thresh_tasmin="40 C", thresh_tasmax="40 C") np.testing.assert_allclose(hwf.values[:1], 0)
def test_season(self, tasmin_series): ts = tasmin_series(np.zeros(360)) miss = checks.missing_any(ts, freq="YS", season="MAM") np.testing.assert_equal(miss, [False]) miss = checks.missing_any(ts, freq="YS", season="JJA") np.testing.assert_array_equal(miss, [True, True]) miss = checks.missing_any(ts, freq="YS", season="SON") np.testing.assert_equal(miss, [False])
def test_1d(self, tasmax_series, tasmin_series): tn1 = np.zeros(366) tx1 = np.zeros(366) tn1[:10] = np.array([20, 23, 23, 23, 23, 21, 23, 23, 23, 23]) tx1[:10] = np.array([29, 31, 31, 31, 29, 31, 31, 31, 31, 31]) tn = tasmin_series(tn1 + K2C, start='1/1/2000') tx = tasmax_series(tx1 + K2C, start='1/1/2000') tnC = tasmin_series(tn1, start='1/1/2000') tnC.attrs['units'] = 'C' txC = tasmax_series(tx1, start='1/1/2000') txC.attrs['units'] = 'C' hwf = atmos.heat_wave_max_length(tn, tx, thresh_tasmin='22 C', thresh_tasmax='30 C') hwfC = atmos.heat_wave_max_length(tnC, txC, thresh_tasmin='22 C', thresh_tasmax='30 C') np.testing.assert_array_equal(hwf, hwfC) np.testing.assert_allclose(hwf.values[:1], 4) hwf = atmos.heat_wave_max_length(tn, tx, thresh_tasmin='20 C', thresh_tasmax='30 C', window=4) np.testing.assert_allclose(hwf.values[:1], 5) # one long hw hwf = atmos.heat_wave_max_length(tn, tx, thresh_tasmin='10 C', thresh_tasmax='10 C') np.testing.assert_allclose(hwf.values[:1], 10) # no hw hwf = atmos.heat_wave_max_length(tn, tx, thresh_tasmin='40 C', thresh_tasmax='40 C') np.testing.assert_allclose(hwf.values[:1], 0)
def test_1d(self, tasmax_series, tasmin_series): tn = tasmin_series(np.asarray([20, 23, 23, 23, 23, 22, 23, 23, 23, 23]) + K2C) tx = tasmax_series(np.asarray([29, 31, 31, 31, 29, 31, 31, 31, 31, 31]) + K2C) wmmtf = xci.tx_tn_days_above(tn, tx) np.testing.assert_allclose(wmmtf.values, [7]) wmmtf = xci.tx_tn_days_above(tn, tx, thresh_tasmax="50 C") np.testing.assert_allclose(wmmtf.values, [0]) wmmtf = xci.tx_tn_days_above(tn, tx, thresh_tasmax="0 C", thresh_tasmin="0 C") np.testing.assert_allclose(wmmtf.values, [10])
def test_nan_presence(self, tasmin_series): i = 3650 A = 10. tn = np.zeros(i) + K2C + A * np.sin(np.arange(i) / 365. * 2 * np.pi) + .1 * np.random.rand(i) tn[10:20] -= 2 tn[9] = np.nan tn = tasmin_series(tn) tn10 = percentile_doy(tn, per=.1) out = temp.cold_spell_duration_index(tn, tn10, freq='AS-JUL') assert np.isnan(out[0])
def test_convert_units(self, tasmin_series): i = 3650 A = 10. tn = np.zeros(i) + A * np.sin(np.arange(i) / 365. * 2 * np.pi) + .1 * np.random.rand(i) tn[10:20] -= 2 tn = tasmin_series(tn + K2C) tn.attrs['units'] = 'C' tn10 = percentile_doy(tn, per=.1) out = temp.cold_spell_duration_index(tn, tn10, freq='AS-JUL') assert out[0] == 10
def test_simple(self, tasmin_series): a = np.zeros(365) a[:6] -= [27, 28, 29, 30, 31, 32] # 2 above 30 mx = tasmin_series(a + K2C) out = xci.tn_days_below(mx, thresh='-10 C') np.testing.assert_array_equal(out[:1], [6]) np.testing.assert_array_equal(out[1:], [0]) out = xci.tn_days_below(mx, thresh='-30 C') np.testing.assert_array_equal(out[:1], [2]) np.testing.assert_array_equal(out[1:], [0])
def test_simple(self, tasmin_series): i = 3650 A = 10. tn = np.zeros(i) + A * np.sin( np.arange(i) / 365. * 2 * np.pi) + .1 * np.random.rand(i) tn[10:20] -= 2 tn = tasmin_series(tn) tn10 = percentile_doy(tn, per=.1) out = xci.cold_spell_duration_index(tn, tn10, freq='YS') assert out[0] == 10 assert out.units == 'days'
def test_convert_units(self, tasmin_series): i = 3650 A = 10.0 tn = (np.zeros(i) + A * np.sin(np.arange(i) / 365.0 * 2 * np.pi) + 0.1 * np.random.rand(i)) tn[10:20] -= 2 tn = tasmin_series(tn + K2C) tn.attrs["units"] = "C" tn10 = percentile_doy(tn, per=0.1) out = atmos.cold_spell_duration_index(tn, tn10, freq="AS-JUL") assert out[0] == 10
def test_1d(self, tasmin_series): a = np.zeros(35) a[25:] = 23 da = tasmin_series(a + K2C) wnf = xci.warm_night_frequency(da, freq='MS') np.testing.assert_allclose(wnf.values, [6, 4]) wnf = xci.warm_night_frequency(da, freq='YS') np.testing.assert_allclose(wnf.values, [10]) wnf = xci.warm_night_frequency(da, thresh='-1 C') np.testing.assert_allclose(wnf.values, [35]) wnf = xci.warm_night_frequency(da, thresh='50 C') np.testing.assert_allclose(wnf.values, [0])
def test_tn90p_simple(self, tasmin_series): i = 366 tas = np.array(range(i)) tas = tasmin_series(tas, start='1/1/2000') t90 = percentile_doy(tas, per=.1) # create cold spell in june tas[175:180] = 1 out = xci.tn90p(tas, t90, freq='MS') assert out[0] == 30 assert out[1] == 29 assert out[5] == 25
def test_simple(self, tasmin_series, tasmax_series): mn = np.zeros(365) mx = np.zeros(365) # 5 days in 1st month mn[10:20] -= 1 mx[10:15] += 1 # 1 day in 2nd month mn[40:44] += [1, 1, -1, -1] mx[40:44] += [1, -1, 1, -1] mn = tasmin_series(mn + K2C) mx = tasmax_series(mx + K2C) out = xci.daily_freezethaw_cycles(mx, mn, 'M') np.testing.assert_array_equal(out[:2], [5, 1]) np.testing.assert_array_equal(out[2:], 0)
def test_1d(self, tasmax_series, tasmin_series): tn = tasmin_series(np.asarray([20, 23, 23, 23, 23, 22, 23, 23, 23, 23]) + K2C) tx = tasmax_series(np.asarray([29, 31, 31, 31, 29, 31, 31, 31, 31, 31]) + K2C) # some hw hwf = xci.heat_wave_frequency( tn, tx, thresh_tasmin="22 C", thresh_tasmax="30 C" ) np.testing.assert_allclose(hwf.values, 2) hwf = xci.heat_wave_frequency( tn, tx, thresh_tasmin="22 C", thresh_tasmax="30 C", window=4 ) np.testing.assert_allclose(hwf.values, 1) # one long hw hwf = xci.heat_wave_frequency( tn, tx, thresh_tasmin="10 C", thresh_tasmax="10 C" ) np.testing.assert_allclose(hwf.values, 1) # no hw hwf = xci.heat_wave_frequency( tn, tx, thresh_tasmin="40 C", thresh_tasmax="40 C" ) np.testing.assert_allclose(hwf.values, 0)
def static_tmax_tmin_setup(tasmax_series, tasmin_series): max_values = np.add([22, 10, 35.2, 25.1, 18.9, 12, 16], K2C) min_values = np.add([17, 3.5, 22.7, 16, 12.4, 7, 12], K2C) tasmax = tasmax_series(max_values) tasmin = tasmin_series(min_values) return tasmax, tasmin
def test_to_period_end(self, tasmin_series): a = np.zeros(365) + K2C + 5.0 a[2] -= 20 ts = tasmin_series(a) miss = checks.missing_any(ts, freq="A-JUN") np.testing.assert_equal(miss, [False])