1 | """This has to do with creating elevation data files for use with ferret2sww. |
---|
2 | It reads a bathymetry ascii file and creates a NetCDF (nc) file similar to |
---|
3 | MOSTs output. |
---|
4 | |
---|
5 | $Author: Peter Row |
---|
6 | |
---|
7 | """ |
---|
8 | |
---|
9 | import sys |
---|
10 | from anuga.file.netcdf import NetCDFFile |
---|
11 | from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a |
---|
12 | import anuga.utilities.log as log |
---|
13 | |
---|
14 | |
---|
15 | def most2nc(input_file, output_file, inverted_bathymetry=False, verbose=True): |
---|
16 | """Convert a MOST file to NetCDF format. |
---|
17 | |
---|
18 | input_file the input file to convert |
---|
19 | output_file the name of the oputput NetCDF file to create |
---|
20 | inverted_bathymetry ?? |
---|
21 | verbose True if the function is to be verbose |
---|
22 | """ |
---|
23 | |
---|
24 | # variable names |
---|
25 | long_name = 'LON' |
---|
26 | lat_name = 'LAT' |
---|
27 | elev_name = 'ELEVATION' |
---|
28 | |
---|
29 | # set up bathymetry |
---|
30 | if inverted_bathymetry: |
---|
31 | up = -1. |
---|
32 | else: |
---|
33 | up = +1. |
---|
34 | |
---|
35 | # read data from the MOST file |
---|
36 | in_file = open(input_file,'r') |
---|
37 | |
---|
38 | if verbose: log.critical('reading header') |
---|
39 | |
---|
40 | nx_ny_str = in_file.readline() |
---|
41 | nx_str,ny_str = nx_ny_str.split() |
---|
42 | nx = int(nx_str) |
---|
43 | ny = int(ny_str) |
---|
44 | h1_list=[] |
---|
45 | for i in range(nx): |
---|
46 | h1_list.append(float(in_file.readline())) |
---|
47 | |
---|
48 | h2_list=[] |
---|
49 | for j in range(ny): |
---|
50 | h2_list.append(float(in_file.readline())) |
---|
51 | |
---|
52 | h2_list.reverse() |
---|
53 | |
---|
54 | if verbose: log.critical('reading depths') |
---|
55 | |
---|
56 | in_depth_list = in_file.readlines() |
---|
57 | in_file.close() |
---|
58 | |
---|
59 | out_depth_list = [[]] |
---|
60 | |
---|
61 | if verbose: log.critical('processing depths') |
---|
62 | |
---|
63 | k=1 |
---|
64 | for in_line in in_depth_list: |
---|
65 | for string in in_line.split(): |
---|
66 | #j = k/nx |
---|
67 | out_depth_list[(k-1)/nx].append(float(string)*up) |
---|
68 | if k==nx*ny: |
---|
69 | break |
---|
70 | if k-(k/nx)*nx ==0: |
---|
71 | out_depth_list.append([]) |
---|
72 | k+=1 |
---|
73 | |
---|
74 | in_file.close() |
---|
75 | out_depth_list.reverse() |
---|
76 | depth_list = out_depth_list |
---|
77 | |
---|
78 | # write the NetCDF file |
---|
79 | if verbose: log.critical('writing results') |
---|
80 | |
---|
81 | out_file = NetCDFFile(output_file, netcdf_mode_w) |
---|
82 | |
---|
83 | out_file.createDimension(long_name,nx) |
---|
84 | |
---|
85 | out_file.createVariable(long_name,'d',(long_name,)) |
---|
86 | out_file.variables[long_name].point_spacing='uneven' |
---|
87 | out_file.variables[long_name].units='degrees_east' |
---|
88 | out_file.variables[long_name][:] = h1_list |
---|
89 | |
---|
90 | out_file.createDimension(lat_name,ny) |
---|
91 | out_file.createVariable(lat_name,'d',(lat_name,)) |
---|
92 | out_file.variables[lat_name].point_spacing='uneven' |
---|
93 | out_file.variables[lat_name].units='degrees_north' |
---|
94 | out_file.variables[lat_name][:] = h2_list |
---|
95 | |
---|
96 | out_file.createVariable(elev_name,'d',(lat_name,long_name)) |
---|
97 | out_file.variables[elev_name].point_spacing='uneven' |
---|
98 | out_file.variables[elev_name].units='meters' |
---|
99 | out_file.variables[elev_name][:] = depth_list |
---|
100 | |
---|
101 | out_file.close() |
---|