Commit a85889e6 authored by Peters, Wouter's avatar Peters, Wouter
Browse files

added new routine to add posterior sample info

parent 40096eeb
......@@ -71,7 +71,7 @@ class ObsPackObservations(Observation):
infile = os.path.join(self.ObsPackDir,'summary','%s_dataset_summary.txt'%(self.ObsPackId,))
f = open(infile,'r')
lines = f.readlines()
dummy = f.close()
status = f.close()
ncfilelist = []
for line in lines:
......@@ -110,7 +110,7 @@ class ObsPackObservations(Observation):
flags = [s.tostring().lower() for s in flags]
flags = map(strip,flags)
flags = [int(f == '...') for f in flags]
dummy = ncf.close()
status = ncf.close()
for n in range(len(dates)):
......@@ -134,7 +134,7 @@ class ObsPackObservations(Observation):
ncf = io.CT_Read(filename,method='read')
ids = ncf.GetVariable('obs_num')
simulated = ncf.GetVariable('flask')
dummy = ncf.close()
status = ncf.close()
msg = "Successfully read data from model sample file (%s)"%filename ; logging.info(msg)
obs_ids = self.Data.getvalues('id')
......@@ -150,7 +150,7 @@ class ObsPackObservations(Observation):
index = obs_ids.index(id)
#print id,val,val.shape
dummy = self.Data[index].simulated = val*1e6 # to umol/mol
status = self.Data[index].simulated = val*1e6 # to umol/mol
else:
......@@ -192,7 +192,7 @@ class ObsPackObservations(Observation):
savedict['dims'] = dimid
savedict['values'] = data.tolist()
savedict['comment'] = "Unique index number within this dataset ranging from 0 to UNLIMITED."
dummy = f.AddData(savedict)
status = f.AddData(savedict)
data = [[d.year,d.month,d.day,d.hour,d.minute,d.second] for d in self.Data.getvalues('xdate') ]
......@@ -206,7 +206,7 @@ class ObsPackObservations(Observation):
savedict['_FillValue'] = -9
savedict['comment'] = "Calendar date components as integers. Times and dates are UTC."
savedict['order'] = "year, month, day, hour, minute, second"
dummy = f.AddData(savedict)
status = f.AddData(savedict)
data = self.Data.getvalues('lat')
......@@ -217,7 +217,7 @@ class ObsPackObservations(Observation):
savedict['values'] = data.tolist()
savedict['missing_value'] = -999.9
savedict['_FillValue'] = -999.9
dummy = f.AddData(savedict)
status = f.AddData(savedict)
data = self.Data.getvalues('lon')
......@@ -228,7 +228,7 @@ class ObsPackObservations(Observation):
savedict['values'] = data.tolist()
savedict['missing_value'] = -999.9
savedict['_FillValue'] = -999.9
dummy = f.AddData(savedict)
status = f.AddData(savedict)
data = self.Data.getvalues('height')
......@@ -239,7 +239,7 @@ class ObsPackObservations(Observation):
savedict['values'] = data.tolist()
savedict['missing_value'] = -999.9
savedict['_FillValue'] = -999.9
dummy = f.AddData(savedict)
status = f.AddData(savedict)
data = self.Data.getvalues('samplingstrategy')
......@@ -251,7 +251,7 @@ class ObsPackObservations(Observation):
savedict['values'] = data.tolist()
savedict['missing_value'] = -9
savedict['_FillValue'] = -9
dummy = f.AddData(savedict)
status = f.AddData(savedict)
data = self.Data.getvalues('evn')
......@@ -263,9 +263,9 @@ class ObsPackObservations(Observation):
savedict['values'] = data
savedict['missing_value'] = '-'
savedict['_FillValue'] = '-'
dummy = f.AddData(savedict)
status = f.AddData(savedict)
dummy = f.close()
status = f.close()
msg = "Successfully wrote data to obs file" ;logging.debug(msg)
msg = "Sample input file for obs operator now in place [%s]"%obsinputfile ; logging.info(msg)
......@@ -351,6 +351,117 @@ class ObsPackObservations(Observation):
self.SiteInfo = SiteInfo
def WriteObsToFile(self):
"""
Write selected information contained in the Observation object to a file.
"""
import shutil
import da.tools.io4 as io
from da.tools.general import ToDectime
outfile = os.path.join(self.DaCycle['dir.output'],'sampleinfo_%s.nc'% self.DaCycle['time.sample.stamp'])
f = io.CT_CDF(outfile,method='create')
msg = 'Creating new Sample output file for postprocessing (%s)' % outfile ; logging.debug(msg)
dimid = f.AddDim('obs',len(self.Data))
dim200char = f.AddDim('string_of200chars',200)
dim10char = f.AddDim('string_of10chars',10)
dimcalcomp = f.AddDim('calendar_components',6)
data = self.Data.getvalues('id')
savedict = io.std_savedict.copy()
savedict['name'] = "obs_num"
savedict['dtype'] = "int"
savedict['long_name'] = "Unique_Dataset_observation_index_number"
savedict['units'] = ""
savedict['dims'] = dimid
savedict['values'] = data.tolist()
savedict['comment'] = "Unique index number within this dataset ranging from 0 to UNLIMITED."
status = f.AddData(savedict)
data = [[d.year,d.month,d.day,d.hour,d.minute,d.second] for d in self.Data.getvalues('xdate') ]
savedict = io.std_savedict.copy()
savedict['dtype'] = "int"
savedict['name'] = "date_components"
savedict['units'] = "integer components of UTC date/time"
savedict['dims'] = dimid+dimcalcomp
savedict['values'] = data
savedict['missing_value'] = -9
savedict['_FillValue'] = -9
savedict['comment'] = "Calendar date components as integers. Times and dates are UTC."
savedict['order'] = "year, month, day, hour, minute, second"
status = f.AddData(savedict)
data = self.Data.getvalues('obs')
savedict = io.std_savedict.copy()
savedict['name'] = "observed"
savedict['long_name'] = "observedvalues"
savedict['units'] = "mol mol-1"
savedict['dims'] = dimid
savedict['values'] = data.tolist()
savedict['comment'] = 'Observations used in optimization'
dummy = f.AddData(savedict)
data = self.Data.getvalues('mdm')
savedict = io.std_savedict.copy()
savedict['name'] = "modeldatamismatch"
savedict['long_name'] = "modeldatamismatch"
savedict['units'] = "[mol mol-1]^2"
savedict['dims'] = dimid
savedict['values'] = data.tolist()
savedict['comment'] = 'Variance of mole fractions resulting from model-data mismatch'
dummy = f.AddData(savedict)
meandata = self.Data.getvalues('simulated')[:,0]
savedict = io.std_savedict.copy()
savedict['name'] = "modelsamplesmean_%s"%type
savedict['long_name'] = "modelsamplesforecastmean_%s" %type
savedict['units'] = "mol mol-1"
savedict['dims'] = dimid
savedict['values'] = meandata.tolist()
savedict['comment'] = '%s mean mixing ratios based on %s state vector'% (type,type,)
dummy = f.AddData(savedict)
devdata = self.Data.getvalues('simulated') - meandata
dimmembers = f.AddDim('members',devdata.shape[1])
savedict = io.std_savedict.copy()
savedict['name'] = "modelsamplesdeviations_%s"% type
savedict['long_name'] = "modelsamplesforecastdeviations_%s"%type
savedict['units'] = "mol mol-1"
savedict['dims'] = dimid+dimmembers
savedict['values'] = devdata.tolist()
savedict['comment'] = '%s mixing ratio deviations based on %s state vector'% (type,type,)
dummy = f.AddData(savedict)
data = self.Data.getvalues('fromfile')
savedict = io.std_savedict.copy()
savedict['name'] = "inputfilename"
savedict['long_name'] = "name of file where original obs data was taken from"
savedict['dtype'] = "char"
savedict['dims'] = dimid+dim200char
savedict['values'] = data
savedict['missing_value'] = '-'
savedict['_FillValue'] = '-'
dummy = f.AddData(savedict)
status = f.close()
msg = "Successfully wrote data to Sample output file (%s)"%outfile ;logging.debug(msg)
return outfile
################### End Class CtObservations ###################
......
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