Beispiel #1
0
    def __init__(self,
                 log,
                 dbConn,
                 tableName,
                 columns,
                 ra,
                 dec,
                 radiusArcsec,
                 sqlWhere=False,
                 raCol="raDeg",
                 decCol="decDeg",
                 separations=False,
                 distinct=False,
                 closest=False):
        self.log = log
        log.debug("instansiating a new 'conesearch' object")
        self.tableName = tableName
        self.dbConn = dbConn
        self.radius = float(radiusArcsec)
        self.raCol = raCol
        self.decCol = decCol
        self.columns = columns
        self.separations = separations
        self.distinct = distinct
        self.sqlWhere = sqlWhere
        self.closest = closest

        if not self.columns:
            self.columns = "*"

        # xt-self-arg-tmpx

        # BULK COORDINATE INTO NUMPY ARRAY
        from astrocalc.coords import coordinates_to_array
        self.ra, self.dec = coordinates_to_array(log=self.log, ra=ra, dec=dec)

        # SETUP THE MESH
        # LESS THAN 1 ARCMIN
        if self.radius < 60.:
            self.htmDepth = 16
        # LESS THAN 1 DEG BUT GREATER THAN 1 ARCMIN
        elif self.radius / (60 * 60) < 1.:
            self.htmDepth = 13
        # GREATER THAN 1 DEGREE
        else:
            self.htmDepth = 10

        # SETUP A MESH AT CHOOSEN DEPTH
        self.mesh = HTM(depth=self.htmDepth, log=self.log)

        # DATETIME REGEX - EXPENSIVE OPERATION, LET"S JUST DO IT ONCE
        self.reDatetime = re.compile('^[0-9]{4}-[0-9]{2}-[0-9]{2}T')

        return None
Beispiel #2
0
    def _list_crossmatch(self, dbRows):
        """*to a finer grain crossmatch of the input coordinates and the database results.*

        **Key Arguments**

        - ``dbRows`` -- the rows return from the database on first crossmatch pass.


        **Return**

        - ``matchIndices1`` -- indices of the coordinate in the original ra and dec lists
        - ``matches`` -- the matched database rows

        """
        self.log.debug('starting the ``_list_crossmatch`` method')

        dbRas = []
        dbRas[:] = [d[self.raCol] for d in dbRows]
        dbDecs = []
        dbDecs[:] = [d[self.decCol] for d in dbRows]

        # 12 SEEMS TO BE GIVING OPTIMAL SPEED FOR MATCHES (VERY ROUGH SPEED
        # TESTS)
        mesh = HTM(depth=12, log=self.log)

        if self.closest:
            maxmatch = 1
        else:
            maxmatch = 0
        matchIndices1, matchIndices2, seps = mesh.match(
            ra1=self.ra,
            dec1=self.dec,
            ra2=np.array(dbRas),
            dec2=np.array(dbDecs),
            radius=float(old_div(self.radius, (60. * 60.))),
            maxmatch=maxmatch  # 1 = match closest 1, 0 = match all
        )

        matches = []

        for m1, m2, s in zip(matchIndices1, matchIndices2, seps):

            if self.separations:

                dbRows[m2]["cmSepArcsec"] = s * (60. * 60.)
            # DEEPCOPY NEEDED IF SAME ELEMENT MATCHED BY 2 SEPERATE
            # ITEMS IN FIRST LIST
            matches.append(copy.deepcopy(dbRows[m2]))

        self.log.debug('completed the ``_list_crossmatch`` method')
        return matchIndices1, matches
Beispiel #3
0
    def _get_on_trixel_sources_from_database_query(self):
        """*generate the mysql query before executing it*
        """
        self.log.debug(
            'completed the ````_get_on_trixel_sources_from_database_query`` method'
        )

        tableName = self.tableName
        raCol = self.raCol
        decCol = self.decCol
        radiusArc = self.radius
        radius = old_div(self.radius, (60. * 60.))

        # GET ALL THE TRIXELS REQUIRED
        trixelArray = self._get_trixel_ids_that_overlap_conesearch_circles()
        if trixelArray.size > 50000 and self.htmDepth == 16:
            self.htmDepth = 13
            self.mesh = HTM(depth=self.htmDepth, log=self.log)
            trixelArray = self._get_trixel_ids_that_overlap_conesearch_circles(
            )
        if trixelArray.size > 50000 and self.htmDepth == 13:
            self.htmDepth = 10
            self.mesh = HTM(depth=self.htmDepth, log=self.log)
            trixelArray = self._get_trixel_ids_that_overlap_conesearch_circles(
            )

        htmLevel = "htm%sID" % self.htmDepth
        if trixelArray.size > 150000:
            self.log.info(
                "Your search radius of the `%(tableName)s` table may be too large (%(radiusArc)s arcsec)"
                % locals())
            minID = np.min(trixelArray)
            maxID = np.max(trixelArray)
            htmWhereClause = "where %(htmLevel)s between %(minID)s and %(maxID)s  " % locals(
            )
        else:
            thesHtmIds = ",".join(np.array(list(map(str, trixelArray))))
            htmWhereClause = "where %(htmLevel)s in (%(thesHtmIds)s)" % locals(
            )

        cols = self.columns[:]
        if cols != "*" and raCol.lower() not in cols.lower():
            cols += ", " + raCol
        if cols != "*" and decCol.lower() not in cols.lower():
            cols += ", " + decCol

        # FINALLY BUILD THE FULL QUERY
        if self.distinct:
            sqlQuery = """select DISTINCT %(cols)s from %(tableName)s %(htmWhereClause)s""" % locals(
            )
        else:
            sqlQuery = """select %(cols)s from %(tableName)s %(htmWhereClause)s""" % locals(
            )

        if self.sqlWhere and len(self.sqlWhere):
            sqlQuery += " and " + self.sqlWhere

        self.log.debug(
            'completed the ``_get_on_trixel_sources_from_database_query`` method'
        )

        return sqlQuery