Changeset 813 in lmdz_wrf
- Timestamp:
- Jun 12, 2016, 12:55:17 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r812 r813 11 11 12 12 fillValue = 1.e20 13 fillValueF = 1.e20 13 fillValueF = 1.e20 14 14 fillValueC = '-' 15 15 fillValueI = -99999 … … 19 19 20 20 ####### Content 21 # contflow: Function to bring back the increment in j,i grid points according to a trip: (inflow directions) 22 # dictionary_key: Function to provide the first key in a dictionay with a given value 23 # dictionary_key_list: Function to provide the first key in a dictionay of lists with a given value 21 24 # ijlonlat: Function to provide the imin,jmin imax,jmax of a lon,lat box 25 # incomming_flow: Function to determine if a fgrid-flow inflows to the central grid point 22 26 # PolyArea: Function to compute the area of the polygon following 'Shoelace formula' 23 27 # significant_decomposition: Function to decompose a given number by its signifcant potencies 24 28 # unitsdsDate: Function to know how many units of time are from a given pair of dates 29 # vals_around: Function to provide the 3x3 values around a given j,i point 30 # subbasin_point: Function to provide sub-basins given a grid point following a matrix of trips 25 31 26 32 def values_line(line, splitv, chars): … … 6460 6466 #print unitsDate('19490101000000', '19760217082932', 'minute') 6461 6467 6462 #quit() 6468 def incomming_flow(dirs): 6469 """ Function to determine if a fgrid-flow inflows to the central grid point 6470 dirs = [3,3] matrix with the trip: (inflow directions) 6471 1: N, 2: NE, 3: E, 4: SE, 5: S, 6: SW: 7: W, 8: NW 6472 being: 6473 0,0: NW point, 0,1: N point, 0,2: NE point, 6474 1,0: W point, 1,1: grid-point, 1,2: E point, 6475 2,0: SW point, 2,1: S point, 2,2: SE point 6476 >>> dirflow = np.array([5, 5, 5, 98, 7, 7, 1, 1, 1], dtype=int).reshape(3,3) 6477 >>> print dirflow 6478 [[5 5 5] 6479 [98 7 7] 6480 [1 1 1]] 6481 >>> print incomming_flow(dirflow) 6482 [[False True False] 6483 [False False True] 6484 [False True False]] 6485 """ 6486 fname = 'incomming_flow' 6487 6488 inflow = np.zeros((3,3), dtype=bool) 6489 6490 rightinflow = np.zeros((3,3), dtype=int) 6491 rightinflow[0,0] = 4 6492 rightinflow[0,1] = 5 6493 rightinflow[0,2] = 6 6494 rightinflow[1,0] = 3 6495 rightinflow[1,1] = 0 6496 rightinflow[1,2] = 7 6497 rightinflow[2,0] = 2 6498 rightinflow[2,1] = 1 6499 rightinflow[2,2] = 8 6500 6501 inflow = np.where(dirs == rightinflow, True, False) 6502 6503 return inflow 6504 6505 def contflow(dirflow): 6506 """ Function to bring back the increment in j,i grid points according to a trip: (inflow directions) 6507 1: N, 2: NE, 3: E, 4: SE, 5: S, 6: SW: 7: W, 8: NW 6508 >>> contflow(2) 6509 [1 1] 6510 >>> contflow(7) 6511 [0 -1] 6512 >>> contflow(98) 6513 [-1 -1] 6514 """ 6515 fname = 'contflow' 6516 inc = np.ones((2), dtype=int)*(-1) 6517 6518 if dirflow == 1: #N 6519 inc[0] = 1 6520 inc[1] = 0 6521 elif dirflow == 2: #NE 6522 inc[0] = 1 6523 inc[1] = 1 6524 elif dirflow == 3: #E 6525 inc[0] = 0 6526 inc[1] = 1 6527 elif dirflow == 4: #SE 6528 inc[0] = -1 6529 inc[1] = 1 6530 elif dirflow == 5: #S 6531 inc[0] = -1 6532 inc[1] = 0 6533 elif dirflow == 6: #SW 6534 inc[0] = -1 6535 inc[1] = -1 6536 elif dirflow == 7: #W 6537 inc[0] = 0 6538 inc[1] = -1 6539 elif dirflow == 8: #NW 6540 inc[0] = 1 6541 inc[1] = -1 6542 else: # 6543 inc[0] = -1 6544 inc[1] = -1 6545 6546 return inc 6547 6548 def vals_around(vals,pt): 6549 """ Function to provide the 3x3 values around a given j,i point 6550 vals = matrix of values 6551 pt = j,i point to serch around 6552 >>> vals_around(np.arange(16).reshape(4,4), [3, 3]) 6553 [[10.0 11.0 None] 6554 [14.0 15.0 None] 6555 [None None None]] 6556 """ 6557 fname = 'vals_around' 6558 6559 vals3x3 = np.array([None]*9).reshape(3,3) 6560 6561 dx = vals.shape[1] 6562 dy = vals.shape[0] 6563 6564 nx = np.max([0,pt[1]-1]) 6565 xx = np.min([dx-1,pt[1]+1]) + 1 6566 ny = np.max([0,pt[0]-1]) 6567 xy = np.min([dy-1,pt[0]+1]) + 1 6568 6569 vals3x3[1-(pt[0]-ny):1+(xy-pt[0]),1-(pt[1]-nx):1+(xx-pt[1])] = vals[ny:xy,nx:xx] 6570 6571 return vals3x3 6572 6573 def dictionary_key(dictv, val): 6574 """ Function to provide the first key in a dictionay with a given value 6575 ditcv= dictionary 6576 val= value to search 6577 >>> dictionary_key({'i': 1, 'ii': 2, 'iii': 3, 'iv': 4}, 3) 6578 'iii' 6579 """ 6580 fname = 'dictionary_key' 6581 6582 keyval = None 6583 for keyv in dictv.keys(): 6584 if dictv[keyv] == val: 6585 keyval = keyv 6586 break 6587 6588 return keyval 6589 6590 def dictionary_key_list(dictv, val): 6591 """ Function to provide the first key in a dictionay of lists with a given value 6592 ditcv= dictionary 6593 val= value to search 6594 >>> dictionary_key_list({'i': [1, 0, -1], 'ii': [2, 8, 16], 'iii': [-3, 3, 9], 'iv': [4]}, 3) 6595 'iii' 6596 """ 6597 fname = 'dictionary_key' 6598 6599 keyval = None 6600 for keyv in dictv.keys(): 6601 if searchInlist(dictv[keyv],val): 6602 keyval = keyv 6603 break 6604 6605 return keyval 6606 6607 def subbasin_point(trips, pt): 6608 """ Function to provide sub-basins given a grid point following a matrix of trips 6609 trips= matrix of trips values 6610 1: N, 2: NE, 3: E, 4: SE, 5: S, 6: SW: 7: W, 8: NW 6611 pt= point within the trip matrix 6612 """ 6613 fname = 'subbasin_point' 6614 6615 subbasin = np.zeros((trips.shape), dtype=bool) 6616 TOTtrips = len(trips.flatten()) 6617 6618 # Dictionary with the j,i points of all the subbasin 6619 ijsubbasin = {} 6620 # Dictionary to tell if a given j,i sub-basin point has been localized (as number from ijsubbasin) 6621 ijfound = {} 6622 # Dictionary to tell if a given sub-flow has been finished (when Narrive == 0) 6623 subfinished = {} 6624 # Dictionary with the list of points which are contained in a sub-flow 6625 ijsubflow = {} 6626 # Number of ji sub-basin points 6627 Nij = 1 6628 6629 subbasin[pt[0], pt[1]] = True 6630 ijsubbasin[Nij] = np.array(pt) 6631 ijfound[Nij] = False 6632 ijsubflow[str(Nij)] = [Nij] 6633 subfinished[str(Nij)] = False 6634 6635 # Loop around all sub-flows 6636 ## 6637 while np.sum(subfinished.values) != 0 and np.sum(ijfound.values) != 0: 6638 Njipt = dictionary_key(subfinished, False) 6639 if Njipt is not None: 6640 print Njipt, 'points subflow:', ijsubflow[Njipt] 6641 for ipt in ijsubflow[Njipt]: 6642 if ijfound[ipt] == False: 6643 jipt = ijsubbasin[ipt] 6644 break 6645 print ' working from point:', ipt, 'ji pair:', jipt 6646 ijfound[ipt] = True 6647 Nij = ipt 6648 else: 6649 # Keep searching since there are grid-points not found! 6650 print ' ' + fname + ': Keep searching since there are grid-points not found!!' 6651 print ijfound 6652 Nij = dictionary_key(ijfound, False) 6653 if Nij is None: 6654 return subbasin, ijsubflow, ijsubbasin 6655 6656 ijfound[Nij] = True 6657 jipt = ijsubbasin[Nij] 6658 Njipt = dictionary_key_list(ijsubflow, Nij) 6659 if subfinished.has_key(Njipt + '1'): 6660 if subfinished.has_key(Njipt + '01'): 6661 if subfinished.has_key(Njipt + '001'): 6662 Njipt = Njipt + '0001' 6663 else: 6664 Njipt = Njipt + '001' 6665 else: 6666 Njipt = Njipt + '01' 6667 else: 6668 Njipt = Njipt + '1' 6669 print ' ' + fname + "new sub-flow '" + Njipt + "' !!" 6670 subfinished[Njipt] = False 6671 Nij = np.max(ijfound.keys()) 6672 6673 print 'Lluis Nij:', Nij 6674 # Looking for which point of the sub-flow retake the search 6675 if Nij == 1: 6676 ipt = 1 6677 jipt = pt 6678 6679 ardtrips = vals_around(trips,jipt) 6680 print ' ' + fname + 'ardtrips _______' 6681 print ardtrips 6682 6683 arrive = incomming_flow(ardtrips) 6684 Narrive = np.sum(arrive) 6685 print Nij, ' ' + fname + ' Narrive:', Narrive 6686 if Narrive == 0: 6687 ijfound[Nij] = True 6688 subfinished[Njipt] = True 6689 else: 6690 print ' ' + fname + 'arrive _______' 6691 print arrive 6692 followvals = np.zeros((3,3), dtype=bool) 6693 followvals = arrive 6694 6695 for ifollow in range(Narrive): 6696 print 'ifollow:',ifollow,'/',Narrive 6697 6698 # We only want to work with that ij, which have not yet been found 6699 while np.sum(followvals) != 0: 6700 print Nij,' Looking for a non-located point in subbasin ________' 6701 print subbasin[jipt[0]-1:jipt[0]+2,jipt[1]-1:jipt[1]+2] 6702 jifollow = index_mat(followvals, True) 6703 jiequiv = jifollow - [1,1] 6704 if subbasin[jipt[0]+jiequiv[0], jipt[1]+jiequiv[1]] == False: 6705 Nij = np.max(ijfound.keys()) + 1 6706 jiptnew = jipt + jiequiv 6707 if ifollow != 0: 6708 # Avoiding repetition of sub-flow name 6709 if subfinished.has_key(Njipt + str(ifollow)): 6710 Njipt = Njipt + '0' + str(ifollow) 6711 else: 6712 Njipt = Njipt + str(ifollow) 6713 print ' ' + fname + "new sub-flow '" + Njipt + "' !!" 6714 subfinished[Njipt] = False 6715 subbasin[jipt[0]+jiequiv[0], jipt[1]+jiequiv[1]] = True 6716 ijfound[Nij] = False 6717 ijsubbasin[Nij] = jiptnew 6718 if ijsubflow.has_key(Njipt): 6719 subflowpts = ijsubflow[Njipt] 6720 subflowpts.append(Nij) 6721 ijsubflow[Njipt] = subflowpts 6722 else: 6723 ijsubflow[Njipt] = [Nij] 6724 break 6725 else: 6726 followvals[jifollow[0],jifollow[1]] = False 6727 if np.sum(followvals) == 0: 6728 ijfound[Nij] = True 6729 subfinished[Njipt] = True 6730 print Nij," subflow '" + Njipt + "' finished!!" 6731 break 6732 6733 if Nij > TOTtrips: 6734 print errormsg 6735 print ' ' + fname + ': sub-flow point', Nij, 'larger than ' + \ 6736 'the number of trips', TOTtrips,'!!' 6737 quit() 6738 6739 if ijsubflow.has_key(Njipt): 6740 print "Lluis points of subflow: '" + Njipt + "' _______=", ijsubflow[Njipt] 6741 for isub in ijsubflow[Njipt]: 6742 print ' ' , isub , ':', ijsubbasin[isub], ijfound[isub] 6743 if Nij == 10: print 'Nij = 9:', ijfound[9] 6744 6745 print subbasin 6746 # if Nij > 4: quit() 6747 6748 return subbasin 6749 6750 # Caceres 6751 rivers = np.array([7, 1, 1, 1, 1, 5, 5, 1, \ 6752 7, 1, 6, 3, 5, 5, 6, 5, \ 6753 8, 7, 5, 3, 5, 6, 3, 5, \ 6754 1, 1, 3, 5, 5, 7, 5, 5, \ 6755 3, 3, 5, 6, 5, 5, 5, 7 ], dtype=int).reshape(5,8) 6756 6757 point = [4,4] 6758 6759 # Porto Do Alegre 6760 #rivers = np.array([5, 5, 1, 7, 1, 8, 8, 2, \ 6761 # 5, 6, 5, 7, 7, 1, 3, 1, \ 6762 # 6, 3, 5, 1, 3, 3, 3, 3, \ 6763 # 7, 5, 5, 5, 5, 5, 5, 1, \ 6764 # 5, 5, 7, 7, 7, 6, 7, 7, \ 6765 # 5, 5, 7, 7, 7, 7, 8, 7, \ 6766 # 6, 6, 7, 7, 7, 7, 7, 7, \ 6767 # 5, 7, 7, 7, 8, 7, 7, 6, \ 6768 # 6, 7, 7, 7, 7, 7, 7, 6], dtype=int).reshape(9,8) 6769 6770 #point = [7,0] 6771 6772 print rivers 6773 print rivers[point[0], point[1]] 6774 6775 masksubbasin, subflows, subflowspt = subbasin_point(rivers, point) 6776 print rivers 6777 print masksubbasin 6778 for subflow in subflows: 6779 print subflow, ':', subflows[subflow] 6780 for isub in subflows[subflow]: 6781 print ' ' , isub , ':', subflowspt[isub] 6782 6783 print 'Total sub-basin:', np.sum(masksubbasin) 6784 6785 quit()
Note: See TracChangeset
for help on using the changeset viewer.