def test_separations_function(self):
        # xt-kwarg_key_and_value

        from astrocalc.coords import separations
        calculator = separations(
            log=log,
            ra1="23:32:23.2324",
            dec1="-13:32:45.43553",
            ra2="23:32:34.642",
            dec2="-12:12:34.9334",
        )
        print(calculator.get())

        calculator = separations(
            log=log,
            ra1=2.3342343,
            dec1=89.23244233,
            ra2=45.343545345,
            dec2=87.3435435
        )
        print(calculator.get())

        calculator = separations(
            log=log,
            ra1=352.5342343,
            dec1=89.23,
            ra2="23:32:34.642",
            dec2="89:12:34.9334"
        )
        print(calculator.get())
    def test_separations_function(self):
        # xt-kwarg_key_and_value

        from astrocalc.coords import separations
        calculator = separations(
            log=log,
            ra1="23:32:23.2324",
            dec1="-13:32:45.43553",
            ra2="23:32:34.642",
            dec2="-12:12:34.9334",
        )
        print calculator.get()

        from astrocalc.coords import separations
        calculator = separations(
            log=log,
            ra1=2.3342343,
            dec1=89.23244233,
            ra2=45.343545345,
            dec2=87.3435435
        )
        print calculator.get()

        from astrocalc.coords import separations
        calculator = separations(
            log=log,
            ra1=352.5342343,
            dec1=89.23,
            ra2="23:32:34.642",
            dec2="89:12:34.9334"
        )
        print calculator.get()
    def _append_separations_to_results(self):
        """
        *append angular separations to results*

        **Key Arguments**

        # -


        **Return**

        - None


        .. todo::
        """
        self.log.debug(
            'starting the ``_append_separations_to_results`` method')

        for row in self.results:
            if "ra" not in row:
                print(row)
                exit(0)

            # CALCULATE SEPARATION IN ARCSEC
            calculator = separations(
                log=self.log,
                ra1=self.ra,
                dec1=self.dec,
                ra2=row["ra"],
                dec2=row["dec"],
            )
            angularSeparation, northSep, eastSep = calculator.get()

            row["separation_arcsec"] = angularSeparation
            row["separation_north_arcsec"] = northSep
            row["separation_east_arcsec"] = eastSep

        self.log.debug(
            'completed the ``_append_separations_to_results`` method')
        return None
    def _append_separations_to_results(
            self):
        """
        *append angular separations to results*

        **Key Arguments:**
            # -

        **Return:**
            - None

        .. todo::

        """
        self.log.info('starting the ``_append_separations_to_results`` method')

        for row in self.results:
            if "ra" not in row:
                print row
                exit(0)

            # CALCULATE SEPARATION IN ARCSEC
            calculator = separations(
                log=self.log,
                ra1=self.ra,
                dec1=self.dec,
                ra2=row["ra"],
                dec2=row["dec"],
            )
            angularSeparation, northSep, eastSep = calculator.get()

            row["separation_arcsec"] = angularSeparation
            row["separation_north_arcsec"] = northSep
            row["separation_east_arcsec"] = eastSep

        self.log.info(
            'completed the ``_append_separations_to_results`` method')
        return None
