Changeset 1062 in lmdz_wrf for trunk


Ignore:
Timestamp:
Aug 30, 2016, 3:46:56 PM (9 years ago)
Author:
lfita
Message:

Adding `CDO_toCF': Function to pass a CDO output file to CF-conventions

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var.py

    r1053 r1062  
    1717## e.g. # nc_var.py -o cleaning_varsfile -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S T2,XLONG,XLAT,Times
    1818## e.g. # nc_var.py -o LMDZ_toCF -f LMDZ/AR40/vas_histins_1-1.nc
     19## e.g. # nc_var.py -o CDO_toCF -f LMDZ/AR40/hfss_reproj-histins.nc
    1920## e.g. # nc_var.py -o pinterp -f wrfout_d01_2001-11-11_00\:00\:00 -S 100000.:97500.:95000.:92500.:90000.:85000.:80000.:75000.:70000.:65000.:60000.:55000.:50000.:45000.:40000.:35000.:30000.:25000.:20000.:15000.:10000.:5000.:2500.:1000.:500.:250.,1,0 -v WRFt,WRFrh
    2021## e.g. # nc_var.py -o pinterp -f /media/ExtDiskC_ext4/DATA/etudes/WRF_LMDZ/WaquaL_highres/short_copies/LMDZ/AR40/histins_19790101000000-19790304000000_short.nc -S 100000.:97500.:95000.:92500.:90000.:85000.:80000.:75000.:70000.:65000.:60000.:55000.:50000.:45000.:40000.:35000.:30000.:25000.:20000.:15000.:10000.:5000.:2500.:1000.:500.:250.,1,0 -v temp,ovap
     
    2930import generic_tools as gen
    3031
    31 operations=['addvals', 'chdimname', 'changevartype', 'checkallvars',                 \
     32operations=['addvals', 'CDO_toCF', 'chdimname', 'changevartype', 'checkallvars',     \
    3233  'checkAllValues', 'checkNaNs',                                                     \
    3334  'chgtimestep', 'chvarname', 'cleaning_varsfile', 'compute_deaccum',                \
     
    121122if oper == 'addvals':
    122123    ncvar.addvals(opts.values, opts.ncfile, opts.varname)
     124elif oper == 'CDO_toCF':
     125    ncvar.CDO_toCF(opts.ncfile)
    123126elif oper == 'chdimname':
    124127    ncvar.chdimname(opts.values, opts.ncfile)
  • trunk/tools/nc_var_tools.py

    r1055 r1062  
    14191419
    14201420  ncf = NetCDFFile(ncfile,'a')
    1421   ncvars = ncf.variables
     1421  ncvars = ncf.variables.keys()
    14221422  ncf.close()
    14231423
     
    14251425      print '  ' + fname + ": File '" + ncfile + "' does not have variable: '" +     \
    14261426        var + "' !!"
    1427       ncf.close()
     1427      print '  variables:', ncvars
    14281428      quit(-1)
    14291429
     
    1676016760    return
    1676116761
     16762def CDO_toCF(ncfile):
     16763    """ Function to pass a CDO output file to CF-conventions
     16764      ncfile= file to transform
     16765      variables= variables to transform
     16766    """
     16767    fname = 'CDO_toCF'
     16768
     16769    if ncfile == 'h':
     16770        print fname + '_____________________________________________________________'
     16771        print CDO_toCF.__doc__
     16772        quit()
     16773
     16774    ncf = NetCDFFile(ncfile,'a')
     16775    ncdims = ncf.dimensions.keys()
     16776    ncvars = ncf.variables.keys()
     16777
     16778    # renaming variables 'lon, lat'
     16779    CFvardims = {'lon': 'lon2', 'lat': 'lat2'}
     16780    for dimvarn in CFvardims.keys():
     16781        if gen.searchInlist(ncvars,dimvarn):
     16782            newn = CFvardims[dimvarn]
     16783            print warnmsg
     16784            print fname + ": renaming variable '" + dimvarn + "' as '" + newn + "' !!"
     16785            newname = ncf.renameVariable(dimvarn,newn)
     16786    ncf.sync()
     16787
     16788    # CFing dimensions
     16789    CFdims = {'x': 'lon', 'y': 'lat'}
     16790    for dimn in CFdims.keys():
     16791        if gen.searchInlist(ncdims,dimn):
     16792            # Making sure CF dimension is not in the file
     16793            if not gen.searchInlist(ncdims,CFdims[dimn]):
     16794                newname = ncf.renameDimension(dimn,CFdims[dimn])
     16795    ncf.sync()
     16796
     16797    ncvars = ncf.variables.keys()
     16798    # CFing variables 'lon, lat'
     16799    olon2 = ncf.variables['lon2']
     16800    dimslon2 = olon2.dimensions
     16801    newvar = ncf.createVariable('lon','f8',dimslon2)
     16802    newvar[:] = olon2[:]
     16803    olonattr = olon2.ncattrs()
     16804    for attrn in olonattr:
     16805        attrv = olon2.getncattr(attrn)
     16806        set_attribute(newvar,attrn,attrv)
     16807
     16808    olat2 = ncf.variables['lat2']
     16809    dimslat2 = olat2.dimensions
     16810    newvar = ncf.createVariable('lat','f8',dimslat2)
     16811    newvar[:] = olat2[:]
     16812    olatattr = olat2.ncattrs()
     16813    for attrn in olatattr:
     16814        attrv = olat2.getncattr(attrn)
     16815        set_attribute(newvar,attrn,attrv)
     16816
     16817    ncvars = ncf.variables.keys()
     16818    # Re-arranging coordinates attribute for all the variables
     16819    for varn in ncvars:
     16820        varobj = ncf.variables[varn]
     16821        varattrs = varobj.ncattrs()
     16822        if gen.searchInlist(varattrs, unicode('coordinates')):
     16823            attrv = varobj.getncattr('coordinates')
     16824            newcoord = str(attrv)
     16825            for dimn in CFdims.keys():
     16826                newcoord = newcoord.replace(dimn, CFdims[dimn])
     16827            newattr = set_attribute(varobj, 'coordinates', newcoord)
     16828
     16829    ncf.sync()
     16830
     16831    # We should removing spurious lon2, lat2, but this would make too much...
     16832
     16833    ncf.close()
     16834
     16835    print fname + ": succesfull CFication of CDO file '" + ncfile + "' !!"
     16836
     16837    return
     16838
    1676216839def cleaning_varsfile(values, filen):
    1676316840    """ Function to keep a list of varibales from a file
Note: See TracChangeset for help on using the changeset viewer.