예제 #1
0
 def _content_check(self, value):
     if value is not None:
         valid_values = ('boolean', 'bit', 'unsignedByte', 'short', 'int',
                         'long', 'char', 'unicodeChar', 'float', 'double',
                         'floatComplex', 'doubleComplex')
         if value not in valid_values:
             vo_warn(W02, value, self._config, self._pos)
예제 #2
0
 def _content_check(self, value):
     if value is not None:
         valid_values = ('BOOLEAN', 'SMALLINT', 'INTEGER', 'BIGINT', 'REAL',
                         'DOUBLE', 'TIMESTAMP', 'CHAR', 'VARCHAR', 'BINARY',
                         'VARBINARY', 'POINT', 'REGION', 'CLOB', 'BLOB')
         if value not in valid_values:
             vo_warn(W02, value, self._config, self._pos)
예제 #3
0
 def _content_check(self, value):
     if value is not None:
         valid_values = {
             'integer', 'real', 'complex', 'boolean', 'char', 'string'
         }
         if value not in valid_values:
             vo_warn(W02, value, self._config, self._pos)
예제 #4
0
    def __init__(self, tree):
        self._tree = tree

        if tree['__version__'] > __dbversion__:  # pragma: no cover
            vo_warn(W24)

        if not 'catalogs' in tree:  # pragma: no cover
            raise VOSError("Invalid VO service catalog database")

        self._catalogs = tree['catalogs']
예제 #5
0
def test_warning_logging_with_io_votable_warning():
    from astropy.io.votable.exceptions import W02, vo_warn

    with pytest.warns(None) as warn_list:
        log.enable_warnings_logging()
        with log.log_to_list() as log_list:
            vo_warn(W02, ('a', 'b'))
        log.disable_warnings_logging()
    assert len(log_list) == 1
    assert len(warn_list) == 0
    assert log_list[0].levelname == 'WARNING'
    x = log_list[0].message.startswith("W02: ?:?:?: W02: a attribute 'b' is "
                                       "invalid.  Must be a standard XML id")
    assert x
    assert log_list[0].origin == 'astropy.tests.test_logger'
예제 #6
0
def test_warning_logging_with_io_votable_warning():
    from astropy.io.votable.exceptions import W02, vo_warn

    with catch_warnings() as warn_list:
        log.enable_warnings_logging()
        with log.log_to_list() as log_list:
            vo_warn(W02, ('a', 'b'))
        log.disable_warnings_logging()
    assert len(log_list) == 1
    assert len(warn_list) == 0
    assert log_list[0].levelname == 'WARNING'
    x = log_list[0].message.startswith(("W02: ?:?:?: W02: a attribute 'b' is "
                                        "invalid.  Must be a standard XML id"))
    assert x
    assert log_list[0].origin == 'astropy.tests.test_logger'
예제 #7
0
    def __init__(self, tree):
        if 'catalogs' not in tree:
            raise VOSError("Invalid VO service catalog database")

        super(VOSDatabase, self).__init__(tree)
        self._catalogs = tree['catalogs']

        if self.version > __dbversion__:  # pragma: no cover
            vo_warn(W24)

        # Maps access URL to primary key(s).
        # URL is the real key, but we chose title because it is more readable
        # when written out to JSON.
        self._url_keys = defaultdict(list)
        for key, cat in self.get_catalogs():
            self._url_keys[cat['url']].append(key)
예제 #8
0
    def __init__(self, tree):
        if 'catalogs' not in tree:
            raise VOSError("Invalid VO service catalog database")

        super(VOSDatabase, self).__init__(tree)
        self._catalogs = tree['catalogs']

        if self.version > __dbversion__:  # pragma: no cover
            vo_warn(W24)

        # Maps access URL to primary key(s).
        # URL is the real key, but we chose title because it is more readable
        # when written out to JSON.
        self._url_keys = defaultdict(list)
        for key, cat in self.get_catalogs():
            self._url_keys[cat['url']].append(key)
