- Timestamp:
- Jun 16, 2016, 1:13:57 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r830 r832 7062 7062 #quit() 7063 7063 7064 def subbasin_point(trips, pt, direction): 7064 def index_mat_way(mat,val,way): 7065 """ Function to look for a value within a matrix following a way and direction 7066 mat: matrix 7067 val: value to search 7068 way: way of search 7069 'anticlockwise12': looking in an anti-clockwise direction starting at 12 7070 'anticlockwise6': looking in an anti-clockwise direction starting at 6 7071 'clockwise12': looking in an clockwise direction starting at 12 7072 'clockwise6': looking in an clockwise direction starting at 6 7073 >>> mat = np.arange(20).reshape(4,5) 7074 >>> mat[2,1] = 3 7075 >>> index_mat_way(mat,3,'clockwise12') 7076 [array([0, 3]), array([2, 1])] 7077 >>> index_mat_way(mat,3,'anticlockwise12') 7078 [array([2, 1]), array([0, 3])] 7079 """ 7080 fname = 'index_mat_way' 7081 7082 ways = ['anticlockwise12', 'anticlockwise6', 'clockwise12', 'clockwise6'] 7083 7084 Ndims = len(mat.shape) 7085 dx = mat.shape[Ndims-1] 7086 dy = mat.shape[Ndims-2] 7087 7088 diffmat = np.abs(mat - val) 7089 mindiff = np.min(diffmat) 7090 if np.sum(diffmat == mindiff) == 1: 7091 ijindex = index_mat(diffmat,mindiff) 7092 return ijindex 7093 7094 if Ndims > 2: dz = mat.shape[Ndims-3] 7095 if Ndims > 3: dt = mat.shape[Ndims-4] 7096 7097 if way == 'anticlockwise12': 7098 # Sorting following a double criteria, first angle and then distance taken from the 7099 # center in the anti-clockwise direction starting at 12 7100 if Ndims > 2: 7101 print errormsg 7102 print ' ' + fame + ': wiht more than 2-dims:', Ndims, "'" + way + \ 7103 "' is not possible !!" 7104 quit(-1) 7105 7106 distrad = radius_dist(dx,dy,dx/2,dy/2) 7107 distangle = radius_angle(dx,dy,dx/2,dy/2) 7108 distangle = np.where(distangle >= np.pi, distangle - 2.*np.pi, distangle) 7109 distangle = np.where(distangle > 0., 2.* np.pi - distangle, -distangle) 7110 7111 sortedij = {} 7112 minangle = 10000. 7113 notfound = np.zeros((dy,dx), dtype=bool) 7114 maskdistrad = ma.array(distrad, mask=notfound) 7115 maskdistangle = ma.array(distangle, mask=notfound) 7116 for ip in range(dx*dy): 7117 minangle = np.min(maskdistangle) 7118 jiangle = multi_index_mat(maskdistangle, minangle) 7119 mindist = 10000. 7120 for ia in range(len(jiangle)): 7121 iaji = jiangle[ia] 7122 if maskdistrad[iaji[0], iaji[1]] < mindist: 7123 mindist = maskdistrad[iaji[0], iaji[1]] 7124 idistangle = iaji 7125 notfound[idistangle[0],idistangle[1]] = True 7126 sortedij[ip] = idistangle 7127 7128 elif way == 'anticlockwise6': 7129 # Sorting following a double criteria, first angle and then distance taken from the 7130 # center in the anti-clockwise direction starting at 6 7131 if Ndims > 2: 7132 print errormsg 7133 print ' ' + fame + ': wiht more than 2-dims:', Ndims, "'" + way + \ 7134 "' is not possible !!" 7135 quit(-1) 7136 7137 distrad = radius_dist(dx,dy,dx/2,dy/2) 7138 distangle = radius_angle(dx,dy,dx/2,dy/2) 7139 distangle = np.where(distangle > np.pi, distangle - 2.*np.pi, distangle) 7140 distangle = np.where(distangle >= 0., np.pi - distangle, np.pi-distangle) 7141 7142 sortedij = {} 7143 minangle = 10000. 7144 notfound = np.zeros((dy,dx), dtype=bool) 7145 maskdistrad = ma.array(distrad, mask=notfound) 7146 maskdistangle = ma.array(distangle, mask=notfound) 7147 for ip in range(dx*dy): 7148 minangle = np.min(maskdistangle) 7149 jiangle = multi_index_mat(maskdistangle, minangle) 7150 mindist = 10000. 7151 for ia in range(len(jiangle)): 7152 iaji = jiangle[ia] 7153 if maskdistrad[iaji[0], iaji[1]] < mindist: 7154 mindist = maskdistrad[iaji[0], iaji[1]] 7155 idistangle = iaji 7156 notfound[idistangle[0],idistangle[1]] = True 7157 sortedij[ip] = idistangle 7158 7159 elif way == 'clockwise12': 7160 # Sorting following a double criteria, first angle and then distance taken from the 7161 # center in the cloc-kwise direction starting at 12 7162 if Ndims > 2: 7163 print errormsg 7164 print ' ' + fame + ': wiht more than 2-dims:', Ndims, "'" + way + \ 7165 "' is not possible !!" 7166 quit(-1) 7167 7168 distrad = radius_dist(dx,dy,dx/2,dy/2) 7169 distangle = radius_angle(dx,dy,dx/2,dy/2) 7170 7171 sortedij = {} 7172 minangle = 10000. 7173 notfound = np.zeros((dy,dx), dtype=bool) 7174 maskdistrad = ma.array(distrad, mask=notfound) 7175 maskdistangle = ma.array(distangle, mask=notfound) 7176 for ip in range(dx*dy): 7177 minangle = np.min(maskdistangle) 7178 jiangle = multi_index_mat(maskdistangle, minangle) 7179 mindist = 10000. 7180 for ia in range(len(jiangle)): 7181 iaji = jiangle[ia] 7182 if maskdistrad[iaji[0], iaji[1]] < mindist: 7183 mindist = maskdistrad[iaji[0], iaji[1]] 7184 idistangle = iaji 7185 notfound[idistangle[0],idistangle[1]] = True 7186 sortedij[ip] = idistangle 7187 7188 elif way == 'clockwise6': 7189 # Sorting following a double criteria, first angle and then distance taken from the 7190 # center in the clockwise direction starting at 6 7191 if Ndims > 2: 7192 print errormsg 7193 print ' ' + fame + ': wiht more than 2-dims:', Ndims, "'" + way + \ 7194 "' is not possible !!" 7195 quit(-1) 7196 7197 distrad = radius_dist(dx,dy,dx/2,dy/2) 7198 distangle = radius_angle(dx,dy,dx/2,dy/2) 7199 distangle = np.where(distangle >= np.pi, distangle - 2.*np.pi, distangle) 7200 distangle = np.where(distangle > 0., np.pi + distangle, np.pi+distangle) 7201 7202 sortedij = {} 7203 minangle = 10000. 7204 notfound = np.zeros((dy,dx), dtype=bool) 7205 maskdistrad = ma.array(distrad, mask=notfound) 7206 maskdistangle = ma.array(distangle, mask=notfound) 7207 for ip in range(dx*dy): 7208 minangle = np.min(maskdistangle) 7209 jiangle = multi_index_mat(maskdistangle, minangle) 7210 mindist = 10000. 7211 for ia in range(len(jiangle)): 7212 iaji = jiangle[ia] 7213 if maskdistrad[iaji[0], iaji[1]] < mindist: 7214 mindist = maskdistrad[iaji[0], iaji[1]] 7215 idistangle = iaji 7216 notfound[idistangle[0],idistangle[1]] = True 7217 sortedij[ip] = idistangle 7218 7219 else: 7220 print errormsg 7221 print ' ' + fname + ": way of search '" + way + "' not ready !!" 7222 print ' available ways:', ways 7223 7224 indices = [] 7225 for ij in range(dx*dy): 7226 sij = sortedij[ij] 7227 sval = mat[sij[0], sij[1]] 7228 if sval == val: 7229 indices.append(sij) 7230 7231 return indices 7232 7233 def subbasin_point_orig(trips, pt): 7065 7234 """ Function to provide sub-basins given a grid point following a matrix of trips 7066 7235 trips= matrix of trips values 7067 7236 1: N, 2: NE, 3: E, 4: SE, 5: S, 6: SW: 7: W, 8: NW 7068 7237 pt= point within the trip matrix 7069 direction= searching direction river-up ('left': clockwise, 'right': anti-clockwise)7070 7238 """ 7071 7239 fname = 'subbasin_point' … … 7085 7253 Nij = 1 7086 7254 7087 subbasin[pt[0], pt[1]] = True 7255 subbasin[pt[0], pt[1]] = True 7088 7256 ijsubbasin[Nij] = np.array(pt) 7089 7257 ijfound[Nij] = False … … 7096 7264 Njipt = dictionary_key(subfinished, False) 7097 7265 if Njipt is not None: 7266 # print Njipt, 'points subflow:', ijsubflow[Njipt] 7098 7267 for ipt in ijsubflow[Njipt]: 7099 7268 if ijfound[ipt] == False: 7100 7269 jipt = ijsubbasin[ipt] 7101 7270 break 7271 # print ' working from point:', ipt, 'ji pair:', jipt 7102 7272 ijfound[ipt] = True 7103 7273 Nij = ipt 7104 7274 else: 7105 7275 # Keep searching since there are grid-points not found! 7276 # print ' ' + fname + ': Keep searching since there are grid-points not found!!' 7277 # print ijfound 7106 7278 Nij = dictionary_key(ijfound, False) 7107 7279 if Nij is None: … … 7112 7284 parentNjipt = dictionary_key_list(ijsubflow, Nij) 7113 7285 Njipt = chainSnum_levnext(parentNjipt, ijsubflow.keys()) 7286 # print ' ' + fname + "new sub-flow '" + Njipt + "' !!" 7114 7287 subfinished[Njipt] = False 7115 7288 Nij = np.max(ijfound.keys()) 7116 7289 7290 # print 'Lluis Nij:', Nij 7117 7291 # Looking for which point of the sub-flow retake the search 7118 7292 if Nij == 1: … … 7121 7295 7122 7296 ardtrips = vals_around(trips,jipt) 7297 # print ' ' + fname + 'ardtrips _______' 7298 # print ardtrips 7123 7299 7124 7300 arrive = incomming_flow(ardtrips) 7125 7301 Narrive = np.sum(arrive) 7302 # print Nij, ' ' + fname + ' Narrive:', Narrive 7126 7303 if Narrive == 0: 7127 7304 ijfound[Nij] = True 7128 7305 subfinished[Njipt] = True 7129 7306 else: 7307 # print ' ' + fname + 'arrive _______' 7308 # print arrive 7130 7309 followvals = np.zeros((3,3), dtype=bool) 7131 7310 followvals = arrive … … 7133 7312 7134 7313 for ifollow in range(Narrive): 7314 # print 'ifollow:',ifollow,'/',Narrive 7135 7315 7136 7316 # We only want to work with that ij, which have not yet been found 7137 7317 while np.sum(followvals) != 0: 7138 7139 # Searching direction river-up. 'left': starting from the left, 'right' starting from the right 7140 if direction == 'left': 7141 jifollow = index_mat_way(followvals, True, 'clockwise6') 7142 elif direction == 'right': 7143 jifollow = index_mat_way(followvals, True, 'anticlockwise6') 7144 7145 jiequiv = jifollow[ifollow] - [1,1] 7318 # print Nij,' Looking for a non-located point in subbasin ________' 7319 # print subbasin[jipt[0]-1:jipt[0]+2,jipt[1]-1:jipt[1]+2] 7320 jifollow = index_mat(followvals, True) 7321 jiequiv = jifollow - [1,1] 7146 7322 if subbasin[jipt[0]+jiequiv[0], jipt[1]+jiequiv[1]] == False: 7147 7323 Nij = np.max(ijfound.keys()) + 1 7148 7324 jiptnew = jipt + jiequiv 7149 if ifollow != 0: 7325 if ifollow != 0: 7150 7326 # Avoiding repetition of sub-flow name 7151 7327 if len(ijsubflow.keys()) > 1: 7152 7328 Njipt = chainSnum_levnext(parentNjipt, ijsubflow.keys()) 7153 7329 else: 7154 # First subflow7155 7330 Njipt = '11' 7156 7331 else: 7157 7332 Njipt = parentNjipt 7333 # print ' ' + fname + "new sub-flow '" + Njipt + "' !!" 7158 7334 subfinished[Njipt] = False 7159 7335 subbasin[jipt[0]+jiequiv[0], jipt[1]+jiequiv[1]] = True … … 7172 7348 ijfound[Nij] = True 7173 7349 subfinished[Njipt] = True 7350 # print Nij," subflow '" + Njipt + "' finished!!" 7174 7351 break 7175 7352 … … 7179 7356 'the number of trips', TOTtrips,'!!' 7180 7357 quit() 7358 7359 # if ijsubflow.has_key(Njipt): 7360 # print "Lluis points of subflow: '" + Njipt + "' _______=", ijsubflow[Njipt] 7361 # for isub in ijsubflow[Njipt]: 7362 # print ' ' , isub , ':', ijsubbasin[isub], ijfound[isub] 7363 # if Nij == 10: print 'Nij = 9:', ijfound[9] 7364 7365 # print subbasin 7366 # if Nij > 4: quit() 7367 7368 return subbasin 7369 7370 def subbasin_point(trips, pt, direction): 7371 """ Function to provide sub-basins given a grid point following a matrix of trips 7372 trips= matrix of trips values 7373 1: N, 2: NE, 3: E, 4: SE, 5: S, 6: SW: 7: W, 8: NW 7374 pt= point within the trip matrix 7375 direction= searching direction river-up ('left': clockwise, 'right': anti-clockwise) 7376 """ 7377 fname = 'subbasin_point' 7378 7379 subbasin = np.zeros((trips.shape), dtype=bool) 7380 TOTtrips = len(trips.flatten()) 7381 7382 # Dictionary with the j,i points of all the subbasin 7383 ijsubbasin = {} 7384 # Dictionary to tell if a given j,i sub-basin point has been localized (as number from ijsubbasin) 7385 ijfound = {} 7386 # Dictionary to tell if a given sub-flow has been finished (when Narrive == 0) 7387 subfinished = {} 7388 # Dictionary with the list of points which are contained in a sub-flow 7389 ijsubflow = {} 7390 # Number of ji sub-basin points 7391 Nij = 1 7392 7393 subbasin[pt[0], pt[1]] = True 7394 ijsubbasin[Nij] = np.array(pt) 7395 ijfound[Nij] = False 7396 ijsubflow[str(Nij)] = [Nij] 7397 subfinished[str(Nij)] = False 7398 7399 # Loop around all sub-flows 7400 ## 7401 while np.sum(subfinished.values) != 0 and np.sum(ijfound.values) != 0: 7402 Njipt = dictionary_key(subfinished, False) 7403 if Njipt is not None: 7404 # print Njipt, 'points subflow:', ijsubflow[Njipt] 7405 for ipt in ijsubflow[Njipt]: 7406 if ijfound[ipt] == False: 7407 jipt = ijsubbasin[ipt] 7408 break 7409 # print ' working from point:', ipt, 'ji pair:', jipt 7410 ijfound[ipt] = True 7411 Nij = ipt 7412 else: 7413 # Keep searching since there are grid-points not found! 7414 # print ' ' + fname + ': Keep searching since there are grid-points not found!!' 7415 # print ijfound 7416 Nij = dictionary_key(ijfound, False) 7417 if Nij is None: 7418 return subbasin, ijsubflow, ijsubbasin 7419 7420 ijfound[Nij] = True 7421 jipt = ijsubbasin[Nij] 7422 parentNjipt = dictionary_key_list(ijsubflow, Nij) 7423 Njipt = chainSnum_levnext(parentNjipt, ijsubflow.keys()) 7424 # print ' ' + fname + "new sub-flow '" + Njipt + "' !!" 7425 subfinished[Njipt] = False 7426 Nij = np.max(ijfound.keys()) 7427 7428 # print 'Lluis Nij:', Nij 7429 # Looking for which point of the sub-flow retake the search 7430 if Nij == 1: 7431 ipt = 1 7432 jipt = pt 7433 7434 ardtrips = vals_around(trips,jipt) 7435 # print ' ' + fname + 'ardtrips _______' 7436 # print ardtrips 7437 7438 arrive = incomming_flow(ardtrips) 7439 Narrive = np.sum(arrive) 7440 # print Nij, ' ' + fname + ' Narrive:', Narrive 7441 if Narrive == 0: 7442 ijfound[Nij] = True 7443 subfinished[Njipt] = True 7444 else: 7445 # print ' ' + fname + 'arrive _______' 7446 # print arrive 7447 followvals = np.zeros((3,3), dtype=bool) 7448 followvals = arrive 7449 parentNjipt = Njipt 7450 7451 for ifollow in range(Narrive): 7452 # print 'ifollow:',ifollow,'/',Narrive 7453 7454 # We only want to work with that ij, which have not yet been found 7455 while np.sum(followvals) != 0: 7456 # print Nij,' Looking for a non-located point in subbasin ________' 7457 # print subbasin[jipt[0]-1:jipt[0]+2,jipt[1]-1:jipt[1]+2] 7458 7459 # Searching direction river-up. 'left': starting from the left, 'right' starting from the right 7460 if direction == 'left': 7461 jifollow0 = index_mat_way(followvals, True, 'clockwise6') 7462 elif direction == 'right': 7463 jifollow0 = index_mat_way(followvals, True, 'anticlockwise6') 7464 if len(jifollow0) > 1 and type(jifollow0) == type([2]): 7465 jifollow = jifollow0[0] 7466 else: 7467 jifollow = jifollow0 7468 ## orig ## jifollow = index_mat(followvals, True) 7469 jiequiv = jifollow - [1,1] 7470 if subbasin[jipt[0]+jiequiv[0], jipt[1]+jiequiv[1]] == False: 7471 Nij = np.max(ijfound.keys()) + 1 7472 jiptnew = jipt + jiequiv 7473 if ifollow != 0: 7474 # Avoiding repetition of sub-flow name 7475 if len(ijsubflow.keys()) > 1: 7476 Njipt = chainSnum_levnext(parentNjipt, ijsubflow.keys()) 7477 else: 7478 Njipt = '11' 7479 else: 7480 Njipt = parentNjipt 7481 # print ' ' + fname + "new sub-flow '" + Njipt + "' !!" 7482 subfinished[Njipt] = False 7483 subbasin[jipt[0]+jiequiv[0], jipt[1]+jiequiv[1]] = True 7484 ijfound[Nij] = False 7485 ijsubbasin[Nij] = jiptnew 7486 if ijsubflow.has_key(Njipt): 7487 subflowpts = ijsubflow[Njipt] 7488 subflowpts.append(Nij) 7489 ijsubflow[Njipt] = subflowpts 7490 else: 7491 ijsubflow[Njipt] = [Nij] 7492 break 7493 else: 7494 followvals[jifollow[0],jifollow[1]] = False 7495 if np.sum(followvals) == 0: 7496 ijfound[Nij] = True 7497 subfinished[Njipt] = True 7498 # print Nij," subflow '" + Njipt + "' finished!!" 7499 break 7500 7501 if Nij > TOTtrips: 7502 print errormsg 7503 print ' ' + fname + ': sub-flow point', Nij, 'larger than ' + \ 7504 'the number of trips', TOTtrips,'!!' 7505 quit() 7506 7507 # if ijsubflow.has_key(Njipt): 7508 # print "Lluis points of subflow: '" + Njipt + "' _______=", ijsubflow[Njipt] 7509 # for isub in ijsubflow[Njipt]: 7510 # print ' ' , isub , ':', ijsubbasin[isub], ijfound[isub] 7511 # if Nij == 10: print 'Nij = 9:', ijfound[9] 7512 7513 # print subbasin 7514 # if Nij > 4: quit() 7181 7515 7182 7516 return subbasin … … 7206 7540 #print rivers 7207 7541 #print rivers[point[0], point[1]] 7208 7209 #masksubbasin, subflows, subflowspt = subbasin_point(rivers, point) 7542 #direc = 'left' 7543 7544 #masksubbasin, subflows, subflowspt = subbasin_point_dir(rivers, point, 'right') 7210 7545 #print rivers 7211 7546 #print masksubbasin … … 7214 7549 # for isub in subflows[subflow]: 7215 7550 # print ' ' , isub , ':', subflowspt[isub] 7216 # 7551 7217 7552 #print 'Total sub-basin:', np.sum(masksubbasin) 7553 #quit() 7218 7554 7219 7555 def color_deg(colors, Nsteps, typedeg, values=None): … … 7248 7584 cols[:,icol] = np.array(colors[icol]) 7249 7585 7250 if typedeg == 'std': 7251 Nstepcols = np.int(Nsteps*1. / (Ncols-1.)) 7252 iicol = 0 7253 for icol in range(Ncols-1): 7254 dcolors = (cols[:,icol+1]- cols[:,icol])/(Nstepcols-1.) 7586 if Ncols > Nsteps: 7587 print warnmsg 7588 print ' ' + fname + ': too few steps:', Nsteps, 'for the number of colors:',\ 7589 Ncols,'!!' 7590 print ' directly assigning the given color' 7591 Nfincol = Nsteps 7592 if Nsteps > 1: 7593 for istp in range(Nsteps): 7594 colordeg[0,istp] = istp / (Nsteps-1.) 7595 colordeg[1:4,istp] = cols[:,istp] 7596 else: 7597 colordeg[0,0] = 1. 7598 colordeg[1:4,0] = cols[:,0] 7599 else: 7600 if typedeg == 'std': 7601 Nstepcols = np.int(Nsteps*1. / (Ncols-1.)) 7602 iicol = 0 7603 for icol in range(Ncols-1): 7604 dcolors = (cols[:,icol+1]- cols[:,icol])/(Nstepcols-1.) 7605 # Toavoid repetition of the color 7606 if icol == 0: 7607 ibeg = 0 7608 else: 7609 ibeg = 1 7610 for i in range(ibeg,Nstepcols): 7611 colordeg[0,iicol] = iicol*1. 7612 colordeg[1:4,iicol] = cols[:,icol] + dcolors*i 7613 iicol = iicol + 1 7614 Nfincol = iicol-1 7615 colordeg[0,:] = colordeg[0,:]/(Nfincol*1.) 7616 7617 elif typedeg == 'vals': 7618 if values is None: 7619 print errormsg 7620 print ' ' + fname + ": type '" + typedeg + "' require values !!" 7621 quit(-1) 7622 Nvals = len(values) 7623 if Nvals != Ncols: 7624 print errormsg 7625 print ' ' + fname + ': different number of values:', Nvals, \ 7626 'and colors:', Ncols, '!!' 7627 print ' provided values:', values 7628 print ' provided colors:', cols 7629 quit(-1) 7630 TOTrange = values[Nvals-1] - values[0] 7631 iicol = 0 7632 for icol in range(Ncols-1): 7633 pairange = values[icol+1] - values[icol] 7634 drange = pairange / TOTrange 7635 Nstepcols = np.int(Nsteps*drange) 7636 dcolors = (cols[:,icol+1]-cols[:,icol])/(Nstepcols-1.) 7255 7637 # To avoid repetition of the color 7256 if icol == 0: 7257 ibeg = 0 7258 else: 7259 ibeg = 1 7260 for i in range(ibeg,Nstepcols): 7261 colordeg[0,iicol] = iicol*1. 7262 colordeg[1:4,iicol] = cols[:,icol] + dcolors*i 7263 iicol = iicol + 1 7264 Nfincol = iicol-1 7265 colordeg[0,:] = colordeg[0,:]/(Nfincol*1.) 7266 7267 elif typedeg == 'vals': 7268 if values is None: 7638 if icol == 0: 7639 ibeg = 0 7640 else: 7641 ibeg = 1 7642 for i in range(ibeg,Nstepcols): 7643 colordeg[0,iicol] = values[icol]+i*pairange/(Nstepcols-1) 7644 colordeg[1:4,iicol] = cols[:,icol] + dcolors*i 7645 iicol = iicol + 1 7646 Nfincol = iicol - 1 7647 else: 7269 7648 print errormsg 7270 print ' ' + fname + ": type '" + typedeg + "' require values!!"7649 print ' ' + fname + ": degradation type '" + typedeg + "' not ready !!" 7271 7650 quit(-1) 7272 Nvals = len(values)7273 if Nvals != Ncols:7274 print errormsg7275 print ' ' + fname + ': different number of values:', Nvals, \7276 'and colors:', Ncols, '!!'7277 print ' provided values:', values7278 print ' provided colors:', cols7279 quit(-1)7280 TOTrange = values[Nvals-1] - values[0]7281 iicol = 07282 for icol in range(Ncols-1):7283 pairange = values[icol+1] - values[icol]7284 drange = pairange / TOTrange7285 Nstepcols = np.int(Nsteps*drange)7286 dcolors = (cols[:,icol+1]-cols[:,icol])/(Nstepcols-1.)7287 # To avoid repetition of the color7288 if icol == 0:7289 ibeg = 07290 else:7291 ibeg = 17292 for i in range(ibeg,Nstepcols):7293 colordeg[0,iicol] = values[icol]+i*pairange/(Nstepcols-1)7294 colordeg[1:4,iicol] = cols[:,icol] + dcolors*i7295 iicol = iicol + 17296 Nfincol = iicol - 17297 else:7298 print errormsg7299 print ' ' + fname + ": degradation type '" + typedeg + "' not ready !!"7300 quit(-1)7301 7651 7302 7652 degcolor = np.zeros((4,Nfincol), dtype=np.float) … … 7348 7698 return hexcol 7349 7699 7350 def index_mat_way(mat,val,way):7351 """ Function to look for a value within a matrix following a way and direction7352 mat: matrix7353 val: value to search7354 way: way of search7355 'anticlockwise12': looking in an anti-clockwise direction starting at 127356 'anticlockwise6': looking in an anti-clockwise direction starting at 67357 'clockwise12': looking in an clockwise direction starting at 127358 'clockwise6': looking in an clockwise direction starting at 67359 >>> mat = np.arange(20).reshape(4,5)7360 >>> mat[2,1] = 37361 >>> index_mat_way(mat,3,'clockwise12')7362 [array([0, 3]), array([2, 1])]7363 >>> index_mat_way(mat,3,'anticlockwise12')7364 [array([2, 1]), array([0, 3])]7365 """7366 fname = 'index_mat_way'7367 7368 ways = ['anticlockwise12', 'anticlockwise6', 'clockwise12', 'clockwise6']7369 7370 Ndims = len(mat.shape)7371 dx = mat.shape[Ndims-1]7372 dy = mat.shape[Ndims-2]7373 7374 diffmat = np.abs(mat - val)7375 mindiff = np.min(diffmat)7376 if np.sum(diffmat == mindiff) == 1:7377 ijindex = index_mat(diffmat,mindiff)7378 return ijindex7379 7380 if Ndims > 2: dz = mat.shape[Ndims-3]7381 if Ndims > 3: dt = mat.shape[Ndims-4]7382 7383 if way == 'anticlockwise12':7384 # Sorting following a double criteria, first angle and then distance taken from the7385 # center in the anti-clockwise direction starting at 127386 if Ndims > 2:7387 print errormsg7388 print ' ' + fame + ': wiht more than 2-dims:', Ndims, "'" + way + \7389 "' is not possible !!"7390 quit(-1)7391 7392 distrad = radius_dist(dx,dy,dx/2,dy/2)7393 distangle = radius_angle(dx,dy,dx/2,dy/2)7394 distangle = np.where(distangle >= np.pi, distangle - 2.*np.pi, distangle)7395 distangle = np.where(distangle > 0., 2.* np.pi - distangle, -distangle)7396 7397 sortedij = {}7398 minangle = 10000.7399 notfound = np.zeros((dy,dx), dtype=bool)7400 maskdistrad = ma.array(distrad, mask=notfound)7401 maskdistangle = ma.array(distangle, mask=notfound)7402 for ip in range(dx*dy):7403 minangle = np.min(maskdistangle)7404 jiangle = multi_index_mat(maskdistangle, minangle)7405 mindist = 10000.7406 for ia in range(len(jiangle)):7407 iaji = jiangle[ia]7408 if maskdistrad[iaji[0], iaji[1]] < mindist:7409 mindist = maskdistrad[iaji[0], iaji[1]]7410 idistangle = iaji7411 notfound[idistangle[0],idistangle[1]] = True7412 sortedij[ip] = idistangle7413 7414 elif way == 'anticlockwise6':7415 # Sorting following a double criteria, first angle and then distance taken from the7416 # center in the anti-clockwise direction starting at 67417 if Ndims > 2:7418 print errormsg7419 print ' ' + fame + ': wiht more than 2-dims:', Ndims, "'" + way + \7420 "' is not possible !!"7421 quit(-1)7422 7423 distrad = radius_dist(dx,dy,dx/2,dy/2)7424 distangle = radius_angle(dx,dy,dx/2,dy/2)7425 distangle = np.where(distangle > np.pi, distangle - 2.*np.pi, distangle)7426 distangle = np.where(distangle >= 0., np.pi - distangle, np.pi-distangle)7427 7428 sortedij = {}7429 minangle = 10000.7430 notfound = np.zeros((dy,dx), dtype=bool)7431 maskdistrad = ma.array(distrad, mask=notfound)7432 maskdistangle = ma.array(distangle, mask=notfound)7433 for ip in range(dx*dy):7434 minangle = np.min(maskdistangle)7435 jiangle = multi_index_mat(maskdistangle, minangle)7436 mindist = 10000.7437 for ia in range(len(jiangle)):7438 iaji = jiangle[ia]7439 if maskdistrad[iaji[0], iaji[1]] < mindist:7440 mindist = maskdistrad[iaji[0], iaji[1]]7441 idistangle = iaji7442 notfound[idistangle[0],idistangle[1]] = True7443 sortedij[ip] = idistangle7444 7445 elif way == 'clockwise12':7446 # Sorting following a double criteria, first angle and then distance taken from the7447 # center in the cloc-kwise direction starting at 127448 if Ndims > 2:7449 print errormsg7450 print ' ' + fame + ': wiht more than 2-dims:', Ndims, "'" + way + \7451 "' is not possible !!"7452 quit(-1)7453 7454 distrad = radius_dist(dx,dy,dx/2,dy/2)7455 distangle = radius_angle(dx,dy,dx/2,dy/2)7456 7457 sortedij = {}7458 minangle = 10000.7459 notfound = np.zeros((dy,dx), dtype=bool)7460 maskdistrad = ma.array(distrad, mask=notfound)7461 maskdistangle = ma.array(distangle, mask=notfound)7462 for ip in range(dx*dy):7463 minangle = np.min(maskdistangle)7464 jiangle = multi_index_mat(maskdistangle, minangle)7465 mindist = 10000.7466 for ia in range(len(jiangle)):7467 iaji = jiangle[ia]7468 if maskdistrad[iaji[0], iaji[1]] < mindist:7469 mindist = maskdistrad[iaji[0], iaji[1]]7470 idistangle = iaji7471 notfound[idistangle[0],idistangle[1]] = True7472 sortedij[ip] = idistangle7473 7474 elif way == 'clockwise6':7475 # Sorting following a double criteria, first angle and then distance taken from the7476 # center in the clockwise direction starting at 67477 if Ndims > 2:7478 print errormsg7479 print ' ' + fame + ': wiht more than 2-dims:', Ndims, "'" + way + \7480 "' is not possible !!"7481 quit(-1)7482 7483 distrad = radius_dist(dx,dy,dx/2,dy/2)7484 distangle = radius_angle(dx,dy,dx/2,dy/2)7485 distangle = np.where(distangle >= np.pi, distangle - 2.*np.pi, distangle)7486 distangle = np.where(distangle > 0., np.pi + distangle, np.pi+distangle)7487 7488 sortedij = {}7489 minangle = 10000.7490 notfound = np.zeros((dy,dx), dtype=bool)7491 maskdistrad = ma.array(distrad, mask=notfound)7492 maskdistangle = ma.array(distangle, mask=notfound)7493 for ip in range(dx*dy):7494 minangle = np.min(maskdistangle)7495 jiangle = multi_index_mat(maskdistangle, minangle)7496 mindist = 10000.7497 for ia in range(len(jiangle)):7498 iaji = jiangle[ia]7499 if maskdistrad[iaji[0], iaji[1]] < mindist:7500 mindist = maskdistrad[iaji[0], iaji[1]]7501 idistangle = iaji7502 notfound[idistangle[0],idistangle[1]] = True7503 sortedij[ip] = idistangle7504 7505 else:7506 print errormsg7507 print ' ' + fname + ": way of search '" + way + "' not ready !!"7508 print ' available ways:', ways7509 7510 indices = []7511 for ij in range(dx*dy):7512 sij = sortedij[ij]7513 sval = mat[sij[0], sij[1]]7514 if sval == val:7515 indices.append(sij)7516 7517 return indices7518 7519 7700 #quit() 7520 7701
Note: See TracChangeset
for help on using the changeset viewer.