# whether to report photozs to TNS and skyportal
report_photoz_TNS = False
report_photoz_skyportal = False

# add redshift to Simbad query
customSimbad = Simbad()
customSimbad.add_votable_fields('z_value')
customSimbad.add_votable_fields('rv_value')
customSimbad.add_votable_fields('rvz_type')
customSimbad.add_votable_fields('rvz_error')
customSimbad.add_votable_fields('rvz_qual')
customSimbad.TIMEOUT = 5 # 5 seconds

# timeout for Ned
customNed = Ned()
customNed.TIMEOUT = 5

class alerce_tns(Alerce):
    'module to interact with alerce api to send TNS report'
    
    def __init__(self, **kwargs):
        
        super().__init__(**kwargs)

        ## fix API address
        #my_config = {
        #    "ZTF_API_URL": "https://dev.api.alerce.online"
        #}
        #self.load_config_from_object(my_config)
        tns_credentials_file = "tns_credentials.json"
Exemple #2
0
    def query_region(self,
                     objectname,
                     match_tol=1.0,
                     obj_radius=1.0,
                     bycoord=False):
        '''
        Fetch remote data from NED and SIMBAD matching coordinates and build table.
        '''
        # Create custom query objects.
        customSimbad = Simbad()
        customNed = Ned()

        # Log SIMBAD votable (changeable) fields.
        logging.debug("SIMBAD votable fields")
        logging.debug(customSimbad.get_votable_fields())

        customSimbad.remove_votable_fields('coordinates')
        # customSimbad.add_votable_fields("otype(3)", "ra(d)", "dec(d)")
        customSimbad.add_votable_fields("otype", "ra(d)", "dec(d)")

        # Download object data from both SIMBAD and NED.
        logging.info(
            "Querying SIMBAD and NED for region {}".format(objectname))

        if bycoord:
            try:
                objectcoords = SkyCoord(objectname)
            except (ValueError, u.UnitsError):
                logging.info("Invalid coordinates.")
                return
        else:
            # Resolve the object name into sky coordinate using NED
            # ensures that NED and SIMBAD searches are using the same position
            sesame_database.set('ned')
            try:
                objectcoords = get_icrs_coordinates(objectname)
            except NameResolveError:
                logging.info("Name resolution failed.")
                return

        logging.info("Name resolved to coordinates {}".format(objectcoords))

        # SIMBAD
        logging.info("SIMBAD is currently being queried...")
        try:
            with warnings.catch_warnings(
            ):  # suppress warnings generated by SIMBAD query
                warnings.simplefilter("ignore")
                simbad_table = customSimbad.query_region(objectcoords,
                                                         radius=obj_radius *
                                                         u.arcmin)
        # workaround. If SIMBAD query finds nothing, returns None but we want a zero-length table
            if type(simbad_table) is not Table:
                logging.debug("No SIMBAD objects")
                simbad_table = Table(data=None,
                                     names=DataController.simbad_table_cols,
                                     dtype=DataController.simbad_table_dtypes,
                                     masked=True)
            logging.info("SUCCESS: SIMBAD Data retrieved.")
        except Timeout:
            logging.debug("SIMBAD timeout error")
            return

    # NED
        logging.info("NED is currently being queried...")
        for attempt in range(
                3):  # sometimes NED times out, so try a couple of times
            Ned.TIMEOUT = (attempt + 1) * DataController.ned_timeout_default
            try:
                ned_table = Ned.query_region(objectcoords,
                                             radius=obj_radius * u.arcmin)
                logging.info("SUCCESS: NED Data retrieved.")
            except RequestException:
                logging.debug("NED problem, retrying")
            else:  # if attempt successful break out of loop, no need to try again
                break
        else:  # closes for loop: executes only if all attempts fail
            logging.debug("NED query failed")
            return

# Save some query stats.
        self.stats.query_name = objectname
        self.stats.sim_count = len(simbad_table)
        self.stats.ned_count = len(ned_table)

        # process tables
        ned_table = self.reformat_table(
            ned_table,
            keepcolsifpresent=[
                'Object Name',
                # cover NED changing names of cols
                'RA(deg)',
                'RA',
                'DEC(deg)',
                'DEC',
                'Type'
            ],
            old_name='Object Name',
            new_name='Name_N',
            old_type='Type',
            new_type='Type_N')

        logging.info("Reformatting tables.")
        simbad_table = self.reformat_table(
            simbad_table,
            keepcolsifpresent=["MAIN_ID", "RA_d", "DEC_d", "OTYPE"],
            old_name='MAIN_ID',
            new_name='Name_S',
            old_type='OTYPE',
            new_type='Type_S')

        logging.info("Building sky coordinates.")
        # Build SkyCoord from appropriate ned and simbad col's with matching units
        ned_coo = SkyCoord(ra=ned_table['RA(deg)'], dec=ned_table['DEC(deg)'])
        sim_coo = SkyCoord(ra=simbad_table['RA_d'], dec=simbad_table['DEC_d'])

        logging.info("Finding object matches.")
        # Find object matches
        if len(ned_coo) > 0 and len(sim_coo) > 0:
            matched_ned, matched_sim, ned_only, sim_only = self.symmetric_match_sky_coords_v2(
                ned_coo, sim_coo, match_tol * u.arcsec)
        else:
            matched_ned = []
            matched_sim = []
            ned_only = []
            sim_only = []
        logging.debug("")
        logging.debug("Matched NED rows:")
        logging.debug(ned_table[matched_ned])
        logging.debug("Matched SIMBAD rows:")
        logging.debug(simbad_table[matched_sim])
        logging.debug("")

        self.stats.overlap_count = len(matched_ned)

        # Explore results
        logging.debug("Matched NED:")
        logging.debug(matched_ned)
        logging.debug("Matched SIMBAD")
        logging.debug(matched_sim)
        logging.debug("NED ONLY")
        logging.debug(ned_only)
        logging.debug("SIMBAD ONLY")
        logging.debug(sim_only)

        # Generate the matched table and save the result.
        logging.info("Building combined table.")
        matched_table = hstack(
            [ned_table[matched_ned], simbad_table[matched_sim]],
            join_type='outer',
            metadata_conflicts='silent')  # Hide the metadata warning.
        self.combined_table = matched_table