Changeset 802 in lmdz_wrf


Ignore:
Timestamp:
Jun 2, 2016, 12:42:21 PM (9 years ago)
Author:
lfita
Message:

Adding `secondsDate': Function to know how many units of time are from a given pair of dates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r795 r802  
    2222# PolyArea: Function to compute the area of the polygon following 'Shoelace formula'
    2323# significant_decomposition: Function to decompose a given number by its signifcant potencies
     24# secondsDate: Function to know how many units of time are from a given pair of dates
    2425
    2526def values_line(line, splitv, chars):
     
    63576358
    63586359    return iipot, potencies, potval
     6360
     6361def secondsDate(refdate, date, units):
     6362    """ Function to know how many units of time are from a given pair of dates
     6363      refdate= date of reference (as [YYYY][MM][DD][HH][MI][SS])
     6364      date= date to look for (as [YYYY][MM][DD][HH][MI][SS])
     6365      units= units of time ('century', 'year', 'month', 'day', 'week', 'hour', 'minute', 'second')
     6366    >>> secondsDate('19490101000000', '19760217082932', 'second')
     6367    856081772.0
     6368    >>> secondsDate('19490101000000', '19760217082932', 'minute')
     6369    237800.492222
     6370    """
     6371    import datetime as dt
     6372    fname = 'SecondsDate'
     6373
     6374    yrref=int(refdate[0:4])
     6375    monref=int(refdate[4:6])
     6376    dayref=int(refdate[6:8])
     6377    horref=int(refdate[8:10])
     6378    minref=int(refdate[10:12])
     6379    secref=int(refdate[12:14])
     6380
     6381    yrd=int(date[0:4])
     6382    mond=int(date[4:6])
     6383    dayd=int(date[6:8])
     6384    hord=int(date[8:10])
     6385    mind=int(date[10:12])
     6386    secd=int(date[12:14])
     6387
     6388    refdateT = dt.datetime(yrref, monref, dayref, horref, minref, secref)
     6389    ddateT = dt.datetime(yrd, mond, dayd, hord, mind, secd)
     6390
     6391    diffT = ddateT - refdateT
     6392
     6393    if searchInlist(dir(diffT), 'total_seconds'):
     6394        totsecs = diffT.total_seconds()
     6395    else:
     6396        totsecs = diffT.days*24*3600. + diffT.seconds
     6397
     6398    if units == 'century':
     6399        times = totsecs / (100.*365.*24.*3600.)
     6400    elif units == 'year':
     6401        times = totsecs / (365.*24.*3600.)
     6402    elif units == 'month':
     6403        print errormsg
     6404        print '  ' + fname + "' units '" + units + "' not ready!!"
     6405        quit(-1)
     6406    elif units == 'week':
     6407        times = totsecs / (7.*24.*3600.)
     6408    elif units == 'day':
     6409        times = totsecs / (24.*3600.)
     6410    elif units == 'hour':
     6411        times = totsecs / (3600.)
     6412    elif units == 'minute':
     6413        times = totsecs / (3600.)
     6414    elif units == 'second':
     6415        times = totsecs / (1.)
     6416
     6417    return times
     6418
     6419print secondsDate('19490101000000', '19760217082932', 'second')
     6420print secondsDate('19490101000000', '19760217082932', 'minute')
     6421
     6422quit()
Note: See TracChangeset for help on using the changeset viewer.