Changeset 2466 in lmdz_wrf


Ignore:
Timestamp:
Apr 24, 2019, 4:24:10 PM (7 years ago)
Author:
lfita
Message:

Adding:

  • `change_CFRefdate': Change CF-time values with a new reference date
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r2464 r2466  
    8686# chainSnum_levnext: Function to provide the next value for a given level from a chainStrnum hirerarchy of numbers
    8787# chainSnum_num: Function to pass a `ChainStrNum' string to a number
     88# change_CFRefdate: Change CF-time values with a new reference date
    8889# changedate360: Class to change a date  on a 360 days/yr (or 12 30-days months) calendar
    8990# check_timestep: Function to check if a time-units are 'timestep' based. If it's the case, transform them
     
    1623616237#  'days since 1949-12-01 00:00:00')
    1623716238
     16239def change_CFRefdate(timevals, origcftimeu, newSrefdate='1949-12-01 00:00:00'):
     16240    """ Change CF-time values with a new reference date
     16241          python datetime package does not work for dates < 1900
     16242      timevals: actual CF time-values
     16243      origcftimeu: current original CF-time units [tunits] since [YYYY]-[MM]-[HH]
     16244        [HH]:[MM]:[SS] (or similar)
     16245      newSrefdate: new CF refDate [YYYY]-[MM]-[HH] [HH]:[MM]:[SS]
     16246    >>> times = [15.5, 45, 74.5, 105, 135.5, 166, 196.5, 227.5, 258, 288.5, 319]
     16247    >>> change_CFRefdate(times, '1850-01-01 00:00:00', '1949-12-01 00:00:00')
     16248    [-36478.5 -36449.  -36419.5 -36389.  -36358.5 -36328.  -36297.5 -36266.5
     16249      -36236.  -36205.5 -36175. ]
     16250    """
     16251    fname = 'change_CFRefedate'
     16252
     16253    availtu = ['years', 'weeks', 'days', 'hours', 'minutes', 'seconds']
     16254
     16255    # original
     16256    origtv = origcftimeu.split(' ')
     16257    origtunits = origtv[0]
     16258    origSrefdate = origtv[2]
     16259
     16260    yrref = int(origSrefdate[0:4])
     16261    monref = int(origSrefdate[5:7])
     16262    dayref = int(origSrefdate[8:10])
     16263
     16264    if int(yrref) < 1970: origsing = -1.
     16265    else: origsing = 1.
     16266
     16267# Does original reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS]
     16268##
     16269    trefT = origcftimeu.find(':')
     16270    if not trefT == -1:
     16271        if len(origtv) == 3:
     16272            horref=int(origSrefdate[11:13])
     16273            minref=int(origSrefdate[14:16])
     16274            secref=int(origSrefdate[17:19])
     16275        else:
     16276            origSreftime = origtv[3]
     16277            horref=int(origSreftime[0:2])
     16278            minref=int(origSreftime[3:5])
     16279            secref=int(origSreftime[6:8])
     16280    else:
     16281        horref = 0
     16282        minref = 0
     16283        secref = 0
     16284
     16285    # Transforming to julian days (reference -4712 January 1st 00 UTC)
     16286    origdays = juliandate(yrref,monref,dayref,horref,minref,secref)
     16287
     16288    # new
     16289    yrnew = int(newSrefdate[0:4])
     16290    monnew = int(newSrefdate[5:7])
     16291    daynew = int(newSrefdate[8:10]) 
     16292
     16293    if int(yrnew) < 1970: newsing = -1.
     16294    else: newsing = 1.
     16295
     16296# Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS]
     16297##
     16298    tnewT = newSrefdate.find(':')
     16299    if not tnewT == -1:
     16300        hornew=int(newSrefdate[11:13])
     16301        minnew=int(newSrefdate[14:16])
     16302        secnew=int(newSrefdate[17:19])
     16303    else:
     16304        hornew = 0
     16305        minnew = 0
     16306        secnew = 0
     16307
     16308    # Transforming to julian days (reference -4712 January 1st 00 UTC)
     16309    newdays = juliandate(yrnew,monnew,daynew,hornew,minnew,secnew)
     16310
     16311    if origtunits == 'years':
     16312        daysdtr = 365.
     16313    elif origtunits == 'weeks':
     16314        daysdtr = 7.
     16315    elif origtunits == 'days':
     16316        daysdtr = 1.
     16317    elif origtunits == 'hours':
     16318        daysdtr = 1./24.
     16319    elif origtunits == 'minutes':
     16320        daysdtr = 1./(24.*60.)
     16321    elif origtunits == 'seconds':
     16322        daysdtr = 1./(24.*3600.)
     16323    else:
     16324        print errormsg
     16325        print '  ' + fname + ": original time-units '" + origtunits + "' not ready !!"
     16326        print '    available ones: ', availtu
     16327        quit(-1)
     16328
     16329    # Seconds difference respect new date
     16330    daysdiff = origdays - newdays
     16331
     16332    if type(timevals) != type(np.ones((2), dtype=int)):
     16333        newtimevals = np.array(timevals, dtype=np.float64)
     16334        newtimevals = newtimevals*daysdtr
     16335    else:
     16336        newtimevals = timevals*daysdtr
     16337
     16338    # Re-scaling to the absolute reference
     16339    newtimevals = newtimevals + origdays
     16340
     16341    # Re-scaling to the CF-time reference
     16342    newtimevals = newtimevals - newdays
     16343
     16344    return newtimevals
     16345
     16346times = [15.5, 45, 74.5, 105, 135.5, 166, 196.5, 227.5, 258, 288.5, 319]
     16347print change_CFRefdate(times, 'days since 1850-01-01 00:00:00', '1949-12-01 00:00:00')
     16348
    1623816349#quit()
    1623916350
Note: See TracChangeset for help on using the changeset viewer.