Пример #1
0
def test_get_pixbeam():
    """Test get_pixbeam"""
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)

    beam = helper.get_pixbeam_pixel(0, 0)
    verify_beam(beam)

    helper.lat = None
    beam = helper.get_beam(285, -66)
    verify_beam(beam)

    helper.lat = -65
    beam = helper.get_beam(285, -66)
    verify_beam(beam)

    area = helper.get_beamarea_pix(285, -66)
    if not (area > 0): raise AssertionError()
    area = helper.get_beamarea_deg2(285, -66)
    if not (area >0): raise AssertionError()

    beam = helper.get_pixbeam(285, -66)
    verify_beam(beam)

    beam = helper.get_pixbeam(None, None)
    verify_beam(beam)
Пример #2
0
def test_ellipse_round_trip():
    """
    Converting an ellipse from pixel to sky coords and back again should give the
    original ellipse (within some tolerance).
    """
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)
    a = 2 * helper.beam.a
    b = helper.beam.b
    pa = helper.beam.pa + 45
    ralist = list(range(-180, 180, 10))
    # SIN projection isn't valid for all decs
    declist = list(range(-85, -10, 10))
    ras, decs = np.meshgrid(ralist, declist)
    for _, (ra, dec) in enumerate(zip(ras.ravel(), decs.ravel())):
        if ra < 0:
            ra += 360
        x, y, sx, sy, theta = helper.sky2pix_ellipse((ra, dec), a, b, pa)
        ra_f, dec_f, major, minor, pa_f = helper.pix2sky_ellipse((x, y), sx,
                                                                 sy, theta)
        assert_almost_equal(ra, ra_f)
        assert_almost_equal(dec, dec_f)
        if not (abs(a - major) / a < 0.05): raise AssertionError()
        if not (abs(b - minor) / b < 0.05): raise AssertionError()
        if not (abs(pa - pa_f) < 1): raise AssertionError()
def main():
    # TODO:
    # make the noise vary as as if there are a bunch of tessellated observations
    # Each obs should have a different noise contribution
    # print "Creating noise with imprinted variation"
    print "creating rms map"
    noise_map = fits.open('bane_smooth.fits')
    noise_map[0].data = np.float32(0.1*(1+2*noise_map[0].data))
    noise_map.writeto('rms.fits', overwrite=True)
    print "Creating noise map"
    # gaussian noise with sigma = 1
    np.random.seed(69084124)
    noise = np.random.normal(0, 1, noise_map[0].data.shape)

    print "imprinting pattern onto noise"
    # rescale the noise so that some parts of the map have 3x the noise of others
    # and imprint a pattern into the noise
    noise_map[0].data *= noise
    noise_map.writeto('temp.fits', overwrite=True)
    print "wrote temp.fits"

    print "Convolving with variable psf"
    wcsh = WCSHelper.from_file('bane_smooth.fits')
    psfh = PSFHelper('psf.fits', wcsh)
    smoothed = convolve(noise, psfh)

    print "writing file"
    noise_map[0].data = np.float32(smoothed)
    noise_map.writeto('noise.fits', overwrite=True)
Пример #4
0
def test_psf_funcs():
    """
    For a SIN projected image, test that the beam/psf calculations are consistent at different points on the sky
    """
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)
    header = helper.wcs.to_header()

    refpix = header['CRPIX2'], header['CRPIX1']  # note x/y swap
    refcoord = header['CRVAL1'], header['CRVAL2']
    beam_ref_coord = helper.get_skybeam(*refcoord)
    beam_ref_pix = helper.get_skybeam(*helper.pix2sky(refpix))

    msg = 'sky beams at ref coord/pix disagree'
    if not abs(beam_ref_coord.a - beam_ref_pix.a) < 1e-5:
        raise AssertionError(msg)
    if not abs(beam_ref_coord.b - beam_ref_pix.b) < 1e-5:
        raise AssertionError(msg)
    if not abs(beam_ref_coord.pa - beam_ref_pix.pa) < 1e-5:
        raise AssertionError(msg)

    psf_zero = helper.get_psf_sky2pix(0, 0)
    psf_refcoord = helper.get_psf_sky2pix(*refcoord)

    diff = np.subtract(psf_zero, psf_refcoord)
    if not np.all(diff < 1e-5):
        raise AssertionError("psf varies in pixel coordinates (it should not)")

    diff = np.subtract(psf_refcoord,
                       (helper._psf_a, helper._psf_b, helper._psf_theta))
    if not np.all(diff < 1e-5):
        raise AssertionError("psf varies in pixel coordinates (it should not)")
    return
