Changeset 1504 in lmdz_wrf


Ignore:
Timestamp:
Apr 10, 2017, 3:50:08 PM (8 years ago)
Author:
lfita
Message:

Adding:

`check_timestep': Function to check if a time-units are 'timestep' based. If it's the case, transform them

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1492 r1504  
    5555# chainSnum_num: Function to pass a `ChainStrNum' string to a number
    5656# changedate360: Class to change a date  on a 360 days/yr (or 12 30-days months) calendar
     57# check_timestep: Function to check if a time-units are 'timestep' based. If it's the case, transform them
    5758# coincident_CFtimes: Function to make coincident times for two different sets of CFtimes
    5859# coldec_hex: Function to pas a decimal ([r,g,b]; [0.,1.]) color to hexadecimal (#[RR][GG][BB], 00-64, 0A-FF)
     
    62756276    return Nvals
    62766277
     6278def check_timestep(otval):
     6279    """ Function to check if a time-units are 'timestep' based. If it's the case, transform them
     6280      otval = time variable object
     6281    """
     6282    fname = 'check_timestep'
     6283
     6284    varatrs = otval.ncattrs()
     6285
     6286    if not gen.searchInfile(varatrs, 'units'):
     6287        print errormsg
     6288        print '  ' + main+'.'+fname + ": object variable without 'units' attribute!!"
     6289        print '    attributes found:', varatrs
     6290        quit(-1)
     6291
     6292    tunits = varattrs.units
     6293    if tunits.split(' ')[0] == 'timesteps':
     6294        print warnmsg
     6295        print '  ' + fname + ": time variable with 'timesteps' as units !!"
     6296        print '    modifying time values'
     6297
     6298        dimvn = otval.dimensions[0]
     6299
     6300        # looking for units of time-step as 'clever' combination with dimension name?
     6301        Tunitssecs = ['second', 'minute', 'hour', 'day']
     6302        foundCFunits = False
     6303        Tunitsguess = []
     6304        for Tunit in Tunitssecs:
     6305            attrn = Tunit[0:1]
     6306            if gen.searchInlist(varatrs,dimvn+'_'+attrn):
     6307                CFtu = Tunit + 's'
     6308                foundCFunits = True
     6309                Tunitsguess.append(dimvn+'_'+attrn)
     6310                break
     6311            attrn = Tunit[0:4]
     6312            if gen.searchInlist(varatrs,dimvn+'_'+attrn):
     6313                CFtu = Tunit + 's'
     6314                foundCFunits = True
     6315                Tunitsguess.append(dimvn+'_'+attrn)
     6316                break
     6317            attrn = Tunit
     6318            if gen.searchInlist(varatrs,dimvn+'_'+attrn):
     6319                CFtu = Tunit + 's'
     6320                foundCFunits = True
     6321                Tunitsguess.append(dimvn+'_'+attrn)
     6322                break
     6323            attrn = Tunit + 's'
     6324            if gen.searchInlist(varatrs,dimvn+'_'+attrn):
     6325                CFtu = Tunit + 's'
     6326                foundCFunits = True
     6327                Tunitsguess.append(dimvn+'_'+attrn)
     6328                break
     6329
     6330        if not foundCFunits:
     6331            print errormsg
     6332            print '  ' + fname + ": time-variable with units as 'timesteps' does " + \
     6333              'not have a prepared way:' + Tunitsguess + 'to find its units !!'
     6334            print "    variables' attributes:", varatrs
     6335            quit(-1)
     6336
     6337        CFtv = otval.getncattr(dimvn+'_'+attrn)
     6338        newTS = [CFtu] + tunits[1:-1]
     6339        newTunits = ' '.join(newTS)
     6340        newTvals = otval[:]*CFtv
     6341
     6342    return newTunits, newTvals
     6343
     6344
    62776345def coincident_CFtimes(tvalB, tunitA, tunitB):
    62786346    """ Function to make coincident times for two different sets of CFtimes
Note: See TracChangeset for help on using the changeset viewer.