climate-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From huiky...@apache.org
Subject [2/5] climate git commit: CLIMATE-647 Updated dataset_processor.temporal_subset
Date Fri, 31 Jul 2015 22:37:33 GMT
CLIMATE-647 Updated dataset_processor.temporal_subset


Project: http://git-wip-us.apache.org/repos/asf/climate/repo
Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/d41cb795
Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/d41cb795
Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/d41cb795

Branch: refs/heads/master
Commit: d41cb79589b22fc1b8f78d6238d40491dc716877
Parents: c032467
Author: huikyole <huikyole@argo.jpl.nasa.gov>
Authored: Tue Jul 28 17:09:14 2015 -0700
Committer: huikyole <huikyole@argo.jpl.nasa.gov>
Committed: Tue Jul 28 17:09:14 2015 -0700

----------------------------------------------------------------------
 ocw/dataset_processor.py | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/climate/blob/d41cb795/ocw/dataset_processor.py
----------------------------------------------------------------------
diff --git a/ocw/dataset_processor.py b/ocw/dataset_processor.py
index 3e35003..a0169a7 100755
--- a/ocw/dataset_processor.py
+++ b/ocw/dataset_processor.py
@@ -29,7 +29,7 @@ import logging
 
 logger = logging.getLogger(__name__)
 
-def temporal_subset(month_start, month_end, target_dataset):
+def temporal_subset(month_start, month_end, target_dataset, average_each_year = False):
     """ Temporally subset data given month_index.
 
     :param month_start: An integer for beginning month (Jan=1)
@@ -41,6 +41,9 @@ def temporal_subset(month_start, month_end, target_dataset):
     :param target_dataset: Dataset object that needs temporal subsetting
     :type target_dataset: Open Climate Workbench Dataset Object
 
+    :param average_each_year: If True, output dataset is averaged for each year
+    :type average_each_year: :class:'boolean'
+
     :returns: A temporal subset OCW Dataset
     :rtype: Open Climate Workbench Dataset Object
     """
@@ -71,8 +74,32 @@ def temporal_subset(month_start, month_end, target_dataset):
                              target_dataset.lons,
                              target_dataset.times[time_index],
                              target_dataset.values[time_index,:],
-                             target_dataset.variable,
-                             target_dataset.name)
+                             variable=target_dataset.variable,
+                             units=target_dataset.units,
+                             name=target_dataset.name)
+
+    if average_each_year:
+        nmonth = len(month_index)
+        ntime  = new_dataset.times.size
+        nyear = ntime/nmonth
+        averaged_time = []              
+        ny, nx = target_dataset.values.shape[1:]
+        averaged_values =ma.zeros([nyear, ny, nx])
+        for iyear in np.arange(nyear):
+            # centered time index of the season between month_start and month_end in each
year
+            center_index = int(nmonth/2)+iyear*nmonth   
+            if nmonth == 1:
+                center_index = iyear
+            averaged_time.append(new_dataset.times[center_index]) 
+            averaged_values[iyear,:] = ma.average(new_dataset.values[nmonth*iyear:nmonth*iyear+nmonth,:],
axis=0)
+        new_dataset = ds.Dataset(target_dataset.lats,
+                                 target_dataset.lons,
+                                 np.array(averaged_time),
+                                 averaged_values,
+                                 variable=target_dataset.variable,
+                                 units=target_dataset.units,
+                                 name=target_dataset.name)
+    
     return new_dataset
 
 def temporal_rebin(target_dataset, temporal_resolution):     


Mime
View raw message