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 = temp.heat_wave_max_length(tn, tx, thresh_tasmin='22 C', thresh_tasmax='30 C') hwfC = temp.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 = temp.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 = temp.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 = temp.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) # some hw hwml = xci.heat_wave_max_length(tn, tx, thresh_tasmin='22 C', thresh_tasmax='30 C') np.testing.assert_allclose(hwml.values, 4) # one long hw hwml = xci.heat_wave_max_length(tn, tx, thresh_tasmin='10 C', thresh_tasmax='10 C') np.testing.assert_allclose(hwml.values, 10) # no hw hwml = xci.heat_wave_max_length(tn, tx, thresh_tasmin='40 C', thresh_tasmax='40 C') np.testing.assert_allclose(hwml.values, 0) hwml = xci.heat_wave_max_length(tn, tx, thresh_tasmin='22 C', thresh_tasmax='30 C', window=5) np.testing.assert_allclose(hwml.values, 0)
def test_convert_units(self, tasmax_series): tx = np.zeros(366) tx[:10] = np.array([29, 31, 31, 31, 29, 31, 31, 31, 31, 31]) tx = tasmax_series(tx, start='1/1/2000') tx.attrs['units'] = 'C' hwi = temp.heat_wave_index(tx, freq='YS') np.testing.assert_array_equal(hwi, [10])
def test_tx10p_simple(self, tasmax_series): i = 366 arr = np.asarray(np.arange(i), "float") tas = tasmax_series(arr, start="1/1/2000") tasC = tas.copy() tasC -= K2C tasC.attrs["units"] = "C" t10 = percentile_doy(tas, per=0.1) # create cold spell in june tas[175:180] = 1 tasC[175:180] = 1 - K2C out = atmos.tx10p(tas, t10, freq="MS") outC = atmos.tx10p(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 = atmos.tx10p(tas, t10, freq="MS") outC = atmos.tx10p(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_convert_units(self, tasmax_series): tx = np.zeros(366) tx[:10] = np.array([29, 31, 31, 31, 29, 31, 31, 31, 31, 31]) tx = tasmax_series(tx, start="1/1/2000") tx.attrs["units"] = "C" hwi = atmos.heat_wave_index(tx, freq="YS") np.testing.assert_array_equal(hwi, [10])
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 test_tx90p_simple(self, tasmax_series): i = 366 arr = np.asarray(np.arange(i), 'float') tas = tasmax_series(arr, start='1/1/2000') tasC = tas.copy() tasC -= K2C tasC.attrs['units'] = 'C' t90 = percentile_doy(tas, per=.1) # create cold spell in june tas[175:180] = 1 tasC[175:180] = 1 - K2C out = temp.tx90p(tas, t90, freq='MS') outC = temp.tx90p(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 = temp.tx90p(tas, t90, freq='MS') outC = temp.tx90p(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_nan_presence(self, tasmax_series): tx = np.zeros(366) tx[:10] = np.array([29, 31, 31, 31, 29, 31, 31, 31, 31, 31]) tx[-1] = np.nan tx = tasmax_series(tx + K2C, start='1/1/2000') hwi = temp.heat_wave_index(tx, freq='YS') np.testing.assert_array_equal(hwi, [np.nan])
def test_simple(self, tasmax_series): a = np.zeros(365) a[:6] += [27, 28, 29, 30, 31, 32] # 2 above 30 mx = tasmax_series(a + K2C) out = xci.tx_days_above(mx, thresh='30 C') np.testing.assert_array_equal(out[:1], [2]) np.testing.assert_array_equal(out[1:], [0])
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_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_simple(self, tasmax_series): a = np.zeros(365) a[10:20] += 30 # 10 days a[40:43] += 50 # too short -> 0 a[80:100] += 30 # at the end and beginning da = tasmax_series(a + K2C) out = xci.heat_wave_index(da, thresh="25 C", freq="M") np.testing.assert_array_equal(out, [10, 0, 12, 8, 0, 0, 0, 0, 0, 0, 0, 0])
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_simple(self, tasmax_series): i = 3650 A = 10. tx = np.zeros(i) + A * np.sin( np.arange(i) / 365. * 2 * np.pi) + .1 * np.random.rand(i) tx[10:20] += 2 tx = tasmax_series(tx) tx90 = percentile_doy(tx, per=.9) out = xci.warm_spell_duration_index(tx, tx90, freq='YS') assert out[0] == 10
def test_1d(self, tasmax_series): a = np.zeros(35) a[25:] = 31 da = tasmax_series(a + K2C) wdf = xci.warm_day_frequency(da, freq='MS') np.testing.assert_allclose(wdf.values, [6, 4]) wdf = xci.warm_day_frequency(da, freq='YS') np.testing.assert_allclose(wdf.values, [10]) wdf = xci.warm_day_frequency(da, thresh='-1 C') np.testing.assert_allclose(wdf.values, [35]) wdf = xci.warm_day_frequency(da, thresh='50 C') np.testing.assert_allclose(wdf.values, [0])
def test_tx10p_simple(self, tasmax_series): i = 366 tas = np.array(range(i)) tas = tasmax_series(tas, start='1/1/2000') t10 = percentile_doy(tas, per=.1) # create cold spell in june tas[175:180] = 1 out = xci.tx10p(tas, t10, freq='MS') assert out[0] == 1 assert out[5] == 5
def test_tx90p_simple(self, tasmax_series): i = 366 tas = np.array(range(i)) tas = tasmax_series(tas, start="1/1/2000") t90 = percentile_doy(tas, per=0.1) # create cold spell in june tas[175:180] = 1 out = xci.tx90p(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 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_simple(self, tasmax_series): tx = np.zeros(366) tx[:10] = np.array([29, 31, 31, 31, 29, 31, 31, 31, 31, 31]) tx = tasmax_series(tx + K2C, start="1/1/2000") hwi = atmos.heat_wave_index(tx, freq="YS") np.testing.assert_array_equal(hwi, [10])