예제 #9
0
    def parse(self, iterator, config):
        super(TablesFile, self).parse(iterator, config)

        if self.tableset is None and self.table is None:
            vo_raise(E07, config=config, pos=self._pos)

        self._version = config['version']
        if config['version'] not in ('1.0', '1.1'):
            vo_warn(W15, config=config, pos=self._pos)

        if self.table:
            if version_compare(config['version'], '1.1') < 0:
                vo_warn(W16, config=config, pos=self._pos)
            self._ntables = 1
        else:
            self._ntables = sum(
                len(schema.tables) for schema in self.tableset.schemas)

        return self
def conesearch(center, radius, verb=1, catalog_db=None, pedantic=None,
               verbose=True, cache=True, timeout=None, query_all=False):
    """
    Perform Cone Search and returns the result of the
    first successful query.

    Parameters
    ----------
    center : str, `astropy.coordinates` object, list, or tuple
        Position of the center of the cone to search.
        It may be specified as an object from the
        :ref:`astropy:astropy-coordinates` package,
        string as accepted by
        :func:`~astroquery.utils.parse_coordinates`, or tuple/list.
        If given as tuple or list, it is assumed to be ``(RA, DEC)``
        in the ICRS coordinate frame, given in decimal degrees.

    radius : float or `~astropy.units.quantity.Quantity`
        Radius of the cone to search:

            - If float is given, it is assumed to be in decimal degrees.
            - If astropy quantity is given, it is internally converted
              to degrees.

    verb : {1, 2, 3}
        Verbosity indicating how many columns are to be returned
        in the resulting table. Support for this parameter by
        a Cone Search service implementation is optional. If the
        service supports the parameter:

            1. Return the bare minimum number of columns that
               the provider considers useful in describing the
               returned objects.
            2. Return a medium number of columns between the
               minimum and maximum (inclusive) that are
               considered by the provider to most typically
               useful to the user.
            3. Return all of the columns that are available for
               describing the objects.

        If not supported, the service should ignore the parameter
        and always return the same columns for every request.

    catalog_db
        May be one of the following, in order from easiest to
        use to most control:

            - `None`: A database of
              ``astroquery.vo_conesearch.conf.conesearch_dbname`` catalogs is
              downloaded from ``astroquery.vo_conesearch.conf.vos_baseurl``.
              The first catalog in the database to successfully return a
              result is used.

            - *catalog name*: A name in the database of
              ``astroquery.vo_conesearch.conf.conesearch_dbname`` catalogs at
              ``astroquery.vo_conesearch.conf.vos_baseurl`` is used.
              For a list of acceptable names, use
              :func:`astroquery.vo_conesearch.vos_catalog.list_catalogs`.

            - *url*: The prefix of a URL to a IVOA Service for
              ``astroquery.vo_conesearch.conf.conesearch_dbname``.
              Must end in either '?' or '&'.

            - `~astroquery.vo_conesearch.vos_catalog.VOSCatalog` object: A
              specific catalog manually downloaded and selected from the
              database (see :ref:`vo-sec-client-vos`).

            - Any of the above 3 options combined in a list, in which case
              they are tried in order.

    pedantic : bool or `None`
        When `True`, raise an error when the result violates the spec,
        otherwise issue a warning. Warnings may be controlled using
        :py:mod:`warnings` module. When not provided, uses the
        configuration setting ``astroquery.vo_conesearch.conf.pedantic``,
        which defaults to `False`.

    verbose : bool
        Verbose output.

    cache : bool
        Use caching for VO Service database. Access to actual VO
        websites referenced by the database still needs internet
        connection.

    timeout : number or `None`
        Timeout limit in seconds for each service being queries.
        If `None`, use default.

    query_all : bool
        This is used by :func:`search_all`.

    Returns
    -------
    obj : `astropy.io.votable.tree.Table`
        First table from first successful VO service request.

    Raises
    ------
    ConeSearchError
        When invalid inputs are passed into Cone Search.

    VOSError
        If VO service request fails.

    """
    # Not using default ConeSearch instance because the attributes are
    # tweaked to match user inputs to this function.
    cs = ConeSearchClass()

    n_timed_out = 0
    service_type = conf.conesearch_dbname
    catalogs = vos_catalog._get_catalogs(
        service_type, catalog_db, cache=cache, verbose=verbose)
    if query_all:
        result = {}
    else:
        result = None

    if pedantic is not None:
        cs.PEDANTIC = pedantic
    if timeout is not None:
        cs.TIMEOUT = timeout

    for name, catalog in catalogs:
        if isinstance(catalog, six.string_types):
            if catalog.startswith('http'):
                url = catalog
            else:
                remote_db = vos_catalog.get_remote_catalog_db(
                    service_type, cache=cache, verbose=verbose)
                catalog = remote_db.get_catalog(catalog)
                url = catalog['url']
        else:
            url = catalog['url']

        cs.URL = url

        if verbose:  # pragma: no cover
            color_print('Trying {0}'.format(url), 'green')

        try:
            r = cs.query_region(center, radius, verb=verb, cache=cache,
                                verbose=verbose)

        except Exception as e:
            err_msg = str(e)
            vo_warn(W25, (url, err_msg))
            if not query_all and 'ConnectTimeoutError' in err_msg:
                n_timed_out += 1
        else:
            if query_all:
                result[r.url] = r
            else:
                result = r
                break

    if result is None:
        err_msg = 'None of the available catalogs returned valid results.'
        if n_timed_out > 0:
            err_msg += ' ({0} URL(s) timed out.)'.format(n_timed_out)
        raise VOSError(err_msg)

    return result
