Changeset 624 in lmdz_wrf
- Timestamp:
- Sep 8, 2015, 12:26:44 PM (10 years ago)
- Location:
- trunk/tools
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r623 r624 874 874 return valpos 875 875 876 def addfileInfile(origfile,destfile,addfile,addsign): 877 """ Function to add the content of a file [addfile] to another one [origfile] at 878 a given position [addsign] creating a new file called [destfile] 879 origfile= original file 880 destfile= destination file 881 addfile= content of the file to add 882 addsign= sign where to add the file 883 """ 884 fname = 'addfileInfile' 885 886 if not os.path.isfile(origfile): 887 print errormsg 888 print ' ' + fname + ": original file '" + origfile + "' does not exist !!" 889 quit() 890 891 if not os.path.isfile(addfile): 892 print errormsg 893 print ' ' + fname + ": adding file '" + addfile + "' does not exist !!" 894 quit() 895 896 ofile = open(origfile, 'r') 897 898 # Inspecting flag 899 ## 900 Nfound = 0 901 for line in ofile: 902 if line == addsign + '\n': Nfound = Nfound + 1 903 904 if Nfound == 0: 905 print errormsg 906 print ' ' + fname + ": adding sign '" + addsign + "' not found !!" 907 quit() 908 print ' '+ fname + ': found', Nfound," times sign '" + addsign + "' " 909 910 ofile.seek(0) 911 dfile = open(destfile, 'w') 912 913 for line in ofile: 914 if line != addsign + '\n': 915 dfile.write(line) 916 else: 917 afile = open(addfile,'r') 918 for aline in afile: 919 print aline 920 dfile.write(aline) 921 922 afile.close() 923 924 ofile.close() 925 dfile.close() 926 927 print main + " successful writting of file '" + destfile + "' !!" 928 return 929 930 876 931 ####### ###### ##### #### ### ## # 877 932 … … 16388 16443 return fvalues 16389 16444 16445 def values_line(line, splitv, chars): 16446 """ Function to retrieve values from a line of ASCII text removing a series of characters 16447 line= line of ASCII text to work with 16448 splitv= character to use to split with the line 16449 chars= list of characters/strings to remove 16450 >>> 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 vals 16465 16390 16466 def reduce_last_spaces(string): 16391 16467 """ Function to reduce the last right spaces from a string … … 16414 16490 16415 16491 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 function 16496 Npt= Number of points on each grid 16497 maxradii= allowed maximum radii 16498 '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 errormsg 16504 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:',line 16514 values = values_line(line, ' ', ['[', ']']) 16515 rad = np.float(values[0]) 16516 # Limited by a given radii 16517 if maxradii is not None and rad > maxradii: break 16518 Nrad = int(values[2]) 16519 couples = None 16520 Nradgood = 0 16521 if Nrad > 1: 16522 Ncouples = 0 16523 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 + 1 16530 elif ix > Npt and iy > Npt: break 16531 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 + 1 16537 elif ix > Npt and iy > Npt: break 16538 16539 if Nradgood > 0: 16540 Nradii[rad] = Nradgood 16541 gridradii[rad] = np.array(couples) 16542 16543 for rad in sorted(gridradii.keys()): 16544 print rad, gridradii[rad] 16545 16546 return gridradii 16547 16548 def squared_radial(Npt): 16549 """ Function to provide the series of radii as composite of pairs (x,y) of gid cells 16550 Npt= largest amount of grid points on x and y directions 16551 >>> squared_radial(5) 16552 [[0.0, 0, 0], [1.0, 1, 0], [1.4142135623730951, 1, 1], [2.0, 2, 0], [2.2360679774997898, 2, 1], [2.8284271247461903, 2, 2], [3.0, 3, 0], [3.1622776601683795, 3, 1], [3.6055512754639891, 3, 2], [4.0, 4, 0], [4.1231056256176606, 4, 1], [4.2426406871192848, 3, 3], [4.4721359549995796, 4, 2], [5.0, array([4, 3]), array([5, 0])], [5.0990195135927845, 5, 1], [5.3851648071345037, 5, 2], [5.6568542494923806, 4, 4], [5.8309518948453007, 5, 3], [6.4031242374328485, 5, 4], [7.0710678118654755, 5, 5]] 16553 """ 16554 fname = 'squared_radial' 16555 16556 radii = [] 16557 gridradii = {} 16558 singradii = [] 16559 Nradii = {} 16560 16561 # Computing all radii 16562 ## 16563 for i in range(Npt+1): 16564 if np.mod(i,100) == 0: print 'done: ',i 16565 for j in range(i+1): 16566 # ir = np.float(i) 16567 # jr = np.float(j) 16568 ir = np.float64(i) 16569 jr = np.float64(j) 16570 rad = np.sqrt(ir*ir + jr*jr) 16571 if not searchInlist(singradii, rad): 16572 singradii.append(rad) 16573 gridradii[rad] = np.array([i,j], dtype=int) 16574 Nradii[rad] = 1 16575 else: 16576 # print warnmsg 16577 # print ' ' + fname + ': repeated radii',rad,'!!' 16578 # print ' Previous values:', gridradii[rad] 16579 16580 oldvalues = gridradii[rad] 16581 Nradii[rad] = Nradii[rad] + 1 16582 newvalues = np.zeros((Nradii[rad],2), dtype=int) 16583 if Nradii[rad] == 2: 16584 newvalues[0,:] = oldvalues 16585 Nstart = 1 16586 else: 16587 Nstart = 0 16588 for Nr in range(Nstart,Nradii[rad]-1): 16589 newvalues[Nr,:] = oldvalues[Nr,:] 16590 newvalues[Nradii[rad]-1,:] = np.array([i,j], dtype=int) 16591 gridradii[rad] = newvalues 16592 16593 # Sorting values 16594 ## 16595 ## Only required to downwrite the output file 16596 # radiif = open('SQradii.dat', 'w') 16597 # radii = [] 16598 # prevrad = 0. 16599 # for rad in sorted(gridradii.keys()): 16600 # dradii = rad - prevrad 16601 ## Check fo repeated values 16602 # radii.append([rad, gridradii[rad][0], gridradii[rad][1]]) 16603 # radiif.write(str(rad) + ' ' + str(dradii) + ' ' + str(Nradii[rad]) + ' [ '+ \ 16604 # numVector_String(gridradii[rad].flatten(), ' ') + ' ] \n') 16605 # prevrad = rad.copy() 16606 16607 # radiif.close() 16608 return gridradii 16416 16609 16417 16610 def getvalues_lonlat(values, ncfile):
Note: See TracChangeset
for help on using the changeset viewer.