Index: /trunk/tools/nc_var.py
===================================================================
--- /trunk/tools/nc_var.py	(revision 2246)
+++ /trunk/tools/nc_var.py	(revision 2247)
@@ -66,4 +66,5 @@
 ## e.g. # nc_var.py -o same_deltasign -f wrfout_d01_1995-01-01_00:00:00 -S 'Time:0|south_north:60|west_east:-1' -v T2,Q2,XLAT,XLONG
 ## e.g. # nc_var.py -o compute_slices_stats -S 'XLONG,-74.,-36.,4.;XLAT,-62.,18.,2.;HGT,500.,7000.,500.@Time|Times:west_east|XLONG:south_north|XLAT@Time' -f wrfout_d01_1995-01-01_00:00:00 -v T2,Q2
+## e.g. # nc_var.py -o dimrename -f /media/lluis/ExtDiskC_ext3/DATA/estudios/FPS_Alps/additional/IOP/select/out_irene/simcdx_vars_cape_120lev_cdxwrf2.nc -S Time -v time0
 
 from optparse import OptionParser
@@ -104,4 +105,5 @@
 # DataSetSection_multivars: Function to get a section (values along multiple variables) of a given data-set
 # DYNAMICO_toCF: Function to pass a DYNAMICO original file to CF-conventions
+# dimrename: Renaming a dimension from a file
 # dimrm: Removing a dimension from a file
 # dimToUnlimited: Operation to create an unlimited dimension from an existing one
@@ -217,5 +219,5 @@
   'computevar_model', 'curve_section', 'DatesFiles',                                 \
   'DataSetSection', 'DataSetSection_multidims', 'DataSetSection_multivars',          \
-  'DYNAMICO_toCF', 'dimrm', 'dimToUnlimited', 'dimVar_creation',                     \
+  'DYNAMICO_toCF', 'dimrename', 'dimrm', 'dimToUnlimited', 'dimVar_creation',        \
   'fattradd',                                                                        \
   'fdimadd', 'fgaddattr', 'field_stats', 'field_stats_dim', 'file_creation',         \
@@ -373,4 +375,6 @@
 elif oper == 'DYNAMICO_toCF':
     ncvar.DYNAMICO_toCF(opts.values, opts.ncfile)
+elif oper == 'dimrename':
+    ncvar.dimrename(opts.ncfile, opts.values, opts.varname)
 elif oper == 'dimrm':
     ncvar.dimrm(opts.ncfile, opts.values, opts.varname)
Index: /trunk/tools/nc_var_tools.py
===================================================================
--- /trunk/tools/nc_var_tools.py	(revision 2246)
+++ /trunk/tools/nc_var_tools.py	(revision 2247)
@@ -61,4 +61,5 @@
 # DataSetSection_multivars: Function to get a section (values along multiple variables) of a given data-set
 # DatesFiles: Function to find different time values on a series of WRF files in a folder
+# dimrename: Renaming a dimension from a file
 # dimrm: Removing a dimension from a file
 # DimsLoop: Function to provide the shape of the dimensions which are not selected
@@ -1754,4 +1755,56 @@
 
   return
+
+ 
+def dimrename(ncfile, newdimn, dimn):
+    """ Renaming a dimension from a file
+    ncfile = netCDF object file
+    values= [newdimn], new name for the dimension
+    dimn = dimension name to change
+    """
+    import shutil as shu
+    fname = 'dimrename'
+
+    if newdimn == 'h':
+        print fname + '_____________________________________________________________'
+        print dimrename.__doc__
+        quit()
+
+    ncf = NetCDFFile(ncfile,'a')
+    if not ncf.dimensions.has_key(dimn):
+        print errormsg
+        print '  ' + fname + ": file does not have dimension name '" + dimn+ "' !!"
+        dimns = ncf.dimensions
+        dimns.sort()
+        print '    available ones:', dimns
+        ncf.close()
+        quit(-1)
+
+    if ncf.dimensions.has_key(newdimn):
+        print warnmsg
+        print '  ' + fname + ": file already have dimension name '" + newdimn+ "' !!"
+        print "    looping all over that variables with dimension name '"+dimn+"' !!"
+        for varn in ncf.variables.keys():
+            ovar = ncf.variables[varn]
+            if gen.searchInlist(ovar.dimensions,dimn):
+                vardims = list(ovar.dimensions)
+                idim = vardims.index(dimn)
+                vardims[idim] = newdimn
+                
+                newvar = ncf.createVariable(varn+'tmp', ovar.dtype, tuple(vardims))
+                newvar[:] = ovar[:]
+                for ncattr in ovar.ncattrs():
+                    attrv = ovar.getncattr(ncattr)
+                    newvar.setncattr(ncattr, attrv)
+                ncf.sync()
+                ncf.renameVariable(varn, varn+'old')
+                ncf.renameVariable(varn+'tmp', varn)
+                ncf.sync()
+    else:
+        newdim = ncf.renameDimension(dimn, newdimn)
+    ncf.sync()
+    ncf.close()
+
+    return
  
 def dimrm(ncfile, values, dimn):
