Beispiel #1
0
def dont_test_load_save_fits_tables():
    # The version of astropy on travis breaks on this!
    # probably a bug that will be fixed by astropy later.
    catalog = [OutputSource()]
    cat.save_catalog('a.fits', catalog, meta=None)
    if not os.path.exists('a_comp.fits'):
        raise AssertionError()

    os.remove('a_comp.fits')
Beispiel #2
0
def test_load_save_catalog():
    """Test that we can load and save various file formats"""
    catalog = [OutputSource()]
    for ext in ['csv', 'vot']:
        fout = 'a.'+ext
        cat.save_catalog(fout, catalog, meta=None)
        fout = 'a_comp.'+ext
        if not os.path.exists(fout):
            raise AssertionError()

        catin = cat.load_catalog(fout)
        if not len(catin) == len(catalog):
            raise AssertionError()

        os.remove(fout)

    # test the prefix is being written.
    fout = 'a.csv'
    cat.save_catalog(fout, catalog, meta=None, prefix='test')
    fout = 'a_comp.csv'
    if not os.path.exists(fout):
        raise AssertionError()
    if 'test_ra' not in open(fout).readlines()[0]:
        raise AssertionError()
    os.remove(fout)

    for ext in ['reg', 'ann', 'bla']:
        fout = 'a.'+ext
        cat.save_catalog(fout, catalog, meta=None)
        fout = 'a_comp.'+ext
        if not os.path.exists(fout):
            raise AssertionError()

        os.remove(fout)

    fout = 'a.db'
    cat.save_catalog(fout, catalog, meta=None)
    if not os.path.exists(fout):
        raise AssertionError()

    # again so that we trigger an overwrite
    cat.save_catalog(fout, catalog, meta=None)
    os.remove(fout)

    badfile = open("file.fox", 'w')
    print("ff 012.d", file=badfile)
    badfile.close()
    assert_raises(Exception, cat.load_catalog, 'file.fox')
    badfile = open("file.fox", 'w')
    print('1 1', file=badfile)
    badfile.close()
    catin = cat.load_catalog('file.fox')
    print(catin)
    if not len(catin) == 1:
        raise AssertionError()

    os.remove('file.fox')
Beispiel #3
0
def test_load_save_catalog():
    """Test that we can load and save various file formats"""
    catalog = [ComponentSource()]
    for ext in ['csv', 'vot']:
        fout = 'a.' + ext
        cat.save_catalog(fout, catalog, meta=None)
        fout = 'a_comp.' + ext
        if not os.path.exists(fout):
            raise AssertionError()

        catin = cat.load_catalog(fout)
        if not len(catin) == len(catalog):
            raise AssertionError()

        os.remove(fout)

    # test the prefix is being written.
    fout = 'a.csv'
    cat.save_catalog(fout, catalog, meta=None, prefix='test')
    fout = 'a_comp.csv'
    if not os.path.exists(fout):
        raise AssertionError()
    if 'test_ra' not in open(fout).readlines()[0]:
        raise AssertionError()
    os.remove(fout)

    for ext in ['reg', 'ann', 'bla']:
        fout = 'a.' + ext
        cat.save_catalog(fout, catalog, meta=None)
        fout = 'a_comp.' + ext
        if not os.path.exists(fout):
            raise AssertionError()

        os.remove(fout)

    fout = 'a.db'
    cat.save_catalog(fout, catalog, meta=None)
    if not os.path.exists(fout):
        raise AssertionError()

    # again so that we trigger an overwrite
    cat.save_catalog(fout, catalog, meta=None)
    os.remove(fout)

    badfile = open("file.fox", 'w')
    print("ff 012.d", file=badfile)
    badfile.close()
    assert_raises(Exception, cat.load_catalog, 'file.fox')
    badfile = open("file.fox", 'w')
    print('1 1', file=badfile)
    badfile.close()
    catin = cat.load_catalog('file.fox')
    print(catin)
    if not len(catin) == 1:
        raise AssertionError()

    os.remove('file.fox')
Beispiel #4
0
def dont_test_load_save_fits_tables():
    """Test that we can load and save fits tables"""
    # The version of astropy on travis breaks on this!
    # probably a bug that will be fixed by astropy later.
    catalog = [OutputSource()]
    cat.save_catalog('a.fits', catalog, meta=None)
    if not os.path.exists('a_comp.fits'):
        raise AssertionError()

    os.remove('a_comp.fits')
