Changeset 1823 in lmdz_wrf for trunk/tools/nc_var_tools.py


Ignore:
Timestamp:
Mar 20, 2018, 1:07:45 AM (7 years ago)
Author:
lfita
Message:

Adding:

  • `compute_WRFtime_bnds': Function to copmute CFtime_bnd from WRFtime variable and a given period
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r1822 r1823  
    4848# compute_tevolboxtraj_radialsec: Function to compute tevolboxtraj_radialsec: temporal evolution at a given point along a number of radii at a given frequency following a trajectory
    4949# compute_WRFtime: function to copmute CFtimes from WRFtime variable
     50# compute_WRFtime_bnds: Function to copmute CFtime_bnd from WRFtime variable and a given period
    5051# curve_section: Function to provide a section of a file following a given curve
    5152# DataSetSection: Function to get a section (values along a dimension) of a given data-set
     
    2119121192    return
    2119221193
     21194
     21195def compute_WRFtime_bnds(timewrfv, period, kindWRFt='begperiod',                    \
     21196  refdate='19491201000000', tunitsval='minutes'):
     21197    """ Function to copmute CFtime_bnd from WRFtime variable and a given period
     21198      refdate= [YYYYMMDDMIHHSS] format of reference date
     21199      tunitsval= CF time units
     21200      timewrfv= matrix string values of WRF 'Times' variable
     21201      period= [tunit],[quantity] period to compute the bounds as:
     21202        [tunit]: unit of time: 'c' century, 'y' year, 'm' month, 'w' week, 'd' day,
     21203          'h' hour, 'i' minute, 's' second
     21204        [quantity] = amount (integer) of [tunit] to cover a period of time_bnds
     21205     kindWRFt: type of time from WRF
     21206       'begperiod': WRF times represent the beginning of the period
     21207       'centperiod': WRF times represent the center of the period
     21208       'endtperiod': WRF times represent the end of the period
     21209    """
     21210    import datetime as dt
     21211    fname = 'compute_WRFtime_bnds'
     21212
     21213    tunit = period.split(',')[0]
     21214    quantity = int(period.split(',')[1])
     21215
     21216    yrref=refdate[0:4]
     21217    monref=refdate[4:6]
     21218    dayref=refdate[6:8]
     21219    horref=refdate[8:10]
     21220    minref=refdate[10:12]
     21221    secref=refdate[12:14]
     21222
     21223    refdateS = yrref + '-' + monref + '-' + dayref + ' ' + horref + ':' + minref +   \
     21224      ':' + secref
     21225
     21226    dimt = timewrfv.shape[0]
     21227
     21228    # Times period
     21229    begTmat = gen.datetimeStr_conversion(timewrfv[0,:], 'WRFdatetime', 'matYmdHMS')
     21230    endTmat= gen.datetimeStr_conversion(timewrfv[dimt-1,:], 'WRFdatetime','matYmdHMS')
     21231    CFbeg = gen.realdatetime1_CFcompilant(begTmat, refdate, tunitsval)
     21232    CFend = gen.realdatetime1_CFcompilant(endTmat, refdate, tunitsval)
     21233    begT = dt.datetime(begTmat[0], begTmat[1], begTmat[2], begTmat[3], begTmat[4],   \
     21234      begTmat[5])
     21235    endT = dt.datetime(endTmat[0], endTmat[1], endTmat[2], endTmat[3], endTmat[4],   \
     21236      endTmat[5])
     21237    dFCt = CFend - CFbeg
     21238
     21239    # Units for the variable 'time' to seconds
     21240    availtunitvals = ['centuries', 'years', 'days', 'hours', 'minutes', 'seconds']
     21241    if tunitsval == 'centuries':
     21242        tsecsuv = 100 * 365 * 24 * 3600.
     21243        dFCt = dFCt*100*365*24*3600
     21244    elif tunitsval == 'years':
     21245        tsecsuv = 365 * 24 * 3600.
     21246        dFCt = dFCt*365*24*3600
     21247    elif tunitsval == 'weeks':
     21248        tsecsuv = 7 * 24 * 3600.
     21249        dFCt = dFCt*7*24*3600
     21250    elif tunitsval == 'days':
     21251        tsecsuv = 24 * 3600.
     21252        dFCt = dFCt*24*3600
     21253    elif tunitsval == 'hours':
     21254        tsecsuv = 3600.
     21255        dFCt = dFCt*3600
     21256    elif tunitsval == 'minutes':
     21257        tsecsuv = 60.
     21258        dFCt = dFCt*60
     21259    elif tunitsval == 'seconds':
     21260        tsecsuv = 1.
     21261    else:
     21262        print errormsg
     21263        print '  ' + fname + ":' tunitsvals= '" + tunitsval + "' not ready!!"
     21264        print '    available ones: ', availtunitvals
     21265        quit(-1)
     21266
     21267    # Quantity of time_bnds
     21268    availtunits = ['c', 'y', 'm', 'd', 'h', 'i', 's']
     21269    if tunit == 'c':
     21270        dtbnds = quantity * 100 * 365 * 24 * 3600.
     21271        begTBmat = [int(begTmat[0]/100)*100, 1, 1, 0, 0, 0]
     21272        endTBmat = [int(endTmat[0]/100)*100+100, 1, 1, 0, 0, 0]
     21273        dTBmat = [quantity*100, 0, 0, 0, 0, 0]
     21274    elif tunit == 'y':
     21275        dtbnds = quantity * 365 * 24 * 3600.
     21276        begTBmat = [begTmat[0], 1, 1, 0, 0, 0]
     21277        endTBmat = [endTmat[0]+1, 1, 1, 0, 0, 0]
     21278        dTBmat = [quantity, 0, 0, 0, 0, 0]
     21279    elif tunit == 'm':
     21280        iyr = begTmat[0]
     21281        imo = begTmat[1]
     21282        eyr = endTmat[0]
     21283        emo = endTmat[1]
     21284        begTBmat = [begTmat[0], begTmat[1], 1, 0, 0, 0]
     21285        endTBmat = [endTmat[0], endTmat[1]+1, 1, 0, 0, 0]
     21286        dTBmat = [0, quantity, 0, 0, 0, 0]
     21287    elif tunit == 'w':
     21288        dtbnds = quantity * 7 * 24 * 3600.
     21289        begTBmat = [begTmat[0], begTmat[1], begTmat[2], 0, 0, 0]
     21290        endTBmat = [endTmat[0], endTmat[1], endTmat[2]+7, 0, 0, 0]
     21291        dTBmat = [0, 0, quantity*7, 0, 0, 0]
     21292    elif tunit == 'd':
     21293        dtbnds = quantity * 24 * 3600.
     21294        begTBmat = [begTmat[0], begTmat[1], begTmat[2], 0, 0, 0]
     21295        endTBmat = [endTmat[0], endTmat[1], endTmat[2]+1, 0, 0, 0]
     21296        dTBmat = [0, 0, quantity, 0, 0, 0]
     21297    elif tunit == 'h':
     21298        dtbnds = quantity * 3600.
     21299        begTBmat = [begTmat[0], begTmat[1], begTmat[2], begTmat[3], 0, 0]
     21300        endTBmat = [endTmat[0], endTmat[1], endTmat[2], endTmat[3]+1, 0, 0]
     21301        dTBmat = [0, 0, 0, quantity, 0, 0]
     21302    elif tunit == 'i':
     21303        dtbnds = quantity * 60.
     21304        begTBmat = [begTmat[0], begTmat[1], begTmat[2], begTmat[3], begTmat[4], 0]
     21305        endTBmat = [endTmat[0], endTmat[1], endTmat[2], endTmat[3], endTmat[4]+1, 0]
     21306        dTBmat = [0, 0, 0, 0, quantity, 0]
     21307    elif tunit == 's':
     21308        dtbnds = quantity * 1.
     21309        begTBmat = [begTmat[0], begTmat[1], begTmat[2], begTmat[3], begTmat[4],      \
     21310          begTmat[5]]
     21311        endTBmat = [endTmat[0], endTmat[1], endTmat[2], endTmat[3], endTmat[4],      \
     21312          endTmat[5]+1]
     21313        dTBmat = [0, 0, 0, 0, 0, quantity]
     21314    else:
     21315        print errormsg
     21316        print '  ' + fname + ":' tunit= '" + tunit + "' not ready!!"
     21317        print '    available ones: ', availtunits
     21318        quit(-1)
     21319
     21320    Nbnds = int(dFCt / dtbnds)
     21321    if Nbnds < 2:
     21322        print errormsg
     21323        print '  ' + fname + ": from: ", begTmat, 'to', endTmat, 'found', Nbnds,     \
     21324          'time_bnds of', quantity,'*',tunit, '!!'
     21325        print '    revise parameters !!'
     21326        quit(-1)
     21327    print '  ' + fname + ' from: ', begTmat, 'to', endTmat, '(', dFCt, ' seconds )', \
     21328      'found', Nbnds, 'time_bnds of', quantity,'*',tunit, '(', dtbnds, 's)'
     21329
     21330    begTB = dt.datetime(begTBmat[0], begTBmat[1], begTBmat[2], begTBmat[3],          \
     21331      begTBmat[4], begTBmat[5])
     21332    endTB = dt.datetime(endTBmat[0], endTBmat[1], endTBmat[2], endTBmat[3],          \
     21333      endTBmat[4], endTBmat[5])
     21334
     21335    WRFtime_bnds = np.zeros((Nbnds,2), dtype=np.float)
     21336    WRFtime = np.zeros((Nbnds), dtype=np.float)
     21337    # For the time_bnds
     21338    if kindWRFt == 'begperiod':
     21339        for itb in range(Nbnds):
     21340            newTBi = begTB + dt.timedelta(seconds=dtbnds*itb)
     21341            newTBe = begTB + dt.timedelta(seconds=dtbnds*(itb+1))
     21342            newT = begTB + dt.timedelta(seconds=dtbnds*itb+dtbnds/2.)
     21343            newTBmati = [newTBi.year, newTBi.month, newTBi.day, newTBi.hour,         \
     21344              newTBi.minute, newTBi.second]
     21345            newTBmate = [newTBe.year, newTBe.month, newTBe.day, newTBe.hour,         \
     21346              newTBe.minute, newTBe.second]
     21347            newTmat = [newT.year, newT.month, newT.day, newT.hour, newT.minute,      \
     21348              newT.second]
     21349            WRFtime_bnds[itb,0] = gen.realdatetime1_CFcompilant(newTBmati, refdate,  \
     21350              tunitsval)
     21351            WRFtime_bnds[itb,1] = gen.realdatetime1_CFcompilant(newTBmate, refdate,  \
     21352              tunitsval)
     21353            WRFtime[itb] = gen.realdatetime1_CFcompilant(newTmat, refdate, tunitsval)
     21354           
     21355    tunits = tunitsval + ' since ' + refdateS
     21356
     21357    return WRFtime_bnds, WRFtime, tunits
     21358
     21359#onc = NetCDFFile('/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'r')
     21360#otime = onc.variables['Times']
     21361#wtimev = otime[:]
     21362#tbnds, tc, tu = compute_WRFtime_bnds(wtimev, 'i,1', kindWRFt='begperiod',            \
     21363#  refdate='19491201000000', tunitsval='minutes')
     21364
     21365#itb = gen.netCDFdatetime_realdatetime(tu, 'standard', tbnds[:,0])
     21366#etb = gen.netCDFdatetime_realdatetime(tu, 'standard', tbnds[:,1])
     21367#ctb = gen.netCDFdatetime_realdatetime(tu, 'standard', tc[:])
     21368
     21369#for it in range(len(tc)):
     21370#    print itb[it], ctb[it], etb[it]
     21371
     21372#onc.close()
    2119321373#quit()
Note: See TracChangeset for help on using the changeset viewer.