calc_climatology_season is replaced by dataset_processor.temporal_subset and utils.calc_temporal_mean. calc_temporal_mean can replace calc_climatology_year and calc_climatology_monthly.
Project: http://git-wip-us.apache.org/repos/asf/climate/repo Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/4b40eced Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/4b40eced Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/4b40eced Branch: refs/heads/master Commit: 4b40eced880dc68f2ca8b7e1099471f0e59a4308 Parents: 4d8fee6 Author: huikyole <[email protected]> Authored: Tue May 12 15:03:59 2015 -0700 Committer: huikyole <[email protected]> Committed: Tue May 12 15:03:59 2015 -0700 ---------------------------------------------------------------------- .../simple_model_to_model_bias_DJF_and_JJA.py | 12 +++--- ocw/dataset_processor.py | 44 ++++++-------------- ocw/utils.py | 11 +++++ 3 files changed, 30 insertions(+), 37 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/climate/blob/4b40eced/examples/simple_model_to_model_bias_DJF_and_JJA.py ---------------------------------------------------------------------- diff --git a/examples/simple_model_to_model_bias_DJF_and_JJA.py b/examples/simple_model_to_model_bias_DJF_and_JJA.py index 36d10c3..364498a 100644 --- a/examples/simple_model_to_model_bias_DJF_and_JJA.py +++ b/examples/simple_model_to_model_bias_DJF_and_JJA.py @@ -55,10 +55,10 @@ print("WRF_Dataset.values shape: (times, lats, lons) - %s \n" % (wrf_dataset.val """ Step 2: Calculate seasonal average """ print("Calculate seasonal average") -knmi_DJF, knmi_DJF_mean = dsp.calc_climatology_season(month_start=12, month_end=2, dataset=knmi_dataset) -wrf_DJF, wrf_DJF_mean = dsp.calc_climatology_season(month_start=12, month_end=2, dataset=wrf_dataset) -print("Seasonally averaged KNMI_Dataset.values shape: (times, lats, lons) - %s \n" % (knmi_DJF.values.shape,)) -print("Seasonally averaged wrf_Dataset.values shape: (times, lats, lons) - %s \n" % (wrf_DJF.values.shape,)) -knmi_JJA, knmi_JJA_mean = dsp.calc_climatology_season(month_start=6, month_end=8, dataset=knmi_dataset) -wrf_JJA, wrf_JJA_mean = dsp.calc_climatology_season(month_start=6, month_end=8, dataset=wrf_dataset) +knmi_DJF_mean = utils.calc_temporal_mean(dsp.temporal_subset(month_start=12, month_end=2, target_dataset=knmi_dataset)) +wrf_DJF_mean = utils.calc_temporal_mean(dsp.temporal_subset(month_start=12, month_end=2, target_dataset=wrf_dataset)) +print("Seasonally averaged KNMI_Dataset.values shape: (times, lats, lons) - %s \n" % (knmi_DJF_mean.shape,)) +print("Seasonally averaged wrf_Dataset.values shape: (times, lats, lons) - %s \n" % (wrf_DJF_mean.shape,)) +knmi_JJA_mean = utils.calc_temporal_mean(dsp.temporal_subset(month_start=6, month_end=8, target_dataset=knmi_dataset)) +wrf_JJA_mean = utils.calc_temporal_mean(dsp.temporal_subset(month_start=6, month_end=8, target_dataset=wrf_dataset)) http://git-wip-us.apache.org/repos/asf/climate/blob/4b40eced/ocw/dataset_processor.py ---------------------------------------------------------------------- diff --git a/ocw/dataset_processor.py b/ocw/dataset_processor.py index a536794..984e142 100644 --- a/ocw/dataset_processor.py +++ b/ocw/dataset_processor.py @@ -29,17 +29,28 @@ import logging logger = logging.getLogger(__name__) -def temporal_subset(target_dataset, month_index): +def temporal_subset(month_start, month_end, target_dataset): """ Temporally subset data given month_index. + :param month_start: An integer for beginning month (Jan=1) + :type month_start: :class:`int` + + :param month_end: An integer for ending month (Jan=1) + :type month_end: :class:`int` + :param target_dataset: Dataset object that needs temporal subsetting :type target_dataset: Open Climate Workbench Dataset Object - :param month_index: an integer array of subset months (June ~ August: [6, 7, 8]) :returns: A temporal subset OCW Dataset :rtype: Open Climate Workbench Dataset Object """ + if month_start > month_end: + month_index = range(month_start,13) + month_index.extend(range(1, month_end+1)) + else: + month_index = range(month_start, month_end+1) + dates = target_dataset.times months = np.array([d.month for d in dates]) time_index = [] @@ -64,35 +75,6 @@ def temporal_subset(target_dataset, month_index): target_dataset.name) return new_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` - - :param month_end: An integer for ending month (Jan=1) - :type month_end: :class:`int` - - :param dataset: OCW dataset object with full-year format - :type dataset: :class:`dataset.Dataset` - - :returns: t_series - monthly average over the given season - means - mean over the entire season - - ''' - - if month_start > month_end: - month_index = range(month_start,13) - month_index.extend(range(1, month_end+1)) - else: - month_index = range(month_start, month_end+1) - - # t_series only includes data of months in month_index - t_series = temporal_subset(dataset, month_index) - means = ma.mean(t_series.values, axis=0) - - return t_series, means - def temporal_rebin(target_dataset, temporal_resolution): """ Rebin a Dataset to a new temporal resolution http://git-wip-us.apache.org/repos/asf/climate/blob/4b40eced/ocw/utils.py ---------------------------------------------------------------------- diff --git a/ocw/utils.py b/ocw/utils.py index 536aa09..1595379 100644 --- a/ocw/utils.py +++ b/ocw/utils.py @@ -20,6 +20,7 @@ import sys import datetime as dt import numpy as np +import numpy.ma as ma import datetime from mpl_toolkits.basemap import shiftgrid @@ -255,6 +256,16 @@ def reshape_monthly_to_annually(dataset): return values +def calc_temporal_mean(dataset): + ''' Calculate climatology of dataset's values for each year + + :param dataset: OCW Dataset whose first dimension is time + :type dataset: :class:`dataset.Dataset` + + :returns: Mean values averaged for the first dimension (time) + ''' + return ma.mean(dataset.values, axis=0) + def calc_climatology_year(dataset): ''' Calculate climatology of dataset's values for each year
