def get_climate(self, heights, year=None): """Average climate information at given heights. Note that prcp is corrected with the precipitation factor and that all other biases (precipitation, temp) are applied Returns ------- (temp, tempformelt, prcp, prcpsol) """ yrs = monthly_timeseries(self.years[0], self.years[-1], include_last_year=True) heights = np.atleast_1d(heights) nh = len(heights) shape = (len(yrs), nh) temp = np.zeros(shape) tempformelt = np.zeros(shape) prcp = np.zeros(shape) prcpsol = np.zeros(shape) for i, yr in enumerate(yrs): t, tm, p, ps = self.mbmod.get_monthly_climate(heights, year=yr) temp[i, :] = t tempformelt[i, :] = tm prcp[i, :] = p prcpsol[i, :] = ps # Note that we do not weight for number of days per month - bad return (np.mean(temp, axis=0), np.mean(tempformelt, axis=0) * 12, np.mean(prcp, axis=0) * 12, np.mean(prcpsol, axis=0) * 12)
def test_date_to_year(self): r = utils.date_to_year(0, 1) self.assertEqual(r, 0) r = utils.date_to_year(1, 1) self.assertEqual(r, 1) r = utils.date_to_year([0, 1], [1, 1]) np.testing.assert_array_equal(r, [0, 1]) yr = utils.date_to_year([1998, 1998], [6, 7]) y, m = utils.year_to_date(yr) np.testing.assert_array_equal(y, [1998, 1998]) np.testing.assert_array_equal(m, [6, 7]) yr = utils.date_to_year([1998, 1998], [2, 3]) y, m = utils.year_to_date(yr) np.testing.assert_array_equal(y, [1998, 1998]) np.testing.assert_array_equal(m, [2, 3]) time = pd.date_range('1/1/1800', periods=300*12, freq='MS') yr = utils.date_to_year(time.year, time.month) y, m = utils.year_to_date(yr) np.testing.assert_array_equal(y, time.year) np.testing.assert_array_equal(m, time.month) myr = utils.monthly_timeseries(1800, 2099) y, m = utils.year_to_date(myr) np.testing.assert_array_equal(y, time.year) np.testing.assert_array_equal(m, time.month) myr = utils.monthly_timeseries(1800, ny=300) y, m = utils.year_to_date(myr) np.testing.assert_array_equal(y, time.year) np.testing.assert_array_equal(m, time.month) time = pd.period_range('0001-01', '3000-12', freq='M') myr = utils.monthly_timeseries(1, 3000) y, m = utils.year_to_date(myr) np.testing.assert_array_equal(y, time.year) np.testing.assert_array_equal(m, time.month) with self.assertRaises(ValueError): utils.monthly_timeseries(1)