Changeset 2159 in lmdz_wrf for trunk/tools


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

Adding:

  • `# cyclevar: Function to provide the given index of a cycle variable. A cycle variable is a given structure with a series of values, once the maximum is overpassed it restarts from the beginning (e.g.: 12 months of a year)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r2158 r2159  
    8989#   i,j-quads within which the curve lays (-1, no value for the given quad [2x2 points around]) and provide
    9090#   the weights of the quad to perform a distance-weighted mean at the given curve point
     91# cyclevar: Function to provide the given index of a cycle variable. A cycle variable
     92#   is a given structure with a series of values, once the maximum is overpassed
     93#   it restarts from the beginning (e.g.: 12 months of a year)
    9194# DateTimeStr_date: Function to transform a string date-time ([YYYY][MM][DD][HH][MI][SS] format) to a date object
    9295# datetimeStr_datetime: Function to transform a string date-time ([YYYY]-[MM]-[DD]_[HH]:[MI]:[SS] format) to a date object
     
    30013004    fname = 'CFtimes_datetime'
    30023005
     3006    availtunits = ['weeks', 'days', 'hours', 'minutes', 'seconds', 'miliseconds']
     3007
    30033008    times = ncfile.variables[tname]
    30043009    timevals = times[:]
     
    30833088        else:
    30843089              print errormsg
    3085               print '    CFtimes_datetime: time units "' + tunits + '" not ready!!!!'
     3090              print fname + '    : time units "' + tunits + '" not ready!!!!'
     3091              print '    avaialable ones:', availtunits
    30863092              quit(-1)
    30873093    else:
     
    31423148        else:
    31433149              print errormsg
    3144               print '    CFtimes_datetime: time units "' + tunits + '" not ready!!!!'
     3150              print '    ' + fname + ': time units "' + tunits + '" not ready!!!!'
    31453151              quit(-1)
    31463152   
     
    31633169    import datetime as dt
    31643170    fname = 'CFtimesvar_datetime'
     3171
     3172    availtunits = ['weeks', 'days', 'hours', 'minutes', 'seconds', 'miliseconds']
    31653173
    31663174    txtunits = units.split(' ')
     
    32323240        else:
    32333241              print errormsg
    3234               print '    CFtimes_datetime: time units "' + tunits + '" not ready!!!!'
     3242              print '    ' + fname + ': time units "' + tunits + '" not ready!!!!'
     3243              print '    available ones:', availtunits
    32353244              quit(-1)
    32363245    else:
     
    32913300        else:
    32923301              print errormsg
    3293               print '    CFtimes_datetime: time units "' + tunits + '" not ready!!!!'
     3302              print '    ' + fname + ': time units "' + tunits + '" not ready!!!!'
     3303              print '    available ones:', availtunits
     3304
    32943305              quit(-1)
    32953306   
     
    1402714038#print WRF_percenlevels(38)
    1402814039
     14040def cyclevar(cycvar,ind):
     14041    """ Function to provide the given index of a cycle variable. A cycle variable
     14042        is a given structure with a series of values, once the maximum is overpassed
     14043        it restarts from the beginning (e.g.: 12 months of a year)
     14044      cycvar: array of values
     14045      ind: index to provide the value from
     14046    >>> cyclevar(np.arange(12)+1, 3)
     14047    4
     14048    >>> cyclevar(np.arange(12)+1, 14)
     14049    3
     14050    """
     14051    fname = 'cyclevar'
     14052
     14053    dcyc = cycvar.shape[0]
     14054    Ncyc = int(ind / dcyc)
     14055    rescyc = ind - Ncyc*dcyc
     14056
     14057    return cycvar[rescyc]
     14058
    1402914059def time_slices(tv, tu, cal, per, amount):
    1403014060    """ Function to return temporal slices of a series of times for a given amount
     
    1404414074    fname = 'time_slices'
    1404514075
     14076    availper = ['year', 'month', 'day', 'hour', 'minute']
     14077
    1404614078    mattimes = CFtimesvar_datetime(tv, tu, cal)
    14047     print 'CF times _______'
     14079    print 'mat times _______'
    1404814080    print mattimes
    1404914081
     
    1406114093    if per == 'year':
    1406214094        imat = 0
     14095        # Getting unique values, since there is no cycle year periods
     14096        timatvals = list(mattimes[:,imat])
     14097        setvar = set(timatvals)
     14098        cyctimes = list(setvar)
     14099        cyctimes.sort()
     14100        Ncyctimes = len(cyctimes)
     14101        cyctimes.append(cyctimes[Ncyctimes-1]+1)
     14102        Ncyctimes = Ncyctimes + 1
     14103
     14104        print 'cyctimes:', cyctimes
     14105
     14106        iit = 0
     14107        itt = 0
     14108        ip = cyctimes[itt]
     14109        ep = cyctimes[itt+amount]
     14110        for it in range(dimt):
     14111            # timeslice: [ini_slice, end_slice, freq_slice]
     14112            timeslice = [iit,iit,1]
     14113            ttv = mattimes[it,imat]
     14114            timeslice[1] = it
     14115            print it, ttv, 'ip:', ip, 'ep:', ep, '<>', (ttv >= ip and ttv < ep)
     14116            if not (ttv >= ip and ttv < ep):
     14117                slices.append(timeslice)
     14118                iit = it + 0
     14119                itt = itt + amount
     14120                ip = cyctimes[itt]
     14121                ep = cyctimes[np.min([Ncyctimes-1,itt+amount])]
     14122
     14123    elif per == 'month':
     14124        imat = 1
     14125
     14126        # cycle for months
     14127        Ncyctimes = 12
     14128        cyctimes = np.arange(Ncyctimes)+1
     14129       
     14130        print 'cyctimes:', cyctimes
     14131
     14132        iit = 0
     14133        itt = 0
     14134        ip = cyctimes[itt]
     14135        ep = cyctimes[itt+amount]
     14136        for it in range(dimt):
     14137            # timeslice: [ini_slice, end_slice, freq_slice]
     14138            timeslice = [iit,iit,1]
     14139            ttv = mattimes[it,imat]
     14140            timeslice[1] = it
     14141            print it, ttv, 'ip:', ip, 'ep:', ep, '<>', (ttv >= ip and ttv < ep)
     14142            if not (ttv >= ip and ttv < ep):
     14143                slices.append(timeslice)
     14144                iit = it + 0
     14145                itt = itt + amount
     14146                ip = cyclevar(cyctimes,itt)
     14147                ep = cyclevar(cyctimes,itt+amount)
     14148
     14149    elif per == 'day':
     14150        imat = 2
    1406314151        # Getting unique values
    1406414152        years = list(mattimes[:,imat])
    1406514153        setvar = set(years)
    1406614154        utimes = list(setvar)
     14155        utimes.sort()
    1406714156        Nutimes = len(utimes)
    1406814157
    1406914158        # introducing amounts
    14070         print utimes, ':', Nutimes
    14071         atimes = np.arange(utimes[0], utimes[Nutimes-1], amount)
     14159        print '  unique ', Nutimes, 'times:', utimes, '<>', utimes[0], utimes[Nutimes-1]+amount, amount
     14160        atimes = np.arange(utimes[0], utimes[Nutimes-1]+amount, amount)
    1407214161        Ntimes = len(atimes)
     14162        print '  amount ', Ntimes, 'periods:', atimes
    1407314163       
    1407414164        itt = 0
     
    1408314173                    timeslice[1] = it
    1408414174                else:
     14175                    timeslice[1] = it
    1408514176                    slices.append(timeslice)
    1408614177                    itt = it + 0
    1408714178                    break
     14179    else:
     14180        print errormsg
     14181        print '  ' + fname + ": period '" + per + "' not ready !!"
     14182        print '    available ones:', availper
     14183        quit(-1)
    1408814184
    1408914185    Nslices = len(slices)
    1409014186    for isl in range(Nslices):
    14091         print atimes[isl], atimes[isl+1], ':', slices[isl]
    14092 
    14093     return slices
     14187        print isl, cyctimes[isl], cyctimes[isl+amount+1], ':', slices[isl]
     14188
     14189    return slices, Nslices
    1409414190
    1409514191tv = []
     14192values = []
     14193totT = 365.
     14194dT = 100.
    1409614195for it in range(0,10):
    14097     tv.append(it*365)
    14098     tv.append(it*365+180.)
    14099     tv.append(it*365+360.)
    14100 
     14196    for itt in range(3):
     14197        tv.append(it*totT+dT*itt)
     14198        values.append(it*1.)
     14199
     14200vals = np.array(values)
     14201itdim = 0
    1410114202tu = 'days since 1949-12-01 00:00:00'
    1410214203per = 'year'
     
    1410414205amount = 1
    1410514206
    14106 time_slices(tv, tu, calend, per, amount)
    14107 
     14207tslc, Ntslc = time_slices(tv, tu, calend, per, amount)
     14208
     14209statn = 'mean'
     14210tstat = np.ones((Ntslc), dtype=np.float)
     14211for islc in range(Ntslc):
     14212    timeslcv = tslc[islc]
     14213    timeslc = [slice(timeslcv[0],timeslcv[1],timeslcv[2])]
     14214    tvals = vals[tuple(timeslc)]
     14215    if statn == 'min':
     14216        tstat[islc] = np.min(tvals, axis=itdim)
     14217    elif statn == 'max':
     14218        tstat[islc] = np.max(tvals, axis=itdim)
     14219    elif statn == 'mean':
     14220        tstat[islc] = np.mean(tvals, axis=itdim)
     14221    elif statn == 'mean2':
     14222        tstat[islc] = np.mean(tvals**2, axis=itdim)
     14223    elif statn == 'std':
     14224        tstat[islc] = np.std(tvals, axis=itdim)
     14225
     14226    timeslc = tslc[islc]
     14227    itime = tv[timeslc[0]]
     14228    etime = tv[timeslc[1]]
     14229    print islc, ':', itime, '-', etime, '<>', tvals, '=', tstat[islc]
     14230
     14231   
    1410814232#quit()
    1410914233
Note: See TracChangeset for help on using the changeset viewer.