def test_mask_to_flags(): obs = '1061313128_99bl_1pol_half_time' testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs) file_type = 'uvfits' ss = SS() ss.read(testfile) ins = INS(ss) freq_inds_1 = np.arange(0, len(ins.freq_array), 2) freq_inds_2 = np.arange(1, len(ins.freq_array), 2) ins.metric_array[1, freq_inds_1] = np.ma.masked ins.metric_array[3, freq_inds_1] = np.ma.masked ins.metric_array[7, freq_inds_2] = np.ma.masked ins.metric_array[-2, freq_inds_2] = np.ma.masked flags = ins.mask_to_flags() test_flags = np.zeros(flags.shape, dtype=bool) test_flags[1:5, freq_inds_1] = True test_flags[7, freq_inds_2] = True test_flags[8, freq_inds_2] = True test_flags[-3:-1, freq_inds_2] = True assert np.all( flags == test_flags), "Test flags were not equal to calculated flags."
def test_write_mwaf(): from astropy.io import fits obs = '1061313128_99bl_1pol_half_time_SSINS' testfile = os.path.join(DATA_PATH, '%s.h5' % obs) prefix = os.path.join(DATA_PATH, '%s_test' % obs) ins = INS(testfile) mwaf_files = [os.path.join(DATA_PATH, '1061313128_12.mwaf')] bad_mwaf_files = [os.path.join(DATA_PATH, 'bad_file_path')] # Compatible shape with mwaf file ins.metric_array = np.ma.ones([55, 384, 1]) ins.metric_array[50, 16 * 11: 16 * (11 + 1)] = np.ma.masked # metadata from the input file, hardcoded for testing purposes time_div = 4 freq_div = 2 NCHANS = 32 boxint = 11 Nbls = 8256 NSCANS = 224 flags = ins.mask_to_flags() # Repeat in time time_rep_flags = np.repeat(flags, time_div, axis=0) # Repeat in freq freq_time_rep_flags = np.repeat(time_rep_flags, freq_div, axis=1) # Repeat in bls freq_time_bls_rep_flags = np.repeat(freq_time_rep_flags[:, np.newaxis, NCHANS * boxint: NCHANS * (boxint + 1)], Nbls, axis=1) # This shape is on MWA wiki. Reshape to this shape. new_flags = freq_time_bls_rep_flags.reshape((NSCANS * Nbls, NCHANS)) # Test some defensive errors with pytest.raises(IOError): ins.write(prefix, output_type='mwaf', mwaf_files=bad_mwaf_files) with pytest.raises(ValueError): ins.write(prefix, output_type='mwaf', mwaf_files=mwaf_files, mwaf_method='bad_method') with pytest.raises(ValueError): ins.write(prefix, output_type='mwaf', mwaf_files=None) ins.write('%s_add' % prefix, output_type='mwaf', mwaf_files=mwaf_files) ins.write('%s_replace' % prefix, output_type='mwaf', mwaf_files=mwaf_files, mwaf_method='replace') with fits.open(mwaf_files[0]) as old_mwaf_hdu: with fits.open('%s_add_12.mwaf' % prefix) as add_mwaf_hdu: assert np.all(add_mwaf_hdu[1].data['FLAGS'] == old_mwaf_hdu[1].data['FLAGS'] + new_flags) with fits.open('%s_replace_12.mwaf' % prefix) as replace_mwaf_hdu: assert np.all(replace_mwaf_hdu[1].data['FLAGS'] == new_flags) for path in ['%s_add_12.mwaf' % prefix, '%s_replace_12.mwaf' % prefix]: os.remove(path)
def test_write_mwaf(): from astropy.io import fits obs = '1061313128_99bl_1pol_half_time_SSINS' testfile = os.path.join(DATA_PATH, '%s.h5' % obs) prefix = os.path.join(DATA_PATH, '%s_test' % obs) ins = INS(testfile) mwaf_files = [os.path.join(DATA_PATH, '1061313128_12.mwaf')] bad_mwaf_files = [os.path.join(DATA_PATH, 'bad_file_path')] # shape of that mwaf file ins.metric_array = np.ma.ones([223, 768, 1]) ins.metric_array[100, 32 * 11:32 * (11 + 1)] = np.ma.masked flags = ins.mask_to_flags() new_flags = np.repeat(flags[:, np.newaxis, 32 * 11:32 * (11 + 1)], 8256, axis=1).reshape((224 * 8256, 32)) # Test some defensive errors with pytest.raises(IOError): ins.write(prefix, output_type='mwaf', mwaf_files=bad_mwaf_files) with pytest.raises(ValueError): ins.write(prefix, output_type='mwaf', mwaf_files=mwaf_files, mwaf_method='bad_method') with pytest.raises(ValueError): ins.write(prefix, output_type='mwaf', mwaf_files=None) ins.write('%s_add' % prefix, output_type='mwaf', mwaf_files=mwaf_files) ins.write('%s_replace' % prefix, output_type='mwaf', mwaf_files=mwaf_files, mwaf_method='replace') with fits.open(mwaf_files[0]) as old_mwaf_hdu: with fits.open('%s_add_12.mwaf' % prefix) as add_mwaf_hdu: assert np.all(add_mwaf_hdu[1].data['FLAGS'] == old_mwaf_hdu[1].data['FLAGS'] + new_flags) with fits.open('%s_replace_12.mwaf' % prefix) as replace_mwaf_hdu: assert np.all(replace_mwaf_hdu[1].data['FLAGS'] == new_flags) for path in ['%s_add_12.mwaf' % prefix, '%s_replace_12.mwaf' % prefix]: os.remove(path)
xlabel='Frequency (Mhz)', ylabel='Time (UTC)') # Try to save memory - hope for garbage collector del ss # Set up MF flagging for routine shapes shape_dict = {'TV6': [1.74e8, 1.81e8], 'TV7': [1.81e8, 1.88e8], 'TV8': [1.88e8, 1.95e8], 'TV9': [1.95e8, 2.02e8]} sig_thresh = {shape: 5 for shape in shape_dict} sig_thresh['narrow'] = 5 sig_thresh['streak'] = 8 mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict, N_samp_thresh=len(ins.time_array) // 2) mf.apply_match_test(ins, apply_samp_thresh=False) mf.apply_samp_thresh_test(ins, event_record=True) Catalog_Plot.INS_plot(ins, '%s_flagged' % prefix, xticks=xticks, yticks=yticks, xticklabels=xticklabels, yticklabels=yticklabels, data_cmap=cm.plasma, ms_vmin=-5, ms_vmax=5, title=args.obsid, xlabel='Frequency (Mhz)', ylabel='Time (UTC)') ins.write(prefix, output_type='mask') uvd = UVData() uvd.read(args.uvd, phase_to_pointing_center=True, correct_cable_len=True) uvf = UVFlag(uvd, mode='flag', waterfall=True) uvf.flag_array = ins.mask_to_flags() utils.apply_uvflag(uvd, uvf, inplace=True) if np.any(uvd.nsample_array == 0): uvd.nsample_array[uvd.nsample_array == 0] = args.nsample_default uvd.write_uvfits('%s/%s.uvfits' % (args.outdir, args.obsid), spoof_nonessential=True)
uv.select(ant_str='crosses') shapes = ['TV4', 'TV5', 'TV6'] shape_dict = { 'TV4': [1.74e8, 1.82e8], 'TV5': [1.82e8, 1.9e8], 'TV6': [1.9e8, 1.98e8] } mf = MF(ins.freq_array, 5, shape_dict=shape_dict, streak=False, narrow=False) for event in ins.match_events: if event[-2] in shapes: ins.metric_array[event[:2]] = np.ma.masked ins_flags = ins.mask_to_flags() for uvd in [auto_uv, uv]: uvf = UVFlag(uvd, mode='flag', waterfall=True) uvf.flag_array = ins_flags uvf.to_baseline(uvd) uvd.flag_array = uvf.flag_array stat_dict = { 'TV4': { 'occ': 0, 'autopow': 0, 'crosspow': 0 }, 'TV5': { 'occ': 0, 'autopow': 0,