Changeset 1195 in lmdz_wrf for trunk/tools
- Timestamp:
- Oct 18, 2016, 3:53:08 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r1191 r1195 62 62 # list_coincidences: Function to provide the coincidences between two lists 63 63 # list_differences: Function to provide the differences between two lists 64 # list_norepeatcombos: Function to all possible (Num-1)-combinations of a Num values without repetitions 64 65 # multi_index_mat: Function to provide the multiple coordinates of a given value inside a matrix 65 66 # num_chainSnum: Function to pass a value to a `ChainStrNum' number … … 69 70 # [YYYY][MM][DD][HH][MI][SS] format) in a 360 years calendar 70 71 # pretty_int: Function to plot nice intervals 72 # prime_decomposition: Function to decompose a given number with its multiple prime numbers 73 # prime_numbers: Function to find all the prime numbers up to a given value above 17 71 74 # printing_dictionary: Function to print the content of a dictionary 72 75 # PolyArea: Function to compute the area of the polygon following 'Shoelace formula' … … 85 88 # stringList_dictKeysVals: Function to provide a dictionary with keys which contain values of lists from a string 86 89 # subbasin_point: Function to provide sub-basins given a grid point following a matrix of trips 90 # timestep_conform: Function to provide the time-step in seconds which conforms 1 temporal unit and the resultant number 91 # of time-steps for a whole period is multiple of a given number 87 92 # unitsdsDate: Function to know how many units of time are from a given pair of dates 88 93 # vals_around: Function to provide the 3x3 values around a given j,i point … … 292 297 293 298 return newlist 299 300 def list_norepeatcombos(Num): 301 """ Function to all possible (Num-1)-combinations of a Num values without repetitions 302 Num= number of elements 303 >>> list_norepeatcombos(4) 304 [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3], [0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]] 305 """ 306 from math import factorial 307 fname = 'list_norepeatcombos' 308 309 listv = range(Num) 310 311 samevals = [] 312 diffvals = [] 313 for iv in listv: 314 if not searchInlist(diffvals,iv): diffvals.append(iv) 315 if searchInlist(samevals,iv): samevals.append(iv) 316 317 combos = [] 318 Nlvs = len(listv) 319 # Number of possible combinations 320 for ic in range(2,Nlvs): 321 Ncombs = factorial(Nlvs)/(factorial(ic)*factorial(Nlvs-ic)) 322 combo = range(ic) 323 # Getting the limits for each element of the combination of ic-elements 324 limitcombo = range(ic) 325 for icc in range(ic): 326 limitcombo[icc] = Nlvs-ic+icc 327 328 combos.append(combo) 329 ncombo = list(combo) 330 for ico in range(Ncombs): 331 # Incrementing the last coordinate 332 ncombo[ic-1] = ncombo[ic-1] + 1 333 if ncombo[ic-1] > limitcombo[ic-1]: 334 # looping all over the others if there is an overpass of the value 335 # previous coordinate will be incremented and the following ones 336 # will be incremented by 1 starting from the new assigned previous 337 # value 338 for icc in range(1,ic): 339 # Incrementing the previous one 340 ncombo[ic-icc-1] = ncombo[ic-icc-1]+1 341 # Incrementing the following ones since the value at the previous 342 # position 343 for iccc in range(ic-icc,ic): 344 ncombo[iccc] = ncombo[iccc-1]+1 345 if ncombo[ic-icc-1] <= limitcombo[ic-icc-1]: 346 combos.append(list(ncombo)) 347 break 348 else: 349 combos.append(list(ncombo)) 350 351 return combos 294 352 295 353 def datetimeStr_conversion(StringDT,typeSi,typeSo): … … 9511 9569 return newlist 9512 9570 9513 9514 9571 def pretty_int(minv,maxv,Nint): 9515 9572 """ Function to plot nice intervals … … 9569 9626 return np.array(values, dtype=np.float) 9570 9627 9628 def prime_numbers(value): 9629 """ Function to find all the prime numbers up to a given value above 17 9630 value: limiting value to use 9631 >>> primes(100) 9632 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] 9633 """ 9634 fname = 'prime_numbers' 9635 9636 primes = [2,3,5,7,11,13,17] 9637 lastprime = 17 9638 for i in range(lastprime+1,value): 9639 #print ' ',i,value 9640 isprime = False 9641 for ip in primes: 9642 # No need to find allover the previous primes, just as big as the half 9643 if ip < i/2: 9644 if np.mod(i,ip) == 0: 9645 isprime = False 9646 break 9647 else: 9648 isprime = True 9649 if isprime: 9650 primes.append(i) 9651 lastprime = i 9652 #print fname + ': prime number',i,'!!' 9653 return primes 9654 9655 def prime_decomposition(num): 9656 """ Function to decompose a given number with its multiple prime numbers 9657 fname= num 9658 >>> prime_decomposition(100) 9659 {2: 2, 5: 2} 9660 """ 9661 fname = 'prime_decomposition' 9662 9663 decomposition = {} 9664 9665 # Prime numbers 9666 primnums = prime_numbers(num) 9667 9668 inum = num 9669 while inum > primnums[0]: 9670 for primn in primnums: 9671 if np.mod(inum,primn) == 0: 9672 inum = inum / primn 9673 if decomposition.has_key(primn): 9674 decomposition[primn] = decomposition[primn] + 1 9675 else: 9676 decomposition[primn] = 1 9677 break 9678 9679 return decomposition 9680 9681 def timestep_conform(TOTtime,tunit,multnum): 9682 """ Function to provide the time-step in seconds which conforms 1 temporal unit and the resultant number of time-steps for 9683 a whole period is multiple of a given number 9684 TOTtime: period [seconds] 9685 tunit: temporal unit at which the time-step has to conform [seconds] 9686 multnum: number to which the resultant number of time-step for `TOTtime' have to be multiple 9687 >>> timestep_conform(86400,3600,5) 9688 [4, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 30, 36, 40, 45, 48, 60, 72, 80, 90, 120, 144, 180, 240, 360, 720] 9689 """ 9690 fname = 'timestep_conform' 9691 9692 # Possible fractions 9693 tunitdecompos = prime_decomposition(tunit) 9694 9695 allvals = [] 9696 for prime in tunitdecompos.keys(): 9697 for it in range(tunitdecompos[prime]): 9698 allvals.append(prime) 9699 9700 Nallvals = len(allvals) 9701 allcombinations = list_norepeatcombos(Nallvals) 9702 Ncombos = len(allcombinations) 9703 9704 # It might be a far more efficient way? I'm lazy... 9705 timestep = [] 9706 for ic in range(Ncombos): 9707 combo = allcombinations[ic] 9708 val = 1 9709 for icc in combo: 9710 val = val*allvals[icc] 9711 9712 if not searchInlist(timestep,val) and np.mod(TOTtime/val,multnum) == 0: 9713 timestep.append(val) 9714 9715 timestep.sort() 9716 return timestep 9717 9718 print timestep_conform(86400,3600,5) 9571 9719 #quit() 9572 9720
Note: See TracChangeset
for help on using the changeset viewer.