-
Notifications
You must be signed in to change notification settings - Fork 2
/
contour2kml.py
executable file
·56 lines (46 loc) · 1.39 KB
/
contour2kml.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
#!/usr/bin/env python
import netCDF4 as nc4
import sys
import os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import simplekml as kml
if __name__ == '__main__':
if len(sys.argv) != 6:
print('Usage: %s <wrfout-file> <varname> <dom-id> <esmf-time> <out-path>' % sys.argv[0])
sys.exit(1)
d = nc4.Dataset(sys.argv[1])
varname = sys.argv[2]
dom_id = int(sys.argv[3])
tstr = sys.argv[4]
out_path = sys.argv[5]
base_name = varname + ('-%02d-' % dom_id) + tstr
# extract ESMF string times
times = [''.join(x) for x in d.variables['Times'][:]]
if tstr not in times:
print('Error: invalid timestamp %s' % tstr)
sys.exit(2)
# extract variables
tndx = times.index(tstr)
fa = d.variables[varname][0,:,:]
lon = d.variables['FXLONG'][0,:,:]
lat = d.variables['FXLAT'][0,:,:]
# add each polygon to the KML document
doc = kml.Kml(name = tstr)
style = kml.Style()
style.linestyle.color = kml.Color.red
style.linestyle.width = 3
style.polystyle.outline = 1
style.polystyle.fill = 0
c = plt.contour(lon, lat, fa, [0]).collections[0]
paths = c.get_paths()
polys = []
for path in paths:
if len(path) > 3:
poly = doc.newpolygon()
poly.outerboundaryis = [(x,y) for x,y in path.to_polygons()[0]]
poly.style = style
polys.append(poly)
# always store as kmz
doc.savekmz(os.path.join(out_path, base_name + '.kmz'))