Index: /trunk/tools/nc_var.py
===================================================================
--- /trunk/tools/nc_var.py	(revision 532)
+++ /trunk/tools/nc_var.py	(revision 533)
@@ -34,5 +34,6 @@
   'seasmean', 'sellonlatbox', 'sellonlatlevbox', 'selvar', 'setvar_asciivalues',     \
   'sorttimesmat', 'spacemean', 'statcompare_files', 'submns', 'subyrs', 'TimeInf',   \
-  'timemean', 'valmod', 'valmod_dim','varaddattrk', 'varaddattr', 'varaddref',       \
+  'TimeSplitmat', 'timemean', 'valmod', 'valmod_dim','varaddattrk', 'varaddattr',    \
+  'varaddref',                                                                       \
   'var_creation', 'varout', 'varoutold', 'varrmattr', 'varrm', 'vrattr',             \
   'WRF_d0Nref',                                                                      \
@@ -218,4 +219,6 @@
 elif oper == 'TimeInf':
     ncvar.TimeInf(opts.ncfile, opts.varname)
+elif oper == 'TimeSplitmat':
+    ncvar.TimeSplitmat(opts.values, opts.ncfile, opts.varname)
 elif oper == 'timemean':
     ncvar.timemean(opts.values, opts.ncfile, opts.varname)
Index: /trunk/tools/nc_var_tools.py
===================================================================
--- /trunk/tools/nc_var_tools.py	(revision 532)
+++ /trunk/tools/nc_var_tools.py	(revision 533)
@@ -10,4 +10,7 @@
 
 fillValue = 1.e20
+fillValueF = 1.e20
+fillValueC = '-'
+fillValueI = -99999
 
 def filexist(filen, emsg, fmsg):
@@ -15957,4 +15960,233 @@
     return
 
