@@ -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'
0 commit comments