-
Notifications
You must be signed in to change notification settings - Fork 2
/
dom2kml.py
executable file
·58 lines (42 loc) · 1.35 KB
/
dom2kml.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
#!/usr/bin/env python
import netCDF4 as nc4
import simplekml as kml
import numpy as np
import sys
if __name__ == '__main__':
if len(sys.argv) < 4:
print('Usage: %s <kml_layer_name> <kml_file_name> <geo_em_file> [<geo_em_file>*]' % sys.argv[0])
sys.exit(1)
# read in input names
kmlname = sys.argv[1]
tgtfile = sys.argv[2]
geonames = sys.argv[3:]
# construct kml document with grid point markers
doc = kml.Kml(name = kmlname)
# the generic poly style
style = kml.Style()
style.linestyle.color = kml.Color.red
style.linestyle.width = 3
style.polystyle.outline = 1
style.polystyle.fill = 0
for geoname in geonames:
# open netCDF data
d = nc4.Dataset(geoname)
# read in CLONG & CLAT
lon = d.variables['CLONG'][0,:,:]
lat = d.variables['CLAT'][0,:,:]
shape = lon.shape
# construct a poly that shows the domain
dom = doc.newpolygon(name = geoname)
bdry = []
bdry.extend([(lon[0,i], lat[0,i]) for i in range(shape[1])])
bdry.extend([(lon[j,-1], lat[j,-1]) for j in range(shape[0])])
bdry.extend([(lon[-1,i], lat[-1,i]) for i in range(shape[1]-1,-1,-1)])
bdry.extend([(lon[j,0], lat[j,0]) for j in range(shape[0]-1,-1,-1)])
dom.outerboundaryis = bdry
dom.style = style
if tgtfile.endswith('kmz'):
doc.savekmz(tgtfile)
else:
doc.save(tgtfile)
print('Success.')