예제 #11
0
def call_vo_service(service_type, catalog_db=None, pedantic=None,
                    verbose=True, cache=True, kwargs={}):
    """
    Makes a generic VO service call.

    Parameters
    ----------
    service_type : str
        Name of the type of service, e.g., 'conesearch_good'.
        Used in error messages and to select a catalog database
        if ``catalog_db`` is not provided.

    catalog_db
        May be one of the following, in order from easiest to
        use to most control:

            - `None`: A database of ``service_type`` catalogs is
              downloaded from ``astroquery.vo_conesearch.conf.vos_baseurl``.
              The first catalog in the database to successfully return a
              result is used.

            - *catalog name*: A name in the database of
              ``service_type`` catalogs at
              ``astroquery.vo_conesearch.conf.vos_baseurl`` is used.
              For a list of acceptable names, use :func:`list_catalogs`.

            - *url*: The prefix of a URL to a IVOA Service for
              ``service_type``. Must end in either '?' or '&'.

            - :class:`VOSCatalog` object: A specific catalog manually
              downloaded and selected from the database
              (see :ref:`vo-sec-client-vos`).

            - Any of the above 3 options combined in a list, in which case
              they are tried in order.

    pedantic : bool or `None`
        When `True`, raise an error when the file violates the spec,
        otherwise issue a warning. Warnings may be controlled using
        :py:mod:`warnings` module. When not provided, uses the
        configuration setting ``astroquery.vo_conesearch.conf.pedantic``,
        which defaults to `False`.

    verbose : bool
        Verbose output.

    cache : bool
        Use caching for VO Service database. Access to actual VO
        websites referenced by the database still needs internet
        connection.

    kwargs : dictionary
        Keyword arguments to pass to the catalog service.
        No checking is done that the arguments are accepted by
        the service, etc.

    Returns
    -------
    obj : `astropy.io.votable.tree.Table`
        First table from first successful VO service request.

    Raises
    ------
    VOSError
        If VO service request fails.

    """
    n_timed_out = 0
    catalogs = _get_catalogs(service_type, catalog_db, cache=cache,
                             verbose=verbose)

    if pedantic is None:  # pragma: no cover
        pedantic = conf.pedantic

    for name, catalog in catalogs:
        if isinstance(catalog, six.string_types):
            if catalog.startswith('http'):
                url = catalog
            else:
                remote_db = get_remote_catalog_db(service_type, cache=cache,
                                                  verbose=verbose)
                catalog = remote_db.get_catalog(catalog)
                url = catalog['url']
        else:
            url = catalog['url']

        if verbose:  # pragma: no cover
            color_print('Trying {0}'.format(url), 'green')

        try:
            return _vo_service_request(url, pedantic, kwargs, cache=cache,
                                       verbose=verbose)
        except Exception as e:
            vo_warn(W25, (url, str(e)))
            if hasattr(e, 'reason') and isinstance(e.reason, socket.timeout):
                n_timed_out += 1

    err_msg = 'None of the available catalogs returned valid results.'
    if n_timed_out > 0:
        err_msg += ' ({0} URL(s) timed out.)'.format(n_timed_out)
    raise VOSError(err_msg)
