def load_EMIC_IMFs_and_dynspec(imf_start, imf_end, IA_filter=None): ''' Loads IMFs and dynamic spectra ''' _ti, _dat, _HM_mags, _dt, _xtime, _xfreq, _pow = get_mag_data(_time_start, _time_end, _probe, low_cut=_band_start, high_cut=_band_end) sample_rate = 1./_dt # Calculate IMFs, get instantaneous phase, frequency, amplitude print('Sifting IMFs and performing HHT') imfs, IPs, IFs, IAs = [], [], [], [] for ii, lbl in zip(range(3), ['x', 'y', 'z']): imf = emd.sift.sift(_dat[:, ii], sift_thresh=1e-2) IP, IF, IA = emd.spectra.frequency_transform(imf, sample_rate, 'hilbert') print(f'{imf.shape[1]} IMFs found for B{lbl}') imfs.append(imf) IPs.append(IP) IFs.append(IF) IAs.append(IA) # Snip IMFs to time st, en = ascr.boundary_idx64(_ti, imf_start, imf_end) for ii in range(3): _imf_t = _ti[st:en] IAs[ii] = IAs[ii][st:en] IFs[ii] = IFs[ii][st:en] IPs[ii] = IPs[ii][st:en] return _ti, _dat, _HM_mags, _imf_t, IAs, IFs, IPs, _xtime, _xfreq, _pow
def get_pc1_peaks(sfreq, spower, band_start, band_end, npeaks=None): ''' Returns integrated spower spectrum between band_start and band_end as well as index locations of the most prominent npeaks ''' fst, fen = ascr.boundary_idx64(sfreq, band_start, band_end) pc1_int_power = spower[fst:fen, :].sum(axis=0) peak_idx = ascr.find_peaks(pc1_int_power, npeaks=npeaks, sortby='prom') return pc1_int_power, peak_idx
spec)] / (kB * 1e6 * spice_dict['F{}DU_Density'.format(spec)]) this_temp = kB * t_perp_kelvin / q # Convert from K to eV spice_dens.append(this_dens) spice_temp.append(this_temp) spice_anis.append(this_anis) ### COMPARISON PLOTS FOR CHECKING ### import matplotlib.pyplot as plt # Plot things :: B0HM, cold density, HOPE/RBSPICE hot densities (4 plots) fig, axes = plt.subplots(4) # B0 B0 = np.sqrt(raw_mags[:, 0]**2 + raw_mags[:, 1]**2 + raw_mags[:, 2]**2) st, en = ascr.boundary_idx64(mag_times, _time_start, _time_end) _st, _en = ascr.boundary_idx64(_times, _time_start, _time_end) axes[0].set_title( 'Magnetic field and Cold e/HOPE/RBSPICE ion densities') axes[0].plot(mag_times[st:en], 1e-9 * B0[st:en], c='k', label='raw') axes[0].plot(_times[_st:_en], _B0[_st:_en], c='r', label='Filtered + Decimated', marker='o') axes[0].legend() # Cold dens axes[1].plot(den_times, 1e6 * edens, c='k', label='raw') axes[1].plot(_times, _cold_dens, c='r', label='decimated')
def plot_velocities_and_energies_single(time_start, time_end, probe='a'): # Import cutoff-derived composition information cutoff_dict = epd.read_cutoff_file(cutoff_filename) # Load particle and field information time, mag, edens = load_CRRES_data(time_start, time_end, nsec=None, crres_path='E://DATA//CRRES//') mag_time, pc1_mags, HM_mags, imf_time, IA, IF, IP, stime, sfreq, spower = \ load_EMIC_IMFs_and_dynspec(time_start, time_end) spower = spower.sum(axis=0) cutoff = np.interp(time.astype(np.int64), cutoff_dict['CUTOFF_TIME'].astype(np.int64), cutoff_dict['CUTOFF_NORM']) o_fracs = epd.calculate_o_from_he_and_cutoff(cutoff, he_frac) # Get velocity/energy data for time plt.ioff() for ii in range(time.shape[0]): # Define time, time index this_time = time[ii] maxpwr_tidx = np.where(abs(stime - this_time) == np.min(abs(stime - this_time)))[0][0] # Find max freq and index fst, fen = ascr.boundary_idx64(sfreq, _band_start, _band_end) maxpwr_fidx = spower[maxpwr_tidx, fst:fen].argmax() maxpwr_fidx += fst maxpwr_freq = sfreq[maxpwr_fidx] #date_string = this_time.astype(object).strftime('%Y%m%d') save_string = this_time.astype(object).strftime('%Y%m%d_%H%M%S') #clr = mapper.to_rgba(time[ii].astype(np.int64)) print('Doing time:', this_time) fig, axes = plt.subplots(nrows=4, ncols=2, figsize=(8.0, 0.5*11.00), gridspec_kw={'width_ratios':[1, 0.01], 'height_ratios':[1, 2, 2, 2] }) # Spectra/IP im0 = axes[0, 0].pcolormesh(stime, sfreq, spower.T, cmap='jet', norm=colors.LogNorm(vmin=1e-4, vmax=1e1)) axes[0, 0].set_ylim(0, _band_end) axes[0, 0].set_ylabel('$f$ [Hz]', rotation=90) fig.colorbar(im0, cax=axes[0, 1], extend='both').set_label( r'$\frac{nT^2}{Hz}$', fontsize=16, rotation=0, labelpad=15) axes[0, 0].axvline(this_time, color='white', ls='-' , alpha=0.7) axes[0, 0].set_xlim(time_start, time_end) axes[0, 0].set_xticklabels([]) axes[0, 0].axvline(this_time, color='white', ls='-', alpha=0.75) axes[0, 0].axhline(maxpwr_freq, color='white', ls='-', alpha=0.75) axes[0, 0].set_title(f'Velocities and Energies :: {this_time}') h_frac = 1. - he_frac - o_fracs[ii] # Cold plasma params, SI units B0 = mag[ii] name = np.array(['H' , 'He' , 'O' ]) mass = np.array([1.0 , 4.0 , 16.0 ]) * PMASS charge = np.array([1.0 , 1.0 , 1.0 ]) * PCHARGE density = np.array([h_frac, he_frac, o_fracs[ii] ]) * edens[ii] ani = np.array([0.0 , 0.0 , 0.0 ]) tpar = np.array([0.0 , 0.0 , 0.0 ]) tper = (ani + 1) * tpar Species, PP = create_species_array(B0, name, mass, charge, density, tper, ani) # Frequencies to evaluate, calculate wavenumber (cold approximation) f_min = _band_start f_max = _band_end Nf = 10000 f_vals = np.linspace(f_min, f_max, Nf) w_vals = 2*np.pi*f_vals k_vals = nls.get_k_cold(w_vals, Species) wv_len = 1e-3 * 2*np.pi / k_vals fidx = np.where(abs(f_vals - maxpwr_freq) == np.min(abs(f_vals - maxpwr_freq)))[0][0] freq = f_vals[fidx] axes[1, 0].plot(f_vals, wv_len, c='k') axes[1, 0].set_ylabel('$\lambda_{EMIC}$ [km]') axes[1, 0].set_ylim(0, 2000) # Velocities Vg, Vp, Vr = nls.get_velocities(w_vals, Species, PP) axes[2, 0].semilogy(f_vals, Vg*1e-3, c='k', label='$V_g$') axes[2, 0].semilogy(f_vals, Vp*1e-3, c='r', label='$V_p$') axes[2, 0].semilogy(f_vals,-Vr*1e-3, c='b', label='$-V_R$') axes[2, 0].set_ylim(10, 4500) axes[2, 0].set_ylabel('Velocities [km/s]') axes[2, 0].legend(bbox_to_anchor=(1.0, 0), loc=3, borderaxespad=0.) # Energies ELAND, ECYCL = nls.get_energies(w_vals, k_vals, PP['pcyc_rad'], PMASS) axes[3, 0].semilogy(f_vals, ELAND*1e-3, c='r', label='$E_0$') axes[3, 0].semilogy(f_vals, ECYCL*1e-3, c='b', label='$E_1$') axes[3, 0].set_ylim(1e-1, 1e3) axes[3, 0].set_ylabel('$E_R$ [keV]') axes[3, 0].set_xlabel('Freq [Hz]', rotation=0) axes[3, 0].legend(bbox_to_anchor=(1.0, 0), loc=3, borderaxespad=0.) # Work out what the energy is at maxpwr_freq num_landau = ELAND[fidx]*1e-3 num_cyclotron = ECYCL[fidx]*1e-3 axes[3, 0].text(0.8, 0.9, f'$E_0(f) =$ {num_landau:.1f} keV', horizontalalignment='left', verticalalignment='center', transform=axes[3, 0].transAxes) axes[3, 0].text(0.8, 0.8, f'$E_1(f) =$ {num_cyclotron:.1f} keV', horizontalalignment='left', verticalalignment='center', transform=axes[3, 0].transAxes) axes[0, 0].set_xticklabels([]) axes[1, 0].set_yticks(np.array([0, 500, 1000, 1500])) for ax in axes[1:, 0]: ax.set_xlim(_band_start, _band_end) ax.axvline(freq, color='k', alpha=0.5, ls='--') if ax != axes[-1, 0]: ax.set_xticklabels([]) axes[1, 1].set_visible(False) axes[2, 1].set_visible(False) axes[3, 1].set_visible(False) #fig.tight_layout(rect=[0, 0, 0.95, 1]) fig.tight_layout() fig.subplots_adjust(wspace=0.05, hspace=0) fig.align_ylabels() if save_plot == True: print('Saving plot...') fig.savefig(_plot_path + 'VELENG_FROMDATA_' + save_string + '.png', dpi=dpi) plt.close('all') else: plt.show() return
line_VG = np.zeros(time.shape[0], dtype=np.float64) line_VR = np.zeros(time.shape[0], dtype=np.float64) max_freqs = np.zeros(time.shape[0], dtype=np.float64) # Collect info for each time for ii in range(time.shape[0]): print('Doing time:', time[ii]) h_frac = 1. - he_frac - o_frac[ii] # Define time, time index this_time = time[ii] maxpwr_tidx = np.where(abs(stime - this_time) == np.min(abs(stime - this_time)))[0][0] # Find max freq and index fst, fen = ascr.boundary_idx64(sfreq, _band_start, _band_end) maxpwr_fidx = spower[maxpwr_tidx, fst:fen].argmax() maxpwr_fidx += fst max_freqs[ii] = sfreq[maxpwr_fidx] # Cold plasma params, SI units B0 = mag[ii] name = np.array(['H' , 'He' , 'O' ]) mass = np.array([1.0 , 4.0 , 16.0 ]) * PMASS charge = np.array([1.0 , 1.0 , 1.0 ]) * PCHARGE density = np.array([h_frac, he_frac, o_frac[ii] ]) * edens[ii] ani = np.array([0.0 , 0.0 , 0.0 ]) tpar = np.array([0.0 , 0.0 , 0.0 ]) tper = (ani + 1) * tpar Species, PP = create_species_array(B0, name, mass, charge, density, tper, ani)