1 import matplotlib
.pyplot
as plt
4 from JPSSD
import time_iso2num
, time_iso2datetime
7 import re
, glob
, sys
, os
9 def process_ignitions(igns
,bounds
=None):
14 for lon
, lat
, time_iso
in zip(igns
[0],igns
[1],igns
[2]):
18 mask
= np
.logical_and(np
.logical_and(np
.logical_and(lon
>bounds
[0],lon
<bounds
[1]),lat
>bounds
[2]),lat
<bounds
[3])
21 time_num
= time_iso2num(time_iso
)
22 time_datetime
= time_iso2datetime(time_iso
)
23 time_data
= '_A%04d%03d_%02d%02d' % (time_datetime
.year
, time_datetime
.timetuple().tm_yday
,
24 time_datetime
.hour
, time_datetime
.minute
)
25 acq_date
= '%04d-%02d-%02d' % (time_datetime
.year
, time_datetime
.month
, time_datetime
.day
)
26 acq_time
= '%02d%02d' % (time_datetime
.hour
, time_datetime
.minute
)
27 except Exception as e
:
28 print 'Error: bad ignition %s specified.' % igns
29 print 'Exception: %s.' % e
31 lon_nofire
= np
.array([])
32 lat_nofire
= np
.array([])
33 ignitions
.update({prefix
+ time_data
: Dict({'lon': lons
, 'lat': lats
,
34 'fire': np
.array(9*np
.ones(lons
.shape
)), 'conf_fire': np
.array(100*np
.ones(lons
.shape
)),
35 'lon_fire': lons
, 'lat_fire': lats
, 'lon_nofire': lon_nofire
, 'lat_nofire': lat_nofire
,
36 'scan_fire': scan
*np
.ones(lons
.shape
), 'track_fire': track
*np
.ones(lons
.shape
),
37 'time_iso': time_iso
, 'time_num': time_num
, 'acq_date': acq_date
, 'acq_time': acq_time
})})
40 def process_infrared_perimeters(dst
,bounds
=None,maxp
=1000,plot
=False):
41 files
= glob
.glob(osp
.join(dst
, '*.kml'))
48 print 'Retrieving perimeters from %s' % file
51 f_str
= ''.join(f
.readlines())
53 except Exception as e
:
54 print 'Error: exception when opening file %s, %s' % (file,e
.message
)
57 name
= re
.findall(r
'<name>(.*?)</name>',f_str
,re
.DOTALL
)[0]
58 date
= re
.match(r
'.*([0-9]{2}-[0-9]{2}-[0-9]{4} [0-9]{4})',name
).groups()[0]
59 time_iso
= date
[6:10]+'-'+date
[0:2]+'-'+date
[3:5]+'T'+date
[11:13]+':'+date
[13:15]+':00'
60 time_num
= time_iso2num(time_iso
)
61 time_datetime
= time_iso2datetime(time_iso
)
62 time_data
= '_A%04d%03d_%02d%02d' % (time_datetime
.year
, time_datetime
.timetuple().tm_yday
,
63 time_datetime
.hour
, time_datetime
.minute
)
64 acq_date
= '%04d-%02d-%02d' % (time_datetime
.year
, time_datetime
.month
, time_datetime
.day
)
65 acq_time
= '%02d%02d' % (time_datetime
.hour
, time_datetime
.minute
)
66 polygons
= re
.findall(r
'<Polygon>(.*?)</Polygon>',f_str
,re
.DOTALL
)
67 buckets
= [re
.split('\r\n\s+',re
.findall(r
'<coordinates>(.*?)</coordinates>',p
,re
.DOTALL
)[0])[1:] for p
in polygons
]
68 coordinates
= [np
.array(re
.split(',',b
)[0:2]).astype(float) for bucket
in buckets
for b
in bucket
]
69 if len(coordinates
) > maxp
:
70 coarse
= len(coordinates
)/maxp
72 coordinates
= coordinates
[1::coarse
]
73 except Exception as e
:
74 print 'Error: file %s has not proper structure.' % file
75 print 'Exception: %s.' % e
77 lon
= np
.array([coord
[0] for coord
in coordinates
])
78 lat
= np
.array([coord
[1] for coord
in coordinates
])
79 mask
= np
.logical_and(np
.logical_and(np
.logical_and(lon
>bounds
[0],lon
<bounds
[1]),lat
>bounds
[2]),lat
<bounds
[3])
82 lon_nofire
= np
.array([])
83 lat_nofire
= np
.array([])
85 plt
.plot(lons
,lats
,'*')
86 perimeters
.update({prefix
+ time_data
: Dict({'file': file, 'lon': lons
, 'lat': lats
,
87 'fire': np
.array(9*np
.ones(lons
.shape
)), 'conf_fire': np
.array(100*np
.ones(lons
.shape
)),
88 'lon_fire': lons
, 'lat_fire': lats
, 'lon_nofire': lon_nofire
, 'lat_nofire': lat_nofire
,
89 'scan_fire': scan
*np
.ones(lons
.shape
), 'track_fire': track
*np
.ones(lons
.shape
),
90 'time_iso': time_iso
, 'time_num': time_num
, 'acq_date': acq_date
, 'acq_time': acq_time
})})
94 print 'Warning: No KML files in the path specified'
99 if __name__
== "__main__":
101 dst
= './pioneer_perim'
103 p
= process_infrared_perimeters(dst
,plot
)