-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot_grids.py
executable file
·334 lines (241 loc) · 11.5 KB
/
plot_grids.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
import os
import json
import argparse
import numpy as np
from mpl_toolkits.basemap import Basemap
from matplotlib import pyplot as plt
from util import speak
from util import projections
from util.ncfunc import get_nc_file
lc_template = 'data/BamberDEM/Greenland_bedrock_topography_V3.nc'
lc_base = 'complete/mcb_datasets/greenland_1km_2016_12_01.mcb.nc'
#==================================
# parse the command line arguments
#==================================
parser = argparse.ArgumentParser() # -h or --help automatically included!
parser.add_argument('-s', '--show', help='Show the generated plot.', action='store_true')
volume = parser.add_mutually_exclusive_group()
volume.add_argument("-v", "--verbose", help="Increase the output verbosity", action="store_true")
volume.add_argument("-q", "--quiet", help="Run silently", action="store_true")
args = parser.parse_args()
speak.notquiet(args,"\nPlotting the representation of the Bamber grid in the EPSG:3413 projection")
speak.notquiet(args, "==========================================================================\n")
#==================
# load in datasets
#==================
speak.notquiet(args,"Loading the datasets.")
nc_template = get_nc_file(lc_template,'r')
nc_base = get_nc_file(lc_base,'r')
speak.verbose(args," Found Bamber DEM")
speak.verbose(args,"\n All data files found!")
#===== Bamber DEM ======
# this is a 1km dataset
#=======================
speak.notquiet(args,"\nCreating the 1 km Bamber template grid."),
template = projections.DataGrid()
template.y = nc_template.variables['projection_y_coordinate']
template.x = nc_template.variables['projection_x_coordinate']
template.ny = template.y[:].shape[0]
template.nx = template.x[:].shape[0]
template.make_grid()
speak.notquiet(args," Done!")
speak.notquiet(args,"\nCreating the 1 km Bamber grid."),
base = projections.DataGrid()
base.y = nc_base.variables['y1']
base.x = nc_base.variables['x1']
base.ny = base.y[:].shape[0]
base.nx = base.x[:].shape[0]
base.make_grid()
speak.notquiet(args," Done!")
#==== Projections ====
# All the projections
# needed for the data
#=====================
speak.notquiet(args,"\nGetting the projections.")
proj_epsg3413, proj_eigen_gl04c = projections.greenland()
speak.notquiet(args," Done!")
#=================================
# Determine the new template grid
#=================================
speak.notquiet(args, "\nProject the Bamber template grid into EPSG:3413.")
T_ll = ( np.amin(template.x_grid[1,:]), np.amin(template.y_grid[1,:]) )
T_lr = ( np.amax(template.x_grid[1,:]), np.amax(template.y_grid[1,:]) )
T_ur = ( np.amax(template.x_grid[-1,:]), np.amax(template.y_grid[-1,:]) )
T_ul = ( np.amin(template.x_grid[-1,:]), np.amin(template.y_grid[-1,:]) )
T_xs = np.array([ T_ll[0], T_lr[0], T_ur[0], T_ul[0], T_ll[0] ])
T_ys = np.array([ T_ll[1], T_lr[1], T_ur[1], T_ul[1], T_ll[1] ])
T_lons, T_lats = proj_eigen_gl04c(T_xs, T_ys, inverse=True)
T2E_xs, T2E_ys = proj_epsg3413(T_lons, T_lats)
e_ll = (np.floor(np.mean([T2E_xs[0], T2E_xs[3]])/1000.)*1000.,
np.floor(np.mean([T2E_ys[0], T2E_ys[1]])/1000.)*1000.)
e_ur = ( np.ceil(np.mean([T2E_xs[1], T2E_xs[2]])/1000.)*1000.,
np.ceil(np.mean([T2E_ys[3], T2E_ys[2]])/1000.)*1000.)
TE_xs = np.array([e_ll[0], e_ur[0], e_ur[0], e_ll[0], e_ll[0]])
TE_ys = np.array([e_ll[1], e_ll[1], e_ur[1], e_ur[1], e_ll[1]])
TE_lons, TE_lats = proj_epsg3413(TE_xs, TE_ys, inverse=True)
#==============================================
# Write the template grid specs to a json file
#==============================================
TE_grid = {'ll':[TE_xs[0], TE_ys[0]],
'ur':[TE_xs[2], TE_ys[2]],
'xs':TE_xs.tolist(),
'ys':TE_ys.tolist(),
'lons': TE_lons.tolist(),
'lats': TE_lats.tolist(),
'projstring':'+proj=stere +lat_ts=70.0 +lat_0=90 +lon_0=-45.0 +k_0=1.0 +x_0=0.0 +y_0=0.0 +ellps=WGS84 +units=m'
}
with open('EPSG3413grid.json', 'w') as f:
json.dump(TE_grid, f, indent=2)
#=================================
# Determine the new shrunken grid
#=================================
speak.notquiet(args, "\nProject the Bamber grid into EPSG:3413.")
B_ll = ( np.amin(base.x_grid[1,:]), np.amin(base.y_grid[1,:]) )
B_lr = ( np.amax(base.x_grid[1,:]), np.amax(base.y_grid[1,:]) )
B_ur = ( np.amax(base.x_grid[-1,:]), np.amax(base.y_grid[-1,:]) )
B_ul = ( np.amin(base.x_grid[-1,:]), np.amin(base.y_grid[-1,:]) )
B_xs = np.array([ B_ll[0], B_lr[0], B_ur[0], B_ul[0], B_ll[0] ])
B_ys = np.array([ B_ll[1], B_lr[1], B_ur[1], B_ul[1], B_ll[1] ])
B_lons, B_lats = proj_eigen_gl04c(B_xs, B_ys, inverse=True)
B2E_xs, B2E_ys = proj_epsg3413(B_lons, B_lats)
speak.verbose(args, " Bamber in EPSG:3413:")
speak.verbose(args, " Lower Left (x,y): ("+str(B2E_xs[0])+", "+str(B2E_ys[0])+")")
speak.verbose(args,"\n Lower Right (x,y): ("+str(B2E_xs[1])+", "+str(B2E_ys[1])+")")
speak.verbose(args,"\n Upper Right (x,y): ("+str(B2E_xs[2])+", "+str(B2E_ys[2])+")")
speak.verbose(args,"\n Upper Left (x,y): ("+str(B2E_xs[3])+", "+str(B2E_ys[3])+")")
speak.notquiet(args, "\nDeterniming the new EPSG:3413 grid from the transformed Bamber grid.")
e_ll = [np.floor(np.mean([B2E_xs[0], B2E_xs[3]])/1000.)*1000.,
np.floor(np.mean([B2E_ys[0], B2E_ys[1]])/1000.)*1000.]
e_ur = [ np.ceil(np.mean([B2E_xs[1], B2E_xs[2]])/1000.)*1000.,
np.ceil(np.mean([B2E_ys[3], B2E_ys[2]])/1000.)*1000.]
E_xs = np.array([e_ll[0], e_ur[0], e_ur[0], e_ll[0], e_ll[0]])
E_ys = np.array([e_ll[1], e_ll[1], e_ur[1], e_ur[1], e_ll[1]])
E_lons, E_lats = proj_epsg3413(E_xs, E_ys, inverse=True)
#NOTE: Best normal grid
offset_y128 = -20000.0
#NOTE: Working extended grid
#offset_y128 = -101000.0
#NOTE: Idea extended grid
#offset_y128 = -84000.0
#NOTE: Best normal grid
offset_xl = -80000.0
offset_x128 =-48000.0
#NOTE: Best extended grid
#offset_xl = -366000.0
#offset_x128 =-130000.0
#NOTE: Ideal extended grid
#offset_xl = -400000.0
#offset_x128 =-130000.0
O_ys = np.array([e_ll[1]+offset_y128,
e_ll[1]+offset_y128,
e_ur[1]-offset_y128-1000.,
e_ur[1]-offset_y128-1000.,
e_ll[1]+offset_y128])
O_xs = np.array([e_ll[0]+offset_xl+offset_x128,
e_ur[0]-offset_x128-1000.,
e_ur[0]-offset_x128-1000.,
e_ll[0]+offset_xl+offset_x128,
e_ll[0]+offset_xl+offset_x128])
O_lons, O_lats = proj_epsg3413(O_xs, O_ys, inverse=True)
speak.notquiet(args, " New EPSG:3413 grid:")
speak.notquiet(args, " Lower Left (x,y): ("+str(O_xs[0])+", "+str(O_ys[0])+")")
speak.notquiet(args, " Lower Left (lat,lon): ("+str(O_lats[0])+", "+str(O_lons[0])+")")
speak.notquiet(args,"\n Lower Right (x,y): ("+str(O_xs[1])+", "+str(O_ys[1])+")")
speak.notquiet(args, " Lower Right (lat,lon): ("+str(O_lats[1])+", "+str(O_lons[1])+")")
speak.notquiet(args,"\n Upper Right (x,y): ("+str(O_xs[2])+", "+str(O_ys[2])+")")
speak.notquiet(args, " Upper Right (lat,lon): ("+str(O_lats[2])+", "+str(O_lons[2])+")")
speak.notquiet(args,"\n Upper Left (x,y): ("+str(O_xs[3])+", "+str(O_ys[3])+")")
speak.notquiet(args, " Upper Left (lat,lon): ("+str(O_lats[3])+", "+str(O_lons[3])+")")
grid_points_y = int( (O_ys[3]-O_ys[0])/1000. +1 )
grid_points_x = int( (O_xs[1]-O_xs[0])/1000. +1 )
speak.notquiet(args,"\n Number of (1km) grid points in y: "+str(grid_points_y))
speak.notquiet(args, " Number of (1km) grid points in x: "+str(grid_points_x))
grid_p128_y = grid_points_y + (grid_points_y % 128)
grid_p128_x = grid_points_x + (grid_points_x % 128)
speak.notquiet(args,"\n Number of (1km) grid points for next multiple of 128 in y: "+str(grid_p128_y))
speak.notquiet(args, " Number of (1km) grid points for next multiple of 128 in x: "+str(grid_p128_x))
speak.notquiet(args, " Number of km needed to (add, subtract) to y: "+str(128 - (grid_points_y % 128))+', '+str(grid_points_y % 128))
speak.notquiet(args, " Number of km needed to (add, subtract) to x: "+str(128 - (grid_points_x % 128))+', '+str(grid_points_x % 128))
#=====================================
# Write the grid specs to a json file
#=====================================
O_grid = {'ll':[O_xs[0], O_ys[0]],
'ur':[O_xs[2], O_ys[2]],
'xs':O_xs.tolist(),
'ys':O_ys.tolist(),
'lons': O_lons.tolist(),
'lats': O_lats.tolist(),
'projstring':'+proj=stere +lat_ts=70.0 +lat_0=90 +lon_0=-45.0 +k_0=1.0 +x_0=0.0 +y_0=0.0 +ellps=WGS84 +units=m'
}
with open('EPSG3413grid_shrunk.json', 'w') as f:
json.dump(O_grid, f, indent=2)
#======
# Plot
#======
speak.notquiet(args,"\nPlotting the grid.")
plt.figure(1, figsize=(12,10), dpi=150)
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
#======= Plot ========
# EPSG:3413 in Bamber
#=====================
plt.subplot(1,2,1)
#NOTE: Basemap adds false Eastings and Northings dependent upon your corners.
# The proj4 projections, however, don't have any, so you'll alsways have
# to passs the map longs and lats.
map_ll_lon, map_ll_lat = proj_eigen_gl04c(B_xs[0]-800000., B_ys[0]-1150000., inverse=True)
map_ur_lon, map_ur_lat = proj_eigen_gl04c(B_xs[2]+800000., B_ys[2]+300000., inverse=True)
glmap = Basemap(llcrnrlon=map_ll_lon, llcrnrlat=map_ll_lat,
urcrnrlon=map_ur_lon, urcrnrlat=map_ur_lat,
projection='stere', lat_ts=71.0, lat_0=90.0, lon_0=321.0,
resolution='l')
glmap.fillcontinents(color='gray', lake_color='white')
glmap.drawcoastlines()
T_X, T_Y = glmap(T_lons, T_lats)
glmap.plot(T_X, T_Y, 'ko-', label='Bamber template grid')
TE_X, TE_Y = glmap(TE_lons, TE_lats)
glmap.plot(TE_X, TE_Y, 'mo-', label='EPSG:3413 template grid')
B_X, B_Y = glmap(B_lons, B_lats)
glmap.plot(B_X, B_Y, 'bo-', label='Bamber grid')
E2B_X, E2B_Y = glmap(E_lons, E_lats)
glmap.plot(E2B_X, E2B_Y, 'ro-', label='EPSG:3413 grid')
O2B_X, O2B_Y = glmap(O_lons, O_lats)
glmap.plot(O2B_X, O2B_Y, 'go-', label='Extended EPSG:3413 grid')
plt.title('Greenland using the Bamber projection')
plt.legend(loc='lower center', fancybox=True, shadow=True)
#======= Plot ========
# Bamber in EPSG:3413
#=====================
plt.subplot(1,2,2)
#NOTE: Basemap adds false Eastings and Northings dependent upon your corners.
# The proj4 projections, however, don't have any, so you'll alsways have
# to passs the map longs and lats.
map_ll_lon, map_ll_lat = proj_epsg3413(E_xs[0]-800000., E_ys[0]-1150000., inverse=True)
map_ur_lon, map_ur_lat = proj_epsg3413(E_xs[2]+800000., E_ys[2]+300000., inverse=True)
glmap = Basemap(llcrnrlon=map_ll_lon, llcrnrlat=map_ll_lat,
urcrnrlon=map_ur_lon, urcrnrlat=map_ur_lat,
resolution='l', epsg=3413)
glmap.fillcontinents(color='gray', lake_color='white')
glmap.drawcoastlines()
T2E_X, T2E_Y = glmap(T_lons, T_lats)
glmap.plot(T2E_X, T2E_Y, 'ko-', label='Bamber template grid')
TE_X, TE_Y = glmap(TE_lons, TE_lats)
glmap.plot(TE_X, TE_Y, 'mo-', label='EPSG:3413 template grid')
B2E_X, B2E_Y = glmap(B_lons, B_lats)
glmap.plot(B2E_X, B2E_Y, 'bo-', label='Bamber grid')
E_X, E_Y = glmap(E_lons, E_lats)
glmap.plot(E_X, E_Y, 'ro-', label='EPSG:3413 grid')
O_X, O_Y = glmap(O_lons, O_lats)
glmap.plot(O_X, O_Y, 'go-', label='Extended EPSG:3413 grid')
plt.title('Greenland using the EPSG:3413 projection')
plt.legend(loc='lower center', fancybox=True, shadow=True)
#========
# Finish
#========
plt.tight_layout()
plt.savefig('Bamber_v_EPSG3413.png', bbox_inches='tight')
if args.show:
plt.show()
nc_base.close()
speak.notquiet(args," Done!\n")