source: anuga_core/source/anuga/shallow_water/most2nc.py @ 6086

Last change on this file since 6086 was 6086, checked in by rwilson, 15 years ago

Changes to handle large files when Scientific.IO.NetCDF provides the feature.

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