Index: trunk/tools/nc_var_tools.py
===================================================================
--- trunk/tools/nc_var_tools.py	(revision 789)
+++ trunk/tools/nc_var_tools.py	(revision 790)
@@ -4634,4 +4634,32 @@
     Lchar = length of the string in the netCDF file
     """
+    fname = 'get_str_nc'
+
+    Nvalues = varo.shape[0]
+
+    strings = []
+
+    for ival in range(Nvalues):
+        stringv = varo[ival,:]
+        string = str('')
+        for ich in range(Lchar):
+            charval = str(stringv[ich:ich+1]).replace('[','').replace(']','').replace("'","")
+            if charval == '--' or len(charval) == 0:
+                break
+            else:
+                if ich == 0:
+                    string = charval
+                else:
+                    string = string + charval
+
+        strings.append(string.strip())
+
+    return strings
+
+def get_str_nc_word(varo, Lchar):
+    """ Function to get string values in a netCDF variable as a chains of 1char values
+    varo= netCDF variable object
+    Lchar = length of the string in the netCDF file
+    """
 
     Nvalues = varo.shape[0]
@@ -12304,9 +12332,9 @@
       onc= output netcdf object
       vn= variable name to include
-      sliced= dictioary with the dimension name as key and [beg, end, int] as values
+      sliced= dictionary with the dimension name as key and [beg, end, int] as values
     """
     fname = 'put_variable_slice'
 
-    if not searchInlist(inc.variables,vn):
+    if not gen.searchInlist(inc.variables,vn):
         print errormsg
         print ' ' + fname + ": variable '" + vn + "' is not in file!!"
@@ -12315,22 +12343,34 @@
     ov = inc.variables[vn]
 
-    dnvs = ov.dimensions
+#    dnvs = ov.dimensions
+    dnvs = []
     varslice = []
 # Getting variable's dimensions
-    for dnv in dnvs:
-        if not searchInlist(onc.dimensions,dnv):
-            od = inc.dimensions[dnv]
-            if od.isunlimited():
-                dsize = None
+    for dnv in ov.dimensions:
+        if gen.searchInlist(sliced.keys(),dnv):
+            newdimsize = sliced[dnv][1] - sliced[dnv][0]
+        else:
+            newdimsize = 10
+        if not gen.searchInlist(onc.dimensions,dnv):
+# Avoiding montone dimensions!
+            if newdimsize > 0:
+                od = inc.dimensions[dnv]
+                if od.isunlimited():
+                    dsize = None
+                else:
+                    dsize = len(od)
+                newdim = onc.createDimension(dnv, dsize)
+        if gen.searchInlist(sliced.keys(),dnv):
+            if newdimsize > 0:
+                varslice.append(slice(sliced[dnv][0], sliced[dnv][1], sliced[dnv][2]))
+                dnvs.append(dnv)
             else:
-                dsize = len(od)
-            newdim = onc.createDimension(dnv, dsize)
-        if searchInlist(sliced.keys(),dnv):
-            varslice.append(slice(sliced[dnv][0], sliced[dnv][1], sliced[dnv][2]))
+                varslice.append(sliced[dnv][0])
         else:
             varslice.append(slice(0,len(inc.dimensions[dnv])))
+            dnvs.append(dnv)
 # Getting variable
     varattrs = ov.ncattrs()
-    if searchInlist(varattrs, '_FillValue'):
+    if gen.searchInlist(varattrs, '_FillValue'):
         varfil = ov._FillValue
     else:
@@ -12338,7 +12378,7 @@
     vartype = ov.dtype
 
-    newvar = onc.createVariable(vn, vartype, dnvs, fill_value=varfil)
-#    print 'Lluis shapes newvar:',newvar.shape,'slice:',varslice
-    newvar[:] = ov[tuple(varslice)]
+    newvar = onc.createVariable(vn, vartype, tuple(dnvs), fill_value=varfil)
+    finalvals = np.squeeze(ov[tuple(varslice)])
+    newvar[:] = finalvals
 
     for attrs in varattrs:
@@ -12353,8 +12393,8 @@
       values= [varn1],[beg1],[end1],[int1]@[...[[varnM],[begM],[endM],[intM]]]
         [varni]: name of the variable ('WRFt', for WRF time varibale)
-        [begi],[endi],[inti]: beginning, end and interval along the dimension-axis
+        [begi],[endi],[inti]: beginning, end and interval along the variable
           [endi] = -1, maximum value
           [inti] = -1, all the values within the range
-          NOTE: dimensions without section by the variables are taken allover their size
+          NOTE: variables without section by the variables are taken allover their size
       filen= netCDF with the data-set
       varn= ',' list of variables ('all', for all variables)
@@ -12373,4 +12413,6 @@
     Nvars = len(values.split('@'))
 
+    nciobj = NetCDFFile(filen,'r')
+
 # Slicing variables
     varns = []
@@ -12384,20 +12426,47 @@
     for ivar in range(Nvars):
         val = values.split('@')[ivar]
