예제 #1
0
def get_data(rawdatafile, start, duration=None, nbins=None, mask=None):
    """Return numpy data array.

        Inputs:
            rawdatafile: A data file as returned by 'open_data_files(...)'
            start: The start of the waterfall plot (in s)
            duration: The duration of the waterfall plot (in s)
            nbins: The duration of the waterfall plot (in bins)
            mask: An rfifind mask to use (Default: no masking)

        Output:
            data: A Spectra object.
    """
    # Read data
    start_bin = np.round(start/rawdatafile.tsamp).astype('int')
    if nbins is None:
        if duration is None:
            raise ValueError("At least one of 'duration' and " \
                             "'nbins' must be provided!")
        else:
            nbins = np.round(duration/rawdatafile.tsamp).astype('int')
    elif duration is not None:
        warnings.warn("Both 'duration' and 'nbins' provided. Will use 'nbins'.")
    data = rawdatafile.get_spectra(start_bin, nbins)
    if mask is not None:
        if isinstance(mask, rfifind.rfifind):
            rfimask = mask
        else:
            rfimask = rfifind.rfifind(mask) 
        datamask = get_mask(rfimask, start_bin, nbins)
        # Mask data
        data = data.masked(datamask, maskval='median-mid80')
    return data
예제 #2
0
def get_data(rawdatafile, start, duration=None, nbins=None, mask=None):
    """Return numpy data array.

        Inputs:
            rawdatafile: A data file as returned by 'open_data_files(...)'
            start: The start of the waterfall plot (in s)
            duration: The duration of the waterfall plot (in s)
            nbins: The duration of the waterfall plot (in bins)
            mask: An rfifind mask to use (Default: no masking)

        Output:
            data: A Spectra object.
    """
    # Read data
    start_bin = np.round(start / rawdatafile.tsamp).astype('int')
    if nbins is None:
        if duration is None:
            raise ValueError("At least one of 'duration' and " \
                             "'nbins' must be provided!")
        else:
            nbins = np.round(duration / rawdatafile.tsamp).astype('int')
    elif duration is not None:
        warnings.warn(
            "Both 'duration' and 'nbins' provided. Will use 'nbins'.")
    data = rawdatafile.get_spectra(start_bin, nbins)
    if mask is not None:
        if isinstance(mask, rfifind.rfifind):
            rfimask = mask
        else:
            rfimask = rfifind.rfifind(mask)
        datamask = get_mask(rfimask, start_bin, nbins)
        # Mask data
        data = data.masked(datamask, maskval='median-mid80')
    return data
예제 #3
0
def maskfile(maskfn,
             data,
             start_bin,
             nbinsextra,
             extra_begin_chan=None,
             extra_end_chan=None):
    rfimask = rfifind.rfifind(maskfn)
    mask = get_mask(rfimask, start_bin, nbinsextra)[::-1]
    num_chan = mask.shape[0]
    if extra_begin_chan != None and extra_end_chan != None:
        if len(extra_begin_chan) != len(extra_end_chan):
            print(
                "length of extra mask begin channels and extra mask end channels need to be the same"
            )
            sys.exit()
        else:
            for ms in range(len(extra_begin_chan)):
                end = num_chan - extra_begin_chan[ms]
                begin = num_chan - extra_end_chan[ms]
                mask[begin:end + 1, :] = True

    masked_chans = mask.all(axis=1)
    # Mask data
    data = data.masked(mask, maskval='median-mid80')

    #datacopy = copy.deepcopy(data)
    return data, masked_chans
예제 #4
0
def maskfile(data, start_bin, nbinsextra):
    if options.maskfile is not None:
        rfimask = rfifind.rfifind(options.maskfile) 
        mask = get_mask(rfimask, start_bin, nbinsextra)
        # Mask data
        data = data.masked(mask, maskval='median-mid80')

        datacopy = copy.deepcopy(data)
    return data
예제 #5
0
def maskfile(data, start_bin, nbinsextra):
    if options.maskfile is not None:
        rfimask = rfifind.rfifind(options.maskfile) 
        mask = get_mask(rfimask, start_bin, nbinsextra)
        # Mask data
        data = data.masked(mask, maskval='median-mid80')

        datacopy = copy.deepcopy(data)
    return data
예제 #6
0
def maskfile(maskfn, data, start_bin, nbinsextra):
    rfimask = rfifind.rfifind(maskfn) 
    mask = get_mask(rfimask, start_bin, nbinsextra)[::-1]
    masked_chans = mask.all(axis=1)
    # Mask data
    data = data.masked(mask, maskval='median-mid80')

    #datacopy = copy.deepcopy(data)
    return data, masked_chans
예제 #7
0
    def get_diagnostic(self):
        # find *rfifind.out file
        rfimask_fn = glob.glob(os.path.join(self.directory, "*%s_rfifind.mask"%os.path.split(self.directory)[-1]))[0]

	mask = rfifind.rfifind(rfimask_fn)
	zaps = 0
	for integ in mask.mask_zap_chans_per_int:
	    zaps += len(integ)
	self.value = 100. * zaps/ float(mask.nchan * mask.nint)
예제 #8
0
def maskfile(maskfn, data, start_bin, nbinsextra):
    rfimask = rfifind.rfifind(maskfn)
    mask = get_mask(rfimask, start_bin, nbinsextra)[::-1]
    masked_chans = mask.all(axis=1)
    # Mask data
    data = data.masked(mask, maskval='median-mid80')

    #datacopy = copy.deepcopy(data)
    return data, masked_chans
예제 #9
0
def pick_rawdatafile(dm, fitsfilenm, freq_lim_1, freq_lim_2, pulse_width,
                     prevDM, prevsubfile, init_flag):
    center_freq = 350.  #MHz
    if init_flag == 1:
        subband = 1  #If this is the first candidate, sub-band at its DM
    else:
        dm_diff = dm - prevDM  #error in DM
        #calculate dispersive smearing in each channel
        t_smear = 8.3e3 * dm_diff * (center_freq**-3) * (
            np.abs(freq_lim_1 - freq_lim_2) / 128.)  #Number of channels = 128

        if pulse_width > (10 * t_smear):
            #pulse does not get smeared out by subbanding at DM of prev. cand
            subband = 0
            rawdatafile = prevsubfile
        else:
            #pulse gets smeared out by subbanding at DM of prev. cand
            subband = 1
    if subband:  #Subband at DM of candidate
        basenm = fitsfilenm[:-5]  #basenm = *_0001, fitsfilenm = *_0001.fits
        subband_file = "%s_subband_%.1f_0001.fits" % (basenm, dm)
        #check if file subbanded at same DM exists, subband at the given DM if it doesn't
        if isfile(subband_file):
            print "Sub-banded file exists at DM of candidate"
        else:
            mask_subband = rfifind.rfifind("%s_rfifind.mask" % (basenm))
            mask_subband.set_zap_chans(power=1000, plot=False)
            mask_subband.set_weights_and_offsets()
            mask_subband.write_weights(filename="%s_weights.txt" % (basenm))
            chan, weights = np.loadtxt("%s_weights.txt" % (basenm),
                                       unpack=True,
                                       skiprows=1,
                                       dtype=int)
            rev_weights = weights[::-1]
            rev_weights[819:1640] = 0
            with open('%s_weights.txt' % (basenm), 'r') as f:
                header = f.readline()
            data = np.column_stack([chan, rev_weights])
            with open('%s_rev_weights.txt' % (basenm), 'w') as f:
                f.write(header)
                np.savetxt(f, data, fmt="%d", delimiter="\t")
            cmd = "psrfits_subband -dm %.1f -nsub 128 -o %s_subband_%.1f -weights %s_rev_weights.txt %s" % (
                dm, basenm, dm, basenm, fitsfilenm)
            print "executing %s" % cmd
            subprocess.call(cmd, shell=True)
        rawdatafile = subband_file
        prevsubfile = subband_file
        prevDM = dm
    return rawdatafile, prevsubfile, prevDM
def maskdata(data, start_bin, nbinsextra, maskfile):
    """
    Performs the masking on the raw data using the boolean array from get_mask.
    Inputs:
        data: raw data (psrfits object) 
        start_bin: the sample number where we want the waterfall plot window to start.
        nbinsextra: number of bins in the waterfall plot
    Output:
        data: 2D array after masking. 
    """

    if maskfile is not None:
        print 'masking'
        rfimask = rfifind.rfifind(maskfile)
        mask = waterfaller.get_mask(rfimask, start_bin, nbinsextra)
        # Mask data
        data = data.masked(mask, maskval='median-mid80')
    return data
예제 #11
0
def maskdata(data, start_bin, nbinsextra, maskfile):
    """
    Performs the masking on the raw data using the boolean array from get_mask.
    Inputs:
        data: raw data (psrfits object) 
        start_bin: the sample number where we want the waterfall plot window to start.
        nbinsextra: number of bins in the waterfall plot
    Output:
        data: 2D array after masking. 
    """
   
    if maskfile is not None:
        print 'masking'
        rfimask = rfifind.rfifind(maskfile)
        mask = waterfaller.get_mask(rfimask, start_bin, nbinsextra)
        # Mask data
        data = data.masked(mask, maskval='median-mid80')
    return data
def pick_rawdatafile(dm, fitsfilenm, freq_lim_1, freq_lim_2, pulse_width, prevDM, prevsubfile, init_flag):
    center_freq = 350. #MHz
    if init_flag == 1:
        subband = 1 #If this is the first candidate, sub-band at its DM
    else:
        dm_diff = dm - prevDM #error in DM 
        #calculate dispersive smearing in each channel
        t_smear = 8.3e3 * dm_diff * ( center_freq**-3 ) * ( np.abs(freq_lim_1 - freq_lim_2) / 128.) #Number of channels = 128 
        
        if pulse_width > (10 * t_smear):
            #pulse does not get smeared out by subbanding at DM of prev. cand
            subband = 0
            rawdatafile = prevsubfile
        else:
            #pulse gets smeared out by subbanding at DM of prev. cand
            subband = 1
    if subband: #Subband at DM of candidate
        basenm = fitsfilenm[:-5] #basenm = *_0001, fitsfilenm = *_0001.fits 
        subband_file = "%s_subband_%.1f_0001.fits" %(basenm,dm)
        #check if file subbanded at same DM exists, subband at the given DM if it doesn't
        if isfile(subband_file):
            print "Sub-banded file exists at DM of candidate"
        else:
            mask_subband = rfifind.rfifind("%s_rfifind.mask"%(basenm))  
            mask_subband.set_zap_chans(power=1000,plot=False)  
            mask_subband.set_weights_and_offsets()
            mask_subband.write_weights(filename="%s_weights.txt"%(basenm))
            chan, weights = np.loadtxt("%s_weights.txt"%(basenm), unpack = True, skiprows=1, dtype=int)
            rev_weights = weights[::-1]
            rev_weights[819:1640] = 0
            with open('%s_weights.txt'%(basenm), 'r') as f:
	        header = f.readline()
            data = np.column_stack([chan,rev_weights])
            with open('%s_rev_weights.txt'%(basenm), 'w') as f:
                f.write(header)
                np.savetxt(f, data, fmt="%d", delimiter="\t")
            cmd = "psrfits_subband -dm %.1f -nsub 128 -o %s_subband_%.1f -weights %s_rev_weights.txt %s"%(dm,basenm,dm,basenm,fitsfilenm)
            print "executing %s" %cmd
            subprocess.call(cmd, shell=True)
        rawdatafile = subband_file
        prevsubfile = subband_file
        prevDM = dm
    return rawdatafile, prevsubfile, prevDM
