Changeset 2164 in lmdz_wrf for trunk/tools/generic_tools.py


Ignore:
Timestamp:
Oct 4, 2018, 7:24:00 PM (6 years ago)
Author:
lfita
Message:

Workin version of:

  • `time_slices': Function to return temporal slices of a series of times for a given amount of periods
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r2163 r2164  
    175175# stringList_dictKeysVals: Function to provide a dictionary with keys which contain values of lists from a string
    176176# subbasin_point: Function to provide sub-basins given a grid point following a matrix of trips
     177# time_slices: Function to return temporal slices of a series of times for a given amount of periods
    177178# timestep_conform: Function to provide the time-step in seconds which conforms 1 temporal unit and the resultant number
    178179#   of time-steps for a whole period is multiple of a given number
     
    1420814209        stmatdates.append(imdate)
    1420914210
     14211    elif per == 'day':
     14212        # Arranging accordingly beginning/ending statistics periods
     14213        istats[3:5] = 0
     14214        dmon = days_month(estats[0], estats[1])
     14215        estats[2] = estats[2]+1
     14216        if estats[2] > dmon:
     14217            estats[1] = estats[1] + 1
     14218            if estats[1] > 12:
     14219                estats[0] = estats[0] + 1
     14220                estats[1] = 1
     14221        estats[3:5] = 0
     14222
     14223        istdate = dt.datetime(istats[0], istats[1], istats[2], istats[3], istats[4], \
     14224          istats[5])
     14225        estdate = dt.datetime(estats[0], estats[1], estats[2], estats[3], estats[4], \
     14226          estats[5])
     14227
     14228        # Getting statistics periods
     14229        stmatdates = [istats]
     14230        idate = istdate
     14231        imdate = istats.copy()
     14232        while idate < estdate:
     14233            imdate = imdate.copy()
     14234            dmon = days_month(imdate[0], imdate[1])
     14235            imdate[2] = imdate[2] + amount
     14236            if imdate[2] > dmon:
     14237                imdate[2] = imdate[2] - dmon
     14238                imdate[1] = imdate[1] + 1
     14239                if imdate[1] > 12:
     14240                    imdate[0] = imdate[0] + 1
     14241                    imdate[1] = 1
     14242            idate= dt.datetime(imdate[0], imdate[1], imdate[2], imdate[3], imdate[4],\
     14243              imdate[5])
     14244            if idate < estdate: stmatdates.append(imdate)
     14245            else: break
     14246        stmatdates.append(imdate)
     14247
    1421014248    else:
    1421114249        print errormsg
     
    1425714295        timeslice[1] = it
    1425814296        icfstup = np.min([icfst+1, Nslices-1])
    14259         print cfstdates[icfst], '<=', tv[it], '<', cfstdates[icfstup]
    1426014297        if not(tv[it] >= cfstdates[icfst] and tv[it] < cfstdates[icfstup]):
    1426114298            slices.append(timeslice)
     
    1427014307    return slices, Nslices
    1427114308
    14272 tv = []
    14273 values = []
    14274 totT = 31.
    14275 dT = 7.
    14276 for it in range(0,10):
    14277    for itt in range(3):
    14278         tv.append(it*totT+dT*itt)
    14279         values.append(it*1.)
    14280 
    14281 vals = np.array(values)
    14282 itdim = 0
    14283 tu = 'days since 1949-12-01 00:00:00'
    14284 per = 'month'
    14285 calend = 'standard'
    14286 amount = 1
    14287 
    14288 tslc, Ntslc = time_slices(tv, tu, calend, per, amount)
    14289 
    14290 statn = 'mean'
    14291 tstat = np.ones((Ntslc), dtype=np.float)
    14292 for islc in range(Ntslc):
    14293     timeslcv = tslc[islc]
    14294     timeslc = [slice(timeslcv[0],timeslcv[1],timeslcv[2])]
    14295     tvals = vals[tuple(timeslc)]
    14296     if statn == 'min':
    14297         tstat[islc] = np.min(tvals, axis=itdim)
    14298     elif statn == 'max':
    14299         tstat[islc] = np.max(tvals, axis=itdim)
    14300     elif statn == 'mean':
    14301         tstat[islc] = np.mean(tvals, axis=itdim)
    14302     elif statn == 'mean2':
    14303         tstat[islc] = np.mean(tvals**2, axis=itdim)
    14304     elif statn == 'std':
    14305         tstat[islc] = np.std(tvals, axis=itdim)
    14306 
    14307     timeslc = tslc[islc]
    14308     itime = tv[timeslc[0]]
    14309     etime = tv[timeslc[1]]
    14310     print islc, ':', itime, '-', etime, '<>', tvals, '=', tstat[islc]
    14311 
     14309#tv = []
     14310#values = []
     14311#totT = 24.*60.
     14312#dT = 7.
     14313#for it in range(0,10):
     14314#   for itt in range(3):
     14315#        tv.append(29*24*60.+it*totT+dT*itt)
     14316#        values.append(it*1.)
     14317
     14318#vals = np.array(values)
     14319#itdim = 0
     14320#tu = 'minutes since 1949-12-01 00:00:00'
     14321#per = 'day'
     14322#calend = 'standard'
     14323#amount = 1
     14324
     14325#tslc, Ntslc = time_slices(tv, tu, calend, per, amount)
     14326
     14327#statn = 'mean'
     14328#tstat = np.ones((Ntslc), dtype=np.float)
     14329#for islc in range(Ntslc):
     14330#    timeslcv = tslc[islc]
     14331#    timeslc = [slice(timeslcv[0],timeslcv[1],timeslcv[2])]
     14332#    tvals = vals[tuple(timeslc)]
     14333#    if statn == 'min':
     14334#        tstat[islc] = np.min(tvals, axis=itdim)
     14335#    elif statn == 'max':
     14336#        tstat[islc] = np.max(tvals, axis=itdim)
     14337#    elif statn == 'mean':
     14338#        tstat[islc] = np.mean(tvals, axis=itdim)
     14339#    elif statn == 'mean2':
     14340#        tstat[islc] = np.mean(tvals**2, axis=itdim)
     14341#    elif statn == 'std':
     14342#        tstat[islc] = np.std(tvals, axis=itdim)
     14343
     14344#    timeslc = tslc[islc]
     14345#    itime = tv[timeslc[0]]
     14346#    etime = tv[timeslc[1]]
     14347#    print islc, ':', itime, '-', etime, '<>', tvals, '=', tstat[islc]
    1431214348   
    1431314349#quit()
Note: See TracChangeset for help on using the changeset viewer.