climate-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From whiteh...@apache.org
Subject [1/3] climate git commit: CLIMATE-623 - Allow propogation of 2D arrays - update subset function in DSP to subset 2D (lat, lon) arrays - update Dataset class to allow 2D (lat, lon) arrays - update associated test in test_dataset.py to allow 2D arrays
Date Thu, 23 Apr 2015 20:48:57 GMT
Repository: climate
Updated Branches:
  refs/heads/master b258cce24 -> f836d76ef


CLIMATE-623 - Allow propogation of 2D arrays
- update subset function in DSP to subset 2D (lat,lon) arrays
- update Dataset class to allow 2D (lat,lon) arrays
- update associated test in test_dataset.py to allow 2D arrays


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

Branch: refs/heads/master
Commit: 126631dbe078e372ee36a41695152af28a2f99e0
Parents: b258cce
Author: Kim Whitehall <kim.d.whitehall@jpl.nasa.gov>
Authored: Tue Apr 21 16:07:39 2015 -0700
Committer: Kim Whitehall <kim.d.whitehall@jpl.nasa.gov>
Committed: Tue Apr 21 16:07:39 2015 -0700

----------------------------------------------------------------------
 ocw/dataset.py            | 24 ++++++++++------
 ocw/dataset_processor.py  | 62 ++++++++++++++++++++++++++++--------------
 ocw/tests/test_dataset.py |  2 +-
 3 files changed, 58 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/climate/blob/126631db/ocw/dataset.py
----------------------------------------------------------------------
diff --git a/ocw/dataset.py b/ocw/dataset.py
index d219d52..b5358f9 100644
--- a/ocw/dataset.py
+++ b/ocw/dataset.py
@@ -180,17 +180,23 @@ class Dataset:
             err_msg = "Longitude Array should be 1 dimensional. %s dimensions found." % lon_dim
         elif time_dim != 1:
             err_msg = "Time Array should be 1 dimensional.  %s dimensions found." % time_dim
-        elif value_dim != 3:
-            err_msg = "Value Array should be 3 dimensional.  %s dimensions found." % value_dim
+        elif value_dim < 2:
+            err_msg = "Value Array should be at least 2 dimensional.  %s dimensions found."
% value_dim
         # Finally check that the Values array conforms to the proper shape
