Changeset 2247 in lmdz_wrf


Ignore:
Timestamp:
Nov 26, 2018, 3:10:55 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `dimrename': Renaming a dimension from a file
Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var.py

    r2225 r2247  
    6666## 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
    6767## 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
     68## 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
    6869
    6970from optparse import OptionParser
     
    104105# DataSetSection_multivars: Function to get a section (values along multiple variables) of a given data-set
    105106# DYNAMICO_toCF: Function to pass a DYNAMICO original file to CF-conventions
     107# dimrename: Renaming a dimension from a file
    106108# dimrm: Removing a dimension from a file
    107109# dimToUnlimited: Operation to create an unlimited dimension from an existing one
     
    217219  'computevar_model', 'curve_section', 'DatesFiles',                                 \
    218220  'DataSetSection', 'DataSetSection_multidims', 'DataSetSection_multivars',          \
    219   'DYNAMICO_toCF', 'dimrm', 'dimToUnlimited', 'dimVar_creation',                     \
     221  'DYNAMICO_toCF', 'dimrename', 'dimrm', 'dimToUnlimited', 'dimVar_creation',        \
    220222  'fattradd',                                                                        \
    221223  'fdimadd', 'fgaddattr', 'field_stats', 'field_stats_dim', 'file_creation',         \
     
    373375elif oper == 'DYNAMICO_toCF':
    374376    ncvar.DYNAMICO_toCF(opts.values, opts.ncfile)
     377elif oper == 'dimrename':
     378    ncvar.dimrename(opts.ncfile, opts.values, opts.varname)
    375379elif oper == 'dimrm':
    376380    ncvar.dimrm(opts.ncfile, opts.values, opts.varname)
  • trunk/tools/nc_var_tools.py

    r2246 r2247  
    6161# DataSetSection_multivars: Function to get a section (values along multiple variables) of a given data-set
    6262# DatesFiles: Function to find different time values on a series of WRF files in a folder
     63# dimrename: Renaming a dimension from a file
    6364# dimrm: Removing a dimension from a file
    6465# DimsLoop: Function to provide the shape of the dimensions which are not selected
     
    17541755
    17551756  return
     1757
     1758 
     1759def dimrename(ncfile, newdimn, dimn):
     1760    """ Renaming a dimension from a file
     1761    ncfile = netCDF object file
     1762    values= [newdimn], new name for the dimension
     1763    dimn = dimension name to change
     1764    """
     1765    import shutil as shu
     1766    fname = 'dimrename'
     1767
     1768    if newdimn == 'h':
     1769        print fname + '_____________________________________________________________'
     1770        print dimrename.__doc__
     1771        quit()
     1772
     1773    ncf = NetCDFFile(ncfile,'a')
     1774    if not ncf.dimensions.has_key(dimn):
     1775        print errormsg
     1776        print '  ' + fname + ": file does not have dimension name '" + dimn+ "' !!"
     1777        dimns = ncf.dimensions
     1778        dimns.sort()
     1779        print '    available ones:', dimns
     1780        ncf.close()
     1781        quit(-1)
     1782
     1783    if ncf.dimensions.has_key(newdimn):
     1784        print warnmsg
     1785        print '  ' + fname + ": file already have dimension name '" + newdimn+ "' !!"
     1786        print "    looping all over that variables with dimension name '"+dimn+"' !!"
     1787        for varn in ncf.variables.keys():
     1788            ovar = ncf.variables[varn]
     1789            if gen.searchInlist(ovar.dimensions,dimn):
     1790                vardims = list(ovar.dimensions)
     1791                idim = vardims.index(dimn)
     1792                vardims[idim] = newdimn
     1793               
     1794                newvar = ncf.createVariable(varn+'tmp', ovar.dtype, tuple(vardims))
     1795                newvar[:] = ovar[:]
     1796                for ncattr in ovar.ncattrs():
     1797                    attrv = ovar.getncattr(ncattr)
     1798                    newvar.setncattr(ncattr, attrv)
     1799                ncf.sync()
     1800                ncf.renameVariable(varn, varn+'old')
     1801                ncf.renameVariable(varn+'tmp', varn)
     1802                ncf.sync()
     1803    else:
     1804        newdim = ncf.renameDimension(dimn, newdimn)
     1805    ncf.sync()
     1806    ncf.close()
     1807
     1808    return
    17561809 
    17571810def dimrm(ncfile, values, dimn):
Note: See TracChangeset for help on using the changeset viewer.