-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjedi_config.py
324 lines (248 loc) · 13.1 KB
/
jedi_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# Standard modules
import os
from astropy.time import Time
from scipy.io.idl import readsav
import numpy as np
import pandas as pd
from collections import OrderedDict
import itertools
# Custom modules
from jpm_logger import JpmLogger
# Declare variables to be accessed by anything that imports this module
eve_data_path = '/Users/jmason86/Dropbox/Research/Data/EVE/eve_lines_2010121-2014146 MEGS-A Mission Bare Bones.sav'
goes_data_path = '/Users/jmason86/Dropbox/Research/Data/GOES/events/GoesEventsC1MinMegsAEra.sav'
output_path = '/Users/jmason86/Dropbox/Research/Postdoc_NASA/Analysis/Coronal Dimming Analysis/JEDI Catalog/'
logger_filename = 'generate_jedi_catalog'
threshold_time_prior_flare_minutes = 300.0
dimming_window_relative_to_flare_minutes_left = -1.0
dimming_window_relative_to_flare_minutes_right = 1440.0
threshold_minimum_dimming_window_minutes = 120.0
n_events = 5052
n_threads = 6 # The number of threads to use when doing parallel processing tasks
verbose = True # Set to log the processing messages to disk and console.
eve_lines = None
goes_flare_events = None
logger = None
jedi_hdf_filename = None
preflare_csv_filename = None
all_minutes_since_last_flare = None
preflare_indices = None
ion_tuples = None
ion_permutations = None
def init():
"""Initialize the jedi catalog: load the data
Inputs:
None.
Optional Inputs:
None
Outputs:
All outputs are globals accessible by doing import jedi_config
logger [JpmLogger]: A configurable log that can optionally also print to console.
all_minutes_since_last_flare [numpy float array]: The amount of time between each flare.
preflare_indices [numpy int array]: The indices where flares are considered time-independent.
Optional Outputs:
None
Example:
jedi_config.init()
"""
global logger, all_minutes_since_last_flare, preflare_indices
# Initialize logger
logger = JpmLogger(filename=logger_filename, path=output_path, console=False)
logger.info('Logger initialized.')
# Set up folders
init_folders()
# Set up filenames
init_filenames()
# Load the EVE data
load_eve_data()
# Get GOES flare events above C1 within date range corresponding to EVE data
load_goes_flare_event_data()
# Compute the amount of time between all flares [minutes]
peak_time = goes_flare_events['peak_time']
all_minutes_since_last_flare = (peak_time[1:] - peak_time[0:-1]).sec / 60.0
# Figure out which flares are independent, store those indices
is_flare_independent = all_minutes_since_last_flare > threshold_time_prior_flare_minutes
preflare_indices = np.where(is_flare_independent)[0] + 1 # Add 1 to map back to event index and not to the differentiated vector
logger.info('Found {0} independent flares of {1} total flares given a time separation of {2} minutes.'.format(len(preflare_indices), len(is_flare_independent), threshold_time_prior_flare_minutes))
def init_folders():
"""Internal-use function to check if necessary folders exist; if not, create them
Inputs:
None. Draws from the globals at the top of this file.
Optional Inputs:
None.
Outputs:
No return. Creates folders on disk.
Optional Outputs:
None.
Example:
init_folders()
"""
if not os.path.exists(output_path):
os.makedirs(output_path)
if not os.path.exists(output_path + 'Processed Pre-Parameterization Data'):
os.makedirs(output_path + 'Processed Pre-Parameterization Data')
if not os.path.exists(output_path + 'Processed Lines Data'):
os.makedirs(output_path + 'Processed Lines Data')
if not os.path.exists(output_path + 'Peak Subtractions'):
os.makedirs(output_path + 'Peak Subtractions')
if not os.path.exists(output_path + 'Fitting'):
os.makedirs(output_path + 'Fitting')
if not os.path.exists(output_path + 'Depth'):
os.makedirs(output_path + 'Depth')
if not os.path.exists(output_path + 'Slope'):
os.makedirs(output_path + 'Slope')
if not os.path.exists(output_path + 'Duration'):
os.makedirs(output_path + 'Duration')
if not os.path.exists(output_path + 'Summary Plots'):
os.makedirs(output_path + 'Summary Plots')
def init_filenames():
"""Internal-use function to set filename globals
These are variables that themselves depend on other globals being defined already but are fully determined at that point.
Inputs:
None. Draws from the globals at the top of this file.
Optional Inputs:
None.
Outputs:
No return. Updates global variables.
jedi_hdf_filename [str]: The unique path/filename for the jedi catalog in this run to be stored in on disk.
preflare_csv_filename [str]: The path/filename of the computed pre-flare irradiances.
Optional Outputs:
None.
Example:
init_filenames()
"""
global jedi_hdf_filename, preflare_csv_filename
jedi_hdf_filename = output_path + 'jedi_{0}'.format(Time.now().iso)
preflare_csv_filename = os.path.join(output_path, 'Preflare Determination/Preflare Irradiances.csv')
def load_eve_data():
"""Internal-use function to load and clean the SDO/EVE data.
Inputs:
None. Draws from the globals at the top of this file.
Optional Inputs:
None.
Outputs:
No return. Updates global variables.
eve_lines [pandas DataFrame]: SDO/EVE level 2 lines data. Stores irradiance, time, and wavelength.
Optional Outputs:
None.
Example:
load_eve_data()
"""
global eve_lines
# TODO: Replace this shortcut method with the method I'm building into sunpy
logger.info('Loading EVE data.')
eve_readsav = readsav(eve_data_path)
irradiance = eve_readsav['irradiance'].byteswap().newbyteorder() # pandas doesn't like big endian
irradiance[irradiance == -1] = np.nan
wavelengths = eve_readsav['wavelength']
wavelengths_str = []
[wavelengths_str.append('{0:1.1f}'.format(wavelength)) for wavelength in wavelengths]
eve_lines = pd.DataFrame(irradiance, columns=wavelengths_str)
eve_lines.index = pd.to_datetime(eve_readsav.iso.astype(str))
eve_lines.sort_index(inplace=True)
eve_lines = eve_lines.drop_duplicates()
def load_goes_flare_event_data():
"""Internal-use function to load and clean the GOES/XRS flare event data from NOAA/SWPC.
Inputs:
None. Draws from the globals at the top of this file.
Optional Inputs:
None.
Outputs:
No return. Updates global variables.
goes_flare_events[pandas DataFrame]: Flares as observed by GOES/XRS. Store class, start and peak time
Optional Outputs:
None.
Example:
load_eve_data()
"""
global goes_flare_events
# flares = get_goes_flare_events(eve_lines.index[0], eve_lines.index[-1]) # TODO: The method in sunpy needs fixing, issue 2434
# Load GOES events from IDL saveset instead of directly through sunpy
logger.info('Loading GOES flare events.')
goes_flare_events = readsav(goes_data_path)
goes_flare_events['class'] = goes_flare_events['class'].astype(str)
goes_flare_events['event_peak_time_human'] = goes_flare_events['event_peak_time_human'].astype(str)
goes_flare_events['event_start_time_human'] = goes_flare_events['event_start_time_human'].astype(str)
goes_flare_events['peak_time'] = Time(goes_flare_events['event_peak_time_jd'], format='jd', scale='utc', precision=0)
goes_flare_events['start_time'] = Time(goes_flare_events['event_start_time_jd'], format='jd', scale='utc', precision=0)
def init_jedi_row():
"""Internal-use function for defining the column headers in the JEDI catalog
Inputs:
None. Draws from the globals set up in init. So you must run the init function before calling this function.
Optional Inputs:
None
Outputs:
jedi_row [pandas DataFrame]: A ~24k column DataFrame with only a single row populated with np.nan's.
Optional Outputs:
None
Example:
jedi_row = init_jedi_row()
"""
jedi_row = pd.DataFrame([OrderedDict([
('Event #', np.nan),
('GOES Flare Start Time', np.nan),
('GOES Flare Peak Time', np.nan),
('GOES Flare Class', np.nan),
('Pre-Flare Start Time', np.nan),
('Pre-Flare End Time', np.nan),
('Flare Interrupt', np.nan),
('Flare Latitude [deg]', np.nan),
('Flare Longitude [deg]', np.nan),
('Flare Position Angle [deg]', np.nan)])])
# Define the combination of columns of the JEDI catalog
global ion_tuples, ion_permutations
ion_tuples = list(itertools.permutations(eve_lines.columns.values, 2))
ion_permutations = pd.Index([' by '.join(ion_tuples[i]) for i in range(len(ion_tuples))])
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Pre-Flare Irradiance [W/m2]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Slope Start Time'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Slope End Time'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Slope Min [%/s]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Slope Max [%/s]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Slope Mean [%/s]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Slope Uncertainty [%/s]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Depth First Time'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Depth First [%]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Depth Max Time'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Depth Max [%]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Depth Uncertainty [%]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Duration Start Time'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Duration End Time'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Duration [s]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Fitting Gamma'))
jedi_row = jedi_row.join(pd.DataFrame(columns=eve_lines.columns + ' Fitting Score'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Slope Start Time'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Slope End Time'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Slope Min [%/s]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Slope Max [%/s]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Slope Mean [%/s]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Slope Uncertainty [%/s]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Depth First Time'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Depth First [%]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Depth Max Time'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Depth Max [%]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Depth Uncertainty [%]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Duration Start Time'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Duration End Time'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Duration [s]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Correction Time Shift [s]'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Correction Scale Factor'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Fitting Gamma'))
jedi_row = jedi_row.join(pd.DataFrame(columns=ion_permutations + ' Fitting Score'))
# Force the dtypes to numeric rather than objects
jedi_row = jedi_row.apply(pd.to_numeric, errors='ignore')
return jedi_row
def write_new_jedi_file_to_disk(jedi_row):
"""Write a jedi_row to disk as a csv file -- intended to be called after first initialization of jedi_row
Inputs:
jedi_row [pandas DataFrame]: The JEDI DataFrame to be written
Optional Inputs:
None
Outputs:
csv file on disk
Optional Outputs:
None
Example:
jedi_row = init_jedi_row()
write_new_jedi_file_to_disk(jedi_row)
"""
jedi_row.to_hdf(jedi_hdf_filename, key='jedi', mode='w')