Repository: climate Updated Branches: refs/heads/master 6ff8fd694 -> d6710a448
- Update the times array of the DS object when monthly climo is calculated - Update docs and remove duplicate documentation Project: http://git-wip-us.apache.org/repos/asf/climate/repo Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/da7c2003 Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/da7c2003 Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/da7c2003 Branch: refs/heads/master Commit: da7c2003cc7e0428ebb988f49d08f3ce767b2bea Parents: 9a2be2e Author: Kim Whitehall <[email protected]> Authored: Sun Mar 22 10:23:20 2015 -0700 Committer: Kim Whitehall <[email protected]> Committed: Thu Mar 26 01:34:13 2015 -0700 ---------------------------------------------------------------------- ocw/utils.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/climate/blob/da7c2003/ocw/utils.py ---------------------------------------------------------------------- diff --git a/ocw/utils.py b/ocw/utils.py index c4b640f..00a6a01 100644 --- a/ocw/utils.py +++ b/ocw/utils.py @@ -20,6 +20,7 @@ import sys import datetime as dt import numpy as np +import datetime from mpl_toolkits.basemap import shiftgrid from dateutil.relativedelta import relativedelta @@ -235,7 +236,6 @@ def reshape_monthly_to_annually(dataset): :type dataset: :class:`dataset.Dataset` :returns: Dataset values array with shape (num_year, 12, num_lat, num_lon) - :rtype: :class:`numpy.ndarray` ''' values = dataset.values[:] @@ -254,14 +254,13 @@ def reshape_monthly_to_annually(dataset): def calc_climatology_year(dataset): ''' Calculate climatology of dataset's values for each year - + :param dataset: Monthly binned Dataset object with an evenly divisible number of months. :type dataset: :class:`dataset.Dataset` :returns: Mean values for each year (annual_mean) and mean values for all years (total_mean) - :rtype: A :func:`tuple` of two :class:`numpy.ndarray` :raise ValueError: If the number of monthly bins is not evenly divisible by 12. @@ -283,7 +282,7 @@ def calc_climatology_year(dataset): def calc_climatology_season(month_start, month_end, dataset): ''' Calculate seasonal mean and time series for given months. - + :param month_start: An integer for beginning month (Jan=1) :type month_start: :class:`int` @@ -293,10 +292,9 @@ def calc_climatology_season(month_start, month_end, dataset): :param dataset: Dataset object with full-year format :type dataset: :class:`dataset.Dataset` - :returns: - t_series - monthly average over the given season + :returns: t_series - monthly average over the given season means - mean over the entire season - :rtype: A :func:`tuple` of two :class:`numpy.ndarray` + ''' if month_start > month_end: @@ -312,19 +310,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. + Follow COARDS climo stats calculation, the year can be given as 0 + but the min year allowed in Python is 1 + http://www.cgd.ucar.edu/cms/eaton/netcdf/CF-20010629.htm#climatology :param dataset: Monthly binned Dataset object with the number of months divisible by 12 :type dataset: :class:`dataset.Dataset` - :returns: Mean values for each month of the year - :rtype: A 3D :class:`numpy.ndarray` of shape (12, num_lats, num_lons) - + :returns: Mean values for each month of the year of shape (12, num_lats, num_lons) + and times array of datetime objects of length 12 + :raise ValueError: If the number of monthly bins is not divisible by 12 ''' @@ -335,5 +337,11 @@ def calc_climatology_monthly(dataset): ) raise ValueError(error) else: - return reshape_monthly_to_annually(dataset).mean(axis=0) + values = reshape_monthly_to_annually(dataset).mean(axis=0) + + # A year can commence from any month + first_month = dataset.times[0].month + times = np.array([datetime.datetime(1, first_month, 1) + relativedelta(months = x) + for x in range(12)]) + return values, times
