From cfdd2f07f1e00fb8ffc941fcf254539ab8c69012 Mon Sep 17 00:00:00 2001
From: ivar <amvdw95@gmail.com>
Date: Tue, 10 Jul 2012 08:57:11 +0000
Subject: [PATCH]

---
 da/tools/MixingratioToPermil.py | 12 ++++-
 da/tools/general.py             | 79 +++++++++++++++++++++++++++++++++
 da/tools/io4.py                 | 37 ++++++++++++++-
 da/tools/pipeline.py            | 10 +++--
 da/tools/standardvariables.py   | 36 +++++++++++++++
 5 files changed, 166 insertions(+), 8 deletions(-)

diff --git a/da/tools/MixingratioToPermil.py b/da/tools/MixingratioToPermil.py
index 8aaa6ccc..762f5e87 100755
--- a/da/tools/MixingratioToPermil.py
+++ b/da/tools/MixingratioToPermil.py
@@ -13,17 +13,25 @@ File created on 11 May 2012.
 def MixingratioToPermil(filename,simulated):
     """ Converts 13C mixing ratios to permil values"""
     import da.tools.io4 as io
-    from numpy import array
+    import logging
+    import numpy as np
 
     pdb         = 0.011112
 
     trlength=len(simulated[0,:])
     memlength=trlength/2
     simulated=simulated*1.e6 #convert to ppm
+ #   np.set_printoptions(threshold=np.nan)
+ #   msg='simulated shape',simulated.shape;logging.info(msg)
+ #   msg='simulated',simulated[0,0],simulated[0,40];logging.info(msg)
+ #   msg='simulated',simulated ;logging.info(msg)
+    simulated=np.float64(simulated)
     simulated[:,memlength:trlength]=1000.*((simulated[:,memlength:trlength]/simulated[:,0:memlength]/pdb)-1.)
+ #   msg='simulated',simulated[0,0],simulated[0,40],memlength,trlength,pdb;logging.info(msg)
+ #   msg='simulated',simulated ;logging.info(msg)
+
 
 
-    print simulated
 
     return simulated
 
diff --git a/da/tools/general.py b/da/tools/general.py
index e519db35..a376bb91 100755
--- a/da/tools/general.py
+++ b/da/tools/general.py
@@ -150,5 +150,84 @@ def RestoreData(filename):
 
     return pickleobject
 
+    
+def NameConvert( name=None, to=None ):
+   """ Convert between old GLOBALVIEW-style and new ObsPack-style
+
+      print NameConvert(name="lef_surface-pfp_1", to='GV' )
+            lef_01P0
+
+      print NameConvert(name="hun_35C3", to='OP' )
+            hun_tower-insitu_35
+   """
+
+   import re
+
+   identifier = 'NameConvert'
+
+   if name == None or to == None: return ""
+
+   platform_dict = { 'surface':0, 'shipboard':1, 'aircraft':2, 'tower':3 }
+   strategy_dict = { 'flask':'D', 'pfp':'P', 'insitu':'C' }
+
+   #----------------------------------->>>
+   if to.upper() == "GV":
+
+      # alt_surface-flask_1 -> alt_01D0
+
+      try:
+         fields = name.split( '_' )
+         code = fields[0]
+         lab_num = int( fields[2] )
+      except:
+         return ""
+
+      try:
+         fields = fields[1].split( '-' )
+         platform = fields[0]
+         strategy = fields[1]
+      except:
+         return ""
+
+      platform_num = [ v for k,v in platform_dict.iteritems() if platform.lower() == k ]
+      if len( platform_num ) == 0:
+         print '%s: Platform %s not found in platform dict.' % ( identifier, platform )
+         return ""
+
+      strategy_char = [ v for k,v in strategy_dict.iteritems() if strategy.lower() == k ]
+      if len( strategy_char ) == 0:
+         print '%s: Strategy %s not found in strategy dict.' % ( identifier, strategy )
+         return "" 
+
+      return "%s_%2.2d%1s%1d" % ( code, lab_num, strategy_char[0].upper(), int( platform_num[0] ) )
+
+   #----------------------------------->>>
+   if to.upper() == "OP":
+
+      # hun_35C3 -> hun_tower-insitu_35
+
+      try:
+         fields = name.split( '_' )
+
+         code = fields[0]
+         lab_num = int( fields[1][:2] )
+         strategy_char = fields[1][2]
+         platform_num = int( fields[1][3] )
+      except:
+         return ""
+
+      platform = [ k for k,v in platform_dict.iteritems() if v == platform_num ]
+      if len( platform ) == 0:
+         print '%s: Platform number %s not found in platform dict.' % ( identifier, platform_num )
+         return ""
+
+      pattern = re.compile( strategy_char, re.IGNORECASE )
+      strategy = [ k for k,v in strategy_dict.iteritems() if pattern.search(v) ]
+      if len( strategy ) == 0:
+         print '%s: Strategy character %s not found in strategy list.' % ( identifier, strategy_char )
+         return ""
+
+      return "%s_%s-%s_%d" % ( code, platform[0], strategy[0], lab_num )
+
 if __name__ == "__main__":
     pass