예제 #12
0
def conesearch(center, radius, verb=1, catalog_db=None, pedantic=None,
               verbose=True, cache=True, timeout=None, query_all=False):
    """
    Perform Cone Search and returns the result of the
    first successful query.

    Parameters
    ----------
    center : str, `astropy.coordinates` object, list, or tuple
        Position of the center of the cone to search.
        It may be specified as an object from the
        :ref:`astropy:astropy-coordinates` package,
        string as accepted by
        :func:`~astroquery.utils.parse_coordinates`, or tuple/list.
        If given as tuple or list, it is assumed to be ``(RA, DEC)``
        in the ICRS coordinate frame, given in decimal degrees.

    radius : float or `~astropy.units.quantity.Quantity`
        Radius of the cone to search:

            - If float is given, it is assumed to be in decimal degrees.
            - If astropy quantity is given, it is internally converted
              to degrees.

    verb : {1, 2, 3}
        Verbosity indicating how many columns are to be returned
        in the resulting table. Support for this parameter by
        a Cone Search service implementation is optional. If the
        service supports the parameter:

            1. Return the bare minimum number of columns that
               the provider considers useful in describing the
               returned objects.
            2. Return a medium number of columns between the
               minimum and maximum (inclusive) that are
               considered by the provider to most typically
               useful to the user.
            3. Return all of the columns that are available for
               describing the objects.

        If not supported, the service should ignore the parameter
        and always return the same columns for every request.

    catalog_db
        May be one of the following, in order from easiest to
        use to most control:

            - `None`: A database of
              ``astroquery.vo_conesearch.conf.conesearch_dbname`` catalogs is
              downloaded from ``astroquery.vo_conesearch.conf.vos_baseurl``.
              The first catalog in the database to successfully return a
              result is used.

            - *catalog name*: A name in the database of
              ``astroquery.vo_conesearch.conf.conesearch_dbname`` catalogs at
              ``astroquery.vo_conesearch.conf.vos_baseurl`` is used.
              For a list of acceptable names, use
              :func:`astroquery.vo_conesearch.vos_catalog.list_catalogs`.

            - *url*: The prefix of a URL to a IVOA Service for
              ``astroquery.vo_conesearch.conf.conesearch_dbname``.
              Must end in either '?' or '&'.

            - `~astroquery.vo_conesearch.vos_catalog.VOSCatalog` object: A
              specific catalog manually downloaded and selected from the
              database (see :ref:`vo-sec-client-vos`).

            - Any of the above 3 options combined in a list, in which case
              they are tried in order.

    pedantic : bool or `None`
        When `True`, raise an error when the result violates the spec,
        otherwise issue a warning. Warnings may be controlled using
        :py:mod:`warnings` module. When not provided, uses the
        configuration setting ``astroquery.vo_conesearch.conf.pedantic``,
        which defaults to `False`.

    verbose : bool
        Verbose output.

    cache : bool
        Use caching for VO Service database. Access to actual VO
        websites referenced by the database still needs internet
        connection.

    timeout : number or `None`
        Timeout limit in seconds for each service being queries.
        If `None`, use default.

    query_all : bool
        This is used by :func:`search_all`.

    Returns
    -------
    obj : `astropy.io.votable.tree.Table`
        First table from first successful VO service request.

    Raises
    ------
    ConeSearchError
        When invalid inputs are passed into Cone Search.

    VOSError
        If VO service request fails.

    """
    # Not using default ConeSearch instance because the attributes are
    # tweaked to match user inputs to this function.
    cs = ConeSearchClass()

    n_timed_out = 0
    service_type = conf.conesearch_dbname
    catalogs = vos_catalog._get_catalogs(
        service_type, catalog_db, cache=cache, verbose=verbose)
    if query_all:
        result = {}
    else:
        result = None

    if pedantic is not None:
        cs.PEDANTIC = pedantic
    if timeout is not None:
        cs.TIMEOUT = timeout

    for name, catalog in catalogs:
        if isinstance(catalog, six.string_types):
            if catalog.startswith('http'):
                url = catalog
            else:
                remote_db = vos_catalog.get_remote_catalog_db(
                    service_type, cache=cache, verbose=verbose)
                catalog = remote_db.get_catalog(catalog)
                url = catalog['url']
        else:
            url = catalog['url']

        cs.URL = url

        if verbose:  # pragma: no cover
            color_print('Trying {0}'.format(url), 'green')

        try:
            r = cs.query_region(center, radius, verb=verb, cache=cache,
                                verbose=verbose)

        except Exception as e:
            err_msg = str(e)
            vo_warn(W25, (url, err_msg))
            if not query_all and 'ConnectTimeoutError' in err_msg:
                n_timed_out += 1
        else:
            if query_all:
                result[r.url] = r
            else:
                result = r
                break

    if result is None:
        err_msg = 'None of the available catalogs returned valid results.'
        if n_timed_out > 0:
            err_msg += ' ({0} URL(s) timed out.)'.format(n_timed_out)
        raise VOSError(err_msg)

    return result
