Skip to content

Commit a37f894

Browse files
committed
Handle case where variable data not provided or is None
1 parent 038091b commit a37f894

4 files changed

Lines changed: 27 additions & 3 deletions

File tree

ncwriter/schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"items": {"type": "string", "pattern": NAME_PATTERN}
3939
},
4040
"type": {"type": "string"},
41-
"attributes": ATTRIBUTES_SCHEMA
41+
"attributes": ATTRIBUTES_SCHEMA,
42+
"data": {"type": ["null", "array"]}
4243
},
4344
"additionalProperties": False
4445
}

ncwriter/template.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,10 @@ def createVariables(self, **kwargs):
407407
varname, datatype, dimensions=dimensions, **var_c_opts)
408408

409409
# add variable values
410-
ncvar[:] = var['data']
410+
if 'data' not in var:
411+
raise ValueError('No data specified for variable {varname}'.format(varname=varname))
412+
if var['data'] is not None:
413+
ncvar[:] = var['data']
411414

412415
# add variable attributes
413416
if var.get('attributes'):

test_ncwriter/test_schema.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ def test_validate_variables(self):
3232
validate_variables({'X': {'type': 'float32'}})
3333
validate_variables({'X': {'dimensions': []}})
3434
validate_variables({'X': {'attributes': {'name': 'X'}}})
35+
validate_variables({'X': {'data': None}})
3536
validate_variables({'X': {'dimensions': ['X'], 'type': 'float32'}})
3637
validate_variables({
3738
'X': {
3839
'dimensions': ['X'],
3940
'type': 'float32',
40-
'attributes': {'name': 'X', 'count': 1}
41+
'attributes': {'name': 'X', 'count': 1},
42+
'data': [42]
4143
}
4244
})
4345

@@ -51,6 +53,8 @@ def test_validate_variables(self):
5153
validate_variables({'X': {'type': 'float32', 'something': 'else'}})
5254
with self.assertRaises(ValidationError):
5355
validate_variables({'X': {'type': 'float32', 'attributes': 'none'}})
56+
with self.assertRaises(ValidationError):
57+
validate_variables({'X': {'data': 100}})
5458

5559
def test_validate_attributes(self):
5660
validate_attributes({})

test_ncwriter/test_template.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ def test_create_empty_file(self):
140140
template.to_netcdf(self.temp_nc_file)
141141
dataset = Dataset(self.temp_nc_file)
142142

143+
def test_create_empty_variable(self):
144+
template = DatasetTemplate(dimensions={'X': 10})
145+
template.variables['X'] = {'dimensions': ['X'], 'type': 'float32'}
146+
self.assertRaises(ValueError, template.to_netcdf, self.temp_nc_file) # not providing 'data' is an error
147+
148+
del self._temp_nc_file # Get a new temp file
149+
template.variables['X']['data'] = None # This is ok, it's a shortcut for all fill values
150+
template.to_netcdf(self.temp_nc_file)
151+
152+
dataset = Dataset(self.temp_nc_file)
153+
dataset.set_auto_mask(True)
154+
dsx = dataset.variables['X']
155+
self.assertIsInstance(dsx[:], np.ma.MaskedArray)
156+
self.assertTrue(dsx[:].mask.all())
157+
143158
def test_create_file(self):
144159
template = DatasetTemplate.from_json(TEMPLATE_JSON)
145160
template.variables['TIME']['data'] = self.values10
@@ -184,6 +199,7 @@ def test_fill_values(self):
184199
template.to_netcdf(self.temp_nc_file)
185200

186201
dataset = Dataset(self.temp_nc_file)
202+
dataset.set_auto_mask(True)
187203
dsx = dataset.variables['X']
188204
self.assertEqual(-999., dsx._FillValue)
189205
self.assertIsInstance(dsx[:], np.ma.MaskedArray)

0 commit comments

Comments
 (0)