Commits

Roberto De Almeida committed 8bf915c

Fix bug with code when there is no missing value attribute

A patch by Eric Firing that catches when missing value is not set.

Comments (0)

Files changed (1)

 
         data = empty(shape_, type)
         self.variables[name] = netcdf_variable(
-                data, type, shape, dimensions, 
+                data, type, shape, dimensions,
                 maskandscale=self.maskandscale)
         return self.variables[name]
 
         self.fp.seek(the_beguine)
 
         # Write data.
-        if (var.data.dtype.byteorder == '<' or 
+        if (var.data.dtype.byteorder == '<' or
                 (var.data.dtype.byteorder == '=' and LITTLE_ENDIAN)):
             var.data = var.data.byteswap()
 
 
     def __getitem__(self, index):
         if not self.maskandscale:
-            data = self.data
-        else:
-            missing_value = (
-                    self._attributes.get('missing_value') or
-                    self._attributes.get('_FillValue'))
-            data = np.ma.masked_values(self.data, missing_value)
-            data = data * self._attributes.get('scale_factor', 1.0) + self._attributes.get('add_offset', 0.0)
+            return self.data[index]
 
-        return data[index]
+        data = self.data[index].copy()
+        missing_value = (
+                self._attributes.get('missing_value') or
+                self._attributes.get('_FillValue'))
+        if missing_value is not None:
+            data = np.ma.masked_values(data, missing_value)
+        scale_factor = self._attributes.get('scale_factor')
+        add_offset = self._attributes.get('add_offset')
+        if add_offset is not None or scale_factor is not None:
+            data = data.astype(np.float64)
+        if scale_factor is not None:
+            data = data * scale_factor
+        if add_offset is not None:
+            data += add_offset
+
+        return data
 
     def __setitem__(self, index, data):
         if self.maskandscale: