示例#1
0
def read_spd_file(spdfn):
    """Return a SPCandidate object for the spd given.
        Input:
            spdfn: *.spd file name.
        Output:
            cand: Candidate object constructed from the given pfd
                file name.
    """
    #spd = sp_utils.spd(spdfn)
    spd = read_spd.spd(spdfn)
    cand = SPCandidate(spd.best_dm, spd.ra_deg, spd.dec_deg, spdfn)
    return cand
示例#2
0
    def _compute_data(self, cand):
        """Create an sp_utils.spd object for the candidate's 
            *.spd file

            Input:
                cand: A ratings2.0 SPCandidate object.

            Output:
                spd: The corresponding sp_utils.spd object.
        """
        spdfn = cand.info['spdfn']
        spd = read_spd.spd(spdfn)
        return spd
示例#3
0
    def _compute_data(self, cand):
        """Create an sp_utils.spd object for the candidate's 
            *.spd file

            Input:
                cand: A ratings2.0 SPCandidate object.

            Output:
                spd: The corresponding sp_utils.spd object.
        """
        spdfn = cand.info['spdfn']
        spd = read_spd.spd(spdfn)
        return spd
示例#4
0
def plot(spdfile, singlepulsefiles=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, 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
    start = topo_start - loc_pulse*duration
    datastart = spdobj.waterfall_start_time
    datasamp = spdobj.waterfall_tsamp
    datanumspectra = spdobj.waterfall_prededisp_nbins
    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(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+duration, 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(datanumspectra*loc_pulse/downsamp)
            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)
            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(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+duration, 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(datanumspectra*loc_pulse/downsamp)
            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)
            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(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+duration, 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(datanumspectra*loc_pulse/downsamp)
            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)
            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
        if integrate_ts:
            sp_pgplot.ppgplot.pgsvp(0.1, 0.7, 0.36, 0.44)
            sp_pgplot.ppgplot.pgswin(datastart - start, datastart-start+duration, 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(datanumspectra*loc_pulse/downsamp)
            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)
            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()
示例#5
0
def plot(spdfile, singlepulsefiles=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, 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
    start = topo_start - loc_pulse * duration
    datastart = spdobj.waterfall_start_time
    datasamp = spdobj.waterfall_tsamp
    datanumspectra = spdobj.waterfall_prededisp_nbins
    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(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 + duration,
                                     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(datanumspectra*loc_pulse/downsamp)
            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)
            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(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 + duration,
                                     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(datanumspectra*loc_pulse/downsamp)
            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)
            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(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 + duration,
                                     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(datanumspectra*loc_pulse/downsamp)
            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)
            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
        if integrate_ts:
            sp_pgplot.ppgplot.pgsvp(0.1, 0.7, 0.36, 0.44)
            sp_pgplot.ppgplot.pgswin(datastart - start,
                                     datastart - start + duration,
                                     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(datanumspectra*loc_pulse/downsamp)
            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)
            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()
示例#6
0
def GBNCC_wrapper(txtfile, maskfile, fitsfilenm, path_sp_files):
    """
	The pipeline should pass job.fits_filenm as argument.
    """
    files = spio.get_textfile(txtfile)
    min_rank = 3
    groups = [i for i in range(7) if (i >= min_rank)][::-1]
    numcands = 0  # counter for max number of candidates
    loop_must_break = False  # dont break the loop unless num of cands >100.

    values = []
    lis = []
    ranks = []
    for group in groups:
        rank = group + 1
        if files[group] != "Number of rank %i groups: 0 " % rank:
            add_values = spio.split_parameters(rank, txtfile)
            values.extend(add_values)
            lis = np.append(
                lis,
                np.where(files == '\tRank:             %i.000000' % rank)[0])
            add_ranks = np.ones(len(add_values)) * rank
            ranks = np.append(ranks, add_ranks)

    if len(values) > 100:
        values = values[0:100]
        lis = lis[0:100]

    #Sort candidates based on DM
    zip_list = zip(values, lis, ranks)
    zip_list = sorted(zip_list, key=itemgetter(0, 0))
    values = [x[0] for x in zip_list]
    lis = [x[1] for x in zip_list]
    ranks = [x[2] for x in zip_list]

    basename = fitsfilenm[:-5]
    #generate subbanded file at a DM of 0 to extract observation parameters
    cmd = "psrfits_subband -dm 0.0 -nsub 128 -o %s_subband_0.0 %s" % (
        basename, fitsfilenm)
    print "executing %s" % cmd
    subprocess.call(cmd, shell=True)
    subfilenm = basename + "_subband_0.0_0001.fits"
    subfile = psrfits.PsrfitsFile(subfilenm)

    for ii in range(len(values)):
        dm_list, time_list, dm_arr, sigma_arr, width_arr = spio.read_RRATrap_info(
            txtfile, lis[ii], int(ranks[ii]))
        wrapper_cand = spcand.params()
        wrapper_cand.read_from_file(values[ii], subfile.tsamp, subfile.specinfo.N, \
                                       get_obs_info(fitsfilenm)['hifreq'], get_obs_info(fitsfilenm)['lofreq'], \
                                       subfile, loc_pulse=0.5, dedisp = True,\
                                       scaleindep = None, zerodm = None, mask = None,\
                                       barytime=True, nsub = None, bandpass_corr = False)
        temp_filename = basename + "_rank_%i" % int(ranks[ii])
        if ii == 0:  #check if index is 0
            correct_rawdatafile, prevsubfile, prevDM  = pick_rawdatafile(wrapper_cand.subdm, fitsfilenm, \
                                                                get_obs_info(fitsfilenm)['hifreq'], get_obs_info(fitsfilenm)['lofreq'], \
                                                                wrapper_cand.pulse_width, prevDM = 0, prevsubfile = '', \
                                                                init_flag = 1)
        else:
            correct_rawdatafile, prevsubfile, prevDM  = pick_rawdatafile(wrapper_cand.subdm, fitsfilenm, \
                                                                get_obs_info(fitsfilenm)['hifreq'], get_obs_info(fitsfilenm)['lofreq'], \
                                                                wrapper_cand.pulse_width, prevDM = prevDM, \
                                                                prevsubfile=prevsubfile, init_flag = 0)

        path = os.path.dirname(os.path.abspath(__file__))
        cmd = "python " + path + "/make_spd.py --use_manual_params --subdm %f --nsub %d" \
                     " --dm %f -T %f -t %f --width-bins %d --downsamp %d --show-spec"\
                      " --noplot --notopo -o %s %s " %(wrapper_cand.subdm, \
                                                 wrapper_cand.nsub, wrapper_cand.dm, \
                                                 wrapper_cand.topo_start_time, wrapper_cand.duration, \
                                                 wrapper_cand.width_bins, wrapper_cand.downsamp,\
                                                 temp_filename, correct_rawdatafile)
        print "executing %s" % cmd
        subprocess.call(cmd, shell=True)
        # Add additional information to the header information array
        text_array = np.array([correct_rawdatafile, subfile.specinfo.telescope, \
                           subfile.specinfo.ra_str, subfile.specinfo.dec_str, \
                           subfile.specinfo.start_MJD[0], int(ranks[ii]), \
                           wrapper_cand.nsub, wrapper_cand.nbins, \
                           wrapper_cand.subdm, wrapper_cand.sigma, wrapper_cand.sample_number, \
                           wrapper_cand.duration, wrapper_cand.width_bins, wrapper_cand.pulse_width, \
                           subfile.tsamp, subfile.specinfo.T, wrapper_cand.topo_start_time])

        temp_filename += "_DM%.1f_%.1fs" % (wrapper_cand.subdm,
                                            wrapper_cand.topo_start_time)
        spd = read_spd.spd(temp_filename + '.spd')
        spd.man_params = None
        text_array = np.append(text_array, spd.waterfall_start_time)
        text_array = np.append(text_array, spd.waterfall_tsamp)
        text_array = np.append(text_array, spd.waterfall_prededisp_nbins)
        text_array = np.append(text_array, spd.min_freq)
        text_array = np.append(text_array, spd.max_freq)
        text_array = np.append(text_array, spd.sweep_duration)
        text_array = np.append(text_array, spd.sweep_start_time)
        text_array = np.append(text_array, spd.bary_pulse_peak_time)
        text_array = np.append(text_array, spd.man_params)

        with open(temp_filename + ".spd", 'wb') as f:
            np.savez_compressed(f, \
                                        Data_dedisp_nozerodm = spd.data_nozerodm_dedisp,\
                                        Data_dedisp_zerodm = spd.data_zerodm_dedisp,\
                                        Data_nozerodm = spd.data_nozerodm,\
                                        delays_nozerodm = spd.dmsweep_delays, \
                                        freqs_nozerodm = spd.dmsweep_freqs,\
                                        Data_zerodm = spd.data_zerodm, \
                                        dm_arr= map(np.float16, dm_arr),\
                                        sigma_arr = map(np.float16, sigma_arr), \
                                        width_arr =map(np.uint8, width_arr),\
                                        dm_list= map(np.float16, dm_list), \
                                        time_list = map(np.float16, time_list), \
                                        text_array = text_array)
        plot_spd.plot(temp_filename+".spd", glob.glob(path_sp_files+'/*.singlepulse'), maskfile, \
                              outfile=basename, just_waterfall=False, \
                              integrate_spec=True, integrate_ts=True, \
                              disp_pulse=False, bandpass_corr = True, tar = None)
        numcands += 1
        print 'Finished sp_candidate : %i' % numcands
        if numcands >= 100:  # Max number of candidates to plot 100.
            loop_must_break = True
            break
示例#7
0
def get_spcandidates(versionnum, directory, header_id=None, timestamp_mjd=None, inst_cache=None):
    """Return single pulse candidates to common DB.

        Inputs:
            versionnum: A combination of the githash values from 
                        PRESTO, the pipeline, and psrfits_utils.
            directory: The directory containing results from the pipeline.
            header_id: header_id number for this beam, as returned by
                        spHeaderLoader/header.upload_header
            timestamp_mjd: mjd timestamp for this observation (default=None).
            inst_cache: ratings2 RatingInstanceIDCache instance.

        Ouputs:
            sp_cands: List of single pulse candidates, plots and tarballs.
            tempdir: Path of temporary directory that SPDs have been untarred,
                     returned so that it can be deleted after successful SPD upload.
    """
    sp_cands = []

    # Create temporary directory
    N = 6
    prefix = "/localscratch/PALFA_spds_"
    suffix = "_tmp/"
    String = ''.join(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for _ in range(N))
    tempdir = prefix+String+suffix
    os.makedirs(tempdir)
    #tempdir = tempfile.mkdtemp(suffix="_tmp", prefix="PALFA_spds_")

    mjd = int(timestamp_mjd)
    remote_spd_base = os.path.join(config.upload.spd_ftp_dir,str(mjd)) 

    # extract spd tarball
    spd_tarfns = glob.glob(os.path.join(directory, "*_spd.tgz"))
    spd_tarball = SPDTarball(spd_tarfns[0],remote_spd_base,tempdir)
    spd_tempdir, spd_list = spd_tarball.extract()

    remote_spd_dir = os.path.join(remote_spd_base,\
                                  os.path.basename(spd_tarfns[0]).rstrip('_spd.tgz'))

    # extract ratings tarball 
    rating_tarfn = spd_tarfns[0].replace("_spd","_spd_rat")
    tar = tarfile.open(rating_tarfn)
    try:
        tar.extractall(path=tempdir)
    except IOError:
        if os.path.isdir(tempdir):
            shutil.rmtree(tempdir)
        raise SinglePulseCandidateError("Error while extracting pfd files " \
                                        "from tarball (%s)!" % tarfn)
    finally:
        tar.close()

    # Gather SP candidates and their rating and plots
    sp_cands.append(spd_tarball)
    for ii,spd_elem in enumerate(spd_list):
        spdfn, spd_size = spd_elem[0], spd_elem[1]
        pngfn = os.path.join(directory, \
                os.path.basename(spdfn.replace(".spd",".spd.png")))
        ratfn = os.path.join(tempdir, \
                os.path.basename(spdfn.replace(".spd",".spd.rat")))
        spd = read_spd.spd(str(spdfn))
        cand = SinglePulseCandidate(ii+1, spd, versionnum, header_id=header_id)
        cand.add_dependent(SinglePulseCandidatePNG(pngfn))
        cand.add_dependent(SinglePulseCandidateSPD(spdfn, spd_size, remote_spd_dir=remote_spd_dir))

        ratvals = ratings2.rating_value.read_file(ratfn)
        cand.add_dependent(SinglePulseCandidateRating(ratvals,inst_cache=inst_cache))

        sp_cands.append(cand)

    # Gather per-beam plots to upload
    fns = glob.glob(os.path.join(directory, "*DMs0-110_singlepulse.png"))
    if len(fns) != 1:
        raise SinglePulseCandidateError("Wrong number of *DMs0-110_singlepulse.png " \
                                        "plots found (%d)!" % len(fns))
    sp_cands.append(SinglePulseBeamPlotDMs0_110(fns[0], versionnum, \
                        header_id=header_id))

    fns = glob.glob(os.path.join(directory, "*DMs100-310_singlepulse.png"))
    if len(fns) != 1:
        raise SinglePulseCandidateError("Wrong number of *DMs100-310_singlepulse.png " \
                                        "plots found (%d)!" % len(fns))
    sp_cands.append(SinglePulseBeamPlotDMs100_310(fns[0], versionnum, \
                        header_id=header_id))

    fns = glob.glob(os.path.join(directory, "*DMs300-2000_singlepulse.png"))
    if len(fns) != 1:
        raise SinglePulseCandidateError("Wrong number of *DMs300-2000_singlepulse.png " \
                                        "plots found (%d)!" % len(fns))
    sp_cands.append(SinglePulseBeamPlotDMs300_2000(fns[0], versionnum, \
                        header_id=header_id))
    fns = glob.glob(os.path.join(directory, "*DMs1000-10000_singlepulse.png"))
    if len(fns) != 1:
        raise SinglePulseCandidateError("Wrong number of *DMs1000-10000_singlepulse.png " \
                                        "plots found (%d)!" % len(fns))
    sp_cands.append(SinglePulseBeamPlotDMs1000_10000(fns[0], versionnum, \
                        header_id=header_id))

    
    # Gather per-beam SP tarballs to upload
    fns = glob.glob(os.path.join(directory, "*_inf.tgz"))
    if len(fns) != 1:
        raise SinglePulseCandidateError("Wrong number of *_inf.tgz " \
                                        "tarballs found (%d)!" % len(fns))
    sp_cands.append(SinglePulseInfTarball(fns[0], versionnum, \
                        header_id=header_id, timestamp_mjd=timestamp_mjd))
    
    fns = glob.glob(os.path.join(directory, "*_singlepulse.tgz"))
    if len(fns) != 1:
        raise SinglePulseCandidateError("Wrong number of *_singlepulse.tgz " \
                                        "tarballs found (%d)!" % len(fns))
    sp_cands.append(SinglePulseCandsTarball(fns[0] , versionnum, \
                        header_id=header_id, timestamp_mjd=timestamp_mjd))

    return sp_cands, tempdir
def GBNCC_wrapper(txtfile, maskfile, fitsfilenm, path_sp_files):
    """
	The pipeline should pass job.fits_filenm as argument.
    """
    files = spio.get_textfile(txtfile)
    min_rank = 3
    groups = [i for i in range(7) if(i>=min_rank)][::-1]
    numcands=0 # counter for max number of candidates
    loop_must_break = False # dont break the loop unless num of cands >100.
    
    values = []
    lis = [] 
    ranks = []
    for group in groups:
        rank = group+1
        if files[group] != "Number of rank %i groups: 0 "%rank:
            add_values = spio.split_parameters(rank, txtfile) 
            values.extend(add_values)
            lis = np.append(lis, np.where(files == '\tRank:             %i.000000'%rank)[0])
            add_ranks = np.ones(len(add_values)) * rank
            ranks = np.append(ranks, add_ranks)
     
    if len(values) > 100:
        values = values[0:100]
        lis = lis[0:100]

    #Sort candidates based on DM
    zip_list = zip(values, lis, ranks)
    zip_list = sorted(zip_list, key=itemgetter(0,0))
    values = [x[0] for x in zip_list]
    lis = [x[1] for x in zip_list]
    ranks = [x[2] for x in zip_list]
    
    basename = fitsfilenm[:-5]
    #generate subbanded file at a DM of 0 to extract observation parameters
    cmd = "psrfits_subband -dm 0.0 -nsub 128 -o %s_subband_0.0 %s"%(basename,fitsfilenm)
    print "executing %s" %cmd
    subprocess.call(cmd, shell=True)
    subfilenm = basename + "_subband_0.0_0001.fits"
    subfile = psrfits.PsrfitsFile(subfilenm)
    
    for ii in range(len(values)):
        dm_list, time_list, dm_arr, sigma_arr, width_arr = spio.read_RRATrap_info(txtfile, lis[ii], int(ranks[ii]))
        wrapper_cand = spcand.params() 
        wrapper_cand.read_from_file(values[ii], subfile.tsamp, subfile.specinfo.N, \
                                       get_obs_info(fitsfilenm)['hifreq'], get_obs_info(fitsfilenm)['lofreq'], \
                                       subfile, loc_pulse=0.5, dedisp = True,\
                                       scaleindep = None, zerodm = None, mask = None,\
                                       barytime=True, nsub = None, bandpass_corr = False) 
        temp_filename = basename+"_rank_%i"%int(ranks[ii])
        if ii == 0: #check if index is 0
            correct_rawdatafile, prevsubfile, prevDM  = pick_rawdatafile(wrapper_cand.subdm, fitsfilenm, \
                                                                get_obs_info(fitsfilenm)['hifreq'], get_obs_info(fitsfilenm)['lofreq'], \
                                                                wrapper_cand.pulse_width, prevDM = 0, prevsubfile = '', \
                                                                init_flag = 1)
        else:
            correct_rawdatafile, prevsubfile, prevDM  = pick_rawdatafile(wrapper_cand.subdm, fitsfilenm, \
                                                                get_obs_info(fitsfilenm)['hifreq'], get_obs_info(fitsfilenm)['lofreq'], \
                                                                wrapper_cand.pulse_width, prevDM = prevDM, \
                                                                prevsubfile=prevsubfile, init_flag = 0)
            
        
        path = os.path.dirname(os.path.abspath(__file__))
        cmd = "python " + path + "/make_spd.py --use_manual_params --subdm %f --nsub %d" \
              	      " --dm %f -T %f -t %f --width-bins %d --downsamp %d --show-spec"\
                      " --noplot --notopo -o %s %s " %(wrapper_cand.subdm, \
                                                 wrapper_cand.nsub, wrapper_cand.dm, \
                                                 wrapper_cand.topo_start_time, wrapper_cand.duration, \
                                                 wrapper_cand.width_bins, wrapper_cand.downsamp,\
                                                 temp_filename, correct_rawdatafile)
        print "executing %s" %cmd
        subprocess.call(cmd, shell=True)       
        # Add additional information to the header information array
        text_array = np.array([correct_rawdatafile, subfile.specinfo.telescope, \
                           subfile.specinfo.ra_str, subfile.specinfo.dec_str, \
                           subfile.specinfo.start_MJD[0], int(ranks[ii]), \
                           wrapper_cand.nsub, wrapper_cand.nbins, \
                           wrapper_cand.subdm, wrapper_cand.sigma, wrapper_cand.sample_number, \
                           wrapper_cand.duration, wrapper_cand.width_bins, wrapper_cand.pulse_width, \
                           subfile.tsamp, subfile.specinfo.T, wrapper_cand.topo_start_time])
                
        temp_filename +="_DM%.1f_%.1fs"%(wrapper_cand.subdm, wrapper_cand.topo_start_time)
        spd = read_spd.spd(temp_filename+'.spd')
        spd.man_params = None 
        text_array = np.append(text_array, spd.waterfall_start_time)
        text_array = np.append(text_array, spd.waterfall_tsamp)
        text_array = np.append(text_array, spd.waterfall_prededisp_nbins)
        text_array = np.append(text_array, spd.min_freq)
        text_array = np.append(text_array, spd.max_freq)
        text_array = np.append(text_array, spd.sweep_duration)
        text_array = np.append(text_array, spd.sweep_start_time)
        text_array = np.append(text_array, spd.bary_pulse_peak_time)
        text_array = np.append(text_array, spd.man_params)

        with open(temp_filename+".spd", 'wb') as f:
            np.savez_compressed(f, \
                                        Data_dedisp_nozerodm = spd.data_nozerodm_dedisp,\
                                        Data_dedisp_zerodm = spd.data_zerodm_dedisp,\
                                        Data_nozerodm = spd.data_nozerodm,\
                                        delays_nozerodm = spd.dmsweep_delays, \
                                        freqs_nozerodm = spd.dmsweep_freqs,\
                                        Data_zerodm = spd.data_zerodm, \
                                        dm_arr= map(np.float16, dm_arr),\
                                        sigma_arr = map(np.float16, sigma_arr), \
                                        width_arr =map(np.uint8, width_arr),\
                                        dm_list= map(np.float16, dm_list), \
                                        time_list = map(np.float16, time_list), \
                                        text_array = text_array)
        plot_spd.plot(temp_filename+".spd", glob.glob(path_sp_files+'/*.singlepulse'), maskfile, \
                              outfile=basename, just_waterfall=False, \
                              integrate_spec=True, integrate_ts=True, \
                              disp_pulse=False, bandpass_corr = True, tar = None)
        numcands+= 1
        print 'Finished sp_candidate : %i'%numcands
        if numcands >= 100:    # Max number of candidates to plot 100.
            loop_must_break = True
            break