Repository: climate Updated Branches: refs/heads/master 9183f5fea -> 6e4920c05
CLIMATE-341: Port calcAnnualCycleMeans from metrics_kyo to utils Project: http://git-wip-us.apache.org/repos/asf/climate/repo Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/d9b0e6b0 Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/d9b0e6b0 Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/d9b0e6b0 Branch: refs/heads/master Commit: d9b0e6b0b1fe66040960a00e0cfb1a5db3d619f5 Parents: 9183f5f Author: rlaidlaw <[email protected]> Authored: Tue Oct 28 01:16:48 2014 -0700 Committer: rlaidlaw <[email protected]> Committed: Tue Oct 28 01:16:48 2014 -0700 ---------------------------------------------------------------------- ocw/tests/test_utils.py | 18 ++++++++++++++++++ ocw/utils.py | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/climate/blob/d9b0e6b0/ocw/tests/test_utils.py ---------------------------------------------------------------------- diff --git a/ocw/tests/test_utils.py b/ocw/tests/test_utils.py index 48beb43..2c0981c 100644 --- a/ocw/tests/test_utils.py +++ b/ocw/tests/test_utils.py @@ -196,5 +196,23 @@ class TestCalcClimatologyYear(unittest.TestCase): np.testing.assert_array_equal( utils.calc_climatology_year(self.test_dataset)[1], total_mean) + +class TestCalcClimatologyMonthly(unittest.TestCase): + ''' Tests the 'calc_climatology_monthly' method from ocw.utils.py ''' + + def setUp(self): + self.lats = np.array([10, 20, 30, 40, 50]) + self.lons = np.array([20, 30, 40, 50, 60]) + self.times = np.array([datetime.datetime(2000, 1, 1) + relativedelta(months = x) for x in range(36)]) + self.values = np.array([1]*300 + [2]*300 + [0]*300).reshape(36, 5, 5) + self.variable = 'testdata' + self.dataset = Dataset(self.lats, self.lons, self.times, self.values, self.variable) + + def test_calc_climatology_monthly(self): + expected_result = np.ones(300).reshape(12, 5, 5) + actual_result = utils.calc_climatology_monthly(self.dataset) + np.testing.assert_array_equal(actual_result, expected_result) + + if __name__ == '__main__': unittest.main() http://git-wip-us.apache.org/repos/asf/climate/blob/d9b0e6b0/ocw/utils.py ---------------------------------------------------------------------- diff --git a/ocw/utils.py b/ocw/utils.py index 8c109b7..326cab9 100644 --- a/ocw/utils.py +++ b/ocw/utils.py @@ -311,3 +311,23 @@ def calc_climatology_season(month_start, month_end, dataset): t_series = reshape_data[:, month_index].mean(axis=1) means = t_series.mean(axis=0) return t_series, means + + +def calc_climatology_monthly(dataset): + ''' Calculate monthly mean values for a dataset. + + :param dataset: Monthly binned Dataset object with the number of months + divisible by 12. + :type dataset: ocw.dataset.Dataset object + + :returns: Mean values for each month of the year + :rtype: A 3D numpy array of shape (12, num_lats, num_lons) + + :raise ValueError: If the number of monthly bins is not divisible by 12. + ''' + + if dataset.values.shape[0] % 12: + raise ValueError("The length of the time axis in the values array should be divisible by 12.") + else: + return reshape_monthly_to_annually(dataset).mean(axis=0) +