diff --git a/da/tools/io4.py b/da/tools/io4.py
index 3d16a79e..5fdf0aa8 100755
--- a/da/tools/io4.py
+++ b/da/tools/io4.py
@@ -269,6 +269,37 @@ class CT_CDF(netCDF4.Dataset):
 
         return False
 
+    def AddVariable(self,datadict,silent=True):
+        """ add variables to file, but no data"""
+        import numpy as np
+
+        existing_vars=self.variables
+
+        if existing_vars.has_key(datadict['name']):
+            return
+        else:
+            if not silent: print 'Creating new dataset: '+datadict['name']
+
+            if datadict.has_key('dtype'):
+                if datadict['dtype'] == 'int':
+                    var = self.createVariable(datadict['name'],'i4',datadict['dims'])
+                elif datadict['dtype'] == 'int64':
+                    var = self.createVariable(datadict['name'],'i8',datadict['dims'])
+                elif datadict['dtype'] == 'char':
+                    var = self.createVariable(datadict['name'],'S1',datadict['dims'],fill_value='!')
+                elif datadict['dtype'] == 'float':
+                    var = self.createVariable(datadict['name'],'f4',datadict['dims'])
+                elif datadict['dtype'] == 'double':
+                    var = self.createVariable(datadict['name'],'f8',datadict['dims'])
+                else:
+                    var = self.createVariable(datadict['name'],'f8',datadict['dims'])
+            else:
+                var = self.createVariable(datadict['name'],'f4',datadict['dims'])
+
+            for k,v in datadict.iteritems(): 
+                if k not in ['name','dims','values','_FillValue','count']: 
+                    var.setncattr(k,v)
+
     def AddData(self,datadict,nsets=1,silent=True):
         """ add fields to file, at end of unlimited dimension"""
         import numpy as np
@@ -284,7 +315,7 @@ class CT_CDF(netCDF4.Dataset):
         if existing_vars.has_key(datadict['name']):
             var = self.variables[datadict['name']] 
             ndims = var.ndim
-            print var,datadict['name'],ConvertCharDims(var,datadict),datadict['values']
+
             datadict = ConvertCharDims(var,datadict)
 
             if ndims == 1:
@@ -307,8 +338,10 @@ class CT_CDF(netCDF4.Dataset):
             if datadict.has_key('dtype'):
                 if datadict['dtype'] == 'int':
                     var = self.createVariable(datadict['name'],'i4',datadict['dims'])#,fill_value=datadict['_FillValue'])
+                elif datadict['dtype'] == 'int64':
+                    var = self.createVariable(datadict['name'],'i8',datadict['dims'])#,fill_value=datadict['_FillValue'])
                 elif datadict['dtype'] == 'char':
-                    var = self.createVariable(datadict['name'],'S1',datadict['dims'],fill_value='x')
+                    var = self.createVariable(datadict['name'],'S1',datadict['dims'],fill_value='!')
                 elif datadict['dtype'] == 'float':
                     var = self.createVariable(datadict['name'],'f4',datadict['dims'])#,fill_value=datadict['_FillValue'])
                 elif datadict['dtype'] == 'double':