예제 #13
0
def main():
	fitsfilenm = sys.argv[1]
	basenm = fitsfilenm.split('.')[0]
	dm = 0
	mask_subband = rfifind.rfifind("%s_rfifind.mask"%(basenm))  
	mask_subband.set_zap_chans(power=1000,plot=False)  
	mask_subband.set_weights_and_offsets()
	mask_subband.write_weights(filename="%s_weights.txt"%(basenm))
	chan, weights = np.loadtxt("%s_weights.txt"%(basenm), unpack = True, skiprows=1, dtype=int)
	rev_weights = weights[::-1]
	with open('%s_weights.txt'%(basenm), 'r') as f:
		header = f.readline()
	data = np.column_stack([chan,rev_weights])
	with open('%s_rev_weights.txt'%(basenm), 'w') as f:
		f.write(header)
		np.savetxt(f, data, fmt="%d", delimiter="\t")
	cmd = "psrfits_subband -dm %.1f -nsub 128 -o %s_subband_%.1f -weights %s_rev_weights.txt %s"%(dm,basenm,dm,basenm,fitsfilenm)
	subprocess.call(cmd, shell=True)
	print('end')
예제 #14
0
def waterfall(rawdatafile, start, duration, dm=None, nbins=None, nsub=None,\
              subdm=None, zerodm=False, downsamp=1, scaleindep=False,\
              width_bins=1, mask=False, maskfn=None, bandpass_corr=False, \
              ref_freq=None):
    """
    Create a waterfall plot (i.e. dynamic specrum) from a raw data file.
    Inputs:
       rawdatafile - a PsrfitsData instance.
       start - start time of the data to be read in for waterfalling.
       duration - duration of data to be waterfalled.
    Optional Inputs:
       dm - DM to use when dedispersing data.
             Default: Don't de-disperse
       nbins - Number of time bins to plot. This option overrides
                the duration argument. 
                Default: determine nbins from duration.
       nsub - Number of subbands to use. Must be a factor of number of channels.
               Default: Number of channels.
       subdm - DM to use when subbanding. Default: same as dm argument.
       zerodm - subtract mean of each time-sample from data before 
                 de-dispersing.
       downsamp - Factor to downsample in time by. Default: Don't downsample.
       scaleindep - Scale each channel independently.
                     Default: Scale using global maximum.
       width_bins - Smooth each channel/subband with a boxcar width_bins wide.
                     Default: Don't smooth.
       maskfn - Filename of RFIFIND mask to use for masking data.
                 Default: Don't mask data.
       bandpass_corr - Correct for the bandpass. Requires an rfifind
                        mask provided by maskfn keyword argument.
                        Default: Do not remove bandpass.
       ref_freq - Reference frequency to de-disperse to. 
                   If subbanding and de-dispersing the start time 
                   will be corrected to account for change in
                   reference frequency. 
                   Default: Frequency of top channel.
    Outputs:
       data - Spectra instance of waterfalled data cube.
       nbinsextra - number of time bins read in from raw data. 
       nbins - number of bins in duration.
       start - corrected start time. 
    """

    if subdm is None:
        subdm = dm

    # Read data
    if ref_freq is None:
        ref_freq = rawdatafile.freqs.max()

    if nsub and dm:
        nchan_per_sub = rawdatafile.nchan/nsub
        top_ctrfreq = rawdatafile.freqs.max() - \
                      0.5*nchan_per_sub*rawdatafile.specinfo.df # center of top subband
        start += 4.15e3 * np.abs(1./ref_freq**2 - 1./top_ctrfreq**2) * dm

    start_bin = np.round(start/rawdatafile.tsamp).astype('int')
    dmfac = 4.15e3 * np.abs(1./rawdatafile.frequencies[0]**2 - 1./rawdatafile.frequencies[-1]**2)

    if nbins is None:
        nbins = np.round(duration/rawdatafile.tsamp).astype('int')

    if dm:
	nbinsextra = np.round((duration + dmfac * dm)/rawdatafile.tsamp).astype('int')
    else:
        nbinsextra = nbins    

    # If at end of observation
    if (start_bin + nbinsextra) > rawdatafile.specinfo.N-1:
        nbinsextra = rawdatafile.specinfo.N-1-start_bin

    data = rawdatafile.get_spectra(start_bin, nbinsextra)

    # Masking
    if mask and maskfn:
        data, masked_chans = maskfile(maskfn, data, start_bin, nbinsextra)
    else:
        masked_chans = np.zeros(rawdatafile.nchan,dtype=bool)

    # Bandpass correction
    if maskfn and bandpass_corr:
        bandpass = rfifind.rfifind(maskfn).bandpass_avg[::-1]
        #bandpass[bandpass == 0] = np.min(bandpass[np.nonzero(bandpass)])
        masked_chans[bandpass == 0] = True

        # ignore top and bottom 1% of band
        ignore_chans = np.ceil(0.01*rawdatafile.nchan) 
        masked_chans[:ignore_chans] = True
        masked_chans[-ignore_chans:] = True


    data_masked = np.ma.masked_array(data.data)
    data_masked[masked_chans] = np.ma.masked
    data.data = data_masked

    if bandpass_corr:
       data.data /= bandpass[:, None]

    # Zerodm filtering
    if (zerodm == True):
        data.data -=  data.data.mean(axis=0)

    
    # Subband data
    if (nsub is not None) and (subdm is not None):
        data.subband(nsub, subdm, padval='mean')

    # Dedisperse
    if dm:
        data.dedisperse(dm, padval='mean')

    # Downsample
    data.downsample(downsamp)

    # scale data
    data = data.scaled(scaleindep)
    
    # Smooth
    if width_bins > 1:
        data.smooth(width_bins, padval='mean')

    return data, nbinsextra, nbins, start
예제 #15
0
#!/usr/bin/env python
import rfifind, sys

if __name__=="__main__":
    a = rfifind.rfifind(sys.argv[1])
    sys.stderr.write("\nWARNING!:  If raw data have channels in decreasing freq\n")
    sys.stderr.write("           order, the channel ordering as given will be\n")
    sys.stderr.write("           inverted!  Use 'invertband=True' in \n")
    sys.stderr.write("           write_weights() in that case!\n")
    if (a.idata.telescope=='GBT' and a.idata.lofreq < 1000.0):
        sys.stderr.write("Data is from GBT Prime Focus, auto-flipping the weights/offsets...\n\n")
        invert = True
    else:
        invert = False
    a.set_zap_chans(power=200.0,
                    edges=0.01,
                    asigma=2.0,
                    ssigma=2.0,
                    usemask=True,
                    plot=True,
                    chans=[])
    a.write_zap_chans()
    a.set_weights_and_offsets()
    a.write_weights(invertband=invert)
    a.write_bandpass(invertband=invert)
    #a.write_weights_and_offsets(invertband=invert)
