fixing confidence in setup.py
[JPSSData.git] / infrared_perimeters.py
blob7ee06a2f73da95c7a7a6fc96e1070af76fe8cd3c
1 import matplotlib.pyplot as plt
2 import numpy as np
3 import os.path as osp
4 from JPSSD import time_iso2num, time_iso2datetime
5 from utils import Dict
6 import saveload as sl
7 import re, glob, sys, os
9 def process_ignitions(igns,bounds=None):
10 prefix = "IGN"
11 ignitions = dict({})
12 scan = 1.
13 track = 1.
14 for lon, lat, time_iso in zip(igns[0],igns[1],igns[2]):
15 try:
16 lon = np.array(lon)
17 lat = np.array(lat)
18 mask = np.logical_and(np.logical_and(np.logical_and(lon>bounds[0],lon<bounds[1]),lat>bounds[2]),lat<bounds[3])
19 lons = lon[mask]
20 lats = lat[mask]
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
30 sys.exit(1)
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})})
38 return ignitions
40 def process_infrared_perimeters(dst,bounds=None,maxp=1000,plot=False):
41 files = glob.glob(osp.join(dst, '*.kml'))
42 prefix = "PER"
43 perimeters = Dict({})
44 scan = .01
45 track = .01
46 if files:
47 for file in files:
48 print 'Retrieving perimeters from %s' % file
49 try:
50 f = open(file,"r")
51 f_str = ''.join(f.readlines())
52 f.close()
53 except Exception as e:
54 print 'Error: exception when opening file %s, %s' % (file,e.message)
55 sys.exit(1)
56 try:
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
71 if coarse > 0:
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
76 sys.exit(1)
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])
80 lons = lon[mask]
81 lats = lat[mask]
82 lon_nofire = np.array([])
83 lat_nofire = np.array([])
84 if plot:
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})})
91 if plot:
92 plt.show()
93 else:
94 print 'Warning: No KML files in the path specified'
95 perimeters = []
96 return perimeters
99 if __name__ == "__main__":
100 plot = False
101 dst = './pioneer_perim'
103 p = process_infrared_perimeters(dst,plot)
104 print p