def objdet(tele, lat, out_file0, fits_file, seed, flood, tab_file,
           catalog_input_name, cellSize_string):
    sources = []

    # get latitude for telescope

    # adding SMA and NOEMA to list
    if tele == 'SMA':
        lat = 19.8243
    elif tele == 'NOEMA':
        lat = 44.6339
    else:
        lat = scope2lat(tele)

    out_file = open(out_file0, 'w')

    sf = SourceFinder()

    print 'Running Aegean Object Detection -->'
    detections = sf.find_sources_in_image(fits_file,
                                          outfile=out_file,
                                          hdu_index=0,
                                          rms=None,
                                          max_summits=None,
                                          innerclip=seed,
                                          outerclip=flood,
                                          cores=cpu_count(),
                                          rmsin=None,
                                          bkgin=None,
                                          beam=None,
                                          doislandflux=False,
                                          nonegative=not False,
                                          nopositive=False,
                                          mask=None,
                                          lat=lat,
                                          imgpsf=None)
    out_file.flush()
    out_file.close()
    if len(detections) == 0:
        raise Exception(
            'No sources detected by Aegean. Please check your inputs.')
    sources.extend(detections)
    # write detected source info to file
    if len(sources) > 0:
        save_catalog(catalog_input_name, sources)

    # print detected sources fro user to choose from
    src_l, ra_l, dec_l, maj_l, min_l, pos_l = run_aegean(
        tab_file, cellSize_string)
    print 'Number of Objects Detected is ', len(src_l)
    print 'Objects Detected-->'
    print 'Object, RA, DEC'
    for i in range(0, len(src_l)):
        print src_l[i], ra_l[i], dec_l[i]
    return (src_l, ra_l, dec_l, maj_l, min_l, pos_l)
def objdet(tele,lat,out_file0,fits_file,seed,flood,tab_file,catalog_input_name,cellSize_string):
  sources = []

  # get latitude for telescope

  # adding SMA and NOEMA to list
  if tele == 'SMA':
    lat = 19.8243
  elif tele == 'NOEMA':
    lat = 44.6339
  else:
    lat = scope2lat(tele)


  out_file = open(out_file0, 'w')

  sf = SourceFinder()

  print 'Running Aegean Object Detection -->'
  detections = sf.find_sources_in_image(fits_file,
                                        outfile=out_file,
                                        hdu_index=0,
                                        rms=None,
                                        max_summits=None,
                                        innerclip=seed,
                                        outerclip=flood,
                                        cores=cpu_count(),
                                        rmsin=None,
                                        bkgin=None, beam=None,
                                        doislandflux=False,
                                        nonegative=not False,
                                        nopositive=False,
                                        mask=None, lat=lat, imgpsf=None)
  out_file.flush()
  out_file.close()
  if len(detections) == 0:
    raise Exception('No sources detected by Aegean. Please check your inputs.')
  sources.extend(detections)
  # write detected source info to file
  if len(sources) > 0:
    save_catalog(catalog_input_name, sources)

  # print detected sources fro user to choose from
  src_l, ra_l, dec_l, maj_l, min_l, pos_l = run_aegean(tab_file, cellSize_string)
  print 'Number of Objects Detected is ', len(src_l)
  print 'Objects Detected-->'
  print 'Object, RA, DEC'
  for i in range(0, len(src_l)):
    print src_l[i], ra_l[i], dec_l[i]
  return(src_l, ra_l, dec_l, maj_l, min_l, pos_l)
def aegean_format(catalogue, out):
    """
    Save a catalogue in a format that Aegean will recognize
    :param catalogue: Catalogue
    :param out: Output filename
    :return:
    """
    sources = []
    for i, c in enumerate(catalogue):
        src = SimpleSource()
        src.ra, src.dec, src.peak_flux = c
        src.uuid = "injected_{0:04d}".format(i)
        src.pa = 0
        src.a = src.b = 0.0326580516349937 * 3600
        sources.append(src)
    save_catalog(out, sources)
    print("Wrote to file {0}".format(out))
    return
Beispiel #8
0
def test_write_fits_table_variable_uuid_lengths():
    """Test that the length of the UUID column is appropriate"""
    catalog = []
    for l in range(10):
        c = ComponentSource()
        c.ra_str = c.dec_str = "hello!"
        c.uuid = 'source-{0:d}'.format(2**l)
        catalog.append(c)
    cat.save_catalog('a.fits', catalog, meta={'Purpose': 'Testing'})
    if not os.path.exists('a_comp.fits'):
        raise AssertionError()

    rcat = cat.load_table('a_comp.fits')
    for src1, src2 in zip(rcat, catalog):
        if len(src1['uuid']) != len(src2.uuid):
            print("len mismatch for source {0}".format(src1))
            print("uuid should be len={0}".format(len(src2.uuid)))
            raise AssertionError("UUID col is of wrong length")
    os.remove('a_comp.fits')
    return
Beispiel #9
0
def test_load_table_write_table():
    catalog = [OutputSource()]
    for fmt in ['csv', 'vo']:
        fout = 'a.'+fmt
        cat.save_catalog(fout, catalog, meta=None)
        fout = 'a_comp.'+fmt
        tab = cat.load_table(fout)
        if not len(tab) == len(catalog):
            raise AssertionError()

        os.remove(fout)

    cat.save_catalog('a.csv', catalog, meta=None)
    tab = cat.load_table('a_comp.csv')
    cat.write_table(tab, 'a.csv')
    if not os.path.exists('a.csv'):
        raise AssertionError()

    os.remove('a.csv')

    assert_raises(Exception, cat.write_table, tab, 'bla.fox')
    assert_raises(Exception, cat.load_table, 'file.fox')