예제 #16
0
def main():
    parser = optparse.OptionParser(prog="sp_pipeline..py", \
                        version=" Chitrang Patel (May. 12, 2015)", \
                        usage="%prog INFILE(PsrFits FILE, SINGLEPULSE FILES)", \
                        description="Create single pulse plots to show the " \
                                    "frequency sweeps of a single pulse,  " \
                    "DM vs time, and SNR vs DM,"\
                                    "in psrFits data.")
    parser.add_option('--infile', dest='infile', type='string', \
                        help="Give a .inf file to read the appropriate header information.")
    parser.add_option('--groupsfile', dest='txtfile', type='string', \
                        help="Give the groups.txt file to read in the groups information.")
    parser.add_option('--mask', dest='maskfile', type='string', \
                        help="Mask file produced by rfifind. (Default: No Mask).", \
                        default=None)
    parser.add_option('-n', dest='maxnumcands', type='int', \
                        help="Maximum number of candidates to plot. (Default: 100).", \
                        default=100)
    options, args = parser.parse_args()
    if not hasattr(options, 'infile'):
        raise ValueError("A .inf file must be given on the command line! ")
    if not hasattr(options, 'txtfile'):
        raise ValueError(
            "The groups.txt file must be given on the command line! ")

    files = get_textfile(options.txtfile)
    print_debug("Begining waterfaller... " + strftime("%Y-%m-%d %H:%M:%S"))
    if not args[0].endswith("fits"):
        raise ValueError("The first file must be a psrFits file! ")
    print_debug('Maximum number of candidates to plot: %i' %
                options.maxnumcands)
    basename = args[0][:-5]
    filetype = "psrfits"
    inffile = options.infile
    topo, bary = bary_and_topo.bary_to_topo(inffile)
    time_shift = bary - topo
    inf = infodata.infodata(inffile)
    RA = inf.RA
    dec = inf.DEC
    MJD = inf.epoch
    mjd = Popen(["mjd2cal", "%f" % MJD], stdout=PIPE, stderr=PIPE)
    date, err = mjd.communicate()
    date = date.split()[2:5]
    telescope = inf.telescope
    N = inf.N
    numcands = 0
    Total_observed_time = inf.dt * N
    print_debug('getting file..')
    values = split_parameters(options.txtfile)
    if len(values) > options.maxnumcands:
        values = sorted(values, key=itemgetter(
            5, 1))  #sorting candidates based on ranks and snr
        values = values[-options.maxnumcands:]
        print "More than", options.maxnumcands, "candidates, making plots for", options.maxnumcands, "candidates"
    values = sorted(values, key=itemgetter(0))
    for ii in range(len(values)):
        #### Array for Plotting DM vs SNR
        print_debug("Making arrays for DM vs Signal to Noise...")
        temp_list = files[values[ii][6] - 6].split()
        npulses = int(temp_list[2])
        temp_lines = files[(values[ii][6] + 3):(values[ii][6] + npulses + 1)]
        arr = np.split(temp_lines, len(temp_lines))
        dm_list = []
        time_list = []
        for i in range(len(arr)):
            dm_val = float(arr[i][0].split()[0])
            time_val = float(arr[i][0].split()[2])
            dm_list.append(dm_val)
            time_list.append(time_val)
        arr_2 = np.array([arr[i][0].split() for i in range(len(arr))],
                         dtype=np.float32)
        dm_arr = np.array([arr_2[i][0] for i in range(len(arr))],
                          dtype=np.float32)
        sigma_arr = np.array([arr_2[i][1] for i in range(len(arr))],
                             dtype=np.float32)
        #### Array for Plotting DM vs Time is in show_spplots.plot(...)

        #### Setting variables up for the waterfall arrays.
        j = ii + 1
        subdm = dm = sweep_dm = values[ii][0]
        sample_number = values[ii][3]
        rank = values[ii][5]
        width_bins = values[ii][4]
        #print "dm", dm
        #print "width_bins", width_bins
        downsamp = np.round(
            (values[ii][2] / sample_number / inf.dt)).astype('int')
        #print "downsamp", downsamp
        pulse_width = width_bins * downsamp * inf.dt
        #print "pulse_width", pulse_width
        if ii == 0:
            mask_subband = rfifind.rfifind("%s_rfifind.mask" % (basename))
            mask_subband.set_zap_chans(power=1000, plot=False)
            mask_subband.set_weights_and_offsets()
            mask_subband.write_weights(filename="%s_weights.txt" % (basename))
            cmd = "psrfits_subband -dm %.2f -nsub 128 -o %s_subband_%.2f -weights %s_weights.txt %s" % (
                dm, basename, dm, basename, args[0])
            call(cmd, shell=True)
            #subband args[0] at dm and then generate a file that will be set equal to rawdatafile
            subband_file = "%s_subband_%.2f_0001.fits" % (basename, dm)
            dm_prev = dm
            subband_prev = subband_file
        else:
            dm_diff = dm - dm_prev
            t_smear = 8.3e3 * dm_diff * (350**-3) * (
                np.abs(rawdatafile.frequencies[0] -
                       rawdatafile.frequencies[-1]) / 128.)
            if (5 * t_smear) > pulse_width:
                cmd = "psrfits_subband -dm %.2f -nsub 128 -o %s_subband_%.2f -weights %s_weights.txt %s" % (
                    dm, basename, dm, basename, args[0])
                call(cmd, shell=True)
                #subband args[0] at dm and then generate a file that will be set equal to rawdatafile
                subband_file = "%s_subband_%.2f_0001.fits" % (basename, dm)
                dm_prev = dm
                subband_prev = subband_file

        rawdatafile = psrfits.PsrfitsFile(subband_file)
        bin_shift = np.round(time_shift / rawdatafile.tsamp).astype('int')
        integrate_dm = None
        sigma = values[ii][1]
        sweep_posn = 0.0
        bary_start_time = values[ii][2]
        topo_start_time = bary_start_time - topo_timeshift(
            bary_start_time, time_shift, topo)[0]
        binratio = 50
        scaleindep = False
        zerodm = None
        duration = binratio * width_bins * rawdatafile.tsamp * downsamp
        start = topo_start_time - (0.25 * duration)
        if (start < 0.0):
            start = 0.0
        if sigma <= 7:
            nsub = 32
        elif sigma >= 7 and sigma < 10:
            nsub = 64
        else:
            nsub = 128
        nbins = np.round(duration / rawdatafile.tsamp).astype('int')
        start_bin = np.round(start / rawdatafile.tsamp).astype('int')
        dmfac = 4.15e3 * np.abs(1. / rawdatafile.frequencies[0]**2 -
                                1. / rawdatafile.frequencies[-1]**2)
        nbinsextra = np.round(
            (duration + dmfac * dm) / rawdatafile.tsamp).astype('int')
        if (start_bin + nbinsextra) > N - 1:
            nbinsextra = N - 1 - start_bin
        data = rawdatafile.get_spectra(start_bin, nbinsextra)
        data = maskdata(data, start_bin, nbinsextra, options.maskfile)
        #make an array to store header information for the .npz files
        temp_filename = basename + "_DM%.1f_%.1fs_rank_%i" % (
            subdm, topo_start_time, rank)
        # Array for Plotting Dedispersed waterfall plot - zerodm - OFF
        print_debug("Running waterfaller with Zero-DM OFF...")
        data, Data_dedisp_nozerodm = waterfall_array(
            start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm,
            integrate_dm, downsamp, scaleindep, width_bins, rawdatafile,
            binratio, data)
        #Add additional information to the header information array
        text_array = np.array([
            subband_file, 'GBT', RA, dec, MJD, rank, nsub, nbins, subdm, sigma,
            sample_number, duration, width_bins, pulse_width,
            rawdatafile.tsamp, Total_observed_time, topo_start_time,
            data.starttime, data.dt, data.numspectra,
            data.freqs.min(),
            data.freqs.max()
        ])
        #### Array for plotting Dedispersed waterfall plot zerodm - ON
        print_debug("Running Waterfaller with Zero-DM ON...")
        #print "before get_spectra",memory.resident()/(1024.0**3)
        data = rawdatafile.get_spectra(start_bin, nbinsextra)
        #print "after get_spectra",memory.resident()/(1024.0**3)
        data = maskdata(data, start_bin, nbinsextra, options.maskfile)
        zerodm = True
        data, Data_dedisp_zerodm = waterfall_array(start_bin, dmfac, duration,
                                                   nbins, zerodm, nsub, subdm,
                                                   dm, integrate_dm, downsamp,
                                                   scaleindep, width_bins,
                                                   rawdatafile, binratio, data)
        #print "waterfall",memory.resident()/(1024.0**3)
        ####Sweeped without zerodm
        start = start + (0.25 * duration)
        start_bin = np.round(start / rawdatafile.tsamp).astype('int')
        sweep_duration = 4.15e3 * np.abs(
            1. / rawdatafile.frequencies[0]**2 -
            1. / rawdatafile.frequencies[-1]**2) * sweep_dm
        nbins = np.round(sweep_duration / (rawdatafile.tsamp)).astype('int')
        if ((nbins + start_bin) > (N - 1)):
            nbins = N - 1 - start_bin

#print "before get_spectra",memory.resident()/(1024.0**3)
        data = rawdatafile.get_spectra(start_bin, nbins)
        #print "after get_spectra",memory.resident()/(1024.0**3)
        data = maskdata(data, start_bin, nbins, options.maskfile)
        zerodm = None
        dm = None
        data, Data_nozerodm = waterfall_array(start_bin, dmfac, duration,
                                              nbins, zerodm, nsub, subdm, dm,
                                              integrate_dm, downsamp,
                                              scaleindep, width_bins,
                                              rawdatafile, binratio, data)
        #print "waterfall",memory.resident()/(1024.0**3)
        text_array = np.append(text_array, sweep_duration)
        text_array = np.append(text_array, data.starttime)
        text_array = np.append(text_array, bary_start_time)
        # Array to Construct the sweep
        if sweep_dm is not None:
            ddm = sweep_dm - data.dm
            delays = psr_utils.delay_from_DM(ddm, data.freqs)
            delays -= delays.min()
            delays_nozerodm = delays
            freqs_nozerodm = data.freqs
        # Sweeped with zerodm-on
        zerodm = True
        downsamp_temp = 1
        data, Data_zerodm = waterfall_array(start_bin, dmfac, duration, nbins,
                                            zerodm, nsub, subdm, dm,
                                            integrate_dm, downsamp_temp,
                                            scaleindep, width_bins,
                                            rawdatafile, binratio, data)
        #print "waterfall",memory.resident()/(1024.0**3)
        # Saving the arrays into the .spd file.
        with open(temp_filename + ".spd", 'wb') as f:
            np.savez_compressed(
                f,
                Data_dedisp_nozerodm=Data_dedisp_nozerodm.astype(np.float16),
                Data_dedisp_zerodm=Data_dedisp_zerodm.astype(np.float16),
                Data_nozerodm=Data_nozerodm.astype(np.float16),
                delays_nozerodm=delays_nozerodm,
                freqs_nozerodm=freqs_nozerodm,
                Data_zerodm=Data_zerodm.astype(np.float16),
                dm_arr=map(np.float16, dm_arr),
                sigma_arr=map(np.float16, sigma_arr),
                dm_list=map(np.float16, dm_list),
                time_list=map(np.float16, time_list),
                text_array=text_array)
        print_debug("Now plotting...")
        #print "Before plot..",memory.resident()/(1024.0**3)
        show_spplots.plot(temp_filename + ".spd",
                          args[1:],
                          xwin=False,
                          outfile=basename,
                          tar=None)
        print_debug("Finished plot %i " % j + strftime("%Y-%m-%d %H:%M:%S"))
        #print "After plot..",memory.resident()/(1024.0**3)
        numcands += 1
        print_debug('Finished sp_candidate : %i' % numcands)
    print_debug("Finished running waterfaller... " +
                strftime("%Y-%m-%d %H:%M:%S"))
