Commit ad278492 authored by karolina's avatar karolina
Browse files

No commit message

No commit message
parent 98e0e626
This diff is collapsed.
This diff is collapsed.
......@@ -15,131 +15,117 @@ import datetime as dt
from numpy import array
import os
disclaimer = "This data belongs to the CarbonTracker project"
email = "wouter.peters@wur.nl"
url = "http://carbontracker.wur.nl"
disclaimer = "This data belongs to the CarbonTracker project"
email = "wouter.peters@wur.nl"
url = "http://carbontracker.wur.nl"
institution = "Wageningen University and Research Center"
source = "CarbonTracker release 2.0"
source = "CarbonTracker release 2.0"
conventions = "CF-1.1"
historytext = 'Created on '+dt.datetime.now().strftime('%B %d, %Y')+' by %s'%os.environ['USER']
historytext = 'Created on ' + dt.datetime.now().strftime('%B %d, %Y') + ' by %s' % os.environ['USER']
std_savedict={'name':'unknown','values':[],'dims':(0,0,),'units':'','long_name':'','_FillValue':float(-999.),'comment':''}
std_savedict = {'name':'unknown', 'values':[], 'dims':(0, 0,), 'units':'', 'long_name':'', '_FillValue':float(-999.), 'comment':''}
class CT_CDF(CDF.CDF):
""" function opens a NetCDF/HDF/GRIB file for writing of output"""
def __init__(self,filename, method='read'):
if method not in ['read','write','create']:
def __init__(self, filename, method='read'):
if method not in ['read', 'write', 'create']:
raise ValueError, 'Method %s is not defined for a CarbonTracker NetCDF file object' % method
if method == 'read':
print 'Reading from file'
super(CDF.CDF,self).__init__(filename, CDF.NC.NOWRITE)
super(CDF.CDF, self).__init__(filename, CDF.NC.NOWRITE)
elif method == 'write':
#print 'Adding to existing file'
super(CT_CDF,self).__init__(filename, CDF.NC.WRITE|CDF.NC.CREATE)
super(CT_CDF, self).__init__(filename, CDF.NC.WRITE | CDF.NC.CREATE)
self.AddCTHeader()
elif method == 'create':
#print 'Creating new file'
super(CT_CDF,self).__init__(filename, CDF.NC.WRITE|CDF.NC.TRUNC|CDF.NC.CREATE)
super(CT_CDF, self).__init__(filename, CDF.NC.WRITE | CDF.NC.TRUNC | CDF.NC.CREATE)
self.AddCTHeader()
def AddCTHeader(self):
self.automode()
#
setattr(self,'Institution',institution)
setattr(self,'Contact',email)
setattr(self,'URL',url)
setattr(self,'Source',source)
setattr(self,'Convention',conventions)
setattr(self,'Disclaimer',disclaimer)
setattr(self,'History',historytext)
def AddParamsDim(self,nparams):
setattr(self, 'Institution', institution)
setattr(self, 'Contact', email)
setattr(self, 'URL', url)
setattr(self, 'Source', source)
setattr(self, 'Convention', conventions)
setattr(self, 'Disclaimer', disclaimer)
setattr(self, 'History', historytext)
def AddParamsDim(self, nparams):
self.automode()
dimparams=self.def_dim('nparameters',nparams)
dimparams = self.def_dim('nparameters', nparams)
return (dimparams,)
def AddMembersDim(self,nmembers):
def AddMembersDim(self, nmembers):
self.automode()
dimmembers=self.def_dim('nmembers',nmembers)
dimmembers = self.def_dim('nmembers', nmembers)
return (dimmembers,)
def AddLagDim(self,nlag,unlimited=True):
def AddLagDim(self, nlag, unlimited=True):
self.automode()
if unlimited:
dimlag =self.def_dim('nlag',CDF.NC.UNLIMITED)
dimlag = self.def_dim('nlag', CDF.NC.UNLIMITED)
else:
dimlag=self.def_dim('nlag',nlag)
dimlag = self.def_dim('nlag', nlag)
return (dimlag,)
def AddObsDim(self,nobs):
def AddObsDim(self, nobs):
self.automode()
dimobs=self.def_dim('nobs',nobs)
dimobs = self.def_dim('nobs', nobs)
return (dimobs,)
def AddLatLonDim(self,istart=0,iend=360,jstart=0,jend=180):
def AddLatLonDim(self, istart=0, iend=360, jstart=0, jend=180):
from numpy import arange, float64
if 'latitude' in self.dimensions(): return (self.dim('latitude'),self.dim('longitude'),) # already exists
if 'latitude' in self.dimensions():
return (self.dim('latitude'), self.dim('longitude')) # already exists
lons=-180+arange(360)*1.0+0.5
lats=-90+arange(180)*1.0+0.5
lons = -180 + arange(360) * 1.0 + 0.5
lats = -90 + arange(180) * 1.0 + 0.5
#
lats=lats[jstart:jend]
lons=lons[istart:iend]
lats = lats[jstart:jend]
lons = lons[istart:iend]
#
self.automode()
dimlon=self.def_dim('longitude',lons.shape[0])
dimlat=self.def_dim('latitude',lats.shape[0])
dimlon = self.def_dim('longitude', lons.shape[0])
dimlat = self.def_dim('latitude', lats.shape[0])
savedict=self.StandardVar(varname='latitude')
savedict['values']=lats.tolist()
savedict['actual_range']=(float(lats[0]),float(lats[-1]))
savedict['dims']=(dimlat,)
savedict = self.StandardVar(varname='latitude')
savedict['values'] = lats.tolist()
savedict['actual_range'] = (float(lats[0]), float(lats[-1]))
savedict['dims'] = (dimlat,)
self.AddData(savedict)
savedict=self.StandardVar(varname='longitude')
savedict['values']=lons.tolist()
savedict['actual_range']=(float(lons[0]),float(lons[-1]))
savedict['dims']=(dimlon,)
savedict = self.StandardVar(varname='longitude')
savedict['values'] = lons.tolist()
savedict['actual_range'] = (float(lons[0]), float(lons[-1]))
savedict['dims'] = (dimlon,)
self.AddData(savedict)
return (dimlat,dimlon,)
return (dimlat, dimlon)
def AddDateDim(self):
self.automode()
if 'date' in self.dimensions(): return (self.dim('date'),)
return (self.def_dim('date',CDF.NC.UNLIMITED),)
if 'date' in self.dimensions():
return (self.dim('date'),)
return (self.def_dim('date', CDF.NC.UNLIMITED),)
def AddDateDimFormat(self):
self.automode()
if 'yyyymmddhhmmss' in self.dimensions(): return (self.dim('yyyymmddhhmmss'),) # already exists
return (self.def_dim('yyyymmddhhmmss',6),)
if 'yyyymmddhhmmss' in self.dimensions():
return (self.dim('yyyymmddhhmmss'),) # already exists
return (self.def_dim('yyyymmddhhmmss', 6),)
def AddDim(self,dimname,dimsize):
if dimname in self.dimensions():
pass
else:
newdim = self.def_dim(dimname,dimsize)
def AddDim(self, dimname, dimsize):
if dimname not in self.dimensions():
newdim = self.def_dim(dimname, dimsize)
return (newdim,)
def has_date(self,dd):
def has_date(self, dd):
if self.inq_unlimlen() > 0:
if dd in self.GetVariable('date').tolist():
return True
......@@ -148,11 +134,11 @@ class CT_CDF(CDF.CDF):
else:
return False
def GetVariable(self,varname):
def GetVariable(self, varname):
""" get variable from ncf file"""
return array(self.var(varname).get())
def StandardVar(self,varname):
def StandardVar(self, varname):
""" return properties of standard variables """
import standardvariables
......@@ -161,65 +147,60 @@ class CT_CDF(CDF.CDF):
else:
return standardvariables.standard_variables['unknown']
def AddData(self,datadict,nsets=1,silent=True):
def AddData(self, datadict, nsets=1, silent=True):
""" add fields to file, at end of unlimited dimension"""
existing_vars=self.variables()
existing_vars = self.variables()
try:
next = datadict['count']
except:
next=0
next = 0
if existing_vars.has_key(datadict['name']):
var = self.var(datadict['name'])
var[next:next+nsets]=datadict['values']
var[next:next + nsets] = datadict['values']
else:
if not silent: print 'Creating new dataset: '+datadict['name']
if not silent:
print 'Creating new dataset: ' + datadict['name']
if datadict.has_key('dtype'):
if datadict['dtype'] == 'int':
var = self.def_var(datadict['name'],CDF.NC.INT,datadict['dims'])
var = self.def_var(datadict['name'], CDF.NC.INT, datadict['dims'])
elif datadict['dtype'] == 'char':
var = self.def_var(datadict['name'],CDF.NC.CHAR,datadict['dims'])
var = self.def_var(datadict['name'], CDF.NC.CHAR, datadict['dims'])
elif datadict['dtype'] == 'double':
var = self.def_var(datadict['name'],CDF.NC.DOUBLE,datadict['dims'])
var = self.def_var(datadict['name'], CDF.NC.DOUBLE, datadict['dims'])
else:
var = self.def_var(datadict['name'],CDF.NC.FLOAT,datadict['dims'])
var = self.def_var(datadict['name'], CDF.NC.FLOAT, datadict['dims'])
else:
var = self.def_var(datadict['name'],CDF.NC.FLOAT,datadict['dims'])
for k,v in datadict.iteritems():
if k not in ['name','dims','values','_FillValue','count']:
setattr(var,k,v)
var = self.def_var(datadict['name'], CDF.NC.FLOAT, datadict['dims'])
for k, v in datadict.iteritems():
if k not in ['name', 'dims', 'values', '_FillValue', 'count']:
setattr(var, k, v)
if var.isrecord():
var[next:next+nsets]=datadict['values']
var[next:next + nsets] = datadict['values']
else:
var[:]=datadict['values']
var[:] = datadict['values']
def GetVariable(file,varname):
def GetVariable(file, varname):
""" get variable from HDF file"""
return array(file.select(varname).get())
def CreateDirs(rundat,dirname):
dirname=os.path.join(rundat.outputdir,dirname)
def CreateDirs(rundat, dirname):
dirname = os.path.join(rundat.outputdir, dirname)
if not os.path.exists(dirname):
print "Creating new output directory "+dirname
print "Creating new output directory " + dirname
os.makedirs(dirname)
else:
print 'Writing files to directory: %s'%(dirname,)
print 'Writing files to directory: %s' % (dirname,)
return dirname
if __name__ == '__main__':
try:
os.remove('test.nc')
except:
pass
ncf=CT_CDF('test.nc','create')
dimgrid=ncf.AddLatLonDim()
dimdate=ncf.AddDateDim()
dimidate=ncf.AddDateDimFormat()
ncf = CT_CDF('test.nc', 'create')
dimgrid = ncf.AddLatLonDim()
dimdate = ncf.AddDateDim()
dimidate = ncf.AddDateDimFormat()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment