- Timestamp:
- Sep 14, 2015, 6:56:14 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r627 r628 13 13 fillValueC = '-' 14 14 fillValueI = -99999 15 16 def values_line(line, splitv, chars): 17 """ Function to retrieve values from a line of ASCII text removing a series of characters 18 line= line of ASCII text to work with 19 splitv= character to use to split with the line 20 chars= list of characters/strings to remove 21 >>> values_line('0.0 0.0 1 [ 0 0 ]', ' ', ['[', ']']) 22 ['0.0', '0.0', '1', '0', '0'] 23 """ 24 fname = 'values_line' 25 26 vals = line.replace('\n', '') 27 for ic in chars: 28 vals = vals.replace(ic, '') 29 30 vals0 = vals.split(splitv) 31 vals = [] 32 for val in vals0: 33 if not len(val) == 0: vals.append(val) 34 35 return vals 15 36 16 37 def filexist(filen, emsg, fmsg): … … 15155 15176 return radpos 15156 15177 15178 def read_SQradii(filename='SQradii.dat', Npt=-1, maxradii=None): 15179 """ Function to read the outcome of the function `squared_radial' 15180 filename= Name of the outcome file of the function 15181 Npt= Number of points on each grid 15182 maxradii= allowed maximum radii 15183 'SQradii.dat': file with the radii and paris of integers after ran squared_radial(1000) 15184 """ 15185 fname = 'read_SQradii' 15186 15187 if not os.path.isfile(filename): 15188 print errormsg 15189 print ' ' + fname + ": file '" + filename + "' not found!!" 15190 quit(-1) 15191 15192 infile = open(filename, 'r') 15193 15194 gridradii = {} 15195 Nradii = {} 15196 radii = [] 15197 for line in infile: 15198 values = values_line(line, ' ', ['[', ']']) 15199 rad = np.float(values[0]) 15200 # Limited by a given radii 15201 if maxradii is not None and rad > maxradii: break 15202 Nrad = int(values[2]) 15203 couples = None 15204 Nradgood = 0 15205 if Nrad > 1: 15206 Ncouples = 0 15207 couples = [] 15208 for ic in range(Nrad): 15209 ix = int(values[3 + ic*2]) 15210 iy = int(values[3 + ic*2 + 1]) 15211 if ix <= Npt and iy <= Npt: 15212 couples.append(np.array([ix,iy], dtype=int)) 15213 Nradgood = Nradgood + 1 15214 elif ix > Npt and iy > Npt: break 15215 else: 15216 ix = int(values[3]) 15217 iy = int(values[4]) 15218 if ix <= Npt and iy <= Npt: 15219 couples = np.array([ix,iy], dtype=int) 15220 Nradgood = Nradgood + 1 15221 elif ix > Npt and iy > Npt: break 15222 15223 if Nradgood > 0: 15224 Nradii[rad] = Nradgood 15225 gridradii[rad] = np.array(couples) 15226 15227 # for rad in sorted(gridradii.keys()): 15228 # print rad, gridradii[rad] 15229 15230 return gridradii 15231 15157 15232 def grid_combinations(x,y): 15158 15233 """ Function to provide all the possible grid points combination for a given pair of values … … 15188 15263 fname = 'radii_points' 15189 15264 15190 radiipos = np.zeros((Nang le, radii, 2), dtype = int)15265 radiipos = np.zeros((Nang, Lrad, 2), dtype = int) 15191 15266 SQradiipos = {} 15192 15267 … … 15199 15274 for ia in range(Nang): 15200 15275 angle = 2.*np.pi*ia/Nang 15276 posangle = np.zeros((Lrad,2), dtype=np.float) 15201 15277 15202 15278 # Points at that given angle 15203 pangle = radial_points(angle, radii)15279 pangle = radial_points(angle,Lrad) 15204 15280 posangle[:,0] = pangle[:,0] + xpos 15205 15281 posangle[:,1] = pangle[:,1] + ypos 15206 15282 15207 for ip in range( radii):15283 for ip in range(Lrad): 15208 15284 iipos = int(posangle[ip,0]) 15209 15285 jjpos = int(posangle[ip,1]) 15210 15286 15211 15287 # Creation of a matrix with all the grid points at each time-step 15212 if iipos >= 0 and iipos < dx and jjpos >= 0 and jjpos < dy:15288 if iipos >= 0 and iipos < dx-1 and jjpos >= 0 and jjpos < dy-1: 15213 15289 radiipos[ia,ip,0] = iipos 15214 15290 radiipos[ia,ip,1] = jjpos … … 15222 15298 15223 15299 # pairs loop 15300 Npairs = len(pairs.flatten())/2 15224 15301 pairsw = [] 15225 for ip in range(len(pairs)): 15226 allpairs = grid_combinations(pairs[ip*2],pairs[ip*2+1]) 15227 15302 print 'Lluis ',SQradii[ir],'pairs:',pairs, type([pairs]) 15303 for ip in range(Npairs): 15304 if Npairs == 0: 15305 print ' Lluis NO pair:', SQradii[ir],':',pairs 15306 break 15307 elif Npairs == 1: 15308 print ' Lluis single pair:',pairs[ip*2],pairs[ip*2+1] 15309 allpairs = grid_combinations(pairs[ip*2],pairs[ip*2+1]) 15310 else: 15311 print ip,' Lluis 1 pair:',pairs[ip,0],pairs[ip,1] 15312 allpairs = grid_combinations(pairs[ip,0],pairs[ip,1]) 15313 15228 15314 for iap in range(len(allpairs)): 15229 15315 ppos = allpairs[iap] 15230 iipos = trajvals[it,1]+ ppos[0]15231 jjpos = trajvals[it,1] + ppos[0]15316 iipos = xpos + ppos[0] 15317 jjpos = ypos + ppos[1] 15232 15318 15233 15319 # Creation of a matrix with all the grid points at each time-step … … 15317 15403 dimt = objfile.variables[timn].shape 15318 15404 15319 if Tbeg + Ttraj > dimt: 15320 print errormsg 15321 print ' ' + fname + ': trajectory has ', Ttraj, ' time steps and starts ' + \ 15322 ' at',Tbeg,' and data',varobj.shape[0], '" !!!!!' 15323 quit(-1) 15405 # Removing this checking 15406 # if Tbeg + Ttraj > dimt: 15407 # print errormsg 15408 # print ' ' + fname + ': trajectory has ', Ttraj, ' time steps and starts ' + \ 15409 # ' at',Tbeg,' and data',varobj.shape[0], '" !!!!!' 15410 # quit(-1) 15324 15411 15325 15412 print ' ' + fname + ': Number of time-steps in trajectory file: ',Ttraj … … 15342 15429 it = 0 15343 15430 15344 trajpos = np.zeros(Ttraj,Nangle,raddi) 15431 trajpos = np.zeros((Ttraj,Nangle,radii), dtype=np.float) 15432 gtrajvals = np.zeros((Ttraj,3), dtype=int) 15433 trajvals = np.zeros((Ttraj,3), dtype=np.float) 15345 15434 15346 15435 # Grid position at each time-step and angle 15347 varvalst = np.ones((Traj, Nangle, radii, 2), dtype=np.float)*fillValue15348 varvalstSQ = np.zeros((Traj, radii, radii), dtype=bool)15436 # varvalst = np.ones((Ttraj, Nangle, radii, 2), dtype=np.float)*fillValue 15437 # varvalstSQ = np.zeros((Ttraj, radii, radii), dtype=bool) 15349 15438 15350 15439 it = 0 … … 16036 16125 16037 16126 compute_tevolboxtraj_radialsec('/bdd/PCER/workspace/lfita/etudes/FF/medic051215_2km/run/trajectory.dat@6,XLONG,XLAT,ZNU,Times,wrf,4,10', \ 16038 ' 001/full_concatenated.nc', 'U10')16127 '/bdd/PCER/workspace/lfita/etudes/FF/medic051215_2km/run/wrfout/wrfout_d02_2005-12-12_00:00:00', 'U10') 16039 16128 quit() 16040 16129 … … 16443 16532 return fvalues 16444 16533 16445 def values_line(line, splitv, chars):16446 """ Function to retrieve values from a line of ASCII text removing a series of characters16447 line= line of ASCII text to work with16448 splitv= character to use to split with the line16449 chars= list of characters/strings to remove16450 >>> values_line('0.0 0.0 1 [ 0 0 ]', ' ', ['[', ']'])16451 ['0.0', '0.0', '1', '0', '0']16452 """16453 fname = 'values_line'16454 16455 vals = line.replace('\n', '')16456 for ic in chars:16457 vals = vals.replace(ic, '')16458 16459 vals0 = vals.split(splitv)16460 vals = []16461 for val in vals0:16462 if not len(val) == 0: vals.append(val)16463 16464 return vals16465 16466 16534 def reduce_last_spaces(string): 16467 16535 """ Function to reduce the last right spaces from a string … … 16490 16558 16491 16559 return newstr 16492 16493 def read_SQradii(filename='SQradii.dat', Npt=1000, maxradii=None):16494 """ Function to read the outcome of the function `squared_radial'16495 filename= Name of the outcome file of the function16496 Npt= Number of points on each grid16497 maxradii= allowed maximum radii16498 'SQradii.dat': file with the radii and paris of integers after ran squared_radial(1000)16499 """16500 fname = 'read_SQradii'16501 16502 if not os.path.isfile(filename):16503 print errormsg16504 print ' ' + fname + ": file '" + filename + "' not found!!"16505 quit(-1)16506 16507 infile = open(filename, 'r')16508 16509 gridradii = {}16510 Nradii = {}16511 radii = []16512 for line in infile:16513 print 'line:',line16514 values = values_line(line, ' ', ['[', ']'])16515 rad = np.float(values[0])16516 # Limited by a given radii16517 if maxradii is not None and rad > maxradii: break16518 Nrad = int(values[2])16519 couples = None16520 Nradgood = 016521 if Nrad > 1:16522 Ncouples = 016523 couples = []16524 for ic in range(Nrad):16525 ix = int(values[3 + ic*2])16526 iy = int(values[3 + ic*2 + 1])16527 if ix <= Npt and iy <= Npt:16528 couples.append(np.array([ix,iy], dtype=int))16529 Nradgood = Nradgood + 116530 elif ix > Npt and iy > Npt: break16531 else:16532 ix = int(values[3])16533 iy = int(values[4])16534 if ix <= Npt and iy <= Npt:16535 couples = np.array([ix,iy], dtype=int)16536 Nradgood = Nradgood + 116537 elif ix > Npt and iy > Npt: break16538 16539 if Nradgood > 0:16540 Nradii[rad] = Nradgood16541 gridradii[rad] = np.array(couples)16542 16543 for rad in sorted(gridradii.keys()):16544 print rad, gridradii[rad]16545 16546 return gridradii16547 16560 16548 16561 def squared_radial(Npt):
Note: See TracChangeset
for help on using the changeset viewer.