def fftplt1(x,Mch,dtrnd=True,demean=True): fs=1 if demean: x = x - x.mean() if dtrnd: Pout = plt.psd(x, NFFT=Mch, detrend=pylab.detrend_linear, noverlap=Mch/2, Fs=fs) else: Pout = plt.psd(x, NFFT=Mch, detrend=pylab.detrend_none, noverlap=Mch/2, Fs=fs) xdtrnd = pylab.detrend_linear(x) xauto = mcmath.acorr_mlab(xdtrnd,2) rhoxauto = (xauto[1] + np.sqrt(abs(xauto[2])))/2 R = mcmath.redspec(rhoxauto,np.arange(Mch/2),Mch) P = Pout[0][:R.size] F = Pout[1][:R.size] Psum = P.sum() Rsum = R.sum() PRratio = Psum/Rsum Rcmp = R*PRratio plt.figure() plt.plot(F,P) plt.plot(F,Rcmp) return (F,P,Rcmp)
def fftplt3(x,Mch,pval=0.1,dtrnd=True,demean=True,titlestr='',Dt=0.01): """Differs from fftplt3 solely by plotting the power spectral density in log-form, 10*log10(P).""" fs=1 titl_plc = (1.,1.) if demean: x = x - x.mean() if dtrnd: Pout = plt.psd(x, NFFT=Mch, detrend=pylab.detrend_linear, noverlap=Mch/2, Fs=fs) else: Pout = plt.psd(x, NFFT=Mch, detrend=pylab.detrend_none, noverlap=Mch/2, Fs=fs) xdtrnd = pylab.detrend_linear(x) xauto = mcmath.acorr_mlab(xdtrnd,2) rhoxauto = (xauto[1] + np.sqrt(abs(xauto[2])))/2 R = mcmath.redspec(rhoxauto,np.arange(Mch/2),Mch) P = Pout[0][:R.size] F = Pout[1][:R.size] Psum = P.sum() Rsum = R.sum() PRratio = Psum/Rsum Rcmp = R*PRratio dof = (x.size / (Mch/2.)) * 1.2 Fval = stats.f.isf(pval,dof,dof) tst = P / Rcmp pass1 = np.where(tst > Fval)[0] maxs = mcmath.extrema_find(P,'max',t=F,dt=Dt) max_ind = np.intersect1d(maxs,pass1) Fmaxs = F[max_ind] Fmaxs2 = np.append(Fmaxs,0.1) Fmaxs2.sort() Tmaxs = 1/(Fmaxs2) Tmaxs = np.round(Tmaxs,2) ax = plt.gca() ax.plot(F,10*np.log10(Rcmp)) ax.set_xticks(Fmaxs2) ax.set_xticklabels(Tmaxs) my_fmt_xdate(ax,rot=90,hal='center') multiline(Fmaxs,c='red',ls='--') xtl = ax.get_xticklabels() ytl = ax.get_yticklabels() plt.setp(xtl,'size',10) plt.setp(ytl,'size',9) ppct = int((1 - pval)*100) titl_str = '%s FFT (chunksize: %d) :: peak CL: %d%%' % (titlestr,Mch,ppct) plt.text(titl_plc[0],titl_plc[1],titl_str,ha='right',va='bottom',size=12,transform=ax.transAxes) plt.xlabel('Peak period (yrs)',size=11) return (F,P,Rcmp)
def integrated_B(shot, num_probes=16, probe=3): """ There are different way that I've seen the data integrated: this will plot the different ones, along with a variety of different moving averages --KG 06/13/19 """ # data = [[0] *3 for i in range(num_probes)] fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True) data = hdr.getquikData(shot) btime = data.time Bz_nBT_15 = cumtrapz( data.unCalibData[2, probe, :] - mj.get_gaussian_moving_avg( data.time, data.unCalibData[2, probe, :], 15), data.time) * -1.103810335732411829e+02 Bz_nBT_100 = cumtrapz( data.unCalibData[2, probe, :] - mj.get_gaussian_moving_avg( data.time, data.unCalibData[2, probe, :], 100), data.time) * -1.103810335732411829e+02 Bz_nBT_500 = cumtrapz( data.unCalibData[2, probe, :] - mj.get_gaussian_moving_avg( data.time, data.unCalibData[2, probe, :], 500), data.time) * -1.103810335732411829e+02 # num_probes = 1 #only plot the first probe # probe_locs = get_probeLocs_calib_setup(shot) # Bz=sp.signal.detrend(magdata.fullData[2,probe_num,:]) # Bx=sp.signal.detrend(magdata.fullData[0,probe_num,:]) # By=sp.signal.detrend(magdata.fullData[1,probe_num,:]) # Bz=cumtrapz(data.unCalibData[2,1,:]-mj.get_gaussian_moving_avg(data.time, data.unCalibData[2,1,:], 1), data.time) Bz = sp.signal.detrend(cumtrapz(data.unCalibData[2, probe, :], data.time)) btime = btime[:len(Bz)] ax1.plot(btime, Bz, label='Detrend, integrated') ax1.plot(btime, Bz_nBT_15, label='-moving avg, win = 15') ax1.plot(btime, Bz_nBT_100, label='-moving avg, win = 100') ax1.plot(btime, Bz_nBT_500, label='-moving avg, win = 500') ax1.legend() Bdot25 = np.zeros([3, 25, 8192]) timeB = data.time[801:] Bdot25[0, 3, :] = data.Bdot[0, 3, :] Bdot25[1, 3, :] = data.Bdot[1, 3, :] Bdot25[2, 3, :] = data.Bdot[2, 3, :] bzero = pyl.detrend_linear(Bdot25[2, 3, 800:]) bint = sp.integrate.cumtrapz(bzero, data.time[800:]) ax2.plot(btime, Bz_nBT_15, label='-moving avg, win = 15') ax2.plot(btime, Bz_nBT_500, label='-moving avg, win = 500') ax2.plot(timeB, bint, label="detrend LINEAR and integrated time shifted") # ax2.set_title("Current") plt.legend() plt.show()
def fftplt1(x, Mch, dtrnd=True, demean=True): fs = 1 if demean: x = x - x.mean() if dtrnd: Pout = plt.psd(x, NFFT=Mch, detrend=pylab.detrend_linear, noverlap=Mch / 2, Fs=fs) else: Pout = plt.psd(x, NFFT=Mch, detrend=pylab.detrend_none, noverlap=Mch / 2, Fs=fs) xdtrnd = pylab.detrend_linear(x) xauto = mcmath.acorr_mlab(xdtrnd, 2) rhoxauto = (xauto[1] + np.sqrt(abs(xauto[2]))) / 2 R = mcmath.redspec(rhoxauto, np.arange(Mch / 2), Mch) P = Pout[0][:R.size] F = Pout[1][:R.size] Psum = P.sum() Rsum = R.sum() PRratio = Psum / Rsum Rcmp = R * PRratio plt.figure() plt.plot(F, P) plt.plot(F, Rcmp) return (F, P, Rcmp)
def process_mjmag_data(shot): data = hdr.getMJMagData(shot) #print data.settings #recast array into [3,25] Bdot25 = np.zeros([3, 25, 8192]) #1 Bdot25[0, 0, :] = data.Bdot[0, 0, :] Bdot25[1, 0, :] = data.Bdot[1, 0, :] #2 Bdot25[0, 1, :] = data.Bdot[0, 1, :] Bdot25[1, 1, :] = data.Bdot[1, 1, :] #3 Bdot25[0, 2, :] = data.Bdot[0, 2, :] Bdot25[1, 2, :] = data.Bdot[1, 2, :] #4 Bdot25[0, 3, :] = data.Bdot[0, 3, :] Bdot25[1, 3, :] = data.Bdot[1, 3, :] #5 Bdot25[0, 4, :] = data.Bdot[0, 4, :] Bdot25[1, 4, :] = data.Bdot[1, 4, :] #6 Bdot25[0, 5, :] = data.Bdot[0, 5, :] Bdot25[1, 5, :] = data.Bdot[1, 5, :] #7 Bdot25[0, 6, :] = data.Bdot[0, 6, :] Bdot25[1, 6, :] = data.Bdot[1, 6, :] #8 Bdot25[0, 7, :] = data.Bdot[0, 7, :] Bdot25[1, 7, :] = data.Bdot[1, 7, :] #9 Bdot25[0, 8, :] = data.Bdot[0, 8, :] Bdot25[1, 8, :] = data.Bdot[1, 8, :] #10 Bdot25[0, 9, :] = data.Bdot[0, 9, :] Bdot25[1, 9, :] = data.Bdot[1, 9, :] #11 Bdot25[0, 10, :] = data.Bdot[0, 10, :] Bdot25[1, 10, :] = data.Bdot[1, 10, :] #12 Bdot25[0, 11, :] = data.Bdot[0, 11, :] Bdot25[1, 11, :] = data.Bdot[1, 11, :] #13 Bdot25[0, 12, :] = data.Bdot[0, 12, :] Bdot25[1, 12, :] = data.Bdot[1, 12, :] Bdot25[2, 12, :] = data.Bdot[2, 2, :] #14 Bdot25[0, 13, :] = data.Bdot[0, 13, :] Bdot25[1, 13, :] = data.Bdot[1, 13, :] Bdot25[2, 13, :] = data.Bdot[2, 1, :] #15 Bdot25[0, 14, :] = data.Bdot[0, 14, :] Bdot25[1, 14, :] = data.Bdot[1, 14, :] Bdot25[2, 14, :] = data.Bdot[2, 0, :] #16 Bdot25[0, 15, :] = data.Bdot[0, 15, :] Bdot25[1, 15, :] = data.Bdot[1, 15, :] #17 Bdot25[0, 16, :] = data.Bdot[2, 3, :] Bdot25[1, 16, :] = data.Bdot[2, 12, :] #18 Bdot25[0, 17, :] = data.Bdot[2, 4, :] Bdot25[1, 17, :] = data.Bdot[2, 13, :] #19 Bdot25[0, 18, :] = data.Bdot[2, 5, :] Bdot25[1, 18, :] = data.Bdot[2, 14, :] #20 Bdot25[0, 19, :] = data.Bdot[2, 6, :] Bdot25[1, 19, :] = data.Bdot[2, 15, :] #21 Bdot25[0, 20, :] = data.Bdot[2, 7, :] #22 Bdot25[0, 21, :] = data.Bdot[2, 8, :] #23 Bdot25[0, 22, :] = data.Bdot[2, 9, :] #24 Bdot25[0, 23, :] = data.Bdot[2, 10, :] #25 Bdot25[0, 24, :] = data.Bdot[2, 11, :] #reintegrate with later start times B25 = np.zeros([3, 25, 7391]) timeB = data.time[801:] for i in np.arange(3): for j in np.arange(25): #bzero = Bdot25[i,j,:]-np.mean(Bdot25[i,j,800:8000]) bzero = plt.detrend_linear(Bdot25[i, j, 800:]) bint = sp.cumtrapz(bzero, data.time[800:]) B25[i, j, :] = bint #compute Bmod Bmod25 = np.zeros([25, 7391]) for j in np.arange(25): Bmod25[j, :] = np.sqrt(B25[0, j, :]**2 + B25[1, j, :]**2 + B25[2, j, :]**2) return data.time, Bdot25, timeB, B25, Bmod25, data
def fftplt2(x,Mch,pval=0.1,dtrnd=True,demean=True,titlestr='',Dt=0.01): """Differs from fftplt1 by returning a plot highlighting sig peaks, with their freq axis tick marks notated by their period. Also allows adjustment of sig level and title string.""" fs=1 titl_plc = (1.,1.) if demean: x = x - x.mean() if dtrnd: Pout = plt.psd(x, NFFT=Mch, detrend=pylab.detrend_linear, noverlap=Mch/2, Fs=fs) else: Pout = plt.psd(x, NFFT=Mch, detrend=pylab.detrend_none, noverlap=Mch/2, Fs=fs) xdtrnd = pylab.detrend_linear(x) xauto = mcmath.acorr_mlab(xdtrnd,2) rhoxauto = (xauto[1] + np.sqrt(abs(xauto[2])))/2 R = mcmath.redspec(rhoxauto,np.arange(Mch/2),Mch) P = Pout[0][:R.size] F = Pout[1][:R.size] Psum = P.sum() Rsum = R.sum() PRratio = Psum/Rsum Rcmp = R*PRratio dof = (x.size / (Mch/2.)) * 1.2 Fval = stats.f.isf(pval,dof,dof) tst = P / Rcmp pass1 = np.where(tst > Fval)[0] maxs = mcmath.extrema_find(P,'max',t=F,dt=Dt) max_ind = np.intersect1d(maxs,pass1) Fmaxs = F[max_ind] Fmaxs2 = np.append(Fmaxs,0.1) Fmaxs2.sort() Tmaxs = 1/(Fmaxs2) Tmaxs = np.round(Tmaxs,2) fig = plt.figure(figsize=(7,5)) ax = fig.add_axes((0.1,0.14,0.88,0.81)) ax.plot(F,P,) ax.plot(F,Rcmp) ax.set_xticks(Fmaxs2) ax.set_xticklabels(Tmaxs) my_fmt_xdate(ax,rot=90,hal='center') multiline(Fmaxs,c='red',ls='--') xtl = ax.get_xticklabels() ytl = ax.get_yticklabels() plt.setp(xtl,'size',10) plt.setp(ytl,'size',9) ppct = int((1 - pval)*100) titl_str = '%s FFT (chunksize: %d) :: peak CL: %d%%' % (titlestr,Mch,ppct) plt.text(titl_plc[0],titl_plc[1],titl_str,ha='right',va='bottom',size=12,transform=ax.transAxes) plt.xlabel('Peak period (yrs)',size=11) return (F,P,Rcmp)
def fftplt3(x, Mch, pval=0.1, dtrnd=True, demean=True, titlestr='', Dt=0.01): """Differs from fftplt3 solely by plotting the power spectral density in log-form, 10*log10(P).""" fs = 1 titl_plc = (1., 1.) if demean: x = x - x.mean() if dtrnd: Pout = plt.psd(x, NFFT=Mch, detrend=pylab.detrend_linear, noverlap=Mch / 2, Fs=fs) else: Pout = plt.psd(x, NFFT=Mch, detrend=pylab.detrend_none, noverlap=Mch / 2, Fs=fs) xdtrnd = pylab.detrend_linear(x) xauto = mcmath.acorr_mlab(xdtrnd, 2) rhoxauto = (xauto[1] + np.sqrt(abs(xauto[2]))) / 2 R = mcmath.redspec(rhoxauto, np.arange(Mch / 2), Mch) P = Pout[0][:R.size] F = Pout[1][:R.size] Psum = P.sum() Rsum = R.sum() PRratio = Psum / Rsum Rcmp = R * PRratio dof = (x.size / (Mch / 2.)) * 1.2 Fval = stats.f.isf(pval, dof, dof) tst = P / Rcmp pass1 = np.where(tst > Fval)[0] maxs = mcmath.extrema_find(P, 'max', t=F, dt=Dt) max_ind = np.intersect1d(maxs, pass1) Fmaxs = F[max_ind] Fmaxs2 = np.append(Fmaxs, 0.1) Fmaxs2.sort() Tmaxs = 1 / (Fmaxs2) Tmaxs = np.round(Tmaxs, 2) ax = plt.gca() ax.plot(F, 10 * np.log10(Rcmp)) ax.set_xticks(Fmaxs2) ax.set_xticklabels(Tmaxs) my_fmt_xdate(ax, rot=90, hal='center') multiline(Fmaxs, c='red', ls='--') xtl = ax.get_xticklabels() ytl = ax.get_yticklabels() plt.setp(xtl, 'size', 10) plt.setp(ytl, 'size', 9) ppct = int((1 - pval) * 100) titl_str = '%s FFT (chunksize: %d) :: peak CL: %d%%' % (titlestr, Mch, ppct) plt.text(titl_plc[0], titl_plc[1], titl_str, ha='right', va='bottom', size=12, transform=ax.transAxes) plt.xlabel('Peak period (yrs)', size=11) return (F, P, Rcmp)
def fftplt2(x, Mch, pval=0.1, dtrnd=True, demean=True, titlestr='', Dt=0.01): """Differs from fftplt1 by returning a plot highlighting sig peaks, with their freq axis tick marks notated by their period. Also allows adjustment of sig level and title string.""" fs = 1 titl_plc = (1., 1.) if demean: x = x - x.mean() if dtrnd: Pout = plt.psd(x, NFFT=Mch, detrend=pylab.detrend_linear, noverlap=Mch / 2, Fs=fs) else: Pout = plt.psd(x, NFFT=Mch, detrend=pylab.detrend_none, noverlap=Mch / 2, Fs=fs) xdtrnd = pylab.detrend_linear(x) xauto = mcmath.acorr_mlab(xdtrnd, 2) rhoxauto = (xauto[1] + np.sqrt(abs(xauto[2]))) / 2 R = mcmath.redspec(rhoxauto, np.arange(Mch / 2), Mch) P = Pout[0][:R.size] F = Pout[1][:R.size] Psum = P.sum() Rsum = R.sum() PRratio = Psum / Rsum Rcmp = R * PRratio dof = (x.size / (Mch / 2.)) * 1.2 Fval = stats.f.isf(pval, dof, dof) tst = P / Rcmp pass1 = np.where(tst > Fval)[0] maxs = mcmath.extrema_find(P, 'max', t=F, dt=Dt) max_ind = np.intersect1d(maxs, pass1) Fmaxs = F[max_ind] Fmaxs2 = np.append(Fmaxs, 0.1) Fmaxs2.sort() Tmaxs = 1 / (Fmaxs2) Tmaxs = np.round(Tmaxs, 2) fig = plt.figure(figsize=(7, 5)) ax = fig.add_axes((0.1, 0.14, 0.88, 0.81)) ax.plot( F, P, ) ax.plot(F, Rcmp) ax.set_xticks(Fmaxs2) ax.set_xticklabels(Tmaxs) my_fmt_xdate(ax, rot=90, hal='center') multiline(Fmaxs, c='red', ls='--') xtl = ax.get_xticklabels() ytl = ax.get_yticklabels() plt.setp(xtl, 'size', 10) plt.setp(ytl, 'size', 9) ppct = int((1 - pval) * 100) titl_str = '%s FFT (chunksize: %d) :: peak CL: %d%%' % (titlestr, Mch, ppct) plt.text(titl_plc[0], titl_plc[1], titl_str, ha='right', va='bottom', size=12, transform=ax.transAxes) plt.xlabel('Peak period (yrs)', size=11) return (F, P, Rcmp)