[1123] | 1 | def most2nc(input_file=None,output_file=None,inverted_bathymetry = False,\ |
---|
| 2 | verbose = True): |
---|
[1122] | 3 | #input_file = 'small.txt' |
---|
| 4 | #output_file = 'small_e.nc' |
---|
[1120] | 5 | |
---|
[1122] | 6 | long_name = 'LON' |
---|
| 7 | lat_name = 'LAT' |
---|
| 8 | if inverted_bathymetry: |
---|
[1124] | 9 | up = -1. |
---|
[1123] | 10 | else: |
---|
[1124] | 11 | up = +1. |
---|
| 12 | |
---|
[1122] | 13 | from Scientific.IO.NetCDF import NetCDFFile |
---|
| 14 | import sys |
---|
[1124] | 15 | |
---|
[1122] | 16 | try: |
---|
[1124] | 17 | if input_file is None: |
---|
| 18 | input_file = sys.argv[1] |
---|
| 19 | if output_file is None: |
---|
| 20 | output_file = sys.argv[2] |
---|
[1122] | 21 | except: |
---|
[1124] | 22 | raise 'usage is: most2nc input output' |
---|
| 23 | |
---|
[1122] | 24 | in_file = open(input_file,'r') |
---|
[1123] | 25 | if verbose: print 'reading header' |
---|
[1122] | 26 | nx_ny_str = in_file.readline() |
---|
| 27 | nx_str,ny_str = nx_ny_str.split() |
---|
| 28 | nx = int(nx_str) |
---|
| 29 | ny = int(ny_str) |
---|
| 30 | h1_list=[] |
---|
| 31 | for i in range(nx): |
---|
| 32 | h1_list.append(float(in_file.readline())) |
---|
[1120] | 33 | |
---|
[1122] | 34 | h2_list=[] |
---|
| 35 | for j in range(ny): |
---|
| 36 | h2_list.append(float(in_file.readline())) |
---|
[1120] | 37 | |
---|
[1122] | 38 | h2_list.reverse() |
---|
[1120] | 39 | |
---|
[1123] | 40 | if verbose: print 'reading depths' |
---|
[1124] | 41 | |
---|
[1122] | 42 | in_depth_list = in_file.readlines() |
---|
| 43 | in_file.close() |
---|
[1120] | 44 | |
---|
[1122] | 45 | out_depth_list = [[]] |
---|
[1120] | 46 | |
---|
[1123] | 47 | if verbose: print 'processing depths' |
---|
[1124] | 48 | |
---|
[1122] | 49 | k=1 |
---|
| 50 | for in_line in in_depth_list: |
---|
| 51 | for string in in_line.split(): |
---|
| 52 | #j = k/nx |
---|
| 53 | out_depth_list[(k-1)/nx].append(float(string)*up) |
---|
| 54 | #print k,len(out_depth_list),(k-1)/nx,out_depth_list[(k-1)/nx][-1],len(out_depth_list[(k-1)/nx]) |
---|
| 55 | if k==nx*ny: |
---|
| 56 | break |
---|
| 57 | if k-(k/nx)*nx ==0: |
---|
| 58 | out_depth_list.append([]) |
---|
| 59 | k+=1 |
---|
[1120] | 60 | |
---|
[1122] | 61 | in_file.close() |
---|
| 62 | out_depth_list.reverse() |
---|
| 63 | depth_list = out_depth_list |
---|
[1120] | 64 | |
---|
[1123] | 65 | if verbose: print 'writing results' |
---|
[1124] | 66 | |
---|
[1122] | 67 | out_file = NetCDFFile(output_file,'w') |
---|
[1120] | 68 | |
---|
[1122] | 69 | out_file.createDimension(long_name,nx) |
---|
| 70 | out_file.createVariable(long_name,'d',(long_name,)) |
---|
| 71 | out_file.variables[long_name].point_spacing='uneven' |
---|
| 72 | out_file.variables[long_name].units='degrees_east' |
---|
| 73 | out_file.variables[long_name].assignValue(h1_list) |
---|
[1120] | 74 | |
---|
[1122] | 75 | out_file.createDimension(lat_name,ny) |
---|
| 76 | out_file.createVariable(lat_name,'d',(lat_name,)) |
---|
| 77 | out_file.variables[lat_name].point_spacing='uneven' |
---|
| 78 | out_file.variables[lat_name].units='degrees_north' |
---|
| 79 | out_file.variables[lat_name].assignValue(h2_list) |
---|
[1120] | 80 | |
---|
[1122] | 81 | out_file.createVariable('ELEVATION','d',(lat_name,long_name)) |
---|
| 82 | out_file.variables['ELEVATION'].point_spacing='uneven' |
---|
| 83 | out_file.variables['ELEVATION'].units='meters' |
---|
| 84 | out_file.variables['ELEVATION'].assignValue(depth_list) |
---|
[1120] | 85 | |
---|
[1122] | 86 | out_file.close() |
---|