1 | def most2nc(input_file=None,output_file=None,inverted_bathymetry = False,\ |
---|
2 | verbose = True): |
---|
3 | #input_file = 'small.txt' |
---|
4 | #output_file = 'small_e.nc' |
---|
5 | |
---|
6 | long_name = 'LON' |
---|
7 | lat_name = 'LAT' |
---|
8 | if inverted_bathymetry: |
---|
9 | up = -1. |
---|
10 | else: |
---|
11 | up = +1. |
---|
12 | |
---|
13 | from Scientific.IO.NetCDF import NetCDFFile |
---|
14 | import sys |
---|
15 | |
---|
16 | try: |
---|
17 | if input_file is None: |
---|
18 | input_file = sys.argv[1] |
---|
19 | if output_file is None: |
---|
20 | output_file = sys.argv[2] |
---|
21 | except: |
---|
22 | raise 'usage is: most2nc input output' |
---|
23 | |
---|
24 | in_file = open(input_file,'r') |
---|
25 | if verbose: print 'reading header' |
---|
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())) |
---|
33 | |
---|
34 | h2_list=[] |
---|
35 | for j in range(ny): |
---|
36 | h2_list.append(float(in_file.readline())) |
---|
37 | |
---|
38 | h2_list.reverse() |
---|
39 | |
---|
40 | if verbose: print 'reading depths' |
---|
41 | |
---|
42 | in_depth_list = in_file.readlines() |
---|
43 | in_file.close() |
---|
44 | |
---|
45 | out_depth_list = [[]] |
---|
46 | |
---|
47 | if verbose: print 'processing depths' |
---|
48 | |
---|
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 |
---|
60 | |
---|
61 | in_file.close() |
---|
62 | out_depth_list.reverse() |
---|
63 | depth_list = out_depth_list |
---|
64 | |
---|
65 | if verbose: print 'writing results' |
---|
66 | |
---|
67 | out_file = NetCDFFile(output_file,'w') |
---|
68 | |
---|
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) |
---|
74 | |
---|
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) |
---|
80 | |
---|
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) |
---|
85 | |
---|
86 | out_file.close() |
---|