Changeset 1421 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Jan 20, 2017, 11:56:32 AM (8 years ago)
Author:
lfita
Message:

Adding:

`mthDYNAMICO_toCF': Function to pass a mthDYNAMICO original file to CF-conventions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r1415 r1421  
    9090# ModelChar: Class object to determine major characterisitcs of a given model output
    9191# model_characteristics: Functino to provide major characterisitcs of a given model output
     92# mthDYNAMICO_toCF: Function to pass a mthDYNAMICO original file to CF-conventions
    9293# ncreplace: Function to replace something from a netCDF file
    9394# ncstepdiff: Function to compute differencies between time-steps (deacumulate) a netCDF file
     
    1776417765    return
    1776517766
    17766 
    1776717767def DYNAMICO_toCF(values,ncfile):
    1776817768    """ Function to pass a DYNAMICO original file to CF-conventions
     
    1783517835
    1783617836    print fname + ": succesfull CFication of DYNAMICO file '" + ncfile + "' !!"
     17837
     17838    return
     17839
     17840
     17841def mthDYNAMICO_toCF(values,ncfile):
     17842    """ Function to pass a mthDYNAMICO original file to CF-conventions
     17843      values= [CFtimeUnits]
     17844        [CFtimeUnits]: [tunits] since [YYYYMMDDHHMISS], CF time-units to which variable time should be used ('!' for spaces)
     17845      ncfile= file to transform
     17846    """
     17847    fname = 'mthDYNAMICO_toCF'
     17848
     17849    if ncfile == 'h':
     17850        print fname + '_____________________________________________________________'
     17851        print mthDYNAMICO_toCF.__doc__
     17852        quit()
     17853
     17854    CFT = values.replace('!',' ')
     17855
     17856    ofile = 'CF_mthDYNAMICO.nc'
     17857 
     17858    ncf = NetCDFFile(ncfile,'a')
     17859    ncdims = ncf.dimensions.keys()
     17860    ncvars = ncf.variables.keys()
     17861
     17862    # CFing variable 'pres'
     17863    if gen.searchInlist(ncvars,'pres'):
     17864        print warnmsg
     17865        print fname + ": renaming variable 'pres' as 'p' !!"
     17866        newname = ncf.renameVariable('pres','p')
     17867    ncf.sync()
     17868
     17869    # Re-aranging time-axis
     17870    tvaro = ncf.variables['time_centered']
     17871    tvals = tvaro[:]
     17872    tunits = tvaro.getncattr('units')
     17873
     17874    RefDate = CFT.split(' ')[2]
     17875    RefDateS = RefDate[0:4] + '-' + RefDate[4:6] + '-' + RefDate[6:8] + ' ' +        \
     17876      RefDate[8:10] + ':' + RefDate[10:12] + ':' + RefDate[12:14]
     17877    CFtimeUnits = ' '.join(CFT.split(' ')[0:2]) + ' '+ RefDateS
     17878
     17879    newtvals = gen.coincident_CFtimes(tvals, CFtimeUnits, tunits)
     17880    tvaro[:] = newtvals
     17881    newattr = set_attribute(tvaro, 'units', CFtimeUnits)
     17882    newattr = set_attribute(tvaro, 'time_origin', CFtimeUnits)
     17883
     17884    ncf.sync()
     17885
     17886    # CFing dimensions
     17887    CFdims = {'time_counter': 'time', 'presnivs': 'pres'}
     17888    for dimn in CFdims.keys():
     17889        if gen.searchInlist(ncdims,dimn):
     17890            # Making sure CF dimension is not in the file
     17891            if not gen.searchInlist(ncdims,CFdims[dimn]):
     17892                newname = ncf.renameDimension(dimn,CFdims[dimn])
     17893    ncf.sync()
     17894
     17895    ncvars = ncf.variables.keys()
     17896    # Re-arranging coordinates attribute for all the variables
     17897    for varn in ncvars:
     17898        varobj = ncf.variables[varn]
     17899        varattrs = varobj.ncattrs()
     17900        if gen.searchInlist(varattrs, unicode('coordinates')):
     17901            attrv = varobj.getncattr('coordinates')
     17902            newcoord = str(attrv)
     17903            for dimn in CFdims.keys():
     17904                newcoord = newcoord.replace(dimn, CFdims[dimn])
     17905            newattr = set_attribute(varobj, 'coordinates', newcoord)
     17906
     17907    ncf.sync()
     17908    ncf.close()
     17909
     17910    print fname + ": succesfull CFication of mthDYNAMICO file '" + ncfile + "' !!"
    1783717911
    1783817912    return
Note: See TracChangeset for help on using the changeset viewer.