예제 #17
0
def main():
    parser = optparse.OptionParser(prog="sp_pipeline..py", \
                        version=" Chitrang Patel (May. 12, 2015)", \
                        usage="%prog INFILE(PsrFits FILE, SINGLEPULSE FILES)", \
                        description="Create single pulse plots to show the " \
                                    "frequency sweeps of a single pulse,  " \
                    "DM vs time, and SNR vs DM,"\
                                    "in psrFits data.")
    parser.add_option('--infile', dest='infile', type='string', \
                        help="Give a .inf file to read the appropriate header information.")
    parser.add_option('--groupsfile', dest='txtfile', type='string', \
                        help="Give the groups.txt file to read in the groups information.") 
    parser.add_option('--mask', dest='maskfile', type='string', \
                        help="Mask file produced by rfifind. (Default: No Mask).", \
                        default=None)
    parser.add_option('-n', dest='maxnumcands', type='int', \
                        help="Maximum number of candidates to plot. (Default: 100).", \
                        default=100)
    options, args = parser.parse_args()
    if not hasattr(options, 'infile'):
        raise ValueError("A .inf file must be given on the command line! ") 
    if not hasattr(options, 'txtfile'):
        raise ValueError("The groups.txt file must be given on the command line! ") 
    
    files = get_textfile(options.txtfile)
    print_debug("Begining waterfaller... "+strftime("%Y-%m-%d %H:%M:%S"))
    if not args[0].endswith("fits"):
        raise ValueError("The first file must be a psrFits file! ") 
    print_debug('Maximum number of candidates to plot: %i'%options.maxnumcands)
    basename = args[0][:-5]
    filetype = "psrfits"
    inffile = options.infile
    topo, bary = bary_and_topo.bary_to_topo(inffile)
    time_shift = bary-topo
    inf = infodata.infodata(inffile)
    RA = inf.RA
    dec = inf.DEC
    MJD = inf.epoch
    mjd = Popen(["mjd2cal", "%f"%MJD], stdout=PIPE, stderr=PIPE)
    date, err = mjd.communicate()
    date = date.split()[2:5]
    telescope = inf.telescope
    N = inf.N
    numcands=0
    Total_observed_time = inf.dt *N
    print_debug('getting file..')
    values = split_parameters(options.txtfile)
    if len(values)> options.maxnumcands:
	values=sorted(values, key=itemgetter(5,1)) #sorting candidates based on ranks and snr
	values=values[-options.maxnumcands:] 
	print "More than", options.maxnumcands, "candidates, making plots for", options.maxnumcands, "candidates" 
    values = sorted(values, key=itemgetter(0))
    for ii in range(len(values)):
        #### Array for Plotting DM vs SNR
        print_debug("Making arrays for DM vs Signal to Noise...")
        temp_list = files[values[ii][6]-6].split()
        npulses = int(temp_list[2])
        temp_lines = files[(values[ii][6]+3):(values[ii][6]+npulses+1)]
        arr = np.split(temp_lines, len(temp_lines))
        dm_list = []
        time_list = []
        for i in range(len(arr)):
            dm_val= float(arr[i][0].split()[0])
            time_val = float(arr[i][0].split()[2])
            dm_list.append(dm_val)
            time_list.append(time_val)
        arr_2 = np.array([arr[i][0].split() for i in range(len(arr))], dtype = np.float32)
        dm_arr = np.array([arr_2[i][0] for i in range(len(arr))], dtype = np.float32)
        sigma_arr = np.array([arr_2[i][1] for i in range(len(arr))], dtype = np.float32)
	#### Array for Plotting DM vs Time is in show_spplots.plot(...)

                
        #### Setting variables up for the waterfall arrays.
        j = ii+1
        subdm = dm = sweep_dm= values[ii][0]
	sample_number = values[ii][3]
	rank=values[ii][5]
	width_bins = values[ii][4]
	#print "dm", dm 	
        #print "width_bins", width_bins 
	downsamp = np.round((values[ii][2]/sample_number/inf.dt)).astype('int')
	#print "downsamp", downsamp 
	pulse_width = width_bins*downsamp*inf.dt
	#print "pulse_width", pulse_width 
	if ii == 0:
	    mask_subband=rfifind.rfifind("%s_rfifind.mask"%(basename))
	    mask_subband.set_zap_chans(power=1000,plot=False)
	    mask_subband.set_weights_and_offsets()
	    mask_subband.write_weights(filename="%s_weights.txt"%(basename))
	    cmd="psrfits_subband -dm %.2f -nsub 128 -o %s_subband_%.2f -weights %s_weights.txt %s"%(dm,basename,dm,basename,args[0])
	    call(cmd, shell=True)
	    #subband args[0] at dm and then generate a file that will be set equal to rawdatafile
	    subband_file="%s_subband_%.2f_0001.fits" %(basename,dm)
	    dm_prev=dm
	    subband_prev= subband_file
	else:	
	    dm_diff=dm-dm_prev
	    t_smear=8.3e3*dm_diff*(350**-3)*(np.abs(rawdatafile.frequencies[0]-rawdatafile.frequencies[-1])/128.)
	    if (5*t_smear) > pulse_width:
		cmd="psrfits_subband -dm %.2f -nsub 128 -o %s_subband_%.2f -weights %s_weights.txt %s"%(dm,basename,dm,basename,args[0])
		call(cmd, shell=True)
		#subband args[0] at dm and then generate a file that will be set equal to rawdatafile
		subband_file="%s_subband_%.2f_0001.fits" %(basename,dm)
		dm_prev=dm
		subband_prev=subband_file

	rawdatafile = psrfits.PsrfitsFile(subband_file)
	bin_shift = np.round(time_shift/rawdatafile.tsamp).astype('int')
	integrate_dm = None
	sigma = values[ii][1]
        sweep_posn = 0.0
        bary_start_time = values[ii][2]
        topo_start_time = bary_start_time - topo_timeshift(bary_start_time, time_shift, topo)[0]
	binratio = 50
	scaleindep = False
        zerodm = None
        duration = binratio * width_bins * rawdatafile.tsamp * downsamp
	start = topo_start_time - (0.25 * duration)
	if (start<0.0):
            start = 0.0        
	if sigma <= 7:
	    nsub = 32
	elif sigma >= 7 and sigma < 10:
            nsub = 64
        else:
            nsub = 128
        nbins = np.round(duration/rawdatafile.tsamp).astype('int')
	start_bin = np.round(start/rawdatafile.tsamp).astype('int')
        dmfac = 4.15e3 * np.abs(1./rawdatafile.frequencies[0]**2 - 1./rawdatafile.frequencies[-1]**2)
	nbinsextra = np.round((duration + dmfac * dm)/rawdatafile.tsamp).astype('int')
	if (start_bin+nbinsextra) > N-1:
            nbinsextra = N-1-start_bin
	data = rawdatafile.get_spectra(start_bin, nbinsextra)
        data = maskdata(data, start_bin, nbinsextra, options.maskfile)
	#make an array to store header information for the .npz files
        temp_filename = basename+"_DM%.1f_%.1fs_rank_%i"%(subdm, topo_start_time, rank)
	# Array for Plotting Dedispersed waterfall plot - zerodm - OFF
        print_debug("Running waterfaller with Zero-DM OFF...")
        data, Data_dedisp_nozerodm = waterfall_array(start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm, integrate_dm, downsamp, scaleindep, width_bins, rawdatafile, binratio, data)
        #Add additional information to the header information array
        text_array = np.array([subband_file, 'GBT', RA, dec, MJD, rank, nsub, nbins, subdm, sigma, sample_number, duration, width_bins, pulse_width, rawdatafile.tsamp, Total_observed_time, topo_start_time, data.starttime, data.dt, data.numspectra, data.freqs.min(), data.freqs.max()])
        #### Array for plotting Dedispersed waterfall plot zerodm - ON
        print_debug("Running Waterfaller with Zero-DM ON...")
	#print "before get_spectra",memory.resident()/(1024.0**3)
        data = rawdatafile.get_spectra(start_bin, nbinsextra)
	#print "after get_spectra",memory.resident()/(1024.0**3)
        data = maskdata(data, start_bin, nbinsextra, options.maskfile)
        zerodm = True
        data, Data_dedisp_zerodm = waterfall_array(start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm, integrate_dm, downsamp, scaleindep, width_bins, rawdatafile, binratio, data)
        #print "waterfall",memory.resident()/(1024.0**3)
	####Sweeped without zerodm
        start = start + (0.25*duration)
        start_bin = np.round(start/rawdatafile.tsamp).astype('int')
        sweep_duration = 4.15e3 * np.abs(1./rawdatafile.frequencies[0]**2-1./rawdatafile.frequencies[-1]**2)*sweep_dm
        nbins = np.round(sweep_duration/(rawdatafile.tsamp)).astype('int')
        if ((nbins+start_bin)> (N-1)):
            nbins = N-1-start_bin
	#print "before get_spectra",memory.resident()/(1024.0**3)
        data = rawdatafile.get_spectra(start_bin, nbins)
	#print "after get_spectra",memory.resident()/(1024.0**3)
        data = maskdata(data, start_bin, nbins, options.maskfile)
        zerodm = None
        dm = None
        data, Data_nozerodm = waterfall_array(start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm, integrate_dm, downsamp, scaleindep, width_bins, rawdatafile, binratio, data)
        #print "waterfall",memory.resident()/(1024.0**3)
	text_array = np.append(text_array, sweep_duration)
        text_array = np.append(text_array, data.starttime)
        text_array = np.append(text_array, bary_start_time)
        # Array to Construct the sweep
        if sweep_dm is not None:
            ddm = sweep_dm-data.dm
            delays = psr_utils.delay_from_DM(ddm, data.freqs)
            delays -= delays.min()
            delays_nozerodm = delays
            freqs_nozerodm = data.freqs
        # Sweeped with zerodm-on 
        zerodm = True
        downsamp_temp = 1
        data, Data_zerodm = waterfall_array(start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm, integrate_dm, downsamp_temp, scaleindep, width_bins, rawdatafile, binratio, data)
        #print "waterfall",memory.resident()/(1024.0**3)
	# Saving the arrays into the .spd file.
        with open(temp_filename+".spd", 'wb') as f:
            np.savez_compressed(f, Data_dedisp_nozerodm = Data_dedisp_nozerodm.astype(np.float16), Data_dedisp_zerodm = Data_dedisp_zerodm.astype(np.float16), Data_nozerodm = Data_nozerodm.astype(np.float16), delays_nozerodm = delays_nozerodm, freqs_nozerodm = freqs_nozerodm, Data_zerodm = Data_zerodm.astype(np.float16), dm_arr= map(np.float16, dm_arr), sigma_arr = map(np.float16, sigma_arr), dm_list= map(np.float16, dm_list), time_list = map(np.float16, time_list), text_array = text_array)
        print_debug("Now plotting...")
        #print "Before plot..",memory.resident()/(1024.0**3)
	show_spplots.plot(temp_filename+".spd", args[1:], xwin=False, outfile = basename, tar = None)
        print_debug("Finished plot %i " %j+strftime("%Y-%m-%d %H:%M:%S"))
	#print "After plot..",memory.resident()/(1024.0**3)
	numcands+=1
        print_debug('Finished sp_candidate : %i'%numcands)
    print_debug("Finished running waterfaller... "+strftime("%Y-%m-%d %H:%M:%S"))
