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

needed for testing

parent 61072d7e
Branches
No related tags found
No related merge requests found
#!/usr/bin/env python
# io.py
"""
Author : peters
Revision History:
File created on 15 Oct 2008.
File modified for CT data assimilation system in July 2010, Wouter Peters
"""
import da.ct.standardvariables
import datetime as dt
import Nio
from numpy import array
import os
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"
conventions = "CF-1.1"
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':''}
class CT_CDF(object):
""" function opens a NetCDF/HDF/GRIB file for writing of output"""
def __init__(self,*arguments):
self.file = Nio.open_file(*arguments)
#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):
self.automode()
dimparams=self.def_dim('nparameters',nparams)
return (dimparams,)
def AddMembersDim(self,nmembers):
self.automode()
dimmembers=self.def_dim('nmembers',nmembers)
return (dimmembers,)
def AddLagDim(self,nlag,unlimited=True):
self.automode()
if unlimited:
dimlag =self.def_dim('nlag',CDF.NC.UNLIMITED)
else:
dimlag=self.def_dim('nlag',nlag)
return (dimlag,)
def AddObsDim(self,nobs):
self.automode()
dimobs=self.def_dim('nobs',nobs)
return (dimobs,)
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
lons=-180+arange(360)*1.0+0.5
lats=-90+arange(180)*1.0+0.5
#
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])
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,)
self.AddData(savedict)
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),)
def AddDateDimFormat(self):
self.automode()
if 'yyyymmddhhmmss' in self.dimensions(): return (self.dim('yyyymmddhhmmss'),) # already exists
return (self.def_dim('yyyymmddhhmmss',6),)
def has_date(self,dd):
if self.inq_unlimlen() > 0:
if dd in self.GetVariable('date').tolist():
return True
else:
return False
else:
return False
def GetVariable(self,varname):
""" get variable from ncf file"""
return array(self.var(varname).get())
def StandardVar(self,varname):
""" return properties of standard variables """
import standardvariables
if varname in standardvariables.standard_variables.keys():
return standardvariables.standard_variables[varname]
else:
return standardvariables.standard_variables['unknown']
def AddData(self,datadict,nsets=1,silent=True):
""" add fields to file, at end of unlimited dimension"""
existing_vars=self.variables()
try:
next = datadict['count']
except:
next=0
if existing_vars.has_key(datadict['name']):
var = self.var(datadict['name'])
var[next:next+nsets]=datadict['values']
else:
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'])
elif datadict['dtype'] == 'char':
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'])
else:
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)
if var.isrecord():
var[next:next+nsets]=datadict['values']
else:
var[:]=datadict['values']
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)
if not os.path.exists(dirname):
print "Creating new output directory "+dirname
os.makedirs(dirname)
else:
print 'Writing files to directory: %s'%(dirname,)
return dirname
if __name__ == '__main__':
import sys
sys.path.append('../../')
ncf=CT_CDF('test.nc','c')
#dimgrid=ncf.AddLatLonDim()
#dimdate=ncf.AddDateDim()
#dimidate=ncf.AddDateDimFormat()
standard_variables = { 'bio_flux_prior' : {'name' : 'bio_flux_prior',\
'units' : 'mol m-2 s-1' ,\
'long_name' : 'Surface flux of carbon dioxide, terrestrial vegetation, not optimized ', \
'comment' : 'time-interval average, centered on times in the date axis', \
'standard_name' : 'surface_carbon_dioxide_mole_flux', \
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'bio_flux_opt' : {'name' : 'bio_flux_opt',\
'units' : 'mol m-2 s-1' ,\
'long_name' : 'Surface flux of carbon dioxide, terrestrial biosphere , optimized ', \
'comment' : 'time-interval average, centered on times in the date axis', \
'standard_name' : 'surface_carbon_dioxide_mole_flux', \
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'ocn_flux_prior' : {'name' : 'ocn_flux_prior',\
'units' : 'mol m-2 s-1' ,\
'long_name' : 'Surface flux of carbon dioxide, open ocean , not optimized ', \
'comment' : 'time-interval average, centered on times in the date axis', \
'standard_name' : 'surface_carbon_dioxide_mole_flux', \
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'ocn_flux_opt' : {'name' : 'ocn_flux_opt',\
'units' : 'mol m-2 s-1' ,\
'long_name' : 'Surface flux of carbon dioxide, open ocean , optimized ', \
'comment' : 'time-interval average, centered on times in the date axis', \
'standard_name' : 'surface_carbon_dioxide_mole_flux', \
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'fossil_flux_imp' : {'name' : 'fossil_flux_imp',\
'units' : 'mol m-2 s-1' ,\
'long_name' : 'Surface flux of carbon dioxide, fossil fuel burning , imposed ', \
'comment' : 'time-interval average, centered on times in the date axis', \
'standard_name' : 'surface_carbon_dioxide_mole_flux', \
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'fire_flux_imp' : {'name' : 'fire_flux_imp',\
'units' : 'mol m-2 s-1' ,\
'long_name' : 'Surface flux of carbon dioxide, biomass burning , imposed ', \
'comment' : 'time-interval average, centered on times in the date axis', \
'standard_name' : 'surface_carbon_dioxide_mole_flux', \
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'bio_flux_prior_cov' : {'name' : 'bio_flux_prior_cov',\
'units' : 'mol2 region-2 s-2' ,\
'long_name' : 'Covariance of surface flux of carbon dioxide, terrestrial vegetation , not optimized ', \
'comment' : 'time-interval average, centered on times in the date axis', \
'standard_name' : '', \
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'bio_flux_opt_cov' : {'name' : 'bio_flux_opt_cov',\
'units' : 'mol2 region-2 s-2' ,\
'long_name' : 'Covariance of surface flux of carbon dioxide, terrestrial vegetation , optimized ', \
'comment' : 'time-interval average, centered on times in the date axis', \
'standard_name' : '', \
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'ocn_flux_prior_cov' : {'name' : 'ocn_flux_prior_cov',\
'units' : 'mol2 region-2 s-2' ,\
'long_name' : 'Covariance of surface flux of carbon dioxide, open ocean , not optimized ', \
'comment' : 'time-interval average, centered on times in the date axis', \
'standard_name' : '', \
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'ocn_flux_opt_cov' : {'name' : 'ocn_flux_opt_cov',\
'units' : 'mol2 region-2 s-2' ,\
'long_name' : 'Covariance of surface flux of carbon dioxide, open ocean , optimized ', \
'comment' : 'time-interval average, centered on times in the date axis', \
'standard_name' : '', \
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'decimal_date' : {'name' : 'decimal_date',\
'units' : 'years' ,\
'long_name' : 'dates and times', \
'comment' : 'time-interval average, centered on times in the date axis', \
'standard_name' : 'date', \
'dims' : (), \
'dtype' : 'double', \
'values' : [], \
'count' : 0 \
} , \
'date' : {'name' : 'date',\
'units' : 'days since 2000-01-01 00:00:00 UTC' ,\
'long_name' : 'UTC dates and times', \
'comment' : 'time-interval average, centered on times in the date axis', \
'standard_name' : 'date', \
'dims' : (), \
'dtype' : 'double', \
'values' : [], \
'count' : 0 \
} , \
'idate' : {'name' : 'idate',\
'units' : 'yyyy MM dd hh mm ss ' ,\
'long_name' : 'integer components of date and time', \
'standard_name' : 'calendar_components', \
'comment' : 'time-interval average, centered on times in the date axis', \
'dims' : (), \
'dtype' : 'int', \
'values' : [], \
'count' : 0 \
} , \
'latitude' : {'name' : 'latitude',\
'units' : 'degrees_north ' ,\
'long_name' : 'latitude', \
'standard_name' : 'latitude', \
'comment' : 'center of interval',\
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'longitude' : {'name' : 'longitude',\
'units' : 'degrees_east ' ,\
'long_name' : 'longitude', \
'standard_name' : 'longitude', \
'comment' : 'center of interval',\
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'height' : {'name' : 'height',\
'units' : 'masl ' ,\
'long_name' : 'height_above_ground_level', \
'standard_name' : 'height_above_ground_level', \
'comment' : 'value is meters above sea level',\
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'co2' : {'name' : 'co2',\
'units' : 'micromol mol-1 ' ,\
'long_name' : 'mole_fraction_of_carbon_dioxide_in_air', \
'standard_name' : 'mole_fraction_of_carbon_dioxide_in_air', \
'comment' : '',\
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'meanstate' : {'name' : 'statevectormean',\
'units' : 'unitless' ,\
'long_name' : 'mean_value_of_state_vector', \
'standard_name' : 'mean_value_of_state_vector', \
'comment' : '',\
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'ensemblestate': {'name' : 'statevectorensemble',\
'units' : 'unitless' ,\
'long_name' : 'ensemble_value_of_state_vector', \
'standard_name' : 'ensemble_value_of_state_vector', \
'comment' : '',\
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
'unknown' : {'name' : '',\
'units' : '' ,\
'long_name' : '', \
'standard_name' : '', \
'comment' : '',\
'dims' : (), \
'values' : [], \
'count' : 0 \
} , \
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment