Exemple #1
0
 def setUp(self):
     self.flux_model = katpoint.FluxDensityModel(
         '(1.0 2.0 2.0 0.0 0.0 0.0 0.0 0.0)')
     self.too_many_params = katpoint.FluxDensityModel(
         '(1.0 2.0 2.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0)')
     self.too_few_params = katpoint.FluxDensityModel('(1.0 2.0 2.0)')
     self.flux_target = katpoint.Target('radec, 0.0, 0.0, ' +
                                        self.flux_model.description)
     self.no_flux_target = katpoint.Target('radec, 0.0, 0.0')
Exemple #2
0
 def setUp(self):
     self.unit_model = katpoint.FluxDensityModel(100., 200., [0.])
     self.unit_model2 = katpoint.FluxDensityModel(100., 200., [0.])
     self.flux_model = katpoint.FluxDensityModel(
         '(1.0 2.0 2.0 0.0 0.0 0.0 0.0 0.0 2.0 0.5 0.25 -0.75)')
     with self.assertWarns(FutureWarning):
         self.too_many_params = katpoint.FluxDensityModel(
             '(1.0 2.0 2.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0)'
         )
     self.too_few_params = katpoint.FluxDensityModel('(1.0 2.0 2.0)')
     self.flux_target = katpoint.Target('radec, 0.0, 0.0, ' +
                                        self.flux_model.description)
     self.no_flux_target = katpoint.Target('radec, 0.0, 0.0')
Exemple #3
0
def get_atca_fcoeffs(source, freq, band, bandLimits):
    """Obtain flux density coefficients for source in ATCA calibator database."""
    headers[
        'Referer'] = 'http://www.narrabri.atnf.csiro.au/calibrators/calibrator_database_viewcal.html?source=' + str(
            source) + '&detailed=true'
    urlName = urllib2.quote(source.encode("utf8"))
    fluxInfo = None
    while fluxInfo is None:
        try:
            req = requests.post(url,
                                data='action=band_fluxdensity&source=' +
                                urlName + '&band=' + band,
                                headers=headers)
            fluxInfo = req.json()
        except:
            pass

    # convert to katpoint units (i.e. MHz)
    coeffs = np.float_(
        fluxInfo['fluxdensity_coefficients'][:-1])  # ATCA flux coeffs
    M = np.array([[1, -3, 9], [0, 1, -6], [0, 0, 1]],
                 dtype=float)  # conversion matrix
    #    M = np.array([[1,-3,9,-27], [0,1,-6,9], [0,0,1,-3],[0,0,0,1]], dtype=float) # conversion matrix
    padded_coeffs = np.zeros(3)
    padded_coeffs[:len(coeffs[:3])] = coeffs[:3]
    katCoeffs = np.dot(M, padded_coeffs)

    # obtain flux for given frequency and return valid params
    model = katpoint.FluxDensityModel(bandLimits[0], bandLimits[1], katCoeffs)
    flux = np.around(model.flux_density(freq / 1e6), 4)
    fluxErr = np.around(np.float_(fluxInfo['fluxdensity_scatter']), 4)
    coeffStr = str(bandLimits[0])+ ' ' +str(bandLimits[1])+ ' '\
        +' '.join(np.array(katCoeffs,dtype=str).tolist())
    return np.float_(
        fluxInfo['observation_mjd']), flux, '(' + coeffStr + ')', fluxErr
Exemple #4
0
 def test_flux_density(self):
     """Test flux density calculation."""
     unit_model = katpoint.FluxDensityModel(100., 200., [0.])
     self.assertEqual(unit_model.flux_density(110.), 1.0,
                      'Flux calculation wrong')
     self.assertRaises(ValueError, katpoint.FluxDensityModel, '1.0 2.0 2.0',
                       2.0, [2.0])
     self.assertRaises(ValueError, katpoint.FluxDensityModel, '1.0')
     self.assertEqual(self.flux_model.flux_density(1.5), 100.0,
                      'Flux calculation wrong')
     self.assertEqual(self.too_many_params.flux_density(1.5), 100.0,
                      'Flux calculation for too many params wrong')
     self.assertEqual(self.too_few_params.flux_density(1.5), 100.0,
                      'Flux calculation for too few params wrong')
     np.testing.assert_equal(
         self.flux_model.flux_density([1.5, 1.5]), np.array([100.0, 100.0]),
         'Flux calculation for multiple frequencies wrong')
     np.testing.assert_equal(
         self.flux_model.flux_density([0.5,
                                       2.5]), np.array([np.nan, np.nan]),
         'Flux calculation for out-of-range frequencies wrong')
     self.assertRaises(ValueError, self.no_flux_target.flux_density)
     np.testing.assert_equal(
         self.no_flux_target.flux_density([1.5, 1.5]),
         np.array([np.nan, np.nan]),
         'Empty flux model leads to wrong empty flux shape')
     self.flux_target.flux_freq_MHz = 1.5
     self.assertEqual(self.flux_target.flux_density(), 100.0,
                      'Flux calculation for default freq wrong')
     print self.flux_target
Exemple #5
0
 def test_cc_fitting(self):
     """Check CC fitting with increasing order and conversion to katpoint models
     """
     input_freqs = np.linspace(100.e6, 500.e6, 10)
     lnunu0 = np.log(input_freqs / input_freqs[5])
     input_cc_tab = np.ones((4, 10), dtype=np.float)
     input_cc_tab[0] = obit_flux_model(lnunu0, 10.)
     input_cc_tab[1] = obit_flux_model(lnunu0, -10., -0.7)
     input_cc_tab[2] = obit_flux_model(lnunu0, 2., 0.1, 0.01)
     input_cc_tab[3] = obit_flux_model(lnunu0, -0.5, -0.5, 0.01, 0.05)
     input_sigma = np.ones(10, dtype=np.float) * 0.1
     for order in range(4):
         cc_tab = input_cc_tab[order]
         kp_model = fit_flux_model(input_freqs, cc_tab, input_freqs[0],
                                   input_sigma, cc_tab[0], order=order)
         # Check the flux densities of the input and fitted models
         kp_flux_model = katpoint.FluxDensityModel(100., 500., kp_model)
         np.testing.assert_allclose(kp_flux_model.flux_density(input_freqs/1.e6), cc_tab)
     # Check a target with zero flux
     cc_tab = np.zeros(10)
     kp_model = fit_flux_model(input_freqs, cc_tab, input_freqs[0],
                               input_sigma, cc_tab[0], order=0)
     kp_flux_model = katpoint.FluxDensityModel(100., 500., kp_model)
     np.testing.assert_array_equal(kp_flux_model.flux_density(input_freqs/1.e6), cc_tab)
Exemple #6
0
    anomalous_flux, anomalous_bin = np.nan, -1
    if src['Jname'] in anomalies:
        anomalous_bin = anomalies[src['Jname']]
        anomalous_flux = flux[anomalous_bin]
        flux[anomalous_bin] = np.nan
    # Fit Baars 1977 polynomial flux model: log10 S[Jy] = a + b*log10(f[MHz]) + c*(log10(f[MHz]))^2
    flux_defined = ~np.isnan(flux)
    log_freq = np.log10(freq[flux_defined])
    log_flux = np.log10(flux[flux_defined])
    flux_poly = np.polyfit(log_freq, log_flux, 2 if len(log_flux) > 3 else 1 if len(log_flux) > 1 else 0)
    # Determine widest possible frequency range where flux is defined (ignore internal gaps in this range)
    defined_bins = flux_defined.nonzero()[0]
    freq_range = [start[defined_bins[0]], start[defined_bins[-1] + 1]]
    # For better or worse, extend range to at least KAT7 frequency band
    freq_range = [min(freq_range[0], 1000.0), max(freq_range[1], 2000.0)]
    flux_str = katpoint.FluxDensityModel(freq_range[0], freq_range[1], flux_poly[::-1]).description
    src_strings.append(', '.join((names, tags, ra, dec, flux_str)) + '\n')
    print(src_strings[-1].strip())

    # Display flux polynomial fits
    test_log_freq = np.linspace(np.log10(start[0]), np.log10(start[-1]), 200)
    test_log_flux = np.polyval(flux_poly, test_log_freq)
    plt.subplot(plot_rows, plot_rows, n + 1)
    plt.plot(log_freq, log_flux, 'ob')
    plt.plot(test_log_freq, test_log_flux, 'r')
    if not np.isnan(anomalous_flux):
        plt.plot(np.log10(freq[anomalous_bin]), np.log10(anomalous_flux), '*b')
    plt.xticks([])
    plt.yticks([])
    plt.axvspan(np.log10(freq_range[0]), np.log10(freq_range[1]), facecolor='g', alpha=0.5)
Exemple #7
0
# Iterate through sources
for src in table:
    names = '1Jy ' + src['_1Jy']
    if len(src['_3C']) > 0:
        names += ' | *' + src['_3C']
    ra, dec = katpoint.deg2rad(src['_RAJ2000']), katpoint.deg2rad(src['_DEJ2000'])
    tags_ra_dec = katpoint.construct_radec_target(ra, dec).add_tags('J2000').description
    # Extract flux data for the current source from flux table
    flux = flux_table[flux_table['_1Jy'] == src['_1Jy']]
    # Determine widest possible frequency range where flux is defined (ignore internal gaps in this range)
    # For better or worse, extend range to at least KAT7 frequency band (also handles empty frequency lists)
    flux_freqs = flux['Freq'].tolist() + [800.0, 2400.0]
    min_freq, max_freq = min(flux_freqs), max(flux_freqs)
    log_freq, log_flux = np.log10(flux['Freq']), np.log10(flux['S'])
    if src['Fct'] == 'LIN':
        flux_str = katpoint.FluxDensityModel(min_freq, max_freq, [src['A'], src['B']]).description
    elif src['Fct'] == 'EXP':
        flux_str = katpoint.FluxDensityModel(min_freq, max_freq, [src['A'], src['B'],
                                                                  0.0, 0.0, src['C'], src['D']]).description
    else:
        # No flux data found for source - skip it (only two sources, 1334-127 and 2342+82, are discarded)
        if len(flux) == 0:
            continue
        # Fit straight-line flux model log10(S) = a + b*log10(v) to frequencies close to KAT7 band
        mid_freqs = (flux['Freq'] > 400) & (flux['Freq'] < 12000)
        flux_poly = np.polyfit(log_freq[mid_freqs], log_flux[mid_freqs], 1)
        flux_str = katpoint.FluxDensityModel(min_freq, max_freq, flux_poly[::-1]).description
    src_strings.append(', '.join((names, tags_ra_dec, flux_str)) + '\n')
    print(src_strings[-1].strip())

    # Display flux model fit
    if 'ASSOC' in psr:
        snr_match = snr.match(psr['ASSOC'])
        if snr_match:
            names += ' | ' + snr_match.group(2)
    if 'RAJ' in psr:
        ra, dec = psr['RAJ'], psr['DECJ']
    else:
        raise RuntimeError(
            "Please run 'psrcat -db_file psrcat.db -e2 > psrcat_full.db' to get radecs for each pulsar"
        )

    # Extract all flux measurements of pulsar
    flux_matches = [flux_bin.match(k) for k in psr]
    freq = np.array([float(m.group(1)) for m in flux_matches if m])
    flux_mJy = np.array([float(psr[m.group(0)]) for m in flux_matches if m])
    if len(freq) == 0:
        flux_model = None
    else:
        # Fit Baars 1977 polynomial flux model: log10 S[Jy] = a + b*log10(f[MHz]) + c*(log10(f[MHz]))^2
        log_freq = np.log10(freq)
        log_flux = np.log10(flux_mJy / 1000.)
        order = 2 if len(log_flux) > 3 else 1 if len(log_flux) > 1 else 0
        flux_poly = np.polyfit(log_freq, log_flux, order)
        freq_range = [1000., 2000.]
        flux_model = katpoint.FluxDensityModel(freq_range[0], freq_range[1],
                                               flux_poly[::-1])
    description = '%s, radec psr, %s, %s' % (names, ra, dec)
    if flux_model:
        description += ', ' + flux_model.description
    print description