예제 #13
0
def call_vo_service(service_type,
                    catalog_db=None,
                    pedantic=None,
                    verbose=True,
                    cache=True,
                    kwargs={}):
    """
    Makes a generic VO service call.

    Parameters
    ----------
    service_type : str
        Name of the type of service, e.g., 'conesearch_good'.
        Used in error messages and to select a catalog database
        if ``catalog_db`` is not provided.

    catalog_db
        May be one of the following, in order from easiest to
        use to most control:

            - `None`: A database of ``service_type`` catalogs is
              downloaded from ``astroquery.vo_conesearch.conf.vos_baseurl``.
              The first catalog in the database to successfully return a
              result is used.

            - *catalog name*: A name in the database of
              ``service_type`` catalogs at
              ``astroquery.vo_conesearch.conf.vos_baseurl`` is used.
              For a list of acceptable names, use :func:`list_catalogs`.

            - *url*: The prefix of a URL to a IVOA Service for
              ``service_type``. Must end in either '?' or '&'.

            - :class:`VOSCatalog` object: A specific catalog manually
              downloaded and selected from the database
              (see :ref:`vo-sec-client-vos`).

            - Any of the above 3 options combined in a list, in which case
              they are tried in order.

    pedantic : bool or `None`
        When `True`, raise an error when the file violates the spec,
        otherwise issue a warning. Warnings may be controlled using
        :py:mod:`warnings` module. When not provided, uses the
        configuration setting ``astroquery.vo_conesearch.conf.pedantic``,
        which defaults to `False`.

    verbose : bool
        Verbose output.

    cache : bool
        Use caching for VO Service database. Access to actual VO
        websites referenced by the database still needs internet
        connection.

    kwargs : dictionary
        Keyword arguments to pass to the catalog service.
        No checking is done that the arguments are accepted by
        the service, etc.

    Returns
    -------
    obj : `astropy.io.votable.tree.Table`
        First table from first successful VO service request.

    Raises
    ------
    VOSError
        If VO service request fails.

    """
    n_timed_out = 0
    catalogs = _get_catalogs(service_type,
                             catalog_db,
                             cache=cache,
                             verbose=verbose)

    if pedantic is None:  # pragma: no cover
        pedantic = conf.pedantic

    for name, catalog in catalogs:
        if isinstance(catalog, six.string_types):
            if catalog.startswith('http'):
                url = catalog
            else:
                remote_db = get_remote_catalog_db(service_type,
                                                  cache=cache,
                                                  verbose=verbose)
                catalog = remote_db.get_catalog(catalog)
                url = catalog['url']
        else:
            url = catalog['url']

        if verbose:  # pragma: no cover
            color_print('Trying {0}'.format(url), 'green')

        try:
            return _vo_service_request(url,
                                       pedantic,
                                       kwargs,
                                       cache=cache,
                                       verbose=verbose)
        except Exception as e:
            vo_warn(W25, (url, str(e)))
            if hasattr(e, 'reason') and isinstance(e.reason, socket.timeout):
                n_timed_out += 1

    err_msg = 'None of the available catalogs returned valid results.'
    if n_timed_out > 0:
        err_msg += ' ({0} URL(s) timed out.)'.format(n_timed_out)
    raise VOSError(err_msg)
