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
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
with open(f'Zap/zap_{file}.ascii', 'a+') as t: t.write( f'{math.floor(event.xdata)} {ar.freq[math.floor(event.xdata)][math.floor(event.ydata)]}\n' ) print("Subints / 2 = ", (ar.getNsubint() // 2) + 1) data = ar.getData() mask = np.zeros(ar.getNbin()) np.set_printoptions(threshold=sys.maxsize) mask[ar.opw] = 1 rms = np.array(calculate_rms_matrix(data, mask=mask)) rms_mean, rms_std = np.mean(rms), np.std(rms) data_lin = np.array([]) sub_pol = ar.getNsubint() * ar.getNpol() num_profs = sub_pol * ar.getNchan() #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)