Example #1
0
    def _grab_conesearch_results_from_db(
            self):
        """ grab conesearch results from db
        """
        self.log.debug(
            'starting the ``_grab_conesearch_results_from_db`` method')

        # ACCOUNT FOR TYPE OF SEARCH
        if self.physicalSearch == False and self.transType == "SN":
            where = ""
            if self.colMaps[self.tableName]["redshiftColName"]:
                where += " and %s is null" % (
                    self.colMaps[self.tableName]["redshiftColName"],)
            if self.colMaps[self.tableName]["distanceColName"]:
                where += " and %s is null" % (
                    self.colMaps[self.tableName]["distanceColName"],)
            if self.colMaps[self.tableName]["semiMajorColName"]:
                where += " and %s is null" % (
                    self.colMaps[self.tableName]["semiMajorColName"],)
            self.sqlQuery += where
        elif self.physicalSearch == True:
            where = ""
            if self.colMaps[self.tableName]["redshiftColName"]:
                where += " or %s is not null" % (
                    self.colMaps[self.tableName]["redshiftColName"],)
            if self.colMaps[self.tableName]["distanceColName"]:
                where += " or %s is not null" % (
                    self.colMaps[self.tableName]["distanceColName"],)
            if self.colMaps[self.tableName]["semiMajorColName"]:
                where += " or %s is not null" % (
                    self.colMaps[self.tableName]["semiMajorColName"],)
            if len(where):
                where = " and (" + where[4:] + ")"
                self.sqlQuery += where

        self.results = []
        rows = dms.execute_mysql_read_query(
            sqlQuery=self.sqlQuery,
            dbConn=self.dbConn,
            log=self.log
        )

        if len(rows):
            # IF ONLY A COUNT(*)
            if self.queryType == 3:
                self.results = [[0.0, rows[0]['number']]]
                return "Count", self.results

            # CALCULATE THE ANGULAR SEPARATION FOR EACH ROW
            for row in rows:
                if "guide_star" in self.tableName:
                    # Guide star cat RA and DEC are in RADIANS
                    ra2 = math.degrees(
                        row[self.colMaps[self.tableName]["raColName"]])
                    dec2 = math.degrees(
                        row[self.colMaps[self.tableName]["decColName"]])
                else:
                    ra2 = row[self.colMaps[self.tableName]["raColName"]]
                    dec2 = row[self.colMaps[self.tableName]["decColName"]]

                separation, northSep, eastSep = dat.get_angular_separation(
                    log=self.log,
                    ra1=self.ra,
                    dec1=self.dec,
                    ra2=ra2,
                    dec2=dec2
                )
                self.results.append([separation, row])

            # SORT BY SEPARATION
            from operator import itemgetter
            self.results = sorted(self.results, key=itemgetter(0))

            # IF NEAREST ONLY REQUESTED
            if self.nearestOnly == True:
                self.results = [self.results[0]]
        else:
            tableName = self.tableName
            self.message = "No matches from %(tableName)s." % locals()

        self.log.debug(
            'completed the ``_grab_conesearch_results_from_db`` method')
        return None
Example #2
0
    def _remove_previous_ned_queries(
            self,
            coordinateList):
        """ remove previous ned queries

        **Key Arguments:**
            # - ``coordinateList`` -- set of coordinate to check for previous queries

        **Return:**
            - ``updatedCoordinateList`` -- coordinate list with previous queries removed
        """
        self.log.info('starting the ``_remove_previous_ned_queries`` method')

        # IMPORTS
        import htmCircle
        import math
        from dryxPython import astrotools as dat
        from datetime import datetime, date, time, timedelta

        # 1 DEGREE QUERY RADIUS
        radius = 60. * 60.
        updatedCoordinateList = []

        # FOR EACH TRANSIENT IN COORDINATE LIST
        for c in coordinateList:
            this = c.split(" ")
            raDeg = float(this[0])
            decDeg = float(this[1])

            # BUILD WHERE SECTION OF CLAUSE
            htmWhereClause = htmCircle.htmCircleRegion(
                16, raDeg, decDeg, float(radius))

            # CONVERT RA AND DEC TO CARTESIAN COORDINATES
            ra = math.radians(raDeg)
            dec = math.radians(decDeg)
            cos_dec = math.cos(dec)
            cx = math.cos(ra) * cos_dec
            cy = math.sin(ra) * cos_dec
            cz = math.sin(dec)
            cartesians = (cx, cy, cz)

            # CREATE CARTESIAN SECTION OF QUERY
            cartesianClause = 'and (cx * %.17f + cy * %.17f + cz * %.17f >= cos(%.17f))' % (
                cartesians[0], cartesians[1], cartesians[2], math.radians(radius / 3600.0))

            # CALCULATE THE OLDEST RESULTS LIMIT
            now = datetime.now()
            td = timedelta(
                days=self.settings["ned stream refresh rate in days"])
            refreshLimit = now - td
            refreshLimit = refreshLimit.strftime("%Y-%m-%d %H:%M:%S")

            # FINALLY BUILD THE FULL QUERY AND HIT DATABASE
            sqlQuery = "select * from tcs_helper_ned_query_history %(htmWhereClause)s %(cartesianClause)s and dateQueried > '%(refreshLimit)s'" % locals(
            )
            rows = dms.execute_mysql_read_query(
                sqlQuery=sqlQuery,
                dbConn=self.cataloguesDbConn,
                log=self.log
            )

            # DETERMINE WHICH COORDINATES REQUIRE A NED QUERY
            match = False
            for row in rows:
                raStream = row["raDeg"]
                decStream = row["decDeg"]
                radiusStream = row["arcsecRadius"]
                dateStream = row["dateQueried"]
                angularSeparation, northSep, eastSep = dat.get_angular_separation(
                    log=self.log,
                    ra1=raDeg,
                    dec1=decDeg,
                    ra2=raStream,
                    dec2=decStream
                )
                if angularSeparation + self.settings["first pass ned search radius arcec"] < radiusStream:
                    match = True

            if match == False:
                updatedCoordinateList.append(c)

        self.log.info('completed the ``_remove_previous_ned_queries`` method')
        return updatedCoordinateList