Changeset 2439 in lmdz_wrf for trunk


Ignore:
Timestamp:
Apr 16, 2019, 4:32:34 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `impose_gregorian': Function to impose gregorian calendar to a series of times with a non-standard calendar
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r2433 r2439  
    127127#   a given character followig a given operation and a set of values
    128128# ijlonlat: Function to provide the imin,jmin imax,jmax of a lon,lat box
     129# impose_gregorian: Function to impose gregorian calendar to a series of times with a
     130#   non-standard calendar
    129131# incomming_flow: Function to determine if a fgrid-flow inflows to the central grid point
    130132# index_flatten_mat: Function to provide the matrix coordinates of an index from its flatten version
     
    1568215684    return newstring
    1568315685
     15686def impose_gregorian(timev, Tunits, cal):
     15687    """ Function to impose gregorian calendar to a series of times with a
     15688        non-standard calendar
     15689      timev: list of values
     15690      Tunits: CF units of time [Tunits] since [DateRef]
     15691      cal: non standard calendar
     15692      >>> impose_gregorian(np.range(800,10000,100), 'days since 1949-12-01 00:00:00',
     15693        'noleap')
     15694      [ 800. 901.]
     15695    """
     15696    import datetime as dt
     15697    fname = 'impose_gregorian'
     15698
     15699    availTu = ['weeks', 'days', 'hours', 'minutes', 'seconds']
     15700    availcalendar = ['noleap', '365d']
     15701
     15702    lTunits = Tunits.split(' ')
     15703    NTunits = len(lTunits)
     15704    Tu = lTunits[0]
     15705    if NTunits < 4:
     15706        print infmsg
     15707        print '  ' + fname + ": CF-time units without time, adding '00:00:00' !!"
     15708        Srefdate = lTunits[2] + ' 00:00:00'
     15709        refdate = dt.datetime.combine(dateStr_date(lTunits[2]),                      \
     15710          timeStr_time('00:00:00'))
     15711    else:
     15712        Srefdate = lTunits[2] + ' ' + lTunits[3]
     15713        refdate = dt.datetime.combine(dateStr_date(lTunits[2]),                      \
     15714          timeStr_time(lTunits[3]))
     15715
     15716    if type(timev) == type(range(2)):
     15717        dimt = len(timev)
     15718        timev = np.array(timev)
     15719    elif type(timev) == type(np.arange(2)):
     15720        dimt = timev.shape[0]
     15721    else:
     15722        print errormsg
     15723        print '  ' + fname + ": type of time values: '", type(timev), "' not ready !!"
     15724        print '    available ones:', type(range(2)), type(np.arange(2))
     15725
     15726    if Tu == 'weeks':
     15727        timevsecs = timev * 24. * 7 * 3600.
     15728    elif Tu == 'days':
     15729        timevsecs = timev * 24. * 3600.
     15730    elif Tu == 'hours':
     15731        timevsecs = timev * 3600.
     15732    elif Tu == 'minutes':
     15733        timevsecs = timev * 60.
     15734    elif Tu == 'seconds':
     15735        timevsecs = timev * 1.
     15736    else:
     15737        print errormsg
     15738        print '  ' + fname + ": CF time units '" + Tu + "' not ready !!"
     15739        print '    available ones:', availTu
     15740        quit(-1)
     15741
     15742    newtimes = np.zeros((dimt), dtype=np.float64)
     15743
     15744    yrref = refdate.year
     15745    if cal == 'noleap' or cal == '365d':
     15746        for it in range(dimt):
     15747            deltaT = dt.timedelta(seconds=timevsecs[it])
     15748            datetime = refdate + deltaT
     15749            yr = datetime.year
     15750            Nleapd = leapdays(yrref, yr)
     15751            if datetime.month <= 2 and days_month(yr, 2) > 28: Nleapd = Nleapd - 1
     15752            newtimes[it] = timevsecs[it] + Nleapd*24*3600.
     15753    else:
     15754        print errormsg
     15755        print '  ' + fname + ": calendar '" + cal + "' not ready !!"
     15756        print '    available ones:', availcalendar
     15757        quit(-1)
     15758       
     15759    # Re-transforming to the original units
     15760    if Tu == 'weeks':
     15761        newtimes = newtimes/(24.*7*3600.)
     15762    elif Tu == 'days':
     15763        newtimes = newtimes/(24.*3600.)
     15764    elif Tu == 'hours':
     15765        newtimes = newtimes/(24.*3600.)
     15766    elif Tu == 'minutes':
     15767        newtimes = newtimes/(60.)
     15768
     15769    return newtimes
     15770
    1568415771#quit()
    1568515772
Note: See TracChangeset for help on using the changeset viewer.