Index: trunk/tools/nc_var.py
===================================================================
--- trunk/tools/nc_var.py	(revision 403)
+++ trunk/tools/nc_var.py	(revision 404)
@@ -25,5 +25,6 @@
   'filter_2dim',                                                                     \
   'flipdim', 'fvaradd', 'gaddattrk', 'gaddattr', 'get_namelist_vars', 'grattr',      \
-  'grmattr', 'igattrs', 'isgattrs', 'isvattrs', 'ivars', 'ivattrs', 'maskvar',       \
+  'grmattr', 'igattrs', 'increaseDimvar', 'isgattrs', 'isvattrs', 'ivars', 'ivattrs',\
+  'maskvar',                                                                         \
   'ncreplace', 'ncstepdiff', 'netcdf_concatenation', 'netcdf_fold_concatenation',    \
   'compute_opersvarsfiles',                                                          \
@@ -156,4 +157,6 @@
 elif oper == 'igattrs':
     ncvar.igattrs(opts.ncfile)
+elif oper == 'increaseDimvar':
+    ncvar.increaseDimvar(opts.values, opts.ncfile, opts.varname)
 elif oper == 'isgattrs':
     ncvar.isgattrs(opts.values, opts.ncfile)
Index: trunk/tools/nc_var_tools.py
===================================================================
--- trunk/tools/nc_var_tools.py	(revision 403)
+++ trunk/tools/nc_var_tools.py	(revision 404)
@@ -2062,4 +2062,6 @@
   ncref.close()
 
+  return
+
 def varrm(ncfile, var):
   """ Removing a variable from a file
@@ -14006,5 +14008,146 @@
 
 #dimToUnlimited('lon', 'test.nc')
-#quit()
+
+def increaseDimvar(values, ncfile, varn):
+    """ Function to increase with 1 dimension an existing variable within a netcdf file. Values 
+    of the variable will be repeated along the new dimension
+      values='[dimname]:[size]:[position]:[unlimited]'
+        dimname: name of the new dimension
+        size: size of the new dimension
+        position: position for the dimensions (starting from 0, outter most [left]) 
+        unlimited: y/n value for an unlimited dimension
+      ncfile= netCDF file to use
+      varn= variable to change its dimensions
+
+    """
+    import subprocess as sub
+
+    fname = 'increaseDimvar'
+
+    ofile = ncfile + '_new.nc'
+
+    if values == 'h':
+        print fname + '_____________________________________________________________'
+        print increaseDimvar.__doc__
+        quit()
+
+    expectargs = '[dimname]:[size]:[position]:[unlimited]'
+ 
+    check_arguments(fname,len(expectargs.split(':')),values,':',expectargs)
+
+    dimname = values.split(':')[0]
+    size = int(values.split(':')[1])
+    position = int(values.split(':')[2])
+    unlimited = values.split(':')[3]
+
+    onc = NetCDFFile(ncfile, 'r')
+
+    dims = onc.dimensions
+    if searchInlist(dims, dimname):
+        print errormsg
+        print '  ' + fname + ": file '"  + ncfile + "' already has dimension '" +    \
+          dimname + "' !!"
+        quit(-1)
+
+    varns = onc.variables
+    if not searchInlist(varns, varn):
+        print errormsg
+        print '  ' + fname + ": file '"  + ncfile + "' has not variable '" + varn +  \
+          "' !!"
+        quit(-1)
+    
+    varobj = onc.variables[varn]
+    vardims = varobj.dimensions
+    varkind = varobj.dtype
+    Norigdims = len(vardims)
+
+    if position > Norigdims - 1:
+        print errormsg
+        print '  ' + fname + ": wrong position ", position," for the new dimension ",\
+          "on variable '" + varn + "' has only", Norigdims," dimensions !!"
+        quit(-1)
+
+    newdimns = []
+    newdimvs = []
+
+    idorig = Norigdims - 1
+    for idn in range(Norigdims + 1):
+        if idn == position:
+            newdimns.append(dimname)
+            newdimvs.append(size)
+        else:
+            newdimns.append(vardims[idorig])
+            newdimvs.append(varobj.shape[idorig])
+            idorig = idorig - 1
+
+# New variable
+    newvarv = np.zeros(tuple(newdimvs), dtype=varkind)
+    for inewdim in range(size):
+        slicev = []
+        for idn in range(Norigdims + 1):
+            if idn == position:
+                slicev.append(inewdim)
+            else:
+                slicev.append(slice(0,varobj.shape[idorig]))
+
+        newvarv[tuple(slicev)] = varobj[:]
+        
+# Creation of a new file with the new variable dimensions
+    newnc = NetCDFFile(ofile, 'w')
+
+# Creation of dimensions
+    for dn in dims:
+        print 'adding dim:',dn,' ...'
+        if onc.dimensions[dn].isunlimited():
+            newdim = newnc.createDimension(dn, None)
+        else:
+            newdim = newnc.createDimension(dn, len(onc.dimensions[dn]))
+
+    if unlimited == 'y':
+        newdim = newnc.createDimension(dimname, None)
+    else:
+        newdim = newnc.createDimension(dimname, size)
+
+    newnc.sync()
+
+# Adding variables
+    for vn in varns:
+        print 'adding variable:',vn,'...'
+        vno = onc.variables[vn]
+        if vn == varn:
+            vdim = tuple(newdimns)
+        else:
+            vdim = vno.dimensions
+        vtype = vno.dtype
+        varatts = vno.ncattrs()
+
+        if searchInlist(varatts, '_FillValue'):
+            fillv = vno.getncattr('_FillValue')
+            newvar = newnc.createVariable(vn, vtype, vdim, fill_value=fillv)
+        else:
+            newvar = newnc.createVariable(vn, vtype, vdim)
+
+        for nattr in vno.ncattrs():
+            if not nattr == '_FillValue':
+                nattrv = vno.getncattr(nattr)
+                newattr = newvar.setncattr(nattr, nattrv)
+
+        if vn == varn:
+            newvar[:] = newvarv[:]
+        else:
+            newvar[:] = vno[:]
+
+
+    onc.close()
+    newnc.sync()
+    newnc.close()
+
+# Adding attributes
+    fgaddattr(ncfile, ofile)
+    sub.call(['mv',ofile,ncfile])
+
+    return
+
+#increaseDimvar('shan:29:1:y', 'test.nc', 'var')
 
 """operation to make: 