Пример #5
0
def test_get_pixbeam():
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)

    beam = helper.get_pixbeam_pixel(0, 0)
    verify_beam(beam)

    helper.lat = None
    beam = helper.get_beam(285, -66)
    verify_beam(beam)

    helper.lat = -65
    beam = helper.get_beam(285, -66)
    verify_beam(beam)

    area = helper.get_beamarea_pix(285, -66)
    if not (area > 0): raise AssertionError()
    area = helper.get_beamarea_deg2(285, -66)
    if not (area > 0): raise AssertionError()

    beam = helper.get_pixbeam(285, -66)
    verify_beam(beam)

    beam = helper.get_pixbeam(None, None)
    verify_beam(beam)
Пример #6
0
def test_psf_files():
    """Test that using external psf files works properly"""

    fname = 'tests/test_files/1904-66_SIN.fits'
    psfname = 'tests/test_files/1904-66_SIN_psf.fits'
    helper = WCSHelper.from_file(fname, psf_file=psfname)

    # The psf image has only 19x19 pix and this ra/dec is
    a, b, pa = helper.get_psf_sky2sky(290., -63.)
    if not np.all(np.isfinite((a, b, pa))):
        raise AssertionError("Beam contains nans")
Пример #7
0
def test_vector_round_trip():
    """
    Converting a vector from pixel to sky coords and back again should give the
    original vector (within some tolerance).
    """
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)
    initial = [1, 45]  # r,theta = 1,45 (degrees)
    ref = helper.refpix
    ra, dec, dist, ang = helper.pix2sky_vec(ref, *initial)
    _, _ , r, theta = helper.sky2pix_vec([ra, dec], dist, ang)
    if not ((abs(r - initial[0]) < 1e-9) and (abs(theta - initial[1]) < 1e-9)): raise AssertionError()
Пример #8
0
def test_vector_round_trip():
    """
    Converting a vector from pixel to sky coords and back again should give the
    original vector (within some tolerance).
    """
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)
    initial = [1, 45]  # r,theta = 1,45 (degrees)
    ref = helper.refpix
    ra, dec, dist, ang = helper.pix2sky_vec(ref, *initial)
    _, _, r, theta = helper.sky2pix_vec((ra, dec), dist, ang)
    if not ((abs(r - initial[0]) < 1e-9) and (abs(theta - initial[1]) < 1e-9)):
        raise AssertionError()
Пример #9
0
def test_get_pixbeam():
    """Test get_pixbeam"""
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)

    beam = Beam(*helper.get_psf_pix2pix(0, 0))
    verify_beam(beam)

    beam = helper.get_skybeam(285, -66)
    verify_beam(beam)

    beam = helper.get_skybeam(285, -66)
    verify_beam(beam)

    area = helper.get_beamarea_pix(285, -66)
    if not (area > 0): raise AssertionError()
    area = helper.get_beamarea_deg2(285, -66)
    if not (area > 0): raise AssertionError()
Пример #10
0
def test_ellipse_round_trip():
    """
    Converting an ellipse from pixel to sky coords and back again should give the
    original ellipse (within some tolerance).
    """
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)
    a = 2 * helper.beam.a
    b = helper.beam.b
    pa = helper.beam.pa + 45
    ralist = list(range(-180, 180, 10))
    # SIN projection isn't valid for all decs
    declist = list(range(-85, -10, 10))
    ras, decs = np.meshgrid(ralist, declist)
    for _, (ra, dec) in enumerate(zip(ras.ravel(), decs.ravel())):
        if ra < 0:
            ra += 360
        x, y, sx, sy, theta = helper.sky2pix_ellipse([ra, dec], a, b, pa)
        ra_f, dec_f, major, minor, pa_f = helper.pix2sky_ellipse([x, y], sx, sy, theta)
        assert_almost_equal(ra, ra_f)
        assert_almost_equal(dec, dec_f)
        if not (abs(a-major)/a < 0.05): raise AssertionError()
        if not (abs(b-minor)/b < 0.05): raise AssertionError()
        if not (abs(pa-pa_f) < 1): raise AssertionError()
