Skip to content

Commit

Permalink
missing_value = 'N/A' led to fail (#84)
Browse files Browse the repository at this point in the history
* missing_value = 'N/A' led to fail

* added test to make sure reading in var with ASCII missing val will work

* fix for mac
  • Loading branch information
doutriaux1 authored Feb 10, 2017
1 parent 28e5996 commit 8ad09a2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
18 changes: 15 additions & 3 deletions Lib/avariable.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ def __init__ (self, parent=None, variableNode=None):
self._grid_ = None # Variable grid, if any
if not hasattr(self,'missing_value'):
self.missing_value = None
elif numpy.isnan(self.missing_value):
self.missing_value = None
else:
if isinstance(self.missing_value,basestring):
self.missing_value = None
elif numpy.isnan(self.missing_value):
self.missing_value = None

# Reminder: children to define self.shape and set self.id

Expand Down Expand Up @@ -418,7 +421,16 @@ def getMissing(self, asarray=0):
if asarray==0 and isinstance(mv, numpy.ndarray):
mv = mv[0]
if type(mv) is types.StringType and self.dtype.char not in ['?','c','O','S']:
mv = float(mv)
try:
mv = float(mv)
except:
if hasattr(self,'_FillValue'):
try:
mv = float(self._FillValue)
except:
mv = None
else:
mv = None
return mv

def _setmissing(self, name, value):
Expand Down
50 changes: 50 additions & 0 deletions Test/test_missing_ascii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import basetest
import subprocess
import cdms2
import os

class TestMissingASCII(basetest.CDMSBaseTest):
def testMissingASCII(self):
nc_dump = """
netcdf tmp_test {
dimensions:
axis_0 = 2 ;
axis_1 = 2 ;
variables:
double axis_0(axis_0) ;
double axis_1(axis_1) ;
double var(axis_0, axis_1) ;
var:missing_value = "N/A" ;
// global attributes:
:Conventions = "CF-1.0" ;
data:
axis_0 = 0, 1 ;
axis_1 = 0, 1 ;
var =
1, 1,
1, 1 ;
}
"""

f=open("tmp_test.asc","w")
f.write(nc_dump)
f.close()

subprocess.call("ncgen -b tmp_test.asc".split())

f=cdms2.open("tmp_test.nc")
v=f("var")
f.close()

self.assertEqual(v.missing_value,1.e20)

os.remove("tmp_test.asc")
os.remove("tmp_test.nc")


if __name__ == "__main__":
basetest.run()

0 comments on commit 8ad09a2

Please sign in to comment.