Beispiel #1
0
def test_fil_to_fil(your_object):
    # from fil
    w = Writer(
        your_object,
        nstart=1,
        nsamp=2,
        outname="temp",
        c_min=10,
        c_max=200,
        outdir="./",
        flag_rfi=False,
        zero_dm_subt=False,
    )
    # test with outname
    w.to_fil()
    assert os.path.isfile("temp.fil")
    y = Your("temp.fil")
    assert y.your_header.nspectra == 2
    assert y.your_header.nchans == 190
    assert (y.get_data(0, 2) -
            your_object.get_data(1, 2)[:, 10:200]).sum() == 0
    os.remove("temp.fil")

    # test without outname
    w = Writer(your_object, nstart=0, nsamp=10, outdir="./", flag_rfi=True)
    w.to_fil()
    assert os.path.isfile("small_converted.fil")
    y = Your("small_converted.fil")
    assert y.your_header.nspectra == 10
    assert y.your_header.nchans == your_object.your_header.nchans
    os.remove("small_converted.fil")
Beispiel #2
0
def test_fil_to_fits():
    file = os.path.join(_install_dir, "data/small.fil")
    f = Your(file)
    w = Writer(
        f,
        outname="temp",
        outdir="./",
        flag_rfi=False,
        zero_dm_subt=False,
        nstart=1,
        nsamp=2,
        c_min=40,
        c_max=200,
    )
    # test with outname
    w.to_fits()
    assert os.path.isfile("temp.fits")
    y = Your("temp.fits")
    assert y.your_header.nspectra == 2
    assert y.your_header.nchans == 160
    assert (y.get_data(0, 2) - f.get_data(1, 2)[:, 40:200]).sum() == 0
    os.remove("temp.fits")

    # test without  outname
    w = Writer(f, outdir="./", flag_rfi=True, zero_dm_subt=True)
    w.to_fits()
    assert os.path.isfile("small_converted.fits")
    y = Your("small_converted.fits")
    assert y.your_header.nchans == f.your_header.nchans
    ns = y.your_header.nspectra
    os.remove("small_converted.fits")
Beispiel #3
0
def test_fits_to_fil():
    # from fits
    file = os.path.join(_install_dir, "data/small.fits")
    f = Your(file)
    w = Writer(f,
               nstart=0,
               nsamp=1,
               outname="temp",
               outdir="./",
               c_min=0,
               c_max=200)
    # test with outname
    w.to_fil()
    assert os.path.isfile("temp.fil")
    y = Your("temp.fil")
    assert y.your_header.nspectra == 1
    assert y.your_header.nchans == 200
    assert (y.get_data(0, 1) - f.get_data(0, 1)[:, 0:200]).sum() == 0
    os.remove("temp.fil")

    # test without outname
    w = Writer(f, outdir="./")
    w.to_fil()
    assert os.path.isfile("small_converted.fil")
    y = Your("small_converted.fil")
    assert y.your_header.nspectra == f.your_header.nspectra
    ns = y.your_header.nspectra
    assert y.your_header.nchans == f.your_header.nchans
    assert (y.get_data(0, ns) - f.get_data(0, ns)).sum() == 0
    os.remove("small_converted.fil")
Beispiel #4
0
def test_fil_to_fil():
    # from fil
    file = os.path.join(_install_dir, 'data/small.fil')
    f = Your(file)
    w = Writer(f)
    # test with outname
    w.to_fil(nstart=0,
             nsamp=2,
             outname='temp.fil',
             c=[0, 200],
             outdir='./',
             flag_rfi=True,
             zero_dm_subt=True)
    assert os.path.isfile('temp.fil')
    y = Your('temp.fil')
    assert y.your_header.nspectra == 2
    assert y.your_header.nchans == 200
    os.remove('temp.fil')

    # test without outname
    w.to_fil(nstart=0, nsamp=1, outdir='./')
    assert os.path.isfile('small_converted.fil')
    y = Your('small_converted.fil')
    assert y.your_header.nspectra == 1
    assert y.your_header.nchans == f.your_header.nchans
    os.remove('small_converted.fil')
Beispiel #5
0
def test_your_from_fits():
    fits_file = os.path.join(_install_dir, 'data/28.fits')
    fits_obj = Your(fits_file)
    assert fits_obj.nchans == 336

    fits_file = os.path.join(_install_dir, 'data/28.fits')
    fits_obj = Your([fits_file])
    assert fits_obj.nchans == 336
