Skip to content

Commit 5392d52

Browse files
author
R Bailey
committed
Added function for weighted averages (useful for newell coupling plotting) and changed plotting to use it.
1 parent b45ee22 commit 5392d52

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

predstorm/data.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,49 @@ def cut(self, starttime=None, endtime=None):
611611
return self
612612

613613

614+
def get_weighted_average(self, key, past_timesteps=4, past_weights=0.65):
615+
"""
616+
Calculates a weighted average of speed and magnetic field bx, by, bz and the Newell coupling ec
617+
for a number of ave_hours (4 by default) back in time
618+
input data time resolution should be 1 hour
619+
aurora output time resolution as given by dt can be higher
620+
corresponds roughly to ap_inter_sol.pro in IDL ovation
621+
622+
Parameters
623+
==========
624+
self : ...
625+
key : str
626+
String of key to return average for.
627+
past_timesteps : int (default=4)
628+
Timesteps previous to integrate over, usually 4 (hours)
629+
past_weights : float (default=0.65)
630+
Reduce weights by factor with each hour back
631+
632+
Returns
633+
=======
634+
avg : np.ndarray
635+
Array containing averaged values. Same length as original.
636+
"""
637+
638+
if key not in self.vars:
639+
raise Exception("Key {} not available in this ({}) SatData object!".format(key, self.source))
640+
641+
avg = np.zeros((len(self)))
642+
643+
for t_ind, timestep in enumerate(self.data[0]):
644+
weights = np.ones(past_timesteps) #make array with weights
645+
for k in np.arange(1,past_timesteps,1):
646+
weights[k] = weights[k-1] * past_weights
647+
648+
t_inds_for_weights = np.arange(t_ind, t_ind-past_timesteps,-1)
649+
t_inds_for_weights[t_inds_for_weights < 0] = 0
650+
651+
#sum last hours with each weight and normalize
652+
avg[t_ind] = np.round(np.nansum(self[key][t_inds_for_weights]*weights) / np.nansum(weights),1)
653+
654+
return avg
655+
656+
614657
def make_hourly_data(self):
615658
"""Takes data with minute resolution and interpolates to hour.
616659
Uses .interp_to_time(). See that function for more usability options.
@@ -2329,12 +2372,12 @@ def save_to_file(filepath, wind=None, dst=None, aurora=None, kp=None, ec=None):
23292372
out[:,13] = dst['dst']
23302373
out[:,14] = kp['kp']
23312374
out[:,15] = aurora['aurora']
2332-
out[:,16] = ec['ec']
2375+
out[:,16] = ec['ec']/4421.
23332376

23342377
#description
23352378
column_vals = '{:>17}{:>16}{:>7}{:>7}{:>7}{:>7}{:>9}{:>9}{:>8}{:>7}{:>8}{:>12}'.format(
23362379
'Y m d H M S', 'matplotlib_time', 'B[nT]', 'Bx', 'By', 'Bz', 'N[ccm-3]', 'V[km/s]',
2337-
'Dst[nT]', 'Kp', 'AP[GW]', 'Ec[Wb/s]')
2380+
'Dst[nT]', 'Kp', 'AP[GW]', 'Ec/4421[(km/s)**(4/3)nT**(2/3)]')
23382381
time_cols_fmt = '%4i %2i %2i %2i %2i %2i %15.6f'
23392382
b_cols_fmt = 4*'%7.2f'
23402383
p_cols_fmt = '%9.0i%9.0i'

predstorm/plot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,11 @@ def plot_solarwind_and_dst_prediction(DSCOVR_data, STEREOA_data, DST_data, DSTPR
239239
axes.append(ax5)
240240

241241
# Plot solar wind density:
242-
plt.plot_date(newell_coupling['time'], newell_coupling['ec']/4421., '-', color=c_ec, label='Newell coupling',linewidth=1.5)
242+
avg_newell_coupling = newell_coupling.get_weighted_average('ec')
243+
plt.plot_date(newell_coupling['time'], avg_newell_coupling/4421., '-', color=c_ec, label='Newell coupling',linewidth=1.5)
243244
plt.ylabel('Newell Coupling / 4421\n$\mathregular{[(km/s)^{4/3} nT^{2/3}]}$',fontsize=fs_ylabel)
244245
# For y limits check where the maximum and minimum are for DSCOVR and STEREO taken together:
245-
plt.ylim([0,np.nanmax(newell_coupling['ec']/4421.)*1.1])
246+
plt.ylim([0,np.nanmax(avg_newell_coupling/4421.)*1.1])
246247

247248
# Indicate level of interest (Ec/4421 = 1.0)
248249
plt.plot_date([plotstart,plotend], [1,1],'--k', alpha=0.5, linewidth=1)

0 commit comments

Comments
 (0)