forked from deeplycloudy/lmatools
/
make_grids.py
448 lines (349 loc) · 18.3 KB
/
make_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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import glob
import os, sys
from datetime import datetime, timedelta
import numpy as np
import tables
import density_to_files
from lmatools.lma_io import read_flashes, to_seconds
from coordinateSystems import MapProjection, GeographicSystem
def dlonlat_at_grid_center(ctr_lat, ctr_lon, dx=4.0e3, dy=4.0e3,
x_bnd = (-100e3, 100e3), y_bnd = (-100e3, 100e3),
proj_datum = 'WGS84', proj_ellipse = 'WGS84'):
"""
Utility function useful for producing a regular grid of lat/lon data,
where an approximate spacing (dx, dy) and total span of the grid (x_bnd, y_bnd)
is desired. Units are in meters.
There is guaranteed to be distortion away from the grid center, i.e.,
only the grid cells adjacent to the center location will have area dx * dy.
Likewise, the lat, lon range is calculated naively using dlat, dlon multiplied
by the number of grid cells implied by x_bnd/dx, y_bnd/dy. This is the naive approach,
but probably what's expected when specifying distances in kilometers for
an inherently distorted lat/lon grid.
Returns:
(dlon, dlat, lon_bnd, lat_bnd)
corresponding to
(dx, dy, x_range, y_range)
"""
# Use the Azimuthal equidistant projection as the method for converting to kilometers.
proj_name = 'aeqd'
mapProj = MapProjection(projection=proj_name, ctrLat=ctr_lat, ctrLon=ctr_lon, lat_ts=ctr_lat,
lon_0=ctr_lon, lat_0=ctr_lat, lat_1=ctr_lat, ellipse=proj_ellipse, datum=proj_datum)
geoProj = GeographicSystem()
# Get dlat
lon_n, lat_n, z_n = geoProj.fromECEF(*mapProj.toECEF(0,dy,0))
dlat = lat_n - ctr_lat
# Get dlon
lon_e, lat_e, z_e = geoProj.fromECEF(*mapProj.toECEF(dx,0,0))
dlon = lon_e - ctr_lon
lon_min = ctr_lon + dlon * (x_bnd[0]/dx)
lon_max = ctr_lon + dlon * (x_bnd[1]/dx)
lat_min = ctr_lat + dlat * (y_bnd[0]/dy)
lat_max = ctr_lat + dlat * (y_bnd[1]/dy)
# Alternate method: lat lon for the actual distance to the NSEW in the projection
#lon_range_n, lat_range_n, z_range_n = geoProj.fromECEF(*mapProj.toECEF(0,y_bnd,0))
#lon_range_e, lat_range_e, z_range_e = geoProj.fromECEF(*mapProj.toECEF(x_bnd,0,0))
return dlon, dlat, (lon_min, lon_max), (lat_min, lat_max)
def write_cf_netcdf_latlon(outfile, t_start, t, xloc, yloc, lon_for_x, lat_for_y, ctr_lat, ctr_lon, grid, grid_var_name, grid_description, format='i', **kwargs):
""" Write a Climate and Forecast Metadata-compliant NetCDF file.
Grid is regular in lon, lat and so no map projection information is necessary.
Should display natively in conformant packages like McIDAS-V.
"""
import pupynere as nc
missing_value = -9999
nc_out = nc.NetCDFFile(outfile, 'w')
nc_out.createDimension('lon', xloc.shape[0])
nc_out.createDimension('lat', yloc.shape[0])
nc_out.createDimension('ntimes', t.shape[0]) #unlimited==None
# declare the coordinate reference system, WGS84 values
proj = nc_out.createVariable('crs', 'i', ())
proj.grid_mapping_name = 'latitude_longitude'
proj.longitude_of_prime_meridian = 0.0
proj.semi_major_axis = 6378137.0
proj.inverse_flattening = 298.257223563
y_coord = nc_out.createVariable('latitude', 'f', ('lat',))
y_coord.units = "degrees_north"
y_coord.long_name = "latitude"
y_coord.standard_name = 'latitude'
x_coord = nc_out.createVariable('longitude', 'f', ('lon',))
x_coord.units = "degrees_east"
x_coord.long_name = "longitude"
x_coord.standard_name = 'longitude'
times = nc_out.createVariable('time', 'f', ('ntimes',) )#, filters=no_compress)
times.long_name="time"
times.units = "seconds since %s" % t_start.strftime('%Y-%m-%d %H:%M:%S')
# lons = nc_out.createVariable('lons', 'd', ('nx','ny') )#, filters=no_compress)
# lons.long_name="longitude"
# lons.standard_name="longitude"
# lons.units = "degrees_east"
#
# lats = nc_out.createVariable('lats', 'd', ('nx','ny') )#, filters=no_compress)
# lats.long_name="latitude"
# lats.standard_name="latitude"
# lats.units = "degrees_north"
lightning2d = nc_out.createVariable(grid_var_name, format, ('ntimes','lon','lat') )#, filters=no_compress)
lightning2d.long_name=grid_description #'LMA VHF event counts (vertically integrated)'
lightning2d.units=kwargs['grid_units']
# lightning2d.coordinates='time lons lats'
lightning2d.grid_mapping = "crs"
lightning2d.missing_value = missing_value
x_coord[:] = xloc[:]
y_coord[:] = yloc[:]
times[:] = t[:]
# lons[:] = lon_for_x[:]
# lats[:] = lat_for_y[:]
for i in range(grid.shape[2]):
lightning2d[i,:,:] = grid[:,:,i]
nc_out.close()
def write_cf_netcdf(outfile, t_start, t, xloc, yloc, lon_for_x, lat_for_y, ctr_lat, ctr_lon, grid, grid_var_name, grid_description, format='i', **kwargs):
""" Write a Climate and Forecast Metadata-compliant NetCDF file.
Should display natively in conformant packages like McIDAS-V.
"""
import pupynere as nc
missing_value = -9999
nc_out = nc.NetCDFFile(outfile, 'w')
nc_out.createDimension('nx', xloc.shape[0])
nc_out.createDimension('ny', yloc.shape[0])
nc_out.createDimension('ntimes', t.shape[0]) #unlimited==None
proj = nc_out.createVariable('Lambert_Azimuthal_Equal_Area', 'i', ())
proj.grid_mapping_name = 'lambert_azimuthal_equal_area'
proj.longitude_of_projection_origin = ctr_lon
proj.latitude_of_projection_origin = ctr_lat
proj.false_easting = 0.0
proj.false_northing = 0.0
# x_coord = nc_out.createVariable('longitude', 'f', ('nx',))
# x_coord.long_name="longitude"
# x_coord.standard_name="longitude"
# x_coord.units = "degrees_east"
x_coord = nc_out.createVariable('x', 'f', ('nx',))
x_coord.units = "km"
x_coord.long_name = "x coordinate of projection"
x_coord.standard_name = 'projection_x_coordinate'
# y_coord = nc_out.createVariable('latitude', 'f', ('nx',))
# y_coord.long_name="latitude"
# y_coord.standard_name="latitude"
# y_coord.units = "degrees_north"
y_coord = nc_out.createVariable('y', 'f', ('ny',))
y_coord.units = "km"
y_coord.long_name = "y coordinate of projection"
y_coord.standard_name = 'projection_y_coordinate'
times = nc_out.createVariable('time', 'f', ('ntimes',) )#, filters=no_compress)
times.long_name="time"
times.units = "seconds since %s" % t_start.strftime('%Y-%m-%d %H:%M:%S')
lons = nc_out.createVariable('lons', 'd', ('nx','ny') )#, filters=no_compress)
lons.long_name="longitude"
lons.standard_name="longitude"
lons.units = "degrees_east"
lats = nc_out.createVariable('lats', 'd', ('nx','ny') )#, filters=no_compress)
lats.long_name="latitude"
lats.standard_name="latitude"
lats.units = "degrees_north"
lightning2d = nc_out.createVariable(grid_var_name, format, ('ntimes','nx','ny') )#, filters=no_compress)
lightning2d.long_name=grid_description #'LMA VHF event counts (vertically integrated)'
lightning2d.units='dimensionless'
lightning2d.coordinates='time lons lats'
lightning2d.grid_mapping = "Lambert_Azimuthal_Equal_Area"
lightning2d.missing_value = missing_value
x_coord[:] = xloc[:]
y_coord[:] = yloc[:]
times[:] = t[:]
lons[:] = lon_for_x[:]
lats[:] = lat_for_y[:]
for i in range(grid.shape[2]):
lightning2d[i,:,:] = grid[:,:,i]
nc_out.close()
def time_edges(start_time, end_time, frame_interval):
""" Return lists cooresponding the start and end times of frames lasting frame_interval
between start_time and end_time. The last interval may extend beyond end_time, but
by no more than frame_interval. This makes each frame the same length.
returns t_edges, duration, where t_edges is a list of datetime objects, and
duration is the total duration between the start and end times (and not the duration
of all frames)
"""
frame_dt = timedelta(0, frame_interval, 0)
duration = end_time - start_time
n_frames = int(np.ceil(to_seconds(duration) / to_seconds(frame_dt)))
t_edges = [start_time + i*frame_dt for i in range(n_frames+1)]
return t_edges, duration
def seconds_since_start_of_day(start_time, t):
""" For each datetime object t, return the number of seconds elapsed since the
start of the date given by start_time. Only the date part of start_time is used.
"""
ref_date = start_time.date()
t_ref = datetime(ref_date.year, ref_date.month, ref_date.day)
t_edges_seconds = [to_seconds(edge - t_ref) for edge in t]
return t_ref, t_edges_seconds
def grid_h5flashfiles(h5_filenames, start_time, end_time,
frame_interval=120.0, dx=4.0e3, dy=4.0e3,
x_bnd = (-100e3, 100e3),
y_bnd = (-100e3, 100e3),
z_bnd = (-20e3, 20e3),
ctr_lat = 35.23833, ctr_lon = -97.46028,
min_points_per_flash=10,
outpath = '',
flash_count_logfile = None,
proj_name = 'aeqd',
proj_datum = 'WGS84',
proj_ellipse = 'WGS84',
output_writer = write_cf_netcdf,
output_filename_prefix="LMA",
output_kwargs = {},
spatial_scale_factor = 1.0/1000.0,
):
from math import ceil
"""
Create 2D plan-view density grids for events, flash origins, flash extents, and mean flash footprint
frame_interval: Frame time-step in seconds
dx, dy: horizontal grid size in m (or deg)
{x,y,z}_bnd: horizontal grid edges in m
ctr_lat, ctr_lon: coordinate center
Uses an azimuthal equidistant map projection on the WGS84 ellipsoid.
read_flashes
filter_flash
extract_events
flash_to_frame
frame0_broadcast, frame1_broadcast, ...
each broadcaster above sends events and flashes to:
projection( event_location), projection(flash_init_location), projection(event_location)
which map respectively to:
point_density->accum_on_grid(event density), point_density->accum_on_grid(flash init density), extent_density->accum_on_grid(flash_extent_density)
grids are in an HDF5 file. how to handle flushing?
"""
if flash_count_logfile is None:
flash_count_logfile = sys.stdout
# reference time is the date part of the start_time
t_edges, duration = time_edges(start_time, end_time, frame_interval)
t_ref, t_edges_seconds = seconds_since_start_of_day(start_time, t_edges)
n_frames = len(t_edges)-1
xedge=np.arange(x_bnd[0], x_bnd[1]+dx, dx)
yedge=np.arange(y_bnd[0], y_bnd[1]+dy, dy)
x0 = xedge[0]
y0 = yedge[0]
if proj_name == 'latlong':
dx_units = '{0:6.4f}deg'.format(dx)
mapProj = GeographicSystem()
else:
dx_units = '{0:5.1f}m'.format(dx)
mapProj = MapProjection(projection=proj_name, ctrLat=ctr_lat, ctrLon=ctr_lon, lat_ts=ctr_lat,
lon_0=ctr_lon, lat_0=ctr_lat, lat_1=ctr_lat, ellipse=proj_ellipse, datum=proj_datum)
geoProj = GeographicSystem()
event_density_grid = np.zeros((xedge.shape[0]-1, yedge.shape[0]-1, n_frames), dtype='int32')
init_density_grid = np.zeros((xedge.shape[0]-1, yedge.shape[0]-1, n_frames), dtype='int32')
extent_density_grid = np.zeros((xedge.shape[0]-1, yedge.shape[0]-1, n_frames), dtype='int32')
footprint_grid = np.zeros((xedge.shape[0]-1, yedge.shape[0]-1, n_frames), dtype='float32')
all_frames = []
extent_frames = []
init_frames = []
event_frames = []
for i in range(n_frames):
extent_out = {'name':'extent'}
init_out = {'name':'init'}
event_out = {'name':'event'}
accum_event_density = density_to_files.accumulate_points_on_grid(event_density_grid[:,:,i], xedge, yedge, out=event_out, label='event')
accum_init_density = density_to_files.accumulate_points_on_grid(init_density_grid[:,:,i], xedge, yedge, out=init_out, label='init')
accum_extent_density = density_to_files.accumulate_points_on_grid(extent_density_grid[:,:,i], xedge, yedge, out=extent_out,label='extent')
accum_footprint = density_to_files.accumulate_points_on_grid(footprint_grid[:,:,i], xedge, yedge, label='footprint')
extent_out['func'] = accum_extent_density
init_out['func'] = accum_init_density
event_out['func'] = accum_event_density
extent_frames.append(extent_out)
init_frames.append(init_out)
event_frames.append(event_out)
event_density_target = density_to_files.point_density(accum_event_density)
init_density_target = density_to_files.point_density(accum_init_density)
extent_density_target = density_to_files.extent_density(x0, y0, dx, dy, accum_extent_density)
mean_footprint_target = density_to_files.extent_density(x0, y0, dx, dy, accum_footprint, weight_key='area')
spew_to_density_types = density_to_files.broadcast( (
density_to_files.project('lon', 'lat', 'alt', mapProj, geoProj, event_density_target, use_flashes=False),
density_to_files.project('init_lon', 'init_lat', 'init_alt', mapProj, geoProj, init_density_target, use_flashes=True),
density_to_files.project('lon', 'lat', 'alt', mapProj, geoProj, extent_density_target, use_flashes=False),
density_to_files.project('lon', 'lat', 'alt', mapProj, geoProj, mean_footprint_target, use_flashes=False),
) )
all_frames.append( density_to_files.extract_events_for_flashes( spew_to_density_types ) )
frame_count_log = density_to_files.flash_count_log(flash_count_logfile)
framer = density_to_files.flashes_to_frames(t_edges_seconds, all_frames, time_key='start', time_edges_datetime=t_edges, flash_counter=frame_count_log)
read_flashes( h5_filenames, framer, base_date=t_ref, min_points=min_points_per_flash)
# print 'event_density_grid ', id(event_density_grid[:,:,-1])
# print 'extent_density_grid', id(extent_density_grid[:,:,-1])
# print 'init_density_grid ', id(init_density_grid[:,:,-1])
x_coord = (xedge[:-1] + xedge[1:])/2.0
y_coord = (yedge[:-1] + yedge[1:])/2.0
nx = x_coord.shape[0]
ny = y_coord.shape[0]
x_all, y_all = (a.T for a in np.meshgrid(x_coord, y_coord))
assert x_all.shape == y_all.shape
assert x_all.shape[0] == nx
assert x_all.shape[1] == ny
z_all = np.zeros_like(x_all)
lons, lats, alts = x,y,z = geoProj.fromECEF( *mapProj.toECEF(x_all, y_all, z_all) )
lons.shape=x_all.shape
lats.shape=y_all.shape
outflile_basename = os.path.join(outpath,'%s_%s_%d_%dsrc_%s-dx_' % (output_filename_prefix, start_time.strftime('%Y%m%d_%H%M%S'), to_seconds(duration), min_points_per_flash, dx_units))
outfiles = (outflile_basename+'flash_extent.nc',
outflile_basename+'flash_init.nc',
outflile_basename+'source.nc',
outflile_basename+'footprint.nc',
)
outgrids = (extent_density_grid,
init_density_grid,
event_density_grid,
footprint_grid
)
field_names = ('flash_extent', 'flash_initiation', 'lma_source', 'flash_footprint')
field_descriptions = ('LMA flash extent density',
'LMA flash initiation density',
'LMA source density',
'LMA local mean flash area')
if proj_name=='latlong':
density_units = "grid"
else:
density_units = "{0:5.1f} km^2".format(dx/1000.0 * dy/1000.0).lstrip()
time_units = "{0:5.1f} min".format(frame_interval/60.0).lstrip()
density_label = 'Count per ' + density_units + " pixel per "+ time_units
field_units = ( density_label,
density_label,
density_label,
"km^2 per flash",
)
output_writer(outfiles[0], t_ref, np.asarray(t_edges_seconds[:-1]),
x_coord*spatial_scale_factor, y_coord*spatial_scale_factor,
lons, lats, ctr_lat, ctr_lon,
outgrids[0], field_names[0], field_descriptions[0],
grid_units=field_units[0],
**output_kwargs)
output_writer(outfiles[1], t_ref, np.asarray(t_edges_seconds[:-1]),
x_coord*spatial_scale_factor, y_coord*spatial_scale_factor,
lons, lats, ctr_lat, ctr_lon,
outgrids[1], field_names[1], field_descriptions[1],
grid_units=field_units[1],
**output_kwargs)
output_writer(outfiles[2], t_ref, np.asarray(t_edges_seconds[:-1]),
x_coord*spatial_scale_factor, y_coord*spatial_scale_factor,
lons, lats, ctr_lat, ctr_lon,
outgrids[2], field_names[2], field_descriptions[2],
grid_units=field_units[2],
**output_kwargs)
output_writer(outfiles[3], t_ref, np.asarray(t_edges_seconds[:-1]),
x_coord*spatial_scale_factor, y_coord*spatial_scale_factor,
lons, lats, ctr_lat, ctr_lon,
outgrids[3], field_names[3], field_descriptions[3], format='f',
grid_units=field_units[3],
**output_kwargs)
print 'max extent is', extent_density_grid.max()
return x_coord, y_coord, lons, lats, extent_density_grid, outfiles, field_names
if __name__ == '__main__':
h5_filenames = glob.glob('data/LYL*090610_20*.h5')
start_time = datetime(2009,6,10, 20,0,0)
end_time = datetime(2009,6,10, 21,0,0)
frame_interval=120.0
dx=4.0e3
dy=4.0e3
x_bnd = (-100e3, 100e3)
y_bnd = (-100e3, 100e3)
# # KOUN
ctr_lat = 35.23833
ctr_lon = -97.46028
# DC
# ctr_lat = 38.889444
# ctr_lon = -77.035278
grid_h5flashfiles(h5_filenames, start_time, end_time, frame_interval=frame_interval,
dx=dx, dy=dy, x_bnd=x_bnd, y_bnd=y_bnd, ctr_lon=ctr_lon, ctr_lat=ctr_lat)