Beispiel #6
0
def test_your_from_fil():
    fil_file = os.path.join(_install_dir, 'data/28.fil')
    fil_obj = Your(fil_file)
    print(fil_obj)
    assert fil_obj.nchans == 336

    fil_file = os.path.join(_install_dir, 'data/28.fil')
    fil_obj = Your([fil_file])
    assert fil_obj.nchans == 336
Beispiel #7
0
def _main():
    from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
    parser = ArgumentParser(description='Script description', formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help='Be verbose')
    parser.add_argument('-o', '--output',type=str, default="_cutout",help='Output File Name')
    parser.add_argument('-d', '--outdir',type=str, default="./",help='Output Directory')
    parser.add_argument('-f','--fch1',type=int, default=0,help='split band start (freq)')
    parser.add_argument('-c','--nchan',type=int, default=336,help='number of channels')
    parser.add_argument('-x','--tstart',type=int, default=0,help='split band start (time)')
    parser.add_argument('-s','--samples',type=int, default=-1,help='number of samples -1 for entire length')
    parser.add_argument('-m', '--mode',type=int, default=0,help='Output File Format, 0 -- filterbank ; 1 -- fits')

    parser.add_argument(dest='files', nargs='+')
    parser.set_defaults(verbose=False)
    values = parser.parse_args()
    for fil in values.files:
        original_file=Your(fil)
        length=original_file.your_header.nspectra
        if values.samples==-1:
            cut_L=length
        else:
            cut_L=values.samples
        writer_object = Writer(
            original_file,
            nstart=values.tstart,
            nsamp=cut_L,
            c_min=values.fch1,
            c_max=values.fch1+values.nchan,
            outdir=values.outdir,
            outname=values.output)
        print(f"writing cutout from {fil} to new file")
        if values.mode == 0:
            writer_object.to_fil()
        elif values.mode == 1:
            writer_object.to_fits()
Beispiel #8
0
def read_header(f):
    y = Your(f)
    dic = vars(y.your_header)
    dic['tsamp'] = y.your_header.tsamp
    dic['nchans'] = y.your_header.nchans
    dic['foff'] = y.your_header.foff
    dic['nspectra'] = y.your_header.nspectra
    nice_print(dic)
Beispiel #9
0
def read_header(f):
    y = Your(f)
    dic = vars(y.your_header)
    dic["tsamp"] = y.your_header.tsamp
    dic["nchans"] = y.your_header.nchans
    dic["channel_bandwidth"] = y.your_header.foff
    dic["nspectra"] = y.your_header.nspectra
    nice_print(dic)
Beispiel #10
0
def test_fits_to_fits():
    file = os.path.join(_install_dir, 'data/small.fits')
    f = Your(file)
    w = Writer(f)
    # test with outname
    w.to_fits(outname='temp.fits', outdir='./', nstart=0, nsamp=2)
    assert os.path.isfile('temp.fits')
    y = Your('temp.fits')
    assert y.your_header.nspectra == 2
    assert y.your_header.nchans == f.your_header.nchans
    os.remove('temp.fits')

    # test without  outname
    w.to_fits(outdir='./', c=[0, 10])
    assert os.path.isfile('small_converted.fits')
    y = Your('small_converted.fits')
    assert y.your_header.nchans == 10
    os.remove('small_converted.fits')
Beispiel #11
0
def test_4pol_fits_to_fits():
    file = os.path.join(_install_dir, "data/test_4pol.fits")
    your_obj = Your(file)

    w = Writer(your_obj,
               nstart=1,
               nsamp=20,
               gulp=20,
               outname="temp_4pol",
               outdir="./",
               npoln=4)
    w.to_fits()

    y = Your("temp_4pol.fits")
    assert y.your_header.nspectra == 20
    assert y.your_header.nchans == your_obj.your_header.nchans
    assert (y.get_data(0, 5, npoln=4) -
            your_obj.get_data(1, 5, npoln=4)).sum() == 0
    os.remove("temp_4pol.fits")
Beispiel #12
0
def test_incorrect_sigmas(fil_file):
    your_object = Your(fil_file)
    data = your_object.get_data(0, 1024)
    with pytest.raises(ValueError):
        sk_sg_filter(data, your_object, -1, 1, 1)

    with pytest.raises(ValueError):
        sk_sg_filter(data, your_object, 3, 15, -1)

    with pytest.raises(ValueError):
        sk_sg_filter(data, your_object, 0, 15, 0)