예제 #14
0
def call_vo_service(service_type, catalog_db=None, pedantic=None,
                    verbose=True, cache=True, kwargs={}):
    """
    Makes a generic VO service call.

    Parameters
    ----------
    service_type : str
        Name of the type of service, e.g., 'conesearch'.
        Used in error messages and to select a catalog database
        if `catalog_db` is not provided.

    catalog_db
        May be one of the following, in order from easiest to
        use to most control:

        - `None`
              A database of `service_type` catalogs is downloaded from
              `astropy.vo.client.vos_baseurl`.  The first catalog
              in the database to successfully return a result is used.

        - *catalog name*
              A name in the database of `service_type` catalogs
              at `astropy.vo.client.vos_baseurl` is used.  For a list
              of acceptable names, see :func:`list_catalogs`.

        - *url*
              The prefix of a *url* to a IVOA Service for `service_type`.
              Must end in either '?' or '&'.

        - `VOSCatalog` object
              A specific catalog manually downloaded and selected
              from the database using the APIs in
              `~astropy.vo.client.vos_catalog`.

        - Any of the above 3 options combined in a list, in which case
          they are tried in order.

    pedantic : bool or `None`
        See  `astropy.io.votable.table.parse`.

    verbose : bool
        Verbose output.

    cache : bool
        See `get_remote_catalog_db`.

    kwargs : dictionary
        Keyword arguments to pass to the catalog service.
        No checking is done that the arguments are accepted by
        the service, etc.

    Returns
    -------
    obj : `astropy.io.votable.tree.Table` object
        First table from first successful VO service request.

    Raises
    ------
    VOSError
        If VO service request fails.

    """
    if catalog_db is None:
        catalog_db = get_remote_catalog_db(service_type, cache=cache)
        catalogs = catalog_db.get_catalogs()
    elif isinstance(catalog_db, VOSDatabase):
        catalogs = catalog_db.get_catalogs()
    elif isinstance(catalog_db, (VOSCatalog, basestring)):
        catalogs = [(None, catalog_db)]
    elif isinstance(catalog_db, list):
        for x in catalog_db:
            assert (isinstance(x, (VOSCatalog, basestring)) and
                    not isinstance(x, VOSDatabase))
        catalogs = [(None, x) for x in catalog_db]
    else:  # pragma: no cover
        raise VOSError('catalog_db must be a catalog database, '
                       'a list of catalogs, or a catalog')

    if pedantic is None:  # pragma: no cover
        pedantic = VO_PEDANTIC

    for name, catalog in catalogs:
        if isinstance(catalog, basestring):
            if catalog.startswith("http"):
                url = catalog
            else:
                remote_db = get_remote_catalog_db(service_type, cache=cache)
                catalog = remote_db.get_catalog(catalog)
                url = catalog['url']
        else:
            url = catalog['url']

        if verbose:  # pragma: no cover
            color_print('Trying {0}'.format(url), 'green')

        try:
            return _vo_service_request(url, pedantic, kwargs)
        except Exception as e:
            vo_warn(W25, (url, str(e)))

    raise VOSError('None of the available catalogs returned valid results.')