-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_stack.py
More file actions
276 lines (231 loc) · 11.7 KB
/
Copy pathplot_stack.py
File metadata and controls
276 lines (231 loc) · 11.7 KB
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
import os
import numpy
import pickle
from swerve import config, subset, read_info_dict, read_info_df
from swerve import plt_config, savefig, savefig_paper
import matplotlib.pyplot as plt
from datetick import datetick
CONFIG = config()
logger = CONFIG['logger'](**CONFIG['logger_kwargs'])
DATA_DIR = CONFIG['dirs']['data']
base_dir = 'data_processed\summary'
limits = CONFIG['limits']
plt_config()
def read(all_file, sid=None):
info_dict = read_info_dict()
info_df = read_info_df(exclude_errors=True, extended=True)
print(f"Reading {all_file}")
with open(all_file, 'rb') as f:
data = pickle.load(f)
return info_dict, info_df, data
def stack_plot_config(axes, data_with_offset, units, offset=40):
plt.grid()
plt.gca().yaxis.set_major_locator(plt.MultipleLocator(offset))
#plt.legend(loc='upper right')
plt.gca().yaxis.set_ticklabels([]) # Remove y-tick labels
plt.gca().set_xlim(limits['plot'][0], limits['plot'][1])
plt.gca().set_ylim(-offset, max(data_with_offset)+10)
axes.spines['top'].set_visible(False)
axes.spines['right'].set_visible(False)
axes.spines['left'].set_visible(False)
#axes.spines['bottom'].set_position(('outward', 10)) # Adjust position of x-axis
axes.yaxis.set_ticks_position('none') # Remove y-axis ticks
datetick()
# remove first x gridline
xgridlines = axes.get_xgridlines()
gridline_of_interest = xgridlines[0]
gridline_of_interest.set_visible(False)
# Add a vertical scale bar on the right side
xmax = axes.get_xlim()[1]
xbar = xmax - (xmax - axes.get_xlim()[0]) * 0.01 # 1% from right edge
axes.plot([xbar, xbar], [0, -offset], color='k', linewidth=1, clip_on=False)
# Add caps
cap_width = 0.01 * (xmax - axes.get_xlim()[0])
axes.plot([xbar - cap_width/2, xbar + cap_width/2], [0, 0], color='k', linewidth=1, clip_on=False)
axes.plot([xbar - cap_width/2, xbar + cap_width/2], [-offset, -offset], color='k', linewidth=1, clip_on=False)
# Add text label
axes.text(xbar - cap_width, -offset/2, f'{offset} {units}', fontsize=plt.rcParams['ytick.labelsize'], verticalalignment='center', horizontalalignment='right')
def plot_all_gic(info, info_df, data_all, data_source=['TVA','NERC'], offset=40):
# note NERC sites that are TVA duplicates
units = '(A)'
sid_copies = CONFIG['sid_duplicates'] if 'sid_duplicates' in CONFIG else {}
for source in data_source:
logger.info(f"Plotting {source} GIC sites")
sids = info_df[(info_df['data_source']==source)]['site_id'].unique()
source_sites = {'sites': [], 'lat': [], 'lon': []}
for sid in sids:
sid_str = str(sid) # TODO: only necessary for October storm data
if sid_str not in data_all.keys():
logger.error(f"Site {sid} not found in data_all, rerun main.py to generate all.pkl")
elif 'GIC' in data_all[sid_str] and source in info[sid_str]['GIC']['measured']:
site_info = info_df[
(info_df['site_id'] == sid) &
(info_df['data_type'] == 'GIC') &
(info_df['data_class'] == 'measured')
]
source_sites['sites'].append(sid_str)
source_sites['lat'].append(site_info['mag_lat'].values)
source_sites['lon'].append(site_info['mag_lon'].values)
# Sort sites by latitude
if not source_sites['lat'] and not source_sites['lon']:
logger.warning(f" No sites found for source {source}. Skipping.")
continue
sorted_sites = sorted(zip(source_sites['lat'], source_sites['sites'], source_sites['lon']))
source_sites['lat'], source_sites['sites'], source_sites['lon'] = zip(*sorted_sites)
if source == 'NERC':
fig, axes = plt.subplots(1, 1, figsize=(8.5, 11))
elif source == 'TVA':
fig, axes = plt.subplots(1, 1, figsize=(8.5, 5))
offset_fix = 0
for i, sid in enumerate(source_sites['sites']):
if sid in sid_copies.keys():
offset_fix +=1
continue # skipping NERC sites that are TVA duplicates
if 'GIC' in data_all[sid].keys() and source in info[sid]['GIC']['measured']:
time = data_all[sid]['GIC']['measured'][source]['original']['time']
data = data_all[sid]['GIC']['measured'][source]['original']['data']
# Subset to desired time range
time, data = subset(time, data, limits['data'][0], limits['data'][1])
# Add offset for each site
data_with_offset = data + (i * offset) - (offset_fix * offset)
# Plot the timeseries
axes.plot(time, data_with_offset, linewidth=0.5)
# Add text to the plot to label waveform
sid_lat = source_sites['lat'][i].item()
sid_lon = source_sites['lon'][i].item()
text = f'{sid}\n({sid_lat:.1f},{sid_lon:.1f})'
if sid in sid_copies.values():
text = f'{sid}*\n({sid_lat:.1f},{sid_lon:.1f})'
axes.text(limits['plot'][0], (i*offset)-(offset_fix*offset), text, fontsize=11, verticalalignment='center', horizontalalignment='left')
stack_plot_config(axes, data_with_offset, units, offset=offset)
# Save the figure
fdir = os.path.join(base_dir, f'_{source.lower()}')
savefig(fdir, f'gic_{source.lower()}', logger)
savefig_paper(os.path.join('figures', fdir), f'gic_{source.lower()}', logger) if 'paper' in CONFIG['dirs'] else None
plt.close()
def plot_all_db(info, info_df, data_all, offset=400):
units = '(nT)'
info_df['site_id'] = info_df['site_id'].astype(str)
info_df = info_df[(info_df['data_type']=='B')]
sids = info_df[~(info_df['data_source']=='TEST')]['site_id'].unique()
# note NERC sites that are TVA duplicates
sid_copies = CONFIG['sid_duplicates'] if 'sid_duplicates' in CONFIG else {}
logger.info("Plotting all dB sites")
source_sites = {'sites': [], 'lat': [], 'lon': []}
for sid in sids:
if 'measured' in info[sid]['B']:
site_info = info_df[
(info_df['site_id'] == sid) &
(info_df['data_type'] == 'B') &
(info_df['data_class'] == 'measured')
]
source_sites['sites'].append(sid)
source_sites['lat'].append(site_info['mag_lat'].values[0])
source_sites['lon'].append(site_info['mag_lon'].values[0])
# Sort sites by latitude
sorted_sites = sorted(zip(source_sites['lat'], source_sites['sites'], source_sites['lon']))
source_sites['lat'], source_sites['sites'], source_sites['lon'] = zip(*sorted_sites)
fig, axes = plt.subplots(1, 1, figsize=(8.5, 11))
offset_fix = 0
for i, sid in enumerate(source_sites['sites']):
if sid in sid_copies.keys():
offset_fix +=1
continue # skipping NERC sites that are TVA duplicates
if 'B' in data_all[sid].keys():
source = info_df[
(info_df['site_id'] == sid) &
(info_df['data_type'] == 'B') &
(info_df['data_class'] == 'measured')
]['data_source'].values[0] #TODO: find a simpler way to get data source
time_b = data_all[sid]['B']['measured'][source]['modified']['time']
data = data_all[sid]['B']['measured'][source]['modified']['data']
# Subset to desired time range
time_b, data = subset(time_b, data, limits['data'][0], limits['data'][1])
data = numpy.linalg.norm(data, axis=1)
# Add offset for each site
data_with_offset = data + (i*offset) - (offset_fix*offset)
# Plot the timeseries
axes.plot(time_b, data_with_offset, linewidth=0.5)
# Add text to the plot to label waveform
sid_lat = source_sites['lat'][i]
sid_lon = source_sites['lon'][i]
text = f'{sid}\n({sid_lat:.1f},{sid_lon:.1f})'
if sid in sid_copies.values():
text = f'{sid}*\n({sid_lat:.1f},{sid_lon:.1f})'
axes.text(limits['plot'][0], (i*offset)-(offset_fix*offset), text,
fontsize=11, verticalalignment='center', horizontalalignment='left')
stack_plot_config(axes, data_with_offset, units, offset=offset)
# Save the figure
fdir = os.path.join(base_dir, '_db')
savefig(fdir, 'db_all', logger)
savefig_paper(os.path.join('figures', fdir), 'db_all', logger)
plt.close()
def plot_bad_gic(info, info_df, data_all, offset=40, sites_per_fig=25):
from swerve import sids
# note NERC sites that are TVA duplicates
units = '(A)'
sites = sids(data_type='GIC', data_class='measured', exclude_errors=False)
source_sites = {'sites': [], 'error':[], 'lat': [], 'lon': []}
for sid in sites:
sid_str = str(sid) # TODO: only necessary for October storm data
source = list(info[sid]['GIC']['measured'].keys())[0]
error_val = info[sid_str]['GIC']['measured'][source][sid_str]['manual_error']
if error_val is None or (isinstance(error_val, float) and numpy.isnan(error_val)):
continue
source_sites['error'].append(error_val)
if sid_str not in data_all.keys():
logger.error(f"Site {sid} not found in data_all, rerun main.py to generate all.pkl")
elif 'GIC' in data_all[sid_str]:
site_info = info_df[
(info_df['site_id'] == sid) &
(info_df['data_type'] == 'GIC') &
(info_df['data_class'] == 'measured')
]
source_sites['sites'].append(sid_str)
# Get lat/lon if available, else use NaN
lat = site_info['mag_lat'].values[0] if len(site_info['mag_lat'].values) > 0 else numpy.nan
lon = site_info['mag_lon'].values[0] if len(site_info['mag_lon'].values) > 0 else numpy.nan
source_sites['lat'].append(lat)
source_sites['lon'].append(lon)
sites_per_fig = sites_per_fig
total_sites = len(source_sites['sites'])
fig, axes = plt.subplots(1, 1, figsize=(8.5, 11))
offset_fix = 0
fig_count = 1
for i, sid in enumerate(source_sites['sites']):
# Start a new figure every 30 sites (except for the first iteration)
if i > 0 and i % sites_per_fig == 0:
stack_plot_config(axes, data_with_offset, units, offset=offset)
fdir = os.path.join(base_dir, f'_rejected')
savefig(fdir, f'gic_rejected_{fig_count}', logger)
plt.close()
fig_count += 1
fig, axes = plt.subplots(1, 1, figsize=(8.5, 11))
offset_fix = 0 # Reset offset_fix for each new figure
source = list(info[sid]['GIC']['measured'].keys())[0] if info[sid]['GIC']['measured'] else None
if source and 'GIC' in data_all[sid].keys() and source in info[sid]['GIC']['measured']:
time = data_all[sid]['GIC']['measured'][source]['original']['time']
data = data_all[sid]['GIC']['measured'][source]['original']['data']
# Subset to desired time range
time, data = subset(time, data, limits['data'][0], limits['data'][1])
# Add offset for each site (relative to current figure)
idx_in_fig = i % sites_per_fig
data_with_offset = data + (idx_in_fig * offset) - (offset_fix * offset)
# Plot the timeseries
axes.plot(time, data_with_offset, linewidth=0.5)
# Add text to the plot to label waveform
sid_lat = source_sites['lat'][i]
sid_lon = source_sites['lon'][i]
text = f'{sid}\n{source_sites['error'][i]}'
axes.text(limits['plot'][0], (idx_in_fig * offset) - (offset_fix * offset), text,
fontsize=11, verticalalignment='center', horizontalalignment='left')
# Save the last figure if there were any sites plotted
if total_sites % sites_per_fig != 0 or total_sites == 0:
stack_plot_config(axes, data_with_offset, units, offset=offset)
fdir = os.path.join(base_dir, f'_rejected')
savefig(fdir, f'gic_rejected_{fig_count}', logger)
plt.close()
info_dict, info_df, data_all = read(CONFIG['files']['all'])
plot_all_gic(info_dict, info_df, data_all)
plot_all_db(info_dict, info_df, data_all)
plot_bad_gic(info_dict, info_df, data_all)