process satellite data from path
[JPSSData.git] / utils.py
bloba50ab25313596604027eb8ae5aaf79b6e484e19c
1 # Copyright (C) 2013-2016 Martin Vejmelka, UC Denver
3 import json,sys
5 class Dict(dict):
6 """
7 A dictionary that allows member access to its keys.
8 A convenience class.
9 """
11 def __init__(self, d):
12 """
13 Updates itself with d.
14 """
15 self.update(d)
17 def __getattr__(self, item):
18 try:
19 return self[item]
20 except KeyError:
21 raise AttributeError(item)
23 def __setattr__(self, item, value):
24 self[item] = value
26 def load_cfg():
27 # load the system configuration
28 cfg = Dict([])
29 try:
30 f_cfg = Dict(json.load(open('conf.json')))
31 except IOError:
32 print 'Warning: any conf.json file specified, creating defaults...'
33 f_cfg = Dict([])
35 # Set default method settings
36 try:
37 # minimum confidence level for the satellite pixels to be considered
38 cfg.minconf = f_cfg['method_settings'].get('minconf',70)
39 # use 3D space-time points (or bounds)
40 cfg.cloud = f_cfg['method_settings'].get('cloud',True)
41 # dynamic penalization term?
42 cfg.dyn_pen = f_cfg['method_settings'].get('dyn_pen',False)
43 # if not so, 5-fold cross validation for C and gamma?
44 if cfg.dyn_pen:
45 cfg.search = False
46 else:
47 cfg.search = f_cfg['method_settings'].get('search',False)
48 # interpolation of the results into fire mesh (if apply to spinup case)
49 cfg.fire_interp = f_cfg['method_settings'].get('fire_interp',False)
50 except:
51 cfg.minconf = 70
52 cfg.cloud = True
53 cfg.dyn_pen = False
54 cfg.search = False
55 cfg.fire_interp = False
57 # Set default data paths
58 try:
59 # if ignitions are known: ([lons],[lats],[dates]) where lons and lats in degrees and dates in ESMF format
60 # examples: "igns" : "([100],[45],['2015-05-15T20:09:00'])" or "igns" : "([100,105],[45,39],['2015-05-15T20:09:00','2015-05-15T23:09:00'])"
61 cfg.igns = eval(f_cfg['data_paths'].get('igns','None'))
62 # if infrared perimeters: path to KML files
63 # examples: perim_path = './pioneer/perim'
64 cfg.perim_path = f_cfg['data_paths'].get('perim_path','')
65 # if forecast wrfout: path to netcdf wrfout forecast file
66 # example: forecast_path = './patch/wrfout_patch'
67 cfg.forecast_path = f_cfg['data_paths'].get('forecast_path','')
68 except:
69 cfg.igns = None
70 cfg.perim_path = ''
71 cfg.forecast_path = ''
73 # Set default plot settings
74 try:
75 # plot observed information (googlearth.kmz with png files)
76 cfg.plot_observed = f_cfg['plot_settings'].get('plot_observed',False)
77 # if so, only fire detections?
78 if cfg.plot_observed:
79 cfg.only_fire = f_cfg['plot_settings'].get('only_fire',False)
80 else:
81 cfg.only_fire = False
82 except:
83 cfg.plot_observed = False
84 cfg.only_fire = False
86 # Set SVM default settings
87 cfg.svm_settings = Dict([])
88 # Plot settings
89 try:
90 # plot original data
91 cfg.svm_settings.plot_data = f_cfg['plot_settings']['plot_svm'].get('plot_data',False)
92 # plot scaled data with artificial data
93 cfg.svm_settings.plot_scaled = f_cfg['plot_settings']['plot_svm'].get('plot_scaled',False)
94 # plot decision volume
95 cfg.svm_settings.plot_decision = f_cfg['plot_settings']['plot_svm'].get('plot_decision',False)
96 # plot polynomial approximation
97 cfg.svm_settings.plot_poly = f_cfg['plot_settings']['plot_svm'].get('plot_poly',False)
98 # plot full hyperplane vs detections with support vectors
99 cfg.svm_settings.plot_supports = f_cfg['plot_settings']['plot_svm'].get('plot_supports',False)
100 # plot resulting fire arrival time vs detections
101 cfg.svm_settings.plot_result = f_cfg['plot_settings']['plot_svm'].get('plot_result',False)
102 except:
103 cfg.svm_settings.plot_data = False
104 cfg.svm_settings.plot_scaled = False
105 cfg.svm_settings.plot_decision = False
106 cfg.svm_settings.plot_poly = False
107 cfg.svm_settings.plot_supports = False
108 cfg.svm_settings.plot_result = False
109 # Method settings
110 try:
111 # normalize the input data between 0 and 1
112 cfg.svm_settings.norm = f_cfg['method_settings']['svm_settings'].get('norm',True)
113 # if not Nans in the data are wanted (all Nans are going to be replaced by the maximum value)
114 cfg.svm_settings.notnan = f_cfg['method_settings']['svm_settings'].get('notnan',True)
115 # artificial creation of clear ground detections under real (preprocessing)
116 cfg.svm_settings.artil = f_cfg['method_settings']['svm_settings'].get('artil',False)
117 # if so, normalized vertical resolution (from 0 to 1) of the aggregation
118 if cfg.svm_settings.artil:
119 cfg.svm_settings.hartil = f_cfg['method_settings']['svm_settings'].get('hartil',.2)
120 else:
121 cfg.svm_settings.hartil = 0
122 # artificial creation of fire detections above real (preprocessing)
123 cfg.svm_settings.artiu = f_cfg['method_settings']['svm_settings'].get('artiu',True)
124 # if so, normalized vertical resolution (from 0 to 1) of the aggregation
125 if cfg.svm_settings.artiu:
126 cfg.svm_settings.hartiu = f_cfg['method_settings']['svm_settings'].get('hartiu',.1)
127 else:
128 cfg.svm_settings.hartiu = 0
129 # creation of an artificial mesh of clear ground detections at the bottom
130 cfg.svm_settings.downarti = f_cfg['method_settings']['svm_settings'].get('downarti',True)
131 # if so, how to the bottom normalized between 0 and 1 and confidence level of the artificial detections
132 if cfg.svm_settings.downarti:
133 cfg.svm_settings.dminz = f_cfg['method_settings']['svm_settings'].get('dminz',.1)
134 cfg.svm_settings.confal = f_cfg['method_settings']['svm_settings'].get('confal',100)
135 else:
136 cfg.svm_settings.dminz = 0
137 cfg.svm_settings.confal = 0
138 # creation of an artificial mesh of fire detections at the top
139 cfg.svm_settings.toparti = f_cfg['method_settings']['svm_settings'].get('toparti',False)
140 # if so, how to the top normalized between 0 and 1 and confidence level of the artificial detections
141 if cfg.svm_settings.toparti:
142 cfg.svm_settings.dmaxz = f_cfg['method_settings']['svm_settings'].get('dmaxz',.1)
143 cfg.svm_settings.confau = f_cfg['method_settings']['svm_settings'].get('confau',100)
144 else:
145 cfg.svm_settings.dmaxz = 0
146 cfg.svm_settings.confau = 0
147 except:
148 cfg.svm_settings.notnan = True
149 cfg.svm_settings.artil = False
150 cfg.svm_settings.hartil = 0
151 cfg.svm_settings.artiu = True
152 cfg.svm_settings.hartiu = .1
153 cfg.svm_settings.downarti = True
154 cfg.svm_settings.dminz = .1
155 cfg.svm_settings.confal = 100
156 cfg.svm_settings.toparti = False
157 cfg.svm_settings.dmaxz = 0
158 cfg.svm_settings.confau = 0
159 cfg.svm_settings.search = cfg.search
161 # Set AppKey for NRT downloads from https://nrt3.modaps.eosdis.nasa.gov/profile/app-keys
162 try:
163 cfg.appkey = f_cfg.get('appkey',None)
164 except:
165 cfg.appkey = None
167 return cfg