Skip to content

Commit

Permalink
Merge pull request #42 from cedadev/bug_fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
RosalynHatcher authored Mar 19, 2018
2 parents 23e2566 + beebfa6 commit c0c16ea
Show file tree
Hide file tree
Showing 5 changed files with 764 additions and 13 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@
entry_points= {
'console_scripts': ['cfchecks = cfchecker.cfchecks:main'],
},
scripts=['src/cf-checker']
)
67 changes: 55 additions & 12 deletions src/cfchecker/cfchecks.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

from netCDF4 import Dataset as netCDF4_Dataset
from netCDF4 import Variable as netCDF4_Variable
from netCDF4 import VLType as netCDF4_VLType

from cfunits import Units

Expand Down Expand Up @@ -580,6 +581,16 @@ def _checker(self):

self._add_debug("Axes: %s" % axes)

valid_types=[numpy.character,
numpy.dtype('c'),
numpy.dtype('b'),
numpy.dtype('i4'),
numpy.int32,
numpy.float32,
numpy.double,
'int16',
'float32']

# Check each variable
for var in self.f.variables.keys():

Expand All @@ -593,6 +604,14 @@ def _checker(self):
if not self.validName(var):
self._add_error("Invalid variable name", var, code='2.3')

dt = self.f.variables[var].dtype
if dt not in valid_types:
try:
if isinstance(self.f.variables[var].datatype, netCDF4_VLType):
self._add_error("Invalid variable type: {} (vlen types not supported)".format(self.f.variables[var].datatype), var, code="2.2")
except:
self._add_error("Invalid variable type: {}".format(dt), var, code="2.2")

# Check to see if a variable with this name already exists (case-insensitive)
lowerVar=var.lower()
if lowerVar in lowerVars:
Expand Down Expand Up @@ -1023,7 +1042,7 @@ def getCoordinateDataVars(self):
# self._add_warn("Data for variable %s lies outside cell boundaries" % var,
# var, code="7.1")
# else:
for i, value in enumerate(varData):
for i, value in (enumerate(varData) if len(varData.shape) else enumerate([varData])):
try:
if not (boundsData[i][0] <= value <= boundsData[i][1]):
self._add_warn("Data for variable %s lies outside cell boundaries" % var,
Expand Down Expand Up @@ -1234,7 +1253,7 @@ def chkGridMappingVar(self, varName):
attr_type='N'

else:
self._add_info("Unknown Type for attribute: %s %s" % (attribute, attr_type))
self._add_info("Invalid Type for attribute: %s %s" % (attribute, attr_type))
continue

if (attribute in self.grid_mapping_attrs.keys() and
Expand Down Expand Up @@ -1623,8 +1642,22 @@ def getTypeCode(self, obj):
# return obj.dtype.char

# print "RSH: type ", obj.dtype.char

return obj.dtype.char

if isinstance(obj, netCDF4_Variable):
# Variable object
if isinstance(obj.datatype, netCDF4_VLType):
# VLEN types not supported
return 'vlen'

try:
typecode = obj.dtype.char
except AttributeError as e:
self._add_warn("Problem getting typecode: {}".format(e), obj.name)
else:
# Attribute object
typecode = obj.dtype.char

return typecode


#-------------------------------------------------------
Expand All @@ -1640,7 +1673,14 @@ def chkAttribute(self, attribute,varName,allCoordVars):
varName)
return

value=var.getncattr(attribute)
try:
value=var.getncattr(attribute)
except KeyError as e:
self._add_error("{} - {}".format(attribute,e), varName, code="2.2")
if self.AttrList.has_key(attribute):
# This is a standard attribute so inform user no further checks being made on it
self._add_info("No further checks made on attribute: {}".format(attribute), varName)
return

self._add_debug("chkAttribute: Checking attribute - %s" % attribute, varName)

Expand All @@ -1661,7 +1701,7 @@ def chkAttribute(self, attribute,varName,allCoordVars):
elif attrType == types.NoneType:
attrType='NoneType'
else:
self._add_info("Unknown Type for attribute: %s %s" % (attribute, attrType))
self._add_info("Invalid Type for attribute: %s %s" % (attribute, attrType))

# If attrType = 'NoneType' then it has been automatically created e.g. missing_value
typeError=0
Expand Down Expand Up @@ -2183,12 +2223,15 @@ def chkUnits(self,varName,allCoordVars):

#print "RSH: in allCoordVars"
# Label variables do not require units attribute
if self.f.variables[varName].dtype.char != 'S':
if hasattr(var, 'axis'):
if not var.axis == 'Z':
try:
if self.f.variables[varName].dtype.char != 'S':
if hasattr(var, 'axis'):
if not var.axis == 'Z':
self._add_warn("units attribute should be present", varName, code="3.1")
elif not hasattr(var,'positive') and not hasattr(var,'formula_terms') and not hasattr(var,'compress'):
self._add_warn("units attribute should be present", varName, code="3.1")
elif not hasattr(var,'positive') and not hasattr(var,'formula_terms') and not hasattr(var,'compress'):
self._add_warn("units attribute should be present", varName, code="3.1")
except:
pass

elif varName not in self.boundsVars and varName not in self.climatologyVars and varName not in self.gridMappingVars:
# Variable is not a boundary or climatology variable
Expand Down Expand Up @@ -2653,7 +2696,7 @@ def chkFlags(self, varName):
varName, code="3.5")

# flag_values values must be mutually exclusive
if type(values) == str:
if isinstance(values, basestring):
values = values.split()

if not self.uniqueList(values):
Expand Down
Loading

0 comments on commit c0c16ea

Please sign in to comment.