Index: trunk/tools/nc_var_tools.py
===================================================================
--- trunk/tools/nc_var_tools.py	(revision 2769)
+++ trunk/tools/nc_var_tools.py	(revision 2770)
@@ -141,4 +141,6 @@
 # netcdf_concatenation: Function to concatenate netCDF files for a given set of variable
 # netcdf_fold_concatenation: Function to concatenate netCDF files in a given folder for a given set of variables
+# netcdf_fold_slice_concatenation_HMT: Function to concatenate netCDF files in a given folder for a given slice and 
+#   for a given set of variables giving Header, Middle, Tail for the name files
 # netcdf_fold_concatenation_HMT: Function to concatenate netCDF files in a given folder for a given set of variables giving Header, Middle, Tail for the name files
 # operation_alongdims: Function to operate along different dimensions of a variable
@@ -32025,4 +32027,357 @@
 #CFfile_fixTime(values, 'CFtest.nc', 'time')
 
+def netcdf_fold_slice_concatenation_HMT(values, ncfile, varn):
+    """ Function to concatenate netCDF files in a given folder for a given slice and 
+      for a given set of variables giving Header, Middle, Tail for the name files
+    netcdf_fold_concateation(values, ncfile, varn)
+      [values]= [slice],[fold],[dimname],[vardimname]
+        [slice]= ':' list of [dimn]|[sec] to use for slicing [varn]
+          [sec]: section
+            * [integer]: which value of the dimension
+            * -1: all along the dimension
+            * -9: last value of the dimension
+            * [beg]@[end]@[freq] slice from [beg] to [end] every [freq]
+          * NOTE: no value provided is for all dimension range
+        [fold]: folder with the location of the netCDF files
+        [dimname]: dimension along which files should be concatenated
+        [vardimname]: name of the dimension variable with the values ('WRFtime' for 
+          CF time conversion from WRF 'Times')
+      [ncfile]= [header],[middle],[tail]
+        [header] = header of the name of the files to concatenate [ncfile]*[middle]*[tail]
+        [middle] = middle of the name of the files to concatenate [ncfile]*[middle]*[tail]
+        [tail] = tail of the name of the files to concatenate [ncfile]*[middle]*[tail]
+      [varn]= ',' separated list of variables to concatenate 
+          [var1],[var2],[...[varN]] or 'all' for all variables
+    """
+    import subprocess as sub
+    fname='netcdf_fold_slice_concatenation_HMT'
+
+    ofile = 'netcdf_fold_slice_concatenated_HMT.nc'
+
+    if values == 'h':
+        print fname + '_____________________________________________________________'
+        print netcdf_fold_slice_concatenation_HMT.__doc__
+        quit()
+
+    expectargs = '[slice],[fold],[dimname],[vardimname]'
+    gen.check_arguments(fname,values,expectargs,':')
+
+    slicevS = values.split(',')[0]
+    fold = values.split(',')[1]
+    condim = values.split(',')[2]
+    vardimname = values.split(',')[3]
+
+    slicedicv = {}
+    for slcv in slicevS.split(':'):
+        dimn = slcv.split('|')[0]
+        slccvv = slcv.split('|')[1]
+            * [integer]: which value of the dimension
+            * -1: all along the dimension
+            * -9: last value of the dimension
+            * [beg]@[end] slice from [beg] to [end]
+        if slccvv.count('@') != 0:
+            slicedicv[dimn] = gen.str_list_k(slccvv,'@','I')
+        else:
+            slicedicv[dimn] = int(slccvv)
+
+    HMT = ncfile.split(',')
+    confiles = gen.files_folder_HMT(fold, HMT[0], HMT[1], HMT[2])
+    Nfiles = len(confiles)
+
+    print '  ' + fname +': concatenating by slice:', slicedicv, ':', Nfiles, 'files'
+    if Nfiles == 0:
+        print errormsg
+        print '  ' +fname+ ": there are no files as '" + fold + '/' + '*'.join(HMT) +\
+          "*' !!"
+        quit(-1)
+    print confiles
+
+# Opening all files
+    ncobjs = []
+    for filen in confiles:
+        print 'charging: ',filen
+        if filen.find(fold) != -1:
+            ncobjs.append(NetCDFFile(filen, 'r'))
+        else:
+            ncobjs.append(NetCDFFile(fold + '/' + filen, 'r'))
+
+# Looking for the new length of the concatenation dimension
+    if not ncobjs[0].dimensions.has_key(condim):
+        print errormsg
+        print '  ' + fname + ": files do not have dimensions '" + condim + "' !!!"
+        quit(-1)
+    if vardimname != 'WRFtime' and not ncobjs[0].variables.has_key(vardimname):
+        print errormsg
+        print '  ' + fname + ": files do not have variable '" + vardimname + "' !!!"
+        quit(-1)
+
+    totcondim = 0
+    for ifile in range(Nfiles):
+        ncobj = ncobjs[ifile]
+        totcondim = totcondim + len(ncobjs[ifile].dimensions[condim])
+
+    print "total concatenated dimension '" + condim + "': ", totcondim
+
+# concatenated file creation
+##
+    newnc = NetCDFFile(ofile, 'w')
+
+# dimension creation 
+##
+    if ncobjs[0].dimensions[condim].isunlimited():
+        newnc.createDimension(condim, None)
+    else:
+        newnc.createDimension(condim, totcondim)
+    newnc.createDimension('file', Nfiles)
+    newnc.createDimension('Lstring', 256)
+
+    # dim-variables
+    newvar = newnc.createVariable('file','c',('file','Lstring'))
+    newvals = writing_str_nc(newvar, confiles, 256)
+    if confiles[0].find(fold) != -1:
+        str="concatenated files following dimension '" + condim + "'"
+    else:
+        str="concatenated files from folder '" + fold + "' following dimension '" +  \
+          condim + "'"
+    vardef = basicvardef(newvar, 'files', str, '-')
+
+# Fake variable for the dimension
+    print 'condim:',condim
+    dims = []
+    dims.append(condim)
+    newvar = newnc.createVariable('int' + condim, 'i4', tuple(dims))
+    newvar[:] = range(totcondim)
+
+# Dimension variable for the concatenation
+    
+    urefvals = '-'
+    if vardimname != 'WRFtime':
+        odimvar = ncobjs[0].variables[vardimname]
+        odimvardims = odimvar.dimensions
+        odimattrs = odimvar.ncattrs()
+        for dimn in odimvardims:
+            if not gen.searchInlist(newnc.dimensions,dimn):
+                idim = gen.index_vec(odimvar.dimensions, dimn)
+                if ncobjs[0].dimensions[dimn].isunlimited():
+                    newdim = newnc.createDimension(dimn,None)
+                else:
+                    Ldim = len(ncobjs[0].dimensions[dimn])
+                    newdim = newnc.createDimension(dimn,Ldim)
+        newvar = newnc.createVariable(vardimname, odimvar.dtype, odimvardims)
+        for attrn in odimattrs:
+            attrv = odimvar.getncattr(attrn)
+            newattr = set_attribute(newvar, attrn, attrv)
+            if attrn == 'units': urefvals = attrv
+    else:
+        print infmsg
+        print '    ' + fname + ": creation of variable 'time' from WRF 'Times' !!"
+        tvals = []
+        for ifile in range(Nfiles):
+            odimvar = ncobjs[ifile].variables['Times']
+            timewrfv = odimvar[:]
+            tvals0, urefvals = compute_WRFtime(timewrfv, refdate='19491201000000',   \
+              tunitsval='minutes')
+            tvals = tvals + list(tvals0)
+        tvals = np.array(tvals)
+        dimtwrf = len(tvals)
+        if not gen.searchInlist(newnc.dimensions,'time'):
+            newnc.createDimension('time',None)
+        newvar = newnc.createVariable('time', 'f8', 'time')
+        newvar[:] = np.zeros((totcondim), dtype=np.float64)
+        basicvardef(newvar, 'time', 'Time', urefvals)
+        newvar.setncattr('axis', 'T')
+        newvar.setncattr('_CoordinateAxisType', 'Time')
+        set_attribute(newvar, 'calendar', 'standard')
+    if urefvals == '-': urefvals = '- -'
+
+    # Checking units of condimvar
+    uvalsecs = urefvals.split(' ')
+    if uvalsecs[1] == 'since': 
+        tunits = True
+        CFtimeu = gen.get_right_CFtimeunits(urefvals)
+        uvalsecs = CFtimeu.split(' ')
+        date=uvalsecs[2]
+        time=uvalsecs[3]
+        refdate = gen.datetimeStr_datetime(date+'_'+time)
+        print warnmsg
+        print '  ' + fname + ': concatenating dimension-variable has time units!'
+        print '    shifting temporal-values since the date of the first file', refdate
+    else:
+        tunits = False
+    begslicetot = 0
+    for ifile in range(Nfiles):
+        slicevartot = []
+        if vardimname != 'WRFtime' and not ncobjs[ifile].variables.has_key(vardimname):
+            print errormsg
+            print '  ' + fname + ": file '" + confiles[ifile] + "' does not have " + \
+              " dimension-variable '" + vardimname + "' !!"
+            quit(-1)
+        if vardimname != 'WRFtime':
+            objvar = ncobjs[ifile].variables[vardimname]
+            for dimn in objvar.dimensions:
+                ldimfile = len(ncobjs[ifile].dimensions[dimn])
+                if dimn == condim:
+                    slicevartot.append(slice(begslicetot,begslicetot+ldimfile))
+                    begslicetot = begslicetot + ldimfile
+                else:
+                    slicevartot.append(slice(0,ldimfile))
+                if tunits:
+                    uvals = objvar.getncattr('units')
+                    #urefvals, objvals = gen.check_timestep(odimvar)
+                    #uvals, objvals = gen.check_timestep(objvar)
+                    newtimes = gen.coincident_CFtimes(objvar[:], urefvals, uvals)
+                    newvar[tuple(slicevartot)] = newtimes[:]
+                else:
+                    newvar[tuple(slicevartot)] = objvar[:]
+                newnc.sync()
+        else:
+            odimvarf = ncobjs[ifile].variables['Times']
+            timewrfvf = odimvarf[:]
+            tvalsf, utimef = compute_WRFtime(timewrfvf, refdate='19491201000000', tunitsval='minutes')
+            ldimfile = tvalsf.shape[0] 
+            dimn = 'time'
+            condimn = 'time'
+            if dimn == condim or (condim == 'Time' and vardimname == 'WRFtime'):
+                slicevartot.append(slice(begslicetot,begslicetot+ldimfile))
+                begslicetot = begslicetot + ldimfile
+            else:
+                slicevartot.append(slice(0,ldimfile))
+            if tunits:
+                uvals = utimef
+                #urefvals, objvals = gen.check_timestep(odimvar)
+                #uvals, objvals = gen.check_timestep(objvar)
+                newtimes = gen.coincident_CFtimes(tvalsf, urefvals, uvals)
+                newvar[tuple(slicevartot)] = newtimes[:]
+            newnc.sync()
+
+# Looping variables
+##
+    if varn == 'all':
+        desvars = ncobjs[0].variables.keys()
+    else:
+        desvars = varn.split(',')
+
+    for dvar in desvars:
+        if not ncobjs[0].variables.has_key(dvar):
+            print errormsg
+            print '  ' + fname + ": files do not have variable '" + dvar + "' !!!"
+            quit(-1)
+
+    if gen.searchInlist(desvars, vardimname): desvars.remove(vardimname)
+
+    # dictionary with the slice on variable
+    slicedimv = {}
+    # list of dimensions to remove due to slice from variable
+    rmdimv = []
+    for dvar in desvars:
+        print '  ' + fname + ": concatenating '" + dvar + "'..."
+        objvar = ncobjs[0].variables[dvar]
+# creation of dimensions
+        vardims = objvar.dimensions
+        lvardims = list(objvar.dimensions)
+        for ndim in vardims:
+            if not newnc.dimensions.has_key(ndim):
+                vardimo = ncobjs[0].dimensions[ndim]
+                if vardimo.isunlimited():
+                    newnc.createDimension(ndim, None)
+                else:
+                    if slicedicv.has_key(ndim):
+                        slcddvv = slicedicv[ndim]
+                        if type(slcddvv) == type([1,2]):
+                            Nvals = np.arange(slcddvv[0], slcddvv[1]+slcddvv[2],     \
+                              slcddvv[2])
+                            newnc.createDimension(ndim, Nvals)
+                            slicedimv[ndim]= slice(slcddvv[0], slcddvv[1]+slcddvv[2],\
+                              slcddvv[2])
+                        else
+                            if slcddv == -1:
+                                newnc.createDimension(ndim, len(vardimo))
+                                slicedimv[ndim]= slice(0, len(vardimo))
+                            elif slcddv == -9:
+                                slicedimv[ndim]= len(vardimo)-1
+                                lvardims.remove(ndim)
+                            else:
+                                slicedimv[ndim]= slcddvv
+                                lvardims.remove(ndim)
+                    else:
+                        newnc.createDimension(ndim, len(vardimo))
+# creation of variable
+        kvar = objvar.dtype
+        if newnc.variables.has_key(dvar):
+            print infmsg
+            print '  ' + fname + ": file already has variable '" + dvar + "' !!"
+            print "    creating a new one called '" + dvar + "2'"
+            newvar = newnc.createVariable(dvar + '2', kvar, lvardims)
+        else:
+            newvar = newnc.createVariable(dvar, kvar, lvardims)
+        varattrs = objvar.ncattrs()
+        for atr in varattrs:
+            attrv = objvar.getncattr(atr)
+            newattr = set_attribute(newvar, atr, attrv)
+
+# variable values
+#        vartotv = np.zeros((newvar.shape), dtype=kvar)
+        objvar = ncobjs[0].variables[dvar]
+        if gen.searchInlist(list(objvar.dimensions),condim):
+            begslicetot = 0
+            for ifile in range(Nfiles):
+                slicevartot = []
+                if ncobjs[ifile].variables.has_key(dvar):
+                    objvar = ncobjs[ifile].variables[dvar]
+                    foundim = False
+                    slcvarif = []
+                    for dimn in objvar.dimensions:
+                        ldimfile = len(ncobjs[ifile].dimensions[dimn])
+                        if dimn == condim:
+                            slicevartot.append(slice(begslicetot,begslicetot+ldimfile))
+                            begslicetot = begslicetot + ldimfile
+                            foundim = True
+                            slicevarif.append(slice(0,ldimfile))
+                        else:
+                            if slicedimv.has_key(dimn):
+                                slcvvdd = slicedimv[dimn]
+                                if slcvdd != type(int):
+                                    Nvals = len(range(slcddvv[0],                    \
+                                      slcddvv[1]+slcddvv[2], slcddvv[2])
+                                    slicevartot.append(slice(0,Nvals))
+                                    slicevarif.append(slcvvdd)
+                                else:
+                                    slicevarif.append(slicedimv[dimn])
+                            else:
+                                slicevartot.append(slice(0,ldimfile))
+                                slicevarif.append(slice(0,ldimfile))
+                    if not foundim:
+                        print 'Dimension ', condim, 'not found!! Assuming  1 value'
+                        slicevartot.append(0)
+                    newvar[tuple(slicevartot)] = objvar[tuple(slicevarif)]
+                    newnc.sync()
+                else:
+                    print errormsg
+                    print '  ' + fname + ": file '" + fold + '/' + confiles[ifile] + \
+                      "' does not have variable '" + dvar + "' !!"
+                    quit(-1)
+        else:
+            newvar[:] = objvar[:]
+            newnc.sync()
+
+#        newvar[:] = vartotv
+
+    gattrs = ncobjs[0].ncattrs()
+    for attr in gattrs:
+        attrv = ncobjs[0].getncattr(attr)
+        newattr = set_attribute(newnc, attr, attrv)
+
+    for ifile in range(Nfiles):
+        ncobjs[ifile].close()
+
+    newnc.sync()
+    add_global_PyNCplot(newnc, main, fname, '1.1')
+
+    newnc.close()
+
+    print "Successfull creation of concatenated file '" + ofile + "' !!!"
+
+    return
+
 #quit() 
 
