Changeset 1005 in lmdz_wrf for trunk


Ignore:
Timestamp:
Aug 11, 2016, 1:38:08 PM (9 years ago)
Author:
lfita
Message:

Adding `WRF_toCF': Function to pass a WRF original file to CF-conventions

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var.py

    r967 r1005  
    1414## e.g. # nc_var.py -o computevar_model -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S hurs
    1515## e.g. # nc_var.py -o model_characteristics -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S WRF
     16## e.g. # nc_var.py -o WRF_toCF -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S XLONG:XLAT:19491201000000:minutes
    1617
    1718from optparse import OptionParser
     
    5152  'vrattr', 'WRF_d0Nref',                                                            \
    5253  'WRF_CFlonlat_creation', 'WRF_CFtime_creation', 'WRF_CFxtime_creation',            \
    53   'list_operations']
     54  'list_operations', 'WRF_toCF']
    5455
    5556### Options
     
    296297elif oper == 'WRF_CFxtime_creation':
    297298    ncvar.WRF_CFxtime_creation(opts.values, opts.ncfile, opts.varname)
     299elif oper == 'WRF_toCF':
     300    ncvar.WRF_toCF(opts.values, opts.ncfile)
    298301else:
    299302    print errormsg
  • trunk/tools/nc_var_tools.py

    r1002 r1005  
    135135# WRF_CFxtime_creation: Function to add a CF-convention time unit in a WRF file using variable 'XTIME'
    136136# WRF_d0Nref: Function for the generation of an extra WRF domain from a given one
     137# WRF_toCF: Function to pass a WRF original file to CF-conventions
    137138
    138139main = 'nc_var_tools.py'
     
    1325113252                  str(fendvs[ivar]) + ',1); '
    1325213253
    13253     ofile = ofile=filen.split('.')[0] + ofiletile + '.nc'
     13254    ofile = filen.split('.')[0] + ofiletile + '.nc'
    1325413255
    1325513256    ncoobj = NetCDFFile(ofile,'w')
     
    1644116442    print fname + ": successful written of '" + ofile + "' !!"
    1644216443
     16444def WRF_toCF(values, ncfile):
     16445    """ Function to pass a WRF original file to CF-conventions
     16446      values= [wrfvl]:[wrfvL]:[refDateTime]:[Tunits]
     16447        [wrfvl]: WRF longitude var name (it might come from WPS!)
     16448        [wrfvL]: WRF latitude var name (it might come from WPS!)
     16449        [refDateTime]: date of reference for times (in [YYYY][MM][DD][HH][MI][SS] format)
     16450        [Tunits]: units for time variable
     16451      ncfile= file to transform
     16452      variables= variables to transform
     16453    """
     16454    fname = 'WRF_toCF'
     16455
     16456    if values == 'h':
     16457        print fname + '_____________________________________________________________'
     16458        print WRF_toCF.__doc__
     16459        quit()
     16460
     16461    arguments = '[wrfvl]:[wrfvL]:[refDateTime]:[Tunits]'
     16462    gen.check_arguments(fname, values, arguments, ':')
     16463
     16464    ofile = 'CF_WRF.nc'
     16465
     16466    wrfvl = values.split(':')[0]
     16467    wrfvL = values.split(':')[1]
     16468    refDateTime = values.split(':')[2]
     16469    Tunits = values.split(':')[3]
     16470
     16471    # CFing time
     16472    WRF_CFtime_creation(refDateTime+','+Tunits, ncfile, 'time')
     16473   
     16474    # CFing lon,lat
     16475    WRF_CFlonlat_creation('lon,lat', ncfile, wrfvl+','+wrfvL)
     16476
     16477    ncf = NetCDFFile(ncfile,'a')
     16478    ncdims = ncf.dimensions
     16479
     16480    CFdims = {'west_east': 'lon', 'south_north': 'lat', 'Time': 'time'}
     16481
     16482    # CFing dimensions
     16483    for dimn in CFdims.keys():
     16484        if gen.searchInlist(ncdims,dimn):
     16485            # Making sure CF dimension is not in the file
     16486            if not gen.searchInlist(ncdims,CFdims[dimn]):
     16487                newname = ncf.renameDimension(dimn,CFdims[dimn])
     16488
     16489    # dimension variables which do not need to attach CF attributes, otherwise values
     16490    #   retrieved from 'variable_values.dat'
     16491    dimvars = ['lon', 'lat', 'time', 'Times']
     16492
     16493    for vn in ncf.variables.keys():
     16494        if not gen.searchInlist(dimvars,vn):
     16495            print fname + ' Lluis vn:',vn
     16496            varobj = ncf.variables[vn]
     16497            varattrs = gen.variables_values(vn)
     16498
     16499            stn = varattrs[1]
     16500            lonn = varattrs[4].replace('|',' ')
     16501            un = varattrs[5].replace('|',' ')
     16502
     16503            newattr = set_attribute(varobj, 'standard_name', stn)
     16504            newattr = set_attribute(varobj, 'long_name', lonn)
     16505            newattr = set_attribute(varobj, 'units', un)
     16506            newattr = set_attribute(varobj, 'coordinates', 'lon lat')
     16507            ncf.sync()
     16508
     16509    ncf.sync()
     16510    ncf.close()
     16511
     16512    print fname + ": succesfull CFication of WRF file '" + ncfile + "' !!"
     16513
     16514    return
     16515
    1644316516#quit()
Note: See TracChangeset for help on using the changeset viewer.