예제 #18
0
def plot(spdfile, singlepulsefiles=None, maskfn = None, spec_width=1.5, loc_pulse=0.5, xwin=False, outfile="spdplot", just_waterfall=True, \
         integrate_spec=True, integrate_ts=True, disp_pulse=True, bandpass_corr= None, tar=None):
    """
       Generates spd plots which include the following subplots:
           De-dispersed Zero-DM filtered Waterfall plot
           De-dispersed Waterfall plot
        optional subplots:
           Dispersed Zero-DM filtered Waterfall plot (Inset of the corresponding dedispersed plot).
           Dispersed Waterfall plot ((Inset of the corresponding dedispersed plot).).
           Dedispersed zero-DM filtered time series for the corresponding waterfall plot.
           Dedispersed time series for the corresponding waterfall plot.
           Spectra of the de-dispersed pulse for each of the above waterfalled plots.
           SNR vs DM
           DM vs. Time
        Inputs:
           spdfile: A .spd file.
        Optional Inputs:  
           spec_width: Twice this number times the pulse_width around the pulse to consider for the spectrum
           loc_pulse: Fraction of the window length where the pulse is located.(eg. 0.25 = 1/4th of the way in.
                                                                                0.5 = middle of the plot)
           singlepulsefiles: list of .singlepulse files
           xwin: plot in an xwin window?
           outfile: name of the output file you want.
           just_waterfall: Do you only want to display the waterfall plots?
           integrate_spec: Do you want to show the pulse spectrum?
           integrate_ts: Do you want to show the time series?
           disp_pulse: Do you want to show the inset dispersed pulse?
           tar: Supply the tarball of the singlepulse files instead of individual files.
    """
    if not spdfile.endswith(".spd"):
	    raise ValueError("The first file must be a .spd file")
    #npzfile = np.load(spdfile)
    spdobj = read_spd.spd(spdfile) 
    ##### Read in the header information and other required variables for the plots. ######
    #text_array = npzfile['text_array']
    man_params = spdobj.man_params
    fn = spdobj.filename
    telescope = spdobj.telescope
    RA = spdobj.ra
    dec = spdobj.dec
    MJD = spdobj.mjd
    mjd = Popen(["mjd2cal", "%f"%MJD], stdout=PIPE, stderr=PIPE)
    date, err = mjd.communicate()
    date = date.split()[2:5]
    rank = spdobj.rank
    nsub = spdobj.waterfall_nsubs
    nbins = spdobj.nsamp
    subdm = dm = sweep_dm = spdobj.best_dm
    sigma = spdobj.sigma
    sample_number = spdobj.pulse_peak_sample
    duration = spdobj.waterfall_duration
    width_bins = spdobj.pulsewidth_bins
    pulse_width = spdobj.pulsewidth_seconds
    tsamp = spdobj.tsamp
    Total_observed_time = spdobj.total_obs_time
    topo_start = spdobj.pulse_peak_time
    datastart = spdobj.waterfall_start_time
    datasamp = spdobj.waterfall_tsamp
    #datanumspectra = spdobj.waterfall_prededisp_nbins
    datanumspectra = spdobj.waterfall_nbins
    start = topo_start - loc_pulse*duration
    min_freq = spdobj.min_freq
    max_freq = spdobj.max_freq
    sweep_duration = spdobj.sweep_duration
    sweeped_start = spdobj.sweep_start_time
    bary_start = spdobj.bary_pulse_peak_time
    downsamp = datasamp/tsamp
    if xwin:
        pgplot_device = "/XWIN"
    else:
        pgplot_device = ""
    if pgplot_device:
        sp_pgplot.ppgplot.pgopen(pgplot_device)
    else:
        if (outfile == "spdplot"): # default filename
            if rank:
                sp_pgplot.ppgplot.pgopen(fn[:-5]+'_DM%.1f_%.1fs_rank_%i.spd.ps/VPS'%(subdm, (start+loc_pulse*duration), rank))
            else:
                sp_pgplot.ppgplot.pgopen(fn[:-5]+'_DM%.1f_%.1fs.spd.ps/VPS'%(subdm, (start+loc_pulse*duration)))
        else:
            if rank:
                sp_pgplot.ppgplot.pgopen(outfile+'_DM%.1f_%.1fs_rank_%i.spd.ps/VPS'%(subdm, (start+loc_pulse*duration), rank))
            else:
                sp_pgplot.ppgplot.pgopen(outfile+'_DM%.1f_%.1fs.spd.ps/VPS'%(subdm, (start+loc_pulse*duration)))
    if (just_waterfall == False):
        sp_pgplot.ppgplot.pgpap(10.25, 8.5/11.0)
        # Dedispersed waterfall plot - zerodm - OFF
        array = spdobj.data_nozerodm_dedisp.astype(np.float64)
        sp_pgplot.ppgplot.pgsvp(0.07, 0.40, 0.50, 0.80)
        sp_pgplot.ppgplot.pgswin(datastart-start, datastart-start+datanumspectra*datasamp, min_freq, max_freq)
        sp_pgplot.ppgplot.pgsch(0.8)
        sp_pgplot.ppgplot.pgslw(3)
        sp_pgplot.ppgplot.pgbox("BCST", 0, 0, "BCNST", 0, 0)
        sp_pgplot.ppgplot.pgslw(3)
        sp_pgplot.ppgplot.pgmtxt('L', 1.8, 0.5, 0.5, "Observing Frequency (MHz)")
        if not integrate_spec:
            sp_pgplot.ppgplot.pgmtxt('R', 1.8, 0.5, 0.5, "Zero-dm filtering - Off")
        sp_pgplot.plot_waterfall(array,rangex = [datastart-start, datastart-start+datanumspectra*datasamp], rangey = [min_freq, max_freq], image = 'apjgrey')
        
         #### Plot Dedispersed Time series - Zerodm filter - Off
        Dedisp_ts = array[::-1].sum(axis = 0)
        times = np.arange(int((datastart-start)/datasamp),int((datastart-start)/datasamp)+datanumspectra)*datasamp
        if integrate_ts: 
            sp_pgplot.ppgplot.pgsvp(0.07, 0.40, 0.80, 0.90)
            sp_pgplot.ppgplot.pgswin(datastart-start, datastart-start+datanumspectra*datasamp, np.min(Dedisp_ts), 1.05*np.max(Dedisp_ts))
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgbox("BC", 0, 0, "BC", 0, 0)
            sp_pgplot.ppgplot.pgsci(1)
            sp_pgplot.ppgplot.pgline(times,Dedisp_ts)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgsci(1)
            
            errx1 = np.array([0.60 * (datastart-start+duration)])
            erry1 = np.array([0.60 * np.max(Dedisp_ts)])
            erry2 = np.array([np.std(Dedisp_ts)])
            errx2 = np.array([pulse_width])
            sp_pgplot.ppgplot.pgerrb(5, errx1, erry1, errx2, 1.0)
            sp_pgplot.ppgplot.pgpt(errx1, erry1, -1)
        
        #### Plot Spectrum - Zerodm filter - Off
        if integrate_spec:
            spectrum_window = spec_width*pulse_width
            window_width = int(spectrum_window/datasamp)
            burst_bin = int(nbins*loc_pulse/downsamp)
            on_spec = array[..., burst_bin-window_width:burst_bin+window_width]
            Dedisp_spec = on_spec.sum(axis=1)
            if bandpass_corr:
                bandpass = rfifind.rfifind(maskfn).bandpass_avg
                bandpass = bandpass.reshape(-1, len(bandpass)/len(Dedisp_spec)).mean(axis=1)
                Dedisp_spec /= bandpass
                del_percent = 0.10 #remove total 10% of band
                chans_del = np.ceil(len(Dedisp_spec)*del_percent/2.)
                Dedisp_spec[-int(chans_del):] = 0
                Dedisp_spec[:int(chans_del)] = 0
            freqs = np.linspace(min_freq, max_freq, len(Dedisp_spec))
            sp_pgplot.ppgplot.pgsvp(0.4, 0.47, 0.5, 0.8)
            sp_pgplot.ppgplot.pgswin(np.min(Dedisp_spec), 1.05*np.max(Dedisp_spec), min_freq, max_freq)
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgbox("BC", 0, 0, "BC", 0, 0)
            sp_pgplot.ppgplot.pgsci(1)
            sp_pgplot.ppgplot.pgline(Dedisp_spec,freqs)
            sp_pgplot.ppgplot.pgmtxt('R', 1.8, 0.5, 0.5, "Zero-dm filtering - Off")
            sp_pgplot.ppgplot.pgsch(0.7)
            sp_pgplot.ppgplot.pgmtxt('T', 1.8, 0.5, 0.5, "Spectrum")
            sp_pgplot.ppgplot.pgsch(0.8)
        
        #Dedispersed waterfall plot - Zerodm ON
        sp_pgplot.ppgplot.pgsvp(0.07, 0.40, 0.1, 0.40)
        sp_pgplot.ppgplot.pgswin(datastart-start , datastart-start+datanumspectra*datasamp, min_freq, max_freq)
        sp_pgplot.ppgplot.pgsch(0.8)
        sp_pgplot.ppgplot.pgslw(3)
        sp_pgplot.ppgplot.pgbox("BCNST", 0, 0, "BCNST", 0, 0)
        sp_pgplot.ppgplot.pgmtxt('B', 2.5, 0.5, 0.5, "Time - %.2f s"%datastart)
        sp_pgplot.ppgplot.pgmtxt('L', 1.8, 0.5, 0.5, "Observing Frequency (MHz)")
        if not integrate_spec:
            sp_pgplot.ppgplot.pgmtxt('R', 1.8, 0.5, 0.5, "Zero-dm filtering - On")
        array = spdobj.data_zerodm_dedisp.astype(np.float64)
        sp_pgplot.plot_waterfall(array,rangex = [datastart-start, datastart-start+datanumspectra*datasamp],rangey = [min_freq, max_freq],image = 'apjgrey')
        #### Plot Dedispersed Time series - Zerodm filter - On
        dedisp_ts = array[::-1].sum(axis = 0)
        times = np.arange(int((datastart-start)/datasamp),int((datastart-start)/datasamp)+datanumspectra)*datasamp
        #times = np.arange(datanumspectra)*datasamp
        if integrate_ts:
            sp_pgplot.ppgplot.pgsvp(0.07, 0.40, 0.40, 0.50)
            sp_pgplot.ppgplot.pgswin(datastart - start, datastart-start+datanumspectra*datasamp, np.min(dedisp_ts), 1.05*np.max(dedisp_ts))
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgbox("BC", 0, 0, "BC", 0, 0)
            sp_pgplot.ppgplot.pgsci(1)
            sp_pgplot.ppgplot.pgline(times,dedisp_ts)
            errx1 = np.array([0.60 * (datastart-start+duration)])
            erry1 = np.array([0.60 * np.max(dedisp_ts)])
            erry2 = np.array([np.std(dedisp_ts)])
            errx2 = np.array([pulse_width])
            sp_pgplot.ppgplot.pgerrb(5, errx1, erry1, errx2, 1.0)
            sp_pgplot.ppgplot.pgpt(errx1, erry1, -1)
        
        #### Plot Spectrum - Zerodm filter - On
        if integrate_spec:
            spectrum_window = spec_width*pulse_width
            window_width = int(spectrum_window/datasamp)
            burst_bin = int(nbins*loc_pulse/downsamp)
            on_spec = array[..., burst_bin-window_width:burst_bin+window_width]
            Dedisp_spec = on_spec.sum(axis=1)
            if bandpass_corr:
                bandpass = rfifind.rfifind(maskfn).bandpass_avg
                bandpass = bandpass.reshape(-1, len(bandpass)/len(Dedisp_spec)).mean(axis=1)
                Dedisp_spec /= bandpass
                del_percent = 0.10 # total
                chans_del = np.ceil(len(Dedisp_spec)*del_percent/2.)
                Dedisp_spec[-int(chans_del):] = 0
                Dedisp_spec[:int(chans_del)] = 0
            freqs = np.linspace(min_freq, max_freq, len(Dedisp_spec))
            sp_pgplot.ppgplot.pgsvp(0.4, 0.47, 0.1, 0.4)
            sp_pgplot.ppgplot.pgswin(np.min(Dedisp_spec), 1.05*np.max(Dedisp_spec), min_freq, max_freq)
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgbox("BC", 0, 0, "BC", 0, 0)
            sp_pgplot.ppgplot.pgsci(1)
            sp_pgplot.ppgplot.pgline(Dedisp_spec,freqs)
            sp_pgplot.ppgplot.pgmtxt('R', 1.8, 0.5, 0.5, "Zero-dm filtering - On")
            sp_pgplot.ppgplot.pgsch(0.7)
            sp_pgplot.ppgplot.pgmtxt('T', 1.8, 0.5, 0.5, "Spectrum")
            sp_pgplot.ppgplot.pgsch(0.8)
        
        if disp_pulse:
            # Sweeped waterfall plot Zerodm - OFF
            array = spdobj.data_nozerodm.astype(np.float64)
            sp_pgplot.ppgplot.pgsvp(0.20, 0.40, 0.50, 0.70)
            sp_pgplot.ppgplot.pgswin(sweeped_start, sweeped_start+sweep_duration, min_freq, max_freq)
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(4)
            sp_pgplot.ppgplot.pgbox("BCST", 0, 0, "BCST", 0, 0)
            sp_pgplot.ppgplot.pgsch(3)
            sp_pgplot.plot_waterfall(array,rangex = [sweeped_start, sweeped_start+sweep_duration],rangey = [min_freq, max_freq],image = 'apjgrey')
            delays = spdobj.dmsweep_delays
            freqs = spdobj.dmsweep_freqs
            sp_pgplot.ppgplot.pgslw(5)
            sweepstart = sweeped_start- 0.2*sweep_duration
            sp_pgplot.ppgplot.pgsci(0)
            sp_pgplot.ppgplot.pgline(delays+sweepstart, freqs)
            sp_pgplot.ppgplot.pgsci(1)
            sp_pgplot.ppgplot.pgslw(3)
            
            # Sweeped waterfall plot Zerodm - ON
            array = spdobj.data_zerodm.astype(np.float64)
            sp_pgplot.ppgplot.pgsvp(0.20, 0.40, 0.1, 0.3)
            sp_pgplot.ppgplot.pgswin(sweeped_start, sweeped_start+sweep_duration, min_freq, max_freq)
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(4)
            sp_pgplot.ppgplot.pgbox("BCST", 0, 0, "BCST", 0, 0)
            sp_pgplot.ppgplot.pgsch(3)
            sp_pgplot.plot_waterfall(array,rangex = [sweeped_start, sweeped_start+sweep_duration],rangey = [min_freq, max_freq],image = 'apjgrey')
            sp_pgplot.ppgplot.pgslw(5)
            sweepstart = sweeped_start- 0.2*sweep_duration
            sp_pgplot.ppgplot.pgsci(0)
            sp_pgplot.ppgplot.pgline(delays+sweepstart, freqs)
            sp_pgplot.ppgplot.pgsci(1)
        
        #### Figure texts 
        if integrate_spec:
            sp_pgplot.ppgplot.pgsvp(0.81, 0.97, 0.64, 0.909)
            sp_pgplot.ppgplot.pgsch(0.62)
        else:
            sp_pgplot.ppgplot.pgsvp(0.745, 0.97, 0.64, 0.909)
            sp_pgplot.ppgplot.pgsch(0.7)
        sp_pgplot.ppgplot.pgslw(3)
        sp_pgplot.ppgplot.pgmtxt('T', -1.1, 0.01, 0.0, "RA: %s" %RA)
        sp_pgplot.ppgplot.pgmtxt('T', -2.6, 0.01, 0.0, "DEC: %s" %dec)
        sp_pgplot.ppgplot.pgmtxt('T', -4.1, 0.01, 0.0, "MJD: %f" %MJD)
        sp_pgplot.ppgplot.pgmtxt('T', -5.6, 0.01, 0.0, "Obs date: %s %s %s" %(date[0], date[1], date[2]))
        sp_pgplot.ppgplot.pgmtxt('T', -7.1, 0.01, 0.0, "Telescope: %s" %telescope)
        sp_pgplot.ppgplot.pgmtxt('T', -8.6, 0.01, 0.0, "DM: %.2f pc cm\u-3\d" %dm)
        if sigma:
            sp_pgplot.ppgplot.pgmtxt('T', -10.1, 0.01, 0.0, "S/N\dMAX\u: %.2f" %sigma)
        else:
            sp_pgplot.ppgplot.pgmtxt('T', -10.1, 0.01, 0.0, "S/N\dMAX\u: N/A")
        sp_pgplot.ppgplot.pgmtxt('T', -11.6, 0.01, 0.0, "Number of samples: %i" %nbins)
        sp_pgplot.ppgplot.pgmtxt('T', -13.1, 0.01, 0.0, "Number of subbands: %i" %nsub)
        sp_pgplot.ppgplot.pgmtxt('T', -14.6, 0.01, 0.0, "Pulse width: %.2f ms" %(pulse_width*1e3))
        sp_pgplot.ppgplot.pgmtxt('T', -16.1, 0.01, 0.0, "Sampling time: %.3f \gms" %(tsamp*1e6))
        sp_pgplot.ppgplot.pgmtxt('T', -17.6, 0.0, 0.0, "Bary pulse peak time: %.2f s" %(bary_start))
        sp_pgplot.ppgplot.pgsvp(0.07, 0.7, 0.01, 0.05)
        sp_pgplot.ppgplot.pgmtxt('T', -2.1, 0.01, 0.0, "%s" %fn)
        
        #DM vs SNR
        if not man_params:
            dm_arr = np.float32(spdobj.dmVt_this_dms)
            sigma_arr = np.float32 (spdobj.dmVt_this_sigmas)
            time_arr = np.float32 (spdobj.dmVt_this_times)
            if integrate_spec:
                sp_pgplot.ppgplot.pgsvp(0.55, 0.80, 0.65, 0.90)
            else:
                sp_pgplot.ppgplot.pgsvp(0.48, 0.73, 0.65, 0.90)
            sp_pgplot.ppgplot.pgswin(np.min(dm_arr), np.max(dm_arr), 0.95*np.min(sigma_arr), 1.05*np.max(sigma_arr))
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgbox("BCNST", 0, 0, "BCNST", 0, 0)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgmtxt('B', 2.5, 0.5, 0.5, "DM (pc cm\u-3\d)")
            sp_pgplot.ppgplot.pgmtxt('L', 1.8, 0.5, 0.5, "Signal-to-noise")
            sp_pgplot.ppgplot.pgpt(dm_arr, sigma_arr, 20)
        else:
            dm_arr = np.array([])
            sigma_arr = np.array([])
            time_arr = np.array([])
            if integrate_spec:
                sp_pgplot.ppgplot.pgsvp(0.55, 0.80, 0.65, 0.90)
            else:
                sp_pgplot.ppgplot.pgsvp(0.48, 0.73, 0.65, 0.90)
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgbox("BCNST", 0, 0, "BCNST", 0, 0)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgmtxt('B', 2.5, 0.5, 0.5, "DM (pc cm\u-3\d)")
            sp_pgplot.ppgplot.pgmtxt('L', 1.8, 0.5, 0.5, "Signal-to-noise")

        # DM vs Time
        print "Making arrays for DM vs time plot"
        spfiles = singlepulsefiles
        threshold = 5.0
        if len(spfiles) > 2:
            dm_list = map(np.float32, list(dm_arr))
            time_list = map(np.float32, list(time_arr))
            if integrate_spec:
                sp_pgplot.ppgplot.pgsvp(0.55, 0.97, 0.1, 0.54)
            else:
                sp_pgplot.ppgplot.pgsvp(0.48, 0.97, 0.1, 0.54)
            dms, times, sigmas, widths, filelist = spio.gen_arrays(dm_arr, spfiles, tar, threshold)
            sp_pgplot.dm_time_plot(dms, times, sigmas, dm_list, sigma_arr, time_list, Total_observed_time, xwin)
        else:
            print "You need a .singlepulse.tgz file to plot DM vs Time plot."
            if integrate_spec:
                sp_pgplot.ppgplot.pgsvp(0.55, 0.97, 0.1, 0.54)
            else:
                sp_pgplot.ppgplot.pgsvp(0.48, 0.97, 0.1, 0.54)
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgbox("BCNST", 0, 0, "BCNST", 0, 0)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgmtxt('B', 2.5, 0.5, 0.5, "Time (s)")
            sp_pgplot.ppgplot.pgmtxt('L', 1.8, 0.5, 0.5, "DM (pc cm\u-3\d)")
    else:
        #sp_pgplot.ppgplot.pgpap(10.25, 10.0/5.0)
        sp_pgplot.ppgplot.pgpap(8.0, 1.5)
        # Dedispersed waterfall plot - zerodm - OFF
        array = spdobj.data_nozerodm_dedisp.astype(np.float64)
        sp_pgplot.ppgplot.pgsvp(0.1, 0.70, 0.44, 0.75)
        sp_pgplot.ppgplot.pgswin(datastart - start, datastart -start+datanumspectra*datasamp, min_freq, max_freq)
        sp_pgplot.ppgplot.pgsch(0.8)
        sp_pgplot.ppgplot.pgslw(3)
        sp_pgplot.ppgplot.pgbox("BCST", 0, 0, "BCNST", 0, 0)
        sp_pgplot.ppgplot.pgslw(3)
        sp_pgplot.ppgplot.pgmtxt('L', 1.8, 0.5, 0.5, "Observing Frequency (MHz)")
        sp_pgplot.plot_waterfall(array,rangex = [datastart-start, datastart-start+datanumspectra*datasamp], rangey = [min_freq, max_freq], image = 'apjgrey')
         
        #### Plot Dedispersed Time series - Zerodm filter - Off
        Dedisp_ts = array[::-1].sum(axis = 0)
        times = np.arange(int((datastart-start)/datasamp),int((datastart-start)/datasamp)+datanumspectra)*datasamp
        #times = np.arange(datanumspectra)*datasamp
        if integrate_ts:
            sp_pgplot.ppgplot.pgsvp(0.1, 0.70, 0.75, 0.83)
            sp_pgplot.ppgplot.pgswin(datastart - start, datastart-start+datanumspectra*datasamp, np.min(Dedisp_ts), 1.05*np.max(Dedisp_ts))
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgbox("BC", 0, 0, "BC", 0, 0)
            sp_pgplot.ppgplot.pgsci(1)
            sp_pgplot.ppgplot.pgline(times,Dedisp_ts)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgsci(1)
            errx1 = np.array([0.60 * (datastart-start+duration)])
            erry1 = np.array([0.60 * np.max(Dedisp_ts)])
            erry2 = np.array([np.std(Dedisp_ts)])
            errx2 = np.array([pulse_width])
            sp_pgplot.ppgplot.pgerrb(5, errx1, erry1, errx2, 1.0)
            sp_pgplot.ppgplot.pgpt(errx1, erry1, -1)
        
        #### Plot Spectrum - Zerodm filter - Off
        if integrate_spec:
            spectrum_window = spec_width*pulse_width
            window_width = int(spectrum_window/datasamp)
            burst_bin = int(nbins*loc_pulse/downsamp)
            on_spec = array[..., burst_bin-window_width:burst_bin+window_width]
            Dedisp_spec = on_spec.sum(axis=1)
            if bandpass_corr:
                bandpass = rfifind.rfifind(maskfn).bandpass_avg
                bandpass = bandpass.reshape(-1, len(bandpass)/len(Dedisp_spec)).mean(axis=1)
                Dedisp_spec /= bandpass
                del_percent = 0.10 # total
                chans_del = np.ceil(len(Dedisp_spec)*del_percent/2.)
                Dedisp_spec[-int(chans_del):] = 0
                Dedisp_spec[:int(chans_del)] = 0
            freqs = np.linspace(min_freq, max_freq, len(Dedisp_spec))
            sp_pgplot.ppgplot.pgsvp(0.7, 0.9, 0.44, 0.75)
            sp_pgplot.ppgplot.pgswin(np.min(Dedisp_spec), 1.05*np.max(Dedisp_spec), min_freq, max_freq)
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgbox("BC", 0, 0, "BC", 0, 0)
            sp_pgplot.ppgplot.pgsci(1)
            sp_pgplot.ppgplot.pgline(Dedisp_spec,freqs)
            sp_pgplot.ppgplot.pgmtxt('R', 1.8, 0.5, 0.5, "Zero-dm filtering - Off")
            sp_pgplot.ppgplot.pgsch(0.7)
            sp_pgplot.ppgplot.pgmtxt('T', 1.8, 0.5, 0.5, "Spectrum")
            sp_pgplot.ppgplot.pgsch(0.8)
        
        #Dedispersed waterfall plot - Zerodm ON
        array = spdobj.data_zerodm_dedisp.astype(np.float64)
        sp_pgplot.ppgplot.pgsvp(0.1, 0.70, 0.05, 0.36)
        sp_pgplot.ppgplot.pgswin(datastart-start , datastart-start+datanumspectra*datasamp, min_freq, max_freq)
        sp_pgplot.ppgplot.pgsch(0.8)
        sp_pgplot.ppgplot.pgslw(3)
        sp_pgplot.ppgplot.pgbox("BCNST", 0, 0, "BCNST", 0, 0)
        sp_pgplot.ppgplot.pgmtxt('B', 2.5, 0.5, 0.5, "Time - %.2f s"%datastart)
        sp_pgplot.ppgplot.pgmtxt('L', 1.8, 0.5, 0.5, "Observing Frequency (MHz)")
        sp_pgplot.plot_waterfall(array,rangex = [datastart-start, datastart-start+datanumspectra*datasamp],rangey = [min_freq, max_freq],image = 'apjgrey')
        
        
        #### Plot Dedispersed Time series - Zerodm filter - On
        dedisp_ts = array[::-1].sum(axis = 0)
        #times = np.arange(datanumspectra)*datasamp
        times = np.arange(int((datastart-start)/datasamp),int((datastart-start)/datasamp)+datanumspectra)*datasamp
        if integrate_ts:
            sp_pgplot.ppgplot.pgsvp(0.1, 0.7, 0.36, 0.44)
            sp_pgplot.ppgplot.pgswin(datastart - start, datastart-start+datanumspectra*datasamp, np.min(dedisp_ts), 1.05*np.max(dedisp_ts))
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgbox("BC", 0, 0, "BC", 0, 0)
            sp_pgplot.ppgplot.pgsci(1)
            sp_pgplot.ppgplot.pgline(times,dedisp_ts)
            errx1 = np.array([0.60 * (datastart-start+duration)])
            erry1 = np.array([0.60 * np.max(dedisp_ts)])
            erry2 = np.array([np.std(dedisp_ts)])
            errx2 = np.array([pulse_width])
            sp_pgplot.ppgplot.pgerrb(5, errx1, erry1, errx2, 1.0)
            sp_pgplot.ppgplot.pgpt(errx1, erry1, -1)
        
        #### Plot Spectrum - Zerodm filter - On
        if integrate_spec:
            spectrum_window = spec_width*pulse_width
            window_width = int(spectrum_window/datasamp)
            burst_bin = int(nbins*loc_pulse/downsamp)
            on_spec = array[..., burst_bin-window_width:burst_bin+window_width]
            Dedisp_spec = on_spec.sum(axis=1)
            if bandpass_corr:
                bandpass = rfifind.rfifind(maskfn).bandpass_avg
                bandpass = bandpass.reshape(-1, len(bandpass)/len(Dedisp_spec)).mean(axis=1)
                Dedisp_spec /= bandpass
                del_percent = 0.10 # total
                chans_del = np.ceil(len(Dedisp_spec)*del_percent/2.)
                Dedisp_spec[-int(chans_del):] = 0
                Dedisp_spec[:int(chans_del)] = 0
            freqs = np.linspace(min_freq, max_freq, len(Dedisp_spec))
            sp_pgplot.ppgplot.pgsvp(0.70, 0.90, 0.05, 0.36)
            sp_pgplot.ppgplot.pgswin(np.min(Dedisp_spec), 1.05*np.max(Dedisp_spec), min_freq, max_freq)
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(3)
            sp_pgplot.ppgplot.pgbox("BC", 0, 0, "BC", 0, 0)
            sp_pgplot.ppgplot.pgsci(1)
            sp_pgplot.ppgplot.pgline(Dedisp_spec,freqs)
            sp_pgplot.ppgplot.pgmtxt('R', 1.8, 0.5, 0.5, "Zero-dm filtering - On")
            sp_pgplot.ppgplot.pgsch(0.7)
            sp_pgplot.ppgplot.pgmtxt('T', 1.8, 0.5, 0.5, "Spectrum")
            sp_pgplot.ppgplot.pgsch(0.8)
        if disp_pulse: 
            # Sweeped waterfall plot Zerodm - OFF
            array = spdobj.data_nozerodm.astype(np.float64)
            sp_pgplot.ppgplot.pgsvp(0.3, 0.70, 0.44, 0.65)
            sp_pgplot.ppgplot.pgswin(sweeped_start, sweeped_start+sweep_duration, min_freq, max_freq)
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(4)
            sp_pgplot.ppgplot.pgbox("BCST", 0, 0, "BCST", 0, 0)
            sp_pgplot.ppgplot.pgsch(3)
            sp_pgplot.plot_waterfall(array,rangex = [sweeped_start, sweeped_start+sweep_duration],rangey = [min_freq, max_freq],image = 'apjgrey')
            delays = spdobj.dmsweep_delays
            freqs = spdobj.dmsweep_freqs
            sp_pgplot.ppgplot.pgslw(5)
            sweepstart = sweeped_start- 0.2*sweep_duration
            sp_pgplot.ppgplot.pgsci(0)
            sp_pgplot.ppgplot.pgline(delays+sweepstart, freqs)
            sp_pgplot.ppgplot.pgsci(1)
            sp_pgplot.ppgplot.pgslw(3)
            
            # Sweeped waterfall plot Zerodm - ON
            array = spdobj.data_zerodm.astype(np.float64)
            sp_pgplot.ppgplot.pgsvp(0.3, 0.70, 0.05, 0.25)
            sp_pgplot.ppgplot.pgswin(sweeped_start, sweeped_start+sweep_duration, min_freq, max_freq)
            sp_pgplot.ppgplot.pgsch(0.8)
            sp_pgplot.ppgplot.pgslw(4)
            sp_pgplot.ppgplot.pgbox("BCST", 0, 0, "BCST", 0, 0)
            sp_pgplot.ppgplot.pgsch(3)
            sp_pgplot.plot_waterfall(array,rangex = [sweeped_start, sweeped_start+sweep_duration],rangey = [min_freq, max_freq],image = 'apjgrey')
            sp_pgplot.ppgplot.pgslw(5)
            sweepstart = sweeped_start- 0.2*sweep_duration
            sp_pgplot.ppgplot.pgsci(0)
            sp_pgplot.ppgplot.pgline(delays+sweepstart, freqs)
            sp_pgplot.ppgplot.pgsci(1)
        
        #### Figure texts 
        sp_pgplot.ppgplot.pgsvp(0.05, 0.95, 0.8, 0.9)
        sp_pgplot.ppgplot.pgsch(0.65)
        sp_pgplot.ppgplot.pgslw(3)
        sp_pgplot.ppgplot.pgmtxt('T', -1.1, 0.01, 0.0, "RA: %s" %RA)
        sp_pgplot.ppgplot.pgmtxt('T', -2.5, 0.01, 0.0, "DEC: %s" %dec)
        sp_pgplot.ppgplot.pgmtxt('T', -3.9, 0.01, 0.0, "MJD: %f" %MJD)
        sp_pgplot.ppgplot.pgmtxt('T', -5.3, 0.01, 0.0, "Obs date: %s %s %s" %(date[0], date[1], date[2]))
        sp_pgplot.ppgplot.pgmtxt('T', -1.1, 0.35, 0.0, "Telescope: %s" %telescope)
        sp_pgplot.ppgplot.pgmtxt('T', -2.5, 0.35, 0.0, "DM: %.2f pc cm\u-3\d" %dm)
        if sigma:
            sp_pgplot.ppgplot.pgmtxt('T', -3.9, 0.35, 0.0, "S/N\dMAX\u: %.2f" %sigma)
        else:
            sp_pgplot.ppgplot.pgmtxt('T', -3.9, 0.35, 0.0, "S/N\dMAX\u: N/A")
        sp_pgplot.ppgplot.pgmtxt('T', -5.3, 0.35, 0.0, "Number of samples: %i" %nbins)
        sp_pgplot.ppgplot.pgmtxt('T', -1.1, 0.65, 0.0, "Number of subbands: %i" %nsub)
        sp_pgplot.ppgplot.pgmtxt('T', -2.5, 0.65, 0.0, "Pulse width: %.2f ms" %(pulse_width*1e3))
        sp_pgplot.ppgplot.pgmtxt('T', -3.9, 0.65, 0.0, "Sampling time: %.3f \gms" %(tsamp*1e6))
        sp_pgplot.ppgplot.pgmtxt('T', -5.3, 0.65, 0.0, "Bary pulse peak time: %.2f s" %(bary_start))
    sp_pgplot.ppgplot.pgiden()
    sp_pgplot.ppgplot.pgclos()
