Changeset 2226 in lmdz_wrf
- Timestamp:
- Nov 12, 2018, 4:41:41 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r2218 r2226 187 187 # stringList_dictKeysVals: Function to provide a dictionary with keys which contain values of lists from a string 188 188 # subbasin_point: Function to provide sub-basins given a grid point following a matrix of trips 189 # temporal_desc: Function to provide the description of a series of temporal values providing 190 # which time-step corresponds with each temporal possible grouping ['year', 191 # 'season', 'month', 'day', 'hour', 'minute', 'second'] 189 192 # time_slices: Function to return temporal slices of a series of times for a given amount of periods 190 193 # timestep_conform: Function to provide the time-step in seconds which conforms 1 temporal unit and the resultant number … … 14054 14057 'hour': full hours [00:00 - 59:59] 14055 14058 'minute': full minutes [00 - 59] 14059 'aggmonth': aggregation mulitple months: 14060 yy1/[1,...,12]/01 00:00:00 - yym/[1,...,12]/01 00:00:00 14061 'aggday': aggregation mulitple days: 14062 yy1/mm1/[01,...,31,30,28/29] 00:00:00 - yym/mm1/[01,...,31,30,28/29] 00:00:00 14063 'agghour': aggregation mulitple hours: 14064 yy1/mm1/dd1 [00,...,23]:00:00 - yym/mmm/ddm [00,...,23]:59:59 14056 14065 amount: amount of periods 14057 14066 """ … … 14059 14068 fname = 'time_slices' 14060 14069 14061 availper = ['year', 'month', 'day', 'hour', 'minute' ]14070 availper = ['year', 'month', 'day', 'hour', 'minute', 'mmonth', 'mday', 'mhour'] 14062 14071 14063 14072 mattimes = CFtimesvar_datetime(tv, tu, cal) … … 14104 14113 estats[5]) 14105 14114 14115 iper = 0 14106 14116 # Getting statistics periods 14107 stmatdates = [istats] 14117 stmatdates = {iper: [istats]} 14118 # Single periods per stats 14108 14119 idate = istdate 14109 14120 imdate = istats.copy() 14110 14121 while idate < estdate: 14122 iper = iper + 1 14111 14123 imdate = imdate.copy() 14112 14124 imdate[0] = imdate[0] + amount 14113 14125 idate= dt.datetime(imdate[0], imdate[1], imdate[2], imdate[3], imdate[4],\ 14114 14126 imdate[5]) 14115 if idate < estdate: stmatdates .append(imdate)14127 if idate < estdate: stmatdates[iper] = [imdate] 14116 14128 else: break 14117 stmatdates .append(imdate)14129 stmatdates[iper] = [imdate] 14118 14130 14119 14131 elif per == 'month': … … 14133 14145 estats[5]) 14134 14146 14147 iper = 0 14135 14148 # Getting statistics periods 14136 stmatdates = [istats]14149 stmatdates = {iper: [istats]} 14137 14150 idate = istdate 14138 14151 imdate = istats.copy() 14139 14152 while idate < estdate: 14153 iper = iper + 1 14140 14154 imdate = imdate.copy() 14141 14155 imdate[1] = imdate[1] + amount … … 14145 14159 idate= dt.datetime(imdate[0], imdate[1], imdate[2], imdate[3], imdate[4],\ 14146 14160 imdate[5]) 14147 if idate < estdate: stmatdates .append(imdate)14161 if idate < estdate: stmatdates[iper] = [imdate] 14148 14162 else: break 14149 stmatdates .append(imdate)14163 stmatdates[iper] = [imdate] 14150 14164 14151 14165 elif per == 'day': 14152 14166 # Arranging accordingly beginning/ending statistics periods 14153 14167 istats[3:5] = 0 14154 dmon = days_month(estats[0], estats[1])14168 iper = 0 14155 14169 estats[2] = estats[2]+1 14156 14170 if estats[2] > dmon: … … 14166 14180 estats[5]) 14167 14181 14182 iper = 0 14168 14183 # Getting statistics periods 14169 stmatdates = [istats]14184 stmatdates = {iper: [istats]} 14170 14185 idate = istdate 14171 14186 imdate = istats.copy() 14172 14187 while idate < estdate: 14188 iper = iper + 1 14173 14189 imdate = imdate.copy() 14174 14190 dmon = days_month(imdate[0], imdate[1]) … … 14182 14198 idate= dt.datetime(imdate[0], imdate[1], imdate[2], imdate[3], imdate[4],\ 14183 14199 imdate[5]) 14184 if idate < estdate: stmatdates .append(imdate)14200 if idate < estdate: stmatdates[iper] = [imdate] 14185 14201 else: break 14186 stmatdates.append(imdate) 14202 stmatdates[iper] = [imdate] 14203 14204 elif per == 'aggmonth': 14205 amount = 1 14206 # Arranging accordingly beginning/ending statistics periods 14207 istats[2] = 1 14208 istats[3:5] = 0 14209 estats[1] = estats[1]+1 14210 if estats[1] > 12: 14211 estats[0] = estats[0] + 1 14212 estats[1] = 1 14213 estats[2] = 1 14214 estats[3:5] = 0 14215 14216 istdate = dt.datetime(istats[0], istats[1], istats[2], istats[3], istats[4], \ 14217 istats[5]) 14218 estdate = dt.datetime(estats[0], estats[1], estats[2], estats[3], estats[4], \ 14219 estats[5]) 14220 14221 iper = 0 14222 # Getting statistics periods. We got 12 periods 14223 stmatdates = {istats[1]: [istats]} 14224 idate = istdate 14225 imdate = istats.copy() 14226 while idate < estdate: 14227 imdate = imdate.copy() 14228 imdate[1] = imdate[1] + amount 14229 if imdate[1] > 12: 14230 imdate[0] = imdate[0] + 1 14231 imdate[1] = 1 14232 idate= dt.datetime(imdate[0], imdate[1], imdate[2], imdate[3], imdate[4],\ 14233 imdate[5]) 14234 if idate < estdate: 14235 if stmatdates.has_key(imdate[1]): 14236 vals = stmatdates[imdate[1]] 14237 vals = vals + [imdate] 14238 stmatdates[imdate[1]] = vals 14239 else: 14240 stmatdates[imdate[1]] = [imdate] 14241 else: break 14242 if stmatdates.has_key(imdate[1]): 14243 vals = stmatdates[imdate[1]] 14244 vals = vals + [imdate] 14245 stmatdates[imdate[1]] = vals 14246 else: 14247 stmatdates[imdate[1]] = [imdate] 14187 14248 14188 14249 else: … … 14219 14280 14220 14281 Srefdate = Srdate + Srtime 14221 Nslices = len(stmatdates )14282 Nslices = len(stmatdates.keys()) 14222 14283 stmdates = np.zeros((Nslices,6), dtype=np.int) 14223 14284 #print ' dates slices ________' 14224 for islc in range(Nslices): 14225 # print islc, ' ', stmatdates[islc] 14226 stmdates[islc,:] = stmatdates[islc] 14227 14228 cfstdates = realdatetime_CFcompilant(stmdates, Srefdate, tunits) 14229 14230 Nslices = len(cfstdates) 14231 itt = 0 14232 icfst = 0 14233 for it in range(dimt): 14234 timeslice = [itt,itt,1] 14235 timeslice[1] = it 14236 icfstup = np.min([icfst+1, Nslices-1]) 14237 if not(tv[it] >= cfstdates[icfst] and tv[it] < cfstdates[icfstup]): 14238 slices.append(timeslice) 14239 itt = it 14240 icfst = icfst + 1 14241 slices.append(timeslice) 14285 if per[0:3] == 'agg': 14286 Lper=len(per) 14287 pern = len[3:Lper] 14288 print ' ' + fname + ": slices as aggregations of '" + pern + "' !!" 14289 else: 14290 for islc in range(Nslices): 14291 #print islc, ' ', stmatdates[islc] 14292 stmdates[islc,:] = stmatdates[islc] 14293 14294 # CF-standard format of the dates of the slices 14295 cfstdates = realdatetime_CFcompilant(stmdates, Srefdate, tunits) 14296 14297 Nslices = len(cfstdates) 14298 itt = 0 14299 icfst = 0 14300 for it in range(dimt): 14301 timeslice = [itt,itt,1] 14302 timeslice[1] = it 14303 icfstup = np.min([icfst+1, Nslices-1]) 14304 if not(tv[it] >= cfstdates[icfst] and tv[it] < cfstdates[icfstup]): 14305 slices.append(timeslice) 14306 itt = it 14307 icfst = icfst + 1 14308 slices.append(timeslice) 14242 14309 14243 14310 Nslices = len(slices) … … 14249 14316 return slices, Nslices 14250 14317 14251 #tv = [] 14252 #values = [] 14253 #totT = 24.*60. 14254 #dT = 7. 14255 #for it in range(0,10): 14256 # for itt in range(3): 14257 # tv.append(29*24*60.+it*totT+dT*itt) 14258 # values.append(it*1.) 14259 14260 #vals = np.array(values) 14261 #itdim = 0 14262 #tu = 'minutes since 1949-12-01 00:00:00' 14263 #per = 'day' 14264 #calend = 'standard' 14265 #amount = 1 14318 tv = [] 14319 values = [] 14320 totT = 24.*60. 14321 dT = 7.345 14322 for it in range(0,10): 14323 for itt in range(3): 14324 tv.append(29*24*60.+it*totT+dT*itt) 14325 #values.append((it*3+itt)*1.) 14326 values.append(it*1.) 14327 14328 #print 'tv:', tv 14329 #print 'values:', values 14330 14331 vals = np.array(values) 14332 itdim = 0 14333 tu = 'minutes since 1949-12-01 00:00:00' 14334 per = 'day' 14335 calend = 'standard' 14336 amount = 1 14266 14337 14267 14338 #tslc, Ntslc = time_slices(tv, tu, calend, per, amount) … … 14288 14359 # etime = tv[timeslc[1]] 14289 14360 # print islc, ':', itime, '-', etime, '<>', tvals, '=', tstat[islc] 14361 #quit() 14290 14362 14291 14363 def Latin_Greek(char): … … 14770 14842 #print all_consecutive_combs([3, 2, 2]) 14771 14843 14844 def temporal_desc(tv, tu, cal): 14845 """ Function to provide the description of a series of temporal values providing 14846 which time-step corresponds with each temporal possible grouping ['year', 14847 'season', 'month', 'day', 'hour', 'minute', 'second'] 14848 tv: time values 14849 tu: time units of [timevals] as in CF-standard [Tunits] since [refdate] 14850 cal: calendar of [timevals] 14851 >>> tv = [] 14852 >>> dT = 20027.345 14853 >>> for it in range(0,30): 14854 >>> tv.append(29*24*60.+dT*it) 14855 >>>tu = 'minutes since 1949-12-01 00:00:00' 14856 >>>per = 'day' 14857 >>>calend = 'standard' 14858 >>>temporal_desc(tv, tu, calend) 14859 # hour : {0: [0], 1: [10, 21], 3: [20], 4: [9], 5: [19], 6: [8], 7: [29], 14860 8: [7, 18], 10: [6, 17, 28], 12: [5, 16, 27], 14: [15, 26], 15: [4], 14861 16: [25], 17: [3, 14], 18: [24], 19: [2, 13], 21: [1, 12, 23], 23: [11, 22]} 14862 # season : {'SON': [18, 19, 20, 21, 22], 'MAM': [5, 6, 7, 8], 14863 'JJA': [12, 13, 14, 15], 'DJF': [0, 1, 2, 25, 26, 27, 28]} 14864 # month : {1: [1, 2, 27, 28], 2: [3, 4, 29], 3: [5, 6], 4: [7, 8], 14865 5: [9, 10, 11], 6: [12, 13], 7: [14, 15], 8: [16, 17], 9: [18, 19], 14866 10: [20, 21, 22], 11: [23, 24], 12: [0, 25, 26]} 14867 # second : {0: [0], 2: [14], 5: [28], 6: [9], 8: [23], 9: [4], 12: [18], 14868 15: [13], 18: [8, 27], 21: [22], 22: [3], 24: [17], 28: [12], 30: [26], 14869 31: [7], 34: [2, 21], 37: [16], 40: [11], 43: [25], 44: [6], 46: [20], 14870 47: [1], 50: [15], 53: [10, 29], 56: [5, 24], 59: [19]} 14871 # year : {1949: [0], 1950: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14872 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26], 1951: [27, 28, 29]} 14873 # day : {4: [9, 20], 6: [7, 18, 29], 9: [3, 5, 16, 27], 12: [1, 14, 25], 14874 14: [12, 23], 18: [10, 21], 20: [8, 19], 23: [4, 6, 17, 28], 26: [2, 15, 26], 14875 28: [13, 24], 30: [0], 31: [11, 22]} 14876 # minute : {0: [0, 29], 2: [3], 4: [6], 6: [9], 8: [12], 10: [15], 12: [18], 14877 14: [21], 16: [24], 18: [27], 20: [1], 22: [4], 24: [7], 27: [10], 29: [13], 14878 31: [16], 33: [19], 35: [22], 37: [25], 39: [28], 41: [2], 43: [5], 45: [8], 14879 47: [11], 49: [14], 51: [17], 54: [20], 56: [23], 58: [26]} 14880 """ 14881 import datetime as dt 14882 fname = 'temporal_desc' 14883 14884 mattimes = CFtimesvar_datetime(tv, tu, cal) 14885 14886 if type(tv) == type(np.arange(2)): 14887 dimt = tv.shape[0] 14888 elif type(tv) == type(range(2)): 14889 dimt = len(tv) 14890 else: 14891 print errormsg 14892 print ' ' + fname + ': time values type ', type(tv), 'not ready !!' 14893 print ' available ones:', type(np.arange(1)), type(range(2)) 14894 quit(-1) 14895 14896 # Beginning and Ending of time data 14897 it = 0 14898 idate = dt.datetime(mattimes[it,0], mattimes[it,1], mattimes[it,2], \ 14899 mattimes[it,3], mattimes[it,4], mattimes[it,5]) 14900 it = dimt-1 14901 edate = dt.datetime(mattimes[it,0], mattimes[it,1], mattimes[it,2], \ 14902 mattimes[it,3], mattimes[it,4], mattimes[it,5]) 14903 14904 istats = mattimes[0,:] 14905 estats = mattimes[dimt-1,:] 14906 14907 temporal_desc = {} 14908 timesecs = ['year', 'season', 'month', 'day', 'hour', 'minute', 'second'] 14909 14910 for timesec in timesecs: 14911 if timesec == 'year': itsec = 0 14912 elif timesec == 'season': itsec = 1 14913 elif timesec == 'month': itsec = 1 14914 elif timesec == 'day': itsec = 2 14915 elif timesec == 'hour': itsec = 3 14916 elif timesec == 'minute': itsec = 4 14917 elif timesec == 'second': itsec = 5 14918 else: 14919 print errormsg 14920 print ' ' + fname + ": '" + timesec + "' not ready !!" 14921 print ' available ones:', timesecs 14922 quit(-1) 14923 14924 tvals = list(set(list(mattimes[:,itsec]))) 14925 tvals.sort() 14926 14927 tvsec = {} 14928 if timesec != 'season': 14929 for it in range(dimt): 14930 if timesec == 'year': print it, ':', mattimes[it,:] 14931 for tvv in tvals: 14932 if mattimes[it,itsec] == tvv: 14933 if tvsec.has_key(tvv): 14934 vvv = tvsec[tvv] 14935 vvv.append(it) 14936 else: 14937 vvv = [it] 14938 tvsec[tvv] = vvv 14939 break 14940 else: 14941 tvals = {'DJF':[12,1,2], 'MAM':[3,4,5], 'JJA':[6,7,8], 'SON':[9,10,11]} 14942 months12 = np.arange(12)+1 14943 for it in range(dimt): 14944 for tvv in tvals: 14945 tvvv = tvals[tvv] 14946 iper = tvvv[0] 14947 eper = tvvv[2] 14948 if cyclevar_within(months12, iper, eper, mattimes[it,itsec]): 14949 if tvsec.has_key(tvv): 14950 vvv = tvsec[tvv] 14951 vvv.append(it) 14952 else: 14953 vvv = [it] 14954 tvsec[tvv] = vvv 14955 break 14956 temporal_desc[timesec]=tvsec 14957 14958 printing_dictionary(temporal_desc) 14959 14960 return temporal_desc 14961 14962 14963 tv = [] 14964 values = [] 14965 totT = 24.*60. 14966 dT = 20027.345 14967 for it in range(0,30): 14968 tv.append(29*24*60.+dT*it) 14969 print 29*24*60.+dT*it 14970 #values.append((it*3+itt)*1.) 14971 values.append(it*1.) 14972 14973 #print 'tv:', tv 14974 #print 'values:', values 14975 14976 vals = np.array(values) 14977 itdim = 0 14978 tu = 'minutes since 1949-12-01 00:00:00' 14979 per = 'day' 14980 calend = 'standard' 14981 amount = 1 14982 14983 temporal_desc(tv, tu, calend) 14984 14772 14985 #quit() 14773 14986
Note: See TracChangeset
for help on using the changeset viewer.