<commit_before>import os
import numpy as np
import pyvo as vo
import warnings
from astropy.table import Table, Column, MaskedColumn, vstack, setdiff
from astropy.coordinates import SkyCoord
from astropy import units as u
#from astroquery.gaia import Gaia
from .solve import PlateSolution
from ..conf import read_conf
from ..database import PlateDB

try:
    import configparser
except ImportError:
    import ConfigParser as configparser


class StarCatalog(Table):
    """
    Class for external astrometric and photometric catalog

    """

    def __init__(self, *args, catalog='Gaia', protocol='TAP', **kwargs):
        """
        Initialise StarCatalog class.

        Parameters
        ----------
        catalog : str
            Name of the external catalog (currently, only Gaia is supported)
        protocol : str
            Catalog query protocol (TAP or SQL)
        """

        super().__init__(*args, **kwargs)

        self.catalog = catalog
        self.protocol = protocol
        self.db_section = None

        self.log = None
        self.gaia_dir = ''
        self.scratch_dir = ''
        self.mag_range = None
        self.name = None

        self.conf = None
        self.db = None
        self.gaia_table = None

    def assign_conf(self, conf):
        """
        Parse configuration and set class attributes.

        """

        if isinstance(conf, str):
            conf = read_conf(conf)

        self.conf = conf

        for attr in ['catalog', 'protocol']:
            try:
                setattr(self, attr, conf.get('Catalog', attr))
            except configparser.Error:
                pass

        try:
            self.protocol = self.protocol.upper()
        except TypeError:
            pass

        if self.protocol == 'SQL':
            try:
                self.db_section = conf.get('Catalog', 'db')
            except configparser.Error:
                pass

    def query_gaia_tap(self, query, skycoord, radius):
        """
        Query Gaia DR2 catalogue with TAP.

        Parameters
        ----------
        query : str
            Query string with placeholders for table name and positional
            constraints
        skycoord : :class:`astropy.coordinates.SkyCoord`
            Sky coordinates for the query center
        radius : :class:`astropy.units.Quantity`
            Angular radius around the center coordinates
        """

        # Initialise TAP service
        tap_service = vo.dal.TAPService('https://gaia.aip.de/tap')

        # Schema and table name in the Gaia database
        gaia_table = 'gdr2.gaia_source'

        # Suppress warnings
        warnings.filterwarnings('ignore', module='astropy.io.votable')

        # Construct query string
        pos_query_str = ('CONTAINS(POINT(\'ICRS\',ra,dec), '
                         'CIRCLE(\'ICRS\',{:f},{:f},{:f}))=1')
        pos_query = (pos_query_str
                     .format(skycoord.ra.to(u.deg).value,
                             skycoord.dec.to(u.deg).value,
                             radius.to(u.deg).value))
        tap_query = query.format(gaia_table, pos_query)

        if self.log is not None:
            self.log.write('Gaia TAP query: {}'.format(tap_query),
                           level=4, event=0)

        tap_result = tap_service.run_async(tap_query, queue='2h')
        tab = tap_result.to_table()

        return tab

    def query_gaia_sql(self, query, skycoord, radius):
        """
        Query Gaia DR2 catalogue with SQL.

        Parameters
        ----------
        query : str
            Query string with placeholders for table name and positional
            constraints
        skycoord : :class:`astropy.coordinates.SkyCoord`
            Sky coordinates for the query center
        radius : :class:`astropy.units.Quantity`
            Angular radius around the center coordinates
        """

        # Construct query string
        pos_query_str = ('pos @ '
                         'SCIRCLE(SPOINT(RADIANS({:f}), RADIANS({:f})), '
                         'RADIANS({:f}))')
        pos_query = (pos_query_str
                     .format(skycoord.ra.to(u.deg).value,
                             skycoord.dec.to(u.deg).value,
                             radius.to(u.deg).value))
        sql_query = query.format(self.gaia_table, pos_query)

        if self.log is not None:
            self.log.write('Gaia SQL query: {}'.format(sql_query),
                           level=4, event=0)

        gaiadb = PlateDB()
        gaiadb.assign_conf(self.conf, section=self.db_section)<commit_msg>Added configuration of reference catalog table name<commit_after>import os