예제 #19
0
def main():
    filfns = args # addtional argument is fileterbank files
    if options.debug:
        print "Input filterbank files:", filfns

    obs = fbobs.fbobs(filfns)
    # filfile.print_header()
    obslen = obs.obslen 

    # Determine start and end of interval (in seconds and samples)
    if options.start < 0:
        options.start = 0
    if (options.end is None) or (options.end > obslen):
        # set to end of filterbank file
        options.end = obslen

    reqstartsamp = int(options.start / obs.tsamp) # requested
    # Round down to a multiple of downsamp bins
    reqstartsamp = reqstartsamp - (reqstartsamp % options.downsamp)
    # Get extra bins for smoothing
    startsamp = reqstartsamp - options.width*options.downsamp

    reqendsamp = int(options.end / obs.tsamp) # requested
    # Round up to a multiple of downsamp bins
    reqendsamp = reqendsamp - (reqendsamp % options.downsamp) + options.downsamp
    # Get extra bins for smoothing
    endsamp = reqendsamp + options.width*options.downsamp
    if options.dm:
        # Get extra bins for dedispersion
        # Compute DM delays
        delay_seconds = psr_utils.delay_from_DM(options.dm, obs.frequencies)
        delay_seconds -= np.min(delay_seconds)
        delay_samples = delay_seconds/(options.downsamp*obs.tsamp)
        maxsamps = np.max(delay_samples*options.downsamp)
        maxsamps = int(np.round(float(maxsamps)/options.downsamp))*options.downsamp
        endsamp += maxsamps

    reqnumsaps = reqendsamp - reqstartsamp
    numsamps = endsamp - startsamp

    if options.debug:
        print "Requested start time: %s s (%d samples)" % \
                    (options.start, reqstartsamp)
        print "Actual start time: %s s (%d samples)" % \
                    (startsamp*obs.tsamp, startsamp)
        print "Requested end time: %s s (%d samples)" % \
                    (options.end, reqendsamp)
        print "Actual end time: %s s (%d samples)" % \
                    (endsamp*obs.tsamp, endsamp)

    # read data
    data = obs.get_sample_interval(startsamp, endsamp).astype('float32')
    obs.close_all()
    data.shape = (numsamps, obs.nchans)
   
    if options.mask is not None:
        if options.debug:
            print "Masking channels using %s" % options.mask
        # Mask channels
        mask = rfifind.rfifind(options.mask)
        maskchans = mask.mask_zap_chans
        maskchans = obs.nchans - 1 - np.array(list(maskchans))
        data = mask_channels(data, maskchans)

    # Modify data
    if options.downsamp > 1:
        if options.debug:
            print "Downsampling by %d bins" % options.downsamp
        data = downsample(data, factor=options.downsamp)
    if options.width > 1:
        if options.debug:
            print "Smoothing with boxcar %d bins wide" % options.width
        data = smooth(data, factor=options.width)[options.width:-options.width,:]
        startsamp += options.width*options.downsamp
        endsamp -= options.width*options.downsamp

    # plot data as an image
    fig = plt.figure()
    fig.canvas.set_window_title("Frequency vs. Time")
    ax = plt.axes((0.15, 0.15, 0.8, 0.7))
    data_scaled = scale(data, indep=options.scaleindep)
    data_scaled = data_scaled[:-maxsamps/options.downsamp]
    endsamp -= maxsamps
    plt.imshow(data_scaled.transpose(), \
                    aspect='auto', cmap=matplotlib.cm.binary, interpolation='nearest', \
                    extent=(startsamp/options.downsamp, endsamp/options.downsamp, \
                                obs.frequencies[-1], obs.frequencies[0]))
    plt.xlabel("Sample")
    plt.ylabel("Observing frequency (MHz)")
    plt.suptitle("Frequency vs. Time")
    fig.text(0.05, 0.02, \
                r"Start time: $\sim$ %s s, End time: $\sim$ %s s; " \
                "Downsampled: %d bins, Smoothed: %d bins; " \
                "DM trace: %g $cm^{-3}pc$" % \
                (options.start, options.end, options.downsamp, \
                    options.width, options.dm), \
                ha="left", va="center", size="x-small")
    if options.dm is not None:
        xlim = plt.xlim()
        ylim = plt.ylim()
        
        # Plot dispersion delay trace
        plt.plot(startsamp/options.downsamp+delay_samples, obs.frequencies, \
                    'r-', lw=5, alpha=0.25)
        plt.xlim(xlim)
        plt.ylim(ylim)

        profax = plt.axes((0.15, 0.85, 0.8, 0.1), sharex=ax)
        dedisp_prof = dedisperse(data, delay_samples)[:-maxsamps/options.downsamp]
        plt.plot(np.linspace(xlim[0],xlim[1],dedisp_prof.size), dedisp_prof, 'k-')
        plt.setp(profax.xaxis.get_ticklabels(), visible=False)
        plt.setp(profax.yaxis.get_ticklabels(), visible=False)
        plt.xlim(xlim)
    fig.canvas.mpl_connect('key_press_event', keypress)
    plt.show()
