Changeset 1092 in lmdz_wrf for trunk


Ignore:
Timestamp:
Sep 9, 2016, 6:16:44 PM (9 years ago)
Author:
lfita
Message:

Addin `time_reset': Function to re-set the time axis of a file

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var.py

    r1090 r1092  
    5252  'seasmean', 'sellonlatbox', 'sellonlatlevbox', 'selvar', 'setvar_asciivalues',     \
    5353  'sorttimesmat', 'spacemean', 'SpatialWeightedMean', 'statcompare_files',           \
    54   'subbasin', 'submns', 'subyrs', 'TimeInf',                                         \
     54  'subbasin', 'submns', 'subyrs', 'TimeInf', 'time_reset',                           \
    5555  'TimeSplitmat', 'timemean', 'valmod', 'valmod_dim','varaddattrk', 'varaddattr',    \
    5656  'varaddref',                                                                       \
     
    273273elif oper == 'TimeInf':
    274274    ncvar.TimeInf(opts.ncfile, opts.varname)
     275elif oper == 'time_reset':
     276    ncvar.time_reset(opts.values, opts.ncfile, opts.varname)
    275277elif oper == 'TimeSplitmat':
    276278    ncvar.TimeSplitmat(opts.values, opts.ncfile, opts.varname)
  • trunk/tools/nc_var_tools.py

    r1088 r1092  
    111111# time_information: Function to provide information about variable time
    112112# timemean: Function to retrieve a time mean series from a multidimensional variable of a file
     113# time_reset: Function to re-set the time axis of a file
    113114# timeshiftvar: Function to temporaly shift a number of time-steps a given variable inside a netCDF file
    114115# TimeSplitmat: Function to transform a file with CFtimes to a matrix [Nyear,Nmonth,Nday,Nhour,Nminute,Nsecond]
     
    1696816969    return
    1696916970
     16971def time_reset(values,ncfile,variable):
     16972    """ Function to re-set the time axis of a file
     16973      values= [kind],[value]
     16974        [kind]: kind of time re-set:
     16975          'setorigin': make time-steps start at a given date [value]=[YYYYMMDDHHMISS]
     16976      ncfile: netcdf file to re-set times
     16977      variable: name of the variable time within the file
     16978    """
     16979    import datetime as dt
     16980    fname = 'time_reset'
     16981
     16982    OpReady = ['setorigin']
     16983
     16984    if values == 'h':
     16985        print fname + '_____________________________________________________________'
     16986        print time_reset.__doc__
     16987        quit()
     16988
     16989    arguments = '[kind],[value]'
     16990    gen.check_arguments(fname, values, arguments, ',')
     16991   
     16992    kind = values.split(',')[0]
     16993    value = values.split(',')[1]
     16994
     16995    ncf = NetCDFFile(ncfile,'a')
     16996    if not ncf.variables.has_key(variable):
     16997        print errormsg
     16998        print '  ' + fname + ": file '" +ncfile+ "' does not have time variable: '"+ \
     16999          variable + "' !!"
     17000        quit(-1)
     17001
     17002    otvar = ncf.variables[variable]
     17003    tvals = otvar[:]
     17004    dimt = tvals.shape[0]
     17005    tunits = otvar.getncattr('units')
     17006    tattrs = otvar.ncattrs()
     17007
     17008    if kind == 'setorigin':
     17009        RefDate = value
     17010        RefDateS = RefDate[0:4] + '-' + RefDate[4:6] + '-' + RefDate[6:8] + ' ' +    \
     17011          RefDate[8:10] + ':' + RefDate[10:12] + ':' + RefDate[12:14]
     17012
     17013        dt = tvals[1] - tvals[0]
     17014        newtvals = np.arange(dimt)*dt
     17015        otvar[:] = newtvals
     17016
     17017        newtunits = ' '.join(tunits.split(' ')[0:2]) + ' ' + RefDateS
     17018        newattr = set_attribute(otvar, 'units', newtunits)
     17019        if gen.searchInlist(tattrs, 'time_origin'):
     17020            newattr = set_attribute(otvar, 'time_origin', newtunits)
     17021    else:
     17022        print errormsg
     17023        print '  ' + fname + ": kind '" + kind + "' not ready !!"
     17024        print '    Operations ready:', OpReady
     17025        quit(-1)
     17026
     17027    ncf.sync()
     17028    ncf.close()
     17029
     17030    return
     17031
     17032
    1697017033#quit()
Note: See TracChangeset for help on using the changeset viewer.