def prepare_file(self, file):
        """
        Prepares the PSRFITS file in the correct format for the program.
        """

        try:
            hdul = fits.open(file)
        except OSError:
            return -1

        name = hdul[0].header['SRC_NAME']
        fe = hdul[0].header['FRONTEND']
        if hdul[0].header[
                'OBS_MODE'] != "PSR" or name != self.psr_name or fe != self.frontend:
            hdul.close()
            return -1
        hdul.close()

        ar = Archive(file, verbose=self.verbose)
        ar.tscrunch(nsubint=1)
        ar.fscrunch(nchan=1)
        n = 1
        nbin = ar.getNbin()
        data = ar.getData()

        return np.copy(data), n, nbin
def load_archive(file, tscrunch=False):

    ar = Archive(file, verbose=False)
    if tscrunch:
        ar.tscrunch(nsubint=4)
        #ar.imshow()
    name = ar.getName()
    mjd = int(ar.getMJD())
    fe = ar.getFrontend()
    nbin = ar.getNbin()
    data = ar.getData().reshape((ar.getNchan() * ar.getNsubint(), nbin))

    return name, mjd, fe, nbin, data
Пример #3
0
    def prepare_file(self, file, do_fit=False):
        """
        Prepares the PSRFITS file in the correct format for the program.
        """

        try:
            hdul = fits.open(file)
        except OSError:
            return -1

        name = hdul[0].header['SRC_NAME']
        fe = hdul[0].header['FRONTEND']
        mjd = hdul[0].header['STT_IMJD']
        if hdul[0].header['OBS_MODE'] != "PSR" or name != self.psr_name:
            hdul.close()
            return -1
        hdul.close()

        tmp_fn = "{0}_{1}_nchan1_template.npy".format(self.psr_name, fe)
        try:
            template = self.load_template(self.temp_dir, tmp_fn)
        except TemplateLoadError:
            print("Template not found")
            reply = str(
                input("Would you like to make a suitable one? ('y' for yes)")
            ).lower().strip()
            if reply[0] == 'y':
                temp = FD_Template(self.psr_name,
                                   fe,
                                   1,
                                   template_dir="templates",
                                   verbose=self.verbose,
                                   *self.dirs)
                template = temp.make_template(gaussian_fit=do_fit)
            else:
                raise TemplateLoadError(
                    "You can make a suitable template via the following command: python template_builder.py psr_name -b [frontend] -d [dirs]"
                )

        ar = Archive(file, verbose=self.verbose)
        if self.epoch_average:
            ar.tscrunch(nsubint=1)

        return ar, template, fe, mjd
Пример #4
0
def get_Jy_per_count(dir, psr_cal_file, fitAA, fitBB):

    file = dir + psr_cal_file

    ar = Archive(file, verbose=False)
    rfi = RFIMitigator(ar)
    ar.tscrunch()
    s_duty = ar.getValue("CAL_PHS")
    duty = ar.getValue("CAL_DCYC")
    nchan = ar.getNchan()
    npol = ar.getNpol()
    nbin = ar.getNbin()
    BW = ar.getBandwidth()
    data = ar.getData()
    CTR_FREQ = ar.getCenterFrequency(weighted=True)

    converted_data = IQUV_to_AABB(data, basis="cartesian")

    frequencies = chan_to_freq(CTR_FREQ, BW, nchan)
    psr_cal, high_psr, low_psr = np.zeros((2, nchan, nbin)), np.zeros(
        (2, nchan)), np.zeros((2, nchan))
    for i in np.arange(2):
        for j in np.arange(nchan):
            psr_cal[i][j], high_psr[i][j], low_psr[i][j] = prepare_cal_profile(
                converted_data[0][i][j], s_duty, duty)

    # Calculate jy_per_count{p, f}
    jy_per_count_factor = np.zeros_like(high_psr)
    # for i in np.arange( 2 ):
    for j in np.arange(nchan):
        jy_per_count_factor[0][j] = fitAA(frequencies[j]) / (
            high_psr[0][j] - low_psr[0][j])  # A has units Jy / count

    for j in np.arange(nchan):
        jy_per_count_factor[1][j] = fitBB(frequencies[j]) / (
            high_psr[1][j] - low_psr[1][j])  # A has units Jy / count

    return jy_per_count_factor
