Skip to content
Snippets Groups Projects
Commit 0dde0adb authored by Peters, Wouter's avatar Peters, Wouter
Browse files

Added a new function to create a variable inside a file without adding any data

Changed the default FillValue for characters from 'x' to '!' as 'x' was used in site names for NOAA (oxk)
parent 7c64acae
No related branches found
No related tags found
No related merge requests found
......@@ -269,6 +269,38 @@ class CT_CDF(netCDF4.Dataset):
return False
def AddVariable(self,datadict,silent=True):
""" add variables to file, but no data"""
import numpy as np
existing_vars=self.variables
if existing_vars.has_key(datadict['name']):
return
else:
if not silent: print 'Creating new dataset: '+datadict['name']
if datadict.has_key('dtype'):
if datadict['dtype'] == 'int':
var = self.createVariable(datadict['name'],'i4',datadict['dims'])
elif datadict['dtype'] == 'int64':
var = self.createVariable(datadict['name'],'i8',datadict['dims'])
elif datadict['dtype'] == 'char':
var = self.createVariable(datadict['name'],'S1',datadict['dims'],fill_value='!')
elif datadict['dtype'] == 'float':
var = self.createVariable(datadict['name'],'f4',datadict['dims'])
elif datadict['dtype'] == 'double':
var = self.createVariable(datadict['name'],'f8',datadict['dims'])
else:
var = self.createVariable(datadict['name'],'f8',datadict['dims'])
else:
var = self.createVariable(datadict['name'],'f4',datadict['dims'])
for k,v in datadict.iteritems():
if k not in ['name','dims','values','_FillValue','count']:
var.setncattr(k,v)
def AddData(self,datadict,nsets=1,silent=True):
""" add fields to file, at end of unlimited dimension"""
import numpy as np
......@@ -310,7 +342,7 @@ class CT_CDF(netCDF4.Dataset):
elif datadict['dtype'] == 'int64':
var = self.createVariable(datadict['name'],'i8',datadict['dims'])#,fill_value=datadict['_FillValue'])
elif datadict['dtype'] == 'char':
var = self.createVariable(datadict['name'],'S1',datadict['dims'],fill_value='x')
var = self.createVariable(datadict['name'],'S1',datadict['dims'],fill_value='!')
elif datadict['dtype'] == 'float':
var = self.createVariable(datadict['name'],'f4',datadict['dims'])#,fill_value=datadict['_FillValue'])
elif datadict['dtype'] == 'double':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment