New file process.py to have the whole process instead of case.py
[JPSSData.git] / process.py
blob721ded2cd04fbb635b5e5b434cefaa4f4f7e9753
1 # General python for any case
2 from JPSSD import *
3 from interpolation import sort_dates
4 from setup import process_satellite_detections
5 from svm import preprocess_data_svm, SVM3
6 from contline import get_contour_verts
7 from contour2kml import contour2kml
8 import saveload as sl
9 from scipy.io import loadmat
10 import datetime as dt
11 import sys
12 from time import time
14 if len(sys.argv) != 4:
15 print 'Error: python %s wrfout start_time days' % sys.argv[0]
16 print ' * wrfout - string, wrfout file of WRF-SFIRE simulation'
17 print ' * start_time - string, YYYYMMDDHHMMSS where: '
18 print ' YYYY - year'
19 print ' MM - month'
20 print ' DD - day'
21 print ' HH - hour'
22 print ' MM - minute'
23 print ' SS - second'
24 print ' * days - float, number of days of simulation (can be less than a day)'
25 sys.exit(0)
27 t_init = time()
29 satellite_file = 'data'
30 fire_file = 'fire_detections.kml'
31 ground_file = 'nofire.kml'
32 bounds_file = 'result.mat'
33 svm_file = 'svm.mat'
34 contour_file = 'perimeters_svm.kml'
36 print ''
37 if os.path.isfile(bounds_file) and os.access(bounds_file,os.R_OK):
38 print '>> Reading the fire mesh <<'
39 sys.stdout.flush()
40 fxlon,fxlat,bbox,time_esmf=read_fire_mesh(sys.argv[1])
42 print ''
43 print '>> File %s already created! Skipping all satellite processing <<' % bounds_file
44 print 'Loading from %s...' % bounds_file
45 result = loadmat(bounds_file)
46 # Taking necessary variables from result dictionary
47 scale = result['time_scale_num'][0]
48 time_num_granules = result['time_num_granules'][0]
49 else:
50 if os.path.isfile(satellite_file) and os.access(satellite_file,os.R_OK):
51 print '>> File %s already created! Skipping satellite retrieval <<' % satellite_file
52 print 'Loading from %s...' % satellite_file
53 data,fxlon,fxlat,time_num=sl.load(satellite_file)
54 bbox = [fxlon.min(),fxlon.max(),fxlat.min(),fxlat.max()]
55 else:
56 print '>> Reading the fire mesh <<'
57 sys.stdout.flush()
58 fxlon,fxlat,bbox,time_esmf=read_fire_mesh(sys.argv[1])
59 # converting times to ISO
60 dti=dt.datetime.strptime(sys.argv[2],'%Y%m%d%H%M%S')
61 time_start_iso='%d-%02d-%02dT%02d:%02d:%02dZ' % (dti.year,dti.month,dti.day,dti.hour,dti.minute,dti.second)
62 dtf=dti+dt.timedelta(days=float(sys.argv[3]))
63 time_final_iso='%d-%02d-%02dT%02d:%02d:%02dZ' % (dtf.year,dtf.month,dtf.day,dtf.hour,dtf.minute,dtf.second)
64 time_iso=(time_start_iso,time_final_iso)
66 print ''
67 print '>> Retrieving satellite data <<'
68 sys.stdout.flush()
69 data=retrieve_af_data(bbox,time_iso)
71 print ''
72 print '>> Saving satellite data file (data) <<'
73 sys.stdout.flush()
74 time_num=map(time_iso2num,time_iso)
75 sl.save((data,fxlon,fxlat,time_num),satellite_file)
76 print 'data file saved correctly!'
78 print ''
79 ffile = (os.path.isfile(fire_file) and os.access(fire_file,os.R_OK))
80 gfile = (os.path.isfile(ground_file) and os.access(ground_file,os.R_OK))
81 if (not ffile) or (not gfile):
82 print '>> Generating KML of fire and ground detections <<'
83 sys.stdout.flush()
84 # sort the granules by dates
85 sdata=sort_dates(data)
86 tt=[ dd[1]['time_num'] for dd in sdata ] # array of times
87 if ffile:
88 print '>> File %s already created! <<' % fire_file
89 else:
90 # writting fire detections file
91 print 'writting KML with fire detections'
92 keys=['latitude','longitude','brightness','scan','track','acq_date','acq_time','satellite','instrument','confidence','bright_t31','frp','scan_angle']
93 dkeys=['lat_fire','lon_fire','brig_fire','scan_fire','track_fire','acq_date','acq_time','sat_fire','instrument','conf_fire','t31_fire','frp_fire','scan_angle_fire']
94 prods={'AF':'Active Fires','FRP':'Fire Radiative Power'}
95 N=[len(d[1]['lat_fire']) for d in sdata]
96 json=sdata2json(sdata,keys,dkeys,N)
97 json2kml(json,fire_file,bbox,prods)
98 if gfile:
99 print '>> File %s already created! <<' % ground_file
100 else:
101 # writting ground detections file
102 print 'writting KML with ground'
103 keys=['latitude','longitude','scan','track','acq_date','acq_time','satellite','instrument','scan_angle']
104 dkeys=['lat_nofire','lon_nofire','scan_nofire','track_nofire','acq_date','acq_time','sat_fire','instrument','scan_angle_nofire']
105 prods={'NF':'No Fire'}
106 N=[len(d[1]['lat_nofire']) for d in sdata]
107 json=sdata2json(sdata,keys,dkeys,N)
108 json2kml(json,ground_file,bbox,prods)
110 print ''
111 print '>> Processing satellite data <<'
112 sys.stdout.flush()
113 result = process_satellite_detections(data,fxlon,fxlat,time_num)
114 # Taking necessary variables from result dictionary
115 scale = result['time_scale_num']
116 time_num_granules = result['time_num_granules']
118 lon = result['fxlon']
119 lat = result['fxlat']
120 U = np.array(result['U']).astype(float)
121 L = np.array(result['L']).astype(float)
122 T = np.array(result['T']).astype(float)
124 print ''
125 print '>> Preprocessing the data <<'
126 sys.stdout.flush()
127 X,y=preprocess_data_svm(lon,lat,U,L,T,scale,time_num_granules)
129 print ''
130 print '>> Running Support Vector Machine <<'
131 sys.stdout.flush()
132 C = 10.
133 kgam = 10.
134 F = SVM3(X,y,C=C,kgam=kgam,fire_grid=(lon,lat))
136 print ''
137 print '>> Saving the results <<'
138 sys.stdout.flush()
139 tscale = 24*3600
140 # Creating the dictionary with the results
141 svm = {'dxlon': lon, 'dxlat': lat, 'U': U/tscale, 'L': L/tscale,
142 'fxlon': F[0], 'fxlat': F[1], 'fmc_g': F[2],
143 'tscale': tscale, 'time_num_granules': time_num_granules,
144 'time_scale_num': scale}
145 # Save resulting file
146 sio.savemat('svm.mat', mdict=svm)
147 print 'The results are saved in svm.mat file'
149 print ''
150 print '>> Computing contour lines of the fire arrival time <<'
151 print 'Computing the contours...'
152 # Scale fire arrival time
153 fmc_g = F[2]*tscale+scale[0]
154 # Granules numeric times
155 data = get_contour_verts(F[0], F[1], fmc_g, time_num_granules, contour_dt_hours=6, contour_dt_init=6, contour_dt_final=6)
156 print 'Creating the KML file...'
157 # Creating the KML file
158 contour2kml(data,'perimeters_svm.kml')
159 print 'The resulting contour lines are saved in perimeters_svm.kml file'
161 print ''
162 print '>> DONE <<'
163 t_final = time()
164 print 'Elapsed time for all the process: %ss.' % str(abs(t_final-t_init))
165 sys.exit()