Esempio n. 5
0
def main(arguments=None):
    """
    *The main function used when `cl_utils.py` is run as a single script from the cl, or when installed as a cl command*
    """
    from astrocalc.coords import unit_conversion
    # setup the command-line util settings
    su = tools(arguments=arguments,
               docString=__doc__,
               logLevel="CRITICAL",
               options_first=True,
               projectName="astrocalc",
               defaultSettingsFile=True)
    arguments, settings, log, dbConn = su.setup()

    # tab completion for raw_input
    readline.set_completer_delims(' \t\n;')
    readline.parse_and_bind("tab: complete")
    readline.set_completer(tab_complete)

    # UNPACK REMAINING CL ARGUMENTS USING `EXEC` TO SETUP THE VARIABLE NAMES
    # AUTOMATICALLY
    a = {}
    for arg, val in list(arguments.items()):
        if arg[0] == "-":
            varname = arg.replace("-", "") + "Flag"
        else:
            varname = arg.replace("<", "").replace(">", "")
        a[varname] = val
        if arg == "--dbConn":
            dbConn = val
            a["dbConn"] = val
        log.debug('%s = %s' % (
            varname,
            val,
        ))

    ## START LOGGING ##
    startTime = times.get_now_sql_datetime()
    log.info('--- STARTING TO RUN THE cl_utils.py AT %s' % (startTime, ))

    # set options interactively if user requests
    if "interactiveFlag" in a and a["interactiveFlag"]:

        # load previous settings
        moduleDirectory = os.path.dirname(__file__) + "/resources"
        pathToPickleFile = "%(moduleDirectory)s/previousSettings.p" % locals()
        try:
            with open(pathToPickleFile):
                pass
            previousSettingsExist = True
        except:
            previousSettingsExist = False
        previousSettings = {}
        if previousSettingsExist:
            previousSettings = pickle.load(open(pathToPickleFile, "rb"))

        # x-raw-input
        # x-boolean-raw-input
        # x-raw-input-with-default-value-from-previous-settings

        # save the most recently used requests
        pickleMeObjects = []
        pickleMe = {}
        theseLocals = locals()
        for k in pickleMeObjects:
            pickleMe[k] = theseLocals[k]
        pickle.dump(pickleMe, open(pathToPickleFile, "wb"))

    coordflip = a["coordflip"]
    sep = a["sep"]
    timeflip = a["timeflip"]
    trans = a["trans"]
    now = a["now"]
    dist = a["dist"]
    ra = a["ra"]
    ra1 = a["ra1"]
    ra2 = a["ra2"]
    dec = a["dec"]
    dec1 = a["dec1"]
    dec2 = a["dec2"]
    datetime = a["datetime"]
    north = a["north"]
    east = a["east"]
    distVal = a["distVal"]
    hVal = a["hcFlag"]
    OmegaMatter = a["wmFlag"]
    OmegaVacuum = a["wvFlag"]
    mpcFlag = a["mpcFlag"]
    redshiftFlag = a["redshiftFlag"]
    cartesianFlag = a["cartesianFlag"]

    # CALL FUNCTIONS/OBJECTS
    if coordflip:

        if cartesianFlag:
            converter = unit_conversion(log=log)
            x, y, z = converter.ra_dec_to_cartesian(ra="23 45 21.23232",
                                                    dec="+01:58:5.45341")
            print(x, y, z)
            return

        try:
            ra = float(ra)
            dec = float(dec)
            degree = True
        except Exception as e:
            degree = False

        if degree is True:
            converter = unit_conversion(log=log)
            try:
                ra = converter.ra_decimal_to_sexegesimal(ra=ra, delimiter=":")
                dec = converter.dec_decimal_to_sexegesimal(dec=dec,
                                                           delimiter=":")
            except Exception as e:
                print(e)
                sys.exit(0)

            print(ra, dec)
        else:
            converter = unit_conversion(log=log)
            try:
                ra = converter.ra_sexegesimal_to_decimal(ra=ra)
                dec = converter.dec_sexegesimal_to_decimal(dec=dec)
            except Exception as e:
                print(e)
                sys.exit(0)
            print(ra, dec)

    if sep:
        from astrocalc.coords import separations
        calculator = separations(
            log=log,
            ra1=ra1,
            dec1=dec1,
            ra2=ra2,
            dec2=dec2,
        )
        angularSeparation, north, east = calculator.get()
        print("""%(angularSeparation)s arcsec (%(north)s N, %(east)s E)""" %
              locals())

    if timeflip:
        try:
            inputMjd = float(datetime)
            if datetime[0] not in ["0", "1", "2"]:
                inputMjd = True
            else:
                inputMjd = False
        except:
            inputMjd = False
        from astrocalc.times import conversions
        converter = conversions(log=log)

        if inputMjd == False:
            try:
                mjd = converter.ut_datetime_to_mjd(utDatetime=datetime)
                print(mjd)
            except Exception as e:
                print(e)
        else:
            try:
                utDate = converter.mjd_to_ut_datetime(mjd=datetime)
                print(utDate)
            except Exception as e:
                print(e)

    if trans:
        # TRANSLATE COORDINATES ACROSS SKY
        from astrocalc.coords import translate
        newRa, newDec = translate(log=log,
                                  ra=ra,
                                  dec=dec,
                                  northArcsec=float(north),
                                  eastArcsec=float(east)).get()
        from astrocalc.coords import unit_conversion
        converter = unit_conversion(log=log)
        ra = converter.ra_decimal_to_sexegesimal(ra=newRa, delimiter=":")
        dec = converter.dec_decimal_to_sexegesimal(dec=newDec, delimiter=":")

        print("%(newRa)s, %(newDec)s (%(ra)s, %(dec)s)" % locals())

    if now:
        from astrocalc.times import now
        mjd = now(log=log).get_mjd()
        print(mjd)

    if dist and redshiftFlag:
        from astrocalc.distances import converter
        c = converter(log=log)
        if not hcFlag:
            hcFlag = 70.
        if not wmFlag:
            wmFlag = 0.3
        if not wvFlag:
            wvFlag = 0.7
        dists = c.redshift_to_distance(z=float(distVal),
                                       WM=float(wmFlag),
                                       WV=float(wvFlag),
                                       H0=float(hcFlag))
        print("Distance Modulus: " + str(dists["dmod"]) + " mag")
        print("Luminousity Distance: " + str(dists["dl_mpc"]) + " Mpc")
        print("Angular Size Scale: " + str(dists["da_scale"]) + " kpc/arcsec")
        print("Angular Size Distance: " + str(dists["da_mpc"]) + " Mpc")
        print("Comoving Radial Distance: " + str(dists["dcmr_mpc"]) + " Mpc")

    if dist and mpcFlag:
        from astrocalc.distances import converter
        c = converter(log=log)
        z = c.distance_to_redshift(mpc=float(distVal))
        print("z = %(z)s" % locals())

    if "dbConn" in locals() and dbConn:
        dbConn.commit()
        dbConn.close()
    ## FINISH LOGGING ##
    endTime = times.get_now_sql_datetime()
    runningTime = times.calculate_time_difference(startTime, endTime)
    log.info(
        '-- FINISHED ATTEMPT TO RUN THE cl_utils.py AT %s (RUNTIME: %s) --' % (
            endTime,
            runningTime,
        ))

    return