import numpy as np
import pyvo as vo
import warnings
from astropy.table import Table, Column, MaskedColumn, vstack, setdiff
from astropy.coordinates import SkyCoord
from astropy import units as u
#from astroquery.gaia import Gaia
from .solve import PlateSolution
from ..conf import read_conf
from ..database import PlateDB

try:
    import configparser
except ImportError:
    import ConfigParser as configparser


class StarCatalog(Table):
    """
    Class for external astrometric and photometric catalog

    """

    def __init__(self, *args, catalog='Gaia', protocol='TAP', **kwargs):
        """
        Initialise StarCatalog class.

        Parameters
        ----------
        catalog : str
            Name of the external catalog (currently, only Gaia is supported)
        protocol : str
            Catalog query protocol (TAP or SQL)
        """

        super().__init__(*args, **kwargs)

        self.catalog = catalog
        self.protocol = protocol
        self.db_section = None
        self.table_name = None

        self.log = None
        self.gaia_dir = ''
        self.scratch_dir = ''
        self.mag_range = None
        self.name = None

        self.conf = None
        self.db = None

    def assign_conf(self, conf):
        """
        Parse configuration and set class attributes.

        """

        if isinstance(conf, str):
            conf = read_conf(conf)

        self.conf = conf

        for attr in ['catalog', 'protocol', 'table_name']:
            try:
                setattr(self, attr, conf.get('Catalog', attr))
            except configparser.Error:
                pass

        try:
            self.protocol = self.protocol.upper()
        except TypeError:
            pass

        if self.protocol == 'SQL':
            try:
                self.db_section = conf.get('Catalog', 'db')
            except configparser.Error:
                pass

    def query_gaia_tap(self, query, skycoord, radius):
        """
        Query Gaia DR2 catalogue with TAP.

        Parameters
        ----------
        query : str
            Query string with placeholders for table name and positional
            constraints
        skycoord : :class:`astropy.coordinates.SkyCoord`
            Sky coordinates for the query center
        radius : :class:`astropy.units.Quantity`
            Angular radius around the center coordinates
        """

        # Initialise TAP service
        tap_service = vo.dal.TAPService('https://gaia.aip.de/tap')

        # Schema and table name in the Gaia database
        table_name = 'gdr2.gaia_source'

        # Suppress warnings
        warnings.filterwarnings('ignore', module='astropy.io.votable')

        # Construct query string
        pos_query_str = ('CONTAINS(POINT(\'ICRS\',ra,dec), '
                         'CIRCLE(\'ICRS\',{:f},{:f},{:f}))=1')
        pos_query = (pos_query_str
                     .format(skycoord.ra.to(u.deg).value,
                             skycoord.dec.to(u.deg).value,
                             radius.to(u.deg).value))
        tap_query = query.format(table_name, pos_query)

        if self.log is not None:
            self.log.write('Gaia TAP query: {}'.format(tap_query),
                           level=4, event=0)

        tap_result = tap_service.run_async(tap_query, queue='2h')
        tab = tap_result.to_table()

        return tab

    def query_gaia_sql(self, query, skycoord, radius):
        """
        Query Gaia DR2 catalogue with SQL.

        Parameters
        ----------
        query : str
            Query string with placeholders for table name and positional
            constraints
        skycoord : :class:`astropy.coordinates.SkyCoord`
            Sky coordinates for the query center
        radius : :class:`astropy.units.Quantity`
            Angular radius around the center coordinates
        """

        # Construct query string
        pos_query_str = ('pos @ '
                         'SCIRCLE(SPOINT(RADIANS({:f}), RADIANS({:f})), '
                         'RADIANS({:f}))')
        pos_query = (pos_query_str
                     .format(skycoord.ra.to(u.deg).value,
                             skycoord.dec.to(u.deg).value,
                             radius.to(u.deg).value))
        sql_query = query.format(self.table_name, pos_query)

        if self.log is not None:
            self.log.write('Gaia SQL query: {}'.format(sql_query),
                           level=4, event=0)

        gaiadb = PlateDB()
        gaiadb.assign_conf(self.conf, section=self.db_section)
{'fraction_non_alphanumeric': 0.10140997830802603, 'fraction_numerical': 0.0019522776572668114, 'mean_word_length': 1.74761620977354, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1431803', 'n_tokens_mistral': 2590, 'n_tokens_neox': 2484, 'n_words': 652}