Beispiel #13
0
def test_save_bandpass():
    fil_file = os.path.join(_install_dir, "data/28.fil")
    y = Your(fil_file)
    bandpass = y.bandpass()
    mask = np.zeros_like(bandpass, dtype=np.bool)
    mask[:10] = True
    save_bandpass(y, bandpass, outname="28_bp.png")
    assert os.path.isfile("28_bp.png")
    os.remove("28_bp.png")
    save_bandpass(y, bandpass, mask=mask)
    assert os.path.isfile("28_bandpass.png")
    os.remove("28_bandpass.png")
Beispiel #14
0
def read_header(f, no_table):
    """
    Makes your header for given files, hands header off to be nicely printed

    Inputs:
    --
    f -- files to create the your header

    no_table -- bool - if true, don't use rich.table
    """
    y = Your(f)
    dic = vars(y.your_header)
    dic["tsamp"] = y.your_header.tsamp
    dic["nchans"] = y.your_header.nchans
    dic["channel_bandwidth"] = y.your_header.foff
    dic["nspectra"] = y.your_header.nspectra
    if no_table:
        nice_print(dic)
    else:
        table_print(dic)
Beispiel #15
0
def test_4pol():
    file = os.path.join(_install_dir, "data/test_4pol.fits")
    y = Your(file)
    data = y.get_data(0, 100, npoln=1)
    assert data.shape == (100, 512)

    data = y.get_data(0, 100, npoln=4)
    assert data.shape == (100, 4, 512)

    data = y.get_data(0,
                      100,
                      npoln=4,
                      time_decimation_factor=2,
                      frequency_decimation_factor=2)
    assert data.shape == (50, 4, 256)

    with pytest.raises(ValueError):
        y.get_data(0, 100, npoln=3)

    with pytest.raises(ValueError):
        y.get_data(0, 100, pol=5)
