You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@climate.apache.org by jo...@apache.org on 2013/08/20 19:58:00 UTC

svn commit: r1515902 - /incubator/climate/trunk/ocw/dataset.py

Author: joyce
Date: Tue Aug 20 17:58:00 2013
New Revision: 1515902

URL: http://svn.apache.org/r1515902
Log:
CLIMATE-267 - Add input validation to Dataset

- Dataset checks that the input arrays have the proper shape and that
  the values array shape matches the lengths of the relevant component
  arrays.

Modified:
    incubator/climate/trunk/ocw/dataset.py

Modified: incubator/climate/trunk/ocw/dataset.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/ocw/dataset.py?rev=1515902&r1=1515901&r2=1515902&view=diff
==============================================================================
--- incubator/climate/trunk/ocw/dataset.py (original)
+++ incubator/climate/trunk/ocw/dataset.py Tue Aug 20 17:58:00 2013
@@ -46,7 +46,13 @@ class Dataset:
         :type variable: string
         :param name: An optional string name for the Dataset.
         :type variable: string
+
+        :raises: ValueError
         '''
+        if self._inputs_are_invalid(lats, lons, times, values):
+            err = "Dataset given improperly shaped array during initialization."
+            raise ValueError(err)
+
         self.lats = lats
         self.lons = lons
         self.times = times
@@ -131,6 +137,22 @@ class Dataset:
 
         return time_resolution
 
+    def _inputs_are_invalid(self, lats, lons, times, values):
+        '''Check if Dataset input values are expected shape.
+        
+        :returns: True if the values are invalid, False otherwise.
+        '''
+        lats_shape = lats.shape
+        lons_shape = lons.shape
+        times_shape = times.shape
+        values_shape = values.shape
+
+        return (
+            len(lats_shape) != 1 or len(lons_shape) != 1 or 
+            len(times_shape) != 1 or len(values_shape) != 3 or
+            values_shape != (times_shape[0], lats_shape[0], lons_shape[0])
+        )
+
 
 class Bounds(object):
     '''Container for holding spatial and temporal bounds information.