Commit 8ccc8c43 authored by Peters, Wouter's avatar Peters, Wouter
Browse files

added custom writing of ensemble data

parent cddd828c
......@@ -35,11 +35,12 @@ class EnsembleMember(object):
def __str__(self):
return "%03d"%self.membernumber
def WriteToFile(self, outdir):
def WriteToFile(self,DaCycle):
""" Write the information needed by an external model to a netcdf file for future use """
from da.tools.io import CT_CDF, std_savedict
import numpy as np
outdir = DaCycle['dir.input']
filename = os.path.join(outdir,'parameters.%03d.nc'% self.membernumber)
f = CT_CDF(filename,'create')
dimparams = f.AddParamsDim(len(self.ParameterValues))
......@@ -55,11 +56,16 @@ class EnsembleMember(object):
savedict['comment'] = 'These are parameter values to use for member %d'%self.membernumber
dummy = f.AddData(savedict)
dummy = self.AddCustomFields(f,DaCycle)
dummy = f.close()
msg = 'Successfully wrote data from ensemble member %d to file (%s) ' % (self.membernumber,filename,) ; logging.info(msg)
def AddCustomFields(self,filehandle,DaCycle):
""" Placeholder to add more custom fields to the output of the ensemble member"""
return None
################### End Class EnsembleMember ###################
......@@ -132,7 +138,7 @@ class StateVector(object):
# Create the first ensemble member with a deviation of 0.0 and add to list
NewMember = EnsembleMember(0)
NewMember = self.GetNewMember(0)
NewMember.ParameterValues = np.zeros((self.nparams),float) + NewMean
dummy = self.EnsembleMembers[lag-1].append(NewMember)
......@@ -142,12 +148,18 @@ class StateVector(object):
randstate = np.random.get_state()
rands = np.random.randn(self.nparams)
NewMember = EnsembleMember(member)
NewMember = self.GetNewMember(member)
NewMember.ParameterValues = np.dot(C,rands) + NewMean
dummy = self.EnsembleMembers[lag-1].append(NewMember)
msg = '%d new ensemble members were added to the state vector # %d'%(self.nmembers,lag) ; logging.debug(msg)
def GetNewMember(self,memberno):
""" Return an ensemblemember object """
return EnsembleMember(memberno)
def Propagate(self):
"""
......@@ -220,7 +232,7 @@ class StateVector(object):
for n in range(self.nlag):
for m in range(self.nmembers):
NewMember = EnsembleMember(m)
NewMember = self.GetNewMember(m)
NewMember.ParameterValues = EnsembleMembers[n,m,:] + MeanState[n] # add the mean to the deviations to hold the full parameter values
dummy = self.EnsembleMembers[n].append(NewMember)
......@@ -251,9 +263,9 @@ if __name__ == "__main__":
DaCycle.Initialize()
print DaCycle
dims = ( int(DaCycle.da_settings['time.nlag']),
int(DaCycle.da_settings['forecast.nmembers']),
int(DaCycle.DaSystem.da_settings['nparameters']),
dims = ( int(DaCycle['time.nlag']),
int(DaCycle['forecast.nmembers']),
int(DaCycle.DaSystem['nparameters']),
)
StateVector = StateVector(dims)
......@@ -263,17 +275,17 @@ if __name__ == "__main__":
members = StateVector.EnsembleMembers[1]
members[0].WriteToFile(DaCycle.da_settings['dir.input'])
members[0].WriteToFile(DaCycle['dir.input'])
StateVector.Propagate()
savedir = DaCycle.da_settings['dir.output']
savedir = DaCycle['dir.output']
filename = os.path.join(savedir,'savestate.nc')
StateVector.WriteToFile(filename)
savedir = DaCycle.da_settings['dir.output']
savedir = DaCycle['dir.output']
filename = os.path.join(savedir,'savestate.nc')
StateVector.ReadFromFile(filename)
......
......@@ -21,6 +21,32 @@ import numpy as np
identifier = 'CarbonTracker Statevector '
version = '0.0'
################### Begin Class CtEnsembleMember ###################
class CTEnsembleMember(EnsembleMember):
def AddCustomFields(self,filehandle,DaCycle):
""" Placeholder to add more custom fields to the output of the ensemble member"""
from da.tools.io import CT_CDF, std_savedict
from da.ct.tools import StateToGrid
import numpy as np
data = StateToGrid(self.ParameterValues,DaCycle.DaSystem.regionmap)
dimgrid = filehandle.AddLatLonDim()
savedict = std_savedict.copy()
savedict['name'] = "parametermap"
savedict['long_name'] = "parametermap_for_member_%d"%self.membernumber
savedict['units'] = "unitless"
savedict['dims'] = dimgrid
savedict['values'] = data.tolist()
savedict['comment'] = 'These are gridded parameter values to use for member %d'%self.membernumber
dummy = filehandle.AddData(savedict)
return None
################### Begin Class CtStateVector ###################
class CtStateVector(StateVector):
......@@ -42,8 +68,8 @@ class CtStateVector(StateVector):
# Get the needed matrices from the specified covariance files
file_ocn_cov = DaSystem.da_settings['ocn.covariance']
file_bio_cov = DaSystem.da_settings['bio.covariance']
file_ocn_cov = DaSystem['ocn.covariance']
file_bio_cov = DaSystem['bio.covariance']
for file in [file_ocn_cov,file_bio_cov]:
......@@ -89,6 +115,12 @@ class CtStateVector(StateVector):
return fullcov
def GetNewMember(self,memberno):
""" Return an CTensemblemember object """
return CTEnsembleMember(memberno)
################### End Class CtStateVector ###################
......@@ -113,9 +145,9 @@ if __name__ == "__main__":
StateVector = CtStateVector()
dims = ( int(DaCycle.da_settings['time.nlag']),
int(DaCycle.da_settings['forecast.nmembers']),
int(DaCycle.DaSystem.da_settings['nparameters']),
dims = ( int(DaCycle['time.nlag']),
int(DaCycle['forecast.nmembers']),
int(DaCycle.DaSystem['nparameters']),
)
StateVector.Initialize(dims)
......@@ -126,10 +158,10 @@ if __name__ == "__main__":
StateVector.Propagate()
savedir = DaCycle.da_settings['dir.output']
savedir = DaCycle['dir.output']
filename = os.path.join(savedir,'savestate.nc')
dummy = StateVector.WriteToFile(filename)
dummy = StateVector.WriteToFile(DaCycle)
StateVector.ReadFromFile(filename)
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