Source code for

# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""An extensible ASCII table reader and writer.
  Classes to read CDS / Vizier table format

:Copyright: Smithsonian Astrophysical Observatory (2011)
:Author: Tom Aldcroft (

import fnmatch
import itertools
import os
import re
from contextlib import suppress

from astropy.units import Unit

from . import core, fixedwidth

__doctest_skip__ = ["*"]

class CdsHeader(core.BaseHeader):
    _subfmt = "CDS"

    col_type_map = {
        "e": core.FloatType,
        "f": core.FloatType,
        "i": core.IntType,
        "a": core.StrType,

    "The ReadMe file to construct header from."
    readme = None

    def get_type_map_key(self, col):
        match = re.match(r"\d*(\S)", col.raw_type.lower())
        if not match:
            raise ValueError(
                f'Unrecognized {self._subfmt} format "{col.raw_type}" for column'

    def get_cols(self, lines):
        Initialize the header Column objects from the table ``lines`` for a CDS/MRT

        lines : list
            List of table lines


        # Read header block for the table ```` from the read
        # me file ``self.readme``.
        if self.readme and
            in_header = False
            readme_inputter = core.BaseInputter()
            f = readme_inputter.get_lines(self.readme)
            # Header info is not in data lines but in a separate file.
            lines = []
            comment_lines = 0
            for line in f:
                line = line.strip()
                if in_header:
                    if line.startswith(("------", "=======")):
                        comment_lines += 1
                        if comment_lines == 3:
                    match = re.match(
                        r"Byte-by-byte Description of file: (?P<name>.+)$",
                    if match:
                        # Split 'name' in case in contains multiple files
                        names = [s for s in re.split("[, ]+","name")) if s]
                        # Iterate on names to find if one matches the tablename
                        # including wildcards.
                        for pattern in names:
                            if fnmatch.fnmatch(, pattern):
                                in_header = True

                raise core.InconsistentTableError(
                    f"Can't find table {} in {self.readme}"

        found_line = False

        for i_col_def, line in enumerate(lines):
            if re.match(r"Byte-by-byte Description", line, re.IGNORECASE):
                found_line = True
            elif found_line:  # First line after list of file descriptions
                i_col_def -= 1  # Set i_col_def to last description line
            raise ValueError('no line with "Byte-by-byte Description" found')

        re_col_def = re.compile(
                (?P<start> \d+ \s* -)? \s*
                (?P<end>   \d+)        \s+
                (?P<format> [\w.]+)     \s+
                (?P<units> \S+)        \s+
                (?P<name>  \S+)
                (\s+ (?P<descr> \S.*))?""",

        cols = []
        for line in itertools.islice(lines, i_col_def + 4, None):
            if line.startswith(("------", "=======")):
            match = re_col_def.match(line)
            if match:
                col = core.Column("name"))
                col.start = int(
                    re.sub(r'[-\s]', '','start') or'end'))) - 1  # fmt: skip
                col.end = int("end"))
                unit ="units")
                if unit == "---":
                    col.unit = None  # "---" is the marker for no unit in CDS/MRT table
                    col.unit = Unit(unit, format="cds", parse_strict="warn")
                col.description = ("descr") or "").strip()
                col.raw_type ="format")
                col.type = self.get_col_type(col)

                match = re.match(
                    # Matches limits specifier (eg []) that may or may not be
                    # present
                    r"(?P<limits>[\[\]] \S* [\[\]])?"
                    # Matches '?' directly
                    # Matches to nullval if and only if '=' is present
                    r"((?P<equal>=)(?P<nullval> \S*))?"
                    # Matches to order specifier: ('+', '-', '+=', '-=')
                    # Matches description text even even if no whitespace is
                    # present after '?'
                    r"(\s* (?P<descriptiontext> \S.*))?",
                if match:
                    col.description = ("descriptiontext") or "").strip()
                    if issubclass(col.type, core.FloatType):
                        fillval = "nan"
                        fillval = "0"

                    if"nullval") == "-":
                        col.null = "---"
                        # CDS/MRT tables can use -, --, ---, or ---- to mark missing values
                        # see
                        for i in [1, 2, 3, 4]:
                  "-" * i, fillval,
                        col.null ="nullval")
                        if col.null is None:
                            col.null = ""
              , fillval,

            else:  # could be a continuation of the previous col's description
                if cols:
                    cols[-1].description += line.strip()
                    raise ValueError(f'Line "{line}" not parsable as CDS header')

        self.names = [ for x in cols]

        self.cols = cols

class CdsData(core.BaseData):
    """CDS table data reader"""

    _subfmt = "CDS"
    splitter_class = fixedwidth.FixedWidthSplitter

    def process_lines(self, lines):
        """Skip over CDS/MRT header by finding the last section delimiter"""
        # If the header has a ReadMe and data has a filename
        # then no need to skip, as the data lines do not have header
        # info. The ``read`` method adds the table_name to the ``data``
        # attribute.
        if self.header.readme and self.table_name:
            return lines
        i_sections = [
            i for i, x in enumerate(lines) if x.startswith(("------", "======="))
        if not i_sections:
            raise core.InconsistentTableError(
                f"No {self._subfmt} section delimiter found"
        return lines[i_sections[-1] + 1 :]

[docs]class Cds(core.BaseReader): """CDS format table. See: Example:: Table: Table name here = ============================================================================== Catalog reference paper Bibliography info here ================================================================================ ADC_Keywords: Keyword ; Another keyword ; etc Description: Catalog description here. ================================================================================ Byte-by-byte Description of file: datafile3.txt -------------------------------------------------------------------------------- Bytes Format Units Label Explanations -------------------------------------------------------------------------------- 1- 3 I3 --- Index Running identification number 5- 6 I2 h RAh Hour of Right Ascension (J2000) 8- 9 I2 min RAm Minute of Right Ascension (J2000) 11- 15 F5.2 s RAs Second of Right Ascension (J2000) -------------------------------------------------------------------------------- Note (1): A CDS file can contain sections with various metadata. Notes can be multiple lines. Note (2): Another note. -------------------------------------------------------------------------------- 1 03 28 39.09 2 04 18 24.11 **About parsing the CDS format** The CDS format consists of a table description and the table data. These can be in separate files as a ``ReadMe`` file plus data file(s), or combined in a single file. Different subsections within the description are separated by lines of dashes or equal signs ("------" or "======"). The table which specifies the column information must be preceded by a line starting with "Byte-by-byte Description of file:". In the case where the table description is combined with the data values, the data must be in the last section and must be preceded by a section delimiter line (dashes or equal signs only). **Basic usage** Use the ```` function as normal, with an optional ``readme`` parameter indicating the CDS ReadMe file. If not supplied it is assumed that the header information is at the top of the given table. Examples:: >>> from import ascii >>> table ="data/cds.dat") >>> table ="data/vizier/table1.dat", readme="data/vizier/ReadMe") >>> table ="data/cds/multi/lhs2065.dat", readme="data/cds/multi/ReadMe") >>> table ="data/cds/glob/lmxbrefs.dat", readme="data/cds/glob/ReadMe") The table name and the CDS ReadMe file can be entered as URLs. This can be used to directly load tables from the Internet. For example, Vizier tables from the CDS:: >>> table ="", ... readme="") If the header (ReadMe) and data are stored in a single file and there is content between the header and the data (for instance Notes), then the parsing process may fail. In this case you can instruct the reader to guess the actual start of the data by supplying ``data_start='guess'`` in the call to the ```` function. You should verify that the output data table matches expectation based on the input CDS file. **Using a reader object** When ``Cds`` reader object is created with a ``readme`` parameter passed to it at initialization, then when the ``read`` method is executed with a table filename, the header information for the specified table is taken from the ``readme`` file. An ``InconsistentTableError`` is raised if the ``readme`` file does not have header information for the given table. >>> readme = "data/vizier/ReadMe" >>> r = ascii.get_reader(ascii.Cds, readme=readme) >>> table ="data/vizier/table1.dat") >>> # table5.dat has the same ReadMe file >>> table ="data/vizier/table5.dat") If no ``readme`` parameter is specified, then the header information is assumed to be at the top of the given table. >>> r = ascii.get_reader(ascii.Cds) >>> table ="data/cds.dat") >>> #The following gives InconsistentTableError, since no >>> #readme file was given and table1.dat does not have a header. >>> table ="data/vizier/table1.dat") Traceback (most recent call last): ... InconsistentTableError: No CDS section delimiter found Caveats: * The Units and Explanations are available in the column ``unit`` and ``description`` attributes, respectively. * The other metadata defined by this format is not available in the output table. """ _format_name = "cds" _io_registry_format_aliases = ["cds"] _io_registry_can_write = False _description = "CDS format table" data_class = CdsData header_class = CdsHeader def __init__(self, readme=None): super().__init__() self.header.readme = readme
[docs] def write(self, table=None): """Not available for the CDS class (raises NotImplementedError)""" raise NotImplementedError
[docs] def read(self, table): # If the read kwarg `data_start` is 'guess' then the table may have extraneous # lines between the end of the header and the beginning of data. if == "guess": # Replicate the first part of up to the point where # the table lines are initially read in. with suppress(TypeError): # For strings only if os.linesep not in table + "": = os.path.basename(table) = self.header = # Get a list of the lines (rows) in the table lines = self.inputter.get_lines(table) # Now try increasing data.start_line by one until the table reads successfully. # For efficiency use the in-memory list of lines instead of `table`, which # could be a file. for data_start in range(len(lines)): = data_start with suppress(Exception): table = super().read(lines) return table else: return super().read(table)