예제 #20
0
def foldfile(filename, simdataPath, foldpath, dm, period):
    os.chdir(foldpath)
    cutfilename = filename[:-5] + '_cut' + filename[-5:]
    #cut file
    #----------------------------------------------
    if dm < 500:
        cutfile = str(
            'python ~/psrsoft/OPTIMUS/python/fitsio_cutfreq.py %s/%s %s %s %s/%s'
            %
            (simdataPath, filename, 270, 270 + 256 * 2, foldpath, cutfilename))
        print cutfile
        output = getoutput(cutfile)
    elif dm < 1000:
        cutfile = str(
            'python ~/psrsoft/OPTIMUS/python/fitsio_cutfreq.py %s/%s %s %s %s/%s'
            % (simdataPath, filename, 416, 416 + 256, foldpath, cutfilename))
        print cutfile
        output = getoutput(cutfile)
    else:
        cutfile = str(
            'python ~/psrsoft/OPTIMUS/python/fitsio_cutfreq.py %s/%s %s %s %s/%s'
            % (simdataPath, filename, 500, 500 + 256, foldpath, cutfilename))
        print cutfile
        output = getoutput(cutfile)

    #rfifind
    maskfilename = filename[:-5]
    rfifind = str('rfifind %s/%s -o %s -time 1' %
                  (foldpath, cutfilename, maskfilename))
    print rfifind
    output = getoutput(rfifind)

    #get number of bad intervals
    maskFile = prestoRFIfind.rfifind(
        glob.glob('%s/%s*.mask' % (foldpath, maskfilename))[0])
    Intervals = maskFile.nchan * maskFile.nint * 1.
    badIntervals = 0.
    for i in maskFile.mask_zap_chans_per_int:
        badIntervals = len(i) + badIntervals

    #fold file
    #if badIntervals more than 50% of Intervals: fold without maskFile
    #if badIntervals less than 50% of Intervals: fold with maskFile
    #foldfile = str('prepfold -noxwin -nosearch -p %s -dm %s -mask %s/%s %s/%s' %(period, dm, foldpath, maskfilename+'_rfifind.mask', foldpath, cutfilename))
    if ((badIntervals / Intervals) > 0.5):
        foldfile = str('prepfold -noxwin -nosearch -p %s -dm %s -o %s %s/%s' %
                       (period, dm, cutfilename[:-5], foldpath, cutfilename))
    else:
        foldfile = str(
            'prepfold -noxwin -nosearch -p %s -dm %s -o %s -mask %s/%s %s/%s' %
            (period, dm, cutfilename[:-5], foldpath,
             maskfilename + '_rfifind.mask', foldpath, cutfilename))

    print foldfile
    output = getoutput(foldfile)

    #find pfdfile
    #pfdfilename = glob.glob('%s/%s*.pfd' %(foldpath, maskfilename))
    pfdfilename = glob.glob('%s/%s*.pfd' % (foldpath, cutfilename[:-5]))

    return pfdfilename