Beispiel #16
0
def _main():
    from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
    parser = ArgumentParser(description='Script description',
                            formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument('-v',
                        '--verbose',
                        dest='verbose',
                        action='store_true',
                        help='Be verbose')
    parser.add_argument('-o',
                        '--output',
                        type=str,
                        default="test",
                        help='Output File Name')
    # parser.add_argument('--ra',type=float, default=51436.202,help='RA')
    # parser.add_argument('--dec',type=float, default=270330.24,help='Dec')
    # parser.add_argument('-c','--ch1',type=int, default=0,help='starting channel')
    # parser.add_argument('-n','--nchans',type=int, default=8192,help='total channels')
    # parser.add_argument('-b','--bin',type=int, default=1,help='tscrunch samples per bin, must be dividend of subintegration?')
    # parser.add_argument('--fbin',type=int, default=1,help='fscrunch samples per bin')
    # parser.add_argument('-N','--segments',type=int, default=20,help='how many files per filterbank segment')
    parser.add_argument(dest='files', nargs='+')
    parser.set_defaults(verbose=False)
    values = parser.parse_args()
    for fil in values.files:
        original_file = Your(fil)
        length = original_file.your_header.nspectra
        writer_object = Writer(
            original_file,
            nstart=0,
            nsamp=length,
            # c_min=0,
            # c_max=100,
            outdir=".",
            outname=values.output)
        writer_object.to_fits()
Beispiel #17
0
def bandpass(f, time=0, nspectra=8192, outname=None):
    """
    Plots and saves the bandpass
    
    Args:
        f: File to read

        time: Time in seconds to read for bandpass

        nspectra: Number of spectra to read for bandpass
    """

    y = Your(f)
    if time > 0:
        ns = time // y.your_header.native_tsamp
    else:
        ns = nspectra
    bandpass = y.bandpass(nspectra=ns)
    save_bandpass(y,
                  bandpass,
                  chan_nos=None,
                  mask=None,
                  outdir=None,
                  outname=outname)
Beispiel #18
0
def test_unsupported_file():
    with pytest.raises(TypeError):
        Your(os.path.join(_install_dir, "data/some.blah"))

    with pytest.raises(TypeError):
        Your([os.path.join(_install_dir, "data/some.blah")])
Beispiel #19
0
def test_paths():
    with pytest.raises(ValueError):
        Your(bytearray("data/28.fil", "utf-8"))
Beispiel #20
0
def test_your_header(fits_file):
    fits_obj = Your(fits_file)
    print(fits_obj.your_header)
    assert True
Beispiel #21
0
def your_object():
    file = os.path.join(_install_dir, "data/small.fil")
    return Your(file)
Beispiel #22
0
def test_repr(fil_file):
    y = Your(fil_file)
    assert repr(y) == f"Using <class 'str'>:\n{fil_file}"
Beispiel #23
0
                        '--sigma',
                        help='Sigma for the savgol filter',
                        required=False,
                        default=[6],
                        type=float,
                        nargs='+')
    parser.add_argument('-o',
                        '--output_dir',
                        help='Output dir for heimdall candidates',
                        type=str,
                        required=False,
                        default='.')

    args = parser.parse_args()

    your_object = Your(file=args.files)

    if args.apply_savgol:
        bandpass = your_object.bandpass(nspectra=8192)
        chan_nos = np.arange(0, bandpass.shape[0], dtype=np.int)
        for fw, sig in zip(args.filter_window, args.sigma):
            mask = savgol_filter(bandpass,
                                 your_object.your_header.foff,
                                 fw=fw,
                                 sig=sig)
            basename = f'{args.output_dir}/{your_object.your_header.basename}_w{fw}_sig{sig}'
            save_bandpass(your_object,
                          bandpass,
                          chan_nos=chan_nos,
                          mask=mask,
                          outdir=args.output_dir + '/',
Beispiel #24
0
    log_filename = values.outdir + '/' + datetime.utcnow().strftime(
        'writer_%Y_%m_%d_%H_%M_%S_%f.log')

    if values.verbose:
        logging.basicConfig(filename=log_filename,
                            level=logging.DEBUG,
                            format=logging_format)
    else:
        logging.basicConfig(filename=log_filename,
                            level=logging.INFO,
                            format=logging_format)

    logging.info("Input Arguments:-")
    for arg, value in sorted(vars(values).items()):
        logging.info("Argument %s: %r", arg, value)
    y = Your(values.files)
    w = Writer(y)

    if values.type == 'fits':
        w.to_fits(c=values.chans,
                  nstart=values.nstart,
                  nsamp=values.nsamp,
                  outdir=values.outdir,
                  outname=values.out_name,
                  progress=values.no_progress,
                  flag_rfi=values.flag_rfi,
                  sk_sig=values.sk_sig,
                  sg_fw=values.sg_fw,
                  sg_sig=values.sg_sig,
                  zero_dm_subt=values.zero_dm_subt)
    elif values.type == 'fil':
Beispiel #25
0
def test_save_bandpass():
    fil_file = os.path.join(_install_dir, 'data/28.fil')
    y = Your(fil_file)
    save_bandpass(y, y.bandpass(), outname='28_bp.png')
    assert os.path.isfile('28_bp.png')
    os.remove('28_bp.png')
Beispiel #26
0
def test_save_bandpass():
    fil_file = os.path.join(_install_dir, "data/28.fil")
    y = Your(fil_file)
    save_bandpass(y, y.bandpass(), outname="28_bp.png")
    assert os.path.isfile("28_bp.png")
    os.remove("28_bp.png")
Beispiel #27
0
def test_your_no_file():
    try:
        f = Your([])
    except ValueError:
        assert True
Beispiel #28
0
def test_your_header():
    fits_file = os.path.join(_install_dir, 'data/28.fits')
    fits_obj = Your(fits_file)
    print(fits_obj.your_header)
    assert True
Beispiel #29
0
                level=logging.DEBUG,
                format=logging_format,
                handlers=[RichHandler(rich_tracebacks=True)],
            )
        else:
            logging.basicConfig(
                level=logging.INFO,
                format=logging_format,
                handlers=[RichHandler(rich_tracebacks=True)],
            )

    logging.info("Input Arguments:-")
    for arg, value in sorted(vars(values).items()):
        logging.info("%s: %r", arg, value)

    your_object = Your(file=values.files)
    max_delay = your_object.dispersion_delay(dms=np.max(values.dm))
    dispersion_delay_samples = np.ceil(max_delay /
                                       your_object.your_header.tsamp)
    logging.info(f"Max Dispersion delay = {max_delay} s")
    logging.info(f"Max Dispersion delay = {dispersion_delay_samples} samples")

    if your_object.your_header.nspectra < 2**18:
        nsamps_gulp = your_object.your_header.nspectra
    else:
        nsamps_gulp = int(
            np.max([(2**np.ceil(np.log2(dispersion_delay_samples))), 2**18]))

    if values.channel_end > 0:
        c_max = values.channel_end
    else:
