Changeset 1889 in lmdz_wrf


Ignore:
Timestamp:
Apr 4, 2018, 5:20:47 PM (7 years ago)
Author:
lfita
Message:

Adding:

  • `itime': Function to provide information of the time from a netCDF file
Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var.py

    r1884 r1889  
    2323## e.g. # nc_var.py -o ifile -f ../PY/wrfout_d01_1995-01-01_00:00:00
    2424## e.g. # nc_var.py -o splitfile_dim -S bottom_top:ZNU:QVAPOR_ZNU:.4f -v QVAPOR,XLONG,XLAT,Times -f ~/PY/wrfout_d01_1995-01-01_00:00:00
     25## e.g. # nc_var.py -o itime -S WRFtime -f ../PY/wrfout_d01_1995-01-01_00:00:00 -v Times
    2526
    2627## e.g. ccrc468-17 # ./nc_var.py -v time -f 123/CCRC_NARCliM_Sydney_All_1990-1999_pr10max.nc -o out -S 1:-1
     
    119120# isgattrs: Give a single global attribute of a file and its type
    120121# isvattrs: Give a single attribute of a variable
     122# itime: Function to provide information of the time from a netCDF file
    121123# ivars: Give all the variable names of a file
    122124# ivattrs: Give all the attributes of a variable and its type
     
    197199  'getvalues_lonlat', 'getvars_tofile', 'grattr',                                    \
    198200  'grmattr', 'idims', 'ifile', 'igattrs', 'increaseDimvar', 'isgattrs',              \
    199   'isvattrs', 'ivars', 'ivattrs', 'LMDZ_toCF', 'lonlat_polygon', 'maskvar',          \
     201  'isvattrs', 'itime', 'ivars', 'ivattrs', 'LMDZ_toCF', 'lonlat_polygon', 'maskvar', \
    200202  'merge_files', 'model_characteristics',                                            \
    201203  'mthDYNAMICO_toCF', 'ncreplace', 'ncstepdiff', 'netcdf_concatenation',             \
     
    394396elif oper == 'isvattrs':
    395397    ncvar.isvattrs(opts.values, opts.ncfile, opts.varname)
     398elif oper == 'itime':
     399    ncvar.itime(opts.values, opts.ncfile, opts.varname)
    396400elif oper == 'ivars':
    397401    ncvar.ivars(opts.ncfile)
  • trunk/tools/nc_var_tools.py

    r1888 r1889  
    9696# isgattrs: Give a single global attribute of a file and its type
    9797# isvattrs: Give a single attribute of a variable
     98# itime: Function to provide information of the time from a netCDF file
    9899# ivars: Give all the variable names of a file
    99100# ivattrs: Give all the attributes of a variable and its type
     
    2421524216
    2421624217    return
     24218
     24219def itime(values, ncfile, variable):
     24220    """ Function to provide information of the time from a netCDF file
     24221      values=[tkind]
     24222        [tkind]: kind of variable time
     24223          'WRFtime': for WRF derived time values (String variable)
     24224          'CFtime': time following CF-conventions (as [tunits] since [refdate])
     24225          'string',[tfmt]: time values as a given string format [tfmt] (following python specification)
     24226      ncfile= file from which provide the information
     24227      variable= name of the variable time in the file
     24228    """
     24229    import datetime as dt
     24230    fname = 'itime'
     24231
     24232    availtkind = ['WRFtime', 'CFtime', 'string,[tfmt]']
     24233
     24234    if values == 'h':
     24235        print fname + '_____________________________________________________________'
     24236        print itime.__doc__
     24237        quit()
     24238
     24239    expectargs = '[tkind]'
     24240    gen.check_arguments(fname, values, expectargs, ':')
     24241
     24242    tkind = values.split(':')[0]
     24243
     24244    onc = NetCDFFile(ncfile, 'r')
     24245
     24246    if not onc.variables.has_key(variable):
     24247        ncvars = list(onc.variables.keys())
     24248        ncvars.sort()
     24249        print errormsg
     24250        print '  ' + fname + ": file '" + ncfile + "' does not have variable '" +    \
     24251          variable + "' !!"
     24252        print '    available ones:', ncvars
     24253        onc.close()
     24254        quit(-1)
     24255
     24256    otvar = onc.variables[variable]
     24257    itvals = otvar[:]
     24258    if tkind[0:7] == 'WRFtime':
     24259        tvals, utime = compute_WRFtime(itvals)
     24260        mattvals = np.zeros((itvals.shape[0],6), dtype=int)
     24261        for it in range(len(tvals)):
     24262            mattvals[it,:]= gen.datetimeStr_conversion(str(tvals[it]),               \
     24263              'cfTime,minutes since 1949-12-01 00:00:00', 'matYmdHMS')
     24264    elif tkind[0:6] == 'CFtime':
     24265        tvals = itvals.copy()
     24266        utime = otvar.getncattr('units')
     24267        mattvals = np.zeros((itvals.shape[0],6), dtype=int)
     24268        for it in range(len(tvals)):
     24269            mattvals[it,:]= gen.datetimeStr_conversion(str(tvals[it]),               \
     24270              'cfTime,'+utime, 'matYmdHMS')
     24271    elif tkind[0:6] == 'string':
     24272        tfmt = tkind.split(',')[1]
     24273        tvals = []
     24274        mattvals = np.zeros((itvals.shape[0],6), dtype=int)
     24275        for it in range(itvals.shape[0]):
     24276            mattvals[it,:] = dt.datetime.string(''.join(itvals[it,:]),tfmt)
     24277            tvals[it] = gen.datetimeStr_conversion(mattvals[it,:].strftime("%Y%m%d"+ \
     24278              "%H%M%S"), 'YmdHMS', 'cfTime,minutes since 1949-12-01 00:00:00')
     24279    else:
     24280        print errormsg
     24281        print '  ' + fname + ": time kind '" + tkind + "' not ready !!"
     24282        print '    available ones:', availtkind
     24283        quit(-1)
     24284
     24285    # Getting information
     24286    dimt = mattvals.shape[0]
     24287    print 'First_date:', mattvals[0,:]
     24288    print 'Last_date:', mattvals[dimt-1,:]
     24289    print 'dimt:', dimt
     24290    date1 = dt.datetime(mattvals[0,0], mattvals[0,1], mattvals[0,2], mattvals[0,3],  \
     24291      mattvals[0,4], mattvals[0,5])
     24292    date2 = dt.datetime(mattvals[1,0], mattvals[1,1], mattvals[1,2], mattvals[1,3],  \
     24293      mattvals[1,4], mattvals[1,5])
     24294    diffdate = date2 - date1
     24295    if gen.searchInlist(dir(diffdate),'total_seconds'):
     24296        totsecs = diffdate.total_seconds()
     24297    else:
     24298        totsecs = diffdate.days()*24*3600. + diffdate.seconds()
     24299
     24300    print 'diffdate_2nd-1st:', diffdate
     24301    print 'diff_seconds_2nd-1st:', totsecs
     24302
     24303    for it in range(1,dimt-1):
     24304        it1 = it + 1
     24305        date1 = dt.datetime(mattvals[it,0], mattvals[it,1], mattvals[it,2],          \
     24306          mattvals[it,3], mattvals[it,4], mattvals[it,5])
     24307        date2 = dt.datetime(mattvals[it1,0], mattvals[it1,1], mattvals[it1,2],       \
     24308          mattvals[it1,3], mattvals[it1,4], mattvals[it1,5])
     24309        diffdate = date2 - date1
     24310        if gen.searchInlist(dir(diffdate),'total_seconds'):
     24311            newtotsecs = diffdate.total_seconds()
     24312        else:
     24313            newtotsecs = diffdate.days()*24*3600. + diffdate.seconds()
     24314        if newtotsecs != totsecs:
     24315            print '  different_diff_seconds_between_it_' + str(it) + '_and_' +       \
     24316              str(it1) + ':', newtotsecs
     24317
     24318    # attributes
     24319    for attrn in otvar.ncattrs():
     24320        attrv = otvar.getncattr(attrn)
     24321        print 'Tattr_@' + attrn + '@:', attrv.replace(' ','!')
     24322
     24323    onc.close()
     24324
     24325    return
     24326
     24327#itime('WRFtime', '../PY/wrfout_d01_1995-01-01_00:00:00', 'Times')
     24328
    2421724329#quit()
Note: See TracChangeset for help on using the changeset viewer.