Пример #5
0
def get_AABB_Fcal(dir, continuum_on, continuum_off, args, G=10.0, T0=1.0):

    ON, OFF = dir + continuum_on, dir + continuum_off

    if args.freq_zap is not None:
        for i, arg in enumerate(args.freq_zap):
            args.freq_zap[i] = int(args.freq_zap[i])

    ar_on, ar_off = Archive(ON, verbose=False), Archive(OFF, verbose=False)
    rfi_on, rfi_off = RFIMitigator(ar_on), RFIMitigator(ar_off)
    s_duty_on, s_duty_off = ar_on.getValue("CAL_PHS"), ar_off.getValue(
        "CAL_PHS")
    duty_on, duty_off = ar_on.getValue("CAL_DCYC"), ar_off.getValue("CAL_DCYC")
    nchan_on, nchan_off = ar_on.getNchan(), ar_off.getNchan()
    npol_on, npol_off = ar_on.getNpol(), ar_off.getNpol()
    nbin_on, nbin_off = ar_on.getNbin(), ar_off.getNbin()
    BW_on, BW_off = ar_on.getBandwidth(), ar_off.getBandwidth()
    CTR_FREQ_on, CTR_FREQ_off = ar_on.getCenterFrequency(
        weighted=True), ar_off.getCenterFrequency(weighted=True)
    ar_on.tscrunch()
    ar_off.tscrunch()

    if args.freq_zap is not None:
        if len(args.freq_zap) == 1:
            if args.channel_space:
                rfi_on.zap_channels(args.freq_zap)
                rfi_off.zap_channels(args.freq_zap)
            else:
                print(
                    "No zapping occurred (tried to zap channels in frequency space). Carrying on with calibration..."
                )
        elif len(args.freq_zap) == 2 and not args.channel_space:
            rfi_on.zap_frequency_range(args.freq_zap[0], args.freq_zap[1])
            rfi_off.zap_frequency_range(args.freq_zap[0], args.freq_zap[1])
        else:
            rfi_on.zap_channels(args.freq_zap)
            rfi_off.zap_channels(args.freq_zap)

    data_on, data_off = ar_on.getData(squeeze=True), ar_off.getData(
        squeeze=True)

    converted_data_on = IQUV_to_AABB(data_on, basis="cartesian")
    converted_data_off = IQUV_to_AABB(data_off, basis="cartesian")

    # Initialize the continuum data. SUBINT, POL,
    continuum_on_source, high_on_mean, low_on_mean = np.zeros(
        (2, nchan_on, nbin_on)), np.zeros((2, nchan_on)), np.zeros(
            (2, nchan_on))
    continuum_off_source, high_off_mean, low_off_mean = np.zeros(
        (2, nchan_off, nbin_off)), np.zeros((2, nchan_off)), np.zeros(
            (2, nchan_off))
    f_on, f_off, C0 = np.zeros_like(high_on_mean), np.zeros_like(
        high_off_mean), np.zeros_like(high_off_mean)
    T_sys = np.zeros_like(C0)
    F_cal = np.zeros_like(T_sys)

    # Load the continuum data
    for i in np.arange(2):
        for j in np.arange(nchan_on):

            continuum_on_source[i][j], high_on_mean[i][j], low_on_mean[i][
                j] = prepare_cal_profile(converted_data_on[0][i][j], s_duty_on,
                                         duty_on)
            continuum_off_source[i][j], high_off_mean[i][j], low_off_mean[i][
                j] = prepare_cal_profile(converted_data_off[0][i][j],
                                         s_duty_off, duty_off)

            f_on[i][j] = (high_on_mean[i][j] / low_on_mean[i][j]) - 1
            f_off[i][j] = (high_off_mean[i][j] / low_off_mean[i][j]) - 1

            if np.isnan(f_on[i][j]):
                f_on[i][j] = 1
            if np.isnan(f_on[i][j]):
                f_off[i][j] = 1

            C0[i][j] = T0 / ((1 / f_on[i][j]) - (1 / f_off[i][j]))
            T_sys[i][j] = C0[i][j] / f_off[i][j]
            F_cal[i][j] = (T_sys[i][j] *
                           f_off[i][j]) / G  # F_cal has units Jy / cal

            if np.isnan(F_cal[i][j]):
                F_cal[i][j] = 0

    frequencies_on_off = chan_to_freq(CTR_FREQ_on, BW_on, nchan_on)

    f1, f2 = interp1d(frequencies_on_off,
                      F_cal[0],
                      kind='cubic',
                      fill_value='extrapolate'), interp1d(
                          frequencies_on_off,
                          F_cal[1],
                          kind='cubic',
                          fill_value='extrapolate')

    return f1, f2
Пример #6
0
    #for i in np.arange( ar.getNsubint() ):
    #    data_lin = np.append( data_lin, data[ i, 324, : ] )
    #data = np.reshape( data, ( num_profs * ar.getNbin() ) )

    # D_FAC = 32
    # for i in range(D_FAC):
    #     st, ed = i*(chan // D_FAC), (i + 1)*(chan // D_FAC)
    #     fig = plt.figure( figsize = (7, 7) )
    #     ax = fig.add_subplot(111)
    #     cmap = plt.cm.Blues
    #     ax.imshow( rms.T[st:ed, :], cmap = cmap, interpolation = 'nearest', extent = [ 0, ch, ed, st ], aspect = 'auto', norm = clr.Normalize( vmin = 0, vmax = np.amax(rms) ) )
    #     fig.colorbar( plt.cm.ScalarMappable( norm = clr.Normalize( vmin = 0, vmax = np.amax(rms) ), cmap = cmap ), ax = ax )
    #     cid = fig.canvas.mpl_connect('key_press_event', on_key)
    #
    #     #fig.canvas.mpl_disconnect(cid)
    #     #ax.plot( np.linspace( 1, num_profs, num = sub_pol * ar.getNbin() ), data_lin, linewidth = 0.1, color = 'k' )
    #     #ax.set_ylim( -630, 630 )
    #     #for i in np.arange( 1, num_profs + 1 ):
    #     #    if (i % ar.getNsubint()) == 0:
    #     #        ax.axvline( i, linewidth = 0.2, color = 'r' )
    #     #ax.set_xlim( 2400, 2600 )
    #
    #     plt.show()
    #     fig.canvas.mpl_disconnect(cid)

    ar.tscrunch()
    ar.fscrunch()
    ar.plot()
    exit()
    ar.imshow(origin='upper')