source: anuga_work/development/extras/bath2most.py @ 4605

Last change on this file since 4605 was 4540, checked in by ole, 17 years ago

Moved more irrelevant stuff from the anuga distribution to the work area

File size: 7.7 KB
Line 
1#y_min = -35.07 #100m Woolongong
2#y_max = -34.+(-10./.6)*0.01
3#x_min = 150.+(+42./.6)*0.01
4#x_max = 151.6
5
6y_min = -35 #100m Botany Bay
7y_max = -33.8
8x_min = 150.+(+50./.6)*0.01
9x_max = 152.4
10
11#y_min = -34.15233 #10m Botany Bay
12#y_max = -33.8950794787
13#x_min = 151.031182
14#x_max = 151.364182675
15
16
17
18#x_min=150+.88# Port Kembla 10m
19#x_max=150+.575/.6
20#y_min=-34.484
21#y_max=-34-0.20/.6
22
23
24#x_min=150+.525/.6 # Port Kembla 50m
25#x_max=150+.575/.6
26#y_min=-34-0.287/.6
27#y_max=-34-0.23/.6
28
29#x_min=150+.525/.6 # Port Kembla
30#x_max=150+.555/.6
31#y_min=-34-0.287/.6
32#y_max=-34-0.27/.6
33
34#x_min=150+.538/.6 # Flagstaff Point
35#x_max=150+.56/.6
36#y_min=-34-0.265/.6
37#y_max=-34-0.245/.6
38
39
40#x_min=150+.538/.6 # Woolongong Beach
41#x_max=150+.555/.6
42#y_min=-34-.27/.6
43#y_max=-34-.26/.6
44
45
46thin_factor = 2
47add_tide = True
48tide_height = 1.1
49clean_bad_points = True
50no_data_value=-1000000
51#if clean_bad_points is False, bad_points will just be
52#set to no_data_value
53
54crop =True
55#crop =False
56invert_bathymetry = True
57
58input_file = 'dem_geog.asc'
59#output_file = 'wool_200m.txt'
60output_file = 'Botany_Bay_200m.txt'
61
62#input_file = 'wollg_geo_r.asc'
63#output_file = 'Port_Kembla_10m.txt'
64
65#input_file = 'bb_geog.asc'
66#output_file = 'Botany_Bay_10m.txt'
67
68#################################################
69#END OF INPUT
70
71
72
73
74"""
75Takes a bathymetry file of format:
76""
77ncols         1333
78nrows         1030
79xllcorner     151.031182
80yllcorner     34.15233
81cellsize      0.00025000050663948
82NODATA_value  -9999
8356.3 55.36667 54.1 53.03333 52.2 ... (row n)
8454.18333 53.11667 52.26667 51.6 51.33334 ... (row n-1)
85...
8654.18333 53.11667 52.26667 51.6 51.33334 ... (row 1)
87""
88
89and makes it format:
90""
91nx ny (ncols=longitude_range,nrows=latitude_range)
92151.031182(longitude 1)
93151.031182+cellsize(longitude 2)
94151.031182+2*cellsize(longitude 3)
95...
96151.031182+(n-1)*cellsize(longitude n)
9734.1523+cellsize(latitude 1)
9834.1523+2*cellsize(latitude 2)
9934.1523+3*cellsize(latitude 3)
100...
10134.1523+(n-1)*cellsize(latitude n)
10256.3 55.36667 54.1 53.03333 52.2 (row 1)
10354.18333 53.11667 52.26667 51.6 51.33334 (row 2)
104...
10554.18333 53.11667 52.26667 51.6 51.33334 (n)
106
107""
108If llcorner is negative for longitude then it will
109then it will automatically change the x increment to -cellsize
110
111Options will include cropping and thinning and finding values
112for NANs.
113
114NB it assumes that the system has the memory to cope with any
115give row or column, or the cropped bathymetry, but not nessesarily
116the whole bathymetry.
117"""
118
119thin_factor = int(thin_factor)
120#from Numeric import allclose
121
122in_file = open(input_file,'r')
123
124nx_in = in_file.readline()
125nx_in = nx_in.split()[1]
126nx_in= int(nx_in)
127
128ny_in = in_file.readline()
129ny_in = ny_in.split()[1]
130ny_in = int(ny_in)
131
132x_min_in = in_file.readline()
133x_min_in = x_min_in.split()[1]
134x_min_in = float(x_min_in)
135y_min_in = in_file.readline()
136y_min_in = y_min_in.split()[1]
137y_min_in = float(y_min_in)
138
139cellsize = in_file.readline()
140cellsize = cellsize.split()[1]
141cellsize = float(cellsize)
142
143nodata = in_file.readline()
144nodata = nodata.split()[1]
145nodata = float(nodata)
146
147x_inc_in = cellsize
148y_inc_in = cellsize
149
150x_max_in = x_min_in+(nx_in-1)*x_inc_in
151y_max_in = y_min_in+(ny_in-1)*y_inc_in
152print 'minimum possible x =',x_min_in
153print 'maximum possible x =',x_max_in
154print 'minimum possible y =',y_min_in
155print 'maximum possible y =',y_max_in
156
157x_inc = x_inc_in*thin_factor
158y_inc = y_inc_in*thin_factor
159
160if crop:
161    x_min = x_min_in+x_inc*int((x_min-x_min_in)/x_inc)
162    x_max = x_min_in+x_inc*int((x_max-x_min_in)/x_inc)
163    y_min = y_min_in+y_inc*int((y_min-y_min_in)/y_inc)
164    y_max = y_min_in+y_inc*int((y_max-y_min_in)/y_inc)
165else:
166    x_min = x_min_in
167    x_max = x_max_in
168    y_min = y_min_in
169    y_max = y_max_in
170    x_min = x_min_in+x_inc*int((x_min-x_min_in)/x_inc)
171    x_max = x_min_in+x_inc*int((x_max-x_min_in)/x_inc)
172    y_min = y_min_in+y_inc*int((y_min-y_min_in)/y_inc)
173    y_max = y_min_in+y_inc*int((y_max-y_min_in)/y_inc)
174
175assert not x_min<x_min_in
176assert not y_min<y_min_in
177assert not x_max>x_max_in
178assert not y_max>y_max_in
179assert not x_min>x_max
180assert not y_min>y_max
181
182nx = int(round((x_max-x_min)/x_inc+1))
183ny = int(round((y_max-y_min)/y_inc+1))
184
185x_max = x_min+(nx-1)*x_inc
186y_max = y_min+(ny-1)*y_inc
187
188print 'x_min =',x_min
189print 'x_max =',x_max
190print 'y_min =',y_min
191print 'y_max =',y_max
192
193x_min_index = int(round((x_min-x_min_in)/x_inc_in))
194x_max_index = int(round((x_max-x_min_in)/x_inc_in))
195
196y_min_index = int(round((y_max_in-y_min)/y_inc_in))
197y_max_index = int(round((y_max_in-y_max)/y_inc_in))
198
199assert x_max==x_min+(nx-1)*x_inc
200assert y_max==y_min+(ny-1)*y_inc
201
202assert x_max_in==x_min_in+(nx_in-1)*x_inc_in
203assert y_max_in==y_min_in+(ny_in-1)*y_inc_in
204
205#assert allclose(x_min_index*x_inc_in+x_min_in,x_min)
206#assert allclose(y_min_index*y_inc_in+y_min_in,y_min)
207#assert abs(x_min_index*x_inc_in+x_min_in-x_min)<0.0000001
208#assert abs(y_min_index*y_inc_in+y_min_in-y_min)<0.0000001
209
210#assert allclose(x_max_index*x_inc_in+x_min_in,x_max)
211#assert allclose(y_max_index*y_inc_in+y_min_in,y_max)
212#assert abs(x_max_index*x_inc_in+x_min_in-x_max)<0.0000001
213#assert abs(y_max_index*y_inc_in+y_min_in-y_max)<0.0000001
214
215#assert abs((y_min_index*1.)/(ny_in-1)-(y_min-y_min_in)/(y_max_in-y_min_in))<0.0000001
216
217h1_list=[]
218for i in range(nx):
219    h1_list.append(x_min+i*x_inc)
220
221h2_list=[]
222for j in range(ny):
223    h2_list.append(y_min+j*y_inc)
224
225h2_list.reverse()
226
227print 'reading bathymetry'
228#burn till line = first wanted line
229for i in range(y_max_index):
230    line = in_file.readline()
231
232
233if invert_bathymetry:
234    up=-1.
235
236nodata=nodata*up+tide_height*up*add_tide*-1.
237
238out_depth_array = []
239nodata_dict = {}
240for i in range(y_min_index-y_max_index+1):
241    k=0
242    in_line = in_file.readline()
243    if (i/thin_factor)*thin_factor-i == 0:
244        print 'reading line %i'%(i+y_min_index)
245        print len(in_line.split())
246        out_depth_array.append([])
247        for string in in_line.split()[x_min_index:x_max_index+1]:
248            if (k/thin_factor)*thin_factor-k == 0:
249                out_depth_array[i/thin_factor].append\
250                  (float(string)*up+tide_height*up*add_tide*-1.)
251                if out_depth_array[i/thin_factor][k/thin_factor]==nodata:
252                    nodata_dict[(i/thin_factor,k/thin_factor)]=None
253            k+=1
254
255in_file.close()
256
257
258if clean_bad_points:
259    print 'cleaning %i nodata points'%len(nodata_dict)
260#cleaning NANs
261    bad_points = {}
262    for point in nodata_dict.keys():
263        bad_points[point]=None
264#
265    for j in range(-1,nx+1):
266        point = (-1,j)
267        bad_points[point]=None
268        point = (ny,j)
269        bad_points[point]=None
270#
271    for i in range(-1,ny+1):
272        point = (i,-1)
273        bad_points[point]=None
274        point = (i,nx)
275        bad_points[point]=None
276#
277    while len(nodata_dict)>0:
278        for point in nodata_dict.keys():
279            sum = 0
280            n = 0
281            for i in (-1+point[0],0+point[0],1+point[0]):#for neighboring points
282                for j in (-1+point[1],0+point[1],1+point[1]):
283                    if not bad_points.has_key((i,j)):
284                        sum+=out_depth_array[i][j]
285                        n+=1
286            if n>0:
287                out_depth_array[point[0]][point[1]]=sum/n
288                bye = nodata_dict.pop(point)
289                bye = bad_points.pop(point)
290        print '%i points to go...'%len(nodata_dict)
291
292if not clean_bad_points:
293     for point in nodata_dict.keys():
294         out_depth_array[point[0]][point[1]]=no_data_value
295
296#out_depth_array.reverse()
297
298print 'writing results'
299out_file = open(output_file,'w')
300
301out_file.write('%i %i\n'%(nx,ny))
302
303for line in h1_list:
304    out_file.write('%10.10f'%line)
305    out_file.write('\n')
306
307for line in h2_list:
308    out_file.write('%10.10f'%line)
309    out_file.write('\n')
310
311k=0
312for line in out_depth_array:
313#    line.reverse()
314    print 'writing line %i'%k
315    for depth in line:
316        out_file.write('%10.10f '%depth)
317    out_file.write('\n')
318    k+=1
319
320
321out_file.close()
Note: See TracBrowser for help on using the repository browser.