def main(arguments=None):
    """
    *The main function used when ``find_atlas_exposure_containing_ssobject.py`` is run as a single script from the cl*
    """

    # SETUP VARIABLES
    # MAKE SURE HEALPIX SMALL ENOUGH TO MATCH FOOTPRINTS CORRECTLY
    nside = 1024
    pi = (4 * math.atan(1.0))
    DEG_TO_RAD_FACTOR = pi / 180.0
    RAD_TO_DEG_FACTOR = 180.0 / pi
    tileSide = 5.46

    i = 0
    outputList = []
    rsyncContent = []
    obscodes = {"02": "T05", "01": "T08"}

    # SETUP THE COMMAND-LINE UTIL SETTINGS
    su = tools(arguments=arguments,
               docString=__doc__,
               logLevel="WARNING",
               options_first=False,
               projectName=False)
    arguments, settings, log, dbConn = su.setup()

    # UNPACK REMAINING CL ARGUMENTS USING `EXEC` TO SETUP THE VARIABLE NAMES
    # AUTOMATICALLY
    for arg, val in arguments.iteritems():
        if arg[0] == "-":
            varname = arg.replace("-", "") + "Flag"
        else:
            varname = arg.replace("<", "").replace(">", "")
        if isinstance(val, str) or isinstance(val, unicode):
            exec(varname + " = '%s'" % (val, ))
        else:
            exec(varname + " = %s" % (val, ))
        if arg == "--dbConn":
            dbConn = val
        log.debug('%s = %s' % (
            varname,
            val,
        ))

    dbSettings = {
        'host': '127.0.0.1',
        'user': '******',
        'tunnel': {
            'remote ip': 'starbase.mp.qub.ac.uk',
            'remote datbase host': 'dormammu',
            'remote user': '******',
            'port': 5003
        },
        'password': '******',
        'db': 'atlas_moving_objects'
    }

    # SETUP DATABASE CONNECTIONS
    dbConn = database(log=log, dbSettings=dbSettings).connect()

    # GRAB THE EXPOSURE LISTING
    for expPrefix, obscode in obscodes.iteritems():
        exposureList = []
        mjds = []
        sqlQuery = "select * from atlas_exposures where expname like '%(expPrefix)s%%'" % locals(
        )
        connected = 0
        while connected == 0:
            try:
                rows = readquery(log=log,
                                 sqlQuery=sqlQuery,
                                 dbConn=dbConn,
                                 quiet=False)
                connected = 1
            except:
                # SETUP DATABASE CONNECTIONS
                dbConn = database(log=log, dbSettings=dbSettings).connect()
                print "Can't connect to DB - try again"
                time.sleep(2)

        t = len(rows)

        print "There are %(t)s '%(expPrefix)s' exposures to check - hang tight" % locals(
        )

        for row in rows:
            row["mjd"] = row["mjd"] + row["exp_time"] / (2. * 60 * 60 * 24)
            exposureList.append(row)
            mjds.append(row["mjd"])

        results = []

        batchSize = 500
        total = len(mjds[1:])
        batches = int(total / batchSize)

        start = 0
        end = 0
        theseBatches = []
        for i in range(batches + 1):
            end = end + batchSize
            start = i * batchSize
            thisBatch = mjds[start:end]
            theseBatches.append(thisBatch)

        i = 0
        totalLen = len(theseBatches)
        index = 0
        for batch in theseBatches:
            i += 1

            if index > 1:
                # Cursor up one line and clear line
                sys.stdout.write("\x1b[1A\x1b[2K")
            print "Requesting batch %(i)04d/%(totalLen)s from JPL" % locals()
            index += 1

            eph = jpl_horizons_ephemeris(log=log,
                                         objectId=[ssobject],
                                         mjd=batch,
                                         obscode=obscode,
                                         verbose=False)

            for b in batch:
                match = 0
                # print b
                for row in eph:
                    if math.floor(row["mjd"] * 10000 +
                                  0.01) == math.floor(b * 10000 + 0.01):
                        match = 1
                        results.append(row)
                if match == 0:
                    for row in eph:
                        if math.floor(row["mjd"] * 10000) == math.floor(b *
                                                                        10000):
                            match = 1
                            results.append(row)
                if match == 0:
                    results.append(None)
                    this = math.floor(b * 10000 + 0.01)
                    print "MJD %(b)s (%(this)s) is missing" % locals()
                    for row in eph:
                        print math.floor(row["mjd"] * 10000 + 0.00001)
                    print ""

        print "Finding the exopsures containing the SS object"

        for e, r in zip(exposureList, results):
            # CALCULATE SEPARATION IN ARCSEC
            if not r:
                continue

            calculator = separations(
                log=log,
                ra1=r["ra_deg"],
                dec1=r["dec_deg"],
                ra2=e["raDeg"],
                dec2=e["decDeg"],
            )
            angularSeparation, north, east = calculator.get()
            sep = float(angularSeparation) / 3600.
            if sep < 5.:

                # THE SKY-LOCATION AS A HEALPIXEL ID
                pinpoint = hp.ang2pix(nside,
                                      theta=r["ra_deg"],
                                      phi=r["dec_deg"],
                                      lonlat=True)

                decCorners = (e["decDeg"] - tileSide / 2,
                              e["decDeg"] + tileSide / 2)
                corners = []
                for d in decCorners:
                    if d > 90.:
                        d = 180. - d
                    elif d < -90.:
                        d = -180 - d
                    raCorners = (
                        e["raDeg"] -
                        (tileSide / 2) / np.cos(d * DEG_TO_RAD_FACTOR),
                        e["raDeg"] +
                        (tileSide / 2) / np.cos(d * DEG_TO_RAD_FACTOR))
                    for rc in raCorners:
                        if rc > 360.:
                            rc = 720. - rc
                        elif rc < 0.:
                            rc = 360. + rc
                        corners.append(hp.ang2vec(rc, d, lonlat=True))

                # NEAR THE POLES RETURN SQUARE INTO TRIANGE - ALMOST DEGENERATE
                pole = False
                for d in decCorners:
                    if d > 87.0 or d < -87.0:
                        pole = True

                if pole == True:
                    corners = corners[1:]
                else:
                    # FLIP CORNERS 3 & 4 SO HEALPY UNDERSTANDS POLYGON SHAPE
                    corners = [corners[0], corners[1], corners[3], corners[2]]

                # RETURN HEALPIXELS IN EXPOSURE AREA
                expPixels = hp.query_polygon(nside, np.array(corners))
                if pinpoint in expPixels:
                    outputList.append({
                        "obs": e["expname"],
                        "mjd": e["mjd"],
                        "raDeg": r["ra_deg"],
                        "decDeg": r["dec_deg"],
                        "mag": r["apparent_mag"],
                        "sep": sep
                    })
                    thisMjd = int(math.floor(e["mjd"]))
                    expname = e["expname"]
                    ssobject_ = ssobject.replace(" ", "_")
                    raStr = r["ra_deg"]
                    decStr = r["dec_deg"]
                    rsyncContent.append(
                        "rsync -av [email protected]:/atlas/red/%(expPrefix)sa/%(thisMjd)s/%(expname)s.fits.fz %(ssobject_)s_atlas_exposures/"
                        % locals())
                    rsyncContent.append(
                        "touch %(ssobject_)s_atlas_exposures/%(expname)s.location"
                        % locals())
                    rsyncContent.append(
                        'echo "_RAJ2000,_DEJ2000,OBJECT\n%(raStr)s,%(decStr)s,%(ssobject)s" > %(ssobject_)s_atlas_exposures/%(expname)s.location'
                        % locals())

    dataSet = list_of_dictionaries(
        log=log,
        listOfDictionaries=outputList,
        # use re.compile('^[0-9]{4}-[0-9]{2}-[0-9]{2}T') for mysql
        reDatetime=False)

    ssobject = ssobject.replace(" ", "_")
    csvData = dataSet.csv(
        filepath="./%(ssobject)s_atlas_exposure_matches.csv" % locals())

    rsyncContent = ("\n").join(rsyncContent)
    pathToWriteFile = "./%(ssobject)s_atlas_exposure_rsync.sh" % locals()
    try:
        log.debug("attempting to open the file %s" % (pathToWriteFile, ))
        writeFile = codecs.open(pathToWriteFile, encoding='utf-8', mode='w')
    except IOError, e:
        message = 'could not open the file %s' % (pathToWriteFile, )
        log.critical(message)
        raise IOError(message)
    def angular_crossmatch_against_catalogue(self,
                                             objectList,
                                             searchPara={},
                                             search_name="",
                                             brightnessFilter=False,
                                             physicalSearch=False,
                                             classificationType=False):
        """*perform an angular separation crossmatch against a given catalogue in the database and annotate the crossmatch with some value added parameters (distances, physical separations, sub-type of transient etc)*

        **Key Arguments**

        - ``objectList`` -- the list of transient locations to match against the crossmatch catalogue
        - ``searchPara`` -- the search parameters for this individual search as lifted from the search algorithm in the sherlock settings file
        - ``search_name`` -- the name of the search as given in the sherlock settings file
        - ``brightnessFilter`` -- is this search to be constrained by magnitude of the catalogue sources? Default *False*. [bright|faint|general]
        - ``physicalSearch`` -- is this angular search a sub-part of a physical separation search
        - ``classificationType`` -- synonym, association or annotation. Default *False*


         **Return**


            - matchedObjects -- any sources matched against the object

        **Usage**

        Take a list of transients from somewhere

        ```python
        transients = [
            {'ps1_designation': u'PS1-14aef',
             'name': u'4L3Piiq',
             'detection_list_id': 2,
             'local_comments': u'',
             'ra': 0.02548233704918263,
             'followup_id': 2065412L,
             'dec': -4.284933417540423,
             'id': 1000006110041705700L,
             'object_classification': 0L
             },

            {'ps1_designation': u'PS1-13dcr',
             'name': u'3I3Phzx',
             'detection_list_id': 2,
             'local_comments': u'',
             'ra': 4.754236999477372,
             'followup_id': 1140386L,
             'dec': 28.276703631398625,
             'id': 1001901011281636100L,
             'object_classification': 0L
             },

            {'ps1_designation': u'PS1-13dhc',
             'name': u'3I3Pixd',
             'detection_list_id': 2,
             'local_comments': u'',
             'ra': 1.3324973428505413,
             'followup_id': 1202386L,
             'dec': 32.98869220595689,
             'id': 1000519791325919200L,
             'object_classification': 0L
             }
        ]
        ```

        Then run the ``angular_crossmatch_against_catalogue`` method to crossmatch against the catalogues and return results:

        ```python
        # ANGULAR CONESEARCH ON CATALOGUE
        search_name = "ned_d spec sn"
        searchPara = self.settings["search algorithm"][search_name]
        matchedObjects = xmatcher.angular_crossmatch_against_catalogue(
            objectList=transients,
            searchPara=searchPara,
            search_name=search_name
        )
        ```


        .. todo ::

            - update key arguments values and definitions with defaults
            - update return values and definitions
            - update usage examples and text
            - update docstring text
            - check sublime snippet exists
            - clip any useful text to docs mindmap
            - regenerate the docs and check redendering of this docstring
        """
        self.log.debug(
            'starting the ``angular_crossmatch_against_catalogue`` method')

        self.log.info("STARTING %s SEARCH" % (search_name, ))

        start_time = time.time()

        # DEFAULTS

        # print search_name, classificationType

        magnitudeLimitFilter = None
        upperMagnitudeLimit = False
        lowerMagnitudeLimit = False

        catalogueName = searchPara["database table"]

        if not "mag column" in searchPara:
            searchPara["mag column"] = None

        if brightnessFilter:
            if "mag column" in searchPara and searchPara["mag column"]:
                magnitudeLimitFilter = self.colMaps[catalogueName][
                    searchPara["mag column"] + "ColName"]
            theseSearchPara = searchPara[brightnessFilter]
        else:
            theseSearchPara = searchPara

        # EXTRACT PARAMETERS FROM ARGUMENTS & SETTINGS FILE
        if classificationType == "synonym":
            radius = self.settings["synonym radius arcsec"]
            matchedType = theseSearchPara["synonym"]
        elif classificationType == "association":
            radius = theseSearchPara["angular radius arcsec"]
            matchedType = theseSearchPara["association"]
        elif classificationType == "annotation":
            radius = theseSearchPara["angular radius arcsec"]
            matchedType = theseSearchPara["annotation"]

        if brightnessFilter == "faint":
            upperMagnitudeLimit = theseSearchPara["mag limit"]
        elif brightnessFilter == "bright":
            lowerMagnitudeLimit = theseSearchPara["mag limit"]
        elif brightnessFilter == "general":
            if "faint" in searchPara:
                lowerMagnitudeLimit = searchPara["faint"]["mag limit"]
            if "bright" in searchPara:
                upperMagnitudeLimit = searchPara["bright"]["mag limit"]

        # VARIABLES
        matchedObjects = []
        matchSubset = []

        transRAs = []
        transRAs[:] = [t['ra'] for t in objectList]
        transDecs = []
        transDecs[:] = [t['dec'] for t in objectList]

        if len(transRAs) == 0:
            return []

        cs = catalogue_conesearch(log=self.log,
                                  ra=transRAs,
                                  dec=transDecs,
                                  radiusArcsec=radius,
                                  colMaps=self.colMaps,
                                  tableName=catalogueName,
                                  dbConn=self.dbConn,
                                  nearestOnly=False,
                                  physicalSearch=physicalSearch,
                                  upperMagnitudeLimit=upperMagnitudeLimit,
                                  lowerMagnitudeLimit=lowerMagnitudeLimit,
                                  magnitudeLimitFilter=magnitudeLimitFilter)

        # catalogueMatches ARE ORDERED BY ANGULAR SEPARATION
        indices, catalogueMatches = cs.search()
        count = 1
        annotatedcatalogueMatches = []

        for i, xm in zip(indices, catalogueMatches):

            # CALCULATE PHYSICAL PARAMETERS ... IF WE CAN
            if "cmSepArcsec" in xm:
                xm["separationArcsec"] = xm["cmSepArcsec"]
                # CALCULATE SEPARATION IN ARCSEC

                calculator = separations(log=self.log,
                                         ra1=objectList[i]["ra"],
                                         dec1=objectList[i]["dec"],
                                         ra2=xm["ra"],
                                         dec2=xm["dec"])
                angularSeparation, north, east = calculator.get()

                xm["northSeparationArcsec"] = north
                xm["eastSeparationArcsec"] = east
                del xm["cmSepArcsec"]

            xm["association_type"] = matchedType
            xm["catalogue_view_name"] = catalogueName
            xm["transient_object_id"] = objectList[i]["id"]
            xm["catalogue_table_name"] = self.colMaps[catalogueName][
                "description"]
            xm["catalogue_table_id"] = self.colMaps[catalogueName]["table_id"]
            xm["catalogue_view_id"] = self.colMaps[catalogueName]["id"]
            if classificationType == "synonym":
                xm["classificationReliability"] = 1
            elif classificationType == "association":
                xm["classificationReliability"] = 2
            elif classificationType == "annotation":
                xm["classificationReliability"] = 3

            xm = self._annotate_crossmatch_with_value_added_parameters(
                crossmatchDict=xm,
                catalogueName=catalogueName,
                searchPara=theseSearchPara,
                search_name=search_name)
            annotatedcatalogueMatches.append(xm)

        catalogueMatches = annotatedcatalogueMatches

        # IF BRIGHT STAR SEARCH
        if brightnessFilter == "bright" and "star" in search_name:
            catalogueMatches = self._bright_star_match(
                matchedObjects=catalogueMatches,
                catalogueName=catalogueName,
                lowerMagnitudeLimit=lowerMagnitudeLimit,
                magnitudeLimitFilter=searchPara["mag column"])

        if brightnessFilter == "general" and "galaxy" in search_name and "galaxy-like" not in search_name and "physical radius kpc" not in theseSearchPara:
            catalogueMatches = self._galaxy_association_cuts(
                matchedObjects=catalogueMatches,
                catalogueName=catalogueName,
                lowerMagnitudeLimit=lowerMagnitudeLimit,
                upperMagnitudeLimit=upperMagnitudeLimit,
                magnitudeLimitFilter=searchPara["mag column"])

        if "match nearest source only" in theseSearchPara and theseSearchPara[
                "match nearest source only"] == True and len(catalogueMatches):
            nearestMatches = []
            transList = []
            for c in catalogueMatches:
                if c["transient_object_id"] not in transList:
                    transList.append(c["transient_object_id"])
                    nearestMatches.append(c)
            catalogueMatches = nearestMatches

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

        self.log.debug("FINISHED %s SEARCH IN %0.5f s" % (
            search_name,
            time.time() - start_time,
        ))

        return catalogueMatches