+def TimeSplitmat(values, ncfile, vn):
+    """ Function to transform a file with CFtimes to a matrix [Nyear,Nmonth,Nday,Nhour,Nminute,Nsecond] 
+      values= [tdim]:[tvdim]:[seltimes]
+      tdim= name of the dimension time
+      tvdim= name of the variable with the time values
+      seltimes= ',' list of which times ('y', 'm', 'd', 'H','M', 'S') one wants to split the values
+        desired final order
+      ncfile= netcdf file to use
+      vn= name of the variable, 'all' for all variables
+    """
+    import datetime as dt
+    fname = 'TimeSplitmat'
+
+    ofile = 'TimeSplitmat.nc'
+
+    if values == 'h':
+        print fname + '_____________________________________________________________'
+        print TimeSplitmat.__doc__
+        quit()
+
+    arguments = '[tdim]:[tvdim]:[seltimes]'
+    check_arguments(fname, len(values.split(':')), arguments, ':',                   \
+      len(arguments.split(',')))
+
+    tdim = values.split(':')[0]
+    tvdim = values.split(':')[1]
+    seltimes = values.split(':')[2]
+
+    of = NetCDFFile(ncfile, 'r')
+
+    stimes = seltimes.split(',')
+    if len(stimes) != 2:
+        print errormsg
+        print fname + ': number of splitting times',len(stimes),'not ready!!'
+        quit(-1)
+
+    if vn == 'all':
+        vns = of.variables.keys()
+    else:
+        vns = [vn]
+
+    otime = of.variables[tvdim]
+    dt = otime.shape[0]
+    if not searchInlist(otime.ncattrs(), 'units'):
+        print errormsg
+        print '  ' + fname + ": varible time '" + dimtv + "' has not units!!"
+        quit(-1)
+    tunits = otime.getncattr('units')
+
+    mattimes = CFtimes_datetime(of,tvdim)
+
+    Nyears = len(np.unique(mattimes[:,0]))
+    Nmonths = len(np.unique(mattimes[:,1]))
+    Ndays = len(np.unique(mattimes[:,2]))
+    Nhours = len(np.unique(mattimes[:,3]))
+    Nminutes = len(np.unique(mattimes[:,4]))
+    Nseconds = len(np.unique(mattimes[:,5]))
+    
+    newdimns = []
+    newdimvs = []
+    firstmat = -1
+    secondmat = -1
+    for st in stimes:
+        if st == 'y': 
+            newdimns.append('years')
+            newdimvs.append(Nyears)
+            if firstmat == -1: 
+                firstmat = 0
+            else:
+                secondmat = 0
+        elif st == 'm': 
+            newdimns.append('months')
+            newdimvs.append(Nmonths)
+            if firstmat == -1: 
+                firstmat = 1
+            else:
+                secondmat = 1
+        elif st == 'd': 
+            newdimns.append('days')
+            newdimvs.append(Ndays)
+            if firstmat == -1: 
+                firstmat = 2
+            else:
+                secondmat = 2
+        elif st == 'H': 
+            newdimns.append('hours')
+            newdimvs.append(Nhours)
+            if firstmat == -1: 
+                firstmat = 3
+            else:
+                secondmat = 3
+        elif st == 'M': 
+            newdimns.append('minutes')
+            newdimvs.append(Nminutes)
+            if firstmat == -1: 
+                firstmat = 4
+            else:
+                secondmat = 4
+        elif st == 'S': 
+            newdimns.append('seconds')
+            newdimvs.append(Nseconds)
+            if firstmat == -1: 
+                firstmat = 5
+            else:
+                secondmat = 5
+        else: 
+            print errormsg
+            print '  ' + fname + ": selected time '" + st + "' not ready!!"
+            quit(-1)
+
+    print '  ' + fname + ':initial selection of:',newdimvs
+    print '  ' + newdimns[0],':',np.unique(mattimes[:,firstmat])
+    print '  ' + newdimns[1],':',np.unique(mattimes[:,secondmat])
+
+# We can have still multiple values (for given days, multiple months...). Assuming 
+#   30days months
+    newdimvs[1] = dt/newdimvs[0]
+    print '    re-arranged to:',newdimvs
+    newtime = np.zeros(tuple(newdimvs), dtype=np.float)
+
+# Openning new file
+    onewfile = NetCDFFile(ofile, 'w')
+
+# Dimensions
+    newdim = onewfile.createDimension(newdimns[0], newdimvs[0])
+    newdim = onewfile.createDimension(newdimns[1], newdimvs[1])
+    newdim = onewfile.createDimension('time', None)
+
+    splitS = seltimes.replace(',','')
+
+    for Vn in vns:
+        print Vn + '...'
+        oVn = of.variables[Vn]
+
+# Removing time dimension from the variable dimensions
+        Vndims = oVn.dimensions
+        if searchInlist(Vndims, tdim) and Vn != tvdim:
+            varNOtdimns = []
+            varNOtdimvs = []
+            for idn in Vndims:
+                if idn != tdim:
+                    varNOtdimns.append(idn)
+                    varNOtdimvs.append(len(of.dimensions[idn]))
+                    if not searchInlist(onewfile.dimensions.keys(), idn):
+                        newdim = onewfile.createDimension(idn,                       \
+                          len(of.dimensions[idn]))
+
+            newvarv = np.ones(tuple(newdimvs+varNOtdimvs), dtype=np.float)*fillValueF
+
+            d1 = 0
+            d2 = -1
+
+            d1val = mattimes[0,firstmat]
+            d2val = mattimes[0,secondmat]
+
+            secondvals = []
+
+            for ddt in range(dt):
+                if mattimes[ddt,firstmat] > d1val: 
+                    d1 = d1 + 1
+                    d1val = mattimes[ddt,firstmat]
+                    d2 = 0
+                else:
+                    d2 = d2 + 1
+                    if d1 == 0: secondvals.append(mattimes[ddt,secondmat])
+                
+                slicevar = []
+                slicenewvar = []
+
+                slicenewvar.append(d1)
+                slicenewvar.append(d2)
+                for idn in Vndims:
+                    if idn != tdim:
+                        slicevar.append(slice(0,len(of.dimensions[idn])))
+                        slicenewvar.append(slice(0,len(of.dimensions[idn])))
+                    else:
+                        slicevar.append(ddt)
+
+#                print ddt,d1,d2,mattimes[ddt,:],'var:',slicevar,'new:',slicenewvar
+
+                newvarv[tuple(slicenewvar)] = oVn[tuple(slicevar)]
+                newtime[d1,d2] = otime[ddt]
+
+            newvar = onewfile.createVariable(Vn + '_' + splitS, 'f',                 \
+              tuple(newdimns + varNOtdimns), fill_value = fillValueF)
+            newattr = newvar.setncattr('desc',  Vn + 'splitted by:' + newdimns[0] +  \
+              ' & ' + newdimns[1])
+            newvar[:] = newvarv
+            onewfile.sync()
+    else:
+        newvar = oVn
+
+# Variable time
+    newvar = onewfile.createVariable('time', 'f8', tuple(newdimns))
+    newvar[:] = newtime
+    basicvardef(newvar, 'time', 'time', tunits)
+
+# Variables selection
+    idn=0
+    for newd in newdimns:
+        print newd, onewfile.dimensions.keys()
+        newvar = onewfile.createVariable(newd, 'f', (newd))
+        if idn == 0:
+            newvar[:] = np.unique(mattimes[:,firstmat])
+        else:
+            newvar[:] = secondvals
+
+        set_attribute(newvar, 'desc', 'Selected ' + newdimns[idn])
+        idn = idn + 1
+
+# Global attributes
+    onewfile.setncattr('script',  fname)
+    onewfile.setncattr('version',  '1.0')
+    onewfile.setncattr('author',  'L. Fita')
+    onewfile.setncattr('institution',  'Laboratoire de Meteorology Dynamique')
+    onewfile.setncattr('university',  'Pierre et Marie Curie')
+    onewfile.setncattr('country',  'France')
+    onewfile.setncattr('description',  'transform a file with CFtimes to a matrix ' +\
+      '[Nyear,Nmonth,Nday,Nhour,Nminute,Nsecond]')
+
+    onewfile.sync()
+    onewfile.close()
+    print fname + ": Successful writting of file '" + ofile + "' !!"
+
+    return
+
+#fileobj = NetCDFFile('/home/lluis/PY/ERAI_pl199501_130-133.nc', 'r')
+#TimeSplitmat(fileobj, 'time', 'time', 'all', 'd,H')
+
 #quit()
 
Index: /trunk/tools/variables_values.dat
===================================================================
--- /trunk/tools/variables_values.dat	(revision 532)
+++ /trunk/tools/variables_values.dat	(revision 533)
@@ -165,4 +165,6 @@
 LRH2M, huss, specific_humidity, 0., 1., specific|humidty|at|2m, 1, BuPu
 TSrhs, huss, specific_humidity, 0., 1., specific|humidty|at|2m, 1, BuPu
+rh, huss, specific_humidity, 0., 1., specific|humidty|at|2m, 1, BuPu  (from TS files to netcdf)
+rhs, huss, specific_humidity, 0., 1., specific|humidty|at|2m, 1, BuPu  (from TS files to netcdf)
 i, i, iced_water_mixing_ratio, 0., 0.0003, iced|water|mixing|ratio, kgkg-1, Purples
 QICE, i, iced_water_mixing_ratio, 0., 0.0003, iced|water|mixing|ratio, kgkg-1, Purples