diff --git a/da/tools/pipeline.py b/da/tools/pipeline.py
index fb241e70..d116d147 100755
--- a/da/tools/pipeline.py
+++ b/da/tools/pipeline.py
@@ -177,7 +177,7 @@ def SampleState(DaCycle,Samples,StateVector, ObservationOperator):
 def SampleOneCycle(DaCycle,Samples,StateVector, ObservationOperator,lag):
     """ Perform all actions needed to sample one cycle """
     import copy
-
+    import numpy as np
     # First set up the information for time start and time end of this sample
 
 
@@ -241,15 +241,17 @@ def SampleOneCycle(DaCycle,Samples,StateVector, ObservationOperator,lag):
     # steps only optimize against the data at the front (lag==nlag) of the filter. This way, each observation is used only 
     # (and at least) once # in the assimilation
 
-    if lag == int(DaCycle['time.nlag'])-1 or DaCycle['time.restart']==False :
-
+    if lag == int(DaCycle['time.nlag'])-1 or DaCycle['time.restart']==False :      
         StateVector.ObsToAssimmilate += (copy.deepcopy(Samples),)
-
+        
         StateVector.nobs += Samples.getlength()
 
         msg = "Added samples from the observation operator to the assimilted obs list in the StateVector" ; logging.debug(msg)
 
+    else:
 
+        StateVector.ObsToAssimmilate += (None,)
+        
     return None
 
 def Invert(DaCycle, StateVector, Optimizer ):
diff --git a/da/tools/standardvariables.py b/da/tools/standardvariables.py
index ec56eb19..b491acf8 100755
--- a/da/tools/standardvariables.py
+++ b/da/tools/standardvariables.py
@@ -196,6 +196,42 @@ standard_variables = { 'bio_flux_prior' : {'name'        : 'bio_flux_prior',\
                                          'values'        : [], \
                                          'count'         : 0 \
                                         } , \
+                       'ocn_flux_prior_ensemble' : {'name'    : 'ocn_flux_prior_ensemble',\
+                                         'units'         : '[mol region-1 s-1]' ,\
+                                         'long_name'     : 'Ensemble of surface flux of carbon dioxide, open ocean , not optimized ', \
+                                         'comment'       : "This is the matrix square root, use (M x M^T)/(nmembers-1) to make covariance", \
+                                         'standard_name' : '', \
+                                         'dims'          : (), \
+                                         'values'        : [], \
+                                         'count'         : 0 \
+                                        } , \
+                       'ocn_flux_opt_ensemble' : {'name'    : 'ocn_flux_opt_ensemble',\
+                                         'units'         : '[mol region-1 s-1]' ,\
+                                         'long_name'     : 'Ensemble of surface flux of carbon dioxide, open ocean , optimized ', \
+                                         'comment'       : "This is the matrix square root, use (M x M^T)/(nmembers-1) to make covariance", \
+                                         'standard_name' : '', \
+                                         'dims'          : (), \
+                                         'values'        : [], \
+                                         'count'         : 0 \
+                                        } , \
+                       'bio_flux_prior_ensemble' : {'name'    : 'bio_flux_prior_ensemble',\
+                                         'units'         : '[mol region-1 s-1]' ,\
+                                         'long_name'     : 'Ensemble of surface flux of carbon dioxide, terrestrial vegetation , not optimized ', \
+                                         'comment'       : "This is the matrix square root, use (M x M^T)/(nmembers-1) to make covariance", \
+                                         'standard_name' : '', \
+                                         'dims'          : (), \
+                                         'values'        : [], \
+                                         'count'         : 0 \
+                                        } , \
+                       'bio_flux_opt_ensemble' : {'name'    : 'bio_flux_opt_ensemble',\
+                                         'units'         : '[mol region-1 s-1]' ,\
+                                         'long_name'     : 'Ensemble of surface flux of carbon dioxide, terrestrial vegetation , optimized ', \
+                                         'comment'       : "This is the matrix square root, use (M x M^T)/(nmembers-1) to make covariance", \
+                                         'standard_name' : '', \
+                                         'dims'          : (), \
+                                         'values'        : [], \
+                                         'count'         : 0 \
+                                        } , \
                        'decimal_date' :  {'name'         : 'decimal_date',\
                                          'units'         : 'years' ,\
                                          'long_name'     : 'dates and times', \
-- 
GitLab