예제 #21
0
def waterfall(rawdatafile, start, duration, dm=None, nbins=None, nsub=None,\
              subdm=None, zerodm=False, downsamp=1, scaleindep=False,\
              width_bins=1, mask=False, maskfn=None, bandpass_corr=False, \
              ref_freq=None):
    """
    Create a waterfall plot (i.e. dynamic specrum) from a raw data file.
    Inputs:
       rawdatafile - a PsrfitsData instance.
       start - start time of the data to be read in for waterfalling.
       duration - duration of data to be waterfalled.
    Optional Inputs:
       dm - DM to use when dedispersing data.
             Default: Don't de-disperse
       nbins - Number of time bins to plot. This option overrides
                the duration argument. 
                Default: determine nbins from duration.
       nsub - Number of subbands to use. Must be a factor of number of channels.
               Default: Number of channels.
       subdm - DM to use when subbanding. Default: same as dm argument.
       zerodm - subtract mean of each time-sample from data before 
                 de-dispersing.
       downsamp - Factor to downsample in time by. Default: Don't downsample.
       scaleindep - Scale each channel independently.
                     Default: Scale using global maximum.
       width_bins - Smooth each channel/subband with a boxcar width_bins wide.
                     Default: Don't smooth.
       maskfn - Filename of RFIFIND mask to use for masking data.
                 Default: Don't mask data.
       bandpass_corr - Correct for the bandpass. Requires an rfifind
                        mask provided by maskfn keyword argument.
                        Default: Do not remove bandpass.
       ref_freq - Reference frequency to de-disperse to. 
                   If subbanding and de-dispersing the start time 
                   will be corrected to account for change in
                   reference frequency. 
                   Default: Frequency of top channel.
    Outputs:
       data - Spectra instance of waterfalled data cube.
       nbinsextra - number of time bins read in from raw data. 
       nbins - number of bins in duration.
       start - corrected start time. 
    """

    if subdm is None:
        subdm = dm

    # Read data
    if ref_freq is None:
        ref_freq = rawdatafile.freqs.max()

    if nsub and dm:
        df = rawdatafile.freqs[1] - rawdatafile.freqs[0]
        nchan_per_sub = rawdatafile.nchan / nsub
        top_ctrfreq = rawdatafile.freqs.max() - \
                      0.5*nchan_per_sub*df # center of top subband
        start += 4.15e3 * np.abs(1. / ref_freq**2 - 1. / top_ctrfreq**2) * dm

    try:
        source_name = rawdatafile.header['source_name']
    except:
        source_name = "Unknown"

    start_bin = np.round(start / rawdatafile.tsamp).astype('int')
    dmfac = 4.15e3 * np.abs(1. / rawdatafile.frequencies[0]**2 -
                            1. / rawdatafile.frequencies[-1]**2)

    if nbins is None:
        nbins = np.round(duration / rawdatafile.tsamp).astype('int')

    if dm:
        nbinsextra = np.round(
            (duration + dmfac * dm) / rawdatafile.tsamp).astype('int')
    else:
        nbinsextra = nbins

    # If at end of observation
    if (start_bin + nbinsextra) > rawdatafile.nspec - 1:
        nbinsextra = rawdatafile.nspec - 1 - start_bin

    data = rawdatafile.get_spectra(start_bin, nbinsextra)
    # Masking
    if mask and maskfn:
        data, masked_chans = maskfile(maskfn, data, start_bin, nbinsextra)
    else:
        masked_chans = np.zeros(rawdatafile.nchan, dtype=bool)

    # Bandpass correction
    if maskfn and bandpass_corr:
        bandpass = rfifind.rfifind(maskfn).bandpass_avg[::-1]
        #bandpass[bandpass == 0] = np.min(bandpass[np.nonzero(bandpass)])
        masked_chans[bandpass == 0] = True

        # ignore top and bottom 1% of band
        # VG: there is some bug here so "masking" these three lines
        #ignore_chans = int(np.ceil(0.01*rawdatafile.nchan))
        #masked_chans[:ignore_chans] = True
        #masked_chans[-ignore_chans:] = True

    data_masked = np.ma.masked_array(data.data)
    data_masked[masked_chans] = np.ma.masked
    data.data = data_masked

    if bandpass_corr:
        data.data /= bandpass[:, None]

    # Zerodm filtering
    if (zerodm == True):
        data.data -= data.data.mean(axis=0)

    # Subband data
    if (nsub is not None) and (subdm is not None):
        data.subband(nsub, subdm, padval='mean')

    # Dedisperse
    if dm:
        data.dedisperse(dm, padval='rotate')

    # Downsample
    # Moving it after the DM-vs-time plot
    #data.downsample(downsamp)

    #Orignal scale has error if chan is flagged so writting this here
    if not scaleindep:
        std = data.data.std()
    for ii in range(data.numchans):
        chan = data.get_chan(ii)
        median = np.median(chan)
        if scaleindep:
            std = chan.std()
        if std:
            chan[:] = (chan - median) / std
        else:
            chan[:] = chan

    # scale data
    #data = data.scaled(scaleindep)
    #data = data.scaled(False)

    # Smooth
    if width_bins > 1:
        for ii in range(data.numchans):
            chan = data.get_chan(ii)
            nbin = len(chan)
            x = range(nbin)
            if (nbin > 4000): deg = 10
            if (nbin < 4000 and nbin > 2000): deg = 8
            if (nbin < 2000 and nbin > 1000): deg = 6
            if (nbin < 1000 and nbin > 150): deg = 5
            if (nbin < 150): deg = 2
            base = np.polyfit(x, chan, deg)
            p = np.poly1d(base)
            chan[:] = chan[:] - p(x)

    return data, nbinsextra, nbins, start, source_name
예제 #22
0
#!/usr/bin/env python
import rfifind, sys

if __name__ == "__main__":
    a = rfifind.rfifind(sys.argv[1])
    sys.stderr.write(
        "\nWARNING!:  If raw data have channels in decreasing freq\n")
    sys.stderr.write(
        "           order, the channel ordering as given will be\n")
    sys.stderr.write("           inverted!  Use 'invertband=True' in \n")
    sys.stderr.write("           write_weights() in that case!\n")
    if (a.idata.telescope == 'GBT' and a.idata.lofreq < 1000.0):
        sys.stderr.write(
            "Data is from GBT Prime Focus, auto-flipping the weights/offsets...\n\n"
        )
        invert = True
    else:
        invert = False
    # Write the bandpass before we zap things
    a.write_bandpass(invertband=invert)
    # Now do the zapping and set the weights
    a.set_zap_chans(power=200.0,
                    edges=0.01,
                    asigma=2.0,
                    ssigma=2.0,
                    usemask=True,
                    plot=True,
                    chans=[])
    a.write_zap_chans()
    a.set_weights_and_offsets()
    a.write_weights(invertband=invert)
예제 #23
0
# Source
subprocess.check_output(['psredit', '-c', 'name=' + pulsar, '-m', pfd])
# Reciever
if telid == '19':
    subprocess.check_output(['psredit', '-c', 'rcvr:name=AI', '-m', pfd])
else:
    subprocess.check_output(['psredit', '-c', 'rcvr:name=AII', '-m', pfd])
# Backend
subprocess.check_output(['psredit', '-c', 'be:name=Ettus-B120', '-m', pfd])
# Proyect
subprocess.check_output(['psredit', '-c', 'obs:projid=PuMA', '-m', pfd])

# Save all relevant data from the pfd using rfifind and psrchive.

arch = psrchive.Archive_load(pfd)
maskrfi = rfifind.rfifind(maskname)
mjd = arch.start_time()

#Usable % of the observations due to RFI. We do not take into account pazi filters.

maskrfi.read_bytemask()
maskarr = maskrfi.bytemask
nbadint = float(np.count_nonzero(maskarr))
ntotalint = float(len(maskrfi.goodints)) * float(len(maskrfi.freqs))

# Parameters (we also have nobs and coords)

datemjd = [mjd.in_days()]
pulsar = sourceantenna[:len(sourceantenna) - 3]
antenna = [arch.get_receiver_name()]
bw = [arch.get_bandwidth()]