You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@climate.apache.org by "Ross Laidlaw (JIRA)" <ji...@apache.org> on 2014/10/28 09:21:34 UTC

[jira] [Commented] (CLIMATE-341) Refactor "calcAnnualCycleMeans" metric from metrics_kyo.py

    [ https://issues.apache.org/jira/browse/CLIMATE-341?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14186560#comment-14186560 ] 

Ross Laidlaw commented on CLIMATE-341:
--------------------------------------

I took a closer look at the utils.py methods 'calc_climatology_year' and 'calc_climatology_season'.  These are slightly different and don't quite fit the goal of 'calcAnnualCycleMeans':

- calc_climatology_year calculates yearly means of a dataset's values
- calc_climatology_season calculates seasonal means of a dataset's values, where a season can span several months and the mean is a single value for the season

In a test case, I tried using 'calc_climatology_season' to calculate monthly means, but when entering '1' for the month_start value and '12' for the month_end value, the 'season' is then an entire year, so this case gives the same results as 'calc_climatology_year' (yearly mean).

I therefore implemented a new method to calculate monthly means (as done in the original calcAnnualCycleMeans method).  The new method is very similar to calc_climatology_year:

{code}
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)
{code}


... and related test case:

{code}
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)
{code}

Test output after completing the method implementation:
{noformat}
nosetests ocw/tests/test_utils.py:TestCalcClimatologyMonthly
.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
{noformat}


> Refactor "calcAnnualCycleMeans" metric from metrics_kyo.py
> ----------------------------------------------------------
>
>                 Key: CLIMATE-341
>                 URL: https://issues.apache.org/jira/browse/CLIMATE-341
>             Project: Apache Open Climate Workbench
>          Issue Type: Sub-task
>          Components: metrics
>    Affects Versions: 0.3-incubating
>            Reporter: Maziyar Boustani
>            Assignee: Ross Laidlaw
>             Fix For: 0.5
>
>
> Reimplement metric "calcAnnualCycleMeans" from [1], possibly in utils.py [2].
> [1]: https://svn.apache.org/repos/asf/incubator/climate/trunk/rcmet/src/main/python/rcmes/toolkit/metrics.py
> [2]:https://svn.apache.org/repos/asf/incubator/climate/trunk/ocw/utils.py



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)