From 14fca6146de83b01633177e0606ade048920dee9 Mon Sep 17 00:00:00 2001 From: jcos Date: Wed, 12 Aug 2020 11:37:25 +0200 Subject: [PATCH 1/5] added RMS iris analysis operator --- esmvalcore/preprocessor/_area.py | 8 +++++--- esmvalcore/preprocessor/_shared.py | 6 +++--- esmvalcore/preprocessor/_time.py | 12 ++++++------ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/esmvalcore/preprocessor/_area.py b/esmvalcore/preprocessor/_area.py index e8ee06b8c3..7fd29b07c0 100644 --- a/esmvalcore/preprocessor/_area.py +++ b/esmvalcore/preprocessor/_area.py @@ -99,7 +99,7 @@ def zonal_statistics(cube, operator): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max'. + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms'. Returns ------- @@ -133,7 +133,7 @@ def meridional_statistics(cube, operator): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max'. + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms'. Returns ------- @@ -224,6 +224,8 @@ def area_statistics(cube, operator, fx_variables=None): +------------+--------------------------------------------------+ | `max` | Maximum value | +------------+--------------------------------------------------+ + | `rms` | Area weighted root mean square. | + +------------+--------------------------------------------------+ Parameters ---------- @@ -231,7 +233,7 @@ def area_statistics(cube, operator, fx_variables=None): Input cube. operator: str The operation, options: mean, median, min, max, std_dev, sum, - variance + variance, rms. fx_variables: dict dictionary of field:filename for the fx_variables diff --git a/esmvalcore/preprocessor/_shared.py b/esmvalcore/preprocessor/_shared.py index 73a7892e79..dec41daf2c 100644 --- a/esmvalcore/preprocessor/_shared.py +++ b/esmvalcore/preprocessor/_shared.py @@ -40,9 +40,9 @@ def get_iris_analysis_operation(operator): ------ ValueError operator not in allowed operators list. - allowed operators: mean, median, std_dev, sum, variance, min, max + allowed operators: mean, median, std_dev, sum, variance, min, max, rms """ - operators = ['mean', 'median', 'std_dev', 'sum', 'variance', 'min', 'max'] + operators = ['mean', 'median', 'std_dev', 'sum', 'variance', 'min', 'max', 'rms'] operator = operator.lower() if operator not in operators: raise ValueError("operator {} not recognised. " @@ -66,4 +66,4 @@ def operator_accept_weights(operator): bool: True if operator support weights, False otherwise """ - return operator.lower() in ('mean', 'sum') + return operator.lower() in ('mean', 'sum', 'rms') diff --git a/esmvalcore/preprocessor/_time.py b/esmvalcore/preprocessor/_time.py index 888266c578..8d9a400bab 100644 --- a/esmvalcore/preprocessor/_time.py +++ b/esmvalcore/preprocessor/_time.py @@ -195,7 +195,7 @@ def daily_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max' + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' Returns ------- @@ -228,7 +228,7 @@ def monthly_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max' + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' Returns ------- @@ -258,7 +258,7 @@ def seasonal_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max' + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' Returns ------- @@ -313,7 +313,7 @@ def annual_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max' + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' Returns ------- @@ -345,7 +345,7 @@ def decadal_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max' + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' Returns ------- @@ -384,7 +384,7 @@ def climate_statistics(cube, operator='mean', period='full'): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max' + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' period: str, optional Period to compute the statistic over. From 2888a6d212106f91c709772ae614c4e918e445ff Mon Sep 17 00:00:00 2001 From: jcos Date: Wed, 12 Aug 2020 12:45:57 +0200 Subject: [PATCH 2/5] fix line length to standards --- doc/recipe/preprocessor.rst | 14 +++++++------- esmvalcore/preprocessor/_area.py | 6 ++++-- esmvalcore/preprocessor/_shared.py | 4 +++- esmvalcore/preprocessor/_time.py | 18 ++++++++++++------ 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/doc/recipe/preprocessor.rst b/doc/recipe/preprocessor.rst index 7f10a90ae7..6f36332c5b 100644 --- a/doc/recipe/preprocessor.rst +++ b/doc/recipe/preprocessor.rst @@ -798,7 +798,7 @@ This function produces statistics for each day in the dataset. Parameters: * operator: operation to apply. Accepted values are 'mean', - 'median', 'std_dev', 'min', 'max' and 'sum'. Default is 'mean' + 'median', 'std_dev', 'min', 'max', 'sum' and 'rms'. Default is 'mean' See also :func:`esmvalcore.preprocessor.daily_statistics`. @@ -811,7 +811,7 @@ This function produces statistics for each month in the dataset. Parameters: * operator: operation to apply. Accepted values are 'mean', - 'median', 'std_dev', 'min', 'max' and 'sum'. Default is 'mean' + 'median', 'std_dev', 'min', 'max', 'sum' and 'rms'. Default is 'mean' See also :func:`esmvalcore.preprocessor.monthly_statistics`. @@ -831,7 +831,7 @@ December and remove such biased initial datapoints. Parameters: * operator: operation to apply. Accepted values are 'mean', - 'median', 'std_dev', 'min', 'max' and 'sum'. Default is 'mean' + 'median', 'std_dev', 'min', 'max', 'sum' and 'rms'. Default is 'mean' See also :func:`esmvalcore.preprocessor.seasonal_mean`. @@ -844,7 +844,7 @@ This function produces statistics for each year. Parameters: * operator: operation to apply. Accepted values are 'mean', - 'median', 'std_dev', 'min', 'max' and 'sum'. Default is 'mean' + 'median', 'std_dev', 'min', 'max', 'sum' and 'rms'. Default is 'mean' See also :func:`esmvalcore.preprocessor.annual_statistics`. @@ -857,7 +857,7 @@ This function produces statistics for each decade. Parameters: * operator: operation to apply. Accepted values are 'mean', - 'median', 'std_dev', 'min', 'max' and 'sum'. Default is 'mean' + 'median', 'std_dev', 'min', 'max', 'sum' and 'rms'. Default is 'mean' See also :func:`esmvalcore.preprocessor.decadal_statistics`. @@ -871,7 +871,7 @@ This function produces statistics for the whole dataset. It can produce scalars Parameters: * operator: operation to apply. Accepted values are 'mean', 'median', - 'std_dev', 'min', 'max' and 'sum'. Default is 'mean' + 'std_dev', 'min', 'max', 'sum' and 'rms'. Default is 'mean' * period: define the granularity of the statistics: get values for the full period, for each month or day of year. @@ -1152,7 +1152,7 @@ areas of the region. This function takes the argument, ``operator``: the name of the operation to apply. This function can be used to apply several different operations in the -horizontal plane: mean, standard deviation, median variance, minimum and maximum. +horizontal plane: mean, standard deviation, median, variance, minimum, maximum and root mean square. Note that this function is applied over the entire dataset. If only a specific region, depth layer or time period is required, then those regions need to be diff --git a/esmvalcore/preprocessor/_area.py b/esmvalcore/preprocessor/_area.py index 7fd29b07c0..57e6ca9691 100644 --- a/esmvalcore/preprocessor/_area.py +++ b/esmvalcore/preprocessor/_area.py @@ -99,7 +99,8 @@ def zonal_statistics(cube, operator): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms'. + Available operators: + 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms'. Returns ------- @@ -133,7 +134,8 @@ def meridional_statistics(cube, operator): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms'. + Available operators: + 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms'. Returns ------- diff --git a/esmvalcore/preprocessor/_shared.py b/esmvalcore/preprocessor/_shared.py index dec41daf2c..89b6d13b32 100644 --- a/esmvalcore/preprocessor/_shared.py +++ b/esmvalcore/preprocessor/_shared.py @@ -42,7 +42,9 @@ def get_iris_analysis_operation(operator): operator not in allowed operators list. allowed operators: mean, median, std_dev, sum, variance, min, max, rms """ - operators = ['mean', 'median', 'std_dev', 'sum', 'variance', 'min', 'max', 'rms'] + operators = [ + 'mean', 'median', 'std_dev', 'sum', 'variance', 'min', 'max', 'rms' + ] operator = operator.lower() if operator not in operators: raise ValueError("operator {} not recognised. " diff --git a/esmvalcore/preprocessor/_time.py b/esmvalcore/preprocessor/_time.py index 8d9a400bab..3c6f64f0c6 100644 --- a/esmvalcore/preprocessor/_time.py +++ b/esmvalcore/preprocessor/_time.py @@ -195,7 +195,8 @@ def daily_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' + Available operators: + 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' Returns ------- @@ -228,7 +229,8 @@ def monthly_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' + Available operators: + 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' Returns ------- @@ -258,7 +260,8 @@ def seasonal_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' + Available operators: + 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' Returns ------- @@ -313,7 +316,8 @@ def annual_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' + Available operators: + 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' Returns ------- @@ -345,7 +349,8 @@ def decadal_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' + Available operators: + 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' Returns ------- @@ -384,7 +389,8 @@ def climate_statistics(cube, operator='mean', period='full'): operator: str, optional Select operator to apply. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' + Available operators: + 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' period: str, optional Period to compute the statistic over. From 045f4082397f17f08d695e3e9dfe5a10d1a28985 Mon Sep 17 00:00:00 2001 From: jcos Date: Thu, 13 Aug 2020 08:49:57 +0200 Subject: [PATCH 3/5] check docstrings format --- esmvalcore/preprocessor/_area.py | 8 ++++---- esmvalcore/preprocessor/_time.py | 24 ++++++++++++------------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/esmvalcore/preprocessor/_area.py b/esmvalcore/preprocessor/_area.py index 57e6ca9691..f34882e09c 100644 --- a/esmvalcore/preprocessor/_area.py +++ b/esmvalcore/preprocessor/_area.py @@ -99,8 +99,8 @@ def zonal_statistics(cube, operator): operator: str, optional Select operator to apply. - Available operators: - 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms'. + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', + 'max', 'rms'. Returns ------- @@ -134,8 +134,8 @@ def meridional_statistics(cube, operator): operator: str, optional Select operator to apply. - Available operators: - 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms'. + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', + 'max', 'rms'. Returns ------- diff --git a/esmvalcore/preprocessor/_time.py b/esmvalcore/preprocessor/_time.py index 3c6f64f0c6..eac8cf7bf5 100644 --- a/esmvalcore/preprocessor/_time.py +++ b/esmvalcore/preprocessor/_time.py @@ -195,8 +195,8 @@ def daily_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: - 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', + 'max', 'rms' Returns ------- @@ -229,8 +229,8 @@ def monthly_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: - 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', + 'max', 'rms' Returns ------- @@ -260,8 +260,8 @@ def seasonal_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: - 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', + 'max', 'rms' Returns ------- @@ -316,8 +316,8 @@ def annual_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: - 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', + 'max', 'rms' Returns ------- @@ -349,8 +349,8 @@ def decadal_statistics(cube, operator='mean'): operator: str, optional Select operator to apply. - Available operators: - 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', + 'max', 'rms' Returns ------- @@ -389,8 +389,8 @@ def climate_statistics(cube, operator='mean', period='full'): operator: str, optional Select operator to apply. - Available operators: - 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms' + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', + 'max', 'rms' period: str, optional Period to compute the statistic over. From 3be8d0b37b2a314061c93fa4243a63884dfa5940 Mon Sep 17 00:00:00 2001 From: jcos Date: Thu, 13 Aug 2020 15:06:27 +0200 Subject: [PATCH 4/5] added unit testing --- tests/unit/preprocessor/_area/test_area.py | 6 ++++++ tests/unit/preprocessor/_time/test_time.py | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/tests/unit/preprocessor/_area/test_area.py b/tests/unit/preprocessor/_area/test_area.py index f16ca8bf37..895612b66f 100644 --- a/tests/unit/preprocessor/_area/test_area.py +++ b/tests/unit/preprocessor/_area/test_area.py @@ -107,6 +107,12 @@ def test_area_statistics_neg_lon(self): expected = np.array([1.]) self.assert_array_equal(result.data, expected) + def test_area_statistics_rms(self): + """Test for area rms of a 2D field.""" + result = area_statistics(self.grid, 'rms') + expected = np.array([1.]) + self.assert_array_equal(result.data, expected) + def test_extract_region(self): """Test for extracting a region from a 2D field.""" result = extract_region(self.grid, 1.5, 2.5, 1.5, 2.5) diff --git a/tests/unit/preprocessor/_time/test_time.py b/tests/unit/preprocessor/_time/test_time.py index bffe806e9f..ef6f8ff3bc 100644 --- a/tests/unit/preprocessor/_time/test_time.py +++ b/tests/unit/preprocessor/_time/test_time.py @@ -372,6 +372,17 @@ def test_time_median(self): expected = np.array([1.]) assert_array_equal(result.data, expected) + def test_time_rms(self): + """Test for time rms of a 1D field.""" + data = np.arange((3)) + times = np.array([15., 45., 75.]) + bounds = np.array([[0., 30.], [30., 60.], [60., 90.]]) + cube = self._create_cube(data, times, bounds) + + result = climate_statistics(cube, operator='rms') + expected = np.array([(5/3)**0.5]) + assert_array_equal(result.data, expected) + class TestSeasonalStatistics(tests.Test): """Test :func:`esmvalcore.preprocessor._time.seasonal_statistics`""" From cdc20a3431c5eb09762d18aa71156b1b4cfd1a00 Mon Sep 17 00:00:00 2001 From: jcos Date: Fri, 14 Aug 2020 11:08:18 +0200 Subject: [PATCH 5/5] added missing rms docu --- doc/recipe/preprocessor.rst | 6 +++--- esmvalcore/preprocessor/_time.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/doc/recipe/preprocessor.rst b/doc/recipe/preprocessor.rst index 6f36332c5b..8979b970ca 100644 --- a/doc/recipe/preprocessor.rst +++ b/doc/recipe/preprocessor.rst @@ -1000,7 +1000,7 @@ Parameters: * filter_type: the type of filter to be applied; default 'lowpass'. Available types: 'lowpass'. * filter_stats: the type of statistic to aggregate on the rolling window; - default 'sum'. Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max'. + default 'sum'. Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max', 'rms'. Examples: * Lowpass filter with a monthly mean as operator: @@ -1127,7 +1127,7 @@ Parameters: The function calculates the zonal statistics by applying an operator along the longitude coordinate. This function takes one argument: -* ``operator``: Which operation to apply: mean, std_dev, median, min, max or sum +* ``operator``: Which operation to apply: mean, std_dev, median, min, max, sum or rms. See also :func:`esmvalcore.preprocessor.zonal_means`. @@ -1139,7 +1139,7 @@ The function calculates the meridional statistics by applying an operator along the latitude coordinate. This function takes one argument: -* ``operator``: Which operation to apply: mean, std_dev, median, min, max or sum +* ``operator``: Which operation to apply: mean, std_dev, median, min, max, sum or rms. See also :func:`esmvalcore.preprocessor.meridional_means`. diff --git a/esmvalcore/preprocessor/_time.py b/esmvalcore/preprocessor/_time.py index eac8cf7bf5..524a82dd52 100644 --- a/esmvalcore/preprocessor/_time.py +++ b/esmvalcore/preprocessor/_time.py @@ -682,7 +682,8 @@ def timeseries_filter(cube, window, span, Available types: 'lowpass'. filter_stats: str, optional Type of statistic to aggregate on the rolling window; default 'sum'. - Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max' + Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', + 'max', 'rms' Returns -------