forked from zhlin/WRF-GoogleEarth
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lfn2shp.py
45 lines (34 loc) · 790 Bytes
/
lfn2shp.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
#!/usr/bin/env python
import matplotlib
try:
matplotlib.use('Agg')
except:
pass
from netCDF4 import Dataset
from pylab import contour
import shapefile
import sys
f=Dataset(sys.argv[1],'r')
if len(sys.argv[1:]) > 1:
n=int(sys.argv[2])
lfn=f.variables['LFN'][n,:,:]
else:
lfn=f.variables['LFN'][0,:,:]
(fny,fnx)=lfn.shape
nx=len(f.dimensions['west_east'])+1
ny=len(f.dimensions['south_north'])+1
(srx,sry)=(fnx/nx,fny/ny)
lfn=lfn[:-sry,:-srx]
if (lfn > 0).all():
sys.exit(1)
x=f.variables['FXLONG'][0,:-sry,:-srx]
y=f.variables['FXLAT'][0,:-sry,:-srx]
c=contour(x,y,lfn,[0]).collections[0]
p=c.get_paths()
poly=[]
for i in p:
poly.extend(i.to_polygons())
w=shapefile.Writer(shapeType=shapefile.POLYGON)
w.poly(parts=poly)
w.save('fire.shp')
sys.exit(0)