Esempio n. 1
0
def test_reconstruction_freq():
    """In principle one can reconstruct the input data from the
    wavelet transform.

    Check within 10% when computing with frequency representation of
    wavelet.
    """
    wa = WaveletAnalysis(anomaly_sst, frequency=True)
    rdata = wa.reconstruction()

    err = wa.data - rdata
    assert (np.abs(err.mean()) < 0.02)
Esempio n. 2
0
def test_reconstruction_freq():
    """In principle one can reconstruct the input data from the
    wavelet transform.

    Check within 10% when computing with frequency representation of
    wavelet.
    """
    wa = WaveletAnalysis(anomaly_sst, frequency=True)
    rdata = wa.reconstruction()

    err = wa.data - rdata
    assert(np.abs(err.mean()) < 0.02)
Esempio n. 3
0
def MyWavelets(data,MyWidths):
    Widths = MyWidths

    ''' 将int型data转为float型sig '''
    sig = np.ones(len(data),np.float)  #产生空的float型sig
    for i in range(0,len(data)): 
        sig[i] = float(data[i])

    sig = np.array( sig )
    wa = WaveletAnalysis(sig, wavelet=Morlet() )
    # wavelet power spectrum
    power = wa.wavelet_power

    # scales 
    scales = wa.scales

    # associated time vector
    t = wa.time

    # reconstruction of the original data
    rx = wa.reconstruction()
    

    ########################################
    # 数据逐帧 0-1标准化
    # print(power.shape)
    # power = np.transpose(power) #转置
    power = power.T
    # print(power.shape)

    # # power_out = np.array([])
    power_out = []
    for i in power:
    #     # np.append(power_out, minmax_scale(i), axis = 0)
        # power_out.append( minmax_scale(i).tolist() )
        power_out.append( minmax_scale(i))
        # print(max( minmax_scale(i) ))
    # power_out = np.array(power_out)


    return power_out
def aren_wavelet(x,filename,fs):
    '''
    Does the same continuous wavelet transmform as the mlpy_cwt
    '''
    from wavelets import WaveletAnalysis
    dt=1/fs
    wa = WaveletAnalysis(x, dt=dt)
    # wavelet power spectrum
    power = wa.wavelet_power
    # scales
    scales = wa.scales
    import mlpy.wavelet as wave
    freqs=[math.pow(i,-1) for i in wave.fourier_from_scales(scales, 'morlet',2)]
    # associated time vector
    t = wa.time
    # reconstruction of the original data
    rx = wa.reconstruction()

    fig, ax = plt.subplots()
    T, S = np.meshgrid(t, scales)
    ax.contourf(T, S, power)
    #ax.set_ylim(freqs[-1],freqs[0])
    #ax.set_yscale('log')
    fig.savefig(filename+'.pdf')
Esempio n. 5
0
#wa = WaveletAnalysis(last1second, dt=dt)

# wavelet power spectrum
power = wa.wavelet_power
re = wa.wavelet_transform.imag
im = wa.wavelet_transform.real
phase = np.arctan(im / re)

# scales
scales = wa.scales

# associated time vector
t = wa.time

# reconstruction of the original data
rx = wa.reconstruction()
#How would you plot this?

fig, ax = plt.subplots()
T, S = np.meshgrid(t, scales)
#ax.contourf(T, S, power, 100)
ax.contourf(T, S, phase, 100)
ax.set_yscale('log')
fig.savefig('test_wavelet_phase_spectrum.png')

