Exemple #1
0
def nearest_neighbour(tile, outdir, checkplots=False, saveplots=False):
    """

    """
    from astropy.table import Table, hstack
    import srpylib
    import match_lists
    import matplotlib.pyplot as plt
    import numpy as np

    db, host, user, password, db_table = rd_config_wsdb(table='wise')

    t = Table.read(outdir + tile + "_WISEfp_DEScat.fits")

    ra_min, ra_max, dec_min, dec_max = srpylib.min_max_match(
        t["RA_CALC_G"], t["DEC_CALC_G"], 1.0, DES_box=True
    )

    query_wise = "select ra, dec FROM allwise.main WHERE ra > " + \
        ra_min + " and ra < " + ra_max + " and dec > " + dec_min + \
        " and dec < " + dec_max
    RA, DEC = sqlutil.get(query_wise, db=db, host=host,
                          user=user, password=password)

    dists, inds = match_lists.match_lists(
        t["RA_CALC_G"].data, t["DEC_CALC_G"].data, RA, DEC, 1.0, 2
    )

    match_distance = dists[:,1]*3600.0

    if checkplots:
        plt.axvline(6.1)
        plt.axvline(6.4)
        plt.axvline(6.5)
        plt.axvline(12.0)
        plt.hist(match_distance, bins=100)
        plt.xlabel('Match Distance(")')
        plt.ylabel("Number")
        plt.title("Nearest Neighbours:" + tile)
        plt.show()

    print("Median Distance:", np.median(match_distance))
Exemple #2
0
def get_coords(infile=None, coord_file=None, tilename=None):
    """
    # Take DES tile, find all WISE objects on that tile.
    # infile = "/data/desardata/Y1A1/" + tile + "/" + tile + "_z.fits.fz"
    # DATAPATH = '/data/desardata/Y1A1/'

    """
    import math

    logger = logging.getLogger()

    outpath = os.path.dirname(coord_file)
    if not os.path.exists(outpath):
        try:
            os.makedirs(outpath)
        except Exception as error:
            logger.exception(error)
            print("Unexpected error:", sys.exc_info()[0])
            traceback.print_exc(file=sys.stdout)
            raise

    logger.debug('Reading tile image to get corners: %s', infile)
    with fits.open(infile) as fhlist:
        hdr = fhlist[1].header
        w = wcs.WCS(hdr, naxis=2)

    # If this is ever used for non square inmages this needs checking
    pix_corners = [[0, 0], [0, hdr["NAXIS1"]], [
        hdr["NAXIS2"], 0], [hdr["NAXIS1"], hdr["NAXIS2"]]]
    w_corners = w.wcs_pix2world(pix_corners, 1)

    [corner_ra, corner_dec] = zip(*w_corners)

    """
    corner_ra = sorted(corner_ra)
    corner_dec = sorted(corner_dec)

    #Take the slightly smaller square inside the not square image
    ra_min = corner_ra[1]
    ra_max = corner_ra[2]
    dec_min = corner_dec[1]
    dec_max = corner_dec[2]
    """

    delta_ra = max(corner_ra) - min(corner_ra)
    delta_dec = max(corner_dec) - min(corner_dec)

    dec_mean = (min(corner_dec) + min(corner_dec)) / 2.0
    delta_ra = delta_ra * math.cos(math.radians(dec_mean))

    logger.debug('dec_mean: %s', dec_mean)
    logger.debug('delta_ra, delta_dec: %s; %s', delta_ra, delta_dec)

    # raw_input("Enter any key to continue: ")
    # Some of these objects won't be on the DES tile so will return infs
    ra_min = str(min(corner_ra))
    ra_max = str(max(corner_ra))
    dec_min = str(min(corner_dec))
    dec_max = str(max(corner_dec))

    query_wise = "select ra, dec FROM allwise.main WHERE ra > " + \
        ra_min + \
        " and ra < " + ra_max + " and dec > " + \
        dec_min + " and dec < " + dec_max

    # special case where crossing  24hrs ra > ra_max or ra < ra_min
    if delta_ra > 180.0:

        query_wise = "select ra, dec FROM allwise.main WHERE (ra > " + \
            ra_max + \
            " or ra < " + ra_min + ") and dec > " + \
            dec_min + " and dec < " + dec_max

    db, host, user, password, db_table = rd_config_wsdb(table='wise')

    RA_wise, DEC_wise = sqlutil.get(
        query_wise, db=db, host=host,
        user=user, password=password
    )
    logger.info('%s: Number of wise sources: %s', tilename, len(RA_wise))

    info = ""
    for (ra, dec) in zip(RA_wise, DEC_wise):
        info += (str(ra) + " " + str(dec) + "\n")

    with open(coord_file, "w") as f:
        logger.debug('Writing coords: %s', coord_file)
        try:
            f.write(info)
            logger.debug('Successfully written coordinate file: %s', coord_file)
        except IOError as e:
            print("I/O error({0}): {1}".format(e.errno, e.strerror))
        except:
            print("Unexpected error:", sys.exc_info()[0])
            raise

        # except IOError as (errno, strerror):
        #    print("I/O error({0}): {1}".format(errno, strerror))
        #    print('Failed to write coordinate file: ' + str(e))
        #    logger.error('Failed to write coordinate file: ' + str(e))
        #    sys.exit()
        #    pass

        # except OSError as (errno, strerror):
        #    print("Hello World:OS error({0}): {1}".format(errno, strerror))
        #    pass

        # except:
        #    print("Unexpected error:", sys.exc_info()[0])
        #    # traceback.print_exc(file=sys.stdout)
        #    raise

    return coord_file