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