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 | |
---|
6 | y_min = -35 #100m Botany Bay |
---|
7 | y_max = -33.8 |
---|
8 | x_min = 150.+(+50./.6)*0.01 |
---|
9 | x_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 | |
---|
46 | thin_factor = 2 |
---|
47 | add_tide = True |
---|
48 | tide_height = 1.1 |
---|
49 | clean_bad_points = True |
---|
50 | no_data_value=-1000000 |
---|
51 | #if clean_bad_points is False, bad_points will just be |
---|
52 | #set to no_data_value |
---|
53 | |
---|
54 | crop =True |
---|
55 | #crop =False |
---|
56 | invert_bathymetry = True |
---|
57 | |
---|
58 | input_file = 'dem_geog.asc' |
---|
59 | #output_file = 'wool_200m.txt' |
---|
60 | output_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 | """ |
---|
75 | Takes a bathymetry file of format: |
---|
76 | "" |
---|
77 | ncols 1333 |
---|
78 | nrows 1030 |
---|
79 | xllcorner 151.031182 |
---|
80 | yllcorner 34.15233 |
---|
81 | cellsize 0.00025000050663948 |
---|
82 | NODATA_value -9999 |
---|
83 | 56.3 55.36667 54.1 53.03333 52.2 ... (row n) |
---|
84 | 54.18333 53.11667 52.26667 51.6 51.33334 ... (row n-1) |
---|
85 | ... |
---|
86 | 54.18333 53.11667 52.26667 51.6 51.33334 ... (row 1) |
---|
87 | "" |
---|
88 | |
---|
89 | and makes it format: |
---|
90 | "" |
---|
91 | nx ny (ncols=longitude_range,nrows=latitude_range) |
---|
92 | 151.031182(longitude 1) |
---|
93 | 151.031182+cellsize(longitude 2) |
---|
94 | 151.031182+2*cellsize(longitude 3) |
---|
95 | ... |
---|
96 | 151.031182+(n-1)*cellsize(longitude n) |
---|
97 | 34.1523+cellsize(latitude 1) |
---|
98 | 34.1523+2*cellsize(latitude 2) |
---|
99 | 34.1523+3*cellsize(latitude 3) |
---|
100 | ... |
---|
101 | 34.1523+(n-1)*cellsize(latitude n) |
---|
102 | 56.3 55.36667 54.1 53.03333 52.2 (row 1) |
---|
103 | 54.18333 53.11667 52.26667 51.6 51.33334 (row 2) |
---|
104 | ... |
---|
105 | 54.18333 53.11667 52.26667 51.6 51.33334 (n) |
---|
106 | |
---|
107 | "" |
---|
108 | If llcorner is negative for longitude then it will |
---|
109 | then it will automatically change the x increment to -cellsize |
---|
110 | |
---|
111 | Options will include cropping and thinning and finding values |
---|
112 | for NANs. |
---|
113 | |
---|
114 | NB it assumes that the system has the memory to cope with any |
---|
115 | give row or column, or the cropped bathymetry, but not nessesarily |
---|
116 | the whole bathymetry. |
---|
117 | """ |
---|
118 | |
---|
119 | thin_factor = int(thin_factor) |
---|
120 | #from Numeric import allclose |
---|
121 | |
---|
122 | in_file = open(input_file,'r') |
---|
123 | |
---|
124 | nx_in = in_file.readline() |
---|
125 | nx_in = nx_in.split()[1] |
---|
126 | nx_in= int(nx_in) |
---|
127 | |
---|
128 | ny_in = in_file.readline() |
---|
129 | ny_in = ny_in.split()[1] |
---|
130 | ny_in = int(ny_in) |
---|
131 | |
---|
132 | x_min_in = in_file.readline() |
---|
133 | x_min_in = x_min_in.split()[1] |
---|
134 | x_min_in = float(x_min_in) |
---|
135 | y_min_in = in_file.readline() |
---|
136 | y_min_in = y_min_in.split()[1] |
---|
137 | y_min_in = float(y_min_in) |
---|
138 | |
---|
139 | cellsize = in_file.readline() |
---|
140 | cellsize = cellsize.split()[1] |
---|
141 | cellsize = float(cellsize) |
---|
142 | |
---|
143 | nodata = in_file.readline() |
---|
144 | nodata = nodata.split()[1] |
---|
145 | nodata = float(nodata) |
---|
146 | |
---|
147 | x_inc_in = cellsize |
---|
148 | y_inc_in = cellsize |
---|
149 | |
---|
150 | x_max_in = x_min_in+(nx_in-1)*x_inc_in |
---|
151 | y_max_in = y_min_in+(ny_in-1)*y_inc_in |
---|
152 | print 'minimum possible x =',x_min_in |
---|
153 | print 'maximum possible x =',x_max_in |
---|
154 | print 'minimum possible y =',y_min_in |
---|
155 | print 'maximum possible y =',y_max_in |
---|
156 | |
---|
157 | x_inc = x_inc_in*thin_factor |
---|
158 | y_inc = y_inc_in*thin_factor |
---|
159 | |
---|
160 | if 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) |
---|
165 | else: |
---|
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 | |
---|
175 | assert not x_min<x_min_in |
---|
176 | assert not y_min<y_min_in |
---|
177 | assert not x_max>x_max_in |
---|
178 | assert not y_max>y_max_in |
---|
179 | assert not x_min>x_max |
---|
180 | assert not y_min>y_max |
---|
181 | |
---|
182 | nx = int(round((x_max-x_min)/x_inc+1)) |
---|
183 | ny = int(round((y_max-y_min)/y_inc+1)) |
---|
184 | |
---|
185 | x_max = x_min+(nx-1)*x_inc |
---|
186 | y_max = y_min+(ny-1)*y_inc |
---|
187 | |
---|
188 | print 'x_min =',x_min |
---|
189 | print 'x_max =',x_max |
---|
190 | print 'y_min =',y_min |
---|
191 | print 'y_max =',y_max |
---|
192 | |
---|
193 | x_min_index = int(round((x_min-x_min_in)/x_inc_in)) |
---|
194 | x_max_index = int(round((x_max-x_min_in)/x_inc_in)) |
---|
195 | |
---|
196 | y_min_index = int(round((y_max_in-y_min)/y_inc_in)) |
---|
197 | y_max_index = int(round((y_max_in-y_max)/y_inc_in)) |
---|
198 | |
---|
199 | assert x_max==x_min+(nx-1)*x_inc |
---|
200 | assert y_max==y_min+(ny-1)*y_inc |
---|
201 | |
---|
202 | assert x_max_in==x_min_in+(nx_in-1)*x_inc_in |
---|
203 | assert 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 | |
---|
217 | h1_list=[] |
---|
218 | for i in range(nx): |
---|
219 | h1_list.append(x_min+i*x_inc) |
---|
220 | |
---|
221 | h2_list=[] |
---|
222 | for j in range(ny): |
---|
223 | h2_list.append(y_min+j*y_inc) |
---|
224 | |
---|
225 | h2_list.reverse() |
---|
226 | |
---|
227 | print 'reading bathymetry' |
---|
228 | #burn till line = first wanted line |
---|
229 | for i in range(y_max_index): |
---|
230 | line = in_file.readline() |
---|
231 | |
---|
232 | |
---|
233 | if invert_bathymetry: |
---|
234 | up=-1. |
---|
235 | |
---|
236 | nodata=nodata*up+tide_height*up*add_tide*-1. |
---|
237 | |
---|
238 | out_depth_array = [] |
---|
239 | nodata_dict = {} |
---|
240 | for 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 | |
---|
255 | in_file.close() |
---|
256 | |
---|
257 | |
---|
258 | if 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 | |
---|
292 | if 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 | |
---|
298 | print 'writing results' |
---|
299 | out_file = open(output_file,'w') |
---|
300 | |
---|
301 | out_file.write('%i %i\n'%(nx,ny)) |
---|
302 | |
---|
303 | for line in h1_list: |
---|
304 | out_file.write('%10.10f'%line) |
---|
305 | out_file.write('\n') |
---|
306 | |
---|
307 | for line in h2_list: |
---|
308 | out_file.write('%10.10f'%line) |
---|
309 | out_file.write('\n') |
---|
310 | |
---|
311 | k=0 |
---|
312 | for 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 | |
---|
321 | out_file.close() |
---|