fig, ax = plt.subplots()
T, S = np.meshgrid(t, scales)
#ax.contourf(T, S, power, 100)
ax.contourf(T, S, power, 100)
ax.set_yscale('log')
fig.savefig('test_wavelet_power_spectrum.png')
Esempio n. 6
0
def defringeflat(flat_file,
                 wbin=10,
                 start_col=10,
                 end_col=980,
                 clip1=0,
                 diagnostic=True,
                 save_to_path=None,
                 filename=None):
    """
	This function is to remove the fringe pattern using
	the method described in Rojo and Harrington (2006).

	Use a fifth order polynomial to remove the continuum.

	Parameters
	----------
	flat_file 		: 	fits
						original flat file

	Optional Parameters
	-------------------
	wbin 			:	int
						the bin width to calculate each 
						enhance row
						Default is 32

	start_col 		: 	int
						starting column number for the
						wavelet analysis
						Default is 10

	end_col 		: 	int
						ending column number for the
						wavelet analysis
						Default is 980

	diagnostic 		: 	boolean
						output the diagnostic plots
						Default is True

	Returns
	-------
	defringe file 	: 	fits
						defringed flat file

	"""
    # the path to save the diagnostic plots
    #save_to_path = 'defringeflat/allflat/'

    #print(flat_file)

    data = fits.open(flat_file, ignore_missing_end=True)

    # Use the data to figure out the values to mask through the image (low counts/order edges)
    hist, bins = np.histogram(data[0].data.flatten(),
                              bins=int(np.sqrt(len(data[0].data.flatten()))))
    bins = bins[0:-1]
    index1 = np.where((bins > np.percentile(data[0].data.flatten(), 10))
                      & (bins < np.percentile(data[0].data.flatten(), 30)))
    try:
        lowval = bins[index1][np.where(hist[index1] == np.min(hist[index1]))]
        #print(lowval, len(lowval))
        if len(lowval) >= 2: lowval = np.min(lowval)
    except:
        lowval = 0  #if no values for index1

    flat = data

    # initial flat plot
    if diagnostic is True:

        # Save the images to a separate folder
        save_to_image_path = save_to_path + '/images/'
        if not os.path.exists(save_to_image_path):
            os.makedirs(save_to_image_path)

        fig = plt.figure(figsize=(8, 8))
        fig.suptitle("original flat", fontsize=12)
        gs = gridspec.GridSpec(2, 1, height_ratios=[6, 1])
        ax0 = plt.subplot(gs[0])
        # Create an ImageNormalize object
        norm = ImageNormalize(flat[0].data, interval=ZScaleInterval())
        ax0.imshow(flat[0].data,
                   cmap='gray',
                   norm=norm,
                   origin='lower',
                   aspect='auto')
        ax0.set_ylabel("Row number")
        ax1 = plt.subplot(gs[1], sharex=ax0)
        ax1.plot(flat[0].data[60, :],
                 'k-',
                 alpha=0.5,
                 label='60th row profile')
        ax1.set_ylabel("Amp (DN)")
        ax1.set_xlabel("Column number")
        plt.legend()
        plt.savefig(save_to_image_path + "defringeflat_{}_0_original_flat.png"\
                 .format(filename), bbox_inches='tight')
        plt.close()

    defringeflat_img = data
    defringe_data = np.array(defringeflat_img[0].data, dtype=float)

    for k in np.arange(0, 1024 - wbin, wbin):
        #print(k)
        #if k != 310: continue
        """
		# Use the data to figure out the values to mask through the image (low counts/order edges)
		hist, bins = np.histogram(flat[0].data[k:k+wbin+1, 0:1024-clip1].flatten(), 
			                      bins=int(np.sqrt(len(flat[0].data[k:k+wbin+1, 0:1024-clip1].flatten()))))
		bins       = bins[0:-1]
		index1     = np.where( (bins > np.percentile(flat[0].data[k:k+wbin+1, 0:1024-clip1].flatten(), 10)) & 
			                   (bins < np.percentile(flat[0].data[k:k+wbin+1, 0:1024-clip1].flatten(), 30)) )
		lowval     = bins[index1][np.where(hist[index1] == np.min(hist[index1]))]
		
		#print(lowval, len(lowval))
		if len(lowval) >= 2: lowval = np.min(lowval)
		"""
        # Find the mask
        mask = np.zeros(flat[0].data[k:k + wbin + 1, 0:1024 - clip1].shape)
        baddata = np.where(
            flat[0].data[k:k + wbin + 1, 0:1024 - clip1] <= lowval)
        mask[baddata] = 1

        # extract the patch from the fits file
        #flat_patch = np.ma.array(flat[0].data[k:k+wbin,:], mask=mask)
        flat_patch = np.array(flat[0].data[k:k + wbin + 1, 0:1024 - clip1])

        # median average the selected region in the order
        flat_patch_median = np.ma.median(flat_patch, axis=0)

        # continuum fit
        # smooth the continuum (Chris's method)
        smoothed = sp.ndimage.uniform_filter1d(flat_patch_median, 30)
        splinefit = sp.interpolate.interp1d(np.arange(len(smoothed)),
                                            smoothed,
                                            kind='cubic')
        cont_fit = splinefit(np.arange(0, 1024 - clip1))  #smoothed

        # Now fit a polynomial
        #pcont     = np.ma.polyfit(np.arange(0, 1024-clip1),
        #	                      cont_fit, 10)
        #cont_fit2 = np.polyval(pcont, np.arange(0,1024))

        #plt.plot(flat_patch_median, c='r')
        #plt.plot(smoothed, c='b')
        #plt.savefig(save_to_image_path + "TEST.png", bbox_inches='tight')
        #plt.close()
        #plt.show()
        #sys.exit()

        #pcont    = np.ma.polyfit(np.arange(start_col,end_col),
        #	                     flat_patch_median[start_col:end_col],10)
        #cont_fit = np.polyval(pcont, np.arange(0,1024))

        # use wavelets package: WaveletAnalysis
        enhance_row = flat_patch_median - cont_fit

        dt = 0.1
        wa = WaveletAnalysis(enhance_row[start_col:end_col], dt=dt)
        # wavelet power spectrum
        power = wa.wavelet_power
        # scales
        cales = wa.scales
        # associated time vector
        t = wa.time
        # reconstruction of the original data
        rx = wa.reconstruction()

        # reconstruct the fringe image
        reconstruct_image = np.zeros(defringe_data[k:k + wbin + 1,
                                                   0:1024 - clip1].shape)
        for i in range(wbin + 1):
            for j in np.arange(start_col, end_col):
                reconstruct_image[i, j] = rx[j - start_col]

        defringe_data[k:k + wbin + 1,
                      0:1024 - clip1] -= reconstruct_image[0:1024 - clip1]

        # Add in something for the edges/masked out data in the reconstructed image
        defringe_data[k:k + wbin + 1, 0:1024 -
                      clip1][baddata] = flat[0].data[k:k + wbin + 1,
                                                     0:1024 - clip1][baddata]

        #print("{} row starting {} is done".format(filename,k))

        # diagnostic plots
        if diagnostic is True:
            print("Generating diagnostic plots")
            # middle cut plot
            fig = plt.figure(figsize=(10, 6))
            fig.suptitle("middle cut at row {}".format(k + wbin // 2),
                         fontsize=12)
            ax1 = fig.add_subplot(2, 1, 1)

            norm = ImageNormalize(flat_patch, interval=ZScaleInterval())
            ax1.imshow(flat_patch,
                       cmap='gray',
                       norm=norm,
                       origin='lower',
                       aspect='auto')
            ax1.set_ylabel("Row number")
            ax2 = fig.add_subplot(2, 1, 2, sharex=ax1)
            ax2.plot(flat_patch[wbin // 2, :], 'k-', alpha=0.5)
            ax2.set_ylabel("Amp (DN)")
            ax2.set_xlabel("Column number")

            plt.tight_layout()
            plt.subplots_adjust(top=0.85, hspace=0.5)
            plt.savefig(save_to_image_path + \
             'defringeflat_{}_flat_start_row_{}_middle_profile.png'\
             .format(filename,k), bbox_inches='tight')
            plt.close()

            # continuum fit plot
            fig = plt.figure(figsize=(10, 6))
            fig.suptitle("continuum fit row {}-{}".format(k, k + wbin),
                         fontsize=12)
            gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1])
            ax0 = plt.subplot(gs[0])
            ax0.plot(flat_patch_median,
                     'k-',
                     alpha=0.5,
                     label='mean average patch')
            ax0.plot(cont_fit, 'r-', alpha=0.5, label='continuum fit')
            #ax0.plot(cont_fit2,'m-', alpha=0.5, label='continuum fit poly')
            ax0.set_ylabel("Amp (DN)")
            plt.legend()
            ax1 = plt.subplot(gs[1])
            ax1.plot(flat_patch_median - cont_fit,
                     'k-',
                     alpha=0.5,
                     label='residual')
            ax1.set_ylabel("Amp (DN)")
            ax1.set_xlabel("Column number")
            plt.legend()

            plt.tight_layout()
            plt.subplots_adjust(top=0.85, hspace=0.5)
            plt.savefig(save_to_image_path + \
                       "defringeflat_{}_start_row_{}_continuum_fit.png".\
                     format(filename,k), bbox_inches='tight')
            #plt.show()
            #sys.exit()
            plt.close()

            # enhance row vs. reconstructed wavelet plot
            try:
                fig = plt.figure(figsize=(10, 6))
                fig.suptitle("reconstruct fringe comparison row {}-{}".\
                          format(k,k+wbin), fontsize=10)
                ax1 = fig.add_subplot(3, 1, 1)

                ax1.set_title('enhance_row start row')
                ax1.plot(enhance_row,
                         'k-',
                         alpha=0.5,
                         label="enhance_row start row {}".format(k))
                ax1.set_ylabel("Amp (DN)")
                #plt.legend()

                ax2 = fig.add_subplot(3, 1, 2, sharex=ax1)
                ax2.set_title('reconstructed fringe pattern')
                ax2.plot(rx,
                         'k-',
                         alpha=0.5,
                         label='reconstructed fringe pattern')
                ax2.set_ylabel("Amp (DN)")
                #plt.legend()

                ax3 = fig.add_subplot(3, 1, 3, sharex=ax1)
                ax3.set_title('residual')
                ax3.plot(enhance_row[start_col:end_col] - rx,
                         'k-',
                         alpha=0.5,
                         label='residual')
                ax3.set_ylabel("Amp (DN)")
                ax3.set_xlabel("Column number")
                #plt.legend()
                plt.tight_layout()
                plt.subplots_adjust(top=0.85, hspace=0.5)
                plt.savefig(save_to_image_path + \
                         "defringeflat_{}_start_row_{}_reconstruct_profile.png".\
                         format(filename,k), bbox_inches='tight')
                plt.close()
            except RuntimeError:
                print("CANNOT GENERATE THE PLOT defringeflat\
					_{}_start_row_{}_reconstruct_profile.png"            \
                 .format(filename,k))
                pass

            # reconstruct image comparison plot
            fig = plt.figure(figsize=(10, 6))
            fig.suptitle("reconstructed image row {}-{}".\
                      format(k,k+wbin), fontsize=12)

            ax1 = fig.add_subplot(3, 1, 1)
            ax1.set_title('raw flat image')
            norm = ImageNormalize(flat_patch, interval=ZScaleInterval())
            ax1.imshow(flat_patch,
                       cmap='gray',
                       norm=norm,
                       origin='lower',
                       label='raw flat image',
                       aspect='auto')
            ax1.set_ylabel("Row number")
            #plt.legend()

            ax2 = fig.add_subplot(3, 1, 2, sharex=ax1)
            ax2.set_title('reconstructed fringe image')
            norm = ImageNormalize(reconstruct_image, interval=ZScaleInterval())
            ax2.imshow(reconstruct_image,
                       cmap='gray',
                       norm=norm,
                       origin='lower',
                       label='reconstructed fringe image',
                       aspect='auto')
            ax2.set_ylabel("Row number")
            #plt.legend()

            ax3 = fig.add_subplot(3, 1, 3, sharex=ax1)
            ax3.set_title('residual')
            norm = ImageNormalize(flat_patch - reconstruct_image,
                                  interval=ZScaleInterval())
            ax3.imshow(flat_patch - reconstruct_image,
                       norm=norm,
                       origin='lower',
                       cmap='gray',
                       label='residual',
                       aspect='auto')
            ax3.set_ylabel("Row number")
            ax3.set_xlabel("Column number")
            #plt.legend()
            plt.tight_layout()
            plt.subplots_adjust(top=0.85, hspace=0.5)
            plt.savefig(save_to_image_path + \
                     "defringeflat_{}_start_row_{}_reconstruct_image.png".\
                     format(filename,k), bbox_inches='tight')
            plt.close()

            # middle residual comparison plot
            fig = plt.figure(figsize=(10, 6))
            fig.suptitle("middle row comparison row {}-{}".\
                      format(k,k+wbin), fontsize=12)

            ax1 = fig.add_subplot(3, 1, 1)
            ax1.plot(flat_patch[wbin // 2, :],
                     'k-',
                     alpha=0.5,
                     label='original flat row {}'.format(k + wbin / 2))
            ax1.set_ylabel("Amp (DN)")
            plt.legend()

            ax2 = fig.add_subplot(3, 1, 2, sharex=ax1)
            ax2.plot(flat_patch[wbin//2,:]-\
                  reconstruct_image[wbin//2,:],'k-',
                  alpha=0.5, label='defringed flat row {}'.format(k+wbin/2))
            ax2.set_ylabel("Amp (DN)")
            plt.legend()

            ax3 = fig.add_subplot(3, 1, 3, sharex=ax1)
            ax3.plot(reconstruct_image[wbin // 2, :],
                     'k-',
                     alpha=0.5,
                     label='difference')
            ax3.set_ylabel("Amp (DN)")
            ax3.set_xlabel("Column number")
            plt.legend()

            plt.tight_layout()
            plt.subplots_adjust(top=0.85, hspace=0.5)
            plt.savefig(save_to_image_path + \
                     "defringeflat_{}_start_row_{}_defringe_middle_profile.png".\
                     format(filename,k), bbox_inches='tight')
            plt.close()

        #if k > 30: sys.exit() # for testing purposes

    # final diagnostic plot
    if diagnostic is True:
        fig = plt.figure(figsize=(8, 8))
        fig.suptitle("defringed flat", fontsize=12)
        gs = gridspec.GridSpec(2, 1, height_ratios=[6, 1])
        ax0 = plt.subplot(gs[0])
        norm = ImageNormalize(defringe_data, interval=ZScaleInterval())
        ax0.imshow(defringe_data,
                   cmap='gray',
                   norm=norm,
                   origin='lower',
                   aspect='auto')
        ax0.set_ylabel("Row number")
        ax1 = plt.subplot(gs[1], sharex=ax0)
        ax1.plot(defringe_data[60, :],
                 'k-',
                 alpha=0.5,
                 label='60th row profile')
        ax1.set_ylabel("Amp (DN)")
        ax1.set_xlabel("Column number")
        plt.legend()

        plt.tight_layout()
        plt.subplots_adjust(top=0.85, hspace=0.5)
        plt.savefig(save_to_image_path + "defringeflat_{}_0_defringe_flat.png"\
         .format(filename), bbox_inches='tight')
        plt.close()

    hdu = fits.PrimaryHDU(data=defringe_data)
    hdu.header = flat[0].header
    return hdu
Esempio n. 7
0
def defringetelluric():

if not os.path.exists(save_to_path):
    os.makedirs(save_to_path)

############################################
print(tell_data_name)

tell_sp = nspf.Spectrum(name=tell_data_name, order=order, path=tell_path)

clickpoints = []
def onclick(event):
	print(event)
	global clickpoints
	clickpoints.append([event.xdata])
	print(clickpoints)
	plt.axvline(event.xdata, c='r', ls='--')
	plt.draw()
	if len(clickpoints) == 2:
		print('Closing Figure')
		plt.axvspan(clickpoints[0][0], clickpoints[1][0], color='0.5', alpha=0.5, zorder=-100)
		plt.draw()
		plt.pause(1)
		plt.close('all')


### Draw the figure with the power spectrum
cal1        = tell_sp.flux#[pixel_range_start:pixel_range_end]
xdim        = len(cal1)#[pixel_range_start:pixel_range_end])
nfil        = xdim//2 + 1
# Smooth the continuum
cal1smooth  = sp.ndimage.median_filter(cal1, size=30)
# Do the FFT
cal1fft     = fftpack.rfft(cal1-cal1smooth)
yp          = abs(cal1fft[0:nfil])**2
yp          = yp / np.max(yp)

fig, ax1 = plt.subplots(figsize=(12,6))
cid = fig.canvas.mpl_connect('button_press_event', onclick)
freq        = np.arange(nfil)
yp[0:3]     = 0 # Fix for very low order noise
ax1.plot(freq, yp)
#ax1.axvline(f_high*2, c='r', ls='--')
#ax1.axvline(f_low*2, c='r', ls='--')
ax1.set_ylabel('Power Spectrum')
ax1.set_xlabel('1 / (1024 pix)')
ax1.set_title('Select the range you would like to filter out')
ax1.set_xlim(0, np.max(freq))
plt.show()
plt.close('all')
f_high   = np.max(clickpoints)/2
f_low    = np.min(clickpoints)/2


if method == 'wavelet':
	#### Wavelets
	from wavelets import WaveletAnalysis

	xdim      = len(tell_sp.flux)#[pixel_range_start:pixel_range_end])
	cal1      = tell_sp.flux#[pixel_range_start:pixel_range_end]

	# Smooth the continuum
	smoothed    = sp.ndimage.uniform_filter1d(cal1, 30)
	splinefit   = sp.interpolate.interp1d(np.arange(len(smoothed)), smoothed, kind='cubic')
	cal1smooth  = splinefit(np.arange(0, len(cal1))) #smoothed

	# use wavelets package: WaveletAnalysis
	enhance_row = cal1 - cal1smooth
	#print(enhance_row)

	dt     = 0.1
	wa     = WaveletAnalysis(enhance_row, dt=dt, axis=0)
	# wavelet power spectrum
	power  = wa.wavelet_power

	# scales
	scales = wa.scales
	# associated time vector
	t      = wa.time
	# reconstruction of the original data
	rx     = wa.reconstruction()

	defringe_data    = np.array(cal1.data, dtype=float)

	# reconstruct the fringe image
	#reconstruct_image      = np.zeros(defringe_data.shape)
	reconstruct_image      = np.real(rx)

	defringe_data -= reconstruct_image
	newSpectrum   = defringe_data

	if PLOT:

		fig = plt.figure(figsize=(12,6))
		ax1 = plt.subplot2grid((3, 1), (0, 0))
		ax2 = plt.subplot2grid((3, 1), (1, 0), rowspan=2)
		
		#freq        = np.arange(nfil)
		ax1.plot(power**2)
		ax1.axvline(f_high*2, c='r', ls='--')
		ax1.axvline(f_low*2, c='r', ls='--')
		ax1.set_ylabel('Power Spectrum')
		ax1.set_xlabel('1 / (1024 pix)')
		#ax1.set_xlim(0, np.max(freq))
	
		ax2.plot(cal1[0:-23], label='original', alpha=0.5, lw=1, c='b')
		ax2.plot(newSpectrum[0:-23]+0.5*np.median(newSpectrum[0:-23]), label='defringed', alpha=0.8, lw=1, c='r')
		ax2.legend()
		ax2.set_ylabel('Flux')
		ax2.set_xlabel('Pixel')
		ax2.set_xlim(0, len(cal1[0:-23]))
		plt.tight_layout()
		plt.savefig(save_to_path+"defringed_spectrum.png", bbox_inches='tight')
		plt.show()



if method == 'hanningnotch':

	## REDSPEC version
	cal1        = tell_sp.flux#[pixel_range_start:pixel_range_end]
	xdim        = len(cal1)#[pixel_range_start:pixel_range_end])
	nfil        = xdim//2 + 1

	#print(xdim, nfil//2+1)
	freq        = np.arange(nfil//2+1) / (nfil / float(xdim))
	fil         = np.zeros(len(freq), dtype=np.float) 
	fil[np.where((freq < f_low) | (freq > f_high))] = 1.
	fil         = np.append(fil, np.flip(fil[1:],axis=0))
	fil         = np.real(np.fft.ifft(fil))
	fil         = np.roll(fil, nfil//2)
	fil         = fil*np.hanning(nfil)

	# Smooth the continuum
	#smoothed    = sp.ndimage.uniform_filter1d(cal1, 30)
	#splinefit   = sp.interpolate.interp1d(np.arange(len(smoothed)), smoothed, kind='cubic')
	#cal1smooth  = splinefit(np.arange(0, len(cal1))) #smoothed
	cal1smooth  = sp.ndimage.median_filter(cal1, size=30)

	"""
	plt.figure()
	plt.plot(abs(np.real(fftpack.fft(cal1orig-cal1smooth)))**2, c='k', lw=0.5)
	plt.plot(abs(np.real(fftpack.fft( sp.ndimage.convolve(cal1orig-cal1smooth, fil, mode='wrap') ) ))**2, c='r', lw=0.5)
	plt.ylim(0,25000)
	#plt.xlim(0,800)
	plt.show()
	#sys.exit()

	plt.figure(figsize=(10,6))
	plt.plot(cal1-cal1smooth, lw=0.5, c='k')
	plt.plot(sp.ndimage.convolve(cal1-cal1smooth, fil, mode='wrap'), lw=0.5, c='r')
	plt.plot(sp.ndimage.median_filter(cal1-cal1smooth, 10), lw=0.5, c='m')
	plt.show(block=False)
	#sys.exit()

	plt.figure()
	plt.plot( (cal1-cal1smooth)-sp.ndimage.convolve(cal1-cal1smooth, fil, mode='wrap'), lw=0.5, c='k')
	plt.show(block=True)
	#sys.exit()
	"""
	newSpectrum       = sp.ndimage.convolve(cal1-cal1smooth, fil, mode='wrap') + cal1smooth

	if PLOT:

		# Do the FFT
		cal1fft     = fftpack.rfft(cal1-cal1smooth)
		yp          = abs(cal1fft[0:nfil])**2
		yp          = yp / np.max(yp)
		yp[0:3]     = 0 # Fix for very low order noise

		fig = plt.figure(figsize=(12,6))
		ax1 = plt.subplot2grid((3, 1), (0, 0))
		ax2 = plt.subplot2grid((3, 1), (1, 0), rowspan=2)
		
		freq        = np.arange(nfil)
		ax1.plot(freq, yp)
		ax1.axvline(f_high*2, c='r', ls='--')
		ax1.axvline(f_low*2, c='r', ls='--')
		ax1.set_ylabel('Power Spectrum')
		ax1.set_xlabel('1 / (1024 pix)')
		ax1.set_xlim(0, np.max(freq))
	
		ax2.plot(cal1[0:-23], label='original', alpha=0.5, lw=1, c='b')
		ax2.plot(newSpectrum[0:-23]+0.5*np.median(newSpectrum[0:-23]), label='defringed', alpha=0.8, lw=1, c='r')
		ax2.legend()
		ax2.set_ylabel('Flux')
		ax2.set_xlabel('Pixel')
		ax2.set_xlim(0, len(cal1[0:-23]))
		plt.tight_layout()
		plt.savefig(save_to_path+"defringed_spectrum.png", bbox_inches='tight')
		plt.show()



if method == 'flatfilter':

	cal1     = tell_sp.flux#[pixel_range_start:pixel_range_end]

	W        = fftpack.fftfreq(cal1.size, d=1./1024)
	fftval   = fftpack.rfft(cal1.astype(float))
	fftval[np.where((W > f_low) & (W < f_high))] = 0

	newSpectrum   = fftpack.irfft(fftval) 
	
	if PLOT: 

		xdim       = len(cal1)#[pixel_range_start:pixel_range_end])
		nfil       = xdim//2 + 1

		freq       = np.arange(nfil)

		# Smooth the continuum
		smoothed   = sp.ndimage.uniform_filter1d(cal1, 30)
		splinefit  = sp.interpolate.interp1d(np.arange(len(smoothed)), smoothed, kind='cubic')
		cal1smooth = splinefit(np.arange(0, len(cal1))) #smoothed

		# Do the FFT
		cal1fft    = fftpack.rfft(cal1-cal1smooth)
		yp         = abs(cal1fft[0:nfil])**2 # Power
		yp         = yp / np.max(yp)

		fig = plt.figure(figsize=(12,6))
		ax1 = plt.subplot2grid((3, 1), (0, 0))
		ax2 = plt.subplot2grid((3, 1), (1, 0), rowspan=2)

		ax1.plot(freq, yp)
		ax1.axvline(f_high, c='r', ls='--')
		ax1.axvline(f_low, c='r', ls='--')
		ax1.set_ylabel('Power Spectrum')
		ax1.set_xlabel('1 / (1024 pix)')
		ax1.set_xlim(0, np.max(freq))
	
		ax2.plot(cal1[0:-23], label='original', alpha=0.5, lw=1, c='b')
		ax2.plot(newSpectrum[0:-23]+0.5*np.median(newSpectrum[0:-23]), label='defringed', alpha=0.8, lw=1, c='r')
		ax2.legend()
		ax2.set_ylabel('Flux')
		ax2.set_xlabel('Pixel')
		ax2.set_xlim(0, len(cal1[0:-23]))
		plt.tight_layout()
		plt.savefig(save_to_path+"defringed_spectrum.png", bbox_inches='tight')
		#plt.show()


fullpath  = tell_path + '/' + tell_data_name + '_' + str(order) + '_all.fits'
save_name = save_to_path + '%s_defringe_%s_all.fits'%(tell_data_name, order)

hdulist = fits.open(fullpath)
hdulist.append(fits.PrimaryHDU())

hdulist[-1].data = tell_sp.flux
hdulist[1].data  = newSpectrum

hdulist[-1].header['COMMENT']  = 'Raw Extracted Spectrum'
hdulist[1].header['COMMENT']   = 'Defringed Spectrum'
try:
	hdulist.writeto(save_name, overwrite=True)
except FileNotFoundError:
	hdulist.writeto(save_name)
Esempio n. 8
0
    for numSig in range(cardSig):
        signal = X[p300[numSig],elec]

        dt = 1.0/fs
        wa = WaveletAnalysis(signal, dt=dt)

        # wavelet power spectrum
        power = wa.wavelet_power
        # scales
        scales = wa.fourier_frequencies
        # associated time vector
        t = wa.time
        print wa.fourier_frequencies
        # reconstruction of the original data
        rx = wa.reconstruction()

        fig, ax = plt.subplots()
        T, S = np.meshgrid(t, scales)
        ax.contourf(T, S, power, 100)
        print(S)
    

elif sys.argv[1] == 'meanS':

    decimation = 4
    X, y, _, _ = prepareFiltered('A',0.5,30,decimation)

    scaler = StandardScaler()
    X = scaler.fit_transform(X)