Esempio n. 8
0
def main(arguments=None):
    """
    *The main function used when ``sky-tile-pinpointer.py`` is run as a single script from the cl*
    """

    # MAKE SURE HEALPIX SMALL ENOUGH TO MATCH FOOTPRINTS CORRECTLY
    nside = 1024

    pi = (4 * math.atan(1.0))
    DEG_TO_RAD_FACTOR = pi / 180.0
    RAD_TO_DEG_FACTOR = 180.0 / pi

    # SETUP THE COMMAND-LINE UTIL SETTINGS
    su = tools(arguments=arguments,
               docString=__doc__,
               logLevel="WARNING",
               options_first=False,
               projectName=False)
    arguments, settings, log, dbConn = su.setup()

    # unpack remaining cl arguments using `exec` to setup the variable names
    # automatically
    for arg, val in arguments.iteritems():
        if arg[0] == "-":
            varname = arg.replace("-", "") + "Flag"
        else:
            varname = arg.replace("<", "").replace(">", "")
        if isinstance(val, str) or isinstance(val, unicode):
            exec(varname + " = '%s'" % (val, ))
        else:
            exec(varname + " = %s" % (val, ))
        if arg == "--dbConn":
            dbConn = val
        log.debug('%s = %s' % (
            varname,
            val,
        ))

    tileSide = float(tileSide)

    # CONVERT RA AND DEC
    # ASTROCALC UNIT CONVERTER OBJECT
    converter = unit_conversion(log=log)
    ra = converter.ra_sexegesimal_to_decimal(ra=ra)
    dec = converter.dec_sexegesimal_to_decimal(dec=dec)

    # THE SKY-LOCATION AS A HEALPIXEL ID
    pinpoint = hp.ang2pix(nside, theta=ra, phi=dec, lonlat=True)

    matchedTileIds = []
    with open(pathToTileList, 'rb') as csvFile:
        csvReader = csv.DictReader(csvFile,
                                   dialect='excel',
                                   delimiter=',',
                                   quotechar='"')
        for row in csvReader:
            row["DEC"] = float(row["DEC"])
            row["RA"] = float(row["RA"])
            decCorners = (row["DEC"] - tileSide / 2, row["DEC"] + tileSide / 2)
            corners = []
            for d in decCorners:
                if d > 90.:
                    d = 180. - d
                elif d < -90.:
                    d = -180 - d
                raCorners = (row["RA"] -
                             (tileSide / 2) / np.cos(d * DEG_TO_RAD_FACTOR),
                             row["RA"] +
                             (tileSide / 2) / np.cos(d * DEG_TO_RAD_FACTOR))
                for r in raCorners:
                    if r > 360.:
                        r = 720. - r
                    elif r < 0.:
                        r = 360. + r
                    corners.append(hp.ang2vec(r, d, lonlat=True))

            # FLIP CORNERS 3 & 4 SO HEALPY UNDERSTANDS POLYGON SHAPE
            corners = [corners[0], corners[1], corners[3], corners[2]]

            # RETURN HEALPIXELS IN EXPOSURE AREA
            expPixels = hp.query_polygon(nside, np.array(corners))
            if pinpoint in expPixels:
                # CALCULATE SEPARATION IN ARCSEC
                calculator = separations(
                    log=log,
                    ra1=ra,
                    dec1=dec,
                    ra2=row["RA"],
                    dec2=row["DEC"],
                )
                angularSeparation, north, east = calculator.get()
                angularSeparation = float(angularSeparation) / 3600
                north = float(north) / 3600
                east = float(east) / 3600
                matchedTileIds.append(
                    row["EXPID"] +
                    ": %(angularSeparation)1.4f deg from center (%(north)1.4f N, %(east)1.4f E)  "
                    % locals())

    csvFile.close()

    for i in matchedTileIds:
        print i

    return
Esempio n. 9
0
                    ra=ra
                )
                dec = converter.dec_sexegesimal_to_decimal(
                    dec=dec
                )
            except Exception, e:
                print e
                sys.exit(0)
            print ra, dec

    if sep:
        from astrocalc.coords import separations
        calculator = separations(
            log=log,
            ra1=ra1,
            dec1=dec1,
            ra2=ra2,
            dec2=dec2,
        )
        angularSeparation, north, east = calculator.get()
        print """%(angularSeparation)s arcsec (%(north)s N, %(east)s E)""" % locals()

    if timeflip:
        try:
            inputMjd = float(datetime)
            if datetime[0] not in ["0", "1", "2"]:
                inputMjd = True
            else:
                inputMjd = False
        except:
            inputMjd = False