Repository: climate Updated Branches: refs/heads/master 8656e3279 -> 536aa3e64
ocw.utils.calc_area_weighted_spatial_average is added Project: http://git-wip-us.apache.org/repos/asf/climate/repo Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/3e9c310b Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/3e9c310b Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/3e9c310b Branch: refs/heads/master Commit: 3e9c310b8b54d81694ee0ded6c4b89eccb085d10 Parents: fea37a9 Author: huikyole <[email protected]> Authored: Fri Jul 31 16:49:09 2015 -0700 Committer: huikyole <[email protected]> Committed: Fri Jul 31 16:49:09 2015 -0700 ---------------------------------------------------------------------- ocw/utils.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/climate/blob/3e9c310b/ocw/utils.py ---------------------------------------------------------------------- diff --git a/ocw/utils.py b/ocw/utils.py index bb5ed37..cc77437 100755 --- a/ocw/utils.py +++ b/ocw/utils.py @@ -343,3 +343,29 @@ def calc_time_series(dataset): t_series.append(dataset.values[t,:,:].mean()) return t_series + +def calc_area_weighted_spatial_average(dataset, area_weight=False): + '''Calculate area weighted average of the values in OCW dataset + + :param dataset: Dataset object + :type dataset: :class:`dataset.Dataset` + + :returns: time series for the dataset of shape (nT) + ''' + + if dataset.lats.ndim ==1: + lons, lats = np.meshgrid(dataset.lons, dataset.lats) + else: + lons = dataset.lons + lats = dataset.lats + weights = np.cos(lats*np.pi/180.) + + nt, ny, nx = dataset.values.shape + spatial_average = ma.zeros(nt) + for it in np.arange(nt): + if area_weight: + spatial_average[it] = ma.average(dataset.values[it,:], weights = weights) + else: + spatial_average[it] = ma.average(dataset.values[it,:]) + + return spatial_average