Пример #11
0
def test_estimate_parinfo_image():
    """Test"""
    log = logging.getLogger("Aegean")
    #log.setLevel(logging.DEBUG)

    wcshelper = WCSHelper.from_file(
        filename='tests/test_files/1904-66_SIN.fits')

    im = np.zeros(shape=(10, 10), dtype=np.float32) * np.nan
    bkg = np.zeros_like(im)
    rms = np.ones_like(im)

    im[2:5, 2:5] = 6.
    im[3, 3] = 8.

    islands = sf.find_islands(im, bkg, rms, log=log)
    sources = sf.estimate_parinfo_image(islands,
                                        im=im,
                                        rms=rms,
                                        wcshelper=wcshelper,
                                        log=log)

    if len(sources) != 1:
        raise AssertionError(
            "Incorrect number of sources found {0}, expecting 1".format(
                len(sources)))
    if not sources[0]['components'].value == 1:
        raise AssertionError("Found {0} components, expecting 1".format(
            sources[0]['components'].value))
    if not sources[0]['c0_amp'].value == 8.0:
        raise AssertionError("c0_amp is not 8.0 (is {0})".format(
            sources[0]['c0_amp'].value))

    # test on a negative island
    im *= -1.
    islands = sf.find_islands(im, bkg, rms, log=log)
    sources = sf.estimate_parinfo_image(islands,
                                        im=im,
                                        rms=rms,
                                        wcshelper=wcshelper,
                                        log=log)

    if len(sources) != 1:
        raise AssertionError(
            "Incorrect number of sources found {0}, expecting 1".format(
                len(sources)))
    if not sources[0]['components'].value == 1:
        raise AssertionError("Found {0} components, expecting 1".format(
            sources[0]['components'].value))
    if not sources[0]['c0_amp'].value == -8.0:
        raise AssertionError("c0_amp is not -8.0 (is {0})".format(
            sources[0]['c0_amp'].value))

    # test on a small island
    im[:, :] = np.nan
    im[2:4, 2:4] = 6.
    im[3, 3] = 8.

    islands = sf.find_islands(im, bkg, rms, log=log)
    sources = sf.estimate_parinfo_image(islands,
                                        im=im,
                                        rms=rms,
                                        wcshelper=wcshelper,
                                        log=log)
    if len(sources) != 1:
        raise AssertionError(
            "Incorrect number of sources found {0}, expecting 1".format(
                len(sources)))
    if not sources[0]['components'].value == 1:
        raise AssertionError("Found {0} components, expecting 1".format(
            sources[0]['components'].value))
    if not (sources[0]['c0_flags'].value & flags.FIXED2PSF):
        raise AssertionError("FIXED2PSF flag not detected")
Пример #12
0
print(fmt % ("OuterClip", str(outerclip)))
print(fmt % ("Cores", str(cores)))
print(fmt % ("catalog file", catalog_file))
print(fmt % ("catalog file [point]", catalog_file_point))
print(fmt % ("plot file", plot_file))
print(fmt % ("cutx0", cutx0))
print(fmt % ("cutx1", cutx1))
print(fmt % ("cuty0", cuty0))
print(fmt % ("cuty1", cuty1))
print(fmt % ("fluxmin", fluxmin))

hdu = fits.open(fits_file)[0]
fits_data = hdu.data
fits_header = hdu.header

mywcs_helper = WCSHelper.from_file(fits_file)

log = logging.getLogger(bname)
sf = SourceFinder(log=log)
found = sf.find_sources_in_image( filename = fits_file,\
                                  innerclip = innerclip,\
                                  outerclip = outerclip,\
                                  cores = 5,\
                                  nonegative=False \
                                  #nonegative=True \
                                  )
save_catalog(catalog_file, found)

shape = fits_data.shape
m, n = shape
m0 = int(m * cuty0)
Пример #13
0
def test_sky_sep():
    """Test sky separation"""
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)
    dist = helper.sky_sep((0, 0), (1, 1))
    if not (dist > 0): raise AssertionError()
Пример #14
0
def test_from_file():
    """Test that we can load from a file"""
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)
    if helper.beam is None: raise AssertionError()
Пример #15
0
def test_from_file():
    """Test that we can load from a file"""
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)
    if helper.beam is None: raise AssertionError()
Пример #16
0
def test_sky_sep():
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)
    dist = helper.sky_sep([0, 0], [1, 1])
    if not (dist > 0): raise AssertionError()
Пример #17
0
def test_from_file():
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)
    if helper.beam is None: raise AssertionError()
Пример #18
0
def test_sky_sep():
    """Test sky separation"""
    fname = 'tests/test_files/1904-66_SIN.fits'
    helper = WCSHelper.from_file(fname)
    dist = helper.sky_sep([0, 0], [1, 1])
    if not (dist > 0): raise AssertionError()