Changeset 2159 in lmdz_wrf for trunk/tools
- Timestamp:
- Oct 4, 2018, 3:24:44 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r2158 r2159 89 89 # i,j-quads within which the curve lays (-1, no value for the given quad [2x2 points around]) and provide 90 90 # 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) 91 94 # DateTimeStr_date: Function to transform a string date-time ([YYYY][MM][DD][HH][MI][SS] format) to a date object 92 95 # datetimeStr_datetime: Function to transform a string date-time ([YYYY]-[MM]-[DD]_[HH]:[MI]:[SS] format) to a date object … … 3001 3004 fname = 'CFtimes_datetime' 3002 3005 3006 availtunits = ['weeks', 'days', 'hours', 'minutes', 'seconds', 'miliseconds'] 3007 3003 3008 times = ncfile.variables[tname] 3004 3009 timevals = times[:] … … 3083 3088 else: 3084 3089 print errormsg 3085 print ' CFtimes_datetime: time units "' + tunits + '" not ready!!!!' 3090 print fname + ' : time units "' + tunits + '" not ready!!!!' 3091 print ' avaialable ones:', availtunits 3086 3092 quit(-1) 3087 3093 else: … … 3142 3148 else: 3143 3149 print errormsg 3144 print ' CFtimes_datetime: time units "' + tunits + '" not ready!!!!'3150 print ' ' + fname + ': time units "' + tunits + '" not ready!!!!' 3145 3151 quit(-1) 3146 3152 … … 3163 3169 import datetime as dt 3164 3170 fname = 'CFtimesvar_datetime' 3171 3172 availtunits = ['weeks', 'days', 'hours', 'minutes', 'seconds', 'miliseconds'] 3165 3173 3166 3174 txtunits = units.split(' ') … … 3232 3240 else: 3233 3241 print errormsg 3234 print ' CFtimes_datetime: time units "' + tunits + '" not ready!!!!' 3242 print ' ' + fname + ': time units "' + tunits + '" not ready!!!!' 3243 print ' available ones:', availtunits 3235 3244 quit(-1) 3236 3245 else: … … 3291 3300 else: 3292 3301 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 3294 3305 quit(-1) 3295 3306 … … 14027 14038 #print WRF_percenlevels(38) 14028 14039 14040 def 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 14029 14059 def time_slices(tv, tu, cal, per, amount): 14030 14060 """ Function to return temporal slices of a series of times for a given amount … … 14044 14074 fname = 'time_slices' 14045 14075 14076 availper = ['year', 'month', 'day', 'hour', 'minute'] 14077 14046 14078 mattimes = CFtimesvar_datetime(tv, tu, cal) 14047 print ' CFtimes _______'14079 print 'mat times _______' 14048 14080 print mattimes 14049 14081 … … 14061 14093 if per == 'year': 14062 14094 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 14063 14151 # Getting unique values 14064 14152 years = list(mattimes[:,imat]) 14065 14153 setvar = set(years) 14066 14154 utimes = list(setvar) 14155 utimes.sort() 14067 14156 Nutimes = len(utimes) 14068 14157 14069 14158 # introducing amounts 14070 print utimes, ':', Nutimes14071 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) 14072 14161 Ntimes = len(atimes) 14162 print ' amount ', Ntimes, 'periods:', atimes 14073 14163 14074 14164 itt = 0 … … 14083 14173 timeslice[1] = it 14084 14174 else: 14175 timeslice[1] = it 14085 14176 slices.append(timeslice) 14086 14177 itt = it + 0 14087 14178 break 14179 else: 14180 print errormsg 14181 print ' ' + fname + ": period '" + per + "' not ready !!" 14182 print ' available ones:', availper 14183 quit(-1) 14088 14184 14089 14185 Nslices = len(slices) 14090 14186 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 14094 14190 14095 14191 tv = [] 14192 values = [] 14193 totT = 365. 14194 dT = 100. 14096 14195 for 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 14200 vals = np.array(values) 14201 itdim = 0 14101 14202 tu = 'days since 1949-12-01 00:00:00' 14102 14203 per = 'year' … … 14104 14205 amount = 1 14105 14206 14106 time_slices(tv, tu, calend, per, amount) 14107 14207 tslc, Ntslc = time_slices(tv, tu, calend, per, amount) 14208 14209 statn = 'mean' 14210 tstat = np.ones((Ntslc), dtype=np.float) 14211 for 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 14108 14232 #quit() 14109 14233
Note: See TracChangeset
for help on using the changeset viewer.