-        varns.append(val.split(',')[0])
-        begvs[ivar] = np.float(val.split(',')[1])
-        endvs[ivar] = np.float(val.split(',')[2])
-        intvs[ivar] = np.float(val.split(',')[3])
-        ofiletile = ofiletile + '_' + varns[ivar] + '_B' + str(begvs[ivar]) + '-E' + \
-          str(endvs[ivar]) + '-I' + str(intvs[ivar])
-        if intvs[ivar] != -1:
-            slicevalS = slicevalS + varns[ivar] + ' (' + str(begvs[ivar]) + ',' +    \
-              str(endvs[ivar]) + ',' + str(intvs[ivar]) + '); '
+        vn = val.split(',')[0]
+        if not gen.searchInlist(nciobj.variables,vn):
+            print errormsg
+            print '  ' + fname + ": file '" + filen + "' does not have variable '" + \
+              vn + "' !!"
+            print '    it has:', nciobj.variables
+            quit(-1)
+
+        ostrvar = nciobj.variables[vn]
+        varns.append(vn)
+        if val.split(',')[3] == 'str':
+            strvalue = val.split(',')[1].replace('!',' ')
+            print warnmsg
+            print '  ' + fname + ": String value for variable '" + val + "' !!"
+            print '    assuming string variable as [Nvalues, Lstring]'
+            print "    from which only is required '" + strvalue + "'"
+            strvals = get_str_nc(ostrvar, ostrvar.shape[1])
+            if not gen.searchInlist(strvals, strvalue):
+                print errormsg
+                print '  ' + fname + ": variable '" + val + "' does not have " +     \
+                  "value: '" + strvalue + "' !!"
+                print '    values:', strvals
+                quit(-1)
+            indstrvalue = strvals.index(strvalue)
+            begvs[ivar] = np.float(indstrvalue)
+            endvs[ivar] = np.float(indstrvalue)
+            intvs[ivar] = -99.
+            ofiletile = ofiletile + '_' + varns[ivar] + '_S'
         else:
-            slicevalS = slicevalS + varns[ivar] + ' (' + str(begvs[ivar]) + ',' +    \
-              str(endvs[ivar]) + ',1); '
+            begvs[ivar] = np.float(val.split(',')[1])
+            endvs[ivar] = np.float(val.split(',')[2])
+            intvs[ivar] = np.float(val.split(',')[3])
+            ofiletile = ofiletile + '_' + varns[ivar] + '_B' + str(begvs[ivar]) + '-E' + \
+              str(endvs[ivar]) + '-I' + str(intvs[ivar])
+            if intvs[ivar] != -1:
+                slicevalS = slicevalS + varns[ivar] + ' (' + str(begvs[ivar]) + ',' +    \
+                  str(endvs[ivar]) + ',' + str(intvs[ivar]) + '); '
+            else:
+                slicevalS = slicevalS + varns[ivar] + ' (' + str(begvs[ivar]) + ',' +    \
+                  str(endvs[ivar]) + ',1); '
 
     ofile = ofile=filen.split('.')[0] + ofiletile + '.nc'
 
-    nciobj = NetCDFFile(filen,'r')
     ncoobj = NetCDFFile(ofile,'w')
 
@@ -12417,6 +12486,6 @@
         print '  ' + fname + ": masking with '" + varns[ivar] + "':",  begvs[ivar],  \
           ',', endvs[ivar], ',', intvs[ivar]
-        if not searchInlist(nciobj.variables,varns[ivar]) and not                    \
-          searchInlist(NOcheck,varns[ivar]):
+        if not gen.searchInlist(nciobj.variables,varns[ivar]) and not                \
+          gen.searchInlist(NOcheck,varns[ivar]):
             print errormsg
             print ' ' + fname + ": variable '" + varns[ivar] + "' is not in ' +      \
@@ -12442,22 +12511,37 @@
             vardims = ovar.dimensions
 
-        if endvs[ivar] == -1: endvs[ivar] = np.max(vals)
-        maskinf = ma.masked_less(vals, begvs[ivar])
-        masksup = ma.masked_greater(vals, endvs[ivar])
-
-        if intvs[ivar] != -1:
-            print errormsg
-            print '  ' + fname + ': non-consecutive slices not ready!!'
-            quit(-1)
-        finalmask = maskinf.mask + masksup.mask
-        idn = 0
+        if intvs[ivar] != -99.:
+            if endvs[ivar] == -1: endvs[ivar] = np.max(vals)
+            maskinf = ma.masked_less(vals, begvs[ivar])
+            masksup = ma.masked_greater(vals, endvs[ivar])
+
+            if intvs[ivar] != -1:
+                print errormsg
+                print '  ' + fname + ': non-consecutive slices not ready!!'
+                quit(-1)
+            finalmask = maskinf.mask + masksup.mask
+            idn = 0
+        else:
+            ostrvar = nciobj.variables[varns[ivar]]
+            finalmask = np.ones((ostrvar.shape), dtype= bool)
+            finalmask[int(begvs[ivar]),:] = False
 
         slicedims = {}
         
+        iidim = 0
         for dn in vardims:
             ddn = len(nciobj.dimensions[dn])
             dinds = np.arange(ddn)
             rightvals = np.where(finalmask, False, True)
-            slicedims[dn]=[np.min(dinds[rightvals]), np.max(dinds[rightvals])+1, None]
+
+            if intvs[ivar] != -99.:
+                slicedims[dn]=[np.min(dinds[rightvals][0,:]), np.max(dinds[rightvals][0,:])+1, None]
+            else:
+                if iidim == 0:
+                    slicedims[dn]=[int(begvs[ivar]), int(begvs[ivar]), None]
+                else:
+                    slicedims[dn]=[0, ddn, None]
+
+            iidim = iidim + 1
 
         print '  ' + fname + ': slicing variables with______'
@@ -12472,6 +12556,6 @@
             dimsize = None
         else:
-            dimsize = slicedims[nd][1] - slicedims[nd][0] + 1
-        newdim = ncoobj.createDimension(nd, dimsize)
+            dimsize = slicedims[nd][1] - slicedims[nd][0]
+        if dimsize > 0: newdim = ncoobj.createDimension(nd, dimsize)
     ncoobj.sync()
 