Beispiel #30
0
    def load_file(self,
                  file_name=[""],
                  start_samp=0,
                  gulp_size=1024,
                  chan_std=False):
        """
        Loads data from a file:

        Inputs:
        file_name -- name or list of files to load, if none given user must use gui to give file
        start_samp -- sample number where to start show the file, defaults to the beginning of the file
        gulp_size -- amount of data to show at a given time
        """
        self.start_samp = start_samp
        self.gulp_size = gulp_size
        self.chan_std = chan_std

        if len(file_name) == 0:
            file_name = filedialog.askopenfilename(
                filetypes=(("fits/fil files", "*.fil *.fits"), ("All files",
                                                                "*.*")))

        logging.info(f"Reading file {file_name}.")
        self.your_obj = Your(file_name)
        self.master.title(self.your_obj.your_header.basename)
        logging.info(f"Printing Header parameters")
        self.get_header()
        if self.dm != 0:
            self.dispersion_delays = calc_dispersion_delays(
                self.dm, self.your_obj.chan_freqs)
            max_delay = np.max(np.abs(self.dispersion_delays))
            if max_delay > self.gulp_size * self.your_obj.your_header.native_tsamp:
                logging.warning(
                    f"Maximum dispersion delay for DM ({self.dm}) = {max_delay:.2f}s is greater than "
                    f"the input gulp size {self.gulp_size*self.your_obj.your_header.native_tsamp}s. Pulses may not be "
                    f"dedispersed completely.")
                logging.warning(
                    f"Use gulp size of {int(max_delay//self.your_obj.your_header.native_tsamp):0d} to "
                    f"dedisperse completely.")
        self.read_data()

        # create three plots, for ax1=time_series, ax2=dynamic spectra, ax4=bandpass
        self.gs = gridspec.GridSpec(2,
                                    2,
                                    width_ratios=[4, 1],
                                    height_ratios=[1, 4],
                                    wspace=0.02,
                                    hspace=0.03)
        ax1 = plt.subplot(self.gs[0, 0])
        ax2 = plt.subplot(self.gs[1, 0])
        ax3 = plt.subplot(self.gs[0, 1])
        ax4 = plt.subplot(self.gs[1, 1])
        ax3.axis("off")
        ax1.set_xticks([])
        ax4.set_yticks([])

        # get the min and max image values to that we can see the typical values well
        self.vmax = min(np.max(self.data),
                        np.median(self.data) + 5 * np.std(self.data))
        self.vmin = max(np.min(self.data),
                        np.median(self.data) - 5 * np.std(self.data))
        self.im_ft = ax2.imshow(self.data,
                                aspect="auto",
                                vmin=self.vmin,
                                vmax=self.vmax)

        # make bandpass
        bp_std = np.std(self.data, axis=1)
        bp_y = np.linspace(self.your_obj.your_header.nchans, 0,
                           len(self.bandpass))
        (self.im_bandpass, ) = ax4.plot(self.bandpass, bp_y, label="Bandpass")
        if self.chan_std:
            self.im_bp_fill = ax4.fill_betweenx(
                x1=self.bandpass - bp_std,
                x2=self.bandpass + bp_std,
                y=bp_y,
                interpolate=False,
                alpha=0.25,
                color="r",
                label="1 STD",
            )
            ax4.legend()
        ax4.set_ylim([-1, len(self.bandpass) + 1])
        ax4.set_xlabel("Avg. Arb. Flux")

        # make time series
        ax4.set_xlabel("Avg. Arb. Flux")
        (self.im_time, ) = ax1.plot(self.time_series)
        ax1.set_xlim(-1, len(self.time_series + 1))
        ax1.set_ylabel("Avg. Arb. Flux")

        plt.colorbar(self.im_ft, orientation="vertical", pad=0.01, aspect=30)

        ax = self.im_ft.axes
        ax.set_xlabel("Time [sec]")
        ax.set_ylabel("Frequency [MHz]")
        ax.set_yticks(np.linspace(0, self.your_obj.your_header.nchans, 8))
        yticks = [
            str(int(j)) for j in np.linspace(self.your_obj.chan_freqs[0],
                                             self.your_obj.chan_freqs[-1], 8)
        ]
        ax.set_yticklabels(yticks)
        self.set_x_axis()

        # a tk.DrawingArea
        self.canvas = FigureCanvasTkAgg(self.im_ft.figure, master=root)
        self.canvas.draw()
        self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)