Beispiel #10
0
def test_load_table_write_table():
    """Test that we can write and load tables with various file formats"""
    catalog = [OutputSource()]
    for fmt in ['csv', 'vo']:
        fout = 'a.'+fmt
        cat.save_catalog(fout, catalog, meta=None)
        fout = 'a_comp.'+fmt
        tab = cat.load_table(fout)
        if not len(tab) == len(catalog):
            raise AssertionError()

    # by keeping this out of the loop, we make use of the internal remove function
    os.remove(fout)

    cat.save_catalog('a.csv', catalog, meta=None)
    tab = cat.load_table('a_comp.csv')
    cat.write_table(tab, 'a.csv')
    if not os.path.exists('a.csv'):
        raise AssertionError()

    os.remove('a.csv')

    assert_raises(Exception, cat.write_table, tab, 'bla.fox')
    assert_raises(Exception, cat.load_table, 'file.fox')
Beispiel #11
0
def test_load_table_write_table():
    """Test that we can write and load tables with various file formats"""
    catalog = [ComponentSource()]
    for fmt in ['csv', 'vo']:
        fout = 'a.' + fmt
        cat.save_catalog(fout, catalog, meta=None)
        fout = 'a_comp.' + fmt
        tab = cat.load_table(fout)
        if not len(tab) == len(catalog):
            raise AssertionError()

    # by keeping this out of the loop, we make use of the internal remove function
    os.remove(fout)

    cat.save_catalog('a.csv', catalog, meta=None)
    tab = cat.load_table('a_comp.csv')
    cat.write_table(tab, 'a.csv')
    if not os.path.exists('a.csv'):
        raise AssertionError()

    os.remove('a.csv')

    assert_raises(Exception, cat.write_table, tab, 'bla.fox')
    assert_raises(Exception, cat.load_table, 'file.fox')
Beispiel #12
0
def test_load_save_catalog():
    catalog = [OutputSource()]
    for ext in ['csv', 'vot']:
        fout = 'a.'+ext
        cat.save_catalog(fout, catalog, meta=None)
        fout = 'a_comp.'+ext
        if not os.path.exists(fout):
            raise AssertionError()

        catin = cat.load_catalog(fout)
        if not len(catin) == len(catalog):
            raise AssertionError()

        os.remove(fout)

    for ext in ['reg', 'ann', 'bla']:
        fout = 'a.'+ext
        cat.save_catalog(fout, catalog, meta=None)
        fout = 'a_comp.'+ext
        if not os.path.exists(fout):
            raise AssertionError()

        os.remove(fout)

    fout = 'a.db'
    cat.save_catalog(fout, catalog, meta=None)
    if not os.path.exists(fout):
        raise AssertionError()

    os.remove(fout)

    badfile = open("file.fox", 'w')
    print("ff 012.d", file=badfile)
    badfile.close()
    assert_raises(Exception, cat.load_catalog, 'file.fox')
    badfile = open("file.fox", 'w')
    print('1 1', file=badfile)
    badfile.close()
    catin = cat.load_catalog('file.fox')
    print(catin)
    if not len(catin) == 1:
        raise AssertionError()

    os.remove('file.fox')
Beispiel #13
0
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)
m1 = int(m * cuty1)
n0 = int(n * cutx0)
n1 = int(n * cutx1)

img = make_model(sf.sources, shape, mywcs_helper)

fig, axs = plt.subplots(2, 2, figsize=(10, 10))
axs[0, 0].imshow(fits_data, norm=mplc.LogNorm(), cmap=cm.jet)
#axs[0,1].imshow( fits_data, norm=mplc.LogNorm(), cmap=cm.jet )
axs[0, 1].imshow(img, norm=mplc.LogNorm(), cmap=cm.jet)
axs[1,0].imshow( fits_data[m0:m1, n0:n1],\
                                          rms=None,
                                          max_summits=None,
                                          innerclip=seed,
                                          outerclip=flood,
                                          cores=cpu_count(),
                                          rmsin=None,
                                          bkgin=None,
                                          beam=None,
                                          doislandflux=False,
                                          nonegative=not False,
                                          nopositive=False,
                                          mask=None,
                                          lat=lat,
                                          imgpsf=None)
out_file.flush()
out_file.close()
if len(detections) == 0:
    raise Exception('No sources detected by Aegean. Please check your inputs.')
sources.extend(detections)
# write detected source info to file
if len(sources) > 0:
    save_catalog(catalog_input_name, sources)

# print detected sources fro user to choose from
src_l, ra_l, dec_l, maj_l, min_l, pos_l = run_aegean(tab_file, cellSize)
print 'Number of Objects Detected is ', len(src_l)
print 'Objects Detected-->'
print 'Object, RA, DEC'
for i in range(0, len(src_l)):
    print src_l[i], ra_l[i], dec_l[i]