-        elif values.shape != (time_count, lat_count, lon_count):
+        if value_dim == 2 and values.shape != (lat_count, lon_count):
             err_msg = """Value Array must be of shape (times, lats, lons).
-Expected shape (%s, %s, %s) but received (%s, %s, %s)""" % (time_count,
-                                                            lat_count,
-                                                            lon_count,
-                                                            values.shape[0],
-                                                            values.shape[1],
-                                                            values.shape[2])
+    Expected shape (%s, %s, %s) but received (%s, %s, %s)""" % (lat_count,
+                                                                lon_count,
+                                                                values.shape[0],
+                                                                values.shape[1])
+        if value_dim == 3 and values.shape != (time_count, lat_count, lon_count):
+            err_msg = """Value Array must be of shape (times, lats, lons).
+    Expected shape (%s, %s, %s) but received (%s, %s, %s)""" % (time_count,
+                                                                lat_count,
+                                                                lon_count,
+                                                                values.shape[0],
+                                                                values.shape[1],
+                                                                values.shape[2])
         if err_msg:
             logger.error(err_msg)
             raise ValueError(err_msg)

http://git-wip-us.apache.org/repos/asf/climate/blob/126631db/ocw/dataset_processor.py
----------------------------------------------------------------------
diff --git a/ocw/dataset_processor.py b/ocw/dataset_processor.py
index ec00262..76da817 100644
--- a/ocw/dataset_processor.py
+++ b/ocw/dataset_processor.py
@@ -184,26 +184,48 @@ def subset(subregion, target_dataset, subregion_name=None):
         subregion_name = target_dataset.name
 
     # Build new dataset with subset information
-    return ds.Dataset(
-        # Slice the lats array with our calculated slice indices
-        target_dataset.lats[dataset_slices["lat_start"]: 
-                            dataset_slices["lat_end"] + 1],
-        # Slice the lons array with our calculated slice indices
-        target_dataset.lons[dataset_slices["lon_start"]: 
-                            dataset_slices["lon_end"] + 1],
-        # Slice the times array with our calculated slice indices
-        target_dataset.times[dataset_slices["time_start"]: 
-                            dataset_slices["time_end"]+ 1],
-        # Slice the values array with our calculated slice indices
-        target_dataset.values[
-            dataset_slices["time_start"]:dataset_slices["time_end"] + 1,
-            dataset_slices["lat_start"]:dataset_slices["lat_end"] + 1,
-            dataset_slices["lon_start"]:dataset_slices["lon_end"] + 1],
-        variable=target_dataset.variable,
-        units=target_dataset.units,
-        name=subregion_name,
-        origin=target_dataset.origin
-    )
+    if target_dataset.values.ndim == 2:
+        return ds.Dataset(
+            # Slice the lats array with our calculated slice indices
+            target_dataset.lats[dataset_slices["lat_start"]: 
+                                dataset_slices["lat_end"] + 1],
+            # Slice the lons array with our calculated slice indices
+            target_dataset.lons[dataset_slices["lon_start"]: 
+                                dataset_slices["lon_end"] + 1],
+            # Slice the times array with our calculated slice indices
+            target_dataset.times[dataset_slices["time_start"]: 
+                                dataset_slices["time_end"]+ 1],
+            # Slice the values array with our calculated slice indices
+            target_dataset.values[
+                dataset_slices["lat_start"]:dataset_slices["lat_end"] + 1,
+                dataset_slices["lon_start"]:dataset_slices["lon_end"] + 1],
+            variable=target_dataset.variable,
+            units=target_dataset.units,
+            name=subregion_name,
+            origin=target_dataset.origin
+        )
+    if target_dataset.values.ndim == 3:
+        return ds.Dataset(
+            # Slice the lats array with our calculated slice indices
+            target_dataset.lats[dataset_slices["lat_start"]: 
+                                dataset_slices["lat_end"] + 1],
+            # Slice the lons array with our calculated slice indices
+            target_dataset.lons[dataset_slices["lon_start"]: 
+                                dataset_slices["lon_end"] + 1],
+            # Slice the times array with our calculated slice indices
+            target_dataset.times[dataset_slices["time_start"]: 
+                                dataset_slices["time_end"]+ 1],
+            # Slice the values array with our calculated slice indices
+            target_dataset.values[
+                dataset_slices["time_start"]:dataset_slices["time_end"] + 1,
+                dataset_slices["lat_start"]:dataset_slices["lat_end"] + 1,
+                dataset_slices["lon_start"]:dataset_slices["lon_end"] + 1],
+            variable=target_dataset.variable,
+            units=target_dataset.units,
+            name=subregion_name,
+            origin=target_dataset.origin
+        )
+
 
 def safe_subset(subregion, target_dataset, subregion_name=None):
     '''Safely subset given dataset with subregion information

http://git-wip-us.apache.org/repos/asf/climate/blob/126631db/ocw/tests/test_dataset.py
----------------------------------------------------------------------
diff --git a/ocw/tests/test_dataset.py b/ocw/tests/test_dataset.py
index bd9dbd6..777d07e 100644
--- a/ocw/tests/test_dataset.py
+++ b/ocw/tests/test_dataset.py
@@ -86,7 +86,7 @@ class TestInvalidDatasetInit(unittest.TestCase):
             Dataset(self.lat, self.lon, self.time, self.value, 'prec')
 
     def test_bad_values_shape(self):
-        self.value = np.array([[1, 2], [2, 3], [3, 4], [4, 5]])
+        self.value = np.array([1, 2, 3, 4, 5])
         with self.assertRaises(ValueError):
             Dataset(self.lat, self.lon, self.time, self.value, 'prec')
 


Mime
View raw message