Changeset 942 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Jun 24, 2016, 11:12:47 AM (8 years ago)
Author:
lfita
Message:

Adding `CFmonthU_daysU': Function to transform from a CF date series with units as 'months since [DATE]' to 'days since [DATE]'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r911 r942  
    1919
    2020####### Content
     21# CFmonthU_daysU: Function to transform from a CF date series with units as 'months since [DATE]' to 'days since [DATE]'
    2122# CFvar_DIAGvar: Function to provide which model diagnostic values can provide a CF-variable from ASCII file
    2223# CFvar_MODvar: Function to provide which model values can provide a CF-variable from ASCII file
     
    79227923        return varv
    79237924
     7925def CFmonthU_daysU(datev,tunits):
     7926    """ Function to transform from a CF date series with units as 'months since [DATE]' to 'days since [DATE]'
     7927      datev= series of dates to transform
     7928      tunits= time units of the dates
     7929    >>> CFmonthU_daysU([1,2,3,4,5,6,17,18,], 'months since 1979-12-01 00:00:00')
     7930    array([  31.,   62.,   91.,  122.,  152.,  183.,  517.,  548.])
     7931    """
     7932    import datetime as dt
     7933    fname = 'CFmonthU_secondsU'
     7934
     7935    if tunits.find('month') == -1:
     7936        print errormsg
     7937        print '  ' + fname + ": times do not have 'months' as units !!"
     7938        print "    they have: '" + tunits + "'"
     7939        quit(-1)
     7940
     7941    secstunits = tunits.replace('_',' ').split(' ')
     7942    newtunits = 'seconds'
     7943    for itsec in range(1,len(secstunits)):
     7944        newtunits = newtunits + ' ' + secstunits[itsec]
     7945
     7946    yr = np.int(secstunits[2].split('-')[0])
     7947    mm = np.int(secstunits[2].split('-')[1])
     7948    dd = np.int(secstunits[2].split('-')[2])
     7949
     7950    if tunits.find(':') != -1:
     7951        hh = np.int(secstunits[3].split(':')[0])
     7952        mi = np.int(secstunits[3].split(':')[1])
     7953        ss = np.int(secstunits[3].split(':')[2])
     7954    else:
     7955        hh = 0
     7956        mi = 0
     7957        ss = 0
     7958
     7959    refT = dt.datetime(yr, mm, dd, hh, mi, ss)
     7960
     7961    refm = 0
     7962    newdatev = np.zeros((len(datev)), dtype=np.float)
     7963    for itv in range(len(datev)):
     7964        if mm + datev[itv] - refm > 12:
     7965            yr = yr + 1
     7966            refm = refm + 12
     7967
     7968        print itv, datev[itv], yr, refm, mm + datev[itv] - refm
     7969        idate = dt.datetime(yr, mm + datev[itv] - refm, dd, hh, mi, ss)
     7970       
     7971        diffT = idate - refT
     7972        if searchInlist(dir(dt.timedelta), 'total_seconds'):
     7973            newdatev[itv] = diffT.total_seconds()/(3600.*24.)
     7974        else:
     7975            newdatev[itv] = diffT.days() + diffT.seconds()/(3600.*24)
     7976
     7977    return newdatev, newtunits
     7978
     7979print CFmonthU_daysU([1,2,3,4,5,6,17,18,], 'months since 1979-12-01 00:00:00')
     7980
    79247981#quit()
    79257982
Note: See TracChangeset for help on using the changeset viewer.