Skip to content

Commit

Permalink
Merge pull request #24 from aodn/close_file_on_error
Browse files Browse the repository at this point in the history
Close file on error & allow dimensionless variables
  • Loading branch information
ocehugo authored Oct 17, 2018
2 parents 1b88d06 + 9c2bad9 commit 9deef78
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
19 changes: 12 additions & 7 deletions ncwriter/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def create_variables(self, **kwargs):
"""
for varname, var in self.variables.items():
datatype = var['type']
dimensions = var['dimensions']
dimensions = var.get('dimensions')
cwargs = kwargs.copy()
if dimensions is None: # no kwargs in createVariable
ncvar = self.ncobj.createVariable(varname, datatype)
Expand Down Expand Up @@ -351,10 +351,15 @@ def to_netcdf(self, outfile, var_args={}, **kwargs):
if not self.is_dim_consistent():
raise ValueError("Dimensions.")

self.ncobj = netCDF4.Dataset(self.outfile, mode='w', **kwargs)
self.create_dimensions()
self.create_variables(**var_args)
self.create_global_attributes()
self.ncobj.sync()
self.ncobj.close()
try:
self.ncobj = netCDF4.Dataset(self.outfile, mode='w', **kwargs)
self.create_dimensions()
self.create_variables(**var_args)
self.create_global_attributes()
self.ncobj.sync()
except Exception:
raise
finally:
self.ncobj.close()

self.ncobj = netCDF4.Dataset(self.outfile, 'a')
13 changes: 13 additions & 0 deletions test_ncwriter/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,19 @@ def test_fill_values_from_masked_array(self):
self.assertTrue(dsx[:5].mask.all())
self.assertTrue((dsx[5:] == x[5:]).all())

def test_close_file_on_exception(self):
template = DatasetTemplate.from_json(TEMPLATE_JSON)
self.assertIsNone(template.ncobj)
self.assertRaises(ValueError, template.to_netcdf, self.temp_nc_file)
self.assertFalse(template.ncobj.isopen())

def test_dimensionless_variable(self):
template = DatasetTemplate(variables={'X': {'type': 'double', 'data': None}})
template.to_netcdf(self.temp_nc_file)

dataset = Dataset(self.temp_nc_file)
self.assertEqual((), dataset.variables['X'].dimensions)

# TODO: add data from multiple numpy arrays
# e.g. template.add_data(TIME=time_values, TEMP=temp_values, PRES=pres_values)
# TODO: add data from Pandas dataframe (later...)
Expand Down

0 comments on commit 9deef78

Please sign in to comment.