Commit 511839d3 authored by Peters, Wouter's avatar Peters, Wouter
Browse files

put in a new method to read file, decided itself for HDF4 or NetCDF format

parent fd9489a5
......@@ -6,7 +6,7 @@ import getopt
from pylab import array, arange, transpose, date2num
from runinfo import *
from datetime import datetime
from da.tools.io4 import CT_CDF, std_savedict, GetVariable
from da.tools.io4 import CT_Read, CT_CDF, std_savedict, GetVariable
from da.tools.general import CreateDirs
"""
......@@ -87,7 +87,7 @@ def SaveWeeklyAvg1x1Data(rundat):
#
filename = os.path.join(rundat.inputdir,'%s'%week.strftime('%Y%m%d'),'flux1x1_%s_%s.nc'%(week.strftime('%Y%m%d%H'),(week+dt).strftime('%Y%m%d%H'),) )
file=io.CT_CDF(filename,'read')
file=io.CT_Read(filename,'read')
bio=array(file.GetVariable(rundat.namedict['background.co2.bio.flux']))
ocean=array(file.GetVariable(rundat.namedict['background.co2.ocean.flux']))
fire=array(file.GetVariable(rundat.namedict['background.co2.fires.flux']))
......@@ -362,7 +362,7 @@ def SaveWeeklyAvgTCData(rundat):
print "Needed input file (%s) does not exist yet, please create weekly eco flux files first, returning..."%infile
return None
ncf_in = io.CT_CDF(infile)
ncf_in = io.CT_Read(infile)
vardict = ncf_in.variables
for vname, vprop in vardict.iteritems():
......@@ -430,7 +430,7 @@ def SaveTCDataExt(rundat):
dimregs = ncf.AddRegionDim(type='tc_ext')
ncf_in=io.CT_CDF(infile,'read')
ncf_in = io.CT_Read(infile,'read')
vardict = ncf_in.variables
for vname, vprop in vardict.iteritems():
......@@ -558,7 +558,7 @@ def SaveTimeAvgData(rundat,infile,avg='monthly'):
else:
pass
file = io.CT_CDF(infile,'read')
file = io.CT_Read(infile,'read')
datasets = file.variables.keys()
date = file.GetVariable('date')
globatts = file.ncattrs()
......
......@@ -85,7 +85,7 @@ class RunInfo(object):
self.nweeks = len(self.weeks)
# run parameters from file
ncf = io.CT_CDF(self.inputfiles[0],'read')
ncf = io.CT_Read(self.inputfiles[0],'read')
self.nlag = len(ncf.dimensions['nlag'])
self.nmembers = len(ncf.dimensions['nmembers'])
self.nparameters = len(ncf.dimensions['nparameters'])
......
......@@ -383,7 +383,7 @@ class StateVector(object):
#import da.tools.io as io
import numpy as np
f = io.CT_CDF(filename,'read')
f = io.CT_Read(filename,'read')
MeanState = f.GetVariable('statevectormean')
EnsembleMembers = f.GetVariable('statevectorensemble')
dummy = f.close()
......
......@@ -26,16 +26,12 @@ class CtDaSystem(DaSystem):
"""
Initialize the object
"""
#import da.tools.io4 as io
import pyhdf.SD as SD
import da.tools.io4 as io
mapfile = os.path.join(self['datadir'],self['regionsfile'])
#ncf = io.CT_CDF(mapfile,'read')
#self.regionmap = ncf.GetVariable('budget_region')
#dummy = ncf.close()
hdf = SD.SD(mapfile)
self.regionmap = hdf.select('budget_region').get()
dummy = hdf.end()
ncf = io.CT_Read(mapfile,'read')
self.regionmap = ncf.GetVariable('budget_region')
dummy = ncf.close()
def Validate(self):
......
......@@ -73,7 +73,7 @@ class CtObservations(Observation):
from string import strip, join
from numpy import array, logical_and
ncf = io.CT_CDF(self.ObsFilename,'read')
ncf = io.CT_Read(self.ObsFilename,'read')
idates = ncf.GetVariable('date_components')
dates = array([dtm.datetime(*d) for d in idates])
......@@ -123,7 +123,7 @@ class CtObservations(Observation):
msg = "...exiting" ; logging.error(msge)
raise IOError,msg
ncf = io.CT_CDF(filename,method='read')
ncf = io.CT_Read(filename,method='read')
ids = ncf.GetVariable('id')
simulated = ncf.GetVariable('flask')
dummy = ncf.close()
......
......@@ -65,7 +65,7 @@ class CtStateVector(StateVector):
The argument is thus referring to the lagged state vector as [1,2,3,4,5,..., nlag]
"""
import da.tools.io as io
import da.tools.io4 as io
try:
import matplotlib.pyplot as plt
except:
......@@ -90,8 +90,8 @@ class CtStateVector(StateVector):
msg = "Using covariance file: %s" % file ; logging.info(msg)
f_ocn = io.CT_CDF(file_ocn_cov,'read')
f_bio = io.CT_CDF(file_bio_cov,'read')
f_ocn = io.CT_Read(file_ocn_cov,'read')
f_bio = io.CT_Read(file_bio_cov,'read')
cov_ocn = f_ocn.GetVariable('CORMAT')
cov_bio = f_bio.GetVariable('qprior')
......
......@@ -26,8 +26,21 @@ historytext = 'created on '+dt.datetime.now().strftime('%B %d, %Y')+' by %s'%os.
std_savedict={'name':'unknown','values':[],'dims':(0,0,),'units':'','long_name':'','comment':''}
import netCDF4
def CT_Read(filename='',method=''):
""" read from an HDF or NetCDF file. Function choses itself which type is needed """
if 'hdf' in filename.split('.'):
return CT_HDF(filename,method)
elif 'nc' in filename.split('.'):
return CT_CDF(filename,method)
else:
msg = 'Could not determine whether input file was NetCDF or HDF trying both: ' ; logging.warning(msg)
try:
return CT_CDF(filename,method)
except:
return CT_HDF(filename,method)
import netCDF4
class CT_CDF(netCDF4.Dataset):
""" function opens a NetCDF file for writing of output"""
......@@ -334,6 +347,48 @@ class CT_CDF(netCDF4.Dataset):
var[:] = datadict['values']
import pyhdf.SD as hdf
class CT_HDF(hdf.SD):
""" function opens a HDF file for reading """
def __init__(self,filename, method='read'):
if method in ['write','create']:
raise ValueError, 'Method %s is not defined for a CarbonTracker HDF file object' % method
if method == 'read':
#print 'Reading from file'
try:
super(CT_HDF,self).__init__(filename)
except HDF4Error:
msg = 'Requested file not found for opening: %s'%filename ; logging.error(msg)
msg = "Exiting" ; logging.info(msg)
sys.exit(2)
def GetVariable(self,varname):
""" get variable from ncf file"""
return self.select(varname).get()
def GetAttribute(self,attname):
""" get attribute from ncf file"""
return getattr(self,attname)
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 close(self):
""" close file"""
return self.end()
def ConvertCharDims(var,datadict):
if not var.dtype == 'S1':
......@@ -351,9 +406,6 @@ def ConvertCharDims(var,datadict):
datadict['values'] = values
return datadict
def GetVariable(file,varname):
""" get variable from HDF file"""
......
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