Changeset 1879 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Apr 3, 2018, 4:45:50 PM (7 years ago)
Author:
lfita
Message:

Adding:

  • `dimrm': Removing a dimension from a file
Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var.py

    r1857 r1879  
    8787# DataSetSection_multivars: Function to get a section (values along multiple variables) of a given data-set
    8888# DYNAMICO_toCF: Function to pass a DYNAMICO original file to CF-conventions
     89# dimrm: Removing a dimension from a file
    8990# dimToUnlimited: Operation to create an unlimited dimension from an existing one
    9091# dimVar_creation: Function to add a 1D variable with the size of a given dimension in a file
     
    183184  'computevar_model', 'curve_section', 'DatesFiles',                                 \
    184185  'DataSetSection', 'DataSetSection_multidims', 'DataSetSection_multivars',          \
    185   'DYNAMICO_toCF', 'dimToUnlimited', 'dimVar_creation',                              \
     186  'DYNAMICO_toCF', 'dimrm', 'dimToUnlimited', 'dimVar_creation',                     \
    186187  'fattradd',                                                                        \
    187188  'fdimadd', 'fgaddattr', 'field_stats', 'field_stats_dim', 'file_creation',         \
     
    332333elif oper == 'DYNAMICO_toCF':
    333334    ncvar.DYNAMICO_toCF(opts.values, opts.ncfile)
     335elif oper == 'dimrm':
     336    ncvar.dimrm(opts.ncfile, opts.values, opts.varname)
    334337elif oper == 'dimToUnlimited':
    335338    ncvar.dimToUnlimited(opts.values, opts.ncfile)
  • trunk/tools/nc_var_tools.py

    r1878 r1879  
    5757# DataSetSection_multivars: Function to get a section (values along multiple variables) of a given data-set
    5858# DatesFiles: Function to find different time values on a series of WRF files in a folder
     59# dimrm: Removing a dimension from a file
    5960# DimsLoop: Function to provide the shape of the dimensions which are not selected
    6061# DYNAMICO_toCF: Function to pass a DYNAMICO original file to CF-conventions
     
    16351636  fname = 'varrm'
    16361637
    1637   if var == 'h':
     1638  if var == 'h' or var is None:
    16381639      print fname + '_____________________________________________________________'
    16391640      print varrm.__doc__
     
    16651666  return
    16661667 
     1668def dimrm(ncfile, values, dimn):
     1669    """ Removing a dimension from a file
     1670    ncfile = netCDF object file
     1671    values= [varsAction], which action to be done on that variables which contain the
     1672      given dimension
     1673        'renamedim',[newname]: create a new dimension and its varibale-dimension with
     1674          name [newname] for that varibales with the diemnsion to remove
     1675        'removevar': remove varibale with the given dimension
     1676    dimn = dimension name to remove
     1677    """
     1678    import shutil as shu
     1679    fname = 'dimrm'
     1680
     1681    if values == 'h':
     1682        print fname + '_____________________________________________________________'
     1683        print dimrm.__doc__
     1684        quit()
     1685
     1686    ncf = NetCDFFile(ncfile,'a')
     1687    dims = ncf.dimensions
     1688    ncvars = ncf.variables.keys()
     1689    ncf.close()
     1690
     1691    if not gen.searchInlist(dims, var):
     1692        print '  ' + fname + ": File '" + ncfile + "' does not have dimension: '" +  \
     1693          dimn + "' !!"
     1694        print '  dimensions:', dims
     1695        quit(-1)
     1696
     1697    if values[0:9] == 'renamedim':
     1698        newdimn = values.split(',')[1]
     1699        ncf = NetCDFFile(ncfile,'a')
     1700        if gen.searchInlist(dims,newdimn):
     1701            print errormsg
     1702            print '  ' + fname + ": file '" + ncfile + "' already has a " +          \
     1703              "dimension labelled '" + newdimn + "' !!"
     1704            print '    pick up another name'
     1705            ncf.close()
     1706            quit()
     1707        newdim = ncf.renameDimension(dimn, newdimn)
     1708        ncf.sync()
     1709        # Checking for the existence of a dimension-variable of the same name
     1710        if gen.searchInlist(ncvars, dimn):
     1711            if gen.searchInlist(ncvars,newdimn):
     1712                print errormsg
     1713                print '  ' + fname + ": file '" + ncfile + "' already has a " +      \
     1714                  "variable labelled '" + newdimn + "' !!"
     1715                print '    pick up another name'
     1716                ncf.close()
     1717                quit()
     1718            newvar = ncf.renameVariable(dimn, newdimn)
     1719            ncf.sync()
     1720    elif values[0:9] == 'removevar':
     1721        ncf = NetCDFFile(ncfile,'r')
     1722        tmpncf = NetCDFFile('tmp_py.nc' , 'w')
     1723        gtmpattr = set_attribute(tmpncf, 'copy', 'temporal')
     1724        tmpncf.sync()
     1725
     1726        for varn in ncf.variables.keys():
     1727            vdimn = varn.dimensions
     1728            if not gen.searchInlist(vdimn,dimn):
     1729                ncf.close()
     1730                fvaradd(ncfile + ',' + varn, 'tmp_py.nc')
     1731                ncf = NetCDFFile(ncfile,'r')
     1732        ncf.close()
     1733        fgaddattr(ncfile, 'tmp_py.nc')
     1734        shu.copyfile('tmp_py.nc', ncfile)
     1735        os.remove('tmp_py.nc')
     1736
     1737    return
     1738
    16671739def seasmean(timename, filename, varn):
    16681740    """ Function to compute the seasonal mean of a variable
Note: See TracChangeset for help on using the changeset viewer.