hash
stringlengths
64
64
content
stringlengths
0
1.51M
c2470286a9711875592649ef66bb5351c0d623f173d80e32b5aa25593dc88bc3
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ Define the Enhanced Character-Separated-Values (ECSV) which allows for reading and writing all the meta data associated with an astropy Table object. """ import json import re import warnings from collections import OrderedDict import numpy as np from astropy.io.ascii.core import convert_numpy from astropy.table import meta, serialize from astropy.utils.data_info import serialize_context_as from astropy.utils.exceptions import AstropyUserWarning from . import basic, core ECSV_VERSION = "1.0" DELIMITERS = (" ", ",") ECSV_DATATYPES = ( "bool", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "float16", "float32", "float64", "float128", "string", ) # Raise warning if not one of these standard dtypes class InvalidEcsvDatatypeWarning(AstropyUserWarning): """ ECSV specific Astropy warning class. """ class EcsvHeader(basic.BasicHeader): """Header class for which the column definition line starts with the comment character. See the :class:`CommentedHeader` class for an example. """ def process_lines(self, lines): """Return only non-blank lines that start with the comment regexp. For these lines strip out the matching characters and leading/trailing whitespace.""" re_comment = re.compile(self.comment) for line in lines: line = line.strip() if not line: continue match = re_comment.match(line) if match: out = line[match.end() :] if out: yield out else: # Stop iterating on first failed match for a non-blank line return def write(self, lines): """ Write header information in the ECSV ASCII format. This function is called at the point when preprocessing has been done to convert the input table columns to `self.cols` which is a list of `astropy.io.ascii.core.Column` objects. In particular `col.str_vals` is available for each column with the string representation of each column item for output. This format starts with a delimiter separated list of the column names in order to make this format readable by humans and simple csv-type readers. It then encodes the full table meta and column attributes and meta as YAML and pretty-prints this in the header. Finally the delimited column names are repeated again, for humans and readers that look for the *last* comment line as defining the column names. """ if self.splitter.delimiter not in DELIMITERS: raise ValueError( "only space and comma are allowed for delimiter in ECSV format" ) # Now assemble the header dict that will be serialized by the YAML dumper header = {"cols": self.cols, "schema": "astropy-2.0"} if self.table_meta: header["meta"] = self.table_meta # Set the delimiter only for the non-default option(s) if self.splitter.delimiter != " ": header["delimiter"] = self.splitter.delimiter header_yaml_lines = [ f"%ECSV {ECSV_VERSION}", "---", ] + meta.get_yaml_from_header(header) lines.extend([self.write_comment + line for line in header_yaml_lines]) lines.append(self.splitter.join([x.info.name for x in self.cols])) def write_comments(self, lines, meta): """ WRITE: Override the default write_comments to do nothing since this is handled in the custom write method. """ pass def update_meta(self, lines, meta): """ READ: Override the default update_meta to do nothing. This process is done in get_cols() for this reader. """ pass def get_cols(self, lines): """ READ: Initialize the header Column objects from the table ``lines``. Parameters ---------- lines : list List of table lines """ # Cache a copy of the original input lines before processing below raw_lines = lines # Extract non-blank comment (header) lines with comment character stripped lines = list(self.process_lines(lines)) # Validate that this is a ECSV file ecsv_header_re = r"""%ECSV [ ] (?P<major> \d+) \. (?P<minor> \d+) \.? (?P<bugfix> \d+)? $""" no_header_msg = ( 'ECSV header line like "# %ECSV <version>" not found as first line.' " This is required for a ECSV file." ) if not lines: raise core.InconsistentTableError(no_header_msg) match = re.match(ecsv_header_re, lines[0].strip(), re.VERBOSE) if not match: raise core.InconsistentTableError(no_header_msg) try: header = meta.get_header_from_yaml(lines) except meta.YamlParseError: raise core.InconsistentTableError("unable to parse yaml in meta header") if "meta" in header: self.table_meta = header["meta"] if "delimiter" in header: delimiter = header["delimiter"] if delimiter not in DELIMITERS: raise ValueError( "only space and comma are allowed for delimiter in ECSV format" ) self.splitter.delimiter = delimiter self.data.splitter.delimiter = delimiter # Create the list of io.ascii column objects from `header` header_cols = OrderedDict((x["name"], x) for x in header["datatype"]) self.names = [x["name"] for x in header["datatype"]] # Read the first non-commented line of table and split to get the CSV # header column names. This is essentially what the Basic reader does. header_line = next(super().process_lines(raw_lines)) header_names = next(self.splitter([header_line])) # Check for consistency of the ECSV vs. CSV header column names if header_names != self.names: raise core.InconsistentTableError( f"column names from ECSV header {self.names} do not " f"match names from header line of CSV data {header_names}" ) # BaseHeader method to create self.cols, which is a list of # io.ascii.core.Column objects (*not* Table Column objects). self._set_cols_from_names() # Transfer attributes from the column descriptor stored in the input # header YAML metadata to the new columns to create this table. for col in self.cols: for attr in ("description", "format", "unit", "meta", "subtype"): if attr in header_cols[col.name]: setattr(col, attr, header_cols[col.name][attr]) col.dtype = header_cols[col.name]["datatype"] # Warn if col dtype is not a valid ECSV datatype, but allow reading for # back-compatibility with existing older files that have numpy datatypes # like datetime64 or object or python str, which are not in the ECSV standard. if col.dtype not in ECSV_DATATYPES: msg = ( f"unexpected datatype {col.dtype!r} of column {col.name!r} " f"is not in allowed ECSV datatypes {ECSV_DATATYPES}. " "Using anyway as a numpy dtype but beware since unexpected " "results are possible." ) warnings.warn(msg, category=InvalidEcsvDatatypeWarning) # Subtype is written like "int64[2,null]" and we want to split this # out to "int64" and [2, None]. subtype = col.subtype if subtype and "[" in subtype: idx = subtype.index("[") col.subtype = subtype[:idx] col.shape = json.loads(subtype[idx:]) # Convert ECSV "string" to numpy "str" for attr in ("dtype", "subtype"): if getattr(col, attr) == "string": setattr(col, attr, "str") # ECSV subtype of 'json' maps to numpy 'object' dtype if col.subtype == "json": col.subtype = "object" def _check_dtype_is_str(col): if col.dtype != "str": raise ValueError(f'datatype of column {col.name!r} must be "string"') class EcsvOutputter(core.TableOutputter): """ After reading the input lines and processing, convert the Reader columns and metadata to an astropy.table.Table object. This overrides the default converters to be an empty list because there is no "guessing" of the conversion function. """ default_converters = [] def __call__(self, cols, meta): # Convert to a Table with all plain Column subclass columns out = super().__call__(cols, meta) # If mixin columns exist (based on the special '__mixin_columns__' # key in the table ``meta``), then use that information to construct # appropriate mixin columns and remove the original data columns. # If no __mixin_columns__ exists then this function just passes back # the input table. out = serialize._construct_mixins_from_columns(out) return out def _convert_vals(self, cols): """READ: Convert str_vals in `cols` to final arrays with correct dtypes. This is adapted from ``BaseOutputter._convert_vals``. In the case of ECSV there is no guessing and all types are known in advance. A big change is handling the possibility of JSON-encoded values, both unstructured object data and structured values that may contain masked data. """ for col in cols: try: # 1-d or N-d object columns are serialized as JSON. if col.subtype == "object": _check_dtype_is_str(col) col_vals = [json.loads(val) for val in col.str_vals] col.data = np.empty([len(col_vals)] + col.shape, dtype=object) col.data[...] = col_vals # Variable length arrays with shape (n, m, ..., *) for fixed # n, m, .. and variable in last axis. Masked values here are # not currently supported. elif col.shape and col.shape[-1] is None: _check_dtype_is_str(col) # Empty (blank) values in original ECSV are changed to "0" # in str_vals with corresponding col.mask being created and # set accordingly. Instead use an empty list here. if hasattr(col, "mask"): for idx in np.nonzero(col.mask)[0]: col.str_vals[idx] = "[]" # Remake as a 1-d object column of numpy ndarrays or # MaskedArray using the datatype specified in the ECSV file. col_vals = [] for str_val in col.str_vals: obj_val = json.loads(str_val) # list or nested lists try: arr_val = np.array(obj_val, dtype=col.subtype) except TypeError: # obj_val has entries that are inconsistent with # dtype. For a valid ECSV file the only possibility # is None values (indicating missing values). data = np.array(obj_val, dtype=object) # Replace all the None with an appropriate fill value mask = data == None kind = np.dtype(col.subtype).kind data[mask] = {"U": "", "S": b""}.get(kind, 0) arr_val = np.ma.array(data.astype(col.subtype), mask=mask) col_vals.append(arr_val) col.shape = () col.dtype = np.dtype(object) # np.array(col_vals_arr, dtype=object) fails ?? so this workaround: col.data = np.empty(len(col_vals), dtype=object) col.data[:] = col_vals # Multidim columns with consistent shape (n, m, ...). These # might be masked. elif col.shape: _check_dtype_is_str(col) # Change empty (blank) values in original ECSV to something # like "[[null, null],[null,null]]" so subsequent JSON # decoding works. Delete `col.mask` so that later code in # core TableOutputter.__call__() that deals with col.mask # does not run (since handling is done here already). if hasattr(col, "mask"): all_none_arr = np.full( shape=col.shape, fill_value=None, dtype=object ) all_none_json = json.dumps(all_none_arr.tolist()) for idx in np.nonzero(col.mask)[0]: col.str_vals[idx] = all_none_json del col.mask col_vals = [json.loads(val) for val in col.str_vals] # Make a numpy object array of col_vals to look for None # (masked values) data = np.array(col_vals, dtype=object) mask = data == None if not np.any(mask): # No None's, just convert to required dtype col.data = data.astype(col.subtype) else: # Replace all the None with an appropriate fill value kind = np.dtype(col.subtype).kind data[mask] = {"U": "", "S": b""}.get(kind, 0) # Finally make a MaskedArray with the filled data + mask col.data = np.ma.array(data.astype(col.subtype), mask=mask) # Regular scalar value column else: if col.subtype: warnings.warn( f"unexpected subtype {col.subtype!r} set for column " f"{col.name!r}, using dtype={col.dtype!r} instead.", category=InvalidEcsvDatatypeWarning, ) converter_func, _ = convert_numpy(col.dtype) col.data = converter_func(col.str_vals) if col.data.shape[1:] != tuple(col.shape): raise ValueError( "shape mismatch between value and column specifier" ) except json.JSONDecodeError: raise ValueError( f"column {col.name!r} failed to convert: " "column value is not valid JSON" ) except Exception as exc: raise ValueError(f"column {col.name!r} failed to convert: {exc}") class EcsvData(basic.BasicData): def _set_fill_values(self, cols): """READ: Set the fill values of the individual cols based on fill_values of BaseData For ECSV handle the corner case of data that has been serialized using the serialize_method='data_mask' option, which writes the full data and mask directly, AND where that table includes a string column with zero-length string entries ("") which are valid data. Normally the super() method will set col.fill_value=('', '0') to replace blanks with a '0'. But for that corner case subset, instead do not do any filling. """ super()._set_fill_values(cols) # Get the serialized columns spec. It might not exist and there might # not even be any table meta, so punt in those cases. try: scs = self.header.table_meta["__serialized_columns__"] except (AttributeError, KeyError): return # Got some serialized columns, so check for string type and serialized # as a MaskedColumn. Without 'data_mask', MaskedColumn objects are # stored to ECSV as normal columns. for col in cols: if ( col.dtype == "str" and col.name in scs and scs[col.name]["__class__"] == "astropy.table.column.MaskedColumn" ): col.fill_values = {} # No data value replacement def str_vals(self): """WRITE: convert all values in table to a list of lists of strings This version considerably simplifies the base method: - No need to set fill values and column formats - No per-item formatting, just use repr() - Use JSON for object-type or multidim values - Only Column or MaskedColumn can end up as cols here. - Only replace masked values with "", not the generalized filling """ for col in self.cols: if len(col.shape) > 1 or col.info.dtype.kind == "O": def format_col_item(idx): obj = col[idx] try: obj = obj.tolist() except AttributeError: pass return json.dumps(obj, separators=(",", ":")) else: def format_col_item(idx): return str(col[idx]) try: col.str_vals = [format_col_item(idx) for idx in range(len(col))] except TypeError as exc: raise TypeError( f"could not convert column {col.info.name!r} to string: {exc}" ) from exc # Replace every masked value in a 1-d column with an empty string. # For multi-dim columns this gets done by JSON via "null". if hasattr(col, "mask") and col.ndim == 1: for idx in col.mask.nonzero()[0]: col.str_vals[idx] = "" out = [col.str_vals for col in self.cols] return out class Ecsv(basic.Basic): """ECSV (Enhanced Character Separated Values) format table. Th ECSV format allows for specification of key table and column meta-data, in particular the data type and unit. See: https://github.com/astropy/astropy-APEs/blob/main/APE6.rst Examples -------- >>> from astropy.table import Table >>> ecsv_content = '''# %ECSV 0.9 ... # --- ... # datatype: ... # - {name: a, unit: m / s, datatype: int64, format: '%03d'} ... # - {name: b, unit: km, datatype: int64, description: This is column b} ... a b ... 001 2 ... 004 3 ... ''' >>> Table.read(ecsv_content, format='ascii.ecsv') <Table length=2> a b m / s km int64 int64 ----- ----- 001 2 004 3 """ _format_name = "ecsv" _description = "Enhanced CSV" _io_registry_suffix = ".ecsv" header_class = EcsvHeader data_class = EcsvData outputter_class = EcsvOutputter max_ndim = None # No limit on column dimensionality def update_table_data(self, table): """ Update table columns in place if mixin columns are present. This is a hook to allow updating the table columns after name filtering but before setting up to write the data. This is currently only used by ECSV and is otherwise just a pass-through. Parameters ---------- table : `astropy.table.Table` Input table for writing Returns ------- table : `astropy.table.Table` Output table for writing """ with serialize_context_as("ecsv"): out = serialize.represent_mixins_as_columns(table) return out
4c78483bd6facb1ba3c50b2f0be6980931cd14bb7da7f3b79c957392674faba8
# Licensed under a 3-clause BSD style license - see LICENSE.rst # This file connects any readers/writers defined in io.misc to the # astropy.table.Table class from . import hdf5, parquet hdf5.register_hdf5() parquet.register_parquet()
d79d306fbbcd24f3135c0ac738ef92909a4c5a559580f358ca905375bf2a60fa
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ This package contains functions for reading and writing HDF5 tables that are not meant to be used directly, but instead are available as readers/writers in `astropy.table`. See :ref:`astropy:table_io` for more details. """ import os import warnings import numpy as np # NOTE: Do not import anything from astropy.table here. # https://github.com/astropy/astropy/issues/6604 from astropy.utils.exceptions import AstropyUserWarning from astropy.utils.misc import NOT_OVERWRITING_MSG HDF5_SIGNATURE = b"\x89HDF\r\n\x1a\n" META_KEY = "__table_column_meta__" __all__ = ["read_table_hdf5", "write_table_hdf5"] def meta_path(path): return path + "." + META_KEY def _find_all_structured_arrays(handle): """ Find all structured arrays in an HDF5 file """ import h5py structured_arrays = [] def append_structured_arrays(name, obj): if isinstance(obj, h5py.Dataset) and obj.dtype.kind == "V": structured_arrays.append(name) handle.visititems(append_structured_arrays) return structured_arrays def is_hdf5(origin, filepath, fileobj, *args, **kwargs): if fileobj is not None: loc = fileobj.tell() try: signature = fileobj.read(8) finally: fileobj.seek(loc) return signature == HDF5_SIGNATURE elif filepath is not None: return filepath.endswith((".hdf5", ".h5")) try: import h5py except ImportError: return False else: return isinstance(args[0], (h5py.File, h5py.Group, h5py.Dataset)) def read_table_hdf5(input, path=None, character_as_bytes=True): """ Read a Table object from an HDF5 file This requires `h5py <http://www.h5py.org/>`_ to be installed. If more than one table is present in the HDF5 file or group, the first table is read in and a warning is displayed. Parameters ---------- input : str or :class:`h5py.File` or :class:`h5py.Group` or :class:`h5py.Dataset` If a string, the filename to read the table from. If an h5py object, either the file or the group object to read the table from. path : str The path from which to read the table inside the HDF5 file. This should be relative to the input file or group. character_as_bytes : bool If `True` then Table columns are left as bytes. If `False` then Table columns are converted to unicode. """ try: import h5py except ImportError: raise Exception("h5py is required to read and write HDF5 files") # This function is iterative, and only gets to writing the file when # the input is an hdf5 Group. Moreover, the input variable is changed in # place. # Here, we save its value to be used at the end when the conditions are # right. input_save = input if isinstance(input, (h5py.File, h5py.Group)): # If a path was specified, follow the path if path is not None: try: input = input[path] except (KeyError, ValueError): raise OSError(f"Path {path} does not exist") # `input` is now either a group or a dataset. If it is a group, we # will search for all structured arrays inside the group, and if there # is one we can proceed otherwise an error is raised. If it is a # dataset, we just proceed with the reading. if isinstance(input, h5py.Group): # Find all structured arrays in group arrays = _find_all_structured_arrays(input) if len(arrays) == 0: raise ValueError(f"no table found in HDF5 group {path}") elif len(arrays) > 0: path = arrays[0] if path is None else path + "/" + arrays[0] if len(arrays) > 1: warnings.warn( "path= was not specified but multiple tables" " are present, reading in first available" f" table (path={path})", AstropyUserWarning, ) return read_table_hdf5(input, path=path) elif not isinstance(input, h5py.Dataset): # If a file object was passed, then we need to extract the filename # because h5py cannot properly read in file objects. if hasattr(input, "read"): try: input = input.name except AttributeError: raise TypeError("h5py can only open regular files") # Open the file for reading, and recursively call read_table_hdf5 with # the file object and the path. f = h5py.File(input, "r") try: return read_table_hdf5(f, path=path, character_as_bytes=character_as_bytes) finally: f.close() # If we are here, `input` should be a Dataset object, which we can now # convert to a Table. # Create a Table object from astropy.table import Table, meta, serialize table = Table(np.array(input)) # Read the meta-data from the file. For back-compatibility, we can read # the old file format where the serialized metadata were saved in the # attributes of the HDF5 dataset. # In the new format, instead, metadata are stored in a new dataset in the # same file. This is introduced in Astropy 3.0 old_version_meta = META_KEY in input.attrs new_version_meta = path is not None and meta_path(path) in input_save if old_version_meta or new_version_meta: if new_version_meta: header = meta.get_header_from_yaml( h.decode("utf-8") for h in input_save[meta_path(path)] ) else: # Must be old_version_meta is True. if (A or B) and not A then B is True header = meta.get_header_from_yaml( h.decode("utf-8") for h in input.attrs[META_KEY] ) if "meta" in list(header.keys()): table.meta = header["meta"] header_cols = {x["name"]: x for x in header["datatype"]} for col in table.columns.values(): for attr in ("description", "format", "unit", "meta"): if attr in header_cols[col.name]: setattr(col, attr, header_cols[col.name][attr]) # Construct new table with mixins, using tbl.meta['__serialized_columns__'] # as guidance. table = serialize._construct_mixins_from_columns(table) else: # Read the meta-data from the file table.meta.update(input.attrs) if not character_as_bytes: table.convert_bytestring_to_unicode() return table def _encode_mixins(tbl): """Encode a Table ``tbl`` that may have mixin columns to a Table with only astropy Columns + appropriate meta-data to allow subsequent decoding. """ from astropy.table import serialize from astropy.utils.data_info import serialize_context_as # Convert the table to one with no mixins, only Column objects. This adds # meta data which is extracted with meta.get_yaml_from_table. with serialize_context_as("hdf5"): encode_tbl = serialize.represent_mixins_as_columns(tbl) return encode_tbl def write_table_hdf5( table, output, path=None, compression=False, append=False, overwrite=False, serialize_meta=False, **create_dataset_kwargs, ): """ Write a Table object to an HDF5 file This requires `h5py <http://www.h5py.org/>`_ to be installed. Parameters ---------- table : `~astropy.table.Table` Data table that is to be written to file. output : str or :class:`h5py.File` or :class:`h5py.Group` If a string, the filename to write the table to. If an h5py object, either the file or the group object to write the table to. path : str The path to which to write the table inside the HDF5 file. This should be relative to the input file or group. If not specified, defaults to ``__astropy_table__``. compression : bool or str or int Whether to compress the table inside the HDF5 file. If set to `True`, ``'gzip'`` compression is used. If a string is specified, it should be one of ``'gzip'``, ``'szip'``, or ``'lzf'``. If an integer is specified (in the range 0-9), ``'gzip'`` compression is used, and the integer denotes the compression level. append : bool Whether to append the table to an existing HDF5 file. overwrite : bool Whether to overwrite any existing file without warning. If ``append=True`` and ``overwrite=True`` then only the dataset will be replaced; the file/group will not be overwritten. serialize_meta : bool Whether to serialize rich table meta-data when writing the HDF5 file, in particular such data required to write and read back mixin columns like ``Time``, ``SkyCoord``, or ``Quantity`` to the file. **create_dataset_kwargs Additional keyword arguments are passed to ``h5py.File.create_dataset()`` or ``h5py.Group.create_dataset()``. """ from astropy.table import meta try: import h5py except ImportError: raise Exception("h5py is required to read and write HDF5 files") if path is None: # table is just an arbitrary, hardcoded string here. path = "__astropy_table__" elif path.endswith("/"): raise ValueError("table path should end with table name, not /") if "/" in path: group, name = path.rsplit("/", 1) else: group, name = None, path if isinstance(output, (h5py.File, h5py.Group)): if len(list(output.keys())) > 0 and name == "__astropy_table__": raise ValueError( "table path should always be set via the " "path= argument when writing to existing " "files" ) elif name == "__astropy_table__": warnings.warn( "table path was not set via the path= argument; " f"using default path {path}" ) if group: try: output_group = output[group] except (KeyError, ValueError): output_group = output.create_group(group) else: output_group = output elif isinstance(output, str): if os.path.exists(output) and not append: if overwrite and not append: os.remove(output) else: raise OSError(NOT_OVERWRITING_MSG.format(output)) # Open the file for appending or writing f = h5py.File(output, "a" if append else "w") # Recursively call the write function try: return write_table_hdf5( table, f, path=path, compression=compression, append=append, overwrite=overwrite, serialize_meta=serialize_meta, ) finally: f.close() else: raise TypeError("output should be a string or an h5py File or Group object") # Check whether table already exists if name in output_group: if append and overwrite: # Delete only the dataset itself del output_group[name] if serialize_meta and name + ".__table_column_meta__" in output_group: del output_group[name + ".__table_column_meta__"] else: raise OSError(f"Table {path} already exists") # Encode any mixin columns as plain columns + appropriate metadata table = _encode_mixins(table) # Table with numpy unicode strings can't be written in HDF5 so # to write such a table a copy of table is made containing columns as # bytestrings. Now this copy of the table can be written in HDF5. if any(col.info.dtype.kind == "U" for col in table.itercols()): table = table.copy(copy_data=False) table.convert_unicode_to_bytestring() # Warn if information will be lost when serialize_meta=False. This is # hardcoded to the set difference between column info attributes and what # HDF5 can store natively (name, dtype) with no meta. if serialize_meta is False: for col in table.itercols(): for attr in ("unit", "format", "description", "meta"): if getattr(col.info, attr, None) not in (None, {}): warnings.warn( "table contains column(s) with defined 'unit', 'format'," " 'description', or 'meta' info attributes. These will" " be dropped since serialize_meta=False.", AstropyUserWarning, ) # Write the table to the file if compression: if compression is True: compression = "gzip" dset = output_group.create_dataset( name, data=table.as_array(), compression=compression, **create_dataset_kwargs, ) else: dset = output_group.create_dataset( name, data=table.as_array(), **create_dataset_kwargs ) if serialize_meta: header_yaml = meta.get_yaml_from_table(table) header_encoded = np.array([h.encode("utf-8") for h in header_yaml]) output_group.create_dataset(meta_path(name), data=header_encoded) else: # Write the Table meta dict key:value pairs to the file as HDF5 # attributes. This works only for a limited set of scalar data types # like numbers, strings, etc., but not any complex types. This path # also ignores column meta like unit or format. for key in table.meta: val = table.meta[key] try: dset.attrs[key] = val except TypeError: warnings.warn( f"Attribute `{key}` of type {type(val)} cannot be written to " "HDF5 files - skipping. (Consider specifying " "serialize_meta=True to write all meta data)", AstropyUserWarning, ) def register_hdf5(): """ Register HDF5 with Unified I/O. """ from astropy.io import registry as io_registry from astropy.table import Table io_registry.register_reader("hdf5", Table, read_table_hdf5) io_registry.register_writer("hdf5", Table, write_table_hdf5) io_registry.register_identifier("hdf5", Table, is_hdf5)
9d588aa5314b2cade2b50d08874dee67ebcbc2dc2c60f75f655e499de3f9b748
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ This module contains functions for serializing core astropy objects via the YAML protocol. It provides functions `~astropy.io.misc.yaml.dump`, `~astropy.io.misc.yaml.load`, and `~astropy.io.misc.yaml.load_all` which call the corresponding functions in `PyYaml <https://pyyaml.org>`_ but use the `~astropy.io.misc.yaml.AstropyDumper` and `~astropy.io.misc.yaml.AstropyLoader` classes to define custom YAML tags for the following astropy classes: - `astropy.units.Unit` - `astropy.units.Quantity` - `astropy.time.Time` - `astropy.time.TimeDelta` - `astropy.coordinates.SkyCoord` - `astropy.coordinates.Angle` - `astropy.coordinates.Latitude` - `astropy.coordinates.Longitude` - `astropy.coordinates.EarthLocation` - `astropy.table.SerializedColumn` Example ======= :: >>> from astropy.io.misc import yaml >>> import astropy.units as u >>> from astropy.time import Time >>> from astropy.coordinates import EarthLocation >>> t = Time(2457389.0, format='mjd', ... location=EarthLocation(1000, 2000, 3000, unit=u.km)) >>> td = yaml.dump(t) >>> print(td) !astropy.time.Time format: mjd in_subfmt: '*' jd1: 4857390.0 jd2: -0.5 location: !astropy.coordinates.earth.EarthLocation ellipsoid: WGS84 x: !astropy.units.Quantity unit: &id001 !astropy.units.Unit {unit: km} value: 1000.0 y: !astropy.units.Quantity unit: *id001 value: 2000.0 z: !astropy.units.Quantity unit: *id001 value: 3000.0 out_subfmt: '*' precision: 3 scale: utc >>> ty = yaml.load(td) >>> ty <Time object: scale='utc' format='mjd' value=2457389.0> >>> ty.location # doctest: +FLOAT_CMP <EarthLocation (1000., 2000., 3000.) km> """ import base64 import numpy as np import yaml from astropy import coordinates as coords from astropy import units as u from astropy.table import SerializedColumn from astropy.time import Time, TimeDelta __all__ = ["AstropyLoader", "AstropyDumper", "load", "load_all", "dump"] def _unit_representer(dumper, obj): out = {"unit": str(obj.to_string())} return dumper.represent_mapping("!astropy.units.Unit", out) def _unit_constructor(loader, node): map = loader.construct_mapping(node) return u.Unit(map["unit"], parse_strict="warn") def _serialized_column_representer(dumper, obj): out = dumper.represent_mapping("!astropy.table.SerializedColumn", obj) return out def _serialized_column_constructor(loader, node): map = loader.construct_mapping(node) return SerializedColumn(map) def _time_representer(dumper, obj): out = obj.info._represent_as_dict() return dumper.represent_mapping("!astropy.time.Time", out) def _time_constructor(loader, node): map = loader.construct_mapping(node) out = Time.info._construct_from_dict(map) return out def _timedelta_representer(dumper, obj): out = obj.info._represent_as_dict() return dumper.represent_mapping("!astropy.time.TimeDelta", out) def _timedelta_constructor(loader, node): map = loader.construct_mapping(node) out = TimeDelta.info._construct_from_dict(map) return out def _ndarray_representer(dumper, obj): if not (obj.flags["C_CONTIGUOUS"] or obj.flags["F_CONTIGUOUS"]): obj = np.ascontiguousarray(obj) if np.isfortran(obj): obj = obj.T order = "F" else: order = "C" data_b64 = base64.b64encode(obj.tobytes()) out = dict( buffer=data_b64, dtype=str(obj.dtype) if not obj.dtype.fields else obj.dtype.descr, shape=obj.shape, order=order, ) return dumper.represent_mapping("!numpy.ndarray", out) def _ndarray_constructor(loader, node): # Convert mapping to a dict useful for initializing ndarray. # Need deep=True since for structured dtype, the contents # include lists and tuples, which need recursion via # construct_sequence. map = loader.construct_mapping(node, deep=True) map["buffer"] = base64.b64decode(map["buffer"]) return np.ndarray(**map) def _void_representer(dumper, obj): data_b64 = base64.b64encode(obj.tobytes()) out = dict( buffer=data_b64, dtype=str(obj.dtype) if not obj.dtype.fields else obj.dtype.descr, ) return dumper.represent_mapping("!numpy.void", out) def _void_constructor(loader, node): # Interpret as node as an array scalar and then index to change to void. map = loader.construct_mapping(node, deep=True) map["buffer"] = base64.b64decode(map["buffer"]) return np.ndarray(shape=(), **map)[()] def _quantity_representer(tag): def representer(dumper, obj): out = obj.info._represent_as_dict() return dumper.represent_mapping(tag, out) return representer def _quantity_constructor(cls): def constructor(loader, node): map = loader.construct_mapping(node) return cls.info._construct_from_dict(map) return constructor def _skycoord_representer(dumper, obj): map = obj.info._represent_as_dict() out = dumper.represent_mapping("!astropy.coordinates.sky_coordinate.SkyCoord", map) return out def _skycoord_constructor(loader, node): map = loader.construct_mapping(node) out = coords.SkyCoord.info._construct_from_dict(map) return out # Straight from yaml's Representer def _complex_representer(self, data): if data.imag == 0.0: data = f"{data.real!r}" elif data.real == 0.0: data = f"{data.imag!r}j" elif data.imag > 0: data = f"{data.real!r}+{data.imag!r}j" else: data = f"{data.real!r}{data.imag!r}j" return self.represent_scalar("tag:yaml.org,2002:python/complex", data) def _complex_constructor(loader, node): map = loader.construct_scalar(node) return complex(map) class AstropyLoader(yaml.SafeLoader): """ Custom SafeLoader that constructs astropy core objects as well as Python tuple and unicode objects. This class is not directly instantiated by user code, but instead is used to maintain the available constructor functions that are called when parsing a YAML stream. See the `PyYaml documentation <https://pyyaml.org/wiki/PyYAMLDocumentation>`_ for details of the class signature. """ def _construct_python_tuple(self, node): return tuple(self.construct_sequence(node)) def _construct_python_unicode(self, node): return self.construct_scalar(node) class AstropyDumper(yaml.SafeDumper): """ Custom SafeDumper that represents astropy core objects as well as Python tuple and unicode objects. This class is not directly instantiated by user code, but instead is used to maintain the available representer functions that are called when generating a YAML stream from an object. See the `PyYaml documentation <https://pyyaml.org/wiki/PyYAMLDocumentation>`_ for details of the class signature. """ def _represent_tuple(self, data): return self.represent_sequence("tag:yaml.org,2002:python/tuple", data) AstropyDumper.add_multi_representer(u.UnitBase, _unit_representer) AstropyDumper.add_multi_representer(u.FunctionUnitBase, _unit_representer) AstropyDumper.add_multi_representer(u.StructuredUnit, _unit_representer) AstropyDumper.add_representer(tuple, AstropyDumper._represent_tuple) AstropyDumper.add_representer(np.ndarray, _ndarray_representer) AstropyDumper.add_representer(np.void, _void_representer) AstropyDumper.add_representer(Time, _time_representer) AstropyDumper.add_representer(TimeDelta, _timedelta_representer) AstropyDumper.add_representer(coords.SkyCoord, _skycoord_representer) AstropyDumper.add_representer(SerializedColumn, _serialized_column_representer) # Numpy dtypes AstropyDumper.add_representer(np.bool_, yaml.representer.SafeRepresenter.represent_bool) for np_type in [ np.int_, np.intc, np.intp, np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64, ]: AstropyDumper.add_representer( np_type, yaml.representer.SafeRepresenter.represent_int ) for np_type in [np.float_, np.float16, np.float32, np.float64, np.longdouble]: AstropyDumper.add_representer( np_type, yaml.representer.SafeRepresenter.represent_float ) for np_type in [np.complex_, complex, np.complex64, np.complex128]: AstropyDumper.add_representer(np_type, _complex_representer) AstropyLoader.add_constructor("tag:yaml.org,2002:python/complex", _complex_constructor) AstropyLoader.add_constructor( "tag:yaml.org,2002:python/tuple", AstropyLoader._construct_python_tuple ) AstropyLoader.add_constructor( "tag:yaml.org,2002:python/unicode", AstropyLoader._construct_python_unicode ) AstropyLoader.add_constructor("!astropy.units.Unit", _unit_constructor) AstropyLoader.add_constructor("!numpy.ndarray", _ndarray_constructor) AstropyLoader.add_constructor("!numpy.void", _void_constructor) AstropyLoader.add_constructor("!astropy.time.Time", _time_constructor) AstropyLoader.add_constructor("!astropy.time.TimeDelta", _timedelta_constructor) AstropyLoader.add_constructor( "!astropy.coordinates.sky_coordinate.SkyCoord", _skycoord_constructor ) AstropyLoader.add_constructor( "!astropy.table.SerializedColumn", _serialized_column_constructor ) for cls, tag in ( (u.Quantity, "!astropy.units.Quantity"), (u.Magnitude, "!astropy.units.Magnitude"), (u.Dex, "!astropy.units.Dex"), (u.Decibel, "!astropy.units.Decibel"), (coords.Angle, "!astropy.coordinates.Angle"), (coords.Latitude, "!astropy.coordinates.Latitude"), (coords.Longitude, "!astropy.coordinates.Longitude"), (coords.EarthLocation, "!astropy.coordinates.earth.EarthLocation"), ): AstropyDumper.add_multi_representer(cls, _quantity_representer(tag)) AstropyLoader.add_constructor(tag, _quantity_constructor(cls)) for cls in list(coords.representation.REPRESENTATION_CLASSES.values()) + list( coords.representation.DIFFERENTIAL_CLASSES.values() ): name = cls.__name__ # Add representations/differentials defined in astropy. if name in coords.representation.__all__: tag = "!astropy.coordinates." + name AstropyDumper.add_multi_representer(cls, _quantity_representer(tag)) AstropyLoader.add_constructor(tag, _quantity_constructor(cls)) def load(stream): """Parse the first YAML document in a stream using the AstropyLoader and produce the corresponding Python object. Parameters ---------- stream : str or file-like YAML input Returns ------- obj : object Object corresponding to YAML document """ return yaml.load(stream, Loader=AstropyLoader) def load_all(stream): """Parse the all YAML documents in a stream using the AstropyLoader class and produce the corresponding Python object. Parameters ---------- stream : str or file-like YAML input Returns ------- obj : object Object corresponding to YAML document """ return yaml.load_all(stream, Loader=AstropyLoader) def dump(data, stream=None, **kwargs): """Serialize a Python object into a YAML stream using the AstropyDumper class. If stream is None, return the produced string instead. Parameters ---------- data : object Object to serialize to YAML stream : file-like, optional YAML output (if not supplied a string is returned) **kwargs Other keyword arguments that get passed to yaml.dump() Returns ------- out : str or None If no ``stream`` is supplied then YAML output is returned as str """ kwargs["Dumper"] = AstropyDumper kwargs.setdefault("default_flow_style", None) return yaml.dump(data, stream=stream, **kwargs)
a57c6d752212fe9b29d039dc4e0ee6ce09ea0c4d8e0163b4eb4cf4f3b9353147
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ This module contains simple input/output related functionality that is not part of a larger framework or standard. """ import pickle __all__ = ["fnpickle", "fnunpickle"] def fnunpickle(fileorname, number=0): """Unpickle pickled objects from a specified file and return the contents. Parameters ---------- fileorname : str or file-like The file name or file from which to unpickle objects. If a file object, it should have been opened in binary mode. number : int If 0, a single object will be returned (the first in the file). If >0, this specifies the number of objects to be unpickled, and a list will be returned with exactly that many objects. If <0, all objects in the file will be unpickled and returned as a list. Raises ------ EOFError If ``number`` is >0 and there are fewer than ``number`` objects in the pickled file. Returns ------- contents : object or list If ``number`` is 0, this is a individual object - the first one unpickled from the file. Otherwise, it is a list of objects unpickled from the file. """ if isinstance(fileorname, str): f = open(fileorname, "rb") close = True else: f = fileorname close = False try: if number > 0: # get that number res = [] for i in range(number): res.append(pickle.load(f)) elif number < 0: # get all objects res = [] eof = False while not eof: try: res.append(pickle.load(f)) except EOFError: eof = True else: # number==0 res = pickle.load(f) finally: if close: f.close() return res def fnpickle(object, fileorname, protocol=None, append=False): """Pickle an object to a specified file. Parameters ---------- object The python object to pickle. fileorname : str or file-like The filename or file into which the `object` should be pickled. If a file object, it should have been opened in binary mode. protocol : int or None Pickle protocol to use - see the :mod:`pickle` module for details on these options. If None, the most recent protocol will be used. append : bool If True, the object is appended to the end of the file, otherwise the file will be overwritten (if a file object is given instead of a file name, this has no effect). """ if protocol is None: protocol = pickle.HIGHEST_PROTOCOL if isinstance(fileorname, str): f = open(fileorname, "ab" if append else "wb") close = True else: f = fileorname close = False try: pickle.dump(object, f, protocol=protocol) finally: if close: f.close()
9d8c81d56b3f67215ab90f2bdb6674a48f8740f914c96fb0b1099cede17688ab
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ This package contains functions for reading and writing Parquet tables that are not meant to be used directly, but instead are available as readers/writers in `astropy.table`. See :ref:`astropy:table_io` for more details. """ import os import warnings import numpy as np from astropy.utils import minversion # NOTE: Do not import anything from astropy.table here. # https://github.com/astropy/astropy/issues/6604 from astropy.utils.exceptions import AstropyUserWarning from astropy.utils.misc import NOT_OVERWRITING_MSG PARQUET_SIGNATURE = b"PAR1" __all__ = [] # nothing is publicly scoped def parquet_identify(origin, filepath, fileobj, *args, **kwargs): """Checks if input is in the Parquet format. Parameters ---------- origin : Any filepath : str or None fileobj : `~pyarrow.NativeFile` or None *args, **kwargs Returns ------- is_parquet : bool True if 'fileobj' is not None and is a pyarrow file, or if 'filepath' is a string ending with '.parquet' or '.parq'. False otherwise. """ if fileobj is not None: try: # safely test if pyarrow file pos = fileobj.tell() # store current stream position except AttributeError: return False signature = fileobj.read(4) # read first 4 bytes fileobj.seek(pos) # return to original location return signature == PARQUET_SIGNATURE elif filepath is not None: return filepath.endswith((".parquet", ".parq")) else: return False def read_table_parquet( input, include_names=None, exclude_names=None, schema_only=False, filters=None ): """ Read a Table object from a Parquet file. This requires `pyarrow <https://arrow.apache.org/docs/python/>`_ to be installed. The ``filters`` parameter consists of predicates that are expressed in disjunctive normal form (DNF), like ``[[('x', '=', 0), ...], ...]``. DNF allows arbitrary boolean logical combinations of single column predicates. The innermost tuples each describe a single column predicate. The list of inner predicates is interpreted as a conjunction (AND), forming a more selective and multiple column predicate. Finally, the most outer list combines these filters as a disjunction (OR). Predicates may also be passed as List[Tuple]. This form is interpreted as a single conjunction. To express OR in predicates, one must use the (preferred) List[List[Tuple]] notation. Each tuple has format: (``key``, ``op``, ``value``) and compares the ``key`` with the ``value``. The supported ``op`` are: ``=`` or ``==``, ``!=``, ``<``, ``>``, ``<=``, ``>=``, ``in`` and ``not in``. If the ``op`` is ``in`` or ``not in``, the ``value`` must be a collection such as a ``list``, a ``set`` or a ``tuple``. Examples: .. code-block:: python ('x', '=', 0) ('y', 'in', ['a', 'b', 'c']) ('z', 'not in', {'a','b'}) Parameters ---------- input : str or path-like or file-like object If a string or path-like object, the filename to read the table from. If a file-like object, the stream to read data. include_names : list [str], optional List of names to include in output. If not supplied, then include all columns. exclude_names : list [str], optional List of names to exclude from output (applied after ``include_names``). If not supplied then no columns are excluded. schema_only : bool, optional Only read the schema/metadata with table information. filters : list [tuple] or list [list [tuple] ] or None, optional Rows which do not match the filter predicate will be removed from scanned data. See `pyarrow.parquet.read_table()` for details. Returns ------- table : `~astropy.table.Table` Table will have zero rows and only metadata information if schema_only is True. """ pa, parquet, _ = get_pyarrow() if not isinstance(input, (str, os.PathLike)): # The 'read' attribute is the key component of a generic # file-like object. if not hasattr(input, "read"): raise TypeError("pyarrow can only open path-like or file-like objects.") schema = parquet.read_schema(input) # Pyarrow stores all metadata as byte-strings, so we convert # to UTF-8 strings here. if schema.metadata is not None: md = {k.decode("UTF-8"): v.decode("UTF-8") for k, v in schema.metadata.items()} else: md = {} from astropy.table import Table, meta, serialize # parse metadata from table yaml meta_dict = {} if "table_meta_yaml" in md: meta_yaml = md.pop("table_meta_yaml").split("\n") meta_hdr = meta.get_header_from_yaml(meta_yaml) if "meta" in meta_hdr: meta_dict = meta_hdr["meta"] else: meta_hdr = None # parse and set serialized columns full_table_columns = {name: name for name in schema.names} has_serialized_columns = False if "__serialized_columns__" in meta_dict: has_serialized_columns = True serialized_columns = meta_dict["__serialized_columns__"] for scol in serialized_columns: for name in _get_names(serialized_columns[scol]): full_table_columns[name] = scol use_names = set(full_table_columns.values()) # Apply include_names before exclude_names if include_names is not None: use_names.intersection_update(include_names) if exclude_names is not None: use_names.difference_update(exclude_names) # Preserve column ordering via list, and use this dict trick # to remove duplicates and preserve ordering (for mixin columns) use_names = list( dict.fromkeys([x for x in full_table_columns.values() if x in use_names]) ) # names_to_read is a list of actual serialized column names, where # e.g. the requested name 'time' becomes ['time.jd1', 'time.jd2'] names_to_read = [] for name in use_names: names = [n for n, col in full_table_columns.items() if name == col] names_to_read.extend(names) if not names_to_read: raise ValueError("No include_names specified were found in the table.") # We need to pop any unread serialized columns out of the meta_dict. if has_serialized_columns: for scol in list(meta_dict["__serialized_columns__"].keys()): if scol not in use_names: meta_dict["__serialized_columns__"].pop(scol) # whether to return the whole table or a formatted empty table. if not schema_only: # Read the pyarrow table, specifying columns and filters. pa_table = parquet.read_table(input, columns=names_to_read, filters=filters) num_rows = pa_table.num_rows else: num_rows = 0 # Now need to convert parquet table to Astropy dtype = [] for name in names_to_read: # Pyarrow string and byte columns do not have native length information # so we must determine those here. if schema.field(name).type not in (pa.string(), pa.binary()): # Convert the pyarrow type into a numpy dtype (which is returned # by the to_pandas_type() method). dtype.append(schema.field(name).type.to_pandas_dtype()) continue # Special-case for string and binary columns md_name = f"table::len::{name}" if md_name in md: # String/bytes length from header. strlen = int(md[md_name]) elif schema_only: # Find the maximum string length. # Choose an arbitrary string length since # are not reading in the table. strlen = 10 warnings.warn( f"No {md_name} found in metadata. Guessing {{strlen}} for schema.", AstropyUserWarning, ) else: strlen = max(len(row.as_py()) for row in pa_table[name]) warnings.warn( f"No {md_name} found in metadata. Using longest string" f" ({strlen} characters).", AstropyUserWarning, ) dtype.append( f"U{strlen}" if schema.field(name).type == pa.string() else f"|S{strlen}" ) # Create the empty numpy record array to store the pyarrow data. data = np.zeros(num_rows, dtype=list(zip(names_to_read, dtype))) if not schema_only: # Convert each column in the pyarrow table to a numpy array for name in names_to_read: data[name][:] = pa_table[name].to_numpy() table = Table(data=data, meta=meta_dict) if meta_hdr is not None: # Set description, format, unit, meta from the column # metadata that was serialized with the table. header_cols = {x["name"]: x for x in meta_hdr["datatype"]} for col in table.columns.values(): for attr in ("description", "format", "unit", "meta"): if attr in header_cols[col.name]: setattr(col, attr, header_cols[col.name][attr]) # Convert all compound columns to astropy objects # (e.g. time.jd1, time.jd2 into a single time column) table = serialize._construct_mixins_from_columns(table) return table def write_table_parquet(table, output, overwrite=False): """ Write a Table object to a Parquet file This requires `pyarrow <https://arrow.apache.org/docs/python/>`_ to be installed. Parameters ---------- table : `~astropy.table.Table` Data table that is to be written to file. output : str or path-like The filename to write the table to. overwrite : bool, optional Whether to overwrite any existing file without warning. Default `False`. """ from astropy.table import meta, serialize from astropy.utils.data_info import serialize_context_as pa, parquet, writer_version = get_pyarrow() if not isinstance(output, (str, os.PathLike)): raise TypeError(f"`output` should be a string or path-like, not {output}") # Convert all compound columns into serialized column names, where # e.g. 'time' becomes ['time.jd1', 'time.jd2']. with serialize_context_as("parquet"): encode_table = serialize.represent_mixins_as_columns(table) # We store the encoded serialization metadata as a yaml string. meta_yaml = meta.get_yaml_from_table(encode_table) meta_yaml_str = "\n".join(meta_yaml) metadata = {} for name, col in encode_table.columns.items(): # Parquet will retain the datatypes of columns, but string and # byte column length is lost. Therefore, we special-case these # types to record the length for precise round-tripping. if col.dtype.type is np.str_: metadata[f"table::len::{name}"] = str(col.dtype.itemsize // 4) elif col.dtype.type is np.bytes_: metadata[f"table::len::{name}"] = str(col.dtype.itemsize) metadata["table_meta_yaml"] = meta_yaml_str # Pyarrow stores all metadata as byte strings, so we explicitly encode # our unicode strings in metadata as UTF-8 byte strings here. metadata_encode = { k.encode("UTF-8"): v.encode("UTF-8") for k, v in metadata.items() } # Build the pyarrow schema by converting from the numpy dtype of each # column to an equivalent pyarrow type with from_numpy_dtype() type_list = [ (name, pa.from_numpy_dtype(encode_table.dtype[name].type)) for name in encode_table.dtype.names ] schema = pa.schema(type_list, metadata=metadata_encode) if os.path.exists(output): if overwrite: # We must remove the file prior to writing below. os.remove(output) else: raise OSError(NOT_OVERWRITING_MSG.format(output)) # We use version='2.0' for full support of datatypes including uint32. with parquet.ParquetWriter(output, schema, version=writer_version) as writer: # Convert each Table column to a pyarrow array arrays = [pa.array(col) for col in encode_table.itercols()] # Create a pyarrow table from the list of arrays and the schema pa_table = pa.Table.from_arrays(arrays, schema=schema) # Write the pyarrow table to a file writer.write_table(pa_table) def _get_names(_dict): """Recursively find the names in a serialized column dictionary. Parameters ---------- _dict : `dict` Dictionary from astropy __serialized_columns__ Returns ------- all_names : `list` [`str`] All the column names mentioned in _dict and sub-dicts. """ all_names = [] for k, v in _dict.items(): if isinstance(v, dict): all_names.extend(_get_names(v)) elif k == "name": all_names.append(v) return all_names def register_parquet(): """ Register Parquet with Unified I/O. """ from astropy.io import registry as io_registry from astropy.table import Table io_registry.register_reader("parquet", Table, read_table_parquet) io_registry.register_writer("parquet", Table, write_table_parquet) io_registry.register_identifier("parquet", Table, parquet_identify) def get_pyarrow(): try: import pyarrow as pa from pyarrow import parquet except ImportError: raise Exception("pyarrow is required to read and write parquet files") if minversion(pa, "6.0.0"): writer_version = "2.4" else: writer_version = "2.0" return pa, parquet, writer_version
5f20ebad094a468c7340f24f0af00948f608d93b832e4653f0248b4be8d2b624
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ Mixin columns for use in ascii/tests/test_ecsv.py, fits/tests/test_connect.py, and misc/tests/test_hdf5.py """ import numpy as np from astropy import coordinates, table, time from astropy import units as u el = coordinates.EarthLocation(x=[1, 2] * u.km, y=[3, 4] * u.km, z=[5, 6] * u.km) sr = coordinates.SphericalRepresentation([0, 1] * u.deg, [2, 3] * u.deg, 1 * u.kpc) cr = coordinates.CartesianRepresentation([0, 1] * u.pc, [4, 5] * u.pc, [8, 6] * u.pc) sd = coordinates.SphericalCosLatDifferential( [0, 1] * u.mas / u.yr, [0, 1] * u.mas / u.yr, 10 * u.km / u.s ) srd = coordinates.SphericalRepresentation(sr, differentials=sd) sc = coordinates.SkyCoord( [1, 2], [3, 4], unit="deg,deg", frame="fk4", obstime="J1990.5" ) scd = coordinates.SkyCoord( [1, 2], [3, 4], [5, 6], unit="deg,deg,m", frame="fk4", obstime=["J1990.5"] * 2 ) scdc = scd.copy() scdc.representation_type = "cartesian" scpm = coordinates.SkyCoord( [1, 2], [3, 4], [5, 6], unit="deg,deg,pc", pm_ra_cosdec=[7, 8] * u.mas / u.yr, pm_dec=[9, 10] * u.mas / u.yr, ) scpmrv = coordinates.SkyCoord( [1, 2], [3, 4], [5, 6], unit="deg,deg,pc", pm_ra_cosdec=[7, 8] * u.mas / u.yr, pm_dec=[9, 10] * u.mas / u.yr, radial_velocity=[11, 12] * u.km / u.s, ) scrv = coordinates.SkyCoord( [1, 2], [3, 4], [5, 6], unit="deg,deg,pc", radial_velocity=[11, 12] * u.km / u.s ) tm = time.Time( [51000.5, 51001.5], format="mjd", scale="tai", precision=5, location=el[0] ) tm2 = time.Time(tm, precision=3, format="iso") tm3 = time.Time(tm, location=el) tm3.info.serialize_method["ecsv"] = "jd1_jd2" obj = table.Column([{"a": 1}, {"b": [2]}], dtype="object") su = table.Column( [(1, (1.5, 1.6)), (2, (2.5, 2.6))], name="su", dtype=[("i", np.int64), ("f", [("p1", np.float64), ("p0", np.float64)])], ) su2 = table.Column( [(["snake", "c"], [1.6, 1.5]), (["eal", "a"], [2.5, 2.6])], dtype=[("name", "U5", (2,)), ("f", "f8", (2,))], ) # NOTE: for testing, the name of the column "x" for the # Quantity is important since it tests the fix for #10215 # (namespace clash, where "x" clashes with "el.x"). mixin_cols = { "tm": tm, "tm2": tm2, "tm3": tm3, "dt": time.TimeDelta([1, 2] * u.day), "sc": sc, "scd": scd, "scdc": scdc, "scpm": scpm, "scpmrv": scpmrv, "scrv": scrv, "x": [1, 2] * u.m, "qdb": [10, 20] * u.dB(u.mW), "qdex": [4.5, 5.5] * u.dex(u.cm / u.s**2), "qmag": [21, 22] * u.ABmag, "lat": coordinates.Latitude([1, 2] * u.deg), "lon": coordinates.Longitude([1, 2] * u.deg, wrap_angle=180.0 * u.deg), "ang": coordinates.Angle([1, 2] * u.deg), "el": el, "sr": sr, "cr": cr, "sd": sd, "srd": srd, "nd": table.NdarrayMixin([1, 2]), "obj": obj, "su": su, "su2": su2, } time_attrs = [ "value", "shape", "format", "scale", "precision", "in_subfmt", "out_subfmt", "location", ] compare_attrs = { "tm": time_attrs, "tm2": time_attrs, "tm3": time_attrs, "dt": ["shape", "value", "format", "scale"], "sc": ["ra", "dec", "representation_type", "frame.name"], "scd": ["ra", "dec", "distance", "representation_type", "frame.name"], "scdc": ["x", "y", "z", "representation_type", "frame.name"], "scpm": [ "ra", "dec", "distance", "pm_ra_cosdec", "pm_dec", "representation_type", "frame.name", ], "scpmrv": [ "ra", "dec", "distance", "pm_ra_cosdec", "pm_dec", "radial_velocity", "representation_type", "frame.name", ], "scrv": [ "ra", "dec", "distance", "radial_velocity", "representation_type", "frame.name", ], "x": ["value", "unit"], "qdb": ["value", "unit"], "qdex": ["value", "unit"], "qmag": ["value", "unit"], "lon": ["value", "unit", "wrap_angle"], "lat": ["value", "unit"], "ang": ["value", "unit"], "el": ["x", "y", "z", "ellipsoid"], "nd": ["data"], "sr": ["lon", "lat", "distance"], "cr": ["x", "y", "z"], "sd": ["d_lon_coslat", "d_lat", "d_distance"], "srd": [ "lon", "lat", "distance", "differentials.s.d_lon_coslat", "differentials.s.d_lat", "differentials.s.d_distance", ], "obj": [], "su": ["i", "f.p0", "f.p1"], "su2": ["name", "f"], } non_trivial_names = { "cr": ["cr.x", "cr.y", "cr.z"], "dt": ["dt.jd1", "dt.jd2"], "el": ["el.x", "el.y", "el.z"], "sc": ["sc.ra", "sc.dec"], "scd": ["scd.ra", "scd.dec", "scd.distance", "scd.obstime.jd1", "scd.obstime.jd2"], "scdc": ["scdc.x", "scdc.y", "scdc.z", "scdc.obstime.jd1", "scdc.obstime.jd2"], "scfc": ["scdc.x", "scdc.y", "scdc.z", "scdc.obstime.jd1", "scdc.obstime.jd2"], "scpm": [ "scpm.ra", "scpm.dec", "scpm.distance", "scpm.pm_ra_cosdec", "scpm.pm_dec", ], "scpmrv": [ "scpmrv.ra", "scpmrv.dec", "scpmrv.distance", "scpmrv.pm_ra_cosdec", "scpmrv.pm_dec", "scpmrv.radial_velocity", ], "scrv": ["scrv.ra", "scrv.dec", "scrv.distance", "scrv.radial_velocity"], "sd": ["sd.d_lon_coslat", "sd.d_lat", "sd.d_distance"], "sr": ["sr.lon", "sr.lat", "sr.distance"], "srd": [ "srd.lon", "srd.lat", "srd.distance", "srd.differentials.s.d_lon_coslat", "srd.differentials.s.d_lat", "srd.differentials.s.d_distance", ], "su": ["su.i", "su.f.p1", "su.f.p0"], "su2": ["su2.name", "su2.f"], "tm": ["tm.jd1", "tm.jd2"], "tm2": ["tm2.jd1", "tm2.jd2"], "tm3": ["tm3.jd1", "tm3.jd2", "tm3.location.x", "tm3.location.y", "tm3.location.z"], } serialized_names = { name: non_trivial_names.get(name, [name]) for name in sorted(mixin_cols) }
746dd2289252e015bdc93195a49da346250684503a7f0270ce102098fc957480
# Licensed under a 3-clause BSD style license - see LICENSE.rst import os import sys from collections import OrderedDict from .base import IORegistryError, _UnifiedIORegistryBase __all__ = ["UnifiedIORegistry", "UnifiedInputRegistry", "UnifiedOutputRegistry"] PATH_TYPES = (str, os.PathLike) # TODO! include bytes def _expand_user_in_args(args): # Conservatively attempt to apply `os.path.expanduser` to the first # argument, which can be either a path or the contents of a table. if len(args) and isinstance(args[0], PATH_TYPES): ex_user = os.path.expanduser(args[0]) if ex_user != args[0] and os.path.exists(os.path.dirname(ex_user)): args = (ex_user,) + args[1:] return args # ----------------------------------------------------------------------------- class UnifiedInputRegistry(_UnifiedIORegistryBase): """Read-only Unified Registry. .. versionadded:: 5.0 Examples -------- First let's start by creating a read-only registry. .. code-block:: python >>> from astropy.io.registry import UnifiedInputRegistry >>> read_reg = UnifiedInputRegistry() There is nothing in this registry. Let's make a reader for the :class:`~astropy.table.Table` class:: from astropy.table import Table def my_table_reader(filename, some_option=1): # Read in the table by any means necessary return table # should be an instance of Table Such a function can then be registered with the I/O registry:: read_reg.register_reader('my-table-format', Table, my_table_reader) Note that we CANNOT then read in a table with:: d = Table.read('my_table_file.mtf', format='my-table-format') Why? because ``Table.read`` uses Astropy's default global registry and this is a separate registry. Instead we can read by the read method on the registry:: d = read_reg.read(Table, 'my_table_file.mtf', format='my-table-format') """ def __init__(self): super().__init__() # set _identifiers self._readers = OrderedDict() self._registries["read"] = dict(attr="_readers", column="Read") self._registries_order = ("read", "identify") # ========================================================================= # Read methods def register_reader( self, data_format, data_class, function, force=False, priority=0 ): """ Register a reader function. Parameters ---------- data_format : str The data format identifier. This is the string that will be used to specify the data type when reading. data_class : class The class of the object that the reader produces. function : function The function to read in a data object. force : bool, optional Whether to override any existing function if already present. Default is ``False``. priority : int, optional The priority of the reader, used to compare possible formats when trying to determine the best reader to use. Higher priorities are preferred over lower priorities, with the default priority being 0 (negative numbers are allowed though). """ if not (data_format, data_class) in self._readers or force: self._readers[(data_format, data_class)] = function, priority else: raise IORegistryError( f"Reader for format '{data_format}' and class '{data_class.__name__}'" " is already defined" ) if data_class not in self._delayed_docs_classes: self._update__doc__(data_class, "read") def unregister_reader(self, data_format, data_class): """ Unregister a reader function Parameters ---------- data_format : str The data format identifier. data_class : class The class of the object that the reader produces. """ if (data_format, data_class) in self._readers: self._readers.pop((data_format, data_class)) else: raise IORegistryError( f"No reader defined for format '{data_format}' and class" f" '{data_class.__name__}'" ) if data_class not in self._delayed_docs_classes: self._update__doc__(data_class, "read") def get_reader(self, data_format, data_class): """Get reader for ``data_format``. Parameters ---------- data_format : str The data format identifier. This is the string that is used to specify the data type when reading/writing. data_class : class The class of the object that can be written. Returns ------- reader : callable The registered reader function for this format and class. """ readers = [(fmt, cls) for fmt, cls in self._readers if fmt == data_format] for reader_format, reader_class in readers: if self._is_best_match(data_class, reader_class, readers): return self._readers[(reader_format, reader_class)][0] else: format_table_str = self._get_format_table_str(data_class, "Read") raise IORegistryError( f"No reader defined for format '{data_format}' and class" f" '{data_class.__name__}'.\n\nThe available formats" f" are:\n\n{format_table_str}" ) def read(self, cls, *args, format=None, cache=False, **kwargs): """ Read in data. Parameters ---------- cls : class *args The arguments passed to this method depend on the format. format : str or None cache : bool Whether to cache the results of reading in the data. **kwargs The arguments passed to this method depend on the format. Returns ------- object or None The output of the registered reader. """ ctx = None try: # Expand a tilde-prefixed path if present in args[0] args = _expand_user_in_args(args) if format is None: path = None fileobj = None if len(args): if isinstance(args[0], PATH_TYPES) and not os.path.isdir(args[0]): from astropy.utils.data import get_readable_fileobj # path might be a os.PathLike object if isinstance(args[0], os.PathLike): args = (os.fspath(args[0]),) + args[1:] path = args[0] try: ctx = get_readable_fileobj( args[0], encoding="binary", cache=cache ) fileobj = ctx.__enter__() except OSError: raise except Exception: fileobj = None else: args = [fileobj] + list(args[1:]) elif hasattr(args[0], "read"): path = None fileobj = args[0] format = self._get_valid_format( "read", cls, path, fileobj, args, kwargs ) reader = self.get_reader(format, cls) data = reader(*args, **kwargs) if not isinstance(data, cls): # User has read with a subclass where only the parent class is # registered. This returns the parent class, so try coercing # to desired subclass. try: data = cls(data) except Exception: raise TypeError( f"could not convert reader output to {cls.__name__} class." ) finally: if ctx is not None: ctx.__exit__(*sys.exc_info()) return data # ----------------------------------------------------------------------------- class UnifiedOutputRegistry(_UnifiedIORegistryBase): """Write-only Registry. .. versionadded:: 5.0 """ def __init__(self): super().__init__() self._writers = OrderedDict() self._registries["write"] = dict(attr="_writers", column="Write") self._registries_order = ("write", "identify") # ========================================================================= # Write Methods def register_writer( self, data_format, data_class, function, force=False, priority=0 ): """ Register a table writer function. Parameters ---------- data_format : str The data format identifier. This is the string that will be used to specify the data type when writing. data_class : class The class of the object that can be written. function : function The function to write out a data object. force : bool, optional Whether to override any existing function if already present. Default is ``False``. priority : int, optional The priority of the writer, used to compare possible formats when trying to determine the best writer to use. Higher priorities are preferred over lower priorities, with the default priority being 0 (negative numbers are allowed though). """ if not (data_format, data_class) in self._writers or force: self._writers[(data_format, data_class)] = function, priority else: raise IORegistryError( f"Writer for format '{data_format}' and class '{data_class.__name__}'" " is already defined" ) if data_class not in self._delayed_docs_classes: self._update__doc__(data_class, "write") def unregister_writer(self, data_format, data_class): """ Unregister a writer function Parameters ---------- data_format : str The data format identifier. data_class : class The class of the object that can be written. """ if (data_format, data_class) in self._writers: self._writers.pop((data_format, data_class)) else: raise IORegistryError( f"No writer defined for format '{data_format}' and class" f" '{data_class.__name__}'" ) if data_class not in self._delayed_docs_classes: self._update__doc__(data_class, "write") def get_writer(self, data_format, data_class): """Get writer for ``data_format``. Parameters ---------- data_format : str The data format identifier. This is the string that is used to specify the data type when reading/writing. data_class : class The class of the object that can be written. Returns ------- writer : callable The registered writer function for this format and class. """ writers = [(fmt, cls) for fmt, cls in self._writers if fmt == data_format] for writer_format, writer_class in writers: if self._is_best_match(data_class, writer_class, writers): return self._writers[(writer_format, writer_class)][0] else: format_table_str = self._get_format_table_str(data_class, "Write") raise IORegistryError( f"No writer defined for format '{data_format}' and class" f" '{data_class.__name__}'.\n\nThe available formats" f" are:\n\n{format_table_str}" ) def write(self, data, *args, format=None, **kwargs): """ Write out data. Parameters ---------- data : object The data to write. *args The arguments passed to this method depend on the format. format : str or None **kwargs The arguments passed to this method depend on the format. Returns ------- object or None The output of the registered writer. Most often `None`. .. versionadded:: 4.3 """ # Expand a tilde-prefixed path if present in args[0] args = _expand_user_in_args(args) if format is None: path = None fileobj = None if len(args): if isinstance(args[0], PATH_TYPES): # path might be a os.PathLike object if isinstance(args[0], os.PathLike): args = (os.fspath(args[0]),) + args[1:] path = args[0] fileobj = None elif hasattr(args[0], "read"): path = None fileobj = args[0] format = self._get_valid_format( "write", data.__class__, path, fileobj, args, kwargs ) writer = self.get_writer(format, data.__class__) return writer(data, *args, **kwargs) # ----------------------------------------------------------------------------- class UnifiedIORegistry(UnifiedInputRegistry, UnifiedOutputRegistry): """Unified I/O Registry. .. versionadded:: 5.0 """ def __init__(self): super().__init__() self._registries_order = ("read", "write", "identify") def get_formats(self, data_class=None, readwrite=None): """ Get the list of registered I/O formats as a `~astropy.table.Table`. Parameters ---------- data_class : class, optional Filter readers/writer to match data class (default = all classes). readwrite : str or None, optional Search only for readers (``"Read"``) or writers (``"Write"``). If None search for both. Default is None. .. versionadded:: 1.3 Returns ------- format_table : :class:`~astropy.table.Table` Table of available I/O formats. """ return super().get_formats(data_class, readwrite)
cbd3ce4a3b5791be4afd5b3ffbf4295b889347e297838fa548353954be33924b
# Licensed under a 3-clause BSD style license - see LICENSE.rst import abc import contextlib import re import warnings from collections import OrderedDict from operator import itemgetter import numpy as np __all__ = ["IORegistryError"] class IORegistryError(Exception): """Custom error for registry clashes.""" pass # ----------------------------------------------------------------------------- class _UnifiedIORegistryBase(metaclass=abc.ABCMeta): """Base class for registries in Astropy's Unified IO. This base class provides identification functions and miscellaneous utilities. For an example how to build a registry subclass we suggest :class:`~astropy.io.registry.UnifiedInputRegistry`, which enables read-only registries. These higher-level subclasses will probably serve better as a baseclass, for instance :class:`~astropy.io.registry.UnifiedIORegistry` subclasses both :class:`~astropy.io.registry.UnifiedInputRegistry` and :class:`~astropy.io.registry.UnifiedOutputRegistry` to enable both reading from and writing to files. .. versionadded:: 5.0 """ def __init__(self): # registry of identifier functions self._identifiers = OrderedDict() # what this class can do: e.g. 'read' &/or 'write' self._registries = dict() self._registries["identify"] = dict(attr="_identifiers", column="Auto-identify") self._registries_order = ("identify",) # match keys in `_registries` # If multiple formats are added to one class the update of the docs is quite # expensive. Classes for which the doc update is temporarly delayed are added # to this set. self._delayed_docs_classes = set() @property def available_registries(self): """Available registries. Returns ------- ``dict_keys`` """ return self._registries.keys() def get_formats(self, data_class=None, filter_on=None): """ Get the list of registered formats as a `~astropy.table.Table`. Parameters ---------- data_class : class or None, optional Filter readers/writer to match data class (default = all classes). filter_on : str or None, optional Which registry to show. E.g. "identify" If None search for both. Default is None. Returns ------- format_table : :class:`~astropy.table.Table` Table of available I/O formats. Raises ------ ValueError If ``filter_on`` is not None nor a registry name. """ from astropy.table import Table # set up the column names colnames = ( "Data class", "Format", *[self._registries[k]["column"] for k in self._registries_order], "Deprecated", ) i_dataclass = colnames.index("Data class") i_format = colnames.index("Format") i_regstart = colnames.index( self._registries[self._registries_order[0]]["column"] ) i_deprecated = colnames.index("Deprecated") # registries regs = set() for k in self._registries.keys() - {"identify"}: regs |= set(getattr(self, self._registries[k]["attr"])) format_classes = sorted(regs, key=itemgetter(0)) # the format classes from all registries except "identify" rows = [] for fmt, cls in format_classes: # see if can skip, else need to document in row if data_class is not None and not self._is_best_match( data_class, cls, format_classes ): continue # flags for each registry has_ = { k: "Yes" if (fmt, cls) in getattr(self, v["attr"]) else "No" for k, v in self._registries.items() } # Check if this is a short name (e.g. 'rdb') which is deprecated in # favor of the full 'ascii.rdb'. ascii_format_class = ("ascii." + fmt, cls) # deprecation flag deprecated = "Yes" if ascii_format_class in format_classes else "" # add to rows rows.append( ( cls.__name__, fmt, *[has_[n] for n in self._registries_order], deprecated, ) ) # filter_on can be in self_registries_order or None if str(filter_on).lower() in self._registries_order: index = self._registries_order.index(str(filter_on).lower()) rows = [row for row in rows if row[i_regstart + index] == "Yes"] elif filter_on is not None: raise ValueError( 'unrecognized value for "filter_on": {0}.\n' f"Allowed are {self._registries_order} and None." ) # Sorting the list of tuples is much faster than sorting it after the # table is created. (#5262) if rows: # Indices represent "Data Class", "Deprecated" and "Format". data = list( zip(*sorted(rows, key=itemgetter(i_dataclass, i_deprecated, i_format))) ) else: data = None # make table # need to filter elementwise comparison failure issue # https://github.com/numpy/numpy/issues/6784 with warnings.catch_warnings(): warnings.simplefilter(action="ignore", category=FutureWarning) format_table = Table(data, names=colnames) if not np.any(format_table["Deprecated"].data == "Yes"): format_table.remove_column("Deprecated") return format_table @contextlib.contextmanager def delay_doc_updates(self, cls): """Contextmanager to disable documentation updates when registering reader and writer. The documentation is only built once when the contextmanager exits. .. versionadded:: 1.3 Parameters ---------- cls : class Class for which the documentation updates should be delayed. Notes ----- Registering multiple readers and writers can cause significant overhead because the documentation of the corresponding ``read`` and ``write`` methods are build every time. Examples -------- see for example the source code of ``astropy.table.__init__``. """ self._delayed_docs_classes.add(cls) yield self._delayed_docs_classes.discard(cls) for method in self._registries.keys() - {"identify"}: self._update__doc__(cls, method) # ========================================================================= # Identifier methods def register_identifier(self, data_format, data_class, identifier, force=False): """ Associate an identifier function with a specific data type. Parameters ---------- data_format : str The data format identifier. This is the string that is used to specify the data type when reading/writing. data_class : class The class of the object that can be written. identifier : function A function that checks the argument specified to `read` or `write` to determine whether the input can be interpreted as a table of type ``data_format``. This function should take the following arguments: - ``origin``: A string ``"read"`` or ``"write"`` identifying whether the file is to be opened for reading or writing. - ``path``: The path to the file. - ``fileobj``: An open file object to read the file's contents, or `None` if the file could not be opened. - ``*args``: Positional arguments for the `read` or `write` function. - ``**kwargs``: Keyword arguments for the `read` or `write` function. One or both of ``path`` or ``fileobj`` may be `None`. If they are both `None`, the identifier will need to work from ``args[0]``. The function should return True if the input can be identified as being of format ``data_format``, and False otherwise. force : bool, optional Whether to override any existing function if already present. Default is ``False``. Examples -------- To set the identifier based on extensions, for formats that take a filename as a first argument, you can do for example .. code-block:: python from astropy.io.registry import register_identifier from astropy.table import Table def my_identifier(*args, **kwargs): return isinstance(args[0], str) and args[0].endswith('.tbl') register_identifier('ipac', Table, my_identifier) unregister_identifier('ipac', Table) """ if not (data_format, data_class) in self._identifiers or force: self._identifiers[(data_format, data_class)] = identifier else: raise IORegistryError( f"Identifier for format {data_format!r} and class" f" {data_class.__name__!r} is already defined" ) def unregister_identifier(self, data_format, data_class): """ Unregister an identifier function Parameters ---------- data_format : str The data format identifier. data_class : class The class of the object that can be read/written. """ if (data_format, data_class) in self._identifiers: self._identifiers.pop((data_format, data_class)) else: raise IORegistryError( f"No identifier defined for format {data_format!r} and class" f" {data_class.__name__!r}" ) def identify_format(self, origin, data_class_required, path, fileobj, args, kwargs): """Loop through identifiers to see which formats match. Parameters ---------- origin : str A string ``"read`` or ``"write"`` identifying whether the file is to be opened for reading or writing. data_class_required : object The specified class for the result of `read` or the class that is to be written. path : str or path-like or None The path to the file or None. fileobj : file-like or None. An open file object to read the file's contents, or ``None`` if the file could not be opened. args : sequence Positional arguments for the `read` or `write` function. Note that these must be provided as sequence. kwargs : dict-like Keyword arguments for the `read` or `write` function. Note that this parameter must be `dict`-like. Returns ------- valid_formats : list List of matching formats. """ valid_formats = [] for data_format, data_class in self._identifiers: if self._is_best_match(data_class_required, data_class, self._identifiers): if self._identifiers[(data_format, data_class)]( origin, path, fileobj, *args, **kwargs ): valid_formats.append(data_format) return valid_formats # ========================================================================= # Utils def _get_format_table_str(self, data_class, filter_on): """``get_formats()``, without column "Data class", as a str.""" format_table = self.get_formats(data_class, filter_on) format_table.remove_column("Data class") format_table_str = "\n".join(format_table.pformat(max_lines=-1)) return format_table_str def _is_best_match(self, class1, class2, format_classes): """ Determine if class2 is the "best" match for class1 in the list of classes. It is assumed that (class2 in classes) is True. class2 is the the best match if: - ``class1`` is a subclass of ``class2`` AND - ``class2`` is the nearest ancestor of ``class1`` that is in classes (which includes the case that ``class1 is class2``) """ if issubclass(class1, class2): classes = {cls for fmt, cls in format_classes} for parent in class1.__mro__: if parent is class2: # class2 is closest registered ancestor return True if parent in classes: # class2 was superceded return False return False def _get_valid_format(self, mode, cls, path, fileobj, args, kwargs): """ Returns the first valid format that can be used to read/write the data in question. Mode can be either 'read' or 'write'. """ valid_formats = self.identify_format(mode, cls, path, fileobj, args, kwargs) if len(valid_formats) == 0: format_table_str = self._get_format_table_str(cls, mode.capitalize()) raise IORegistryError( "Format could not be identified based on the" " file name or contents, please provide a" " 'format' argument.\n" f"The available formats are:\n{format_table_str}" ) elif len(valid_formats) > 1: return self._get_highest_priority_format(mode, cls, valid_formats) return valid_formats[0] def _get_highest_priority_format(self, mode, cls, valid_formats): """ Returns the reader or writer with the highest priority. If it is a tie, error. """ if mode == "read": format_dict = self._readers mode_loader = "reader" elif mode == "write": format_dict = self._writers mode_loader = "writer" best_formats = [] current_priority = -np.inf for format in valid_formats: try: _, priority = format_dict[(format, cls)] except KeyError: # We could throw an exception here, but get_reader/get_writer handle # this case better, instead maximally deprioritise the format. priority = -np.inf if priority == current_priority: best_formats.append(format) elif priority > current_priority: best_formats = [format] current_priority = priority if len(best_formats) > 1: raise IORegistryError( "Format is ambiguous - options are:" f" {', '.join(sorted(valid_formats, key=itemgetter(0)))}" ) return best_formats[0] def _update__doc__(self, data_class, readwrite): """ Update the docstring to include all the available readers / writers for the ``data_class.read``/``data_class.write`` functions (respectively). Don't update if the data_class does not have the relevant method. """ # abort if method "readwrite" isn't on data_class if not hasattr(data_class, readwrite): return from .interface import UnifiedReadWrite FORMATS_TEXT = "The available built-in formats are:" # Get the existing read or write method and its docstring class_readwrite_func = getattr(data_class, readwrite) if not isinstance(class_readwrite_func.__doc__, str): # No docstring--could just be test code, or possibly code compiled # without docstrings return lines = class_readwrite_func.__doc__.splitlines() # Find the location of the existing formats table if it exists sep_indices = [ii for ii, line in enumerate(lines) if FORMATS_TEXT in line] if sep_indices: # Chop off the existing formats table, including the initial blank line chop_index = sep_indices[0] lines = lines[:chop_index] # Find the minimum indent, skipping the first line because it might be odd matches = [re.search(r"(\S)", line) for line in lines[1:]] left_indent = " " * min(match.start() for match in matches if match) # Get the available unified I/O formats for this class # Include only formats that have a reader, and drop the 'Data class' column format_table = self.get_formats(data_class, readwrite.capitalize()) format_table.remove_column("Data class") # Get the available formats as a table, then munge the output of pformat() # a bit and put it into the docstring. new_lines = format_table.pformat(max_lines=-1, max_width=80) table_rst_sep = re.sub("-", "=", new_lines[1]) new_lines[1] = table_rst_sep new_lines.insert(0, table_rst_sep) new_lines.append(table_rst_sep) # Check for deprecated names and include a warning at the end. if "Deprecated" in format_table.colnames: new_lines.extend( [ "", "Deprecated format names like ``aastex`` will be " "removed in a future version. Use the full ", "name (e.g. ``ascii.aastex``) instead.", ] ) new_lines = [FORMATS_TEXT, ""] + new_lines lines.extend([left_indent + line for line in new_lines]) # Depending on Python version and whether class_readwrite_func is # an instancemethod or classmethod, one of the following will work. if isinstance(class_readwrite_func, UnifiedReadWrite): class_readwrite_func.__class__.__doc__ = "\n".join(lines) else: try: class_readwrite_func.__doc__ = "\n".join(lines) except AttributeError: class_readwrite_func.__func__.__doc__ = "\n".join(lines)
38453102c4396f7fafe38f884a34df032a791b5b78e3f4ad140a974f8e5ef1bf
# Licensed under a 3-clause BSD style license - see LICENSE.rst import inspect import os import re from .base import IORegistryError __all__ = ["UnifiedReadWriteMethod", "UnifiedReadWrite"] # ----------------------------------------------------------------------------- class UnifiedReadWrite: """Base class for the worker object used in unified read() or write() methods. This lightweight object is created for each `read()` or `write()` call via ``read`` / ``write`` descriptors on the data object class. The key driver is to allow complete format-specific documentation of available method options via a ``help()`` method, e.g. ``Table.read.help('fits')``. Subclasses must define a ``__call__`` method which is what actually gets called when the data object ``read()`` or ``write()`` method is called. For the canonical example see the `~astropy.table.Table` class implementation (in particular the ``connect.py`` module there). Parameters ---------- instance : object Descriptor calling instance or None if no instance cls : type Descriptor calling class (either owner class or instance class) method_name : str Method name, e.g. 'read' or 'write' registry : ``_UnifiedIORegistryBase`` or None, optional The IO registry. """ def __init__(self, instance, cls, method_name, registry=None): if registry is None: from astropy.io.registry.compat import default_registry as registry self._registry = registry self._instance = instance self._cls = cls self._method_name = method_name # 'read' or 'write' @property def registry(self): """Unified I/O registry instance.""" return self._registry def help(self, format=None, out=None): """Output help documentation for the specified unified I/O ``format``. By default the help output is printed to the console via ``pydoc.pager``. Instead one can supplied a file handle object as ``out`` and the output will be written to that handle. Parameters ---------- format : str Unified I/O format name, e.g. 'fits' or 'ascii.ecsv' out : None or path-like Output destination (default is stdout via a pager) """ cls = self._cls method_name = self._method_name # Get reader or writer function associated with the registry get_func = ( self._registry.get_reader if method_name == "read" else self._registry.get_writer ) try: if format: read_write_func = get_func(format, cls) except IORegistryError as err: reader_doc = "ERROR: " + str(err) else: if format: # Format-specific header = ( f"{cls.__name__}.{method_name}(format='{format}') documentation\n" ) doc = read_write_func.__doc__ else: # General docs header = f"{cls.__name__}.{method_name} general documentation\n" doc = getattr(cls, method_name).__doc__ reader_doc = re.sub(".", "=", header) reader_doc += header reader_doc += re.sub(".", "=", header) reader_doc += os.linesep if doc is not None: reader_doc += inspect.cleandoc(doc) if out is None: import pydoc pydoc.pager(reader_doc) else: out.write(reader_doc) def list_formats(self, out=None): """Print a list of available formats to console (or ``out`` filehandle) out : None or file handle object Output destination (default is stdout via a pager) """ tbl = self._registry.get_formats(self._cls, self._method_name.capitalize()) del tbl["Data class"] if out is None: tbl.pprint(max_lines=-1, max_width=-1) else: out.write("\n".join(tbl.pformat(max_lines=-1, max_width=-1))) return out # ----------------------------------------------------------------------------- class UnifiedReadWriteMethod(property): """Descriptor class for creating read() and write() methods in unified I/O. The canonical example is in the ``Table`` class, where the ``connect.py`` module creates subclasses of the ``UnifiedReadWrite`` class. These have custom ``__call__`` methods that do the setup work related to calling the registry read() or write() functions. With this, the ``Table`` class defines read and write methods as follows:: read = UnifiedReadWriteMethod(TableRead) write = UnifiedReadWriteMethod(TableWrite) Parameters ---------- func : `~astropy.io.registry.UnifiedReadWrite` subclass Class that defines read or write functionality """ # We subclass property to ensure that __set__ is defined and that, # therefore, we are a data descriptor, which cannot be overridden. # This also means we automatically inherit the __doc__ of fget (which will # be a UnifiedReadWrite subclass), and that this docstring gets recognized # and properly typeset by sphinx (which was previously an issue; see # gh-11554). # We override __get__ to pass both instance and class to UnifiedReadWrite. def __get__(self, instance, owner_cls): return self.fget(instance, owner_cls)
c35b604661ea32491cf275cd1c15f20626516d156153b9e825c90d1cc612c555
# Licensed under a 3-clause BSD style license - see LICENSE.rst import functools from .core import UnifiedIORegistry __all__ = [ "register_reader", "register_writer", "register_identifier", "unregister_reader", "unregister_writer", "unregister_identifier", "get_reader", "get_writer", "get_formats", "read", "write", "identify_format", "delay_doc_updates", ] # make a default global-state registry (not publicly scoped, but often accessed) # this is for backward compatibility when ``io.registry`` was a file. default_registry = UnifiedIORegistry() # also need to expose the enclosed registries _identifiers = default_registry._identifiers _readers = default_registry._readers _writers = default_registry._writers def _make_io_func(method_name): """Makes a function for a method on UnifiedIORegistry. .. todo:: Make kwarg "registry" not hidden. Returns ------- wrapper : callable Signature matches method on UnifiedIORegistry. Accepts (hidden) kwarg "registry". default is ``default_registry``. """ @functools.wraps(getattr(default_registry, method_name)) def wrapper(*args, registry=None, **kwargs): # written this way in case ever controlled by ScienceState if registry is None: registry = default_registry # get and call bound method from registry instance return getattr(registry, method_name)(*args, **kwargs) return wrapper # ============================================================================= # JIT function creation and lookup (PEP 562) def __dir__(): dir_out = list(globals()) return sorted(dir_out + __all__) def __getattr__(method: str): if method in __all__: return _make_io_func(method) raise AttributeError(f"module {__name__!r} has no attribute {method!r}")
19c3bd9672efaae155d5309b07e2b147747b0c4ae43b58ebca2aa741073c753a
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ This module handles the conversion of various VOTABLE datatypes to/from TABLEDATA_ and BINARY_ formats. """ # STDLIB import re import sys from struct import pack as _struct_pack from struct import unpack as _struct_unpack # THIRD-PARTY import numpy as np from numpy import ma # ASTROPY from astropy.utils.xml.writer import xml_escape_cdata # LOCAL from .exceptions import ( E01, E02, E03, E04, E05, E06, E24, W01, W30, W31, W39, W46, W47, W49, W51, W55, vo_raise, vo_warn, warn_or_raise, ) __all__ = ["get_converter", "Converter", "table_column_to_votable_datatype"] pedantic_array_splitter = re.compile(r" +") array_splitter = re.compile(r"\s+|(?:\s*,\s*)") """ A regex to handle splitting values on either whitespace or commas. SPEC: Usage of commas is not actually allowed by the spec, but many files in the wild use them. """ _zero_int = b"\0\0\0\0" _empty_bytes = b"" _zero_byte = b"\0" struct_unpack = _struct_unpack struct_pack = _struct_pack if sys.byteorder == "little": def _ensure_bigendian(x): if x.dtype.byteorder != ">": return x.byteswap() return x else: def _ensure_bigendian(x): if x.dtype.byteorder == "<": return x.byteswap() return x def _make_masked_array(data, mask): """ Masked arrays of zero length that also have a mask of zero length cause problems in Numpy (at least in 1.6.2). This function creates a masked array from data and a mask, unless it is zero length. """ # np.ma doesn't like setting mask to [] if len(data): return ma.array(np.array(data), mask=np.array(mask, dtype="bool")) else: return ma.array(np.array(data)) def bitarray_to_bool(data, length): """ Converts a bit array (a string of bits in a bytes object) to a boolean Numpy array. Parameters ---------- data : bytes The bit array. The most significant byte is read first. length : int The number of bits to read. The least significant bits in the data bytes beyond length will be ignored. Returns ------- array : numpy bool array """ results = [] for byte in data: for bit_no in range(7, -1, -1): bit = byte & (1 << bit_no) bit = bit != 0 results.append(bit) if len(results) == length: break if len(results) == length: break return np.array(results, dtype="b1") def bool_to_bitarray(value): """ Converts a numpy boolean array to a bit array (a string of bits in a bytes object). Parameters ---------- value : numpy bool array Returns ------- bit_array : bytes The first value in the input array will be the most significant bit in the result. The length will be `floor((N + 7) / 8)` where `N` is the length of `value`. """ value = value.flat bit_no = 7 byte = 0 bytes = [] for v in value: if v: byte |= 1 << bit_no if bit_no == 0: bytes.append(byte) bit_no = 7 byte = 0 else: bit_no -= 1 if bit_no != 7: bytes.append(byte) return struct_pack(f"{len(bytes)}B", *bytes) class Converter: """ The base class for all converters. Each subclass handles converting a specific VOTABLE data type to/from the TABLEDATA_ and BINARY_ on-disk representations. Parameters ---------- field : `~astropy.io.votable.tree.Field` object describing the datatype config : dict The parser configuration dictionary pos : tuple The position in the XML file where the FIELD object was found. Used for error messages. """ def __init__(self, field, config=None, pos=None): pass @staticmethod def _parse_length(read): return struct_unpack(">I", read(4))[0] @staticmethod def _write_length(length): return struct_pack(">I", int(length)) def supports_empty_values(self, config): """ Returns True when the field can be completely empty. """ return config.get("version_1_3_or_later") def parse(self, value, config=None, pos=None): """ Convert the string *value* from the TABLEDATA_ format into an object with the correct native in-memory datatype and mask flag. Parameters ---------- value : str value in TABLEDATA format Returns ------- native : tuple A two-element tuple of: value, mask. The value as a Numpy array or scalar, and *mask* is True if the value is missing. """ raise NotImplementedError("This datatype must implement a 'parse' method.") def parse_scalar(self, value, config=None, pos=None): """ Parse a single scalar of the underlying type of the converter. For non-array converters, this is equivalent to parse. For array converters, this is used to parse a single element of the array. Parameters ---------- value : str value in TABLEDATA format Returns ------- native : (2,) tuple (value, mask) The value as a Numpy array or scalar, and *mask* is True if the value is missing. """ return self.parse(value, config, pos) def output(self, value, mask): """ Convert the object *value* (in the native in-memory datatype) to a unicode string suitable for serializing in the TABLEDATA_ format. Parameters ---------- value The value, the native type corresponding to this converter mask : bool If `True`, will return the string representation of a masked value. Returns ------- tabledata_repr : unicode """ raise NotImplementedError("This datatype must implement a 'output' method.") def binparse(self, read): """ Reads some number of bytes from the BINARY_ format representation by calling the function *read*, and returns the native in-memory object representation for the datatype handled by *self*. Parameters ---------- read : function A function that given a number of bytes, returns a byte string. Returns ------- native : (2,) tuple (value, mask). The value as a Numpy array or scalar, and *mask* is True if the value is missing. """ raise NotImplementedError("This datatype must implement a 'binparse' method.") def binoutput(self, value, mask): """ Convert the object *value* in the native in-memory datatype to a string of bytes suitable for serialization in the BINARY_ format. Parameters ---------- value The value, the native type corresponding to this converter mask : bool If `True`, will return the string representation of a masked value. Returns ------- bytes : bytes The binary representation of the value, suitable for serialization in the BINARY_ format. """ raise NotImplementedError("This datatype must implement a 'binoutput' method.") class Char(Converter): """ Handles the char datatype. (7-bit unsigned characters) Missing values are not handled for string or unicode types. """ default = _empty_bytes def __init__(self, field, config=None, pos=None): if config is None: config = {} Converter.__init__(self, field, config, pos) self.field_name = field.name if field.arraysize is None: vo_warn(W47, (), config, pos) field.arraysize = "1" if field.arraysize == "*": self.format = "O" self.binparse = self._binparse_var self.binoutput = self._binoutput_var self.arraysize = "*" else: if field.arraysize.endswith("*"): field.arraysize = field.arraysize[:-1] try: self.arraysize = int(field.arraysize) except ValueError: vo_raise(E01, (field.arraysize, "char", field.ID), config) self.format = f"U{self.arraysize:d}" self.binparse = self._binparse_fixed self.binoutput = self._binoutput_fixed self._struct_format = f">{self.arraysize:d}s" def supports_empty_values(self, config): return True def parse(self, value, config=None, pos=None): if self.arraysize != "*" and len(value) > self.arraysize: vo_warn(W46, ("char", self.arraysize), config, pos) # Warn about non-ascii characters if warnings are enabled. try: value.encode("ascii") except UnicodeEncodeError: vo_warn(W55, (self.field_name, value), config, pos) return value, False def output(self, value, mask): if mask: return "" # The output methods for Char assume that value is either str or bytes. # This method needs to return a str, but needs to warn if the str contains # non-ASCII characters. try: if isinstance(value, str): value.encode("ascii") else: # Check for non-ASCII chars in the bytes object. value = value.decode("ascii") except (ValueError, UnicodeEncodeError): warn_or_raise(E24, UnicodeEncodeError, (value, self.field_name)) finally: if isinstance(value, bytes): # Convert the bytes to str regardless of non-ASCII chars. value = value.decode("utf-8") return xml_escape_cdata(value) def _binparse_var(self, read): length = self._parse_length(read) return read(length).decode("ascii"), False def _binparse_fixed(self, read): s = struct_unpack(self._struct_format, read(self.arraysize))[0] end = s.find(_zero_byte) s = s.decode("ascii") if end != -1: return s[:end], False return s, False def _binoutput_var(self, value, mask): if mask or value is None or value == "": return _zero_int if isinstance(value, str): try: value = value.encode("ascii") except ValueError: vo_raise(E24, (value, self.field_name)) return self._write_length(len(value)) + value def _binoutput_fixed(self, value, mask): if mask: value = _empty_bytes elif isinstance(value, str): try: value = value.encode("ascii") except ValueError: vo_raise(E24, (value, self.field_name)) return struct_pack(self._struct_format, value) class UnicodeChar(Converter): """ Handles the unicodeChar data type. UTF-16-BE. Missing values are not handled for string or unicode types. """ default = "" def __init__(self, field, config=None, pos=None): Converter.__init__(self, field, config, pos) if field.arraysize is None: vo_warn(W47, (), config, pos) field.arraysize = "1" if field.arraysize == "*": self.format = "O" self.binparse = self._binparse_var self.binoutput = self._binoutput_var self.arraysize = "*" else: try: self.arraysize = int(field.arraysize) except ValueError: vo_raise(E01, (field.arraysize, "unicode", field.ID), config) self.format = f"U{self.arraysize:d}" self.binparse = self._binparse_fixed self.binoutput = self._binoutput_fixed self._struct_format = f">{self.arraysize*2:d}s" def parse(self, value, config=None, pos=None): if self.arraysize != "*" and len(value) > self.arraysize: vo_warn(W46, ("unicodeChar", self.arraysize), config, pos) return value, False def output(self, value, mask): if mask: return "" return xml_escape_cdata(str(value)) def _binparse_var(self, read): length = self._parse_length(read) return read(length * 2).decode("utf_16_be"), False def _binparse_fixed(self, read): s = struct_unpack(self._struct_format, read(self.arraysize * 2))[0] s = s.decode("utf_16_be") end = s.find("\0") if end != -1: return s[:end], False return s, False def _binoutput_var(self, value, mask): if mask or value is None or value == "": return _zero_int encoded = value.encode("utf_16_be") return self._write_length(len(encoded) / 2) + encoded def _binoutput_fixed(self, value, mask): if mask: value = "" return struct_pack(self._struct_format, value.encode("utf_16_be")) class Array(Converter): """ Handles both fixed and variable-lengths arrays. """ def __init__(self, field, config=None, pos=None): if config is None: config = {} Converter.__init__(self, field, config, pos) if config.get("verify", "ignore") == "exception": self._splitter = self._splitter_pedantic else: self._splitter = self._splitter_lax def parse_scalar(self, value, config=None, pos=0): return self._base.parse_scalar(value, config, pos) @staticmethod def _splitter_pedantic(value, config=None, pos=None): return pedantic_array_splitter.split(value) @staticmethod def _splitter_lax(value, config=None, pos=None): if "," in value: vo_warn(W01, (), config, pos) return array_splitter.split(value) class VarArray(Array): """ Handles variable lengths arrays (i.e. where *arraysize* is '*'). """ format = "O" def __init__(self, field, base, arraysize, config=None, pos=None): Array.__init__(self, field, config) self._base = base self.default = np.array([], dtype=self._base.format) def output(self, value, mask): output = self._base.output result = [output(x, m) for x, m in np.broadcast(value, mask)] return " ".join(result) def binparse(self, read): length = self._parse_length(read) result = [] result_mask = [] binparse = self._base.binparse for i in range(length): val, mask = binparse(read) result.append(val) result_mask.append(mask) return _make_masked_array(result, result_mask), False def binoutput(self, value, mask): if value is None or len(value) == 0: return _zero_int length = len(value) result = [self._write_length(length)] binoutput = self._base.binoutput for x, m in zip(value, value.mask): result.append(binoutput(x, m)) return _empty_bytes.join(result) class ArrayVarArray(VarArray): """ Handles an array of variable-length arrays, i.e. where *arraysize* ends in '*'. """ def parse(self, value, config=None, pos=None): if value.strip() == "": return ma.array([]), False parts = self._splitter(value, config, pos) items = self._base._items parse_parts = self._base.parse_parts if len(parts) % items != 0: vo_raise(E02, (items, len(parts)), config, pos) result = [] result_mask = [] for i in range(0, len(parts), items): value, mask = parse_parts(parts[i : i + items], config, pos) result.append(value) result_mask.append(mask) return _make_masked_array(result, result_mask), False class ScalarVarArray(VarArray): """ Handles a variable-length array of numeric scalars. """ def parse(self, value, config=None, pos=None): if value.strip() == "": return ma.array([]), False parts = self._splitter(value, config, pos) parse = self._base.parse result = [] result_mask = [] for x in parts: value, mask = parse(x, config, pos) result.append(value) result_mask.append(mask) return _make_masked_array(result, result_mask), False class NumericArray(Array): """ Handles a fixed-length array of numeric scalars. """ vararray_type = ArrayVarArray def __init__(self, field, base, arraysize, config=None, pos=None): Array.__init__(self, field, config, pos) self._base = base self._arraysize = arraysize self.format = f"{tuple(arraysize)}{base.format}" self._items = 1 for dim in arraysize: self._items *= dim self._memsize = np.dtype(self.format).itemsize self._bigendian_format = ">" + self.format self.default = np.empty(arraysize, dtype=self._base.format) self.default[...] = self._base.default def parse(self, value, config=None, pos=None): if config is None: config = {} elif config["version_1_3_or_later"] and value == "": return np.zeros(self._arraysize, dtype=self._base.format), True parts = self._splitter(value, config, pos) if len(parts) != self._items: warn_or_raise(E02, E02, (self._items, len(parts)), config, pos) if config.get("verify", "ignore") == "exception": return self.parse_parts(parts, config, pos) else: if len(parts) == self._items: pass elif len(parts) > self._items: parts = parts[: self._items] else: parts = parts + ([self._base.default] * (self._items - len(parts))) return self.parse_parts(parts, config, pos) def parse_parts(self, parts, config=None, pos=None): base_parse = self._base.parse result = [] result_mask = [] for x in parts: value, mask = base_parse(x, config, pos) result.append(value) result_mask.append(mask) result = np.array(result, dtype=self._base.format).reshape(self._arraysize) result_mask = np.array(result_mask, dtype="bool").reshape(self._arraysize) return result, result_mask def output(self, value, mask): base_output = self._base.output value = np.asarray(value) mask = np.asarray(mask) if mask.size <= 1: func = np.broadcast else: # When mask is already array but value is scalar, this prevents broadcast func = zip return " ".join(base_output(x, m) for x, m in func(value.flat, mask.flat)) def binparse(self, read): result = np.frombuffer(read(self._memsize), dtype=self._bigendian_format)[0] result_mask = self._base.is_null(result) return result, result_mask def binoutput(self, value, mask): filtered = self._base.filter_array(value, mask) filtered = _ensure_bigendian(filtered) return filtered.tobytes() class Numeric(Converter): """ The base class for all numeric data types. """ array_type = NumericArray vararray_type = ScalarVarArray null = None def __init__(self, field, config=None, pos=None): Converter.__init__(self, field, config, pos) self._memsize = np.dtype(self.format).itemsize self._bigendian_format = ">" + self.format if field.values.null is not None: self.null = np.asarray(field.values.null, dtype=self.format) self.default = self.null self.is_null = self._is_null else: self.is_null = np.isnan def binparse(self, read): result = np.frombuffer(read(self._memsize), dtype=self._bigendian_format) return result[0], self.is_null(result[0]) def _is_null(self, value): return value == self.null class FloatingPoint(Numeric): """ The base class for floating-point datatypes. """ default = np.nan def __init__(self, field, config=None, pos=None): if config is None: config = {} Numeric.__init__(self, field, config, pos) precision = field.precision width = field.width if precision is None: format_parts = ["{!r:>"] else: format_parts = ["{:"] if width is not None: format_parts.append(str(width)) if precision is not None: if precision.startswith("E"): format_parts.append(f".{int(precision[1:]):d}g") elif precision.startswith("F"): format_parts.append(f".{int(precision[1:]):d}f") else: format_parts.append(f".{int(precision):d}f") format_parts.append("}") self._output_format = "".join(format_parts) self.nan = np.array(np.nan, self.format) if self.null is None: self._null_output = "NaN" self._null_binoutput = self.binoutput(self.nan, False) self.filter_array = self._filter_nan else: self._null_output = self.output(np.asarray(self.null), False) self._null_binoutput = self.binoutput(np.asarray(self.null), False) self.filter_array = self._filter_null if config.get("verify", "ignore") == "exception": self.parse = self._parse_pedantic else: self.parse = self._parse_permissive def supports_empty_values(self, config): return True def _parse_pedantic(self, value, config=None, pos=None): if value.strip() == "": return self.null, True f = float(value) return f, self.is_null(f) def _parse_permissive(self, value, config=None, pos=None): try: f = float(value) return f, self.is_null(f) except ValueError: # IRSA VOTables use the word 'null' to specify empty values, # but this is not defined in the VOTable spec. if value.strip() != "": vo_warn(W30, value, config, pos) return self.null, True @property def output_format(self): return self._output_format def output(self, value, mask): if mask: return self._null_output if np.isfinite(value): if not np.isscalar(value): value = value.dtype.type(value) result = self._output_format.format(value) if result.startswith("array"): raise RuntimeError() if self._output_format[2] == "r" and result.endswith(".0"): result = result[:-2] return result elif np.isnan(value): return "NaN" elif np.isposinf(value): return "+InF" elif np.isneginf(value): return "-InF" # Should never raise vo_raise(f"Invalid floating point value '{value}'") def binoutput(self, value, mask): if mask: return self._null_binoutput value = _ensure_bigendian(value) return value.tobytes() def _filter_nan(self, value, mask): return np.where(mask, np.nan, value) def _filter_null(self, value, mask): return np.where(mask, self.null, value) class Double(FloatingPoint): """ Handles the double datatype. Double-precision IEEE floating-point. """ format = "f8" class Float(FloatingPoint): """ Handles the float datatype. Single-precision IEEE floating-point. """ format = "f4" class Integer(Numeric): """ The base class for all the integral datatypes. """ default = 0 def __init__(self, field, config=None, pos=None): Numeric.__init__(self, field, config, pos) def parse(self, value, config=None, pos=None): if config is None: config = {} mask = False if isinstance(value, str): value = value.lower() if value == "": if config["version_1_3_or_later"]: mask = True else: warn_or_raise(W49, W49, (), config, pos) if self.null is not None: value = self.null else: value = self.default elif value == "nan": mask = True if self.null is None: warn_or_raise(W31, W31, (), config, pos) value = self.default else: value = self.null elif value.startswith("0x"): value = int(value[2:], 16) else: value = int(value, 10) else: value = int(value) if self.null is not None and value == self.null: mask = True if value < self.val_range[0]: warn_or_raise(W51, W51, (value, self.bit_size), config, pos) value = self.val_range[0] elif value > self.val_range[1]: warn_or_raise(W51, W51, (value, self.bit_size), config, pos) value = self.val_range[1] return value, mask def output(self, value, mask): if mask: if self.null is None: warn_or_raise(W31, W31) return "NaN" return str(self.null) return str(value) def binoutput(self, value, mask): if mask: if self.null is None: vo_raise(W31) else: value = self.null value = _ensure_bigendian(value) return value.tobytes() def filter_array(self, value, mask): if np.any(mask): if self.null is not None: return np.where(mask, self.null, value) else: vo_raise(W31) return value class UnsignedByte(Integer): """ Handles the unsignedByte datatype. Unsigned 8-bit integer. """ format = "u1" val_range = (0, 255) bit_size = "8-bit unsigned" class Short(Integer): """ Handles the short datatype. Signed 16-bit integer. """ format = "i2" val_range = (-32768, 32767) bit_size = "16-bit" class Int(Integer): """ Handles the int datatype. Signed 32-bit integer. """ format = "i4" val_range = (-2147483648, 2147483647) bit_size = "32-bit" class Long(Integer): """ Handles the long datatype. Signed 64-bit integer. """ format = "i8" val_range = (-9223372036854775808, 9223372036854775807) bit_size = "64-bit" class ComplexArrayVarArray(VarArray): """ Handles an array of variable-length arrays of complex numbers. """ def parse(self, value, config=None, pos=None): if value.strip() == "": return ma.array([]), True parts = self._splitter(value, config, pos) items = self._base._items parse_parts = self._base.parse_parts if len(parts) % items != 0: vo_raise(E02, (items, len(parts)), config, pos) result = [] result_mask = [] for i in range(0, len(parts), items): value, mask = parse_parts(parts[i : i + items], config, pos) result.append(value) result_mask.append(mask) return _make_masked_array(result, result_mask), False class ComplexVarArray(VarArray): """ Handles a variable-length array of complex numbers. """ def parse(self, value, config=None, pos=None): if value.strip() == "": return ma.array([]), True parts = self._splitter(value, config, pos) parse_parts = self._base.parse_parts result = [] result_mask = [] for i in range(0, len(parts), 2): value = [float(x) for x in parts[i : i + 2]] value, mask = parse_parts(value, config, pos) result.append(value) result_mask.append(mask) return ( _make_masked_array(np.array(result, dtype=self._base.format), result_mask), False, ) class ComplexArray(NumericArray): """ Handles a fixed-size array of complex numbers. """ vararray_type = ComplexArrayVarArray def __init__(self, field, base, arraysize, config=None, pos=None): NumericArray.__init__(self, field, base, arraysize, config, pos) self._items *= 2 def parse(self, value, config=None, pos=None): parts = self._splitter(value, config, pos) if parts == [""]: parts = [] return self.parse_parts(parts, config, pos) def parse_parts(self, parts, config=None, pos=None): if len(parts) != self._items: vo_raise(E02, (self._items, len(parts)), config, pos) base_parse = self._base.parse_parts result = [] result_mask = [] for i in range(0, self._items, 2): value = [float(x) for x in parts[i : i + 2]] value, mask = base_parse(value, config, pos) result.append(value) result_mask.append(mask) result = np.array(result, dtype=self._base.format).reshape(self._arraysize) result_mask = np.array(result_mask, dtype="bool").reshape(self._arraysize) return result, result_mask class Complex(FloatingPoint, Array): """ The base class for complex numbers. """ array_type = ComplexArray vararray_type = ComplexVarArray default = np.nan def __init__(self, field, config=None, pos=None): FloatingPoint.__init__(self, field, config, pos) Array.__init__(self, field, config, pos) def parse(self, value, config=None, pos=None): stripped = value.strip() if stripped == "" or stripped.lower() == "nan": return np.nan, True splitter = self._splitter parts = [float(x) for x in splitter(value, config, pos)] if len(parts) != 2: vo_raise(E03, (value,), config, pos) return self.parse_parts(parts, config, pos) _parse_permissive = parse _parse_pedantic = parse def parse_parts(self, parts, config=None, pos=None): value = complex(*parts) return value, self.is_null(value) def output(self, value, mask): if mask: if self.null is None: return "NaN" else: value = self.null real = self._output_format.format(float(value.real)) imag = self._output_format.format(float(value.imag)) if self._output_format[2] == "r": if real.endswith(".0"): real = real[:-2] if imag.endswith(".0"): imag = imag[:-2] return real + " " + imag class FloatComplex(Complex): """ Handle floatComplex datatype. Pair of single-precision IEEE floating-point numbers. """ format = "c8" class DoubleComplex(Complex): """ Handle doubleComplex datatype. Pair of double-precision IEEE floating-point numbers. """ format = "c16" class BitArray(NumericArray): """ Handles an array of bits. """ vararray_type = ArrayVarArray def __init__(self, field, base, arraysize, config=None, pos=None): NumericArray.__init__(self, field, base, arraysize, config, pos) self._bytes = ((self._items - 1) // 8) + 1 @staticmethod def _splitter_pedantic(value, config=None, pos=None): return list(re.sub(r"\s", "", value)) @staticmethod def _splitter_lax(value, config=None, pos=None): if "," in value: vo_warn(W01, (), config, pos) return list(re.sub(r"\s|,", "", value)) def output(self, value, mask): if np.any(mask): vo_warn(W39) value = np.asarray(value) mapping = {False: "0", True: "1"} return "".join(mapping[x] for x in value.flat) def binparse(self, read): data = read(self._bytes) result = bitarray_to_bool(data, self._items) result = result.reshape(self._arraysize) result_mask = np.zeros(self._arraysize, dtype="b1") return result, result_mask def binoutput(self, value, mask): if np.any(mask): vo_warn(W39) return bool_to_bitarray(value) class Bit(Converter): """ Handles the bit datatype. """ format = "b1" array_type = BitArray vararray_type = ScalarVarArray default = False binary_one = b"\x08" binary_zero = b"\0" def parse(self, value, config=None, pos=None): if config is None: config = {} mapping = {"1": True, "0": False} if value is False or value.strip() == "": if not config["version_1_3_or_later"]: warn_or_raise(W49, W49, (), config, pos) return False, True else: try: return mapping[value], False except KeyError: vo_raise(E04, (value,), config, pos) def output(self, value, mask): if mask: vo_warn(W39) if value: return "1" else: return "0" def binparse(self, read): data = read(1) return (ord(data) & 0x8) != 0, False def binoutput(self, value, mask): if mask: vo_warn(W39) if value: return self.binary_one return self.binary_zero class BooleanArray(NumericArray): """ Handles an array of boolean values. """ vararray_type = ArrayVarArray def binparse(self, read): data = read(self._items) binparse = self._base.binparse_value result = [] result_mask = [] for char in data: value, mask = binparse(char) result.append(value) result_mask.append(mask) result = np.array(result, dtype="b1").reshape(self._arraysize) result_mask = np.array(result_mask, dtype="b1").reshape(self._arraysize) return result, result_mask def binoutput(self, value, mask): binoutput = self._base.binoutput value = np.asarray(value) mask = np.asarray(mask) result = [binoutput(x, m) for x, m in np.broadcast(value.flat, mask.flat)] return _empty_bytes.join(result) class Boolean(Converter): """ Handles the boolean datatype. """ format = "b1" array_type = BooleanArray vararray_type = ScalarVarArray default = False binary_question_mark = b"?" binary_true = b"T" binary_false = b"F" def parse(self, value, config=None, pos=None): if value == "": return False, True if value is False: return False, True mapping = { "TRUE": (True, False), "FALSE": (False, False), "1": (True, False), "0": (False, False), "T": (True, False), "F": (False, False), "\0": (False, True), " ": (False, True), "?": (False, True), "": (False, True), } try: return mapping[value.upper()] except KeyError: vo_raise(E05, (value,), config, pos) def output(self, value, mask): if mask: return "?" if value: return "T" return "F" def binparse(self, read): value = ord(read(1)) return self.binparse_value(value) _binparse_mapping = { ord("T"): (True, False), ord("t"): (True, False), ord("1"): (True, False), ord("F"): (False, False), ord("f"): (False, False), ord("0"): (False, False), ord("\0"): (False, True), ord(" "): (False, True), ord("?"): (False, True), } def binparse_value(self, value): try: return self._binparse_mapping[value] except KeyError: vo_raise(E05, (value,)) def binoutput(self, value, mask): if mask: return self.binary_question_mark if value: return self.binary_true return self.binary_false converter_mapping = { "double": Double, "float": Float, "bit": Bit, "boolean": Boolean, "unsignedByte": UnsignedByte, "short": Short, "int": Int, "long": Long, "floatComplex": FloatComplex, "doubleComplex": DoubleComplex, "char": Char, "unicodeChar": UnicodeChar, } def get_converter(field, config=None, pos=None): """ Get an appropriate converter instance for a given field. Parameters ---------- field : astropy.io.votable.tree.Field config : dict, optional Parser configuration dictionary pos : tuple Position in the input XML file. Used for error messages. Returns ------- converter : astropy.io.votable.converters.Converter """ if config is None: config = {} if field.datatype not in converter_mapping: vo_raise(E06, (field.datatype, field.ID), config) cls = converter_mapping[field.datatype] converter = cls(field, config, pos) arraysize = field.arraysize # With numeric datatypes, special things need to happen for # arrays. if field.datatype not in ("char", "unicodeChar") and arraysize is not None: if arraysize[-1] == "*": arraysize = arraysize[:-1] last_x = arraysize.rfind("x") if last_x == -1: arraysize = "" else: arraysize = arraysize[:last_x] fixed = False else: fixed = True if arraysize != "": arraysize = [int(x) for x in arraysize.split("x")] arraysize.reverse() else: arraysize = [] if arraysize != []: converter = converter.array_type(field, converter, arraysize, config) if not fixed: converter = converter.vararray_type(field, converter, arraysize, config) return converter numpy_dtype_to_field_mapping = { np.float64().dtype.num: "double", np.float32().dtype.num: "float", np.bool_().dtype.num: "bit", np.uint8().dtype.num: "unsignedByte", np.int16().dtype.num: "short", np.int32().dtype.num: "int", np.int64().dtype.num: "long", np.complex64().dtype.num: "floatComplex", np.complex128().dtype.num: "doubleComplex", np.unicode_().dtype.num: "unicodeChar", } numpy_dtype_to_field_mapping[np.bytes_().dtype.num] = "char" def _all_matching_dtype(column): first_dtype = False first_shape = () for x in column: if not isinstance(x, np.ndarray) or len(x) == 0: continue if first_dtype is False: first_dtype = x.dtype first_shape = x.shape[1:] elif first_dtype != x.dtype: return False, () elif first_shape != x.shape[1:]: first_shape = () return first_dtype, first_shape def numpy_to_votable_dtype(dtype, shape): """ Converts a numpy dtype and shape to a dictionary of attributes for a VOTable FIELD element and correspond to that type. Parameters ---------- dtype : Numpy dtype instance shape : tuple Returns ------- attributes : dict A dict containing 'datatype' and 'arraysize' keys that can be set on a VOTable FIELD element. """ if dtype.num not in numpy_dtype_to_field_mapping: raise TypeError(f"{dtype!r} can not be represented in VOTable") if dtype.char == "S": return {"datatype": "char", "arraysize": str(dtype.itemsize)} elif dtype.char == "U": return {"datatype": "unicodeChar", "arraysize": str(dtype.itemsize // 4)} else: result = {"datatype": numpy_dtype_to_field_mapping[dtype.num]} if len(shape): result["arraysize"] = "x".join(str(x) for x in shape) return result def table_column_to_votable_datatype(column): """ Given a `astropy.table.Column` instance, returns the attributes necessary to create a VOTable FIELD element that corresponds to the type of the column. This necessarily must perform some heuristics to determine the type of variable length arrays fields, since they are not directly supported by Numpy. If the column has dtype of "object", it performs the following tests: - If all elements are byte or unicode strings, it creates a variable-length byte or unicode field, respectively. - If all elements are numpy arrays of the same dtype and with a consistent shape in all but the first dimension, it creates a variable length array of fixed sized arrays. If the dtypes match, but the shapes do not, a variable length array is created. If the dtype of the input is not understood, it sets the data type to the most inclusive: a variable length unicodeChar array. Parameters ---------- column : `astropy.table.Column` instance Returns ------- attributes : dict A dict containing 'datatype' and 'arraysize' keys that can be set on a VOTable FIELD element. """ votable_string_dtype = None if column.info.meta is not None: votable_string_dtype = column.info.meta.get("_votable_string_dtype") if column.dtype.char == "O": if votable_string_dtype is not None: return {"datatype": votable_string_dtype, "arraysize": "*"} elif isinstance(column[0], np.ndarray): dtype, shape = _all_matching_dtype(column) if dtype is not False: result = numpy_to_votable_dtype(dtype, shape) if "arraysize" not in result: result["arraysize"] = "*" else: result["arraysize"] += "*" return result # All bets are off, do the most generic thing return {"datatype": "unicodeChar", "arraysize": "*"} # For fixed size string columns, datatype here will be unicodeChar, # but honor the original FIELD datatype if present. result = numpy_to_votable_dtype(column.dtype, column.shape[1:]) if result["datatype"] == "unicodeChar" and votable_string_dtype == "char": result["datatype"] = "char" return result
6d2e00423525dd2ca228b29405790075db3e092c62d4bd21b1ce28bde7ad9ce9
# Licensed under a 3-clause BSD style license - see LICENSE.rst import os from astropy.io import registry as io_registry from astropy.table import Table from astropy.table.column import BaseColumn from astropy.units import Quantity from astropy.utils.misc import NOT_OVERWRITING_MSG from . import from_table, parse from .tree import Table as VOTable from .tree import VOTableFile def is_votable(origin, filepath, fileobj, *args, **kwargs): """ Reads the header of a file to determine if it is a VOTable file. Parameters ---------- origin : str or readable file-like Path or file object containing a VOTABLE_ xml file. Returns ------- is_votable : bool Returns `True` if the given file is a VOTable file. """ from . import is_votable if origin == "read": if fileobj is not None: try: result = is_votable(fileobj) finally: fileobj.seek(0) return result elif filepath is not None: return is_votable(filepath) elif isinstance(args[0], (VOTableFile, VOTable)): return True else: return False else: return False def read_table_votable( input, table_id=None, use_names_over_ids=False, verify=None, **kwargs ): """ Read a Table object from an VO table file Parameters ---------- input : str or `~astropy.io.votable.tree.VOTableFile` or `~astropy.io.votable.tree.Table` If a string, the filename to read the table from. If a :class:`~astropy.io.votable.tree.VOTableFile` or :class:`~astropy.io.votable.tree.Table` object, the object to extract the table from. table_id : str or int, optional The table to read in. If a `str`, it is an ID corresponding to the ID of the table in the file (not all VOTable files assign IDs to their tables). If an `int`, it is the index of the table in the file, starting at 0. use_names_over_ids : bool, optional When `True` use the ``name`` attributes of columns as the names of columns in the `~astropy.table.Table` instance. Since names are not guaranteed to be unique, this may cause some columns to be renamed by appending numbers to the end. Otherwise (default), use the ID attributes as the column names. verify : {'ignore', 'warn', 'exception'}, optional When ``'exception'``, raise an error when the file violates the spec, otherwise either issue a warning (``'warn'``) or silently continue (``'ignore'``). Warnings may be controlled using the standard Python mechanisms. See the `warnings` module in the Python standard library for more information. When not provided, uses the configuration setting ``astropy.io.votable.verify``, which defaults to ``'ignore'``. **kwargs Additional keyword arguments are passed on to :func:`astropy.io.votable.table.parse`. """ if not isinstance(input, (VOTableFile, VOTable)): input = parse(input, table_id=table_id, verify=verify, **kwargs) # Parse all table objects table_id_mapping = dict() tables = [] if isinstance(input, VOTableFile): for table in input.iter_tables(): if table.ID is not None: table_id_mapping[table.ID] = table tables.append(table) if len(tables) > 1: if table_id is None: raise ValueError( "Multiple tables found: table id should be set via the table_id=" " argument. The available tables are" f" {', '.join(table_id_mapping)}, or integers less than" f" {len(tables)}." ) elif isinstance(table_id, str): if table_id in table_id_mapping: table = table_id_mapping[table_id] else: raise ValueError(f"No tables with id={table_id} found") elif isinstance(table_id, int): if table_id < len(tables): table = tables[table_id] else: raise IndexError( f"Table index {table_id} is out of range. {len(tables)} tables" " found" ) elif len(tables) == 1: table = tables[0] else: raise ValueError("No table found") elif isinstance(input, VOTable): table = input # Convert to an astropy.table.Table object return table.to_table(use_names_over_ids=use_names_over_ids) def write_table_votable( input, output, table_id=None, overwrite=False, tabledata_format=None ): """ Write a Table object to an VO table file Parameters ---------- input : Table The table to write out. output : str The filename to write the table to. table_id : str, optional The table ID to use. If this is not specified, the 'ID' keyword in the ``meta`` object of the table will be used. overwrite : bool, optional Whether to overwrite any existing file without warning. tabledata_format : str, optional The format of table data to write. Must be one of ``tabledata`` (text representation), ``binary`` or ``binary2``. Default is ``tabledata``. See :ref:`astropy:votable-serialization`. """ # Only those columns which are instances of BaseColumn or Quantity can be written unsupported_cols = input.columns.not_isinstance((BaseColumn, Quantity)) if unsupported_cols: unsupported_names = [col.info.name for col in unsupported_cols] raise ValueError( f"cannot write table with mixin column(s) {unsupported_names} to VOTable" ) # Check if output file already exists if isinstance(output, str) and os.path.exists(output): if overwrite: os.remove(output) else: raise OSError(NOT_OVERWRITING_MSG.format(output)) # Create a new VOTable file table_file = from_table(input, table_id=table_id) # Write out file table_file.to_xml(output, tabledata_format=tabledata_format) io_registry.register_reader("votable", Table, read_table_votable) io_registry.register_writer("votable", Table, write_table_votable) io_registry.register_identifier("votable", Table, is_votable)
b6be6b29d08fc26446a30a40791251bf4437f25c9296edeef5c2964d43b9fe1e
# Licensed under a 3-clause BSD style license - see LICENSE.rst # TODO: Test FITS parsing # STDLIB import base64 import codecs import gzip import io import re import urllib.request import warnings # THIRD-PARTY import numpy as np from numpy import ma # LOCAL from astropy import __version__ as astropy_version from astropy.io import fits from astropy.utils.collections import HomogeneousList from astropy.utils.exceptions import AstropyDeprecationWarning from astropy.utils.xml.writer import XMLWriter from . import converters from . import ucd as ucd_mod from . import util, xmlutil from .exceptions import ( E06, E08, E09, E10, E11, E12, E13, E15, E16, E17, E18, E19, E20, E21, E22, E23, E25, W06, W07, W08, W09, W10, W11, W12, W13, W15, W17, W18, W19, W20, W21, W22, W26, W27, W28, W29, W32, W33, W35, W36, W37, W38, W40, W41, W42, W43, W44, W45, W50, W52, W53, W54, vo_raise, vo_reraise, vo_warn, warn_or_raise, warn_unknown_attrs, ) try: from . import tablewriter _has_c_tabledata_writer = True except ImportError: _has_c_tabledata_writer = False __all__ = [ "Link", "Info", "Values", "Field", "Param", "CooSys", "TimeSys", "FieldRef", "ParamRef", "Group", "Table", "Resource", "VOTableFile", "Element", ] # The default number of rows to read in each chunk before converting # to an array. DEFAULT_CHUNK_SIZE = 256 RESIZE_AMOUNT = 1.5 ###################################################################### # FACTORY FUNCTIONS def _resize(masked, new_size): """ Masked arrays can not be resized inplace, and `np.resize` and `ma.resize` are both incompatible with structured arrays. Therefore, we do all this. """ new_array = ma.zeros((new_size,), dtype=masked.dtype) length = min(len(masked), new_size) new_array[:length] = masked[:length] return new_array def _lookup_by_attr_factory(attr, unique, iterator, element_name, doc): """ Creates a function useful for looking up an element by a given attribute. Parameters ---------- attr : str The attribute name unique : bool Should be `True` if the attribute is unique and therefore this should return only one value. Otherwise, returns a list of values. iterator : generator A generator that iterates over some arbitrary set of elements element_name : str The XML element name of the elements being iterated over (used for error messages only). doc : str A docstring to apply to the generated function. Returns ------- factory : function A function that looks up an element by the given attribute. """ def lookup_by_attr(self, ref, before=None): """ Given a string *ref*, finds the first element in the iterator where the given attribute == *ref*. If *before* is provided, will stop searching at the object *before*. This is important, since "forward references" are not allowed in the VOTABLE format. """ for element in getattr(self, iterator)(): if element is before: if getattr(element, attr, None) == ref: vo_raise( f"{element_name} references itself", element._config, element._pos, KeyError, ) break if getattr(element, attr, None) == ref: yield element def lookup_by_attr_unique(self, ref, before=None): for element in lookup_by_attr(self, ref, before=before): return element raise KeyError( "No {} with {} '{}' found before the referencing {}".format( element_name, attr, ref, element_name ) ) if unique: lookup_by_attr_unique.__doc__ = doc return lookup_by_attr_unique else: lookup_by_attr.__doc__ = doc return lookup_by_attr def _lookup_by_id_or_name_factory(iterator, element_name, doc): """ Like `_lookup_by_attr_factory`, but looks in both the "ID" and "name" attributes. """ def lookup_by_id_or_name(self, ref, before=None): """ Given an key *ref*, finds the first element in the iterator with the attribute ID == *ref* or name == *ref*. If *before* is provided, will stop searching at the object *before*. This is important, since "forward references" are not allowed in the VOTABLE format. """ for element in getattr(self, iterator)(): if element is before: if ref in (element.ID, element.name): vo_raise( f"{element_name} references itself", element._config, element._pos, KeyError, ) break if ref in (element.ID, element.name): return element raise KeyError( "No {} with ID or name '{}' found before the referencing {}".format( element_name, ref, element_name ) ) lookup_by_id_or_name.__doc__ = doc return lookup_by_id_or_name def _get_default_unit_format(config): """ Get the default unit format as specified in the VOTable spec. """ # The unit format changed between VOTable versions 1.3 and 1.4, # see issue #10791. if config["version_1_4_or_later"]: return "vounit" else: return "cds" def _get_unit_format(config): """ Get the unit format based on the configuration. """ if config.get("unit_format") is None: format = _get_default_unit_format(config) else: format = config["unit_format"] return format ###################################################################### # ATTRIBUTE CHECKERS def check_astroyear(year, field, config=None, pos=None): """ Raises a `~astropy.io.votable.exceptions.VOTableSpecError` if *year* is not a valid astronomical year as defined by the VOTABLE standard. Parameters ---------- year : str An astronomical year string field : str The name of the field this year was found in (used for error message) config, pos : optional Information about the source of the value """ if year is not None and re.match(r"^[JB]?[0-9]+([.][0-9]*)?$", year) is None: warn_or_raise(W07, W07, (field, year), config, pos) return False return True def check_string(string, attr_name, config=None, pos=None): """ Raises a `~astropy.io.votable.exceptions.VOTableSpecError` if *string* is not a string or Unicode string. Parameters ---------- string : str An astronomical year string attr_name : str The name of the field this year was found in (used for error message) config, pos : optional Information about the source of the value """ if string is not None and not isinstance(string, str): warn_or_raise(W08, W08, attr_name, config, pos) return False return True def resolve_id(ID, id, config=None, pos=None): if ID is None and id is not None: warn_or_raise(W09, W09, (), config, pos) return id return ID def check_ucd(ucd, config=None, pos=None): """ Warns or raises a `~astropy.io.votable.exceptions.VOTableSpecError` if *ucd* is not a valid `unified content descriptor`_ string as defined by the VOTABLE standard. Parameters ---------- ucd : str A UCD string. config, pos : optional Information about the source of the value """ if config is None: config = {} if config.get("version_1_1_or_later"): try: ucd_mod.parse_ucd( ucd, check_controlled_vocabulary=config.get("version_1_2_or_later", False), has_colon=config.get("version_1_2_or_later", False), ) except ValueError as e: # This weird construction is for Python 3 compatibility if config.get("verify", "ignore") == "exception": vo_raise(W06, (ucd, str(e)), config, pos) elif config.get("verify", "ignore") == "warn": vo_warn(W06, (ucd, str(e)), config, pos) return False else: return False return True ###################################################################### # PROPERTY MIXINS class _IDProperty: @property def ID(self): """ The XML ID_ of the element. May be `None` or a string conforming to XML ID_ syntax. """ return self._ID @ID.setter def ID(self, ID): xmlutil.check_id(ID, "ID", self._config, self._pos) self._ID = ID @ID.deleter def ID(self): self._ID = None class _NameProperty: @property def name(self): """An optional name for the element.""" return self._name @name.setter def name(self, name): xmlutil.check_token(name, "name", self._config, self._pos) self._name = name @name.deleter def name(self): self._name = None class _XtypeProperty: @property def xtype(self): """Extended data type information.""" return self._xtype @xtype.setter def xtype(self, xtype): if xtype is not None and not self._config.get("version_1_2_or_later"): warn_or_raise( W28, W28, ("xtype", self._element_name, "1.2"), self._config, self._pos ) check_string(xtype, "xtype", self._config, self._pos) self._xtype = xtype @xtype.deleter def xtype(self): self._xtype = None class _UtypeProperty: _utype_in_v1_2 = False @property def utype(self): """The usage-specific or `unique type`_ of the element.""" return self._utype @utype.setter def utype(self, utype): if ( self._utype_in_v1_2 and utype is not None and not self._config.get("version_1_2_or_later") ): warn_or_raise( W28, W28, ("utype", self._element_name, "1.2"), self._config, self._pos ) check_string(utype, "utype", self._config, self._pos) self._utype = utype @utype.deleter def utype(self): self._utype = None class _UcdProperty: _ucd_in_v1_2 = False @property def ucd(self): """The `unified content descriptor`_ for the element.""" return self._ucd @ucd.setter def ucd(self, ucd): if ucd is not None and ucd.strip() == "": ucd = None if ucd is not None: if self._ucd_in_v1_2 and not self._config.get("version_1_2_or_later"): warn_or_raise( W28, W28, ("ucd", self._element_name, "1.2"), self._config, self._pos, ) check_ucd(ucd, self._config, self._pos) self._ucd = ucd @ucd.deleter def ucd(self): self._ucd = None class _DescriptionProperty: @property def description(self): """ An optional string describing the element. Corresponds to the DESCRIPTION_ element. """ return self._description @description.setter def description(self, description): self._description = description @description.deleter def description(self): self._description = None ###################################################################### # ELEMENT CLASSES class Element: """ A base class for all classes that represent XML elements in the VOTABLE file. """ _element_name = "" _attr_list = [] def _add_unknown_tag(self, iterator, tag, data, config, pos): warn_or_raise(W10, W10, tag, config, pos) def _ignore_add(self, iterator, tag, data, config, pos): warn_unknown_attrs(tag, data.keys(), config, pos) def _add_definitions(self, iterator, tag, data, config, pos): if config.get("version_1_1_or_later"): warn_or_raise(W22, W22, (), config, pos) warn_unknown_attrs(tag, data.keys(), config, pos) def parse(self, iterator, config): """ For internal use. Parse the XML content of the children of the element. Parameters ---------- iterator : xml iterable An iterator over XML elements as returned by `~astropy.utils.xml.iterparser.get_xml_iterator`. config : dict The configuration dictionary that affects how certain elements are read. Returns ------- self : `~astropy.io.votable.tree.Element` Returns self as a convenience. """ raise NotImplementedError() def to_xml(self, w, **kwargs): """ For internal use. Output the element to XML. Parameters ---------- w : astropy.utils.xml.writer.XMLWriter object An XML writer to write to. **kwargs : dict Any configuration parameters to control the output. """ raise NotImplementedError() class SimpleElement(Element): """ A base class for simple elements, such as FIELD, PARAM and INFO that don't require any special parsing or outputting machinery. """ def __init__(self): Element.__init__(self) def __repr__(self): buff = io.StringIO() SimpleElement.to_xml(self, XMLWriter(buff)) return buff.getvalue().strip() def parse(self, iterator, config): for start, tag, data, pos in iterator: if start and tag != self._element_name: self._add_unknown_tag(iterator, tag, data, config, pos) elif tag == self._element_name: break return self def to_xml(self, w, **kwargs): w.element(self._element_name, attrib=w.object_attrs(self, self._attr_list)) class SimpleElementWithContent(SimpleElement): """ A base class for simple elements, such as FIELD, PARAM and INFO that don't require any special parsing or outputting machinery. """ def __init__(self): SimpleElement.__init__(self) self._content = None def parse(self, iterator, config): for start, tag, data, pos in iterator: if start and tag != self._element_name: self._add_unknown_tag(iterator, tag, data, config, pos) elif tag == self._element_name: if data: self.content = data break return self def to_xml(self, w, **kwargs): w.element( self._element_name, self._content, attrib=w.object_attrs(self, self._attr_list), ) @property def content(self): """The content of the element.""" return self._content @content.setter def content(self, content): check_string(content, "content", self._config, self._pos) self._content = content @content.deleter def content(self): self._content = None class Link(SimpleElement, _IDProperty): """ LINK_ elements: used to reference external documents and servers through a URI. The keyword arguments correspond to setting members of the same name, documented below. """ _attr_list = [ "ID", "content_role", "content_type", "title", "value", "href", "action", ] _element_name = "LINK" def __init__( self, ID=None, title=None, value=None, href=None, action=None, id=None, config=None, pos=None, **kwargs, ): if config is None: config = {} self._config = config self._pos = pos SimpleElement.__init__(self) content_role = kwargs.get("content-role") or kwargs.get("content_role") content_type = kwargs.get("content-type") or kwargs.get("content_type") if "gref" in kwargs: warn_or_raise(W11, W11, (), config, pos) self.ID = resolve_id(ID, id, config, pos) self.content_role = content_role self.content_type = content_type self.title = title self.value = value self.href = href self.action = action warn_unknown_attrs( "LINK", kwargs.keys(), config, pos, ["content-role", "content_role", "content-type", "content_type", "gref"], ) @property def content_role(self): """ Defines the MIME role of the referenced object. Must be one of: None, 'query', 'hints', 'doc', 'location' or 'type' """ return self._content_role @content_role.setter def content_role(self, content_role): if ( content_role == "type" and not self._config["version_1_3_or_later"] ) or content_role not in (None, "query", "hints", "doc", "location"): vo_warn(W45, (content_role,), self._config, self._pos) self._content_role = content_role @content_role.deleter def content_role(self): self._content_role = None @property def content_type(self): """Defines the MIME content type of the referenced object.""" return self._content_type @content_type.setter def content_type(self, content_type): xmlutil.check_mime_content_type(content_type, self._config, self._pos) self._content_type = content_type @content_type.deleter def content_type(self): self._content_type = None @property def href(self): """ A URI to an arbitrary protocol. The vo package only supports http and anonymous ftp. """ return self._href @href.setter def href(self, href): xmlutil.check_anyuri(href, self._config, self._pos) self._href = href @href.deleter def href(self): self._href = None def to_table_column(self, column): meta = {} for key in self._attr_list: val = getattr(self, key, None) if val is not None: meta[key] = val column.meta.setdefault("links", []) column.meta["links"].append(meta) @classmethod def from_table_column(cls, d): return cls(**d) class Info(SimpleElementWithContent, _IDProperty, _XtypeProperty, _UtypeProperty): """ INFO_ elements: arbitrary key-value pairs for extensions to the standard. The keyword arguments correspond to setting members of the same name, documented below. """ _element_name = "INFO" _attr_list_11 = ["ID", "name", "value"] _attr_list_12 = _attr_list_11 + ["xtype", "ref", "unit", "ucd", "utype"] _utype_in_v1_2 = True def __init__( self, ID=None, name=None, value=None, id=None, xtype=None, ref=None, unit=None, ucd=None, utype=None, config=None, pos=None, **extra, ): if config is None: config = {} self._config = config self._pos = pos SimpleElementWithContent.__init__(self) self.ID = resolve_id(ID, id, config, pos) or xmlutil.fix_id(name, config, pos) self.name = name self.value = value self.xtype = xtype self.ref = ref self.unit = unit self.ucd = ucd self.utype = utype if config.get("version_1_2_or_later"): self._attr_list = self._attr_list_12 else: self._attr_list = self._attr_list_11 if xtype is not None: warn_unknown_attrs("INFO", ["xtype"], config, pos) if ref is not None: warn_unknown_attrs("INFO", ["ref"], config, pos) if unit is not None: warn_unknown_attrs("INFO", ["unit"], config, pos) if ucd is not None: warn_unknown_attrs("INFO", ["ucd"], config, pos) if utype is not None: warn_unknown_attrs("INFO", ["utype"], config, pos) warn_unknown_attrs("INFO", extra.keys(), config, pos) @property def name(self): """[*required*] The key of the key-value pair.""" return self._name @name.setter def name(self, name): if name is None: warn_or_raise(W35, W35, "name", self._config, self._pos) xmlutil.check_token(name, "name", self._config, self._pos) self._name = name @property def value(self): """ [*required*] The value of the key-value pair. (Always stored as a string or unicode string). """ return self._value @value.setter def value(self, value): if value is None: warn_or_raise(W35, W35, "value", self._config, self._pos) check_string(value, "value", self._config, self._pos) self._value = value @property def content(self): """The content inside the INFO element.""" return self._content @content.setter def content(self, content): check_string(content, "content", self._config, self._pos) self._content = content @content.deleter def content(self): self._content = None @property def ref(self): """ Refer to another INFO_ element by ID_, defined previously in the document. """ return self._ref @ref.setter def ref(self, ref): if ref is not None and not self._config.get("version_1_2_or_later"): warn_or_raise(W28, W28, ("ref", "INFO", "1.2"), self._config, self._pos) xmlutil.check_id(ref, "ref", self._config, self._pos) # TODO: actually apply the reference # if ref is not None: # try: # other = self._votable.get_values_by_id(ref, before=self) # except KeyError: # vo_raise( # "VALUES ref='%s', which has not already been defined." % # self.ref, self._config, self._pos, KeyError) # self.null = other.null # self.type = other.type # self.min = other.min # self.min_inclusive = other.min_inclusive # self.max = other.max # self.max_inclusive = other.max_inclusive # self._options[:] = other.options self._ref = ref @ref.deleter def ref(self): self._ref = None @property def unit(self): """A string specifying the units_ for the INFO_.""" return self._unit @unit.setter def unit(self, unit): if unit is None: self._unit = None return from astropy import units as u if not self._config.get("version_1_2_or_later"): warn_or_raise(W28, W28, ("unit", "INFO", "1.2"), self._config, self._pos) # First, parse the unit in the default way, so that we can # still emit a warning if the unit is not to spec. default_format = _get_default_unit_format(self._config) unit_obj = u.Unit(unit, format=default_format, parse_strict="silent") if isinstance(unit_obj, u.UnrecognizedUnit): warn_or_raise(W50, W50, (unit,), self._config, self._pos) format = _get_unit_format(self._config) if format != default_format: unit_obj = u.Unit(unit, format=format, parse_strict="silent") self._unit = unit_obj @unit.deleter def unit(self): self._unit = None def to_xml(self, w, **kwargs): attrib = w.object_attrs(self, self._attr_list) if "unit" in attrib: attrib["unit"] = self.unit.to_string("cds") w.element(self._element_name, self._content, attrib=attrib) class Values(Element, _IDProperty): """ VALUES_ element: used within FIELD_ and PARAM_ elements to define the domain of values. The keyword arguments correspond to setting members of the same name, documented below. """ def __init__( self, votable, field, ID=None, null=None, ref=None, type="legal", id=None, config=None, pos=None, **extras, ): if config is None: config = {} self._config = config self._pos = pos Element.__init__(self) self._votable = votable self._field = field self.ID = resolve_id(ID, id, config, pos) self.null = null self._ref = ref self.type = type self.min = None self.max = None self.min_inclusive = True self.max_inclusive = True self._options = [] warn_unknown_attrs("VALUES", extras.keys(), config, pos) def __repr__(self): buff = io.StringIO() self.to_xml(XMLWriter(buff)) return buff.getvalue().strip() @property def null(self): """ For integral datatypes, *null* is used to define the value used for missing values. """ return self._null @null.setter def null(self, null): if null is not None and isinstance(null, str): try: null_val = self._field.converter.parse_scalar( null, self._config, self._pos )[0] except Exception: warn_or_raise(W36, W36, null, self._config, self._pos) null_val = self._field.converter.parse_scalar( "0", self._config, self._pos )[0] else: null_val = null self._null = null_val @null.deleter def null(self): self._null = None @property def type(self): """ [*required*] Defines the applicability of the domain defined by this VALUES_ element. Must be one of the following strings: - 'legal': The domain of this column applies in general to this datatype. (default) - 'actual': The domain of this column applies only to the data enclosed in the parent table. """ return self._type @type.setter def type(self, type): if type not in ("legal", "actual"): vo_raise(E08, type, self._config, self._pos) self._type = type @property def ref(self): """ Refer to another VALUES_ element by ID_, defined previously in the document, for MIN/MAX/OPTION information. """ return self._ref @ref.setter def ref(self, ref): xmlutil.check_id(ref, "ref", self._config, self._pos) if ref is not None: try: other = self._votable.get_values_by_id(ref, before=self) except KeyError: warn_or_raise(W43, W43, ("VALUES", self.ref), self._config, self._pos) ref = None else: self.null = other.null self.type = other.type self.min = other.min self.min_inclusive = other.min_inclusive self.max = other.max self.max_inclusive = other.max_inclusive self._options[:] = other.options self._ref = ref @ref.deleter def ref(self): self._ref = None @property def min(self): """ The minimum value of the domain. See :attr:`min_inclusive`. """ return self._min @min.setter def min(self, min): if hasattr(self._field, "converter") and min is not None: self._min = self._field.converter.parse(min)[0] else: self._min = min @min.deleter def min(self): self._min = None @property def min_inclusive(self): """When `True`, the domain includes the minimum value.""" return self._min_inclusive @min_inclusive.setter def min_inclusive(self, inclusive): if inclusive == "yes": self._min_inclusive = True elif inclusive == "no": self._min_inclusive = False else: self._min_inclusive = bool(inclusive) @min_inclusive.deleter def min_inclusive(self): self._min_inclusive = True @property def max(self): """ The maximum value of the domain. See :attr:`max_inclusive`. """ return self._max @max.setter def max(self, max): if hasattr(self._field, "converter") and max is not None: self._max = self._field.converter.parse(max)[0] else: self._max = max @max.deleter def max(self): self._max = None @property def max_inclusive(self): """When `True`, the domain includes the maximum value.""" return self._max_inclusive @max_inclusive.setter def max_inclusive(self, inclusive): if inclusive == "yes": self._max_inclusive = True elif inclusive == "no": self._max_inclusive = False else: self._max_inclusive = bool(inclusive) @max_inclusive.deleter def max_inclusive(self): self._max_inclusive = True @property def options(self): """ A list of string key-value tuples defining other OPTION elements for the domain. All options are ignored -- they are stored for round-tripping purposes only. """ return self._options def parse(self, iterator, config): if self.ref is not None: for start, tag, data, pos in iterator: if start: warn_or_raise(W44, W44, tag, config, pos) else: if tag != "VALUES": warn_or_raise(W44, W44, tag, config, pos) break else: for start, tag, data, pos in iterator: if start: if tag == "MIN": if "value" not in data: vo_raise(E09, "MIN", config, pos) self.min = data["value"] self.min_inclusive = data.get("inclusive", "yes") warn_unknown_attrs( "MIN", data.keys(), config, pos, ["value", "inclusive"] ) elif tag == "MAX": if "value" not in data: vo_raise(E09, "MAX", config, pos) self.max = data["value"] self.max_inclusive = data.get("inclusive", "yes") warn_unknown_attrs( "MAX", data.keys(), config, pos, ["value", "inclusive"] ) elif tag == "OPTION": if "value" not in data: vo_raise(E09, "OPTION", config, pos) xmlutil.check_token(data.get("name"), "name", config, pos) self.options.append((data.get("name"), data.get("value"))) warn_unknown_attrs( "OPTION", data.keys(), config, pos, ["value", "name"] ) elif tag == "VALUES": break return self def is_defaults(self): """ Are the settings on this ``VALUE`` element all the same as the XML defaults? """ # If there's nothing meaningful or non-default to write, # don't write anything. return ( self.ref is None and self.null is None and self.ID is None and self.max is None and self.min is None and self.options == [] ) def to_xml(self, w, **kwargs): def yes_no(value): if value: return "yes" return "no" if self.is_defaults(): return if self.ref is not None: w.element("VALUES", attrib=w.object_attrs(self, ["ref"])) else: with w.tag("VALUES", attrib=w.object_attrs(self, ["ID", "null", "ref"])): if self.min is not None: w.element( "MIN", value=self._field.converter.output(self.min, False), inclusive=yes_no(self.min_inclusive), ) if self.max is not None: w.element( "MAX", value=self._field.converter.output(self.max, False), inclusive=yes_no(self.max_inclusive), ) for name, value in self.options: w.element("OPTION", name=name, value=value) def to_table_column(self, column): # Have the ref filled in here meta = {} for key in ["ID", "null"]: val = getattr(self, key, None) if val is not None: meta[key] = val if self.min is not None: meta["min"] = {"value": self.min, "inclusive": self.min_inclusive} if self.max is not None: meta["max"] = {"value": self.max, "inclusive": self.max_inclusive} if len(self.options): meta["options"] = dict(self.options) column.meta["values"] = meta def from_table_column(self, column): if column.info.meta is None or "values" not in column.info.meta: return meta = column.info.meta["values"] for key in ["ID", "null"]: val = meta.get(key, None) if val is not None: setattr(self, key, val) if "min" in meta: self.min = meta["min"]["value"] self.min_inclusive = meta["min"]["inclusive"] if "max" in meta: self.max = meta["max"]["value"] self.max_inclusive = meta["max"]["inclusive"] if "options" in meta: self._options = list(meta["options"].items()) class Field( SimpleElement, _IDProperty, _NameProperty, _XtypeProperty, _UtypeProperty, _UcdProperty, ): """ FIELD_ element: describes the datatype of a particular column of data. The keyword arguments correspond to setting members of the same name, documented below. If *ID* is provided, it is used for the column name in the resulting recarray of the table. If no *ID* is provided, *name* is used instead. If neither is provided, an exception will be raised. """ _attr_list_11 = [ "ID", "name", "datatype", "arraysize", "ucd", "unit", "width", "precision", "utype", "ref", ] _attr_list_12 = _attr_list_11 + ["xtype"] _element_name = "FIELD" def __init__( self, votable, ID=None, name=None, datatype=None, arraysize=None, ucd=None, unit=None, width=None, precision=None, utype=None, ref=None, type=None, id=None, xtype=None, config=None, pos=None, **extra, ): if config is None: if hasattr(votable, "_get_version_checks"): config = votable._get_version_checks() else: config = {} self._config = config self._pos = pos SimpleElement.__init__(self) if config.get("version_1_2_or_later"): self._attr_list = self._attr_list_12 else: self._attr_list = self._attr_list_11 if xtype is not None: warn_unknown_attrs(self._element_name, ["xtype"], config, pos) # TODO: REMOVE ME ---------------------------------------- # This is a terrible hack to support Simple Image Access # Protocol results from https://astroarchive.noirlab.edu/ . It creates a field # for the coordinate projection type of type "double", which # actually contains character data. We have to hack the field # to store character data, or we can't read it in. A warning # will be raised when this happens. if ( config.get("verify", "ignore") != "exception" and name == "cprojection" and ID == "cprojection" and ucd == "VOX:WCS_CoordProjection" and datatype == "double" ): datatype = "char" arraysize = "3" vo_warn(W40, (), config, pos) # ---------------------------------------- self.description = None self._votable = votable self.ID = resolve_id(ID, id, config, pos) or xmlutil.fix_id(name, config, pos) self.name = name if name is None: if self._element_name == "PARAM" and not config.get("version_1_1_or_later"): pass else: warn_or_raise(W15, W15, self._element_name, config, pos) self.name = self.ID if self._ID is None and name is None: vo_raise(W12, self._element_name, config, pos) datatype_mapping = { "string": "char", "unicodeString": "unicodeChar", "int16": "short", "int32": "int", "int64": "long", "float32": "float", "float64": "double", # The following appear in some Vizier tables "unsignedInt": "long", "unsignedShort": "int", } datatype_mapping.update(config.get("datatype_mapping", {})) if datatype in datatype_mapping: warn_or_raise(W13, W13, (datatype, datatype_mapping[datatype]), config, pos) datatype = datatype_mapping[datatype] self.ref = ref self.datatype = datatype self.arraysize = arraysize self.ucd = ucd self.unit = unit self.width = width self.precision = precision self.utype = utype self.type = type self._links = HomogeneousList(Link) self.title = self.name self.values = Values(self._votable, self) self.xtype = xtype self._setup(config, pos) warn_unknown_attrs(self._element_name, extra.keys(), config, pos) @classmethod def uniqify_names(cls, fields): """ Make sure that all names and titles in a list of fields are unique, by appending numbers if necessary. """ unique = {} for field in fields: i = 2 new_id = field.ID while new_id in unique: new_id = field.ID + f"_{i:d}" i += 1 if new_id != field.ID: vo_warn(W32, (field.ID, new_id), field._config, field._pos) field.ID = new_id unique[new_id] = field.ID for field in fields: i = 2 if field.name is None: new_name = field.ID implicit = True else: new_name = field.name implicit = False if new_name != field.ID: while new_name in unique: new_name = field.name + f" {i:d}" i += 1 if not implicit and new_name != field.name: vo_warn(W33, (field.name, new_name), field._config, field._pos) field._unique_name = new_name unique[new_name] = field.name def _setup(self, config, pos): if self.values._ref is not None: self.values.ref = self.values._ref self.converter = converters.get_converter(self, config, pos) @property def datatype(self): """ [*required*] The datatype of the column. Valid values (as defined by the spec) are: 'boolean', 'bit', 'unsignedByte', 'short', 'int', 'long', 'char', 'unicodeChar', 'float', 'double', 'floatComplex', or 'doubleComplex' Many VOTABLE files in the wild use 'string' instead of 'char', so that is also a valid option, though 'string' will always be converted to 'char' when writing the file back out. """ return self._datatype @datatype.setter def datatype(self, datatype): if datatype is None: if self._config.get("version_1_1_or_later"): warn_or_raise(E10, E10, self._element_name, self._config, self._pos) datatype = "char" if datatype not in converters.converter_mapping: vo_raise(E06, (datatype, self.ID), self._config, self._pos) self._datatype = datatype @property def precision(self): """ Along with :attr:`width`, defines the `numerical accuracy`_ associated with the data. These values are used to limit the precision when writing floating point values back to the XML file. Otherwise, it is purely informational -- the Numpy recarray containing the data itself does not use this information. """ return self._precision @precision.setter def precision(self, precision): if precision is not None and not re.match(r"^[FE]?[0-9]+$", precision): vo_raise(E11, precision, self._config, self._pos) self._precision = precision @precision.deleter def precision(self): self._precision = None @property def width(self): """ Along with :attr:`precision`, defines the `numerical accuracy`_ associated with the data. These values are used to limit the precision when writing floating point values back to the XML file. Otherwise, it is purely informational -- the Numpy recarray containing the data itself does not use this information. """ return self._width @width.setter def width(self, width): if width is not None: width = int(width) if width <= 0: vo_raise(E12, width, self._config, self._pos) self._width = width @width.deleter def width(self): self._width = None # ref on FIELD and PARAM behave differently than elsewhere -- here # they're just informational, such as to refer to a coordinate # system. @property def ref(self): """ On FIELD_ elements, ref is used only for informational purposes, for example to refer to a COOSYS_ or TIMESYS_ element. """ return self._ref @ref.setter def ref(self, ref): xmlutil.check_id(ref, "ref", self._config, self._pos) self._ref = ref @ref.deleter def ref(self): self._ref = None @property def unit(self): """A string specifying the units_ for the FIELD_.""" return self._unit @unit.setter def unit(self, unit): if unit is None: self._unit = None return from astropy import units as u # First, parse the unit in the default way, so that we can # still emit a warning if the unit is not to spec. default_format = _get_default_unit_format(self._config) unit_obj = u.Unit(unit, format=default_format, parse_strict="silent") if isinstance(unit_obj, u.UnrecognizedUnit): warn_or_raise(W50, W50, (unit,), self._config, self._pos) format = _get_unit_format(self._config) if format != default_format: unit_obj = u.Unit(unit, format=format, parse_strict="silent") self._unit = unit_obj @unit.deleter def unit(self): self._unit = None @property def arraysize(self): """ Specifies the size of the multidimensional array if this FIELD_ contains more than a single value. See `multidimensional arrays`_. """ return self._arraysize @arraysize.setter def arraysize(self, arraysize): if arraysize is not None and not re.match( r"^([0-9]+x)*[0-9]*[*]?(s\W)?$", arraysize ): vo_raise(E13, arraysize, self._config, self._pos) self._arraysize = arraysize @arraysize.deleter def arraysize(self): self._arraysize = None @property def type(self): """ The type attribute on FIELD_ elements is reserved for future extensions. """ return self._type @type.setter def type(self, type): self._type = type @type.deleter def type(self): self._type = None @property def values(self): """ A :class:`Values` instance (or `None`) defining the domain of the column. """ return self._values @values.setter def values(self, values): assert values is None or isinstance(values, Values) self._values = values @values.deleter def values(self): self._values = None @property def links(self): """ A list of :class:`Link` instances used to reference more details about the meaning of the FIELD_. This is purely informational and is not used by the `astropy.io.votable` package. """ return self._links def parse(self, iterator, config): for start, tag, data, pos in iterator: if start: if tag == "VALUES": self.values.__init__( self._votable, self, config=config, pos=pos, **data ) self.values.parse(iterator, config) elif tag == "LINK": link = Link(config=config, pos=pos, **data) self.links.append(link) link.parse(iterator, config) elif tag == "DESCRIPTION": warn_unknown_attrs("DESCRIPTION", data.keys(), config, pos) elif tag != self._element_name: self._add_unknown_tag(iterator, tag, data, config, pos) else: if tag == "DESCRIPTION": if self.description is not None: warn_or_raise(W17, W17, self._element_name, config, pos) self.description = data or None elif tag == self._element_name: break if self.description is not None: self.title = " ".join(x.strip() for x in self.description.splitlines()) else: self.title = self.name self._setup(config, pos) return self def to_xml(self, w, **kwargs): attrib = w.object_attrs(self, self._attr_list) if "unit" in attrib: attrib["unit"] = self.unit.to_string("cds") with w.tag(self._element_name, attrib=attrib): if self.description is not None: w.element("DESCRIPTION", self.description, wrap=True) if not self.values.is_defaults(): self.values.to_xml(w, **kwargs) for link in self.links: link.to_xml(w, **kwargs) def to_table_column(self, column): """ Sets the attributes of a given `astropy.table.Column` instance to match the information in this `Field`. """ for key in ["ucd", "width", "precision", "utype", "xtype"]: val = getattr(self, key, None) if val is not None: column.meta[key] = val if not self.values.is_defaults(): self.values.to_table_column(column) for link in self.links: link.to_table_column(column) if self.description is not None: column.description = self.description if self.unit is not None: # TODO: Use units framework when it's available column.unit = self.unit if ( isinstance(self.converter, converters.FloatingPoint) and self.converter.output_format != "{!r:>}" ): column.format = self.converter.output_format elif isinstance(self.converter, converters.Char): column.info.meta["_votable_string_dtype"] = "char" elif isinstance(self.converter, converters.UnicodeChar): column.info.meta["_votable_string_dtype"] = "unicodeChar" @classmethod def from_table_column(cls, votable, column): """ Restores a `Field` instance from a given `astropy.table.Column` instance. """ kwargs = {} meta = column.info.meta if meta: for key in ["ucd", "width", "precision", "utype", "xtype"]: val = meta.get(key, None) if val is not None: kwargs[key] = val # TODO: Use the unit framework when available if column.info.unit is not None: kwargs["unit"] = column.info.unit kwargs["name"] = column.info.name result = converters.table_column_to_votable_datatype(column) kwargs.update(result) field = cls(votable, **kwargs) if column.info.description is not None: field.description = column.info.description field.values.from_table_column(column) if meta and "links" in meta: for link in meta["links"]: field.links.append(Link.from_table_column(link)) # TODO: Parse format into precision and width return field class Param(Field): """ PARAM_ element: constant-valued columns in the data. :class:`Param` objects are a subclass of :class:`Field`, and have all of its methods and members. Additionally, it defines :attr:`value`. """ _attr_list_11 = Field._attr_list_11 + ["value"] _attr_list_12 = Field._attr_list_12 + ["value"] _element_name = "PARAM" def __init__( self, votable, ID=None, name=None, value=None, datatype=None, arraysize=None, ucd=None, unit=None, width=None, precision=None, utype=None, type=None, id=None, config=None, pos=None, **extra, ): self._value = value Field.__init__( self, votable, ID=ID, name=name, datatype=datatype, arraysize=arraysize, ucd=ucd, unit=unit, precision=precision, utype=utype, type=type, id=id, config=config, pos=pos, **extra, ) @property def value(self): """ [*required*] The constant value of the parameter. Its type is determined by the :attr:`~Field.datatype` member. """ return self._value @value.setter def value(self, value): if value is None: value = "" if isinstance(value, str): self._value = self.converter.parse(value, self._config, self._pos)[0] else: self._value = value def _setup(self, config, pos): Field._setup(self, config, pos) self.value = self._value def to_xml(self, w, **kwargs): tmp_value = self._value self._value = self.converter.output(tmp_value, False) # We must always have a value if self._value is None: self._value = "" Field.to_xml(self, w, **kwargs) self._value = tmp_value class CooSys(SimpleElement): """ COOSYS_ element: defines a coordinate system. The keyword arguments correspond to setting members of the same name, documented below. """ _attr_list = ["ID", "equinox", "epoch", "system"] _element_name = "COOSYS" def __init__( self, ID=None, equinox=None, epoch=None, system=None, id=None, config=None, pos=None, **extra, ): if config is None: config = {} self._config = config self._pos = pos # COOSYS was deprecated in 1.2 but then re-instated in 1.3 if config.get("version_1_2_or_later") and not config.get( "version_1_3_or_later" ): warn_or_raise(W27, W27, (), config, pos) SimpleElement.__init__(self) self.ID = resolve_id(ID, id, config, pos) self.equinox = equinox self.epoch = epoch self.system = system warn_unknown_attrs("COOSYS", extra.keys(), config, pos) @property def ID(self): """ [*required*] The XML ID of the COOSYS_ element, used for cross-referencing. May be `None` or a string conforming to XML ID_ syntax. """ return self._ID @ID.setter def ID(self, ID): if self._config.get("version_1_1_or_later"): if ID is None: vo_raise(E15, (), self._config, self._pos) xmlutil.check_id(ID, "ID", self._config, self._pos) self._ID = ID @property def system(self): """ Specifies the type of coordinate system. Valid choices are: 'eq_FK4', 'eq_FK5', 'ICRS', 'ecl_FK4', 'ecl_FK5', 'galactic', 'supergalactic', 'xy', 'barycentric', or 'geo_app' """ return self._system @system.setter def system(self, system): if system not in ( "eq_FK4", "eq_FK5", "ICRS", "ecl_FK4", "ecl_FK5", "galactic", "supergalactic", "xy", "barycentric", "geo_app", ): warn_or_raise(E16, E16, system, self._config, self._pos) self._system = system @system.deleter def system(self): self._system = None @property def equinox(self): """ A parameter required to fix the equatorial or ecliptic systems (as e.g. "J2000" as the default "eq_FK5" or "B1950" as the default "eq_FK4"). """ return self._equinox @equinox.setter def equinox(self, equinox): check_astroyear(equinox, "equinox", self._config, self._pos) self._equinox = equinox @equinox.deleter def equinox(self): self._equinox = None @property def epoch(self): """ Specifies the epoch of the positions. It must be a string specifying an astronomical year. """ return self._epoch @epoch.setter def epoch(self, epoch): check_astroyear(epoch, "epoch", self._config, self._pos) self._epoch = epoch @epoch.deleter def epoch(self): self._epoch = None class TimeSys(SimpleElement): """ TIMESYS_ element: defines a time system. The keyword arguments correspond to setting members of the same name, documented below. """ _attr_list = ["ID", "timeorigin", "timescale", "refposition"] _element_name = "TIMESYS" def __init__( self, ID=None, timeorigin=None, timescale=None, refposition=None, id=None, config=None, pos=None, **extra, ): if config is None: config = {} self._config = config self._pos = pos # TIMESYS is supported starting in version 1.4 if not config["version_1_4_or_later"]: warn_or_raise(W54, W54, config["version"], config, pos) SimpleElement.__init__(self) self.ID = resolve_id(ID, id, config, pos) self.timeorigin = timeorigin self.timescale = timescale self.refposition = refposition warn_unknown_attrs( "TIMESYS", extra.keys(), config, pos, ["ID", "timeorigin", "timescale", "refposition"], ) @property def ID(self): """ [*required*] The XML ID of the TIMESYS_ element, used for cross-referencing. Must be a string conforming to XML ID_ syntax. """ return self._ID @ID.setter def ID(self, ID): if ID is None: vo_raise(E22, (), self._config, self._pos) xmlutil.check_id(ID, "ID", self._config, self._pos) self._ID = ID @property def timeorigin(self): """ Specifies the time origin of the time coordinate, given as a Julian Date for the the time scale and reference point defined. It is usually given as a floating point literal; for convenience, the magic strings "MJD-origin" (standing for 2400000.5) and "JD-origin" (standing for 0) are also allowed. The timeorigin attribute MUST be given unless the time’s representation contains a year of a calendar era, in which case it MUST NOT be present. In VOTables, these representations currently are Gregorian calendar years with xtype="timestamp", or years in the Julian or Besselian calendar when a column has yr, a, or Ba as its unit and no time origin is given. """ return self._timeorigin @timeorigin.setter def timeorigin(self, timeorigin): if ( timeorigin is not None and timeorigin != "MJD-origin" and timeorigin != "JD-origin" ): try: timeorigin = float(timeorigin) except ValueError: warn_or_raise(E23, E23, timeorigin, self._config, self._pos) self._timeorigin = timeorigin @timeorigin.deleter def timeorigin(self): self._timeorigin = None @property def timescale(self): """ [*required*] String specifying the time scale used. Values should be taken from the IVOA timescale vocabulary (documented at http://www.ivoa.net/rdf/timescale). """ return self._timescale @timescale.setter def timescale(self, timescale): self._timescale = timescale @timescale.deleter def timescale(self): self._timescale = None @property def refposition(self): """ [*required*] String specifying the reference position. Values should be taken from the IVOA refposition vocabulary (documented at http://www.ivoa.net/rdf/refposition). """ return self._refposition @refposition.setter def refposition(self, refposition): self._refposition = refposition @refposition.deleter def refposition(self): self._refposition = None class FieldRef(SimpleElement, _UtypeProperty, _UcdProperty): """ FIELDref_ element: used inside of GROUP_ elements to refer to remote FIELD_ elements. """ _attr_list_11 = ["ref"] _attr_list_12 = _attr_list_11 + ["ucd", "utype"] _element_name = "FIELDref" _utype_in_v1_2 = True _ucd_in_v1_2 = True def __init__( self, table, ref, ucd=None, utype=None, config=None, pos=None, **extra ): """ *table* is the :class:`Table` object that this :class:`FieldRef` is a member of. *ref* is the ID to reference a :class:`Field` object defined elsewhere. """ if config is None: config = {} self._config = config self._pos = pos SimpleElement.__init__(self) self._table = table self.ref = ref self.ucd = ucd self.utype = utype if config.get("version_1_2_or_later"): self._attr_list = self._attr_list_12 else: self._attr_list = self._attr_list_11 if ucd is not None: warn_unknown_attrs(self._element_name, ["ucd"], config, pos) if utype is not None: warn_unknown_attrs(self._element_name, ["utype"], config, pos) @property def ref(self): """The ID_ of the FIELD_ that this FIELDref_ references.""" return self._ref @ref.setter def ref(self, ref): xmlutil.check_id(ref, "ref", self._config, self._pos) self._ref = ref @ref.deleter def ref(self): self._ref = None def get_ref(self): """ Lookup the :class:`Field` instance that this :class:`FieldRef` references. """ for field in self._table._votable.iter_fields_and_params(): if isinstance(field, Field) and field.ID == self.ref: return field vo_raise(f"No field named '{self.ref}'", self._config, self._pos, KeyError) class ParamRef(SimpleElement, _UtypeProperty, _UcdProperty): """ PARAMref_ element: used inside of GROUP_ elements to refer to remote PARAM_ elements. The keyword arguments correspond to setting members of the same name, documented below. It contains the following publicly-accessible members: *ref*: An XML ID referring to a <PARAM> element. """ _attr_list_11 = ["ref"] _attr_list_12 = _attr_list_11 + ["ucd", "utype"] _element_name = "PARAMref" _utype_in_v1_2 = True _ucd_in_v1_2 = True def __init__(self, table, ref, ucd=None, utype=None, config=None, pos=None): if config is None: config = {} self._config = config self._pos = pos Element.__init__(self) self._table = table self.ref = ref self.ucd = ucd self.utype = utype if config.get("version_1_2_or_later"): self._attr_list = self._attr_list_12 else: self._attr_list = self._attr_list_11 if ucd is not None: warn_unknown_attrs(self._element_name, ["ucd"], config, pos) if utype is not None: warn_unknown_attrs(self._element_name, ["utype"], config, pos) @property def ref(self): """The ID_ of the PARAM_ that this PARAMref_ references.""" return self._ref @ref.setter def ref(self, ref): xmlutil.check_id(ref, "ref", self._config, self._pos) self._ref = ref @ref.deleter def ref(self): self._ref = None def get_ref(self): """ Lookup the :class:`Param` instance that this :class:``PARAMref`` references. """ for param in self._table._votable.iter_fields_and_params(): if isinstance(param, Param) and param.ID == self.ref: return param vo_raise(f"No params named '{self.ref}'", self._config, self._pos, KeyError) class Group( Element, _IDProperty, _NameProperty, _UtypeProperty, _UcdProperty, _DescriptionProperty, ): """ GROUP_ element: groups FIELD_ and PARAM_ elements. This information is currently ignored by the vo package---that is the columns in the recarray are always flat---but the grouping information is stored so that it can be written out again to the XML file. The keyword arguments correspond to setting members of the same name, documented below. """ def __init__( self, table, ID=None, name=None, ref=None, ucd=None, utype=None, id=None, config=None, pos=None, **extra, ): if config is None: config = {} self._config = config self._pos = pos Element.__init__(self) self._table = table self.ID = resolve_id(ID, id, config, pos) or xmlutil.fix_id(name, config, pos) self.name = name self.ref = ref self.ucd = ucd self.utype = utype self.description = None self._entries = HomogeneousList((FieldRef, ParamRef, Group, Param)) warn_unknown_attrs("GROUP", extra.keys(), config, pos) def __repr__(self): return f"<GROUP>... {len(self._entries)} entries ...</GROUP>" @property def ref(self): """ Currently ignored, as it's not clear from the spec how this is meant to work. """ return self._ref @ref.setter def ref(self, ref): xmlutil.check_id(ref, "ref", self._config, self._pos) self._ref = ref @ref.deleter def ref(self): self._ref = None @property def entries(self): """ [read-only] A list of members of the GROUP_. This list may only contain objects of type :class:`Param`, :class:`Group`, :class:`ParamRef` and :class:`FieldRef`. """ return self._entries def _add_fieldref(self, iterator, tag, data, config, pos): fieldref = FieldRef(self._table, config=config, pos=pos, **data) self.entries.append(fieldref) def _add_paramref(self, iterator, tag, data, config, pos): paramref = ParamRef(self._table, config=config, pos=pos, **data) self.entries.append(paramref) def _add_param(self, iterator, tag, data, config, pos): if isinstance(self._table, VOTableFile): votable = self._table else: votable = self._table._votable param = Param(votable, config=config, pos=pos, **data) self.entries.append(param) param.parse(iterator, config) def _add_group(self, iterator, tag, data, config, pos): group = Group(self._table, config=config, pos=pos, **data) self.entries.append(group) group.parse(iterator, config) def parse(self, iterator, config): tag_mapping = { "FIELDref": self._add_fieldref, "PARAMref": self._add_paramref, "PARAM": self._add_param, "GROUP": self._add_group, "DESCRIPTION": self._ignore_add, } for start, tag, data, pos in iterator: if start: tag_mapping.get(tag, self._add_unknown_tag)( iterator, tag, data, config, pos ) else: if tag == "DESCRIPTION": if self.description is not None: warn_or_raise(W17, W17, "GROUP", config, pos) self.description = data or None elif tag == "GROUP": break return self def to_xml(self, w, **kwargs): with w.tag( "GROUP", attrib=w.object_attrs(self, ["ID", "name", "ref", "ucd", "utype"]) ): if self.description is not None: w.element("DESCRIPTION", self.description, wrap=True) for entry in self.entries: entry.to_xml(w, **kwargs) def iter_fields_and_params(self): """ Recursively iterate over all :class:`Param` elements in this :class:`Group`. """ for entry in self.entries: if isinstance(entry, Param): yield entry elif isinstance(entry, Group): yield from entry.iter_fields_and_params() def iter_groups(self): """ Recursively iterate over all sub-:class:`Group` instances in this :class:`Group`. """ for entry in self.entries: if isinstance(entry, Group): yield entry yield from entry.iter_groups() class Table(Element, _IDProperty, _NameProperty, _UcdProperty, _DescriptionProperty): """ TABLE_ element: optionally contains data. It contains the following publicly-accessible and mutable attribute: *array*: A Numpy masked array of the data itself, where each row is a row of votable data, and columns are named and typed based on the <FIELD> elements of the table. The mask is parallel to the data array, except for variable-length fields. For those fields, the numpy array's column type is "object" (``"O"``), and another masked array is stored there. If the Table contains no data, (for example, its enclosing :class:`Resource` has :attr:`~Resource.type` == 'meta') *array* will have zero-length. The keyword arguments correspond to setting members of the same name, documented below. """ def __init__( self, votable, ID=None, name=None, ref=None, ucd=None, utype=None, nrows=None, id=None, config=None, pos=None, **extra, ): if config is None: config = {} self._config = config self._pos = pos self._empty = False Element.__init__(self) self._votable = votable self.ID = resolve_id(ID, id, config, pos) or xmlutil.fix_id(name, config, pos) self.name = name xmlutil.check_id(ref, "ref", config, pos) self._ref = ref self.ucd = ucd self.utype = utype if nrows is not None: nrows = int(nrows) if nrows < 0: raise ValueError("'nrows' cannot be negative.") self._nrows = nrows self.description = None self.format = "tabledata" self._fields = HomogeneousList(Field) self._params = HomogeneousList(Param) self._groups = HomogeneousList(Group) self._links = HomogeneousList(Link) self._infos = HomogeneousList(Info) self.array = ma.array([]) warn_unknown_attrs("TABLE", extra.keys(), config, pos) def __repr__(self): return repr(self.to_table()) def __bytes__(self): return bytes(self.to_table()) def __str__(self): return str(self.to_table()) @property def ref(self): return self._ref @ref.setter def ref(self, ref): """ Refer to another TABLE, previously defined, by the *ref* ID_ for all metadata (FIELD_, PARAM_ etc.) information. """ # When the ref changes, we want to verify that it will work # by actually going and looking for the referenced table. # If found, set a bunch of properties in this table based # on the other one. xmlutil.check_id(ref, "ref", self._config, self._pos) if ref is not None: try: table = self._votable.get_table_by_id(ref, before=self) except KeyError: warn_or_raise(W43, W43, ("TABLE", self.ref), self._config, self._pos) ref = None else: self._fields = table.fields self._params = table.params self._groups = table.groups self._links = table.links else: del self._fields[:] del self._params[:] del self._groups[:] del self._links[:] self._ref = ref @ref.deleter def ref(self): self._ref = None @property def format(self): """ [*required*] The serialization format of the table. Must be one of: 'tabledata' (TABLEDATA_), 'binary' (BINARY_), 'binary2' (BINARY2_) 'fits' (FITS_). Note that the 'fits' format, since it requires an external file, can not be written out. Any file read in with 'fits' format will be read out, by default, in 'tabledata' format. See :ref:`astropy:votable-serialization`. """ return self._format @format.setter def format(self, format): format = format.lower() if format == "fits": vo_raise( "fits format can not be written out, only read.", self._config, self._pos, NotImplementedError, ) if format == "binary2": if not self._config["version_1_3_or_later"]: vo_raise( "binary2 only supported in votable 1.3 or later", self._config, self._pos, ) elif format not in ("tabledata", "binary"): vo_raise(f"Invalid format '{format}'", self._config, self._pos) self._format = format @property def nrows(self): """ [*immutable*] The number of rows in the table, as specified in the XML file. """ return self._nrows @property def fields(self): """ A list of :class:`Field` objects describing the types of each of the data columns. """ return self._fields @property def params(self): """ A list of parameters (constant-valued columns) for the table. Must contain only :class:`Param` objects. """ return self._params @property def groups(self): """ A list of :class:`Group` objects describing how the columns and parameters are grouped. Currently this information is only kept around for round-tripping and informational purposes. """ return self._groups @property def links(self): """ A list of :class:`Link` objects (pointers to other documents or servers through a URI) for the table. """ return self._links @property def infos(self): """ A list of :class:`Info` objects for the table. Allows for post-operational diagnostics. """ return self._infos def is_empty(self): """ Returns True if this table doesn't contain any real data because it was skipped over by the parser (through use of the ``table_number`` kwarg). """ return self._empty def create_arrays(self, nrows=0, config=None): """ Create a new array to hold the data based on the current set of fields, and store them in the *array* and member variable. Any data in the existing array will be lost. *nrows*, if provided, is the number of rows to allocate. """ if nrows is None: nrows = 0 fields = self.fields if len(fields) == 0: array = np.recarray((nrows,), dtype="O") mask = np.zeros((nrows,), dtype="b") else: # for field in fields: field._setup(config) Field.uniqify_names(fields) dtype = [] for x in fields: if x._unique_name == x.ID: id = x.ID else: id = (x._unique_name, x.ID) dtype.append((id, x.converter.format)) array = np.recarray((nrows,), dtype=np.dtype(dtype)) descr_mask = [] for d in array.dtype.descr: new_type = (d[1][1] == "O" and "O") or "bool" if len(d) == 2: descr_mask.append((d[0], new_type)) elif len(d) == 3: descr_mask.append((d[0], new_type, d[2])) mask = np.zeros((nrows,), dtype=descr_mask) self.array = ma.array(array, mask=mask) def _resize_strategy(self, size): """ Return a new (larger) size based on size, used for reallocating an array when it fills up. This is in its own function so the resizing strategy can be easily replaced. """ # Once we go beyond 0, make a big step -- after that use a # factor of 1.5 to help keep memory usage compact if size == 0: return 512 return int(np.ceil(size * RESIZE_AMOUNT)) def _add_field(self, iterator, tag, data, config, pos): field = Field(self._votable, config=config, pos=pos, **data) self.fields.append(field) field.parse(iterator, config) def _add_param(self, iterator, tag, data, config, pos): param = Param(self._votable, config=config, pos=pos, **data) self.params.append(param) param.parse(iterator, config) def _add_group(self, iterator, tag, data, config, pos): group = Group(self, config=config, pos=pos, **data) self.groups.append(group) group.parse(iterator, config) def _add_link(self, iterator, tag, data, config, pos): link = Link(config=config, pos=pos, **data) self.links.append(link) link.parse(iterator, config) def _add_info(self, iterator, tag, data, config, pos): if not config.get("version_1_2_or_later"): warn_or_raise(W26, W26, ("INFO", "TABLE", "1.2"), config, pos) info = Info(config=config, pos=pos, **data) self.infos.append(info) info.parse(iterator, config) def parse(self, iterator, config): columns = config.get("columns") # If we've requested to read in only a specific table, skip # all others table_number = config.get("table_number") current_table_number = config.get("_current_table_number") skip_table = False if current_table_number is not None: config["_current_table_number"] += 1 if table_number is not None and table_number != current_table_number: skip_table = True self._empty = True table_id = config.get("table_id") if table_id is not None: if table_id != self.ID: skip_table = True self._empty = True if self.ref is not None: # This table doesn't have its own datatype descriptors, it # just references those from another table. # This is to call the property setter to go and get the # referenced information self.ref = self.ref for start, tag, data, pos in iterator: if start: if tag == "DATA": warn_unknown_attrs("DATA", data.keys(), config, pos) break else: if tag == "TABLE": return self elif tag == "DESCRIPTION": if self.description is not None: warn_or_raise(W17, W17, "RESOURCE", config, pos) self.description = data or None else: tag_mapping = { "FIELD": self._add_field, "PARAM": self._add_param, "GROUP": self._add_group, "LINK": self._add_link, "INFO": self._add_info, "DESCRIPTION": self._ignore_add, } for start, tag, data, pos in iterator: if start: if tag == "DATA": if len(self.fields) == 0: warn_or_raise(E25, E25, None, config, pos) warn_unknown_attrs("DATA", data.keys(), config, pos) break tag_mapping.get(tag, self._add_unknown_tag)( iterator, tag, data, config, pos ) else: if tag == "DESCRIPTION": if self.description is not None: warn_or_raise(W17, W17, "RESOURCE", config, pos) self.description = data or None elif tag == "TABLE": # For error checking purposes Field.uniqify_names(self.fields) # We still need to create arrays, even if the file # contains no DATA section self.create_arrays(nrows=0, config=config) return self self.create_arrays(nrows=self._nrows, config=config) fields = self.fields names = [x.ID for x in fields] # Deal with a subset of the columns, if requested. if not columns: colnumbers = list(range(len(fields))) else: if isinstance(columns, str): columns = [columns] columns = np.asarray(columns) if issubclass(columns.dtype.type, np.integer): if np.any(columns < 0) or np.any(columns > len(fields)): raise ValueError("Some specified column numbers out of range") colnumbers = columns elif issubclass(columns.dtype.type, np.character): try: colnumbers = [names.index(x) for x in columns] except ValueError: raise ValueError(f"Columns '{columns}' not found in fields list") else: raise TypeError("Invalid columns list") if (not skip_table) and (len(fields) > 0): for start, tag, data, pos in iterator: if start: if tag == "TABLEDATA": warn_unknown_attrs("TABLEDATA", data.keys(), config, pos) self.array = self._parse_tabledata( iterator, colnumbers, fields, config ) break elif tag == "BINARY": warn_unknown_attrs("BINARY", data.keys(), config, pos) self.array = self._parse_binary( 1, iterator, colnumbers, fields, config, pos ) break elif tag == "BINARY2": if not config["version_1_3_or_later"]: warn_or_raise(W52, W52, config["version"], config, pos) self.array = self._parse_binary( 2, iterator, colnumbers, fields, config, pos ) break elif tag == "FITS": warn_unknown_attrs("FITS", data.keys(), config, pos, ["extnum"]) try: extnum = int(data.get("extnum", 0)) if extnum < 0: raise ValueError("'extnum' cannot be negative.") except ValueError: vo_raise(E17, (), config, pos) self.array = self._parse_fits(iterator, extnum, config) break else: warn_or_raise(W37, W37, tag, config, pos) break for start, tag, data, pos in iterator: if not start and tag == "DATA": break for start, tag, data, pos in iterator: if start and tag == "INFO": if not config.get("version_1_2_or_later"): warn_or_raise(W26, W26, ("INFO", "TABLE", "1.2"), config, pos) info = Info(config=config, pos=pos, **data) self.infos.append(info) info.parse(iterator, config) elif not start and tag == "TABLE": break return self def _parse_tabledata(self, iterator, colnumbers, fields, config): # Since we don't know the number of rows up front, we'll # reallocate the record array to make room as we go. This # prevents the need to scan through the XML twice. The # allocation is by factors of 1.5. invalid = config.get("invalid", "exception") # Need to have only one reference so that we can resize the # array array = self.array del self.array parsers = [field.converter.parse for field in fields] binparsers = [field.converter.binparse for field in fields] numrows = 0 alloc_rows = len(array) colnumbers_bits = [i in colnumbers for i in range(len(fields))] row_default = [x.converter.default for x in fields] mask_default = [True] * len(fields) array_chunk = [] mask_chunk = [] chunk_size = config.get("chunk_size", DEFAULT_CHUNK_SIZE) for start, tag, data, pos in iterator: if tag == "TR": # Now parse one row row = row_default[:] row_mask = mask_default[:] i = 0 for start, tag, data, pos in iterator: if start: binary = data.get("encoding", None) == "base64" warn_unknown_attrs(tag, data.keys(), config, pos, ["encoding"]) else: if tag == "TD": if i >= len(fields): vo_raise(E20, len(fields), config, pos) if colnumbers_bits[i]: try: if binary: rawdata = base64.b64decode(data.encode("ascii")) buf = io.BytesIO(rawdata) buf.seek(0) try: value, mask_value = binparsers[i](buf.read) except Exception as e: vo_reraise( e, config, pos, "(in row {:d}, col '{}')".format( len(array_chunk), fields[i].ID ), ) else: try: value, mask_value = parsers[i]( data, config, pos ) except Exception as e: vo_reraise( e, config, pos, "(in row {:d}, col '{}')".format( len(array_chunk), fields[i].ID ), ) except Exception as e: if invalid == "exception": vo_reraise(e, config, pos) else: row[i] = value row_mask[i] = mask_value elif tag == "TR": break else: self._add_unknown_tag(iterator, tag, data, config, pos) i += 1 if i < len(fields): vo_raise(E21, (i, len(fields)), config, pos) array_chunk.append(tuple(row)) mask_chunk.append(tuple(row_mask)) if len(array_chunk) == chunk_size: while numrows + chunk_size > alloc_rows: alloc_rows = self._resize_strategy(alloc_rows) if alloc_rows != len(array): array = _resize(array, alloc_rows) array[numrows : numrows + chunk_size] = array_chunk array.mask[numrows : numrows + chunk_size] = mask_chunk numrows += chunk_size array_chunk = [] mask_chunk = [] elif not start and tag == "TABLEDATA": break # Now, resize the array to the exact number of rows we need and # put the last chunk values in there. alloc_rows = numrows + len(array_chunk) array = _resize(array, alloc_rows) array[numrows:] = array_chunk if alloc_rows != 0: array.mask[numrows:] = mask_chunk numrows += len(array_chunk) if self.nrows is not None and self.nrows >= 0 and self.nrows != numrows: warn_or_raise(W18, W18, (self.nrows, numrows), config, pos) self._nrows = numrows return array def _get_binary_data_stream(self, iterator, config): have_local_stream = False for start, tag, data, pos in iterator: if tag == "STREAM": if start: warn_unknown_attrs( "STREAM", data.keys(), config, pos, ["type", "href", "actuate", "encoding", "expires", "rights"], ) if "href" not in data: have_local_stream = True if data.get("encoding", None) != "base64": warn_or_raise( W38, W38, data.get("encoding", None), config, pos ) else: href = data["href"] xmlutil.check_anyuri(href, config, pos) encoding = data.get("encoding", None) else: buffer = data break if have_local_stream: buffer = base64.b64decode(buffer.encode("ascii")) string_io = io.BytesIO(buffer) string_io.seek(0) read = string_io.read else: if not href.startswith(("http", "ftp", "file")): vo_raise( "The vo package only supports remote data through http, " + "ftp or file", self._config, self._pos, NotImplementedError, ) fd = urllib.request.urlopen(href) if encoding is not None: if encoding == "gzip": fd = gzip.GzipFile(href, "rb", fileobj=fd) elif encoding == "base64": fd = codecs.EncodedFile(fd, "base64") else: vo_raise( f"Unknown encoding type '{encoding}'", self._config, self._pos, NotImplementedError, ) read = fd.read def careful_read(length): result = read(length) if len(result) != length: raise EOFError return result return careful_read def _parse_binary(self, mode, iterator, colnumbers, fields, config, pos): fields = self.fields careful_read = self._get_binary_data_stream(iterator, config) # Need to have only one reference so that we can resize the # array array = self.array del self.array binparsers = [field.converter.binparse for field in fields] numrows = 0 alloc_rows = len(array) while True: # Resize result arrays if necessary if numrows >= alloc_rows: alloc_rows = self._resize_strategy(alloc_rows) array = _resize(array, alloc_rows) row_data = [] row_mask_data = [] try: if mode == 2: mask_bits = careful_read(int((len(fields) + 7) / 8)) row_mask_data = list( converters.bitarray_to_bool(mask_bits, len(fields)) ) # Ignore the mask for string columns (see issue 8995) for i, f in enumerate(fields): if row_mask_data[i] and ( f.datatype == "char" or f.datatype == "unicodeChar" ): row_mask_data[i] = False for i, binparse in enumerate(binparsers): try: value, value_mask = binparse(careful_read) except EOFError: raise except Exception as e: vo_reraise( e, config, pos, f"(in row {numrows:d}, col '{fields[i].ID}')", ) row_data.append(value) if mode == 1: row_mask_data.append(value_mask) else: row_mask_data[i] = row_mask_data[i] or value_mask except EOFError: break row = [x.converter.default for x in fields] row_mask = [False] * len(fields) for i in colnumbers: row[i] = row_data[i] row_mask[i] = row_mask_data[i] array[numrows] = tuple(row) array.mask[numrows] = tuple(row_mask) numrows += 1 array = _resize(array, numrows) return array def _parse_fits(self, iterator, extnum, config): for start, tag, data, pos in iterator: if tag == "STREAM": if start: warn_unknown_attrs( "STREAM", data.keys(), config, pos, ["type", "href", "actuate", "encoding", "expires", "rights"], ) href = data["href"] encoding = data.get("encoding", None) else: break if not href.startswith(("http", "ftp", "file")): vo_raise( "The vo package only supports remote data through http, ftp or file", self._config, self._pos, NotImplementedError, ) fd = urllib.request.urlopen(href) if encoding is not None: if encoding == "gzip": fd = gzip.GzipFile(href, "r", fileobj=fd) elif encoding == "base64": fd = codecs.EncodedFile(fd, "base64") else: vo_raise( f"Unknown encoding type '{encoding}'", self._config, self._pos, NotImplementedError, ) hdulist = fits.open(fd) array = hdulist[int(extnum)].data if array.dtype != self.array.dtype: warn_or_raise(W19, W19, (), self._config, self._pos) return array def to_xml(self, w, **kwargs): specified_format = kwargs.get("tabledata_format") if specified_format is not None: format = specified_format else: format = self.format if format == "fits": format = "tabledata" with w.tag( "TABLE", attrib=w.object_attrs(self, ("ID", "name", "ref", "ucd", "utype", "nrows")), ): if self.description is not None: w.element("DESCRIPTION", self.description, wrap=True) for element_set in (self.fields, self.params): for element in element_set: element._setup({}, None) if self.ref is None: for element_set in (self.fields, self.params, self.groups, self.links): for element in element_set: element.to_xml(w, **kwargs) elif kwargs["version_1_2_or_later"]: index = list(self._votable.iter_tables()).index(self) group = Group(self, ID=f"_g{index}") group.to_xml(w, **kwargs) if len(self.array): with w.tag("DATA"): if format == "tabledata": self._write_tabledata(w, **kwargs) elif format == "binary": self._write_binary(1, w, **kwargs) elif format == "binary2": self._write_binary(2, w, **kwargs) if kwargs["version_1_2_or_later"]: for element in self._infos: element.to_xml(w, **kwargs) def _write_tabledata(self, w, **kwargs): fields = self.fields array = self.array with w.tag("TABLEDATA"): w._flush() if _has_c_tabledata_writer and not kwargs.get("_debug_python_based_parser"): supports_empty_values = [ field.converter.supports_empty_values(kwargs) for field in fields ] fields = [field.converter.output for field in fields] indent = len(w._tags) - 1 tablewriter.write_tabledata( w.write, array.data, array.mask, fields, supports_empty_values, indent, 1 << 8, ) else: write = w.write indent_spaces = w.get_indentation_spaces() tr_start = indent_spaces + "<TR>\n" tr_end = indent_spaces + "</TR>\n" td = indent_spaces + " <TD>{}</TD>\n" td_empty = indent_spaces + " <TD/>\n" fields = [ ( i, field.converter.output, field.converter.supports_empty_values(kwargs), ) for i, field in enumerate(fields) ] for row in range(len(array)): write(tr_start) array_row = array.data[row] mask_row = array.mask[row] for i, output, supports_empty_values in fields: data = array_row[i] masked = mask_row[i] if supports_empty_values and np.all(masked): write(td_empty) else: try: val = output(data, masked) except Exception as e: vo_reraise( e, additional="(in row {:d}, col '{}')".format( row, self.fields[i].ID ), ) if len(val): write(td.format(val)) else: write(td_empty) write(tr_end) def _write_binary(self, mode, w, **kwargs): fields = self.fields array = self.array if mode == 1: tag_name = "BINARY" else: tag_name = "BINARY2" with w.tag(tag_name): with w.tag("STREAM", encoding="base64"): fields_basic = [ (i, field.converter.binoutput) for (i, field) in enumerate(fields) ] data = io.BytesIO() for row in range(len(array)): array_row = array.data[row] array_mask = array.mask[row] if mode == 2: flattened = np.array([np.all(x) for x in array_mask]) data.write(converters.bool_to_bitarray(flattened)) for i, converter in fields_basic: try: chunk = converter(array_row[i], array_mask[i]) assert type(chunk) == bytes except Exception as e: vo_reraise( e, additional=f"(in row {row:d}, col '{fields[i].ID}')" ) data.write(chunk) w._flush() w.write(base64.b64encode(data.getvalue()).decode("ascii")) def to_table(self, use_names_over_ids=False): """ Convert this VO Table to an `astropy.table.Table` instance. Parameters ---------- use_names_over_ids : bool, optional When `True` use the ``name`` attributes of columns as the names of columns in the `astropy.table.Table` instance. Since names are not guaranteed to be unique, this may cause some columns to be renamed by appending numbers to the end. Otherwise (default), use the ID attributes as the column names. .. warning:: Variable-length array fields may not be restored identically when round-tripping through the `astropy.table.Table` instance. """ from astropy.table import Table meta = {} for key in ["ID", "name", "ref", "ucd", "utype", "description"]: val = getattr(self, key, None) if val is not None: meta[key] = val if use_names_over_ids: names = [field.name for field in self.fields] unique_names = [] for i, name in enumerate(names): new_name = name i = 2 while new_name in unique_names: new_name = f"{name}{i}" i += 1 unique_names.append(new_name) names = unique_names else: names = [field.ID for field in self.fields] table = Table(self.array, names=names, meta=meta) for name, field in zip(names, self.fields): column = table[name] field.to_table_column(column) return table @classmethod def from_table(cls, votable, table): """ Create a `Table` instance from a given `astropy.table.Table` instance. """ kwargs = {} for key in ["ID", "name", "ref", "ucd", "utype"]: val = table.meta.get(key) if val is not None: kwargs[key] = val new_table = cls(votable, **kwargs) if "description" in table.meta: new_table.description = table.meta["description"] for colname in table.colnames: column = table[colname] new_table.fields.append(Field.from_table_column(votable, column)) if table.mask is None: new_table.array = ma.array(np.asarray(table)) else: new_table.array = ma.array(np.asarray(table), mask=np.asarray(table.mask)) return new_table def iter_fields_and_params(self): """ Recursively iterate over all FIELD and PARAM elements in the TABLE. """ yield from self.params yield from self.fields for group in self.groups: yield from group.iter_fields_and_params() get_field_by_id = _lookup_by_attr_factory( "ID", True, "iter_fields_and_params", "FIELD or PARAM", """ Looks up a FIELD or PARAM element by the given ID. """, ) get_field_by_id_or_name = _lookup_by_id_or_name_factory( "iter_fields_and_params", "FIELD or PARAM", """ Looks up a FIELD or PARAM element by the given ID or name. """, ) get_fields_by_utype = _lookup_by_attr_factory( "utype", False, "iter_fields_and_params", "FIELD or PARAM", """ Looks up a FIELD or PARAM element by the given utype and returns an iterator emitting all matches. """, ) def iter_groups(self): """ Recursively iterate over all GROUP elements in the TABLE. """ for group in self.groups: yield group yield from group.iter_groups() get_group_by_id = _lookup_by_attr_factory( "ID", True, "iter_groups", "GROUP", """ Looks up a GROUP element by the given ID. Used by the group's "ref" attribute """, ) get_groups_by_utype = _lookup_by_attr_factory( "utype", False, "iter_groups", "GROUP", """ Looks up a GROUP element by the given utype and returns an iterator emitting all matches. """, ) def iter_info(self): yield from self.infos class Resource( Element, _IDProperty, _NameProperty, _UtypeProperty, _DescriptionProperty ): """ RESOURCE_ element: Groups TABLE_ and RESOURCE_ elements. The keyword arguments correspond to setting members of the same name, documented below. """ def __init__( self, name=None, ID=None, utype=None, type="results", id=None, config=None, pos=None, **kwargs, ): if config is None: config = {} self._config = config self._pos = pos Element.__init__(self) self.name = name self.ID = resolve_id(ID, id, config, pos) self.utype = utype self.type = type self._extra_attributes = kwargs self.description = None self._coordinate_systems = HomogeneousList(CooSys) self._time_systems = HomogeneousList(TimeSys) self._groups = HomogeneousList(Group) self._params = HomogeneousList(Param) self._infos = HomogeneousList(Info) self._links = HomogeneousList(Link) self._tables = HomogeneousList(Table) self._resources = HomogeneousList(Resource) warn_unknown_attrs("RESOURCE", kwargs.keys(), config, pos) def __repr__(self): buff = io.StringIO() w = XMLWriter(buff) w.element(self._element_name, attrib=w.object_attrs(self, self._attr_list)) return buff.getvalue().strip() @property def type(self): """ [*required*] The type of the resource. Must be either: - 'results': This resource contains actual result values (default) - 'meta': This resource contains only datatype descriptions (FIELD_ elements), but no actual data. """ return self._type @type.setter def type(self, type): if type not in ("results", "meta"): vo_raise(E18, type, self._config, self._pos) self._type = type @property def extra_attributes(self): """ A dictionary of string keys to string values containing any extra attributes of the RESOURCE_ element that are not defined in the specification. (The specification explicitly allows for extra attributes here, but nowhere else.) """ return self._extra_attributes @property def coordinate_systems(self): """ A list of coordinate system definitions (COOSYS_ elements) for the RESOURCE_. Must contain only `CooSys` objects. """ return self._coordinate_systems @property def time_systems(self): """ A list of time system definitions (TIMESYS_ elements) for the RESOURCE_. Must contain only `TimeSys` objects. """ return self._time_systems @property def infos(self): """ A list of informational parameters (key-value pairs) for the resource. Must only contain `Info` objects. """ return self._infos @property def groups(self): """ A list of groups """ return self._groups @property def params(self): """ A list of parameters (constant-valued columns) for the resource. Must contain only `Param` objects. """ return self._params @property def links(self): """ A list of links (pointers to other documents or servers through a URI) for the resource. Must contain only `Link` objects. """ return self._links @property def tables(self): """ A list of tables in the resource. Must contain only `Table` objects. """ return self._tables @property def resources(self): """ A list of nested resources inside this resource. Must contain only `Resource` objects. """ return self._resources def _add_table(self, iterator, tag, data, config, pos): table = Table(self._votable, config=config, pos=pos, **data) self.tables.append(table) table.parse(iterator, config) def _add_info(self, iterator, tag, data, config, pos): info = Info(config=config, pos=pos, **data) self.infos.append(info) info.parse(iterator, config) def _add_group(self, iterator, tag, data, config, pos): group = Group(self, config=config, pos=pos, **data) self.groups.append(group) group.parse(iterator, config) def _add_param(self, iterator, tag, data, config, pos): param = Param(self._votable, config=config, pos=pos, **data) self.params.append(param) param.parse(iterator, config) def _add_coosys(self, iterator, tag, data, config, pos): coosys = CooSys(config=config, pos=pos, **data) self.coordinate_systems.append(coosys) coosys.parse(iterator, config) def _add_timesys(self, iterator, tag, data, config, pos): timesys = TimeSys(config=config, pos=pos, **data) self.time_systems.append(timesys) timesys.parse(iterator, config) def _add_resource(self, iterator, tag, data, config, pos): resource = Resource(config=config, pos=pos, **data) self.resources.append(resource) resource.parse(self._votable, iterator, config) def _add_link(self, iterator, tag, data, config, pos): link = Link(config=config, pos=pos, **data) self.links.append(link) link.parse(iterator, config) def parse(self, votable, iterator, config): self._votable = votable tag_mapping = { "TABLE": self._add_table, "INFO": self._add_info, "PARAM": self._add_param, "GROUP": self._add_group, "COOSYS": self._add_coosys, "TIMESYS": self._add_timesys, "RESOURCE": self._add_resource, "LINK": self._add_link, "DESCRIPTION": self._ignore_add, } for start, tag, data, pos in iterator: if start: tag_mapping.get(tag, self._add_unknown_tag)( iterator, tag, data, config, pos ) elif tag == "DESCRIPTION": if self.description is not None: warn_or_raise(W17, W17, "RESOURCE", config, pos) self.description = data or None elif tag == "RESOURCE": break del self._votable return self def to_xml(self, w, **kwargs): attrs = w.object_attrs(self, ("ID", "type", "utype")) attrs.update(self.extra_attributes) with w.tag("RESOURCE", attrib=attrs): if self.description is not None: w.element("DESCRIPTION", self.description, wrap=True) for element_set in ( self.coordinate_systems, self.time_systems, self.params, self.infos, self.links, self.tables, self.resources, ): for element in element_set: element.to_xml(w, **kwargs) def iter_tables(self): """ Recursively iterates over all tables in the resource and nested resources. """ yield from self.tables for resource in self.resources: yield from resource.iter_tables() def iter_fields_and_params(self): """ Recursively iterates over all FIELD_ and PARAM_ elements in the resource, its tables and nested resources. """ yield from self.params for table in self.tables: yield from table.iter_fields_and_params() for resource in self.resources: yield from resource.iter_fields_and_params() def iter_coosys(self): """ Recursively iterates over all the COOSYS_ elements in the resource and nested resources. """ yield from self.coordinate_systems for resource in self.resources: yield from resource.iter_coosys() def iter_timesys(self): """ Recursively iterates over all the TIMESYS_ elements in the resource and nested resources. """ yield from self.time_systems for resource in self.resources: yield from resource.iter_timesys() def iter_info(self): """ Recursively iterates over all the INFO_ elements in the resource and nested resources. """ yield from self.infos for table in self.tables: yield from table.iter_info() for resource in self.resources: yield from resource.iter_info() class VOTableFile(Element, _IDProperty, _DescriptionProperty): """ VOTABLE_ element: represents an entire file. The keyword arguments correspond to setting members of the same name, documented below. *version* is settable at construction time only, since conformance tests for building the rest of the structure depend on it. """ def __init__(self, ID=None, id=None, config=None, pos=None, version="1.4"): if config is None: config = {} self._config = config self._pos = pos Element.__init__(self) self.ID = resolve_id(ID, id, config, pos) self.description = None self._coordinate_systems = HomogeneousList(CooSys) self._time_systems = HomogeneousList(TimeSys) self._params = HomogeneousList(Param) self._infos = HomogeneousList(Info) self._resources = HomogeneousList(Resource) self._groups = HomogeneousList(Group) version = str(version) if version == "1.0": warnings.warn( "VOTable 1.0 support is deprecated in astropy 4.3 and will be " "removed in a future release", AstropyDeprecationWarning, ) elif (version != "1.0") and (version not in self._version_namespace_map): allowed_from_map = "', '".join(self._version_namespace_map) raise ValueError(f"'version' should be in ('1.0', '{allowed_from_map}').") self._version = version def __repr__(self): n_tables = len(list(self.iter_tables())) return f"<VOTABLE>... {n_tables} tables ...</VOTABLE>" @property def version(self): """ The version of the VOTable specification that the file uses. """ return self._version @version.setter def version(self, version): version = str(version) if version not in self._version_namespace_map: allowed_from_map = "', '".join(self._version_namespace_map) raise ValueError( "astropy.io.votable only supports VOTable versions" f" '{allowed_from_map}'" ) self._version = version @property def coordinate_systems(self): """ A list of coordinate system descriptions for the file. Must contain only `CooSys` objects. """ return self._coordinate_systems @property def time_systems(self): """ A list of time system descriptions for the file. Must contain only `TimeSys` objects. """ return self._time_systems @property def params(self): """ A list of parameters (constant-valued columns) that apply to the entire file. Must contain only `Param` objects. """ return self._params @property def infos(self): """ A list of informational parameters (key-value pairs) for the entire file. Must only contain `Info` objects. """ return self._infos @property def resources(self): """ A list of resources, in the order they appear in the file. Must only contain `Resource` objects. """ return self._resources @property def groups(self): """ A list of groups, in the order they appear in the file. Only supported as a child of the VOTABLE element in VOTable 1.2 or later. """ return self._groups def _add_param(self, iterator, tag, data, config, pos): param = Param(self, config=config, pos=pos, **data) self.params.append(param) param.parse(iterator, config) def _add_resource(self, iterator, tag, data, config, pos): resource = Resource(config=config, pos=pos, **data) self.resources.append(resource) resource.parse(self, iterator, config) def _add_coosys(self, iterator, tag, data, config, pos): coosys = CooSys(config=config, pos=pos, **data) self.coordinate_systems.append(coosys) coosys.parse(iterator, config) def _add_timesys(self, iterator, tag, data, config, pos): timesys = TimeSys(config=config, pos=pos, **data) self.time_systems.append(timesys) timesys.parse(iterator, config) def _add_info(self, iterator, tag, data, config, pos): info = Info(config=config, pos=pos, **data) self.infos.append(info) info.parse(iterator, config) def _add_group(self, iterator, tag, data, config, pos): if not config.get("version_1_2_or_later"): warn_or_raise(W26, W26, ("GROUP", "VOTABLE", "1.2"), config, pos) group = Group(self, config=config, pos=pos, **data) self.groups.append(group) group.parse(iterator, config) def _get_version_checks(self): config = {} config["version_1_1_or_later"] = util.version_compare(self.version, "1.1") >= 0 config["version_1_2_or_later"] = util.version_compare(self.version, "1.2") >= 0 config["version_1_3_or_later"] = util.version_compare(self.version, "1.3") >= 0 config["version_1_4_or_later"] = util.version_compare(self.version, "1.4") >= 0 return config # Map VOTable version numbers to namespace URIs and schema information. _version_namespace_map = { # Version 1.0 isn't well-supported, but is allowed on parse (with a warning). # It used DTD rather than schema, so this information would not be useful. # By omitting 1.0 from this dict we can use the keys as the list of versions # that are allowed in various other checks. "1.1": { "namespace_uri": "http://www.ivoa.net/xml/VOTable/v1.1", "schema_location_attr": "xsi:noNamespaceSchemaLocation", "schema_location_value": "http://www.ivoa.net/xml/VOTable/v1.1", }, "1.2": { "namespace_uri": "http://www.ivoa.net/xml/VOTable/v1.2", "schema_location_attr": "xsi:noNamespaceSchemaLocation", "schema_location_value": "http://www.ivoa.net/xml/VOTable/v1.2", }, # With 1.3 we'll be more explicit with the schema location. # - xsi:schemaLocation uses the namespace name along with the URL # to reference it. # - For convenience, but somewhat confusingly, the namespace URIs # are also usable URLs for accessing an applicable schema. # However to avoid confusion, we'll use the explicit schema URL. "1.3": { "namespace_uri": "http://www.ivoa.net/xml/VOTable/v1.3", "schema_location_attr": "xsi:schemaLocation", "schema_location_value": ( "http://www.ivoa.net/xml/VOTable/v1.3" " http://www.ivoa.net/xml/VOTable/VOTable-1.3.xsd" ), }, # With 1.4 namespace URIs stopped incrementing with minor version changes # so we use the same URI as with 1.3. See this IVOA note for more info: # http://www.ivoa.net/documents/Notes/XMLVers/20180529/ "1.4": { "namespace_uri": "http://www.ivoa.net/xml/VOTable/v1.3", "schema_location_attr": "xsi:schemaLocation", "schema_location_value": ( "http://www.ivoa.net/xml/VOTable/v1.3" " http://www.ivoa.net/xml/VOTable/VOTable-1.4.xsd" ), }, } def parse(self, iterator, config): config["_current_table_number"] = 0 for start, tag, data, pos in iterator: if start: if tag == "xml": pass elif tag == "VOTABLE": if "version" not in data: warn_or_raise(W20, W20, self.version, config, pos) config["version"] = self.version else: config["version"] = self._version = data["version"] if config["version"].lower().startswith("v"): warn_or_raise(W29, W29, config["version"], config, pos) self._version = config["version"] = config["version"][1:] if config["version"] not in self._version_namespace_map: vo_warn(W21, config["version"], config, pos) if "xmlns" in data: ns_info = self._version_namespace_map.get(config["version"], {}) correct_ns = ns_info.get("namespace_uri") if data["xmlns"] != correct_ns: vo_warn(W41, (correct_ns, data["xmlns"]), config, pos) else: vo_warn(W42, (), config, pos) break else: vo_raise(E19, (), config, pos) config.update(self._get_version_checks()) tag_mapping = { "PARAM": self._add_param, "RESOURCE": self._add_resource, "COOSYS": self._add_coosys, "TIMESYS": self._add_timesys, "INFO": self._add_info, "DEFINITIONS": self._add_definitions, "DESCRIPTION": self._ignore_add, "GROUP": self._add_group, } for start, tag, data, pos in iterator: if start: tag_mapping.get(tag, self._add_unknown_tag)( iterator, tag, data, config, pos ) elif tag == "DESCRIPTION": if self.description is not None: warn_or_raise(W17, W17, "VOTABLE", config, pos) self.description = data or None if not len(self.resources) and config["version_1_2_or_later"]: warn_or_raise(W53, W53, (), config, pos) return self def to_xml( self, fd, compressed=False, tabledata_format=None, _debug_python_based_parser=False, _astropy_version=None, ): """ Write to an XML file. Parameters ---------- fd : str or file-like Where to write the file. If a file-like object, must be writable. compressed : bool, optional When `True`, write to a gzip-compressed file. (Default: `False`) tabledata_format : str, optional Override the format of the table(s) data to write. Must be one of ``tabledata`` (text representation), ``binary`` or ``binary2``. By default, use the format that was specified in each `Table` object as it was created or read in. See :ref:`astropy:votable-serialization`. """ if tabledata_format is not None: if tabledata_format.lower() not in ("tabledata", "binary", "binary2"): raise ValueError(f"Unknown format type '{format}'") kwargs = { "version": self.version, "tabledata_format": tabledata_format, "_debug_python_based_parser": _debug_python_based_parser, "_group_number": 1, } kwargs.update(self._get_version_checks()) with util.convert_to_writable_filelike(fd, compressed=compressed) as fd: w = XMLWriter(fd) version = self.version if _astropy_version is None: lib_version = astropy_version else: lib_version = _astropy_version xml_header = """ <?xml version="1.0" encoding="utf-8"?> <!-- Produced with astropy.io.votable version {lib_version} http://www.astropy.org/ -->\n""" w.write(xml_header.lstrip().format(**locals())) # Build the VOTABLE tag attributes. votable_attr = { "version": version, "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", } ns_info = self._version_namespace_map.get(version, {}) namespace_uri = ns_info.get("namespace_uri") if namespace_uri: votable_attr["xmlns"] = namespace_uri schema_location_attr = ns_info.get("schema_location_attr") schema_location_value = ns_info.get("schema_location_value") if schema_location_attr and schema_location_value: votable_attr[schema_location_attr] = schema_location_value with w.tag("VOTABLE", votable_attr): if self.description is not None: w.element("DESCRIPTION", self.description, wrap=True) element_sets = [ self.coordinate_systems, self.time_systems, self.params, self.infos, self.resources, ] if kwargs["version_1_2_or_later"]: element_sets[0] = self.groups for element_set in element_sets: for element in element_set: element.to_xml(w, **kwargs) def iter_tables(self): """ Iterates over all tables in the VOTable file in a "flat" way, ignoring the nesting of resources etc. """ for resource in self.resources: yield from resource.iter_tables() def get_first_table(self): """ Often, you know there is only one table in the file, and that's all you need. This method returns that first table. """ for table in self.iter_tables(): if not table.is_empty(): return table raise IndexError("No table found in VOTABLE file.") get_table_by_id = _lookup_by_attr_factory( "ID", True, "iter_tables", "TABLE", """ Looks up a TABLE_ element by the given ID. Used by the table "ref" attribute. """, ) get_tables_by_utype = _lookup_by_attr_factory( "utype", False, "iter_tables", "TABLE", """ Looks up a TABLE_ element by the given utype, and returns an iterator emitting all matches. """, ) def get_table_by_index(self, idx): """ Get a table by its ordinal position in the file. """ for i, table in enumerate(self.iter_tables()): if i == idx: return table raise IndexError(f"No table at index {idx:d} found in VOTABLE file.") def iter_fields_and_params(self): """ Recursively iterate over all FIELD_ and PARAM_ elements in the VOTABLE_ file. """ for resource in self.resources: yield from resource.iter_fields_and_params() get_field_by_id = _lookup_by_attr_factory( "ID", True, "iter_fields_and_params", "FIELD", """ Looks up a FIELD_ element by the given ID_. Used by the field's "ref" attribute. """, ) get_fields_by_utype = _lookup_by_attr_factory( "utype", False, "iter_fields_and_params", "FIELD", """ Looks up a FIELD_ element by the given utype and returns an iterator emitting all matches. """, ) get_field_by_id_or_name = _lookup_by_id_or_name_factory( "iter_fields_and_params", "FIELD", """ Looks up a FIELD_ element by the given ID_ or name. """, ) def iter_values(self): """ Recursively iterate over all VALUES_ elements in the VOTABLE_ file. """ for field in self.iter_fields_and_params(): yield field.values get_values_by_id = _lookup_by_attr_factory( "ID", True, "iter_values", "VALUES", """ Looks up a VALUES_ element by the given ID. Used by the values "ref" attribute. """, ) def iter_groups(self): """ Recursively iterate over all GROUP_ elements in the VOTABLE_ file. """ for table in self.iter_tables(): yield from table.iter_groups() get_group_by_id = _lookup_by_attr_factory( "ID", True, "iter_groups", "GROUP", """ Looks up a GROUP_ element by the given ID. Used by the group's "ref" attribute """, ) get_groups_by_utype = _lookup_by_attr_factory( "utype", False, "iter_groups", "GROUP", """ Looks up a GROUP_ element by the given utype and returns an iterator emitting all matches. """, ) def iter_coosys(self): """ Recursively iterate over all COOSYS_ elements in the VOTABLE_ file. """ yield from self.coordinate_systems for resource in self.resources: yield from resource.iter_coosys() get_coosys_by_id = _lookup_by_attr_factory( "ID", True, "iter_coosys", "COOSYS", """Looks up a COOSYS_ element by the given ID.""", ) def iter_timesys(self): """ Recursively iterate over all TIMESYS_ elements in the VOTABLE_ file. """ yield from self.time_systems for resource in self.resources: yield from resource.iter_timesys() get_timesys_by_id = _lookup_by_attr_factory( "ID", True, "iter_timesys", "TIMESYS", """Looks up a TIMESYS_ element by the given ID.""", ) def iter_info(self): """ Recursively iterate over all INFO_ elements in the VOTABLE_ file. """ yield from self.infos for resource in self.resources: yield from resource.iter_info() get_info_by_id = _lookup_by_attr_factory( "ID", True, "iter_info", "INFO", """Looks up a INFO element by the given ID.""" ) def set_all_tables_format(self, format): """ Set the output storage format of all tables in the file. """ for table in self.iter_tables(): table.format = format @classmethod def from_table(cls, table, table_id=None): """ Create a `VOTableFile` instance from a given `astropy.table.Table` instance. Parameters ---------- table_id : str, optional Set the given ID attribute on the returned Table instance. """ votable_file = cls() resource = Resource() votable = Table.from_table(votable_file, table) if table_id is not None: votable.ID = table_id resource.tables.append(votable) votable_file.resources.append(resource) return votable_file
4de1b8e349b0914b7676ac70e815dfab3bfab3a5c4be8deeee40d386df111003
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ This file contains a contains the high-level functions to read a VOTable file. """ # STDLIB import io import os import sys import textwrap import warnings from astropy.utils import data from astropy.utils.decorators import deprecated_renamed_argument from astropy.utils.xml import iterparser # LOCAL from . import exceptions, tree __all__ = [ "parse", "parse_single_table", "from_table", "writeto", "validate", "reset_vo_warnings", ] VERIFY_OPTIONS = ["ignore", "warn", "exception"] @deprecated_renamed_argument("pedantic", "verify", since="5.0") def parse( source, columns=None, invalid="exception", verify=None, chunk_size=tree.DEFAULT_CHUNK_SIZE, table_number=None, table_id=None, filename=None, unit_format=None, datatype_mapping=None, _debug_python_based_parser=False, ): """ Parses a VOTABLE_ xml file (or file-like object), and returns a `~astropy.io.votable.tree.VOTableFile` object. Parameters ---------- source : path-like or file-like Path or file-like object containing a VOTABLE_ xml file. If file, must be readable. columns : sequence of str, optional List of field names to include in the output. The default is to include all fields. invalid : str, optional One of the following values: - 'exception': throw an exception when an invalid value is encountered (default) - 'mask': mask out invalid values verify : {'ignore', 'warn', 'exception'}, optional When ``'exception'``, raise an error when the file violates the spec, otherwise either issue a warning (``'warn'``) or silently continue (``'ignore'``). Warnings may be controlled using the standard Python mechanisms. See the `warnings` module in the Python standard library for more information. When not provided, uses the configuration setting ``astropy.io.votable.verify``, which defaults to 'ignore'. .. versionchanged:: 4.0 ``verify`` replaces the ``pedantic`` argument, which will be deprecated in future. .. versionchanged:: 5.0 The ``pedantic`` argument is deprecated. chunk_size : int, optional The number of rows to read before converting to an array. Higher numbers are likely to be faster, but will consume more memory. table_number : int, optional The number of table in the file to read in. If `None`, all tables will be read. If a number, 0 refers to the first table in the file, and only that numbered table will be parsed and read in. Should not be used with ``table_id``. table_id : str, optional The ID of the table in the file to read in. Should not be used with ``table_number``. filename : str, optional A filename, URL or other identifier to use in error messages. If *filename* is None and *source* is a string (i.e. a path), then *source* will be used as a filename for error messages. Therefore, *filename* is only required when source is a file-like object. unit_format : str, astropy.units.format.Base instance or None, optional The unit format to use when parsing unit attributes. If a string, must be the name of a unit formatter. The built-in formats include ``generic``, ``fits``, ``cds``, and ``vounit``. A custom formatter may be provided by passing a `~astropy.units.UnitBase` instance. If `None` (default), the unit format to use will be the one specified by the VOTable specification (which is ``cds`` up to version 1.3 of VOTable, and ``vounit`` in more recent versions of the spec). datatype_mapping : dict, optional A mapping of datatype names (`str`) to valid VOTable datatype names (str). For example, if the file being read contains the datatype "unsignedInt" (an invalid datatype in VOTable), include the mapping ``{"unsignedInt": "long"}``. Returns ------- votable : `~astropy.io.votable.tree.VOTableFile` object See Also -------- astropy.io.votable.exceptions : The exceptions this function may raise. """ from . import conf invalid = invalid.lower() if invalid not in ("exception", "mask"): raise ValueError( "accepted values of ``invalid`` are: ``'exception'`` or ``'mask'``." ) if verify is None: conf_verify_lowercase = conf.verify.lower() # We need to allow verify to be booleans as strings since the # configuration framework doesn't make it easy/possible to have mixed # types. if conf_verify_lowercase in ["false", "true"]: verify = conf_verify_lowercase == "true" else: verify = conf_verify_lowercase if isinstance(verify, bool): verify = "exception" if verify else "warn" elif verify not in VERIFY_OPTIONS: raise ValueError(f"verify should be one of {'/'.join(VERIFY_OPTIONS)}") if datatype_mapping is None: datatype_mapping = {} config = { "columns": columns, "invalid": invalid, "verify": verify, "chunk_size": chunk_size, "table_number": table_number, "filename": filename, "unit_format": unit_format, "datatype_mapping": datatype_mapping, } if isinstance(source, str): source = os.path.expanduser(source) if filename is None and isinstance(source, str): config["filename"] = source with iterparser.get_xml_iterator( source, _debug_python_based_parser=_debug_python_based_parser ) as iterator: return tree.VOTableFile(config=config, pos=(1, 1)).parse(iterator, config) def parse_single_table(source, **kwargs): """ Parses a VOTABLE_ xml file (or file-like object), reading and returning only the first `~astropy.io.votable.tree.Table` instance. See `parse` for a description of the keyword arguments. Returns ------- votable : `~astropy.io.votable.tree.Table` object """ if kwargs.get("table_number") is None: kwargs["table_number"] = 0 votable = parse(source, **kwargs) return votable.get_first_table() def writeto(table, file, tabledata_format=None): """ Writes a `~astropy.io.votable.tree.VOTableFile` to a VOTABLE_ xml file. Parameters ---------- table : `~astropy.io.votable.tree.VOTableFile` or `~astropy.table.Table` instance. file : str or writable file-like Path or file object to write to tabledata_format : str, optional Override the format of the table(s) data to write. Must be one of ``tabledata`` (text representation), ``binary`` or ``binary2``. By default, use the format that was specified in each ``table`` object as it was created or read in. See :ref:`astropy:astropy:votable-serialization`. """ from astropy.table import Table if isinstance(table, Table): table = tree.VOTableFile.from_table(table) elif not isinstance(table, tree.VOTableFile): raise TypeError( "first argument must be astropy.io.vo.VOTableFile or " "astropy.table.Table instance" ) table.to_xml( file, tabledata_format=tabledata_format, _debug_python_based_parser=True ) def validate(source, output=sys.stdout, xmllint=False, filename=None): """ Prints a validation report for the given file. Parameters ---------- source : path-like or file-like Path to a VOTABLE_ xml file or `~pathlib.Path` object having Path to a VOTABLE_ xml file. If file-like object, must be readable. output : file-like, optional Where to output the report. Defaults to ``sys.stdout``. If `None`, the output will be returned as a string. Must be writable. xmllint : bool, optional When `True`, also send the file to ``xmllint`` for schema and DTD validation. Requires that ``xmllint`` is installed. The default is `False`. ``source`` must be a file on the local filesystem in order for ``xmllint`` to work. filename : str, optional A filename to use in the error messages. If not provided, one will be automatically determined from ``source``. Returns ------- is_valid : bool or str Returns `True` if no warnings were found. If ``output`` is `None`, the return value will be a string. """ from astropy.utils.console import color_print, print_code_line return_as_str = False if output is None: output = io.StringIO() return_as_str = True lines = [] votable = None reset_vo_warnings() if isinstance(source, str): source = os.path.expanduser(source) with data.get_readable_fileobj(source, encoding="binary") as fd: content = fd.read() content_buffer = io.BytesIO(content) content_buffer.seek(0) if filename is None: if isinstance(source, str): filename = source elif hasattr(source, "name"): filename = source.name elif hasattr(source, "url"): filename = source.url else: filename = "<unknown>" with warnings.catch_warnings(record=True) as warning_lines: warnings.resetwarnings() warnings.simplefilter("always", exceptions.VOWarning, append=True) try: votable = parse(content_buffer, verify="warn", filename=filename) except ValueError as e: lines.append(str(e)) lines = [ str(x.message) for x in warning_lines if issubclass(x.category, exceptions.VOWarning) ] + lines content_buffer.seek(0) output.write(f"Validation report for {filename}\n\n") if len(lines): xml_lines = iterparser.xml_readlines(content_buffer) for warning in lines: w = exceptions.parse_vowarning(warning) if not w["is_something"]: output.write(w["message"]) output.write("\n\n") else: line = xml_lines[w["nline"] - 1] warning = w["warning"] if w["is_warning"]: color = "yellow" else: color = "red" color_print( f"{w['nline']:d}: ", "", warning or "EXC", color, ": ", "", textwrap.fill( w["message"], initial_indent=" ", subsequent_indent=" ", ).lstrip(), file=output, ) print_code_line(line, w["nchar"], file=output) output.write("\n") else: output.write("astropy.io.votable found no violations.\n\n") success = 0 if xmllint and os.path.exists(filename): from . import xmlutil if votable is None: version = "1.1" else: version = votable.version success, stdout, stderr = xmlutil.validate_schema(filename, version) if success != 0: output.write("xmllint schema violations:\n\n") output.write(stderr.decode("utf-8")) else: output.write("xmllint passed\n") if return_as_str: return output.getvalue() return len(lines) == 0 and success == 0 def from_table(table, table_id=None): """ Given an `~astropy.table.Table` object, return a `~astropy.io.votable.tree.VOTableFile` file structure containing just that single table. Parameters ---------- table : `~astropy.table.Table` instance table_id : str, optional If not `None`, set the given id on the returned `~astropy.io.votable.tree.Table` instance. Returns ------- votable : `~astropy.io.votable.tree.VOTableFile` instance """ return tree.VOTableFile.from_table(table, table_id=table_id) def is_votable(source): """ Reads the header of a file to determine if it is a VOTable file. Parameters ---------- source : path-like or file-like Path or file object containing a VOTABLE_ xml file. If file, must be readable. Returns ------- is_votable : bool Returns `True` if the given file is a VOTable file. """ if isinstance(source, str): source = os.path.expanduser(source) try: with iterparser.get_xml_iterator(source) as iterator: for start, tag, d, pos in iterator: if tag != "xml": return False break for start, tag, d, pos in iterator: if tag != "VOTABLE": return False break return True except ValueError: return False def reset_vo_warnings(): """ Resets all of the vo warning state so that warnings that have already been emitted will be emitted again. This is used, for example, by `validate` which must emit all warnings each time it is called. """ from . import converters, xmlutil # -----------------------------------------------------------# # This is a special variable used by the Python warnings # # infrastructure to keep track of warnings that have # # already been seen. Since we want to get every single # # warning out of this, we have to delete all of them first. # # -----------------------------------------------------------# for module in (converters, exceptions, tree, xmlutil): try: del module.__warningregistry__ except AttributeError: pass
9f5dca729ea209a946e6c6f8c622691f16522c8b49e5aed0869681e53ce8cbd2
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ This package reads and writes data formats used by the Virtual Observatory (VO) initiative, particularly the VOTable XML format. """ from astropy import config as _config from .exceptions import ( IOWarning, UnimplementedWarning, VOTableChangeWarning, VOTableSpecError, VOTableSpecWarning, VOWarning, ) from .table import from_table, is_votable, parse, parse_single_table, validate, writeto __all__ = [ "Conf", "conf", "parse", "parse_single_table", "validate", "from_table", "is_votable", "writeto", "VOWarning", "VOTableChangeWarning", "VOTableSpecWarning", "UnimplementedWarning", "IOWarning", "VOTableSpecError", ] class Conf(_config.ConfigNamespace): """ Configuration parameters for `astropy.io.votable`. """ verify = _config.ConfigItem( "ignore", "Can be 'exception' (treat fixable violations of the VOTable spec as " "exceptions), 'warn' (show warnings for VOTable spec violations), or " "'ignore' (silently ignore VOTable spec violations)", aliases=["astropy.io.votable.table.pedantic", "astropy.io.votable.pedantic"], ) conf = Conf()
901e6cbb6bcf068c63086fc296d64ae3dd72a080f92dab734efa570de037028f
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ .. _warnings: Warnings -------- .. note:: Most of the following warnings indicate violations of the VOTable specification. They should be reported to the authors of the tools that produced the VOTable file. To control the warnings emitted, use the standard Python :mod:`warnings` module and the ``astropy.io.votable.exceptions.conf.max_warnings`` configuration item. Most of these are of the type `VOTableSpecWarning`. {warnings} .. _exceptions: Exceptions ---------- .. note:: This is a list of many of the fatal exceptions emitted by ``astropy.io.votable`` when the file does not conform to spec. Other exceptions may be raised due to unforeseen cases or bugs in ``astropy.io.votable`` itself. {exceptions} """ # STDLIB import io import re from textwrap import dedent from warnings import warn from astropy import config as _config from astropy.utils.exceptions import AstropyWarning __all__ = [ "Conf", "conf", "warn_or_raise", "vo_raise", "vo_reraise", "vo_warn", "warn_unknown_attrs", "parse_vowarning", "VOWarning", "VOTableChangeWarning", "VOTableSpecWarning", "UnimplementedWarning", "IOWarning", "VOTableSpecError", ] # NOTE: Cannot put this in __init__.py due to circular import. class Conf(_config.ConfigNamespace): """ Configuration parameters for `astropy.io.votable.exceptions`. """ max_warnings = _config.ConfigItem( 10, "Number of times the same type of warning is displayed before being suppressed", cfgtype="integer", ) conf = Conf() def _format_message(message, name, config=None, pos=None): if config is None: config = {} if pos is None: pos = ("?", "?") filename = config.get("filename", "?") return f"{filename}:{pos[0]}:{pos[1]}: {name}: {message}" def _suppressed_warning(warning, config, stacklevel=2): warning_class = type(warning) config.setdefault("_warning_counts", dict()).setdefault(warning_class, 0) config["_warning_counts"][warning_class] += 1 message_count = config["_warning_counts"][warning_class] if message_count <= conf.max_warnings: if message_count == conf.max_warnings: warning.formatted_message += ( " (suppressing further warnings of this type...)" ) warn(warning, stacklevel=stacklevel + 1) def warn_or_raise( warning_class, exception_class=None, args=(), config=None, pos=None, stacklevel=1 ): """ Warn or raise an exception, depending on the verify setting. """ if config is None: config = {} # NOTE: the default here is deliberately warn rather than ignore, since # one would expect that calling warn_or_raise without config should not # silence the warnings. config_value = config.get("verify", "warn") if config_value == "exception": if exception_class is None: exception_class = warning_class vo_raise(exception_class, args, config, pos) elif config_value == "warn": vo_warn(warning_class, args, config, pos, stacklevel=stacklevel + 1) def vo_raise(exception_class, args=(), config=None, pos=None): """ Raise an exception, with proper position information if available. """ if config is None: config = {} raise exception_class(args, config, pos) def vo_reraise(exc, config=None, pos=None, additional=""): """ Raise an exception, with proper position information if available. Restores the original traceback of the exception, and should only be called within an "except:" block of code. """ if config is None: config = {} message = _format_message(str(exc), exc.__class__.__name__, config, pos) if message.split()[0] == str(exc).split()[0]: message = str(exc) if len(additional): message += " " + additional exc.args = (message,) raise exc def vo_warn(warning_class, args=(), config=None, pos=None, stacklevel=1): """ Warn, with proper position information if available. """ if config is None: config = {} # NOTE: the default here is deliberately warn rather than ignore, since # one would expect that calling warn_or_raise without config should not # silence the warnings. if config.get("verify", "warn") != "ignore": warning = warning_class(args, config, pos) _suppressed_warning(warning, config, stacklevel=stacklevel + 1) def warn_unknown_attrs(element, attrs, config, pos, good_attr=[], stacklevel=1): for attr in attrs: if attr not in good_attr: vo_warn(W48, (attr, element), config, pos, stacklevel=stacklevel + 1) _warning_pat = re.compile( r":?(?P<nline>[0-9?]+):(?P<nchar>[0-9?]+): " + r"((?P<warning>[WE]\d+): )?(?P<rest>.*)$" ) def parse_vowarning(line): """ Parses the vo warning string back into its parts. """ result = {} match = _warning_pat.search(line) if match: result["warning"] = warning = match.group("warning") if warning is not None: result["is_warning"] = warning[0].upper() == "W" result["is_exception"] = not result["is_warning"] result["number"] = int(match.group("warning")[1:]) result["doc_url"] = f"io/votable/api_exceptions.html#{warning.lower()}" else: result["is_warning"] = False result["is_exception"] = False result["is_other"] = True result["number"] = None result["doc_url"] = None try: result["nline"] = int(match.group("nline")) except ValueError: result["nline"] = 0 try: result["nchar"] = int(match.group("nchar")) except ValueError: result["nchar"] = 0 result["message"] = match.group("rest") result["is_something"] = True else: result["warning"] = None result["is_warning"] = False result["is_exception"] = False result["is_other"] = False result["is_something"] = False if not isinstance(line, str): line = line.decode("utf-8") result["message"] = line return result class VOWarning(AstropyWarning): """ The base class of all VO warnings and exceptions. Handles the formatting of the message with a warning or exception code, filename, line and column number. """ default_args = () message_template = "" def __init__(self, args, config=None, pos=None): if config is None: config = {} if not isinstance(args, tuple): args = (args,) msg = self.message_template.format(*args) self.formatted_message = _format_message( msg, self.__class__.__name__, config, pos ) Warning.__init__(self, self.formatted_message) def __str__(self): return self.formatted_message @classmethod def get_short_name(cls): if len(cls.default_args): return cls.message_template.format(*cls.default_args) return cls.message_template class VOTableChangeWarning(VOWarning, SyntaxWarning): """ A change has been made to the input XML file. """ class VOTableSpecWarning(VOWarning, SyntaxWarning): """ The input XML file violates the spec, but there is an obvious workaround. """ class UnimplementedWarning(VOWarning, SyntaxWarning): """ A feature of the VOTABLE_ spec is not implemented. """ class IOWarning(VOWarning, RuntimeWarning): """ A network or IO error occurred, but was recovered using the cache. """ class VOTableSpecError(VOWarning, ValueError): """ The input XML file violates the spec and there is no good workaround. """ class W01(VOTableSpecWarning): """ The VOTable spec states: If a cell contains an array or complex number, it should be encoded as multiple numbers separated by whitespace. Many VOTable files in the wild use commas as a separator instead, and ``astropy.io.votable`` can support this convention depending on the :ref:`astropy:verifying-votables` setting. ``astropy.io.votable`` always outputs files using only spaces, regardless of how they were input. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#toc-header-35>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:TABLEDATA>`__ """ message_template = "Array uses commas rather than whitespace" class W02(VOTableSpecWarning): r""" XML ids must match the following regular expression:: ^[A-Za-z_][A-Za-z0-9_\.\-]*$ The VOTable 1.1 says the following: According to the XML standard, the attribute ``ID`` is a string beginning with a letter or underscore (``_``), followed by a sequence of letters, digits, or any of the punctuation characters ``.`` (dot), ``-`` (dash), ``_`` (underscore), or ``:`` (colon). However, this is in conflict with the XML standard, which says colons may not be used. VOTable 1.1's own schema does not allow a colon here. Therefore, ``astropy.io.votable`` disallows the colon. VOTable 1.2 corrects this error in the specification. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:name>`__, `XML Names <https://www.w3.org/TR/xml-names/>`__ """ message_template = "{} attribute '{}' is invalid. Must be a standard XML id" default_args = ("x", "y") class W03(VOTableChangeWarning): """ The VOTable 1.1 spec says the following about ``name`` vs. ``ID`` on ``FIELD`` and ``VALUE`` elements: ``ID`` and ``name`` attributes have a different role in VOTable: the ``ID`` is meant as a *unique identifier* of an element seen as a VOTable component, while the ``name`` is meant for presentation purposes, and need not to be unique throughout the VOTable document. The ``ID`` attribute is therefore required in the elements which have to be referenced, but in principle any element may have an ``ID`` attribute. ... In summary, the ``ID`` is different from the ``name`` attribute in that (a) the ``ID`` attribute is made from a restricted character set, and must be unique throughout a VOTable document whereas names are standard XML attributes and need not be unique; and (b) there should be support in the parsing software to look up references and extract the relevant element with matching ``ID``. It is further recommended in the VOTable 1.2 spec: While the ``ID`` attribute has to be unique in a VOTable document, the ``name`` attribute need not. It is however recommended, as a good practice, to assign unique names within a ``TABLE`` element. This recommendation means that, between a ``TABLE`` and its corresponding closing ``TABLE`` tag, ``name`` attributes of ``FIELD``, ``PARAM`` and optional ``GROUP`` elements should be all different. Since ``astropy.io.votable`` requires a unique identifier for each of its columns, ``ID`` is used for the column name when present. However, when ``ID`` is not present, (since it is not required by the specification) ``name`` is used instead. However, ``name`` must be cleansed by replacing invalid characters (such as whitespace) with underscores. .. note:: This warning does not indicate that the input file is invalid with respect to the VOTable specification, only that the column names in the record array may not match exactly the ``name`` attributes specified in the file. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:name>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:name>`__ """ message_template = "Implicitly generating an ID from a name '{}' -> '{}'" default_args = ("x", "y") class W04(VOTableSpecWarning): """ The ``content-type`` attribute must use MIME content-type syntax as defined in `RFC 2046 <https://tools.ietf.org/html/rfc2046>`__. The current check for validity is somewhat over-permissive. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:link>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:link>`__ """ message_template = "content-type '{}' must be a valid MIME content type" default_args = ("x",) class W05(VOTableSpecWarning): """ The attribute must be a valid URI as defined in `RFC 2396 <https://www.ietf.org/rfc/rfc2396.txt>`_. """ message_template = "'{}' is not a valid URI" default_args = ("x",) class W06(VOTableSpecWarning): """ This warning is emitted when a ``ucd`` attribute does not match the syntax of a `unified content descriptor <http://vizier.u-strasbg.fr/doc/UCD.htx>`__. If the VOTable version is 1.2 or later, the UCD will also be checked to ensure it conforms to the controlled vocabulary defined by UCD1+. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:ucd>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:ucd>`__ """ message_template = "Invalid UCD '{}': {}" default_args = ("x", "explanation") class W07(VOTableSpecWarning): """ As astro year field is a Besselian or Julian year matching the regular expression:: ^[JB]?[0-9]+([.][0-9]*)?$ Defined in this XML Schema snippet:: <xs:simpleType name="astroYear"> <xs:restriction base="xs:token"> <xs:pattern value="[JB]?[0-9]+([.][0-9]*)?"/> </xs:restriction> </xs:simpleType> """ message_template = "Invalid astroYear in {}: '{}'" default_args = ("x", "y") class W08(VOTableSpecWarning): """ To avoid local-dependent number parsing differences, ``astropy.io.votable`` may require a string or unicode string where a numeric type may make more sense. """ message_template = "'{}' must be a str or bytes object" default_args = ("x",) class W09(VOTableSpecWarning): """ The VOTable specification uses the attribute name ``ID`` (with uppercase letters) to specify unique identifiers. Some VOTable-producing tools use the more standard lowercase ``id`` instead. ``astropy.io.votable`` accepts ``id`` and emits this warning if ``verify`` is ``'warn'``. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:name>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:name>`__ """ message_template = "ID attribute not capitalized" class W10(VOTableSpecWarning): """ The parser has encountered an element that does not exist in the specification, or appears in an invalid context. Check the file against the VOTable schema (with a tool such as `xmllint <http://xmlsoft.org/xmllint.html>`__. If the file validates against the schema, and you still receive this warning, this may indicate a bug in ``astropy.io.votable``. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#ToC54>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#ToC58>`__ """ message_template = "Unknown tag '{}'. Ignoring" default_args = ("x",) class W11(VOTableSpecWarning): """ Earlier versions of the VOTable specification used a ``gref`` attribute on the ``LINK`` element to specify a `GLU reference <http://aladin.u-strasbg.fr/glu/>`__. New files should specify a ``glu:`` protocol using the ``href`` attribute. Since ``astropy.io.votable`` does not currently support GLU references, it likewise does not automatically convert the ``gref`` attribute to the new form. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:link>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:link>`__ """ message_template = "The gref attribute on LINK is deprecated in VOTable 1.1" class W12(VOTableChangeWarning): """ In order to name the columns of the Numpy record array, each ``FIELD`` element must have either an ``ID`` or ``name`` attribute to derive a name from. Strictly speaking, according to the VOTable schema, the ``name`` attribute is required. However, if ``name`` is not present by ``ID`` is, and ``verify`` is not ``'exception'``, ``astropy.io.votable`` will continue without a ``name`` defined. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:name>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:name>`__ """ message_template = ( "'{}' element must have at least one of 'ID' or 'name' attributes" ) default_args = ("x",) class W13(VOTableSpecWarning): """ Some VOTable files in the wild use non-standard datatype names. These are mapped to standard ones using the following mapping:: string -> char unicodeString -> unicodeChar int16 -> short int32 -> int int64 -> long float32 -> float float64 -> double unsignedInt -> long unsignedShort -> int To add more datatype mappings during parsing, use the ``datatype_mapping`` keyword to `astropy.io.votable.parse`. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:datatypes>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:datatypes>`__ """ message_template = "'{}' is not a valid VOTable datatype, should be '{}'" default_args = ("x", "y") # W14: Deprecated class W15(VOTableSpecWarning): """ The ``name`` attribute is required on every ``FIELD`` element. However, many VOTable files in the wild omit it and provide only an ``ID`` instead. In this case, when ``verify`` is not ``'exception'`` ``astropy.io.votable`` will copy the ``name`` attribute to a new ``ID`` attribute. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:name>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:name>`__ """ message_template = "{} element missing required 'name' attribute" default_args = ("x",) # W16: Deprecated class W17(VOTableSpecWarning): """ A ``DESCRIPTION`` element can only appear once within its parent element. According to the schema, it may only occur once (`1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#ToC54>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#ToC58>`__) However, it is a `proposed extension <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:addesc>`__ to VOTable 1.2. """ message_template = "{} element contains more than one DESCRIPTION element" default_args = ("x",) class W18(VOTableSpecWarning): """ The number of rows explicitly specified in the ``nrows`` attribute does not match the actual number of rows (``TR`` elements) present in the ``TABLE``. This may indicate truncation of the file, or an internal error in the tool that produced it. If ``verify`` is not ``'exception'``, parsing will proceed, with the loss of some performance. **References:** `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#ToC10>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#ToC10>`__ """ message_template = "TABLE specified nrows={}, but table contains {} rows" default_args = ("x", "y") class W19(VOTableSpecWarning): """ The column fields as defined using ``FIELD`` elements do not match those in the headers of the embedded FITS file. If ``verify`` is not ``'exception'``, the embedded FITS file will take precedence. """ message_template = ( "The fields defined in the VOTable do not match those in the " + "embedded FITS file" ) class W20(VOTableSpecWarning): """ If no version number is explicitly given in the VOTable file, the parser assumes it is written to the VOTable 1.1 specification. """ message_template = "No version number specified in file. Assuming {}" default_args = ("1.1",) class W21(UnimplementedWarning): """ Unknown issues may arise using ``astropy.io.votable`` with VOTable files from a version other than 1.1, 1.2, 1.3, or 1.4. """ message_template = ( "astropy.io.votable is designed for VOTable version 1.1, 1.2, 1.3," " and 1.4, but this file is {}" ) default_args = ("x",) class W22(VOTableSpecWarning): """ Version 1.0 of the VOTable specification used the ``DEFINITIONS`` element to define coordinate systems. Version 1.1 now uses ``COOSYS`` elements throughout the document. **References:** `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:definitions>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:definitions>`__ """ message_template = "The DEFINITIONS element is deprecated in VOTable 1.1. Ignoring" class W23(IOWarning): """ Raised when the VO service database can not be updated (possibly due to a network outage). This is only a warning, since an older and possible out-of-date VO service database was available locally. """ message_template = "Unable to update service information for '{}'" default_args = ("x",) class W24(VOWarning, FutureWarning): """ The VO catalog database retrieved from the www is designed for a newer version of ``astropy.io.votable``. This may cause problems or limited features performing service queries. Consider upgrading ``astropy.io.votable`` to the latest version. """ message_template = ( "The VO catalog database is for a later version of astropy.io.votable" ) class W25(IOWarning): """ A VO service query failed due to a network error or malformed arguments. Another alternative service may be attempted. If all services fail, an exception will be raised. """ message_template = "'{}' failed with: {}" default_args = ("service", "...") class W26(VOTableSpecWarning): """ The given element was not supported inside of the given element until the specified VOTable version, however the version declared in the file is for an earlier version. These attributes may not be written out to the file. """ message_template = "'{}' inside '{}' added in VOTable {}" default_args = ("child", "parent", "X.X") class W27(VOTableSpecWarning): """ The ``COOSYS`` element was deprecated in VOTABLE version 1.2 in favor of a reference to the Space-Time Coordinate (STC) data model (see `utype <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:utype>`__ and the IVOA note `referencing STC in VOTable <http://ivoa.net/Documents/latest/VOTableSTC.html>`__. """ message_template = "COOSYS deprecated in VOTable 1.2" class W28(VOTableSpecWarning): """ The given attribute was not supported on the given element until the specified VOTable version, however the version declared in the file is for an earlier version. These attributes may not be written out to the file. """ message_template = "'{}' on '{}' added in VOTable {}" default_args = ("attribute", "element", "X.X") class W29(VOTableSpecWarning): """ Some VOTable files specify their version number in the form "v1.0", when the only supported forms in the spec are "1.0". **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#ToC54>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#ToC58>`__ """ message_template = "Version specified in non-standard form '{}'" default_args = ("v1.0",) class W30(VOTableSpecWarning): """ Some VOTable files write missing floating-point values in non-standard ways, such as "null" and "-". If ``verify`` is not ``'exception'``, any non-standard floating-point literals are treated as missing values. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:datatypes>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:datatypes>`__ """ message_template = "Invalid literal for float '{}'. Treating as empty." default_args = ("x",) class W31(VOTableSpecWarning): """ Since NaN's can not be represented in integer fields directly, a null value must be specified in the FIELD descriptor to support reading NaN's from the tabledata. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:datatypes>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:datatypes>`__ """ message_template = "NaN given in an integral field without a specified null value" class W32(VOTableSpecWarning): """ Each field in a table must have a unique ID. If two or more fields have the same ID, some will be renamed to ensure that all IDs are unique. From the VOTable 1.2 spec: The ``ID`` and ``ref`` attributes are defined as XML types ``ID`` and ``IDREF`` respectively. This means that the contents of ``ID`` is an identifier which must be unique throughout a VOTable document, and that the contents of the ``ref`` attribute represents a reference to an identifier which must exist in the VOTable document. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:name>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:name>`__ """ message_template = "Duplicate ID '{}' renamed to '{}' to ensure uniqueness" default_args = ("x", "x_2") class W33(VOTableChangeWarning): """ Each field in a table must have a unique name. If two or more fields have the same name, some will be renamed to ensure that all names are unique. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:name>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:name>`__ """ message_template = "Column name '{}' renamed to '{}' to ensure uniqueness" default_args = ("x", "x_2") class W34(VOTableSpecWarning): """ The attribute requires the value to be a valid XML token, as defined by `XML 1.0 <http://www.w3.org/TR/2000/WD-xml-2e-20000814#NT-Nmtoken>`__. """ message_template = "'{}' is an invalid token for attribute '{}'" default_args = ("x", "y") class W35(VOTableSpecWarning): """ The ``name`` and ``value`` attributes are required on all ``INFO`` elements. **References:** `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#ToC54>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#ToC32>`__ """ message_template = "'{}' attribute required for INFO elements" default_args = ("x",) class W36(VOTableSpecWarning): """ If the field specifies a ``null`` value, that value must conform to the given ``datatype``. **References:** `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:values>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:values>`__ """ message_template = "null value '{}' does not match field datatype, setting to 0" default_args = ("x",) class W37(UnimplementedWarning): """ The 3 datatypes defined in the VOTable specification and supported by ``astropy.io.votable`` are ``TABLEDATA``, ``BINARY`` and ``FITS``. **References:** `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:data>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:data>`__ """ message_template = "Unsupported data format '{}'" default_args = ("x",) class W38(VOTableSpecWarning): """ The only encoding for local binary data supported by the VOTable specification is base64. """ message_template = "Inline binary data must be base64 encoded, got '{}'" default_args = ("x",) class W39(VOTableSpecWarning): """ Bit values do not support masking. This warning is raised upon setting masked data in a bit column. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:datatypes>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:datatypes>`__ """ message_template = "Bit values can not be masked" class W40(VOTableSpecWarning): """ This is a terrible hack to support Simple Image Access Protocol results from `NOIRLab Astro Data Archive <https://astroarchive.noirlab.edu/>`__. It creates a field for the coordinate projection type of type "double", which actually contains character data. We have to hack the field to store character data, or we can't read it in. A warning will be raised when this happens. """ message_template = "'cprojection' datatype repaired" class W41(VOTableSpecWarning): """ An XML namespace was specified on the ``VOTABLE`` element, but the namespace does not match what is expected for a ``VOTABLE`` file. The ``VOTABLE`` namespace is:: http://www.ivoa.net/xml/VOTable/vX.X where "X.X" is the version number. Some files in the wild set the namespace to the location of the VOTable schema, which is not correct and will not pass some validating parsers. """ message_template = ( "An XML namespace is specified, but is incorrect. Expected '{}', got '{}'" ) default_args = ("x", "y") class W42(VOTableSpecWarning): """ The root element should specify a namespace. The ``VOTABLE`` namespace is:: http://www.ivoa.net/xml/VOTable/vX.X where "X.X" is the version number. """ message_template = "No XML namespace specified" class W43(VOTableSpecWarning): """ Referenced elements should be defined before referees. From the VOTable 1.2 spec: In VOTable1.2, it is further recommended to place the ID attribute prior to referencing it whenever possible. """ message_template = "{} ref='{}' which has not already been defined" default_args = ("element", "x") class W44(VOTableSpecWarning): """ ``VALUES`` elements that reference another element should not have their own content. From the VOTable 1.2 spec: The ``ref`` attribute of a ``VALUES`` element can be used to avoid a repetition of the domain definition, by referring to a previously defined ``VALUES`` element having the referenced ``ID`` attribute. When specified, the ``ref`` attribute defines completely the domain without any other element or attribute, as e.g. ``<VALUES ref="RAdomain"/>`` """ message_template = "VALUES element with ref attribute has content ('{}')" default_args = ("element",) class W45(VOWarning, ValueError): """ The ``content-role`` attribute on the ``LINK`` element must be one of the following:: query, hints, doc, location And in VOTable 1.3, additionally:: type **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#ToC54>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#ToC58>`__ `1.3 <http://www.ivoa.net/documents/VOTable/20130315/PR-VOTable-1.3-20130315.html#sec:link>`__ """ message_template = "content-role attribute '{}' invalid" default_args = ("x",) class W46(VOTableSpecWarning): """ The given char or unicode string is too long for the specified field length. """ message_template = "{} value is too long for specified length of {}" default_args = ("char or unicode", "x") class W47(VOTableSpecWarning): """ If no arraysize is specified on a char field, the default of '1' is implied, but this is rarely what is intended. """ message_template = "Missing arraysize indicates length 1" class W48(VOTableSpecWarning): """ The attribute is not defined in the specification. """ message_template = "Unknown attribute '{}' on {}" default_args = ("attribute", "element") class W49(VOTableSpecWarning): """ Prior to VOTable 1.3, the empty cell was illegal for integer fields. If a \"null\" value was specified for the cell, it will be used for the value, otherwise, 0 will be used. """ message_template = "Empty cell illegal for integer fields." class W50(VOTableSpecWarning): """ Invalid unit string as defined in the `Units in the VO, Version 1.0 <https://www.ivoa.net/documents/VOUnits>`_ (VOTable version >= 1.4) or `Standards for Astronomical Catalogues, Version 2.0 <http://cdsarc.u-strasbg.fr/doc/catstd-3.2.htx>`_ (version < 1.4). Consider passing an explicit ``unit_format`` parameter if the units in this file conform to another specification. """ message_template = "Invalid unit string '{}'" default_args = ("x",) class W51(VOTableSpecWarning): """ The integer value is out of range for the size of the field. """ message_template = "Value '{}' is out of range for a {} integer field" default_args = ("x", "n-bit") class W52(VOTableSpecWarning): """ The BINARY2 format was introduced in VOTable 1.3. It should not be present in files marked as an earlier version. """ message_template = ( "The BINARY2 format was introduced in VOTable 1.3, but " "this file is declared as version '{}'" ) default_args = ("1.2",) class W53(VOTableSpecWarning): """ The VOTABLE element must contain at least one RESOURCE element. """ message_template = "VOTABLE element must contain at least one RESOURCE element." default_args = () class W54(VOTableSpecWarning): """ The TIMESYS element was introduced in VOTable 1.4. It should not be present in files marked as an earlier version. """ message_template = ( "The TIMESYS element was introduced in VOTable 1.4, but " "this file is declared as version '{}'" ) default_args = ("1.3",) class W55(VOTableSpecWarning): """ When non-ASCII characters are detected when reading a TABLEDATA value for a FIELD with ``datatype="char"``, we can issue this warning. """ message_template = ( 'FIELD ({}) has datatype="char" but contains non-ASCII value ({})' ) default_args = ("", "") class E01(VOWarning, ValueError): """ The size specifier for a ``char`` or ``unicode`` field must be only a number followed, optionally, by an asterisk. Multi-dimensional size specifiers are not supported for these datatypes. Strings, which are defined as a set of characters, can be represented in VOTable as a fixed- or variable-length array of characters:: <FIELD name="unboundedString" datatype="char" arraysize="*"/> A 1D array of strings can be represented as a 2D array of characters, but given the logic above, it is possible to define a variable-length array of fixed-length strings, but not a fixed-length array of variable-length strings. """ message_template = "Invalid size specifier '{}' for a {} field (in field '{}')" default_args = ("x", "char/unicode", "y") class E02(VOWarning, ValueError): """ The number of array elements in the data does not match that specified in the FIELD specifier. """ message_template = ( "Incorrect number of elements in array. Expected multiple of {}, got {}" ) default_args = ("x", "y") class E03(VOWarning, ValueError): """ Complex numbers should be two values separated by whitespace. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:datatypes>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:datatypes>`__ """ message_template = "'{}' does not parse as a complex number" default_args = ("x",) class E04(VOWarning, ValueError): """ A ``bit`` array should be a string of '0's and '1's. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:datatypes>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:datatypes>`__ """ message_template = "Invalid bit value '{}'" default_args = ("x",) class E05(VOWarning, ValueError): r""" A ``boolean`` value should be one of the following strings (case insensitive) in the ``TABLEDATA`` format:: 'TRUE', 'FALSE', '1', '0', 'T', 'F', '\0', ' ', '?' and in ``BINARY`` format:: 'T', 'F', '1', '0', '\0', ' ', '?' **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:datatypes>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:datatypes>`__ """ message_template = "Invalid boolean value '{}'" default_args = ("x",) class E06(VOWarning, ValueError): """ The supported datatypes are:: double, float, bit, boolean, unsignedByte, short, int, long, floatComplex, doubleComplex, char, unicodeChar The following non-standard aliases are also supported, but in these case :ref:`W13 <W13>` will be raised:: string -> char unicodeString -> unicodeChar int16 -> short int32 -> int int64 -> long float32 -> float float64 -> double unsignedInt -> long unsignedShort -> int To add more datatype mappings during parsing, use the ``datatype_mapping`` keyword to `astropy.io.votable.parse`. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:datatypes>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:datatypes>`__ """ message_template = "Unknown datatype '{}' on field '{}'" default_args = ("x", "y") # E07: Deprecated class E08(VOWarning, ValueError): """ The ``type`` attribute on the ``VALUES`` element must be either ``legal`` or ``actual``. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:values>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:values>`__ """ message_template = "type must be 'legal' or 'actual', but is '{}'" default_args = ("x",) class E09(VOWarning, ValueError): """ The ``MIN``, ``MAX`` and ``OPTION`` elements must always have a ``value`` attribute. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:values>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:values>`__ """ message_template = "'{}' must have a value attribute" default_args = ("x",) class E10(VOWarning, ValueError): """ From VOTable 1.1 and later, ``FIELD`` and ``PARAM`` elements must have a ``datatype`` field. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#elem:FIELD>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#elem:FIELD>`__ """ message_template = "'datatype' attribute required on all '{}' elements" default_args = ("FIELD",) class E11(VOWarning, ValueError): """ The precision attribute is meant to express the number of significant digits, either as a number of decimal places (e.g. ``precision="F2"`` or equivalently ``precision="2"`` to express 2 significant figures after the decimal point), or as a number of significant figures (e.g. ``precision="E5"`` indicates a relative precision of 10-5). It is validated using the following regular expression:: [EF]?[1-9][0-9]* **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:form>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:form>`__ """ message_template = "precision '{}' is invalid" default_args = ("x",) class E12(VOWarning, ValueError): """ The width attribute is meant to indicate to the application the number of characters to be used for input or output of the quantity. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:form>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:form>`__ """ message_template = "width must be a positive integer, got '{}'" default_args = ("x",) class E13(VOWarning, ValueError): r""" From the VOTable 1.2 spec: A table cell can contain an array of a given primitive type, with a fixed or variable number of elements; the array may even be multidimensional. For instance, the position of a point in a 3D space can be defined by the following:: <FIELD ID="point_3D" datatype="double" arraysize="3"/> and each cell corresponding to that definition must contain exactly 3 numbers. An asterisk (\*) may be appended to indicate a variable number of elements in the array, as in:: <FIELD ID="values" datatype="int" arraysize="100*"/> where it is specified that each cell corresponding to that definition contains 0 to 100 integer numbers. The number may be omitted to specify an unbounded array (in practice up to =~2×10⁹ elements). A table cell can also contain a multidimensional array of a given primitive type. This is specified by a sequence of dimensions separated by the ``x`` character, with the first dimension changing fastest; as in the case of a simple array, the last dimension may be variable in length. As an example, the following definition declares a table cell which may contain a set of up to 10 images, each of 64×64 bytes:: <FIELD ID="thumbs" datatype="unsignedByte" arraysize="64×64×10*"/> **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#sec:dim>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#sec:dim>`__ """ message_template = "Invalid arraysize attribute '{}'" default_args = ("x",) class E14(VOWarning, ValueError): """ All ``PARAM`` elements must have a ``value`` attribute. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#elem:FIELD>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#elem:FIELD>`__ """ message_template = "value attribute is required for all PARAM elements" class E15(VOWarning, ValueError): """ All ``COOSYS`` elements must have an ``ID`` attribute. Note that the VOTable 1.1 specification says this attribute is optional, but its corresponding schema indicates it is required. In VOTable 1.2, the ``COOSYS`` element is deprecated. """ message_template = "ID attribute is required for all COOSYS elements" class E16(VOTableSpecWarning): """ The ``system`` attribute on the ``COOSYS`` element must be one of the following:: 'eq_FK4', 'eq_FK5', 'ICRS', 'ecl_FK4', 'ecl_FK5', 'galactic', 'supergalactic', 'xy', 'barycentric', 'geo_app' **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#elem:COOSYS>`__ """ message_template = "Invalid system attribute '{}'" default_args = ("x",) class E17(VOWarning, ValueError): """ ``extnum`` attribute must be a positive integer. **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#ToC54>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#ToC58>`__ """ message_template = "extnum must be a positive integer" class E18(VOWarning, ValueError): """ The ``type`` attribute of the ``RESOURCE`` element must be one of "results" or "meta". **References**: `1.1 <http://www.ivoa.net/documents/VOTable/20040811/REC-VOTable-1.1-20040811.html#ToC54>`__, `1.2 <http://www.ivoa.net/documents/VOTable/20091130/REC-VOTable-1.2.html#ToC58>`__ """ message_template = "type must be 'results' or 'meta', not '{}'" default_args = ("x",) class E19(VOWarning, ValueError): """ Raised either when the file doesn't appear to be XML, or the root element is not VOTABLE. """ message_template = "File does not appear to be a VOTABLE" class E20(VOTableSpecError): """ The table had only *x* fields defined, but the data itself has more columns than that. """ message_template = "Data has more columns than are defined in the header ({})" default_args = ("x",) class E21(VOWarning, ValueError): """ The table had *x* fields defined, but the data itself has only *y* columns. """ message_template = "Data has fewer columns ({}) than are defined in the header ({})" default_args = ("x", "y") class E22(VOWarning, ValueError): """ All ``TIMESYS`` elements must have an ``ID`` attribute. """ message_template = "ID attribute is required for all TIMESYS elements" class E23(VOTableSpecWarning): """ The ``timeorigin`` attribute on the ``TIMESYS`` element must be either a floating point literal specifying a valid Julian Date, or, for convenience, the string "MJD-origin" (standing for 2400000.5) or the string "JD-origin" (standing for 0). **References**: `1.4 <http://www.ivoa.net/documents/VOTable/20191021/REC-VOTable-1.4-20191021.html#ToC21>`__ """ message_template = "Invalid timeorigin attribute '{}'" default_args = ("x",) class E24(VOWarning, ValueError): """ Non-ASCII unicode values should not be written when the FIELD ``datatype="char"``, and cannot be written in BINARY or BINARY2 serialization. """ message_template = ( 'Attempt to write non-ASCII value ({}) to FIELD ({}) which has datatype="char"' ) default_args = ("", "") class E25(VOTableSpecWarning): """ A VOTable cannot have a DATA section without any defined FIELD; DATA will be ignored. """ message_template = "No FIELDs are defined; DATA section will be ignored." def _get_warning_and_exception_classes(prefix): classes = [] for key, val in globals().items(): if re.match(prefix + "[0-9]{2}", key): classes.append((key, val)) classes.sort() return classes def _build_doc_string(): def generate_set(prefix): classes = _get_warning_and_exception_classes(prefix) out = io.StringIO() for name, cls in classes: out.write(f".. _{name}:\n\n") msg = f"{cls.__name__}: {cls.get_short_name()}" if not isinstance(msg, str): msg = msg.decode("utf-8") out.write(msg) out.write("\n") out.write("~" * len(msg)) out.write("\n\n") doc = cls.__doc__ if not isinstance(doc, str): doc = doc.decode("utf-8") out.write(dedent(doc)) out.write("\n\n") return out.getvalue() warnings = generate_set("W") exceptions = generate_set("E") return {"warnings": warnings, "exceptions": exceptions} if __doc__ is not None: __doc__ = __doc__.format(**_build_doc_string()) __all__.extend([x[0] for x in _get_warning_and_exception_classes("W")]) __all__.extend([x[0] for x in _get_warning_and_exception_classes("E")])
176a8670da149f0c983e6dbe3ca23dd14b6406086d7dd02992b4d4a71d7efc07
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ This file contains routines to verify the correctness of UCD strings. """ # STDLIB import re # LOCAL from astropy.utils import data __all__ = ["parse_ucd", "check_ucd"] class UCDWords: """ Manages a list of acceptable UCD words. Works by reading in a data file exactly as provided by IVOA. This file resides in data/ucd1p-words.txt. """ def __init__(self): self._primary = set() self._secondary = set() self._descriptions = {} self._capitalization = {} with data.get_pkg_data_fileobj("data/ucd1p-words.txt", encoding="ascii") as fd: for line in fd.readlines(): type, name, descr = (x.strip() for x in line.split("|")) name_lower = name.lower() if type in "QPEVC": self._primary.add(name_lower) if type in "QSEVC": self._secondary.add(name_lower) self._descriptions[name_lower] = descr self._capitalization[name_lower] = name def is_primary(self, name): """ Returns True if *name* is a valid primary name. """ return name.lower() in self._primary def is_secondary(self, name): """ Returns True if *name* is a valid secondary name. """ return name.lower() in self._secondary def get_description(self, name): """ Returns the official English description of the given UCD *name*. """ return self._descriptions[name.lower()] def normalize_capitalization(self, name): """ Returns the standard capitalization form of the given name. """ return self._capitalization[name.lower()] _ucd_singleton = None def parse_ucd(ucd, check_controlled_vocabulary=False, has_colon=False): """ Parse the UCD into its component parts. Parameters ---------- ucd : str The UCD string check_controlled_vocabulary : bool, optional If `True`, then each word in the UCD will be verified against the UCD1+ controlled vocabulary, (as required by the VOTable specification version 1.2), otherwise not. has_colon : bool, optional If `True`, the UCD may contain a colon (as defined in earlier versions of the standard). Returns ------- parts : list The result is a list of tuples of the form: (*namespace*, *word*) If no namespace was explicitly specified, *namespace* will be returned as ``'ivoa'`` (i.e., the default namespace). Raises ------ ValueError if *ucd* is invalid """ global _ucd_singleton if _ucd_singleton is None: _ucd_singleton = UCDWords() if has_colon: m = re.search(r"[^A-Za-z0-9_.:;\-]", ucd) else: m = re.search(r"[^A-Za-z0-9_.;\-]", ucd) if m is not None: raise ValueError(f"UCD has invalid character '{m.group(0)}' in '{ucd}'") word_component_re = r"[A-Za-z0-9][A-Za-z0-9\-_]*" word_re = rf"{word_component_re}(\.{word_component_re})*" parts = ucd.split(";") words = [] for i, word in enumerate(parts): colon_count = word.count(":") if colon_count == 1: ns, word = word.split(":", 1) if not re.match(word_component_re, ns): raise ValueError(f"Invalid namespace '{ns}'") ns = ns.lower() elif colon_count > 1: raise ValueError(f"Too many colons in '{word}'") else: ns = "ivoa" if not re.match(word_re, word): raise ValueError(f"Invalid word '{word}'") if ns == "ivoa" and check_controlled_vocabulary: if i == 0: if not _ucd_singleton.is_primary(word): if _ucd_singleton.is_secondary(word): raise ValueError( f"Secondary word '{word}' is not valid as a primary word" ) else: raise ValueError(f"Unknown word '{word}'") else: if not _ucd_singleton.is_secondary(word): if _ucd_singleton.is_primary(word): raise ValueError( f"Primary word '{word}' is not valid as a secondary word" ) else: raise ValueError(f"Unknown word '{word}'") try: normalized_word = _ucd_singleton.normalize_capitalization(word) except KeyError: normalized_word = word words.append((ns, normalized_word)) return words def check_ucd(ucd, check_controlled_vocabulary=False, has_colon=False): """ Returns False if *ucd* is not a valid `unified content descriptor`_. Parameters ---------- ucd : str The UCD string check_controlled_vocabulary : bool, optional If `True`, then each word in the UCD will be verified against the UCD1+ controlled vocabulary, (as required by the VOTable specification version 1.2), otherwise not. has_colon : bool, optional If `True`, the UCD may contain a colon (as defined in earlier versions of the standard). Returns ------- valid : bool """ if ucd is None: return True try: parse_ucd( ucd, check_controlled_vocabulary=check_controlled_vocabulary, has_colon=has_colon, ) except ValueError: return False return True
0c5f7ae231506a4dc9e161b36b115471724659958e13308b42d7bb86dea6bc8f
# Licensed under a 3-clause BSD style license - see LICENSE.rst from os.path import join from setuptools import Extension def get_extensions(build_type="release"): VO_DIR = "astropy/io/votable/src" return [ Extension( "astropy.io.votable.tablewriter", [join(VO_DIR, "tablewriter.c")], include_dirs=[VO_DIR], ) ]
159d67c0728a1d409a77b3a81d85d111e6270be8b088b040b7c6a255adc5eee8
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ Various utilities and cookbook-like things. """ # STDLIB import codecs import contextlib import gzip import io import os import re from packaging.version import Version __all__ = [ "convert_to_writable_filelike", "stc_reference_frames", "coerce_range_list_param", ] @contextlib.contextmanager def convert_to_writable_filelike(fd, compressed=False): """ Returns a writable file-like object suitable for streaming output. Parameters ---------- fd : str or file-like May be: - a file path string, in which case it is opened, and the file object is returned. - an object with a :meth:``write`` method, in which case that object is returned. compressed : bool, optional If `True`, create a gzip-compressed file. (Default is `False`). Returns ------- fd : writable file-like """ if isinstance(fd, str): fd = os.path.expanduser(fd) if fd.endswith(".gz") or compressed: with gzip.GzipFile(fd, "wb") as real_fd: encoded_fd = io.TextIOWrapper(real_fd, encoding="utf8") yield encoded_fd encoded_fd.flush() real_fd.flush() return else: with open(fd, "w", encoding="utf8") as real_fd: yield real_fd return elif hasattr(fd, "write"): assert callable(fd.write) if compressed: fd = gzip.GzipFile(fileobj=fd) # If we can't write Unicode strings, use a codecs.StreamWriter # object needs_wrapper = False try: fd.write("") except TypeError: needs_wrapper = True if not hasattr(fd, "encoding") or fd.encoding is None: needs_wrapper = True if needs_wrapper: yield codecs.getwriter("utf-8")(fd) fd.flush() else: yield fd fd.flush() return else: raise TypeError("Can not be coerced to writable file-like object") # <http://www.ivoa.net/documents/REC/DM/STC-20071030.html> stc_reference_frames = { "FK4", "FK5", "ECLIPTIC", "ICRS", "GALACTIC", "GALACTIC_I", "GALACTIC_II", "SUPER_GALACTIC", "AZ_EL", "BODY", "GEO_C", "GEO_D", "MAG", "GSE", "GSM", "SM", "HGC", "HGS", "HEEQ", "HRTN", "HPC", "HPR", "HCC", "HGI", "MERCURY_C", "VENUS_C", "LUNA_C", "MARS_C", "JUPITER_C_III", "SATURN_C_III", "URANUS_C_III", "NEPTUNE_C_III", "PLUTO_C", "MERCURY_G", "VENUS_G", "LUNA_G", "MARS_G", "JUPITER_G_III", "SATURN_G_III", "URANUS_G_III", "NEPTUNE_G_III", "PLUTO_G", "UNKNOWNFrame", } def coerce_range_list_param(p, frames=None, numeric=True): """ Coerces and/or verifies the object *p* into a valid range-list-format parameter. As defined in `Section 8.7.2 of Simple Spectral Access Protocol <http://www.ivoa.net/documents/REC/DAL/SSA-20080201.html>`_. Parameters ---------- p : str or sequence May be a string as passed verbatim to the service expecting a range-list, or a sequence. If a sequence, each item must be either: - a numeric value - a named value, such as, for example, 'J' for named spectrum (if the *numeric* kwarg is False) - a 2-tuple indicating a range - the last item my be a string indicating the frame of reference frames : sequence of str, optional A sequence of acceptable frame of reference keywords. If not provided, the default set in ``set_reference_frames`` will be used. numeric : bool, optional TODO Returns ------- parts : tuple The result is a tuple: - a string suitable for passing to a service as a range-list argument - an integer counting the number of elements """ def str_or_none(x): if x is None: return "" if numeric: x = float(x) return str(x) def numeric_or_range(x): if isinstance(x, tuple) and len(x) == 2: return f"{str_or_none(x[0])}/{str_or_none(x[1])}" else: return str_or_none(x) def is_frame_of_reference(x): return isinstance(x, str) if p is None: return None, 0 elif isinstance(p, (tuple, list)): has_frame_of_reference = len(p) > 1 and is_frame_of_reference(p[-1]) if has_frame_of_reference: points = p[:-1] else: points = p[:] out = ",".join([numeric_or_range(x) for x in points]) length = len(points) if has_frame_of_reference: if frames is not None and p[-1] not in frames: raise ValueError(f"'{p[-1]}' is not a valid frame of reference") out += ";" + p[-1] length += 1 return out, length elif isinstance(p, str): number = r"([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)?" if not numeric: number = r"(" + number + ")|([A-Z_]+)" match = re.match( "^" + number + r"([,/]" + number + r")+(;(?P<frame>[<A-Za-z_0-9]+))?$", p ) if match is None: raise ValueError(f"'{p}' is not a valid range list") frame = match.groupdict()["frame"] if frames is not None and frame is not None and frame not in frames: raise ValueError(f"{frame!r} is not a valid frame of reference") return p, p.count(",") + p.count(";") + 1 try: float(p) return str(p), 1 except TypeError: raise ValueError(f"'{p}' is not a valid range list") def version_compare(a, b): """ Compare two VOTable version identifiers. """ def version_to_tuple(v): if v[0].lower() == "v": v = v[1:] return Version(v) av = version_to_tuple(a) bv = version_to_tuple(b) # Can't use cmp because it was removed from Python 3.x return (av > bv) - (av < bv)
2ed3370e5af263efead8d55844d2c60a6832601fe7e71a9f3c3c53107add4f15
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ Various XML-related utilities """ # ASTROPY from astropy.logger import log from astropy.utils import data from astropy.utils.xml import check as xml_check from astropy.utils.xml import validate # LOCAL from .exceptions import W02, W03, W04, W05, vo_warn, warn_or_raise __all__ = [ "check_id", "fix_id", "check_token", "check_mime_content_type", "check_anyuri", "validate_schema", ] def check_id(ID, name="ID", config=None, pos=None): """ Raises a `~astropy.io.votable.exceptions.VOTableSpecError` if *ID* is not a valid XML ID_. *name* is the name of the attribute being checked (used only for error messages). """ if ID is not None and not xml_check.check_id(ID): warn_or_raise(W02, W02, (name, ID), config, pos) return False return True def fix_id(ID, config=None, pos=None): """ Given an arbitrary string, create one that can be used as an xml id. This is rather simplistic at the moment, since it just replaces non-valid characters with underscores. """ if ID is None: return None corrected = xml_check.fix_id(ID) if corrected != ID: vo_warn(W03, (ID, corrected), config, pos) return corrected _token_regex = r"(?![\r\l\t ])[^\r\l\t]*(?![\r\l\t ])" def check_token(token, attr_name, config=None, pos=None): """ Raises a `ValueError` if *token* is not a valid XML token. As defined by XML Schema Part 2. """ if token is not None and not xml_check.check_token(token): return False return True def check_mime_content_type(content_type, config=None, pos=None): """ Raises a `~astropy.io.votable.exceptions.VOTableSpecError` if *content_type* is not a valid MIME content type. As defined by RFC 2045 (syntactically, at least). """ if content_type is not None and not xml_check.check_mime_content_type(content_type): warn_or_raise(W04, W04, content_type, config, pos) return False return True def check_anyuri(uri, config=None, pos=None): """ Raises a `~astropy.io.votable.exceptions.VOTableSpecError` if *uri* is not a valid URI. As defined in RFC 2396. """ if uri is not None and not xml_check.check_anyuri(uri): warn_or_raise(W05, W05, uri, config, pos) return False return True def validate_schema(filename, version="1.1"): """ Validates the given file against the appropriate VOTable schema. Parameters ---------- filename : str The path to the XML file to validate version : str, optional The VOTABLE version to check, which must be a string \"1.0\", \"1.1\", \"1.2\" or \"1.3\". If it is not one of these, version \"1.1\" is assumed. For version \"1.0\", it is checked against a DTD, since that version did not have an XML Schema. Returns ------- returncode, stdout, stderr : int, str, str Returns the returncode from xmllint and the stdout and stderr as strings """ if version not in ("1.0", "1.1", "1.2", "1.3"): log.info(f"{filename} has version {version}, using schema 1.1") version = "1.1" if version in ("1.1", "1.2", "1.3"): schema_path = data.get_pkg_data_filename(f"data/VOTable.v{version}.xsd") else: schema_path = data.get_pkg_data_filename("data/VOTable.dtd") return validate.validate_schema(filename, schema_path)
8cea550af167b1f1f1eeabe49ae38fc02f4c55a6ed5210d3af2ea6eaaf42dec1
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ Script support for validating a VO file. """ def main(args=None): import argparse from . import table parser = argparse.ArgumentParser( description="Check a VOTable file for compliance to the VOTable specification" ) parser.add_argument("filename", nargs=1, help="Path to VOTable file to check") args = parser.parse_args(args) table.validate(args.filename[0])
95060ace711dbb68c6d4494f52eb1b26221b7e6e8d8ec888cdd9e6b09f5b584a
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ ``fitsheader`` is a command line script based on astropy.io.fits for printing the header(s) of one or more FITS file(s) to the standard output in a human- readable format. Example uses of fitsheader: 1. Print the header of all the HDUs of a .fits file:: $ fitsheader filename.fits 2. Print the header of the third and fifth HDU extension:: $ fitsheader --extension 3 --extension 5 filename.fits 3. Print the header of a named extension, e.g. select the HDU containing keywords EXTNAME='SCI' and EXTVER='2':: $ fitsheader --extension "SCI,2" filename.fits 4. Print only specific keywords:: $ fitsheader --keyword BITPIX --keyword NAXIS filename.fits 5. Print keywords NAXIS, NAXIS1, NAXIS2, etc using a wildcard:: $ fitsheader --keyword NAXIS* filename.fits 6. Dump the header keywords of all the files in the current directory into a machine-readable csv file:: $ fitsheader --table ascii.csv *.fits > keywords.csv 7. Specify hierarchical keywords with the dotted or spaced notation:: $ fitsheader --keyword ESO.INS.ID filename.fits $ fitsheader --keyword "ESO INS ID" filename.fits 8. Compare the headers of different fits files, following ESO's ``fitsort`` format:: $ fitsheader --fitsort --extension 0 --keyword ESO.INS.ID *.fits 9. Same as above, sorting the output along a specified keyword:: $ fitsheader -f -s DATE-OBS -e 0 -k DATE-OBS -k ESO.INS.ID *.fits 10. Sort first by OBJECT, then DATE-OBS:: $ fitsheader -f -s OBJECT -s DATE-OBS *.fits Note that compressed images (HDUs of type :class:`~astropy.io.fits.CompImageHDU`) really have two headers: a real BINTABLE header to describe the compressed data, and a fake IMAGE header representing the image that was compressed. Astropy returns the latter by default. You must supply the ``--compressed`` option if you require the real header that describes the compression. With Astropy installed, please run ``fitsheader --help`` to see the full usage documentation. """ import argparse import sys import numpy as np from astropy import __version__, log from astropy.io import fits DESCRIPTION = """ Print the header(s) of a FITS file. Optional arguments allow the desired extension(s), keyword(s), and output format to be specified. Note that in the case of a compressed image, the decompressed header is shown by default. This script is part of the Astropy package. See https://docs.astropy.org/en/latest/io/fits/usage/scripts.html#module-astropy.io.fits.scripts.fitsheader for further documentation. """.strip() class ExtensionNotFoundException(Exception): """Raised if an HDU extension requested by the user does not exist.""" pass class HeaderFormatter: """Class to format the header(s) of a FITS file for display by the `fitsheader` tool; essentially a wrapper around a `HDUList` object. Example usage: fmt = HeaderFormatter('/path/to/file.fits') print(fmt.parse(extensions=[0, 3], keywords=['NAXIS', 'BITPIX'])) Parameters ---------- filename : str Path to a single FITS file. verbose : bool Verbose flag, to show more information about missing extensions, keywords, etc. Raises ------ OSError If `filename` does not exist or cannot be read. """ def __init__(self, filename, verbose=True): self.filename = filename self.verbose = verbose self._hdulist = fits.open(filename) def parse(self, extensions=None, keywords=None, compressed=False): """Returns the FITS file header(s) in a readable format. Parameters ---------- extensions : list of int or str, optional Format only specific HDU(s), identified by number or name. The name can be composed of the "EXTNAME" or "EXTNAME,EXTVER" keywords. keywords : list of str, optional Keywords for which the value(s) should be returned. If not specified, then the entire header is returned. compressed : bool, optional If True, shows the header describing the compression, rather than the header obtained after decompression. (Affects FITS files containing `CompImageHDU` extensions only.) Returns ------- formatted_header : str or astropy.table.Table Traditional 80-char wide format in the case of `HeaderFormatter`; an Astropy Table object in the case of `TableHeaderFormatter`. """ # `hdukeys` will hold the keys of the HDUList items to display if extensions is None: hdukeys = range(len(self._hdulist)) # Display all by default else: hdukeys = [] for ext in extensions: try: # HDU may be specified by number hdukeys.append(int(ext)) except ValueError: # The user can specify "EXTNAME" or "EXTNAME,EXTVER" parts = ext.split(",") if len(parts) > 1: extname = ",".join(parts[0:-1]) extver = int(parts[-1]) hdukeys.append((extname, extver)) else: hdukeys.append(ext) # Having established which HDUs the user wants, we now format these: return self._parse_internal(hdukeys, keywords, compressed) def _parse_internal(self, hdukeys, keywords, compressed): """The meat of the formatting; in a separate method to allow overriding.""" result = [] for idx, hdu in enumerate(hdukeys): try: cards = self._get_cards(hdu, keywords, compressed) except ExtensionNotFoundException: continue if idx > 0: # Separate HDUs by a blank line result.append("\n") result.append(f"# HDU {hdu} in {self.filename}:\n") for c in cards: result.append(f"{c}\n") return "".join(result) def _get_cards(self, hdukey, keywords, compressed): """Returns a list of `astropy.io.fits.card.Card` objects. This function will return the desired header cards, taking into account the user's preference to see the compressed or uncompressed version. Parameters ---------- hdukey : int or str Key of a single HDU in the HDUList. keywords : list of str, optional Keywords for which the cards should be returned. compressed : bool, optional If True, shows the header describing the compression. Raises ------ ExtensionNotFoundException If the hdukey does not correspond to an extension. """ # First we obtain the desired header try: if compressed: # In the case of a compressed image, return the header before # decompression (not the default behavior) header = self._hdulist[hdukey]._header else: header = self._hdulist[hdukey].header except (IndexError, KeyError): message = f"{self.filename}: Extension {hdukey} not found." if self.verbose: log.warning(message) raise ExtensionNotFoundException(message) if not keywords: # return all cards cards = header.cards else: # specific keywords are requested cards = [] for kw in keywords: try: crd = header.cards[kw] if isinstance(crd, fits.card.Card): # Single card cards.append(crd) else: # Allow for wildcard access cards.extend(crd) except KeyError: # Keyword does not exist if self.verbose: log.warning( "{filename} (HDU {hdukey}): Keyword {kw} not found.".format( filename=self.filename, hdukey=hdukey, kw=kw ) ) return cards def close(self): self._hdulist.close() class TableHeaderFormatter(HeaderFormatter): """Class to convert the header(s) of a FITS file into a Table object. The table returned by the `parse` method will contain four columns: filename, hdu, keyword, and value. Subclassed from HeaderFormatter, which contains the meat of the formatting. """ def _parse_internal(self, hdukeys, keywords, compressed): """Method called by the parse method in the parent class.""" tablerows = [] for hdu in hdukeys: try: for card in self._get_cards(hdu, keywords, compressed): tablerows.append( { "filename": self.filename, "hdu": hdu, "keyword": card.keyword, "value": str(card.value), } ) except ExtensionNotFoundException: pass if tablerows: from astropy import table return table.Table(tablerows) return None def print_headers_traditional(args): """Prints FITS header(s) using the traditional 80-char format. Parameters ---------- args : argparse.Namespace Arguments passed from the command-line as defined below. """ for idx, filename in enumerate(args.filename): # support wildcards if idx > 0 and not args.keyword: print() # print a newline between different files formatter = None try: formatter = HeaderFormatter(filename) print( formatter.parse(args.extensions, args.keyword, args.compressed), end="" ) except OSError as e: log.error(str(e)) finally: if formatter: formatter.close() def print_headers_as_table(args): """Prints FITS header(s) in a machine-readable table format. Parameters ---------- args : argparse.Namespace Arguments passed from the command-line as defined below. """ tables = [] # Create a Table object for each file for filename in args.filename: # Support wildcards formatter = None try: formatter = TableHeaderFormatter(filename) tbl = formatter.parse(args.extensions, args.keyword, args.compressed) if tbl: tables.append(tbl) except OSError as e: log.error(str(e)) # file not found or unreadable finally: if formatter: formatter.close() # Concatenate the tables if len(tables) == 0: return False elif len(tables) == 1: resulting_table = tables[0] else: from astropy import table resulting_table = table.vstack(tables) # Print the string representation of the concatenated table resulting_table.write(sys.stdout, format=args.table) def print_headers_as_comparison(args): """Prints FITS header(s) with keywords as columns. This follows the dfits+fitsort format. Parameters ---------- args : argparse.Namespace Arguments passed from the command-line as defined below. """ from astropy import table tables = [] # Create a Table object for each file for filename in args.filename: # Support wildcards formatter = None try: formatter = TableHeaderFormatter(filename, verbose=False) tbl = formatter.parse(args.extensions, args.keyword, args.compressed) if tbl: # Remove empty keywords tbl = tbl[np.where(tbl["keyword"] != "")] else: tbl = table.Table([[filename]], names=("filename",)) tables.append(tbl) except OSError as e: log.error(str(e)) # file not found or unreadable finally: if formatter: formatter.close() # Concatenate the tables if len(tables) == 0: return False elif len(tables) == 1: resulting_table = tables[0] else: resulting_table = table.vstack(tables) # If we obtained more than one hdu, merge hdu and keywords columns hdus = resulting_table["hdu"] if np.ma.isMaskedArray(hdus): hdus = hdus.compressed() if len(np.unique(hdus)) > 1: for tab in tables: new_column = table.Column([f"{row['hdu']}:{row['keyword']}" for row in tab]) tab.add_column(new_column, name="hdu+keyword") keyword_column_name = "hdu+keyword" else: keyword_column_name = "keyword" # Check how many hdus we are processing final_tables = [] for tab in tables: final_table = [table.Column([tab["filename"][0]], name="filename")] if "value" in tab.colnames: for row in tab: if row["keyword"] in ("COMMENT", "HISTORY"): continue final_table.append( table.Column([row["value"]], name=row[keyword_column_name]) ) final_tables.append(table.Table(final_table)) final_table = table.vstack(final_tables) # Sort if requested if args.sort: final_table.sort(args.sort) # Reorganise to keyword by columns final_table.pprint(max_lines=-1, max_width=-1) def main(args=None): """This is the main function called by the `fitsheader` script.""" parser = argparse.ArgumentParser( description=DESCRIPTION, formatter_class=argparse.RawDescriptionHelpFormatter ) parser.add_argument( "--version", action="version", version=f"%(prog)s {__version__}" ) parser.add_argument( "-e", "--extension", metavar="HDU", action="append", dest="extensions", help=( "specify the extension by name or number; " "this argument can be repeated " "to select multiple extensions" ), ) parser.add_argument( "-k", "--keyword", metavar="KEYWORD", action="append", type=str, help=( "specify a keyword; this argument can be " "repeated to select multiple keywords; " "also supports wildcards" ), ) mode_group = parser.add_mutually_exclusive_group() mode_group.add_argument( "-t", "--table", nargs="?", default=False, metavar="FORMAT", help=( "print the header(s) in machine-readable table " "format; the default format is " '"ascii.fixed_width" (can be "ascii.csv", ' '"ascii.html", "ascii.latex", "fits", etc)' ), ) mode_group.add_argument( "-f", "--fitsort", action="store_true", help=( "print the headers as a table with each unique " "keyword in a given column (fitsort format) " ), ) parser.add_argument( "-s", "--sort", metavar="SORT_KEYWORD", action="append", type=str, help=( "sort output by the specified header keywords, " "can be repeated to sort by multiple keywords; " "Only supported with -f/--fitsort" ), ) parser.add_argument( "-c", "--compressed", action="store_true", help=( "for compressed image data, " "show the true header which describes " "the compression rather than the data" ), ) parser.add_argument( "filename", nargs="+", help="path to one or more files; wildcards are supported", ) args = parser.parse_args(args) # If `--table` was used but no format specified, # then use ascii.fixed_width by default if args.table is None: args.table = "ascii.fixed_width" if args.sort: args.sort = [key.replace(".", " ") for key in args.sort] if not args.fitsort: log.error( "Sorting with -s/--sort is only supported in conjunction with" " -f/--fitsort" ) # 2: Unix error convention for command line syntax sys.exit(2) if args.keyword: args.keyword = [key.replace(".", " ") for key in args.keyword] # Now print the desired headers try: if args.table: print_headers_as_table(args) elif args.fitsort: print_headers_as_comparison(args) else: print_headers_traditional(args) except OSError: # A 'Broken pipe' OSError may occur when stdout is closed prematurely, # eg. when calling `fitsheader file.fits | head`. We let this pass. pass
7102a8827569e6a5120bbee54fdac29c45724c35e81d53f2f113504dac504ab9
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ ``fitscheck`` is a command line script based on astropy.io.fits for verifying and updating the CHECKSUM and DATASUM keywords of .fits files. ``fitscheck`` can also detect and often fix other FITS standards violations. ``fitscheck`` facilitates re-writing the non-standard checksums originally generated by astropy.io.fits with standard checksums which will interoperate with CFITSIO. ``fitscheck`` will refuse to write new checksums if the checksum keywords are missing or their values are bad. Use ``--force`` to write new checksums regardless of whether or not they currently exist or pass. Use ``--ignore-missing`` to tolerate missing checksum keywords without comment. Example uses of fitscheck: 1. Add checksums:: $ fitscheck --write *.fits 2. Write new checksums, even if existing checksums are bad or missing:: $ fitscheck --write --force *.fits 3. Verify standard checksums and FITS compliance without changing the files:: $ fitscheck --compliance *.fits 4. Only check and fix compliance problems, ignoring checksums:: $ fitscheck --checksum none --compliance --write *.fits 5. Verify standard interoperable checksums:: $ fitscheck *.fits 6. Delete checksum keywords:: $ fitscheck --checksum remove --write *.fits """ import argparse import logging import sys import warnings from astropy import __version__ from astropy.io import fits log = logging.getLogger("fitscheck") DESCRIPTION = """ e.g. fitscheck example.fits Verifies and optionally re-writes the CHECKSUM and DATASUM keywords for a .fits file. Optionally detects and fixes FITS standard compliance problems. This script is part of the Astropy package. See https://docs.astropy.org/en/latest/io/fits/usage/scripts.html#module-astropy.io.fits.scripts.fitscheck for further documentation. """.strip() def handle_options(args): if not len(args): args = ["-h"] parser = argparse.ArgumentParser( description=DESCRIPTION, formatter_class=argparse.RawDescriptionHelpFormatter ) parser.add_argument( "--version", action="version", version=f"%(prog)s {__version__}" ) parser.add_argument( "fits_files", metavar="file", nargs="+", help=".fits files to process." ) parser.add_argument( "-k", "--checksum", dest="checksum_kind", choices=["standard", "remove", "none"], help="Choose FITS checksum mode or none. Defaults standard.", default="standard", ) parser.add_argument( "-w", "--write", dest="write_file", help="Write out file checksums and/or FITS compliance fixes.", default=False, action="store_true", ) parser.add_argument( "-f", "--force", dest="force", help="Do file update even if original checksum was bad.", default=False, action="store_true", ) parser.add_argument( "-c", "--compliance", dest="compliance", help="Do FITS compliance checking; fix if possible.", default=False, action="store_true", ) parser.add_argument( "-i", "--ignore-missing", dest="ignore_missing", help="Ignore missing checksums.", default=False, action="store_true", ) parser.add_argument( "-v", "--verbose", dest="verbose", help="Generate extra output.", default=False, action="store_true", ) global OPTIONS OPTIONS = parser.parse_args(args) if OPTIONS.checksum_kind == "none": OPTIONS.checksum_kind = False elif OPTIONS.checksum_kind == "standard": OPTIONS.checksum_kind = True elif OPTIONS.checksum_kind == "remove": OPTIONS.write_file = True OPTIONS.force = True return OPTIONS.fits_files def setup_logging(): log.handlers.clear() if OPTIONS.verbose: log.setLevel(logging.INFO) else: log.setLevel(logging.WARNING) handler = logging.StreamHandler() handler.setFormatter(logging.Formatter("%(message)s")) log.addHandler(handler) def verify_checksums(filename): """ Prints a message if any HDU in `filename` has a bad checksum or datasum. """ with warnings.catch_warnings(record=True) as wlist: warnings.simplefilter("always") with fits.open(filename, checksum=OPTIONS.checksum_kind) as hdulist: for i, hdu in enumerate(hdulist): # looping on HDUs is needed to read them and verify the # checksums if not OPTIONS.ignore_missing: if not hdu._checksum: log.warning( "MISSING {!r} .. Checksum not found in HDU #{}".format( filename, i ) ) return 1 if not hdu._datasum: log.warning( "MISSING {!r} .. Datasum not found in HDU #{}".format( filename, i ) ) return 1 for w in wlist: if str(w.message).startswith( ("Checksum verification failed", "Datasum verification failed") ): log.warning("BAD %r %s", filename, str(w.message)) return 1 log.info(f"OK {filename!r}") return 0 def verify_compliance(filename): """Check for FITS standard compliance.""" with fits.open(filename) as hdulist: try: hdulist.verify("exception") except fits.VerifyError as exc: log.warning("NONCOMPLIANT %r .. %s", filename, str(exc).replace("\n", " ")) return 1 return 0 def update(filename): """ Sets the ``CHECKSUM`` and ``DATASUM`` keywords for each HDU of `filename`. Also updates fixes standards violations if possible and requested. """ output_verify = "silentfix" if OPTIONS.compliance else "ignore" # For unit tests we reset temporarily the warning filters. Indeed, before # updating the checksums, fits.open will verify the existing checksums and # raise warnings, which are later caught and converted to log.warning... # which is an issue when testing, using the "error" action to convert # warnings to exceptions. with warnings.catch_warnings(): warnings.resetwarnings() with fits.open( filename, do_not_scale_image_data=True, checksum=OPTIONS.checksum_kind, mode="update", ) as hdulist: hdulist.flush(output_verify=output_verify) def process_file(filename): """ Handle a single .fits file, returning the count of checksum and compliance errors. """ try: checksum_errors = verify_checksums(filename) if OPTIONS.compliance: compliance_errors = verify_compliance(filename) else: compliance_errors = 0 if OPTIONS.write_file and checksum_errors == 0 or OPTIONS.force: update(filename) return checksum_errors + compliance_errors except Exception as e: log.error(f"EXCEPTION {filename!r} .. {e}") return 1 def main(args=None): """ Processes command line parameters into options and files, then checks or update FITS DATASUM and CHECKSUM keywords for the specified files. """ errors = 0 fits_files = handle_options(args or sys.argv[1:]) setup_logging() for filename in fits_files: errors += process_file(filename) if errors: log.warning(f"{errors} errors") return int(bool(errors))
71ee64da8a605c44ef218fd416c5a1e626644217b6f71f08a1028a00366c5b16
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ ``fitsinfo`` is a command-line script based on astropy.io.fits for printing a summary of the HDUs in one or more FITS files(s) to the standard output. Example usage of ``fitsinfo``: 1. Print a summary of the HDUs in a FITS file:: $ fitsinfo filename.fits Filename: filename.fits No. Name Type Cards Dimensions Format 0 PRIMARY PrimaryHDU 138 () 1 SCI ImageHDU 61 (800, 800) int16 2 SCI ImageHDU 61 (800, 800) int16 3 SCI ImageHDU 61 (800, 800) int16 4 SCI ImageHDU 61 (800, 800) int16 2. Print a summary of HDUs of all the FITS files in the current directory:: $ fitsinfo *.fits """ import argparse import astropy.io.fits as fits from astropy import __version__, log DESCRIPTION = """ Print a summary of the HDUs in a FITS file(s). This script is part of the Astropy package. See https://docs.astropy.org/en/latest/io/fits/usage/scripts.html#module-astropy.io.fits.scripts.fitsinfo for further documentation. """.strip() def fitsinfo(filename): """ Print a summary of the HDUs in a FITS file. Parameters ---------- filename : str The path to a FITS file. """ try: fits.info(filename) except OSError as e: log.error(str(e)) return def main(args=None): """The main function called by the `fitsinfo` script.""" parser = argparse.ArgumentParser( description=DESCRIPTION, formatter_class=argparse.RawDescriptionHelpFormatter ) parser.add_argument( "--version", action="version", version=f"%(prog)s {__version__}" ) parser.add_argument( "filename", nargs="+", help="Path to one or more FITS files. Wildcards are supported.", ) args = parser.parse_args(args) for idx, filename in enumerate(args.filename): if idx > 0: print() fitsinfo(filename)
98d9e63c3c1436c79de77208c0a3c6da474211bab63b3faa2aa740ee76e5a775
# Licensed under a 3-clause BSD style license - see LICENSE.rst import argparse import glob import logging import os import sys from astropy import __version__ from astropy.io import fits from astropy.io.fits.util import fill log = logging.getLogger("fitsdiff") DESCRIPTION = """ Compare two FITS image files and report the differences in header keywords and data. fitsdiff [options] filename1 filename2 where filename1 filename2 are the two files to be compared. They may also be wild cards, in such cases, they must be enclosed by double or single quotes, or they may be directory names. If both are directory names, all files in each of the directories will be included; if only one is a directory name, then the directory name will be prefixed to the file name(s) specified by the other argument. for example:: fitsdiff "*.fits" "/machine/data1" will compare all FITS files in the current directory to the corresponding files in the directory /machine/data1. This script is part of the Astropy package. See https://docs.astropy.org/en/latest/io/fits/usage/scripts.html#fitsdiff for further documentation. """.strip() EPILOG = fill( """ If the two files are identical within the specified conditions, it will report "No difference is found." If the value(s) of -c and -k takes the form '@filename', list is in the text file 'filename', and each line in that text file contains one keyword. Example ------- fitsdiff -k filename,filtnam1 -n 5 -r 1.e-6 test1.fits test2 This command will compare files test1.fits and test2.fits, report maximum of 5 different pixels values per extension, only report data values larger than 1.e-6 relative to each other, and will neglect the different values of keywords FILENAME and FILTNAM1 (or their very existence). fitsdiff command-line arguments can also be set using the environment variable FITSDIFF_SETTINGS. If the FITSDIFF_SETTINGS environment variable is present, each argument present will override the corresponding argument on the command-line unless the --exact option is specified. The FITSDIFF_SETTINGS environment variable exists to make it easier to change the behavior of fitsdiff on a global level, such as in a set of regression tests. """.strip(), width=80, ) class StoreListAction(argparse.Action): def __init__(self, option_strings, dest, nargs=None, **kwargs): if nargs is not None: raise ValueError("nargs not allowed") super().__init__(option_strings, dest, nargs, **kwargs) def __call__(self, parser, namespace, values, option_string=None): setattr(namespace, self.dest, []) # Accept either a comma-separated list or a filename (starting with @) # containing a value on each line if values and values[0] == "@": value = values[1:] if not os.path.exists(value): log.warning(f"{self.dest} argument {value} does not exist") return try: values = [v.strip() for v in open(value).readlines()] setattr(namespace, self.dest, values) except OSError as exc: log.warning( "reading {} for {} failed: {}; ignoring this argument".format( value, self.dest, exc ) ) del exc else: setattr(namespace, self.dest, [v.strip() for v in values.split(",")]) def handle_options(argv=None): parser = argparse.ArgumentParser( description=DESCRIPTION, epilog=EPILOG, formatter_class=argparse.RawDescriptionHelpFormatter, ) parser.add_argument( "--version", action="version", version=f"%(prog)s {__version__}" ) parser.add_argument( "fits_files", metavar="file", nargs="+", help=".fits files to process." ) parser.add_argument( "-q", "--quiet", action="store_true", help="Produce no output and just return a status code.", ) parser.add_argument( "-n", "--num-diffs", type=int, default=10, dest="numdiffs", metavar="INTEGER", help=( "Max number of data differences (image pixel or table element) " "to report per extension (default %(default)s)." ), ) parser.add_argument( "-r", "--rtol", "--relative-tolerance", type=float, default=None, dest="rtol", metavar="NUMBER", help=( "The relative tolerance for comparison of two numbers, " "specifically two floating point numbers. This applies to data " "in both images and tables, and to floating point keyword values " "in headers (default %(default)s)." ), ) parser.add_argument( "-a", "--atol", "--absolute-tolerance", type=float, default=None, dest="atol", metavar="NUMBER", help=( "The absolute tolerance for comparison of two numbers, " "specifically two floating point numbers. This applies to data " "in both images and tables, and to floating point keyword values " "in headers (default %(default)s)." ), ) parser.add_argument( "-b", "--no-ignore-blanks", action="store_false", dest="ignore_blanks", default=True, help=( "Don't ignore trailing blanks (whitespace) in string values. " "Otherwise trailing blanks both in header keywords/values and in " "table column values) are not treated as significant i.e., " "without this option 'ABCDEF ' and 'ABCDEF' are considered " "equivalent. " ), ) parser.add_argument( "--no-ignore-blank-cards", action="store_false", dest="ignore_blank_cards", default=True, help=( "Don't ignore entirely blank cards in headers. Normally fitsdiff " "does not consider blank cards when comparing headers, but this " "will ensure that even blank cards match up. " ), ) parser.add_argument( "--exact", action="store_true", dest="exact_comparisons", default=False, help=( "Report ALL differences, " "overriding command-line options and FITSDIFF_SETTINGS. " ), ) parser.add_argument( "-o", "--output-file", metavar="FILE", help="Output results to this file; otherwise results are printed to stdout.", ) parser.add_argument( "-u", "--ignore-hdus", action=StoreListAction, default=[], dest="ignore_hdus", metavar="HDU_NAMES", help=( "Comma-separated list of HDU names not to be compared. HDU " "names may contain wildcard patterns." ), ) group = parser.add_argument_group("Header Comparison Options") group.add_argument( "-k", "--ignore-keywords", action=StoreListAction, default=[], dest="ignore_keywords", metavar="KEYWORDS", help=( "Comma-separated list of keywords not to be compared. Keywords " "may contain wildcard patterns. To exclude all keywords, use " '"*"; make sure to have double or single quotes around the ' "asterisk on the command-line." ), ) group.add_argument( "-c", "--ignore-comments", action=StoreListAction, default=[], dest="ignore_comments", metavar="COMMENTS", help=( "Comma-separated list of keywords whose comments will not be " "compared. Wildcards may be used as with --ignore-keywords." ), ) group = parser.add_argument_group("Table Comparison Options") group.add_argument( "-f", "--ignore-fields", action=StoreListAction, default=[], dest="ignore_fields", metavar="COLUMNS", help=( "Comma-separated list of fields (i.e. columns) not to be " 'compared. All columns may be excluded using "*" as with ' "--ignore-keywords." ), ) options = parser.parse_args(argv) # Determine which filenames to compare if len(options.fits_files) != 2: parser.error( "\nfitsdiff requires two arguments; see `fitsdiff --help` for more details." ) return options def setup_logging(outfile=None): log.setLevel(logging.INFO) error_handler = logging.StreamHandler(sys.stderr) error_handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) error_handler.setLevel(logging.WARNING) log.addHandler(error_handler) if outfile is not None: output_handler = logging.FileHandler(outfile) else: output_handler = logging.StreamHandler() class LevelFilter(logging.Filter): """Log only messages matching the specified level.""" def __init__(self, name="", level=logging.NOTSET): logging.Filter.__init__(self, name) self.level = level def filter(self, rec): return rec.levelno == self.level # File output logs all messages, but stdout logs only INFO messages # (since errors are already logged to stderr) output_handler.addFilter(LevelFilter(level=logging.INFO)) output_handler.setFormatter(logging.Formatter("%(message)s")) log.addHandler(output_handler) def match_files(paths): if os.path.isfile(paths[0]) and os.path.isfile(paths[1]): # shortcut if both paths are files return [paths] dirnames = [None, None] filelists = [None, None] for i, path in enumerate(paths): if glob.has_magic(path): files = [os.path.split(f) for f in glob.glob(path)] if not files: log.error("Wildcard pattern %r did not match any files.", path) sys.exit(2) dirs, files = list(zip(*files)) if len(set(dirs)) > 1: log.error("Wildcard pattern %r should match only one directory.", path) sys.exit(2) dirnames[i] = set(dirs).pop() filelists[i] = sorted(files) elif os.path.isdir(path): dirnames[i] = path filelists[i] = [ f for f in sorted(os.listdir(path)) if os.path.isfile(os.path.join(path, f)) ] elif os.path.isfile(path): dirnames[i] = os.path.dirname(path) filelists[i] = [os.path.basename(path)] else: log.error( "%r is not an existing file, directory, or wildcard " "pattern; see `fitsdiff --help` for more usage help.", path, ) sys.exit(2) dirnames[i] = os.path.abspath(dirnames[i]) filematch = set(filelists[0]) & set(filelists[1]) for a, b in [(0, 1), (1, 0)]: if len(filelists[a]) > len(filematch) and not os.path.isdir(paths[a]): for extra in sorted(set(filelists[a]) - filematch): log.warning("%r has no match in %r", extra, dirnames[b]) return [ (os.path.join(dirnames[0], f), os.path.join(dirnames[1], f)) for f in filematch ] def main(args=None): args = args or sys.argv[1:] if "FITSDIFF_SETTINGS" in os.environ: args = os.environ["FITSDIFF_SETTINGS"].split() + args opts = handle_options(args) if opts.rtol is None: opts.rtol = 0.0 if opts.atol is None: opts.atol = 0.0 if opts.exact_comparisons: # override the options so that each is the most restrictive opts.ignore_keywords = [] opts.ignore_comments = [] opts.ignore_fields = [] opts.rtol = 0.0 opts.atol = 0.0 opts.ignore_blanks = False opts.ignore_blank_cards = False if not opts.quiet: setup_logging(opts.output_file) files = match_files(opts.fits_files) close_file = False if opts.quiet: out_file = None elif opts.output_file: out_file = open(opts.output_file, "w") close_file = True else: out_file = sys.stdout identical = [] try: for a, b in files: # TODO: pass in any additional arguments here too diff = fits.diff.FITSDiff( a, b, ignore_hdus=opts.ignore_hdus, ignore_keywords=opts.ignore_keywords, ignore_comments=opts.ignore_comments, ignore_fields=opts.ignore_fields, numdiffs=opts.numdiffs, rtol=opts.rtol, atol=opts.atol, ignore_blanks=opts.ignore_blanks, ignore_blank_cards=opts.ignore_blank_cards, ) diff.report(fileobj=out_file) identical.append(diff.identical) return int(not all(identical)) finally: if close_file: out_file.close() # Close the file if used for the logging output, and remove handlers to # avoid having them multiple times for unit tests. for handler in log.handlers: if isinstance(handler, logging.FileHandler): handler.close() log.removeHandler(handler)
dfe656eb2ffdc45479043656cccd7d47c1067aeaf2b9dfe96f557fe95d740049
# Licensed under a 3-clause BSD style license - see PYFITS.rst import sys import numpy as np from astropy.io.fits.column import FITS2NUMPY, ColDefs, Column from astropy.io.fits.fitsrec import FITS_rec, FITS_record from astropy.io.fits.util import _is_int, _is_pseudo_integer, _pseudo_zero from astropy.utils import lazyproperty from .base import DELAYED, DTYPE2BITPIX from .image import PrimaryHDU from .table import _TableLikeHDU class Group(FITS_record): """ One group of the random group data. """ def __init__(self, input, row=0, start=None, end=None, step=None, base=None): super().__init__(input, row, start, end, step, base) @property def parnames(self): return self.array.parnames @property def data(self): # The last column in the coldefs is the data portion of the group return self.field(self.array._coldefs.names[-1]) @lazyproperty def _unique(self): return _par_indices(self.parnames) def par(self, parname): """ Get the group parameter value. """ if _is_int(parname): result = self.array[self.row][parname] else: indx = self._unique[parname.upper()] if len(indx) == 1: result = self.array[self.row][indx[0]] # if more than one group parameter have the same name else: result = self.array[self.row][indx[0]].astype("f8") for i in indx[1:]: result += self.array[self.row][i] return result def setpar(self, parname, value): """ Set the group parameter value. """ # TODO: It would be nice if, instead of requiring a multi-part value to # be an array, there were an *option* to automatically split the value # into multiple columns if it doesn't already fit in the array data # type. if _is_int(parname): self.array[self.row][parname] = value else: indx = self._unique[parname.upper()] if len(indx) == 1: self.array[self.row][indx[0]] = value # if more than one group parameter have the same name, the # value must be a list (or tuple) containing arrays else: if isinstance(value, (list, tuple)) and len(indx) == len(value): for i in range(len(indx)): self.array[self.row][indx[i]] = value[i] else: raise ValueError( "Parameter value must be a sequence with " "{} arrays/numbers.".format(len(indx)) ) class GroupData(FITS_rec): """ Random groups data object. Allows structured access to FITS Group data in a manner analogous to tables. """ _record_type = Group def __new__( cls, input=None, bitpix=None, pardata=None, parnames=[], bscale=None, bzero=None, parbscales=None, parbzeros=None, ): """ Parameters ---------- input : array or FITS_rec instance input data, either the group data itself (a `numpy.ndarray`) or a record array (`FITS_rec`) which will contain both group parameter info and the data. The rest of the arguments are used only for the first case. bitpix : int data type as expressed in FITS ``BITPIX`` value (8, 16, 32, 64, -32, or -64) pardata : sequence of array parameter data, as a list of (numeric) arrays. parnames : sequence of str list of parameter names. bscale : int ``BSCALE`` of the data bzero : int ``BZERO`` of the data parbscales : sequence of int list of bscales for the parameters parbzeros : sequence of int list of bzeros for the parameters """ if not isinstance(input, FITS_rec): if pardata is None: npars = 0 else: npars = len(pardata) if parbscales is None: parbscales = [None] * npars if parbzeros is None: parbzeros = [None] * npars if parnames is None: parnames = [f"PAR{idx + 1}" for idx in range(npars)] if len(parnames) != npars: raise ValueError( "The number of parameter data arrays does " "not match the number of parameters." ) unique_parnames = _unique_parnames(parnames + ["DATA"]) if bitpix is None: bitpix = DTYPE2BITPIX[input.dtype.name] fits_fmt = GroupsHDU._bitpix2tform[bitpix] # -32 -> 'E' format = FITS2NUMPY[fits_fmt] # 'E' -> 'f4' data_fmt = f"{str(input.shape[1:])}{format}" formats = ",".join(([format] * npars) + [data_fmt]) gcount = input.shape[0] cols = [ Column( name=unique_parnames[idx], format=fits_fmt, bscale=parbscales[idx], bzero=parbzeros[idx], ) for idx in range(npars) ] cols.append( Column( name=unique_parnames[-1], format=fits_fmt, bscale=bscale, bzero=bzero, ) ) coldefs = ColDefs(cols) self = FITS_rec.__new__( cls, np.rec.array(None, formats=formats, names=coldefs.names, shape=gcount), ) # By default the data field will just be 'DATA', but it may be # uniquified if 'DATA' is already used by one of the group names self._data_field = unique_parnames[-1] self._coldefs = coldefs self.parnames = parnames for idx, name in enumerate(unique_parnames[:-1]): column = coldefs[idx] # Note: _get_scale_factors is used here and in other cases # below to determine whether the column has non-default # scale/zero factors. # TODO: Find a better way to do this than using this interface scale, zero = self._get_scale_factors(column)[3:5] if scale or zero: self._cache_field(name, pardata[idx]) else: np.rec.recarray.field(self, idx)[:] = pardata[idx] column = coldefs[self._data_field] scale, zero = self._get_scale_factors(column)[3:5] if scale or zero: self._cache_field(self._data_field, input) else: np.rec.recarray.field(self, npars)[:] = input else: self = FITS_rec.__new__(cls, input) self.parnames = None return self def __array_finalize__(self, obj): super().__array_finalize__(obj) if isinstance(obj, GroupData): self.parnames = obj.parnames elif isinstance(obj, FITS_rec): self.parnames = obj._coldefs.names def __getitem__(self, key): out = super().__getitem__(key) if isinstance(out, GroupData): out.parnames = self.parnames return out @property def data(self): """ The raw group data represented as a multi-dimensional `numpy.ndarray` array. """ # The last column in the coldefs is the data portion of the group return self.field(self._coldefs.names[-1]) @lazyproperty def _unique(self): return _par_indices(self.parnames) def par(self, parname): """ Get the group parameter values. """ if _is_int(parname): result = self.field(parname) else: indx = self._unique[parname.upper()] if len(indx) == 1: result = self.field(indx[0]) # if more than one group parameter have the same name else: result = self.field(indx[0]).astype("f8") for i in indx[1:]: result += self.field(i) return result class GroupsHDU(PrimaryHDU, _TableLikeHDU): """ FITS Random Groups HDU class. See the :ref:`astropy:random-groups` section in the Astropy documentation for more details on working with this type of HDU. """ _bitpix2tform = {8: "B", 16: "I", 32: "J", 64: "K", -32: "E", -64: "D"} _data_type = GroupData _data_field = "DATA" """ The name of the table record array field that will contain the group data for each group; 'DATA' by default, but may be preceded by any number of underscores if 'DATA' is already a parameter name """ def __init__(self, data=None, header=None): super().__init__(data=data, header=header) if data is not DELAYED: self.update_header() # Update the axes; GROUPS HDUs should always have at least one axis if len(self._axes) <= 0: self._axes = [0] self._header["NAXIS"] = 1 self._header.set("NAXIS1", 0, after="NAXIS") @classmethod def match_header(cls, header): keyword = header.cards[0].keyword return keyword == "SIMPLE" and "GROUPS" in header and header["GROUPS"] is True @lazyproperty def data(self): """ The data of a random group FITS file will be like a binary table's data. """ if self._axes == [0]: return data = self._get_tbdata() data._coldefs = self.columns data.parnames = self.parnames del self.columns return data @lazyproperty def parnames(self): """The names of the group parameters as described by the header.""" pcount = self._header["PCOUNT"] # The FITS standard doesn't really say what to do if a parname is # missing, so for now just assume that won't happen return [self._header["PTYPE" + str(idx + 1)] for idx in range(pcount)] @lazyproperty def columns(self): if self._has_data and hasattr(self.data, "_coldefs"): return self.data._coldefs format = self._bitpix2tform[self._header["BITPIX"]] pcount = self._header["PCOUNT"] parnames = [] bscales = [] bzeros = [] for idx in range(pcount): bscales.append(self._header.get("PSCAL" + str(idx + 1), None)) bzeros.append(self._header.get("PZERO" + str(idx + 1), None)) parnames.append(self._header["PTYPE" + str(idx + 1)]) formats = [format] * len(parnames) dim = [None] * len(parnames) # Now create columns from collected parameters, but first add the DATA # column too, to contain the group data. parnames.append("DATA") bscales.append(self._header.get("BSCALE")) bzeros.append(self._header.get("BZEROS")) data_shape = self.shape[:-1] formats.append(str(int(np.prod(data_shape))) + format) dim.append(data_shape) parnames = _unique_parnames(parnames) self._data_field = parnames[-1] cols = [ Column(name=name, format=fmt, bscale=bscale, bzero=bzero, dim=dim) for name, fmt, bscale, bzero, dim in zip( parnames, formats, bscales, bzeros, dim ) ] coldefs = ColDefs(cols) return coldefs @property def _nrows(self): if not self._data_loaded: # The number of 'groups' equates to the number of rows in the table # representation of the data return self._header.get("GCOUNT", 0) else: return len(self.data) @lazyproperty def _theap(self): # Only really a lazyproperty for symmetry with _TableBaseHDU return 0 @property def is_image(self): return False @property def size(self): """ Returns the size (in bytes) of the HDU's data part. """ size = 0 naxis = self._header.get("NAXIS", 0) # for random group image, NAXIS1 should be 0, so we skip NAXIS1. if naxis > 1: size = 1 for idx in range(1, naxis): size = size * self._header["NAXIS" + str(idx + 1)] bitpix = self._header["BITPIX"] gcount = self._header.get("GCOUNT", 1) pcount = self._header.get("PCOUNT", 0) size = abs(bitpix) * gcount * (pcount + size) // 8 return size def update_header(self): old_naxis = self._header.get("NAXIS", 0) if self._data_loaded: if isinstance(self.data, GroupData): self._axes = list(self.data.data.shape)[1:] self._axes.reverse() self._axes = [0] + self._axes field0 = self.data.dtype.names[0] field0_code = self.data.dtype.fields[field0][0].name elif self.data is None: self._axes = [0] field0_code = "uint8" # For lack of a better default else: raise ValueError("incorrect array type") self._header["BITPIX"] = DTYPE2BITPIX[field0_code] self._header["NAXIS"] = len(self._axes) # add NAXISi if it does not exist for idx, axis in enumerate(self._axes): if idx == 0: after = "NAXIS" else: after = "NAXIS" + str(idx) self._header.set("NAXIS" + str(idx + 1), axis, after=after) # delete extra NAXISi's for idx in range(len(self._axes) + 1, old_naxis + 1): try: del self._header["NAXIS" + str(idx)] except KeyError: pass if self._has_data and isinstance(self.data, GroupData): self._header.set("GROUPS", True, after="NAXIS" + str(len(self._axes))) self._header.set("PCOUNT", len(self.data.parnames), after="GROUPS") self._header.set("GCOUNT", len(self.data), after="PCOUNT") column = self.data._coldefs[self._data_field] scale, zero = self.data._get_scale_factors(column)[3:5] if scale: self._header.set("BSCALE", column.bscale) if zero: self._header.set("BZERO", column.bzero) for idx, name in enumerate(self.data.parnames): self._header.set("PTYPE" + str(idx + 1), name) column = self.data._coldefs[idx] scale, zero = self.data._get_scale_factors(column)[3:5] if scale: self._header.set("PSCAL" + str(idx + 1), column.bscale) if zero: self._header.set("PZERO" + str(idx + 1), column.bzero) # Update the position of the EXTEND keyword if it already exists if "EXTEND" in self._header: if len(self._axes): after = "NAXIS" + str(len(self._axes)) else: after = "NAXIS" self._header.set("EXTEND", after=after) def _writedata_internal(self, fileobj): """ Basically copy/pasted from `_ImageBaseHDU._writedata_internal()`, but we have to get the data's byte order a different way... TODO: Might be nice to store some indication of the data's byte order as an attribute or function so that we don't have to do this. """ size = 0 if self.data is not None: self.data._scale_back() # Based on the system type, determine the byteorders that # would need to be swapped to get to big-endian output if sys.byteorder == "little": swap_types = ("<", "=") else: swap_types = ("<",) # deal with unsigned integer 16, 32 and 64 data if _is_pseudo_integer(self.data.dtype): # Convert the unsigned array to signed output = np.array( self.data - _pseudo_zero(self.data.dtype), dtype=f">i{self.data.dtype.itemsize}", ) should_swap = False else: output = self.data fname = self.data.dtype.names[0] byteorder = self.data.dtype.fields[fname][0].str[0] should_swap = byteorder in swap_types if should_swap: if output.flags.writeable: output.byteswap(True) try: fileobj.writearray(output) finally: output.byteswap(True) else: # For read-only arrays, there is no way around making # a byteswapped copy of the data. fileobj.writearray(output.byteswap(False)) else: fileobj.writearray(output) size += output.size * output.itemsize return size def _verify(self, option="warn"): errs = super()._verify(option=option) # Verify locations and values of mandatory keywords. self.req_cards( "NAXIS", 2, lambda v: (_is_int(v) and 1 <= v <= 999), 1, option, errs ) self.req_cards("NAXIS1", 3, lambda v: (_is_int(v) and v == 0), 0, option, errs) after = self._header["NAXIS"] + 3 pos = lambda x: x >= after self.req_cards("GCOUNT", pos, _is_int, 1, option, errs) self.req_cards("PCOUNT", pos, _is_int, 0, option, errs) self.req_cards("GROUPS", pos, lambda v: (v is True), True, option, errs) return errs def _calculate_datasum(self): """ Calculate the value for the ``DATASUM`` card in the HDU. """ if self._has_data: # We have the data to be used. # Check the byte order of the data. If it is little endian we # must swap it before calculating the datasum. # TODO: Maybe check this on a per-field basis instead of assuming # that all fields have the same byte order? byteorder = self.data.dtype.fields[self.data.dtype.names[0]][0].str[0] if byteorder != ">": if self.data.flags.writeable: byteswapped = True d = self.data.byteswap(True) d.dtype = d.dtype.newbyteorder(">") else: # If the data is not writeable, we just make a byteswapped # copy and don't bother changing it back after d = self.data.byteswap(False) d.dtype = d.dtype.newbyteorder(">") byteswapped = False else: byteswapped = False d = self.data byte_data = d.view(type=np.ndarray, dtype=np.ubyte) cs = self._compute_checksum(byte_data) # If the data was byteswapped in this method then return it to # its original little-endian order. if byteswapped: d.byteswap(True) d.dtype = d.dtype.newbyteorder("<") return cs else: # This is the case where the data has not been read from the file # yet. We can handle that in a generic manner so we do it in the # base class. The other possibility is that there is no data at # all. This can also be handled in a generic manner. return super()._calculate_datasum() def _summary(self): summary = super()._summary() name, ver, classname, length, shape, format, gcount = summary # Drop the first axis from the shape if shape: shape = shape[1:] if shape and all(shape): # Update the format format = self.columns[0].dtype.name # Update the GCOUNT report gcount = f"{self._gcount} Groups {self._pcount} Parameters" return (name, ver, classname, length, shape, format, gcount) def _par_indices(names): """ Given a list of objects, returns a mapping of objects in that list to the index or indices at which that object was found in the list. """ unique = {} for idx, name in enumerate(names): # Case insensitive name = name.upper() if name in unique: unique[name].append(idx) else: unique[name] = [idx] return unique def _unique_parnames(names): """ Given a list of parnames, including possible duplicates, returns a new list of parnames with duplicates prepended by one or more underscores to make them unique. This is also case insensitive. """ upper_names = set() unique_names = [] for name in names: name_upper = name.upper() while name_upper in upper_names: name = "_" + name name_upper = "_" + name_upper unique_names.append(name) upper_names.add(name_upper) return unique_names
c97ddb5cad4051694de8e76f10046688e50a4ad489a5683b39c6499ccfe3c361
# Licensed under a 3-clause BSD style license - see PYFITS.rst import contextlib import csv import operator import os import re import sys import textwrap import warnings from contextlib import suppress import numpy as np from numpy import char as chararray # This module may have many dependencies on astropy.io.fits.column, but # astropy.io.fits.column has fewer dependencies overall, so it's easier to # keep table/column-related utilities in astropy.io.fits.column from astropy.io.fits.column import ( ATTRIBUTE_TO_KEYWORD, FITS2NUMPY, KEYWORD_NAMES, KEYWORD_TO_ATTRIBUTE, TDEF_RE, ColDefs, Column, _AsciiColDefs, _cmp_recformats, _convert_format, _FormatP, _FormatQ, _makep, _parse_tformat, _scalar_to_format, ) from astropy.io.fits.fitsrec import FITS_rec, _get_recarray_field, _has_unicode_fields from astropy.io.fits.header import Header, _pad_length from astropy.io.fits.util import _is_int, _str_to_num, path_like from astropy.utils import lazyproperty from astropy.utils.exceptions import AstropyDeprecationWarning from .base import DELAYED, ExtensionHDU, _ValidHDU class FITSTableDumpDialect(csv.excel): """ A CSV dialect for the Astropy format of ASCII dumps of FITS tables. """ delimiter = " " lineterminator = "\n" quotechar = '"' quoting = csv.QUOTE_ALL skipinitialspace = True class _TableLikeHDU(_ValidHDU): """ A class for HDUs that have table-like data. This is used for both Binary/ASCII tables as well as Random Access Group HDUs (which are otherwise too dissimilar for tables to use _TableBaseHDU directly). """ _data_type = FITS_rec _columns_type = ColDefs # TODO: Temporary flag representing whether uints are enabled; remove this # after restructuring to support uints by default on a per-column basis _uint = False @classmethod def match_header(cls, header): """ This is an abstract HDU type for HDUs that contain table-like data. This is even more abstract than _TableBaseHDU which is specifically for the standard ASCII and Binary Table types. """ raise NotImplementedError @classmethod def from_columns( cls, columns, header=None, nrows=0, fill=False, character_as_bytes=False, **kwargs, ): """ Given either a `ColDefs` object, a sequence of `Column` objects, or another table HDU or table data (a `FITS_rec` or multi-field `numpy.ndarray` or `numpy.recarray` object, return a new table HDU of the class this method was called on using the column definition from the input. See also `FITS_rec.from_columns`. Parameters ---------- columns : sequence of `Column`, `ColDefs` -like The columns from which to create the table data, or an object with a column-like structure from which a `ColDefs` can be instantiated. This includes an existing `BinTableHDU` or `TableHDU`, or a `numpy.recarray` to give some examples. If these columns have data arrays attached that data may be used in initializing the new table. Otherwise the input columns will be used as a template for a new table with the requested number of rows. header : `Header` An optional `Header` object to instantiate the new HDU yet. Header keywords specifically related to defining the table structure (such as the "TXXXn" keywords like TTYPEn) will be overridden by the supplied column definitions, but all other informational and data model-specific keywords are kept. nrows : int Number of rows in the new table. If the input columns have data associated with them, the size of the largest input column is used. Otherwise the default is 0. fill : bool If `True`, will fill all cells with zeros or blanks. If `False`, copy the data from input, undefined cells will still be filled with zeros/blanks. character_as_bytes : bool Whether to return bytes for string columns when accessed from the HDU. By default this is `False` and (unicode) strings are returned, but for large tables this may use up a lot of memory. Notes ----- Any additional keyword arguments accepted by the HDU class's ``__init__`` may also be passed in as keyword arguments. """ coldefs = cls._columns_type(columns) data = FITS_rec.from_columns( coldefs, nrows=nrows, fill=fill, character_as_bytes=character_as_bytes ) hdu = cls( data=data, header=header, character_as_bytes=character_as_bytes, **kwargs ) coldefs._add_listener(hdu) return hdu @lazyproperty def columns(self): """ The :class:`ColDefs` objects describing the columns in this table. """ # The base class doesn't make any assumptions about where the column # definitions come from, so just return an empty ColDefs return ColDefs([]) @property def _nrows(self): """ table-like HDUs must provide an attribute that specifies the number of rows in the HDU's table. For now this is an internal-only attribute. """ raise NotImplementedError def _get_tbdata(self): """Get the table data from an input HDU object.""" columns = self.columns # TODO: Details related to variable length arrays need to be dealt with # specifically in the BinTableHDU class, since they're a detail # specific to FITS binary tables if ( any(type(r) in (_FormatP, _FormatQ) for r in columns._recformats) and self._data_size is not None and self._data_size > self._theap ): # We have a heap; include it in the raw_data raw_data = self._get_raw_data(self._data_size, np.uint8, self._data_offset) tbsize = self._header["NAXIS1"] * self._header["NAXIS2"] data = raw_data[:tbsize].view(dtype=columns.dtype, type=np.rec.recarray) else: raw_data = self._get_raw_data(self._nrows, columns.dtype, self._data_offset) if raw_data is None: # This can happen when a brand new table HDU is being created # and no data has been assigned to the columns, which case just # return an empty array raw_data = np.array([], dtype=columns.dtype) data = raw_data.view(np.rec.recarray) self._init_tbdata(data) data = data.view(self._data_type) columns._add_listener(data) return data def _init_tbdata(self, data): columns = self.columns data.dtype = data.dtype.newbyteorder(">") # hack to enable pseudo-uint support data._uint = self._uint # pass datLoc, for P format data._heapoffset = self._theap data._heapsize = self._header["PCOUNT"] tbsize = self._header["NAXIS1"] * self._header["NAXIS2"] data._gap = self._theap - tbsize # pass the attributes for idx, col in enumerate(columns): # get the data for each column object from the rec.recarray col.array = data.field(idx) # delete the _arrays attribute so that it is recreated to point to the # new data placed in the column object above del columns._arrays def _update_load_data(self): """Load the data if asked to.""" if not self._data_loaded: self.data def _update_column_added(self, columns, column): """ Update the data upon addition of a new column through the `ColDefs` interface. """ # recreate data from the columns self.data = FITS_rec.from_columns( self.columns, nrows=self._nrows, fill=False, character_as_bytes=self._character_as_bytes, ) def _update_column_removed(self, columns, col_idx): """ Update the data upon removal of a column through the `ColDefs` interface. """ # recreate data from the columns self.data = FITS_rec.from_columns( self.columns, nrows=self._nrows, fill=False, character_as_bytes=self._character_as_bytes, ) class _TableBaseHDU(ExtensionHDU, _TableLikeHDU): """ FITS table extension base HDU class. Parameters ---------- data : array Data to be used. header : `Header` instance Header to be used. If the ``data`` is also specified, header keywords specifically related to defining the table structure (such as the "TXXXn" keywords like TTYPEn) will be overridden by the supplied column definitions, but all other informational and data model-specific keywords are kept. name : str Name to be populated in ``EXTNAME`` keyword. uint : bool, optional Set to `True` if the table contains unsigned integer columns. ver : int > 0 or None, optional The ver of the HDU, will be the value of the keyword ``EXTVER``. If not given or None, it defaults to the value of the ``EXTVER`` card of the ``header`` or 1. (default: None) character_as_bytes : bool Whether to return bytes for string columns. By default this is `False` and (unicode) strings are returned, but this does not respect memory mapping and loads the whole column in memory when accessed. """ _manages_own_heap = False """ This flag implies that when writing VLA tables (P/Q format) the heap pointers that go into P/Q table columns should not be reordered or rearranged in any way by the default heap management code. This is included primarily as an optimization for compressed image HDUs which perform their own heap maintenance. """ def __init__( self, data=None, header=None, name=None, uint=False, ver=None, character_as_bytes=False, ): super().__init__(data=data, header=header, name=name, ver=ver) self._uint = uint self._character_as_bytes = character_as_bytes if data is DELAYED: # this should never happen if header is None: raise ValueError("No header to setup HDU.") # if the file is read the first time, no need to copy, and keep it # unchanged else: self._header = header else: # construct a list of cards of minimal header cards = [ ("XTENSION", self._extension, self._ext_comment), ("BITPIX", 8, "array data type"), ("NAXIS", 2, "number of array dimensions"), ("NAXIS1", 0, "length of dimension 1"), ("NAXIS2", 0, "length of dimension 2"), ("PCOUNT", 0, "number of group parameters"), ("GCOUNT", 1, "number of groups"), ("TFIELDS", 0, "number of table fields"), ] if header is not None: # Make a "copy" (not just a view) of the input header, since it # may get modified. the data is still a "view" (for now) hcopy = header.copy(strip=True) cards.extend(hcopy.cards) self._header = Header(cards) if isinstance(data, np.ndarray) and data.dtype.fields is not None: # self._data_type is FITS_rec. if isinstance(data, self._data_type): self.data = data else: self.data = self._data_type.from_columns(data) # TEMP: Special column keywords are normally overwritten by attributes # from Column objects. In Astropy 3.0, several new keywords are now # recognized as being special column keywords, but we don't # automatically clear them yet, as we need to raise a deprecation # warning for at least one major version. if header is not None: future_ignore = set() for keyword in header.keys(): match = TDEF_RE.match(keyword) try: base_keyword = match.group("label") except Exception: continue # skip if there is no match if base_keyword in { "TCTYP", "TCUNI", "TCRPX", "TCRVL", "TCDLT", "TRPOS", }: future_ignore.add(base_keyword) if future_ignore: keys = ", ".join(x + "n" for x in sorted(future_ignore)) warnings.warn( "The following keywords are now recognized as special " "column-related attributes and should be set via the " "Column objects: {}. In future, these values will be " "dropped from manually specified headers automatically " "and replaced with values generated based on the " "Column objects.".format(keys), AstropyDeprecationWarning, ) # TODO: Too much of the code in this class uses header keywords # in making calculations related to the data size. This is # unreliable, however, in cases when users mess with the header # unintentionally--code that does this should be cleaned up. self._header["NAXIS1"] = self.data._raw_itemsize self._header["NAXIS2"] = self.data.shape[0] self._header["TFIELDS"] = len(self.data._coldefs) self.columns = self.data._coldefs self.columns._add_listener(self.data) self.update() with suppress(TypeError, AttributeError): # Make the ndarrays in the Column objects of the ColDefs # object of the HDU reference the same ndarray as the HDU's # FITS_rec object. for idx, col in enumerate(self.columns): col.array = self.data.field(idx) # Delete the _arrays attribute so that it is recreated to # point to the new data placed in the column objects above del self.columns._arrays elif data is None: pass else: raise TypeError("Table data has incorrect type.") # Ensure that the correct EXTNAME is set on the new header if one was # created, or that it overrides the existing EXTNAME if different if name: self.name = name if ver is not None: self.ver = ver @classmethod def match_header(cls, header): """ This is an abstract type that implements the shared functionality of the ASCII and Binary Table HDU types, which should be used instead of this. """ raise NotImplementedError @lazyproperty def columns(self): """ The :class:`ColDefs` objects describing the columns in this table. """ if self._has_data and hasattr(self.data, "_coldefs"): return self.data._coldefs return self._columns_type(self) @lazyproperty def data(self): data = self._get_tbdata() data._coldefs = self.columns data._character_as_bytes = self._character_as_bytes # Columns should now just return a reference to the data._coldefs del self.columns return data @data.setter def data(self, data): if "data" in self.__dict__: if self.__dict__["data"] is data: return else: self._data_replaced = True else: self._data_replaced = True self._modified = True if data is None and self.columns: # Create a new table with the same columns, but empty rows formats = ",".join(self.columns._recformats) data = np.rec.array( None, formats=formats, names=self.columns.names, shape=0 ) if isinstance(data, np.ndarray) and data.dtype.fields is not None: # Go ahead and always make a view, even if the data is already the # correct class (self._data_type) so we can update things like the # column defs, if necessary data = data.view(self._data_type) if not isinstance(data.columns, self._columns_type): # This would be the place, if the input data was for an ASCII # table and this is binary table, or vice versa, to convert the # data to the appropriate format for the table type new_columns = self._columns_type(data.columns) data = FITS_rec.from_columns(new_columns) if "data" in self.__dict__: self.columns._remove_listener(self.__dict__["data"]) self.__dict__["data"] = data self.columns = self.data.columns self.columns._add_listener(self.data) self.update() with suppress(TypeError, AttributeError): # Make the ndarrays in the Column objects of the ColDefs # object of the HDU reference the same ndarray as the HDU's # FITS_rec object. for idx, col in enumerate(self.columns): col.array = self.data.field(idx) # Delete the _arrays attribute so that it is recreated to # point to the new data placed in the column objects above del self.columns._arrays elif data is None: pass else: raise TypeError("Table data has incorrect type.") # returning the data signals to lazyproperty that we've already handled # setting self.__dict__['data'] return data @property def _nrows(self): if not self._data_loaded: return self._header.get("NAXIS2", 0) else: return len(self.data) @lazyproperty def _theap(self): size = self._header["NAXIS1"] * self._header["NAXIS2"] return self._header.get("THEAP", size) # TODO: Need to either rename this to update_header, for symmetry with the # Image HDUs, or just at some point deprecate it and remove it altogether, # since header updates should occur automatically when necessary... def update(self): """ Update header keywords to reflect recent changes of columns. """ self._header.set("NAXIS1", self.data._raw_itemsize, after="NAXIS") self._header.set("NAXIS2", self.data.shape[0], after="NAXIS1") self._header.set("TFIELDS", len(self.columns), after="GCOUNT") self._clear_table_keywords() self._populate_table_keywords() def copy(self): """ Make a copy of the table HDU, both header and data are copied. """ # touch the data, so it's defined (in the case of reading from a # FITS file) return self.__class__(data=self.data.copy(), header=self._header.copy()) def _prewriteto(self, checksum=False, inplace=False): if self._has_data: self.data._scale_back(update_heap_pointers=not self._manages_own_heap) # check TFIELDS and NAXIS2 self._header["TFIELDS"] = len(self.data._coldefs) self._header["NAXIS2"] = self.data.shape[0] # calculate PCOUNT, for variable length tables tbsize = self._header["NAXIS1"] * self._header["NAXIS2"] heapstart = self._header.get("THEAP", tbsize) self.data._gap = heapstart - tbsize pcount = self.data._heapsize + self.data._gap if pcount > 0: self._header["PCOUNT"] = pcount # update the other T****n keywords self._populate_table_keywords() # update TFORM for variable length columns for idx in range(self.data._nfields): format = self.data._coldefs._recformats[idx] if isinstance(format, _FormatP): _max = self.data.field(idx).max # May be either _FormatP or _FormatQ format_cls = format.__class__ format = format_cls(format.dtype, repeat=format.repeat, max=_max) self._header["TFORM" + str(idx + 1)] = format.tform return super()._prewriteto(checksum, inplace) def _verify(self, option="warn"): """ _TableBaseHDU verify method. """ errs = super()._verify(option=option) if len(self._header) > 1: if not ( isinstance(self._header[0], str) and self._header[0].rstrip() == self._extension ): err_text = "The XTENSION keyword must match the HDU type." fix_text = f"Converted the XTENSION keyword to {self._extension}." def fix(header=self._header): header[0] = (self._extension, self._ext_comment) errs.append( self.run_option( option, err_text=err_text, fix_text=fix_text, fix=fix ) ) self.req_cards("NAXIS", None, lambda v: (v == 2), 2, option, errs) self.req_cards("BITPIX", None, lambda v: (v == 8), 8, option, errs) self.req_cards( "TFIELDS", 7, lambda v: (_is_int(v) and v >= 0 and v <= 999), 0, option, errs, ) tfields = self._header["TFIELDS"] for idx in range(tfields): self.req_cards("TFORM" + str(idx + 1), None, None, None, option, errs) return errs def _summary(self): """ Summarize the HDU: name, dimensions, and formats. """ class_name = self.__class__.__name__ # if data is touched, use data info. if self._data_loaded: if self.data is None: nrows = 0 else: nrows = len(self.data) ncols = len(self.columns) format = self.columns.formats # if data is not touched yet, use header info. else: nrows = self._header["NAXIS2"] ncols = self._header["TFIELDS"] format = ", ".join( [self._header["TFORM" + str(j + 1)] for j in range(ncols)] ) format = f"[{format}]" dims = f"{nrows}R x {ncols}C" ncards = len(self._header) return (self.name, self.ver, class_name, ncards, dims, format) def _update_column_removed(self, columns, idx): super()._update_column_removed(columns, idx) # Fix the header to reflect the column removal self._clear_table_keywords(index=idx) def _update_column_attribute_changed( self, column, col_idx, attr, old_value, new_value ): """ Update the header when one of the column objects is updated. """ # base_keyword is the keyword without the index such as TDIM # while keyword is like TDIM1 base_keyword = ATTRIBUTE_TO_KEYWORD[attr] keyword = base_keyword + str(col_idx + 1) if keyword in self._header: if new_value is None: # If the new value is None, i.e. None was assigned to the # column attribute, then treat this as equivalent to deleting # that attribute del self._header[keyword] else: self._header[keyword] = new_value else: keyword_idx = KEYWORD_NAMES.index(base_keyword) # Determine the appropriate keyword to insert this one before/after # if it did not already exist in the header for before_keyword in reversed(KEYWORD_NAMES[:keyword_idx]): before_keyword += str(col_idx + 1) if before_keyword in self._header: self._header.insert( before_keyword, (keyword, new_value), after=True ) break else: for after_keyword in KEYWORD_NAMES[keyword_idx + 1 :]: after_keyword += str(col_idx + 1) if after_keyword in self._header: self._header.insert(after_keyword, (keyword, new_value)) break else: # Just append self._header[keyword] = new_value def _clear_table_keywords(self, index=None): """ Wipe out any existing table definition keywords from the header. If specified, only clear keywords for the given table index (shifting up keywords for any other columns). The index is zero-based. Otherwise keywords for all columns. """ # First collect all the table structure related keyword in the header # into a single list so we can then sort them by index, which will be # useful later for updating the header in a sensible order (since the # header *might* not already be written in a reasonable order) table_keywords = [] for idx, keyword in enumerate(self._header.keys()): match = TDEF_RE.match(keyword) try: base_keyword = match.group("label") except Exception: continue # skip if there is no match if base_keyword in KEYWORD_TO_ATTRIBUTE: # TEMP: For Astropy 3.0 we don't clear away the following keywords # as we are first raising a deprecation warning that these will be # dropped automatically if they were specified in the header. We # can remove this once we are happy to break backward-compatibility if base_keyword in { "TCTYP", "TCUNI", "TCRPX", "TCRVL", "TCDLT", "TRPOS", }: continue num = int(match.group("num")) - 1 # convert to zero-base table_keywords.append((idx, match.group(0), base_keyword, num)) # First delete rev_sorted_idx_0 = sorted( table_keywords, key=operator.itemgetter(0), reverse=True ) for idx, keyword, _, num in rev_sorted_idx_0: if index is None or index == num: del self._header[idx] # Now shift up remaining column keywords if only one column was cleared if index is not None: sorted_idx_3 = sorted(table_keywords, key=operator.itemgetter(3)) for _, keyword, base_keyword, num in sorted_idx_3: if num <= index: continue old_card = self._header.cards[keyword] new_card = (base_keyword + str(num), old_card.value, old_card.comment) self._header.insert(keyword, new_card) del self._header[keyword] # Also decrement TFIELDS if "TFIELDS" in self._header: self._header["TFIELDS"] -= 1 def _populate_table_keywords(self): """Populate the new table definition keywords from the header.""" for idx, column in enumerate(self.columns): for keyword, attr in KEYWORD_TO_ATTRIBUTE.items(): val = getattr(column, attr) if val is not None: keyword = keyword + str(idx + 1) self._header[keyword] = val class TableHDU(_TableBaseHDU): """ FITS ASCII table extension HDU class. Parameters ---------- data : array or `FITS_rec` Data to be used. header : `Header` Header to be used. name : str Name to be populated in ``EXTNAME`` keyword. ver : int > 0 or None, optional The ver of the HDU, will be the value of the keyword ``EXTVER``. If not given or None, it defaults to the value of the ``EXTVER`` card of the ``header`` or 1. (default: None) character_as_bytes : bool Whether to return bytes for string columns. By default this is `False` and (unicode) strings are returned, but this does not respect memory mapping and loads the whole column in memory when accessed. """ _extension = "TABLE" _ext_comment = "ASCII table extension" _padding_byte = " " _columns_type = _AsciiColDefs __format_RE = re.compile(r"(?P<code>[ADEFIJ])(?P<width>\d+)(?:\.(?P<prec>\d+))?") def __init__( self, data=None, header=None, name=None, ver=None, character_as_bytes=False ): super().__init__( data, header, name=name, ver=ver, character_as_bytes=character_as_bytes ) @classmethod def match_header(cls, header): card = header.cards[0] xtension = card.value if isinstance(xtension, str): xtension = xtension.rstrip() return card.keyword == "XTENSION" and xtension == cls._extension def _get_tbdata(self): columns = self.columns names = [n for idx, n in enumerate(columns.names)] # determine if there are duplicate field names and if there # are throw an exception dup = np.rec.find_duplicate(names) if dup: raise ValueError(f"Duplicate field names: {dup}") # TODO: Determine if this extra logic is necessary--I feel like the # _AsciiColDefs class should be responsible for telling the table what # its dtype should be... itemsize = columns.spans[-1] + columns.starts[-1] - 1 dtype = {} for idx in range(len(columns)): data_type = "S" + str(columns.spans[idx]) if idx == len(columns) - 1: # The last column is padded out to the value of NAXIS1 if self._header["NAXIS1"] > itemsize: data_type = "S" + str( columns.spans[idx] + self._header["NAXIS1"] - itemsize ) dtype[columns.names[idx]] = (data_type, columns.starts[idx] - 1) raw_data = self._get_raw_data(self._nrows, dtype, self._data_offset) data = raw_data.view(np.rec.recarray) self._init_tbdata(data) return data.view(self._data_type) def _calculate_datasum(self): """ Calculate the value for the ``DATASUM`` card in the HDU. """ if self._has_data: # We have the data to be used. # We need to pad the data to a block length before calculating # the datasum. bytes_array = self.data.view(type=np.ndarray, dtype=np.ubyte) padding = np.frombuffer(_pad_length(self.size) * b" ", dtype=np.ubyte) d = np.append(bytes_array, padding) cs = self._compute_checksum(d) return cs else: # This is the case where the data has not been read from the file # yet. We can handle that in a generic manner so we do it in the # base class. The other possibility is that there is no data at # all. This can also be handled in a generic manner. return super()._calculate_datasum() def _verify(self, option="warn"): """ `TableHDU` verify method. """ errs = super()._verify(option=option) self.req_cards("PCOUNT", None, lambda v: (v == 0), 0, option, errs) tfields = self._header["TFIELDS"] for idx in range(tfields): self.req_cards("TBCOL" + str(idx + 1), None, _is_int, None, option, errs) return errs class BinTableHDU(_TableBaseHDU): """ Binary table HDU class. Parameters ---------- data : array, `FITS_rec`, or `~astropy.table.Table` Data to be used. header : `Header` Header to be used. name : str Name to be populated in ``EXTNAME`` keyword. uint : bool, optional Set to `True` if the table contains unsigned integer columns. ver : int > 0 or None, optional The ver of the HDU, will be the value of the keyword ``EXTVER``. If not given or None, it defaults to the value of the ``EXTVER`` card of the ``header`` or 1. (default: None) character_as_bytes : bool Whether to return bytes for string columns. By default this is `False` and (unicode) strings are returned, but this does not respect memory mapping and loads the whole column in memory when accessed. """ _extension = "BINTABLE" _ext_comment = "binary table extension" def __init__( self, data=None, header=None, name=None, uint=False, ver=None, character_as_bytes=False, ): from astropy.table import Table if isinstance(data, Table): from astropy.io.fits.convenience import table_to_hdu hdu = table_to_hdu(data) if header is not None: hdu.header.update(header) data = hdu.data header = hdu.header super().__init__( data, header, name=name, uint=uint, ver=ver, character_as_bytes=character_as_bytes, ) @classmethod def match_header(cls, header): card = header.cards[0] xtension = card.value if isinstance(xtension, str): xtension = xtension.rstrip() return card.keyword == "XTENSION" and xtension in (cls._extension, "A3DTABLE") def _calculate_datasum_with_heap(self): """ Calculate the value for the ``DATASUM`` card given the input data """ with _binary_table_byte_swap(self.data) as data: dout = data.view(type=np.ndarray, dtype=np.ubyte) csum = self._compute_checksum(dout) # Now add in the heap data to the checksum (we can skip any gap # between the table and the heap since it's all zeros and doesn't # contribute to the checksum if data._get_raw_data() is None: # This block is still needed because # test_variable_length_table_data leads to ._get_raw_data # returning None which means _get_heap_data doesn't work. # Which happens when the data is loaded in memory rather than # being unloaded on disk for idx in range(data._nfields): if isinstance(data.columns._recformats[idx], _FormatP): for coldata in data.field(idx): # coldata should already be byteswapped from the call # to _binary_table_byte_swap if not len(coldata): continue csum = self._compute_checksum(coldata, csum) else: csum = self._compute_checksum(data._get_heap_data(), csum) return csum def _calculate_datasum(self): """ Calculate the value for the ``DATASUM`` card in the HDU. """ if self._has_data: # This method calculates the datasum while incorporating any # heap data, which is obviously not handled from the base # _calculate_datasum return self._calculate_datasum_with_heap() else: # This is the case where the data has not been read from the file # yet. We can handle that in a generic manner so we do it in the # base class. The other possibility is that there is no data at # all. This can also be handled in a generic manner. return super()._calculate_datasum() def _writedata_internal(self, fileobj): size = 0 if self.data is None: return size with _binary_table_byte_swap(self.data) as data: if _has_unicode_fields(data): # If the raw data was a user-supplied recarray, we can't write # unicode columns directly to the file, so we have to switch # to a slower row-by-row write self._writedata_by_row(fileobj) else: fileobj.writearray(data) # write out the heap of variable length array columns this has # to be done after the "regular" data is written (above) # to avoid a bug in the lustre filesystem client, don't # write 0-byte objects if data._gap > 0: fileobj.write((data._gap * "\0").encode("ascii")) nbytes = data._gap if not self._manages_own_heap: # Write the heap data one column at a time, in the order # that the data pointers appear in the column (regardless # if that data pointer has a different, previous heap # offset listed) for idx in range(data._nfields): if not isinstance(data.columns._recformats[idx], _FormatP): continue field = self.data.field(idx) for row in field: if len(row) > 0: nbytes += row.nbytes fileobj.writearray(row) else: heap_data = data._get_heap_data() if len(heap_data) > 0: nbytes += len(heap_data) fileobj.writearray(heap_data) data._heapsize = nbytes - data._gap size += nbytes size += self.data.size * self.data._raw_itemsize return size def _writedata_by_row(self, fileobj): fields = [self.data.field(idx) for idx in range(len(self.data.columns))] # Creating Record objects is expensive (as in # `for row in self.data:` so instead we just iterate over the row # indices and get one field at a time: for idx in range(len(self.data)): for field in fields: item = field[idx] field_width = None if field.dtype.kind == "U": # Read the field *width* by reading past the field kind. i = field.dtype.str.index(field.dtype.kind) field_width = int(field.dtype.str[i + 1 :]) item = np.char.encode(item, "ascii") fileobj.writearray(item) if field_width is not None: j = item.dtype.str.index(item.dtype.kind) item_length = int(item.dtype.str[j + 1 :]) # Fix padding problem (see #5296). padding = "\x00" * (field_width - item_length) fileobj.write(padding.encode("ascii")) _tdump_file_format = textwrap.dedent( """ - **datafile:** Each line of the data file represents one row of table data. The data is output one column at a time in column order. If a column contains an array, each element of the column array in the current row is output before moving on to the next column. Each row ends with a new line. Integer data is output right-justified in a 21-character field followed by a blank. Floating point data is output right justified using 'g' format in a 21-character field with 15 digits of precision, followed by a blank. String data that does not contain whitespace is output left-justified in a field whose width matches the width specified in the ``TFORM`` header parameter for the column, followed by a blank. When the string data contains whitespace characters, the string is enclosed in quotation marks (``""``). For the last data element in a row, the trailing blank in the field is replaced by a new line character. For column data containing variable length arrays ('P' format), the array data is preceded by the string ``'VLA_Length= '`` and the integer length of the array for that row, left-justified in a 21-character field, followed by a blank. .. note:: This format does *not* support variable length arrays using the ('Q' format) due to difficult to overcome ambiguities. What this means is that this file format cannot support VLA columns in tables stored in files that are over 2 GB in size. For column data representing a bit field ('X' format), each bit value in the field is output right-justified in a 21-character field as 1 (for true) or 0 (for false). - **cdfile:** Each line of the column definitions file provides the definitions for one column in the table. The line is broken up into 8, sixteen-character fields. The first field provides the column name (``TTYPEn``). The second field provides the column format (``TFORMn``). The third field provides the display format (``TDISPn``). The fourth field provides the physical units (``TUNITn``). The fifth field provides the dimensions for a multidimensional array (``TDIMn``). The sixth field provides the value that signifies an undefined value (``TNULLn``). The seventh field provides the scale factor (``TSCALn``). The eighth field provides the offset value (``TZEROn``). A field value of ``""`` is used to represent the case where no value is provided. - **hfile:** Each line of the header parameters file provides the definition of a single HDU header card as represented by the card image. """ ) def dump(self, datafile=None, cdfile=None, hfile=None, overwrite=False): """ Dump the table HDU to a file in ASCII format. The table may be dumped in three separate files, one containing column definitions, one containing header parameters, and one for table data. Parameters ---------- datafile : path-like or file-like, optional Output data file. The default is the root name of the fits file associated with this HDU appended with the extension ``.txt``. cdfile : path-like or file-like, optional Output column definitions file. The default is `None`, no column definitions output is produced. hfile : path-like or file-like, optional Output header parameters file. The default is `None`, no header parameters output is produced. overwrite : bool, optional If ``True``, overwrite the output file if it exists. Raises an ``OSError`` if ``False`` and the output file exists. Default is ``False``. Notes ----- The primary use for the `dump` method is to allow viewing and editing the table data and parameters in a standard text editor. The `load` method can be used to create a new table from the three plain text (ASCII) files. """ if isinstance(datafile, path_like): datafile = os.path.expanduser(datafile) if isinstance(cdfile, path_like): cdfile = os.path.expanduser(cdfile) if isinstance(hfile, path_like): hfile = os.path.expanduser(hfile) # check if the output files already exist exist = [] files = [datafile, cdfile, hfile] for f in files: if isinstance(f, path_like): if os.path.exists(f) and os.path.getsize(f) != 0: if overwrite: os.remove(f) else: exist.append(f) if exist: raise OSError( " ".join([f"File '{f}' already exists." for f in exist]) + " If you mean to " "replace the file(s) " "then use the argument " "'overwrite=True'." ) # Process the data self._dump_data(datafile) # Process the column definitions if cdfile: self._dump_coldefs(cdfile) # Process the header parameters if hfile: self._header.tofile(hfile, sep="\n", endcard=False, padding=False) if isinstance(dump.__doc__, str): dump.__doc__ += _tdump_file_format.replace("\n", "\n ") def load(cls, datafile, cdfile=None, hfile=None, replace=False, header=None): """ Create a table from the input ASCII files. The input is from up to three separate files, one containing column definitions, one containing header parameters, and one containing column data. The column definition and header parameters files are not required. When absent the column definitions and/or header parameters are taken from the header object given in the header argument; otherwise sensible defaults are inferred (though this mode is not recommended). Parameters ---------- datafile : path-like or file-like Input data file containing the table data in ASCII format. cdfile : path-like or file-like, optional Input column definition file containing the names, formats, display formats, physical units, multidimensional array dimensions, undefined values, scale factors, and offsets associated with the columns in the table. If `None`, the column definitions are taken from the current values in this object. hfile : path-like or file-like, optional Input parameter definition file containing the header parameter definitions to be associated with the table. If `None`, the header parameter definitions are taken from the current values in this objects header. replace : bool, optional When `True`, indicates that the entire header should be replaced with the contents of the ASCII file instead of just updating the current header. header : `~astropy.io.fits.Header`, optional When the cdfile and hfile are missing, use this Header object in the creation of the new table and HDU. Otherwise this Header supersedes the keywords from hfile, which is only used to update values not present in this Header, unless ``replace=True`` in which this Header's values are completely replaced with the values from hfile. Notes ----- The primary use for the `load` method is to allow the input of ASCII data that was edited in a standard text editor of the table data and parameters. The `dump` method can be used to create the initial ASCII files. """ # Process the parameter file if header is None: header = Header() if hfile: if replace: header = Header.fromtextfile(hfile) else: header.extend( Header.fromtextfile(hfile), update=True, update_first=True ) coldefs = None # Process the column definitions file if cdfile: coldefs = cls._load_coldefs(cdfile) # Process the data file data = cls._load_data(datafile, coldefs) if coldefs is None: coldefs = ColDefs(data) # Create a new HDU using the supplied header and data hdu = cls(data=data, header=header) hdu.columns = coldefs return hdu if isinstance(load.__doc__, str): load.__doc__ += _tdump_file_format.replace("\n", "\n ") load = classmethod(load) # Have to create a classmethod from this here instead of as a decorator; # otherwise we can't update __doc__ def _dump_data(self, fileobj): """ Write the table data in the ASCII format read by BinTableHDU.load() to fileobj. """ if not fileobj and self._file: root = os.path.splitext(self._file.name)[0] fileobj = root + ".txt" close_file = False if isinstance(fileobj, str): fileobj = open(fileobj, "w") close_file = True linewriter = csv.writer(fileobj, dialect=FITSTableDumpDialect) # Process each row of the table and output one row at a time def format_value(val, format): if format[0] == "S": itemsize = int(format[1:]) return "{:{size}}".format(val, size=itemsize) elif format in np.typecodes["AllInteger"]: # output integer return f"{val:21d}" elif format in np.typecodes["Complex"]: return f"{val.real:21.15g}+{val.imag:.15g}j" elif format in np.typecodes["Float"]: # output floating point return f"{val:#21.15g}" for row in self.data: line = [] # the line for this row of the table # Process each column of the row. for column in self.columns: # format of data in a variable length array # where None means it is not a VLA: vla_format = None format = _convert_format(column.format) if isinstance(format, _FormatP): # P format means this is a variable length array so output # the length of the array for this row and set the format # for the VLA data line.append("VLA_Length=") line.append(f"{len(row[column.name]):21d}") _, dtype, option = _parse_tformat(column.format) vla_format = FITS2NUMPY[option[0]][0] if vla_format: # Output the data for each element in the array for val in row[column.name].flat: line.append(format_value(val, vla_format)) else: # The column data is a single element dtype = self.data.dtype.fields[column.name][0] array_format = dtype.char if array_format == "V": array_format = dtype.base.char if array_format == "S": array_format += str(dtype.itemsize) if dtype.char == "V": for value in row[column.name].flat: line.append(format_value(value, array_format)) else: line.append(format_value(row[column.name], array_format)) linewriter.writerow(line) if close_file: fileobj.close() def _dump_coldefs(self, fileobj): """ Write the column definition parameters in the ASCII format read by BinTableHDU.load() to fileobj. """ close_file = False if isinstance(fileobj, str): fileobj = open(fileobj, "w") close_file = True # Process each column of the table and output the result to the # file one at a time for column in self.columns: line = [column.name, column.format] attrs = ["disp", "unit", "dim", "null", "bscale", "bzero"] line += [ "{!s:16s}".format(value if value else '""') for value in (getattr(column, attr) for attr in attrs) ] fileobj.write(" ".join(line)) fileobj.write("\n") if close_file: fileobj.close() @classmethod def _load_data(cls, fileobj, coldefs=None): """ Read the table data from the ASCII file output by BinTableHDU.dump(). """ close_file = False if isinstance(fileobj, path_like): fileobj = os.path.expanduser(fileobj) fileobj = open(fileobj) close_file = True initialpos = fileobj.tell() # We'll be returning here later linereader = csv.reader(fileobj, dialect=FITSTableDumpDialect) # First we need to do some preprocessing on the file to find out how # much memory we'll need to reserve for the table. This is necessary # even if we already have the coldefs in order to determine how many # rows to reserve memory for vla_lengths = [] recformats = [] names = [] nrows = 0 if coldefs is not None: recformats = coldefs._recformats names = coldefs.names def update_recformats(value, idx): fitsformat = _scalar_to_format(value) recformat = _convert_format(fitsformat) if idx >= len(recformats): recformats.append(recformat) else: if _cmp_recformats(recformats[idx], recformat) < 0: recformats[idx] = recformat # TODO: The handling of VLAs could probably be simplified a bit for row in linereader: nrows += 1 if coldefs is not None: continue col = 0 idx = 0 while idx < len(row): if row[idx] == "VLA_Length=": if col < len(vla_lengths): vla_length = vla_lengths[col] else: vla_length = int(row[idx + 1]) vla_lengths.append(vla_length) idx += 2 while vla_length: update_recformats(row[idx], col) vla_length -= 1 idx += 1 col += 1 else: if col >= len(vla_lengths): vla_lengths.append(None) update_recformats(row[idx], col) col += 1 idx += 1 # Update the recformats for any VLAs for idx, length in enumerate(vla_lengths): if length is not None: recformats[idx] = str(length) + recformats[idx] dtype = np.rec.format_parser(recformats, names, None).dtype # TODO: In the future maybe enable loading a bit at a time so that we # can convert from this format to an actual FITS file on disk without # needing enough physical memory to hold the entire thing at once hdu = BinTableHDU.from_columns( np.recarray(shape=1, dtype=dtype), nrows=nrows, fill=True ) # TODO: It seems to me a lot of this could/should be handled from # within the FITS_rec class rather than here. data = hdu.data for idx, length in enumerate(vla_lengths): if length is not None: arr = data.columns._arrays[idx] dt = recformats[idx][len(str(length)) :] # NOTE: FormatQ not supported here; it's hard to determine # whether or not it will be necessary to use a wider descriptor # type. The function documentation will have to serve as a # warning that this is not supported. recformats[idx] = _FormatP(dt, max=length) data.columns._recformats[idx] = recformats[idx] name = data.columns.names[idx] data._cache_field(name, _makep(arr, arr, recformats[idx])) def format_value(col, val): # Special formatting for a couple particular data types if recformats[col] == FITS2NUMPY["L"]: return bool(int(val)) elif recformats[col] == FITS2NUMPY["M"]: # For some reason, in arrays/fields where numpy expects a # complex it's not happy to take a string representation # (though it's happy to do that in other contexts), so we have # to convert the string representation for it: return complex(val) else: return val # Jump back to the start of the data and create a new line reader fileobj.seek(initialpos) linereader = csv.reader(fileobj, dialect=FITSTableDumpDialect) for row, line in enumerate(linereader): col = 0 idx = 0 while idx < len(line): if line[idx] == "VLA_Length=": vla_len = vla_lengths[col] idx += 2 slice_ = slice(idx, idx + vla_len) data[row][col][:] = line[idx : idx + vla_len] idx += vla_len elif dtype[col].shape: # This is an array column array_size = int(np.multiply.reduce(dtype[col].shape)) slice_ = slice(idx, idx + array_size) idx += array_size else: slice_ = None if slice_ is None: # This is a scalar row element data[row][col] = format_value(col, line[idx]) idx += 1 else: data[row][col].flat[:] = [ format_value(col, val) for val in line[slice_] ] col += 1 if close_file: fileobj.close() return data @classmethod def _load_coldefs(cls, fileobj): """ Read the table column definitions from the ASCII file output by BinTableHDU.dump(). """ close_file = False if isinstance(fileobj, path_like): fileobj = os.path.expanduser(fileobj) fileobj = open(fileobj) close_file = True columns = [] for line in fileobj: words = line[:-1].split() kwargs = {} for key in ["name", "format", "disp", "unit", "dim"]: kwargs[key] = words.pop(0).replace('""', "") for key in ["null", "bscale", "bzero"]: word = words.pop(0).replace('""', "") if word: word = _str_to_num(word) kwargs[key] = word columns.append(Column(**kwargs)) if close_file: fileobj.close() return ColDefs(columns) @contextlib.contextmanager def _binary_table_byte_swap(data): """ Ensures that all the data of a binary FITS table (represented as a FITS_rec object) is in a big-endian byte order. Columns are swapped in-place one at a time, and then returned to their previous byte order when this context manager exits. Because a new dtype is needed to represent the byte-swapped columns, the new dtype is temporarily applied as well. """ orig_dtype = data.dtype names = [] formats = [] offsets = [] to_swap = [] if sys.byteorder == "little": swap_types = ("<", "=") else: swap_types = ("<",) for idx, name in enumerate(orig_dtype.names): field = _get_recarray_field(data, idx) field_dtype, field_offset = orig_dtype.fields[name] names.append(name) formats.append(field_dtype) offsets.append(field_offset) if isinstance(field, chararray.chararray): continue # only swap unswapped # must use field_dtype.base here since for multi-element dtypes, # the .str with be '|V<N>' where <N> is the total bytes per element if field.itemsize > 1 and field_dtype.base.str[0] in swap_types: to_swap.append(field) # Override the dtype for this field in the new record dtype with # the byteswapped version formats[-1] = field_dtype.newbyteorder() # deal with var length table recformat = data.columns._recformats[idx] if isinstance(recformat, _FormatP): coldata = data.field(idx) for c in coldata: if ( not isinstance(c, chararray.chararray) and c.itemsize > 1 and c.dtype.str[0] in swap_types ): to_swap.append(c) for arr in reversed(to_swap): arr.byteswap(True) data.dtype = np.dtype({"names": names, "formats": formats, "offsets": offsets}) yield data for arr in to_swap: arr.byteswap(True) data.dtype = orig_dtype
60dc63e7614edd03c9b9fbb046a73c51deff51a6c79c70ce2dab50c0943034d4
# Licensed under a 3-clause BSD style license - see PYFITS.rst from .base import BITPIX2DTYPE, DELAYED, DTYPE2BITPIX, register_hdu, unregister_hdu from .compressed import CompImageHDU from .groups import Group, GroupData, GroupsHDU from .hdulist import HDUList from .image import ImageHDU, PrimaryHDU from .nonstandard import FitsHDU from .streaming import StreamingHDU from .table import BinTableHDU, TableHDU __all__ = [ "HDUList", "PrimaryHDU", "ImageHDU", "TableHDU", "BinTableHDU", "GroupsHDU", "GroupData", "Group", "CompImageHDU", "FitsHDU", "StreamingHDU", "register_hdu", "unregister_hdu", "DELAYED", "BITPIX2DTYPE", "DTYPE2BITPIX", ]
112b1b0b88e3f121c1d2c3c6b05e66d60d6bb46b25440c4cf786847b31caf953
# Licensed under a 3-clause BSD style license - see PYFITS.rst import gzip import io from astropy.io.fits.file import _File from astropy.io.fits.header import Header, _pad_length from astropy.io.fits.util import fileobj_name from astropy.utils import lazyproperty from .base import NonstandardExtHDU from .hdulist import HDUList class FitsHDU(NonstandardExtHDU): """ A non-standard extension HDU for encapsulating entire FITS files within a single HDU of a container FITS file. These HDUs have an extension (that is an XTENSION keyword) of FITS. The FITS file contained in the HDU's data can be accessed by the `hdulist` attribute which returns the contained FITS file as an `HDUList` object. """ _extension = "FITS" @lazyproperty def hdulist(self): self._file.seek(self._data_offset) fileobj = io.BytesIO() # Read the data into a BytesIO--reading directly from the file # won't work (at least for gzipped files) due to problems deep # within the gzip module that make it difficult to read gzip files # embedded in another file fileobj.write(self._file.read(self.size)) fileobj.seek(0) if self._header["COMPRESS"]: fileobj = gzip.GzipFile(fileobj=fileobj) return HDUList.fromfile(fileobj, mode="readonly") @classmethod def fromfile(cls, filename, compress=False): """ Like `FitsHDU.fromhdulist()`, but creates a FitsHDU from a file on disk. Parameters ---------- filename : str The path to the file to read into a FitsHDU compress : bool, optional Gzip compress the FITS file """ with HDUList.fromfile(filename) as hdulist: return cls.fromhdulist(hdulist, compress=compress) @classmethod def fromhdulist(cls, hdulist, compress=False): """ Creates a new FitsHDU from a given HDUList object. Parameters ---------- hdulist : HDUList A valid Headerlet object. compress : bool, optional Gzip compress the FITS file """ fileobj = bs = io.BytesIO() if compress: if hasattr(hdulist, "_file"): name = fileobj_name(hdulist._file) else: name = None fileobj = gzip.GzipFile(name, mode="wb", fileobj=bs) hdulist.writeto(fileobj) if compress: fileobj.close() # A proper HDUList should still be padded out to a multiple of 2880 # technically speaking padding = (_pad_length(bs.tell()) * cls._padding_byte).encode("ascii") bs.write(padding) bs.seek(0) cards = [ ("XTENSION", cls._extension, "FITS extension"), ("BITPIX", 8, "array data type"), ("NAXIS", 1, "number of array dimensions"), ("NAXIS1", len(bs.getvalue()), "Axis length"), ("PCOUNT", 0, "number of parameters"), ("GCOUNT", 1, "number of groups"), ] # Add the XINDn keywords proposed by Perry, though nothing is done with # these at the moment if len(hdulist) > 1: for idx, hdu in enumerate(hdulist[1:]): cards.append( ( "XIND" + str(idx + 1), hdu._header_offset, f"byte offset of extension {idx + 1}", ) ) cards.append(("COMPRESS", compress, "Uses gzip compression")) header = Header(cards) return cls._readfrom_internal(_File(bs), header=header) @classmethod def match_header(cls, header): card = header.cards[0] if card.keyword != "XTENSION": return False xtension = card.value if isinstance(xtension, str): xtension = xtension.rstrip() return xtension == cls._extension # TODO: Add header verification def _summary(self): # TODO: Perhaps make this more descriptive... return (self.name, self.ver, self.__class__.__name__, len(self._header))
9aa3513fddf4e0063593850b045ac9c5c750d8e6cd42786fa40de1b0859dfbad
# Licensed under a 3-clause BSD style license - see PYFITS.rst import mmap import sys import warnings import numpy as np from astropy.io.fits.header import Header from astropy.io.fits.util import ( _is_dask_array, _is_int, _is_pseudo_integer, _pseudo_zero, ) from astropy.io.fits.verify import VerifyWarning from astropy.utils import isiterable, lazyproperty from .base import BITPIX2DTYPE, DELAYED, DTYPE2BITPIX, ExtensionHDU, _ValidHDU __all__ = ["Section", "PrimaryHDU", "ImageHDU"] class _ImageBaseHDU(_ValidHDU): """FITS image HDU base class. Attributes ---------- header image header data image data """ standard_keyword_comments = { "SIMPLE": "conforms to FITS standard", "XTENSION": "Image extension", "BITPIX": "array data type", "NAXIS": "number of array dimensions", "GROUPS": "has groups", "PCOUNT": "number of parameters", "GCOUNT": "number of groups", } def __init__( self, data=None, header=None, do_not_scale_image_data=False, uint=True, scale_back=False, ignore_blank=False, **kwargs, ): from .groups import GroupsHDU super().__init__(data=data, header=header) if data is DELAYED: # Presumably if data is DELAYED then this HDU is coming from an # open file, and was not created in memory if header is None: # this should never happen raise ValueError("No header to setup HDU.") else: # TODO: Some of this card manipulation should go into the # PrimaryHDU and GroupsHDU subclasses # construct a list of cards of minimal header if isinstance(self, ExtensionHDU): c0 = ("XTENSION", "IMAGE", self.standard_keyword_comments["XTENSION"]) else: c0 = ("SIMPLE", True, self.standard_keyword_comments["SIMPLE"]) cards = [ c0, ("BITPIX", 8, self.standard_keyword_comments["BITPIX"]), ("NAXIS", 0, self.standard_keyword_comments["NAXIS"]), ] if isinstance(self, GroupsHDU): cards.append(("GROUPS", True, self.standard_keyword_comments["GROUPS"])) if isinstance(self, (ExtensionHDU, GroupsHDU)): cards.append(("PCOUNT", 0, self.standard_keyword_comments["PCOUNT"])) cards.append(("GCOUNT", 1, self.standard_keyword_comments["GCOUNT"])) if header is not None: orig = header.copy() header = Header(cards) header.extend(orig, strip=True, update=True, end=True) else: header = Header(cards) self._header = header self._do_not_scale_image_data = do_not_scale_image_data self._uint = uint self._scale_back = scale_back # Keep track of whether BZERO/BSCALE were set from the header so that # values for self._orig_bzero and self._orig_bscale can be set # properly, if necessary, once the data has been set. bzero_in_header = "BZERO" in self._header bscale_in_header = "BSCALE" in self._header self._bzero = self._header.get("BZERO", 0) self._bscale = self._header.get("BSCALE", 1) # Save off other important values from the header needed to interpret # the image data self._axes = [ self._header.get("NAXIS" + str(axis + 1), 0) for axis in range(self._header.get("NAXIS", 0)) ] # Not supplying a default for BITPIX makes sense because BITPIX # is either in the header or should be determined from the dtype of # the data (which occurs when the data is set). self._bitpix = self._header.get("BITPIX") self._gcount = self._header.get("GCOUNT", 1) self._pcount = self._header.get("PCOUNT", 0) self._blank = None if ignore_blank else self._header.get("BLANK") self._verify_blank() self._orig_bitpix = self._bitpix self._orig_blank = self._header.get("BLANK") # These get set again below, but need to be set to sensible defaults # here. self._orig_bzero = self._bzero self._orig_bscale = self._bscale # Set the name attribute if it was provided (if this is an ImageHDU # this will result in setting the EXTNAME keyword of the header as # well) if "name" in kwargs and kwargs["name"]: self.name = kwargs["name"] if "ver" in kwargs and kwargs["ver"]: self.ver = kwargs["ver"] # Set to True if the data or header is replaced, indicating that # update_header should be called self._modified = False if data is DELAYED: if not do_not_scale_image_data and (self._bscale != 1 or self._bzero != 0): # This indicates that when the data is accessed or written out # to a new file it will need to be rescaled self._data_needs_rescale = True return else: # Setting data will update the header and set _bitpix, _bzero, # and _bscale to the appropriate BITPIX for the data, and always # sets _bzero=0 and _bscale=1. self.data = data # Check again for BITPIX/BSCALE/BZERO in case they changed when the # data was assigned. This can happen, for example, if the input # data is an unsigned int numpy array. self._bitpix = self._header.get("BITPIX") # Do not provide default values for BZERO and BSCALE here because # the keywords will have been deleted in the header if appropriate # after scaling. We do not want to put them back in if they # should not be there. self._bzero = self._header.get("BZERO") self._bscale = self._header.get("BSCALE") # Handle case where there was no BZERO/BSCALE in the initial header # but there should be a BSCALE/BZERO now that the data has been set. if not bzero_in_header: self._orig_bzero = self._bzero if not bscale_in_header: self._orig_bscale = self._bscale @classmethod def match_header(cls, header): """ _ImageBaseHDU is sort of an abstract class for HDUs containing image data (as opposed to table data) and should never be used directly. """ raise NotImplementedError @property def is_image(self): return True @property def section(self): """ Access a section of the image array without loading the entire array into memory. The :class:`Section` object returned by this attribute is not meant to be used directly by itself. Rather, slices of the section return the appropriate slice of the data, and loads *only* that section into memory. Sections are useful for retrieving a small subset of data from a remote file that has been opened with the ``use_fsspec=True`` parameter. For example, you can use this feature to download a small cutout from a large FITS image hosted in the Amazon S3 cloud (see the :ref:`astropy:fits-cloud-files` section of the Astropy documentation for more details.) For local files, sections are mostly obsoleted by memmap support, but should still be used to deal with very large scaled images. Note that sections cannot currently be written to. Moreover, any in-memory updates to the image's ``.data`` property may not be reflected in the slices obtained via ``.section``. See the :ref:`astropy:data-sections` section of the documentation for more details. """ return Section(self) @property def shape(self): """ Shape of the image array--should be equivalent to ``self.data.shape``. """ # Determine from the values read from the header return tuple(reversed(self._axes)) @property def header(self): return self._header @header.setter def header(self, header): self._header = header self._modified = True self.update_header() @lazyproperty def data(self): """ Image/array data as a `~numpy.ndarray`. Please remember that the order of axes on an Numpy array are opposite of the order specified in the FITS file. For example for a 2D image the "rows" or y-axis are the first dimension, and the "columns" or x-axis are the second dimension. If the data is scaled using the BZERO and BSCALE parameters, this attribute returns the data scaled to its physical values unless the file was opened with ``do_not_scale_image_data=True``. """ if len(self._axes) < 1: return data = self._get_scaled_image_data(self._data_offset, self.shape) self._update_header_scale_info(data.dtype) return data @data.setter def data(self, data): if "data" in self.__dict__ and self.__dict__["data"] is not None: if self.__dict__["data"] is data: return else: self._data_replaced = True was_unsigned = _is_pseudo_integer(self.__dict__["data"].dtype) else: self._data_replaced = True was_unsigned = False if ( data is not None and not isinstance(data, np.ndarray) and not _is_dask_array(data) ): # Try to coerce the data into a numpy array--this will work, on # some level, for most objects try: data = np.array(data) except Exception: raise TypeError( f"data object {data!r} could not be coerced into an ndarray" ) if data.shape == (): raise TypeError( f"data object {data!r} should have at least one dimension" ) self.__dict__["data"] = data self._modified = True if self.data is None: self._axes = [] else: # Set new values of bitpix, bzero, and bscale now, but wait to # revise original values until header is updated. self._bitpix = DTYPE2BITPIX[data.dtype.name] self._bscale = 1 self._bzero = 0 self._blank = None self._axes = list(data.shape) self._axes.reverse() # Update the header, including adding BZERO/BSCALE if new data is # unsigned. Does not change the values of self._bitpix, # self._orig_bitpix, etc. self.update_header() if data is not None and was_unsigned: self._update_header_scale_info(data.dtype) # Keep _orig_bitpix as it was until header update is done, then # set it, to allow easier handling of the case of unsigned # integer data being converted to something else. Setting these here # is needed only for the case do_not_scale_image_data=True when # setting the data to unsigned int. # If necessary during initialization, i.e. if BSCALE and BZERO were # not in the header but the data was unsigned, the attributes below # will be update in __init__. self._orig_bitpix = self._bitpix self._orig_bscale = self._bscale self._orig_bzero = self._bzero # returning the data signals to lazyproperty that we've already handled # setting self.__dict__['data'] return data def update_header(self): """ Update the header keywords to agree with the data. """ if not ( self._modified or self._header._modified or (self._has_data and self.shape != self.data.shape) ): # Not likely that anything needs updating return old_naxis = self._header.get("NAXIS", 0) if "BITPIX" not in self._header: bitpix_comment = self.standard_keyword_comments["BITPIX"] else: bitpix_comment = self._header.comments["BITPIX"] # Update the BITPIX keyword and ensure it's in the correct # location in the header self._header.set("BITPIX", self._bitpix, bitpix_comment, after=0) # If the data's shape has changed (this may have happened without our # noticing either via a direct update to the data.shape attribute) we # need to update the internal self._axes if self._has_data and self.shape != self.data.shape: self._axes = list(self.data.shape) self._axes.reverse() # Update the NAXIS keyword and ensure it's in the correct location in # the header if "NAXIS" in self._header: naxis_comment = self._header.comments["NAXIS"] else: naxis_comment = self.standard_keyword_comments["NAXIS"] self._header.set("NAXIS", len(self._axes), naxis_comment, after="BITPIX") # TODO: This routine is repeated in several different classes--it # should probably be made available as a method on all standard HDU # types # add NAXISi if it does not exist for idx, axis in enumerate(self._axes): naxisn = "NAXIS" + str(idx + 1) if naxisn in self._header: self._header[naxisn] = axis else: if idx == 0: after = "NAXIS" else: after = "NAXIS" + str(idx) self._header.set(naxisn, axis, after=after) # delete extra NAXISi's for idx in range(len(self._axes) + 1, old_naxis + 1): try: del self._header["NAXIS" + str(idx)] except KeyError: pass if "BLANK" in self._header: self._blank = self._header["BLANK"] # Add BSCALE/BZERO to header if data is unsigned int. self._update_pseudo_int_scale_keywords() self._modified = False def _update_header_scale_info(self, dtype=None): """ Delete BSCALE/BZERO from header if necessary. """ # Note that _dtype_for_bitpix determines the dtype based on the # "original" values of bitpix, bscale, and bzero, stored in # self._orig_bitpix, etc. It contains the logic for determining which # special cases of BZERO/BSCALE, if any, are auto-detected as following # the FITS unsigned int convention. # Added original_was_unsigned with the intent of facilitating the # special case of do_not_scale_image_data=True and uint=True # eventually. # FIXME: unused, maybe it should be useful? # if self._dtype_for_bitpix() is not None: # original_was_unsigned = self._dtype_for_bitpix().kind == 'u' # else: # original_was_unsigned = False if self._do_not_scale_image_data or ( self._orig_bzero == 0 and self._orig_bscale == 1 ): return if dtype is None: dtype = self._dtype_for_bitpix() if ( dtype is not None and dtype.kind == "u" and (self._scale_back or self._scale_back is None) ): # Data is pseudo-unsigned integers, and the scale_back option # was not explicitly set to False, so preserve all the scale # factors return for keyword in ["BSCALE", "BZERO"]: try: del self._header[keyword] # Since _update_header_scale_info can, currently, be called # *after* _prewriteto(), replace these with blank cards so # the header size doesn't change self._header.append() except KeyError: pass if dtype is None: dtype = self._dtype_for_bitpix() if dtype is not None: self._header["BITPIX"] = DTYPE2BITPIX[dtype.name] self._bzero = 0 self._bscale = 1 self._bitpix = self._header["BITPIX"] self._blank = self._header.pop("BLANK", None) def scale(self, type=None, option="old", bscale=None, bzero=None): """ Scale image data by using ``BSCALE``/``BZERO``. Call to this method will scale `data` and update the keywords of ``BSCALE`` and ``BZERO`` in the HDU's header. This method should only be used right before writing to the output file, as the data will be scaled and is therefore not very usable after the call. Parameters ---------- type : str, optional destination data type, use a string representing a numpy dtype name, (e.g. ``'uint8'``, ``'int16'``, ``'float32'`` etc.). If is `None`, use the current data type. option : str, optional How to scale the data: ``"old"`` uses the original ``BSCALE`` and ``BZERO`` values from when the data was read/created (defaulting to 1 and 0 if they don't exist). For integer data only, ``"minmax"`` uses the minimum and maximum of the data to scale. User-specified ``bscale``/``bzero`` values always take precedence. bscale, bzero : int, optional User-specified ``BSCALE`` and ``BZERO`` values """ # Disable blank support for now self._scale_internal( type=type, option=option, bscale=bscale, bzero=bzero, blank=None ) def _scale_internal( self, type=None, option="old", bscale=None, bzero=None, blank=0 ): """ This is an internal implementation of the `scale` method, which also supports handling BLANK properly. TODO: This is only needed for fixing #3865 without introducing any public API changes. We should support BLANK better when rescaling data, and when that is added the need for this internal interface should go away. Note: the default of ``blank=0`` merely reflects the current behavior, and is not necessarily a deliberate choice (better would be to disallow conversion of floats to ints without specifying a BLANK if there are NaN/inf values). """ if self.data is None: return # Determine the destination (numpy) data type if type is None: type = BITPIX2DTYPE[self._bitpix] _type = getattr(np, type) # Determine how to scale the data # bscale and bzero takes priority if bscale is not None and bzero is not None: _scale = bscale _zero = bzero elif bscale is not None: _scale = bscale _zero = 0 elif bzero is not None: _scale = 1 _zero = bzero elif ( option == "old" and self._orig_bscale is not None and self._orig_bzero is not None ): _scale = self._orig_bscale _zero = self._orig_bzero elif option == "minmax" and not issubclass(_type, np.floating): if _is_dask_array(self.data): min = self.data.min().compute() max = self.data.max().compute() else: min = np.minimum.reduce(self.data.flat) max = np.maximum.reduce(self.data.flat) if _type == np.uint8: # uint8 case _zero = min _scale = (max - min) / (2.0**8 - 1) else: _zero = (max + min) / 2.0 # throw away -2^N nbytes = 8 * _type().itemsize _scale = (max - min) / (2.0**nbytes - 2) else: _scale = 1 _zero = 0 # Do the scaling if _zero != 0: if _is_dask_array(self.data): self.data = self.data - _zero else: # 0.9.6.3 to avoid out of range error for BZERO = +32768 # We have to explicitly cast _zero to prevent numpy from raising an # error when doing self.data -= zero, and we do this instead of # self.data = self.data - zero to avoid doubling memory usage. np.add(self.data, -_zero, out=self.data, casting="unsafe") self._header["BZERO"] = _zero else: try: del self._header["BZERO"] except KeyError: pass if _scale and _scale != 1: self.data = self.data / _scale self._header["BSCALE"] = _scale else: try: del self._header["BSCALE"] except KeyError: pass # Set blanks if blank is not None and issubclass(_type, np.integer): # TODO: Perhaps check that the requested BLANK value fits in the # integer type being scaled to? self.data[np.isnan(self.data)] = blank self._header["BLANK"] = blank if self.data.dtype.type != _type: self.data = np.array(np.around(self.data), dtype=_type) # Update the BITPIX Card to match the data self._bitpix = DTYPE2BITPIX[self.data.dtype.name] self._bzero = self._header.get("BZERO", 0) self._bscale = self._header.get("BSCALE", 1) self._blank = blank self._header["BITPIX"] = self._bitpix # Since the image has been manually scaled, the current # bitpix/bzero/bscale now serve as the 'original' scaling of the image, # as though the original image has been completely replaced self._orig_bitpix = self._bitpix self._orig_bzero = self._bzero self._orig_bscale = self._bscale self._orig_blank = self._blank def _verify(self, option="warn"): # update_header can fix some things that would otherwise cause # verification to fail, so do that now... self.update_header() self._verify_blank() return super()._verify(option) def _verify_blank(self): # Probably not the best place for this (it should probably happen # in _verify as well) but I want to be able to raise this warning # both when the HDU is created and when written if self._blank is None: return messages = [] # TODO: Once the FITSSchema framewhere is merged these warnings # should be handled by the schema if not _is_int(self._blank): messages.append( "Invalid value for 'BLANK' keyword in header: {!r} " "The 'BLANK' keyword must be an integer. It will be " "ignored in the meantime.".format(self._blank) ) self._blank = None if not self._bitpix > 0: messages.append( "Invalid 'BLANK' keyword in header. The 'BLANK' keyword " "is only applicable to integer data, and will be ignored " "in this HDU." ) self._blank = None for msg in messages: warnings.warn(msg, VerifyWarning) def _prewriteto(self, checksum=False, inplace=False): if self._scale_back: self._scale_internal( BITPIX2DTYPE[self._orig_bitpix], blank=self._orig_blank ) self.update_header() if not inplace and self._data_needs_rescale: # Go ahead and load the scaled image data and update the header # with the correct post-rescaling headers _ = self.data return super()._prewriteto(checksum, inplace) def _writedata_internal(self, fileobj): size = 0 if self.data is None: return size elif _is_dask_array(self.data): return self._writeinternal_dask(fileobj) else: # Based on the system type, determine the byteorders that # would need to be swapped to get to big-endian output if sys.byteorder == "little": swap_types = ("<", "=") else: swap_types = ("<",) # deal with unsigned integer 16, 32 and 64 data if _is_pseudo_integer(self.data.dtype): # Convert the unsigned array to signed output = np.array( self.data - _pseudo_zero(self.data.dtype), dtype=f">i{self.data.dtype.itemsize}", ) should_swap = False else: output = self.data byteorder = output.dtype.str[0] should_swap = byteorder in swap_types if should_swap: if output.flags.writeable: output.byteswap(True) try: fileobj.writearray(output) finally: output.byteswap(True) else: # For read-only arrays, there is no way around making # a byteswapped copy of the data. fileobj.writearray(output.byteswap(False)) else: fileobj.writearray(output) size += output.size * output.itemsize return size def _writeinternal_dask(self, fileobj): if sys.byteorder == "little": swap_types = ("<", "=") else: swap_types = ("<",) # deal with unsigned integer 16, 32 and 64 data if _is_pseudo_integer(self.data.dtype): raise NotImplementedError("This dtype isn't currently supported with dask.") else: output = self.data byteorder = output.dtype.str[0] should_swap = byteorder in swap_types if should_swap: from dask.utils import M # NOTE: the inplace flag to byteswap needs to be False otherwise the array is # byteswapped in place every time it is computed and this affects # the input dask array. output = output.map_blocks(M.byteswap, False).map_blocks( M.newbyteorder, "S" ) initial_position = fileobj.tell() n_bytes = output.nbytes # Extend the file n_bytes into the future fileobj.seek(initial_position + n_bytes - 1) fileobj.write(b"\0") fileobj.flush() if fileobj.fileobj_mode not in ("rb+", "wb+", "ab+"): # Use another file handle if the current one is not in # read/write mode fp = open(fileobj.name, mode="rb+") should_close = True else: fp = fileobj._file should_close = False try: outmmap = mmap.mmap( fp.fileno(), length=initial_position + n_bytes, access=mmap.ACCESS_WRITE ) outarr = np.ndarray( shape=output.shape, dtype=output.dtype, offset=initial_position, buffer=outmmap, ) output.store(outarr, lock=True, compute=True) finally: if should_close: fp.close() outmmap.close() # On Windows closing the memmap causes the file pointer to return to 0, so # we need to go back to the end of the data (since padding may be written # after) fileobj.seek(initial_position + n_bytes) return n_bytes def _dtype_for_bitpix(self): """ Determine the dtype that the data should be converted to depending on the BITPIX value in the header, and possibly on the BSCALE value as well. Returns None if there should not be any change. """ bitpix = self._orig_bitpix # Handle possible conversion to uints if enabled if self._uint and self._orig_bscale == 1: if bitpix == 8 and self._orig_bzero == -128: return np.dtype("int8") for bits, dtype in ( (16, np.dtype("uint16")), (32, np.dtype("uint32")), (64, np.dtype("uint64")), ): if bitpix == bits and self._orig_bzero == 1 << (bits - 1): return dtype if bitpix > 16: # scale integers to Float64 return np.dtype("float64") elif bitpix > 0: # scale integers to Float32 return np.dtype("float32") def _convert_pseudo_integer(self, data): """ Handle "pseudo-unsigned" integers, if the user requested it. Returns the converted data array if so; otherwise returns None. In this case case, we don't need to handle BLANK to convert it to NAN, since we can't do NaNs with integers, anyway, i.e. the user is responsible for managing blanks. """ dtype = self._dtype_for_bitpix() # bool(dtype) is always False--have to explicitly compare to None; this # caused a fair amount of hair loss if dtype is not None and dtype.kind == "u": # Convert the input raw data into an unsigned integer array and # then scale the data adjusting for the value of BZERO. Note that # we subtract the value of BZERO instead of adding because of the # way numpy converts the raw signed array into an unsigned array. bits = dtype.itemsize * 8 data = np.array(data, dtype=dtype) data -= np.uint64(1 << (bits - 1)) return data def _get_scaled_image_data(self, offset, shape): """ Internal function for reading image data from a file and apply scale factors to it. Normally this is used for the entire image, but it supports alternate offset/shape for Section support. """ code = BITPIX2DTYPE[self._orig_bitpix] raw_data = self._get_raw_data(shape, code, offset) raw_data.dtype = raw_data.dtype.newbyteorder(">") if self._do_not_scale_image_data or ( self._orig_bzero == 0 and self._orig_bscale == 1 and self._blank is None ): # No further conversion of the data is necessary return raw_data try: if self._file.strict_memmap: raise ValueError( "Cannot load a memory-mapped image: " "BZERO/BSCALE/BLANK header keywords present. " "Set memmap=False." ) except AttributeError: # strict_memmap not set pass data = None if not (self._orig_bzero == 0 and self._orig_bscale == 1): data = self._convert_pseudo_integer(raw_data) if data is None: # In these cases, we end up with floating-point arrays and have to # apply bscale and bzero. We may have to handle BLANK and convert # to NaN in the resulting floating-point arrays. # The BLANK keyword should only be applied for integer data (this # is checked in __init__ but it can't hurt to double check here) blanks = None if self._blank is not None and self._bitpix > 0: blanks = raw_data.flat == self._blank # The size of blanks in bytes is the number of elements in # raw_data.flat. However, if we use np.where instead we will # only use 8 bytes for each index where the condition is true. # So if the number of blank items is fewer than # len(raw_data.flat) / 8, using np.where will use less memory if blanks.sum() < len(blanks) / 8: blanks = np.where(blanks) new_dtype = self._dtype_for_bitpix() if new_dtype is not None: data = np.array(raw_data, dtype=new_dtype) else: # floating point cases if self._file is not None and self._file.memmap: data = raw_data.copy() elif not raw_data.flags.writeable: # create a writeable copy if needed data = raw_data.copy() # if not memmap, use the space already in memory else: data = raw_data del raw_data if self._orig_bscale != 1: np.multiply(data, self._orig_bscale, data) if self._orig_bzero != 0: data += self._orig_bzero if self._blank: data.flat[blanks] = np.nan return data def _summary(self): """ Summarize the HDU: name, dimensions, and formats. """ class_name = self.__class__.__name__ # if data is touched, use data info. if self._data_loaded: if self.data is None: format = "" else: format = self.data.dtype.name format = format[format.rfind(".") + 1 :] else: if self.shape and all(self.shape): # Only show the format if all the dimensions are non-zero # if data is not touched yet, use header info. format = BITPIX2DTYPE[self._bitpix] else: format = "" if ( format and not self._do_not_scale_image_data and (self._orig_bscale != 1 or self._orig_bzero != 0) ): new_dtype = self._dtype_for_bitpix() if new_dtype is not None: format += f" (rescales to {new_dtype.name})" # Display shape in FITS-order shape = tuple(reversed(self.shape)) return (self.name, self.ver, class_name, len(self._header), shape, format, "") def _calculate_datasum(self): """ Calculate the value for the ``DATASUM`` card in the HDU. """ if self._has_data: # We have the data to be used. d = self.data # First handle the special case where the data is unsigned integer # 16, 32 or 64 if _is_pseudo_integer(self.data.dtype): d = np.array( self.data - _pseudo_zero(self.data.dtype), dtype=f"i{self.data.dtype.itemsize}", ) # Check the byte order of the data. If it is little endian we # must swap it before calculating the datasum. if d.dtype.str[0] != ">": if d.flags.writeable: byteswapped = True d = d.byteswap(True) d.dtype = d.dtype.newbyteorder(">") else: # If the data is not writeable, we just make a byteswapped # copy and don't bother changing it back after d = d.byteswap(False) d.dtype = d.dtype.newbyteorder(">") byteswapped = False else: byteswapped = False cs = self._compute_checksum(d.flatten().view(np.uint8)) # If the data was byteswapped in this method then return it to # its original little-endian order. if byteswapped and not _is_pseudo_integer(self.data.dtype): d.byteswap(True) d.dtype = d.dtype.newbyteorder("<") return cs else: # This is the case where the data has not been read from the file # yet. We can handle that in a generic manner so we do it in the # base class. The other possibility is that there is no data at # all. This can also be handled in a generic manner. return super()._calculate_datasum() class Section: """ Class enabling subsets of ImageHDU data to be loaded lazily via slicing. Slices of this object load the corresponding section of an image array from the underlying FITS file, and applies any BSCALE/BZERO factors. Section slices cannot be assigned to, and modifications to a section are not saved back to the underlying file. See the :ref:`astropy:data-sections` section of the Astropy documentation for more details. """ def __init__(self, hdu): self.hdu = hdu @property def shape(self): # Implementing `.shape` enables `astropy.nddata.Cutout2D` to accept # `ImageHDU.section` in place of `.data`. return self.hdu.shape def __getitem__(self, key): """Returns a slice of HDU data specified by `key`. If the image HDU is backed by a file handle, this method will only read the chunks of the file needed to extract `key`, which is useful in situations where the file is located on a slow or remote file system (e.g., cloud storage). """ if not isinstance(key, tuple): key = (key,) naxis = len(self.hdu.shape) return_scalar = ( all(isinstance(k, (int, np.integer)) for k in key) and len(key) == naxis ) if not any(k is Ellipsis for k in key): # We can always add a ... at the end, after making note of whether # to return a scalar. key += (Ellipsis,) ellipsis_count = len([k for k in key if k is Ellipsis]) if len(key) - ellipsis_count > naxis or ellipsis_count > 1: raise IndexError("too many indices for array") # Insert extra dimensions as needed. idx = next(i for i, k in enumerate(key + (Ellipsis,)) if k is Ellipsis) key = key[:idx] + (slice(None),) * (naxis - len(key) + 1) + key[idx + 1 :] return_0dim = ( all(isinstance(k, (int, np.integer)) for k in key) and len(key) == naxis ) dims = [] offset = 0 # Find all leading axes for which a single point is used. for idx in range(naxis): axis = self.hdu.shape[idx] indx = _IndexInfo(key[idx], axis) offset = offset * axis + indx.offset if not _is_int(key[idx]): dims.append(indx.npts) break is_contiguous = indx.contiguous for jdx in range(idx + 1, naxis): axis = self.hdu.shape[jdx] indx = _IndexInfo(key[jdx], axis) dims.append(indx.npts) if indx.npts == axis and indx.contiguous: # The offset needs to multiply the length of all remaining axes offset *= axis else: is_contiguous = False if is_contiguous: dims = tuple(dims) or (1,) bitpix = self.hdu._orig_bitpix offset = self.hdu._data_offset + offset * abs(bitpix) // 8 # Note: the actual file read operations are delegated to # `util._array_from_file` via `ImageHDU._get_scaled_image_data` data = self.hdu._get_scaled_image_data(offset, dims) else: data = self._getdata(key) if return_scalar: data = data.item() elif return_0dim: data = data.squeeze() return data def _getdata(self, keys): for idx, (key, axis) in enumerate(zip(keys, self.hdu.shape)): if isinstance(key, slice): ks = range(*key.indices(axis)) break elif isiterable(key): # Handle both integer and boolean arrays. ks = np.arange(axis, dtype=int)[key] break # This should always break at some point if _getdata is called. data = [self[keys[:idx] + (k,) + keys[idx + 1 :]] for k in ks] if any(isinstance(key, slice) or isiterable(key) for key in keys[idx + 1 :]): # data contains multidimensional arrays; combine them. return np.array(data) else: # Only singleton dimensions remain; concatenate in a 1D array. return np.concatenate([np.atleast_1d(array) for array in data]) class PrimaryHDU(_ImageBaseHDU): """ FITS primary HDU class. """ _default_name = "PRIMARY" def __init__( self, data=None, header=None, do_not_scale_image_data=False, ignore_blank=False, uint=True, scale_back=None, ): """ Construct a primary HDU. Parameters ---------- data : array or ``astropy.io.fits.hdu.base.DELAYED``, optional The data in the HDU. header : `~astropy.io.fits.Header`, optional The header to be used (as a template). If ``header`` is `None`, a minimal header will be provided. do_not_scale_image_data : bool, optional If `True`, image data is not scaled using BSCALE/BZERO values when read. (default: False) ignore_blank : bool, optional If `True`, the BLANK header keyword will be ignored if present. Otherwise, pixels equal to this value will be replaced with NaNs. (default: False) uint : bool, optional Interpret signed integer data where ``BZERO`` is the central value and ``BSCALE == 1`` as unsigned integer data. For example, ``int16`` data with ``BZERO = 32768`` and ``BSCALE = 1`` would be treated as ``uint16`` data. (default: True) scale_back : bool, optional If `True`, when saving changes to a file that contained scaled image data, restore the data to the original type and reapply the original BSCALE/BZERO values. This could lead to loss of accuracy if scaling back to integer values after performing floating point operations on the data. Pseudo-unsigned integers are automatically rescaled unless scale_back is explicitly set to `False`. (default: None) """ super().__init__( data=data, header=header, do_not_scale_image_data=do_not_scale_image_data, uint=uint, ignore_blank=ignore_blank, scale_back=scale_back, ) # insert the keywords EXTEND if header is None: dim = self._header["NAXIS"] if dim == 0: dim = "" self._header.set("EXTEND", True, after="NAXIS" + str(dim)) @classmethod def match_header(cls, header): card = header.cards[0] # Due to problems discussed in #5808, we cannot assume the 'GROUPS' # keyword to be True/False, have to check the value return ( card.keyword == "SIMPLE" and ("GROUPS" not in header or header["GROUPS"] != True) # noqa: E712 and card.value ) def update_header(self): super().update_header() # Update the position of the EXTEND keyword if it already exists if "EXTEND" in self._header: if len(self._axes): after = "NAXIS" + str(len(self._axes)) else: after = "NAXIS" self._header.set("EXTEND", after=after) def _verify(self, option="warn"): errs = super()._verify(option=option) # Verify location and value of mandatory keywords. # The EXTEND keyword is only mandatory if the HDU has extensions; this # condition is checked by the HDUList object. However, if we already # have an EXTEND keyword check that its position is correct if "EXTEND" in self._header: naxis = self._header.get("NAXIS", 0) self.req_cards( "EXTEND", naxis + 3, lambda v: isinstance(v, bool), True, option, errs ) return errs class ImageHDU(_ImageBaseHDU, ExtensionHDU): """ FITS image extension HDU class. """ _extension = "IMAGE" def __init__( self, data=None, header=None, name=None, do_not_scale_image_data=False, uint=True, scale_back=None, ver=None, ): """ Construct an image HDU. Parameters ---------- data : array The data in the HDU. header : `~astropy.io.fits.Header` The header to be used (as a template). If ``header`` is `None`, a minimal header will be provided. name : str, optional The name of the HDU, will be the value of the keyword ``EXTNAME``. do_not_scale_image_data : bool, optional If `True`, image data is not scaled using BSCALE/BZERO values when read. (default: False) uint : bool, optional Interpret signed integer data where ``BZERO`` is the central value and ``BSCALE == 1`` as unsigned integer data. For example, ``int16`` data with ``BZERO = 32768`` and ``BSCALE = 1`` would be treated as ``uint16`` data. (default: True) scale_back : bool, optional If `True`, when saving changes to a file that contained scaled image data, restore the data to the original type and reapply the original BSCALE/BZERO values. This could lead to loss of accuracy if scaling back to integer values after performing floating point operations on the data. Pseudo-unsigned integers are automatically rescaled unless scale_back is explicitly set to `False`. (default: None) ver : int > 0 or None, optional The ver of the HDU, will be the value of the keyword ``EXTVER``. If not given or None, it defaults to the value of the ``EXTVER`` card of the ``header`` or 1. (default: None) """ # This __init__ currently does nothing differently from the base class, # and is only explicitly defined for the docstring. super().__init__( data=data, header=header, name=name, do_not_scale_image_data=do_not_scale_image_data, uint=uint, scale_back=scale_back, ver=ver, ) @classmethod def match_header(cls, header): card = header.cards[0] xtension = card.value if isinstance(xtension, str): xtension = xtension.rstrip() return card.keyword == "XTENSION" and xtension == cls._extension def _verify(self, option="warn"): """ ImageHDU verify method. """ errs = super()._verify(option=option) naxis = self._header.get("NAXIS", 0) # PCOUNT must == 0, GCOUNT must == 1; the former is verified in # ExtensionHDU._verify, however ExtensionHDU._verify allows PCOUNT # to be >= 0, so we need to check it here self.req_cards( "PCOUNT", naxis + 3, lambda v: (_is_int(v) and v == 0), 0, option, errs ) return errs class _IndexInfo: def __init__(self, indx, naxis): if _is_int(indx): if indx < 0: # support negative indexing indx = indx + naxis if 0 <= indx < naxis: self.npts = 1 self.offset = indx self.contiguous = True else: raise IndexError(f"Index {indx} out of range.") elif isinstance(indx, slice): start, stop, step = indx.indices(naxis) self.npts = (stop - start) // step self.offset = start self.contiguous = step == 1 elif isiterable(indx): self.npts = len(indx) self.offset = 0 self.contiguous = False else: raise IndexError(f"Illegal index {indx}")
41b9d2d9b5f089a6498a8258c037fc1dcef9a2c71af410018217c2d03744c6fd
# Licensed under a 3-clause BSD style license - see PYFITS.rst import ctypes import gc import itertools import math import re import time import warnings from contextlib import suppress import numpy as np from astropy.io.fits import conf from astropy.io.fits.card import Card from astropy.io.fits.column import KEYWORD_NAMES as TABLE_KEYWORD_NAMES from astropy.io.fits.column import TDEF_RE, ColDefs, Column from astropy.io.fits.fitsrec import FITS_rec from astropy.io.fits.header import Header from astropy.io.fits.util import ( _get_array_mmap, _is_int, _is_pseudo_integer, _pseudo_zero, ) from astropy.utils import lazyproperty from astropy.utils.exceptions import AstropyUserWarning from .base import BITPIX2DTYPE, DELAYED, DTYPE2BITPIX, ExtensionHDU from .image import ImageHDU from .table import BinTableHDU try: from astropy.io.fits import compression COMPRESSION_SUPPORTED = COMPRESSION_ENABLED = True except ImportError: COMPRESSION_SUPPORTED = COMPRESSION_ENABLED = False # Quantization dithering method constants; these are right out of fitsio.h NO_DITHER = -1 SUBTRACTIVE_DITHER_1 = 1 SUBTRACTIVE_DITHER_2 = 2 QUANTIZE_METHOD_NAMES = { NO_DITHER: "NO_DITHER", SUBTRACTIVE_DITHER_1: "SUBTRACTIVE_DITHER_1", SUBTRACTIVE_DITHER_2: "SUBTRACTIVE_DITHER_2", } DITHER_SEED_CLOCK = 0 DITHER_SEED_CHECKSUM = -1 COMPRESSION_TYPES = ("RICE_1", "GZIP_1", "GZIP_2", "PLIO_1", "HCOMPRESS_1") # Default compression parameter values DEFAULT_COMPRESSION_TYPE = "RICE_1" DEFAULT_QUANTIZE_LEVEL = 16.0 DEFAULT_QUANTIZE_METHOD = NO_DITHER DEFAULT_DITHER_SEED = DITHER_SEED_CLOCK DEFAULT_HCOMP_SCALE = 0 DEFAULT_HCOMP_SMOOTH = 0 DEFAULT_BLOCK_SIZE = 32 DEFAULT_BYTE_PIX = 4 CMTYPE_ALIASES = {"RICE_ONE": "RICE_1"} COMPRESSION_KEYWORDS = { "ZIMAGE", "ZCMPTYPE", "ZBITPIX", "ZNAXIS", "ZMASKCMP", "ZSIMPLE", "ZTENSION", "ZEXTEND", } class CompImageHeader(Header): """ Header object for compressed image HDUs designed to keep the compression header and the underlying image header properly synchronized. This essentially wraps the image header, so that all values are read from and written to the image header. However, updates to the image header will also update the table header where appropriate. Note that if no image header is passed in, the code will instantiate a regular `~astropy.io.fits.Header`. """ # TODO: The difficulty of implementing this screams a need to rewrite this # module _keyword_remaps = { "SIMPLE": "ZSIMPLE", "XTENSION": "ZTENSION", "BITPIX": "ZBITPIX", "NAXIS": "ZNAXIS", "EXTEND": "ZEXTEND", "BLOCKED": "ZBLOCKED", "PCOUNT": "ZPCOUNT", "GCOUNT": "ZGCOUNT", "CHECKSUM": "ZHECKSUM", "DATASUM": "ZDATASUM", } _zdef_re = re.compile(r"(?P<label>^[Zz][a-zA-Z]*)(?P<num>[1-9][0-9 ]*$)?") _compression_keywords = set(_keyword_remaps.values()).union( ["ZIMAGE", "ZCMPTYPE", "ZMASKCMP", "ZQUANTIZ", "ZDITHER0"] ) _indexed_compression_keywords = {"ZNAXIS", "ZTILE", "ZNAME", "ZVAL"} # TODO: Once it place it should be possible to manage some of this through # the schema system, but it's not quite ready for that yet. Also it still # makes more sense to change CompImageHDU to subclass ImageHDU :/ def __new__(cls, table_header, image_header=None): # 2019-09-14 (MHvK): No point wrapping anything if no image_header is # given. This happens if __getitem__ and copy are called - our super # class will aim to initialize a new, possibly partially filled # header, but we cannot usefully deal with that. # TODO: the above suggests strongly we should *not* subclass from # Header. See also comment above about the need for reorganization. if image_header is None: return Header(table_header) else: return super().__new__(cls) def __init__(self, table_header, image_header): self._cards = image_header._cards self._keyword_indices = image_header._keyword_indices self._rvkc_indices = image_header._rvkc_indices self._modified = image_header._modified self._table_header = table_header # We need to override and Header methods that can modify the header, and # ensure that they sync with the underlying _table_header def __setitem__(self, key, value): # This isn't pretty, but if the `key` is either an int or a tuple we # need to figure out what keyword name that maps to before doing # anything else; these checks will be repeated later in the # super().__setitem__ call but I don't see another way around it # without some major refactoring if self._set_slice(key, value, self): return if isinstance(key, int): keyword, index = self._keyword_from_index(key) elif isinstance(key, tuple): keyword, index = key else: # We don't want to specify and index otherwise, because that will # break the behavior for new keywords and for commentary keywords keyword, index = key, None if self._is_reserved_keyword(keyword): return super().__setitem__(key, value) if index is not None: remapped_keyword = self._remap_keyword(keyword) self._table_header[remapped_keyword, index] = value # Else this will pass through to ._update def __delitem__(self, key): if isinstance(key, slice) or self._haswildcard(key): # If given a slice pass that on to the superclass and bail out # early; we only want to make updates to _table_header when given # a key specifying a single keyword return super().__delitem__(key) if isinstance(key, int): keyword, index = self._keyword_from_index(key) elif isinstance(key, tuple): keyword, index = key else: keyword, index = key, None if key not in self: raise KeyError(f"Keyword {key!r} not found.") super().__delitem__(key) remapped_keyword = self._remap_keyword(keyword) if remapped_keyword in self._table_header: if index is not None: del self._table_header[(remapped_keyword, index)] else: del self._table_header[remapped_keyword] def append(self, card=None, useblanks=True, bottom=False, end=False): # This logic unfortunately needs to be duplicated from the base class # in order to determine the keyword if isinstance(card, str): card = Card(card) elif isinstance(card, tuple): card = Card(*card) elif card is None: card = Card() elif not isinstance(card, Card): raise ValueError( "The value appended to a Header must be either a keyword or " "(keyword, value, [comment]) tuple; got: {!r}".format(card) ) if self._is_reserved_keyword(card.keyword): return super().append(card=card, useblanks=useblanks, bottom=bottom, end=end) remapped_keyword = self._remap_keyword(card.keyword) # card.keyword strips the HIERARCH if present so this must be added # back to avoid a warning. if str(card).startswith("HIERARCH ") and not remapped_keyword.startswith( "HIERARCH " ): remapped_keyword = "HIERARCH " + remapped_keyword card = Card(remapped_keyword, card.value, card.comment) # Here we disable the use of blank cards, because the call above to # Header.append may have already deleted a blank card in the table # header, thanks to inheritance: Header.append calls 'del self[-1]' # to delete a blank card, which calls CompImageHeader.__deltitem__, # which deletes the blank card both in the image and the table headers! self._table_header.append(card=card, useblanks=False, bottom=bottom, end=end) def insert(self, key, card, useblanks=True, after=False): if isinstance(key, int): # Determine condition to pass through to append if after: if key == -1: key = len(self._cards) else: key += 1 if key >= len(self._cards): self.append(card, end=True) return if isinstance(card, str): card = Card(card) elif isinstance(card, tuple): card = Card(*card) elif not isinstance(card, Card): raise ValueError( "The value inserted into a Header must be either a keyword or " "(keyword, value, [comment]) tuple; got: {!r}".format(card) ) if self._is_reserved_keyword(card.keyword): return # Now the tricky part is to determine where to insert in the table # header. If given a numerical index we need to map that to the # corresponding index in the table header. Although rare, there may be # cases where there is no mapping in which case we just try the same # index # NOTE: It is crucial that remapped_index in particular is figured out # before the image header is modified remapped_index = self._remap_index(key) remapped_keyword = self._remap_keyword(card.keyword) super().insert(key, card, useblanks=useblanks, after=after) card = Card(remapped_keyword, card.value, card.comment) # Here we disable the use of blank cards, because the call above to # Header.insert may have already deleted a blank card in the table # header, thanks to inheritance: Header.insert calls 'del self[-1]' # to delete a blank card, which calls CompImageHeader.__delitem__, # which deletes the blank card both in the image and the table headers! self._table_header.insert(remapped_index, card, useblanks=False, after=after) def _update(self, card): keyword = card[0] if self._is_reserved_keyword(keyword): return super()._update(card) if keyword in Card._commentary_keywords: # Otherwise this will result in a duplicate insertion return remapped_keyword = self._remap_keyword(keyword) self._table_header._update((remapped_keyword,) + card[1:]) # Last piece needed (I think) for synchronizing with the real header # This one is tricky since _relativeinsert calls insert def _relativeinsert(self, card, before=None, after=None, replace=False): keyword = card[0] if self._is_reserved_keyword(keyword): return # Now we have to figure out how to remap 'before' and 'after' if before is None: if isinstance(after, int): remapped_after = self._remap_index(after) else: remapped_after = self._remap_keyword(after) remapped_before = None else: if isinstance(before, int): remapped_before = self._remap_index(before) else: remapped_before = self._remap_keyword(before) remapped_after = None super()._relativeinsert(card, before=before, after=after, replace=replace) remapped_keyword = self._remap_keyword(keyword) card = Card(remapped_keyword, card[1], card[2]) self._table_header._relativeinsert( card, before=remapped_before, after=remapped_after, replace=replace ) @classmethod def _is_reserved_keyword(cls, keyword, warn=True): msg = ( "Keyword {!r} is reserved for use by the FITS Tiled Image " "Convention and will not be stored in the header for the " "image being compressed.".format(keyword) ) if keyword == "TFIELDS": if warn: warnings.warn(msg) return True m = TDEF_RE.match(keyword) if m and m.group("label").upper() in TABLE_KEYWORD_NAMES: if warn: warnings.warn(msg) return True m = cls._zdef_re.match(keyword) if m: label = m.group("label").upper() num = m.group("num") if num is not None and label in cls._indexed_compression_keywords: if warn: warnings.warn(msg) return True elif label in cls._compression_keywords: if warn: warnings.warn(msg) return True return False @classmethod def _remap_keyword(cls, keyword): # Given a keyword that one might set on an image, remap that keyword to # the name used for it in the COMPRESSED HDU header # This is mostly just a lookup in _keyword_remaps, but needs handling # for NAXISn keywords is_naxisn = False if keyword[:5] == "NAXIS": with suppress(ValueError): index = int(keyword[5:]) is_naxisn = index > 0 if is_naxisn: return f"ZNAXIS{index}" # If the keyword does not need to be remapped then just return the # original keyword return cls._keyword_remaps.get(keyword, keyword) def _remap_index(self, idx): # Given an integer index into this header, map that to the index in the # table header for the same card. If the card doesn't exist in the # table header (generally should *not* be the case) this will just # return the same index # This *does* also accept a keyword or (keyword, repeat) tuple and # obtains the associated numerical index with self._cardindex if not isinstance(idx, int): idx = self._cardindex(idx) keyword, repeat = self._keyword_from_index(idx) remapped_insert_keyword = self._remap_keyword(keyword) with suppress(IndexError, KeyError): idx = self._table_header._cardindex((remapped_insert_keyword, repeat)) return idx def clear(self): """ Remove all cards from the header. """ self._table_header.clear() super().clear() # TODO: Fix this class so that it doesn't actually inherit from BinTableHDU, # but instead has an internal BinTableHDU reference class CompImageHDU(BinTableHDU): """ Compressed Image HDU class. """ _manages_own_heap = True """ The calls to CFITSIO lay out the heap data in memory, and we write it out the same way CFITSIO organizes it. In principle this would break if a user manually changes the underlying compressed data by hand, but there is no reason they would want to do that (and if they do that's their responsibility). """ _default_name = "COMPRESSED_IMAGE" def __init__( self, data=None, header=None, name=None, compression_type=DEFAULT_COMPRESSION_TYPE, tile_size=None, hcomp_scale=DEFAULT_HCOMP_SCALE, hcomp_smooth=DEFAULT_HCOMP_SMOOTH, quantize_level=DEFAULT_QUANTIZE_LEVEL, quantize_method=DEFAULT_QUANTIZE_METHOD, dither_seed=DEFAULT_DITHER_SEED, do_not_scale_image_data=False, uint=False, scale_back=False, **kwargs, ): """ Parameters ---------- data : array, optional Uncompressed image data header : `~astropy.io.fits.Header`, optional Header to be associated with the image; when reading the HDU from a file (data=DELAYED), the header read from the file name : str, optional The ``EXTNAME`` value; if this value is `None`, then the name from the input image header will be used; if there is no name in the input image header then the default name ``COMPRESSED_IMAGE`` is used. compression_type : str, optional Compression algorithm: one of ``'RICE_1'``, ``'RICE_ONE'``, ``'PLIO_1'``, ``'GZIP_1'``, ``'GZIP_2'``, ``'HCOMPRESS_1'`` tile_size : int, optional Compression tile sizes. Default treats each row of image as a tile. hcomp_scale : float, optional HCOMPRESS scale parameter hcomp_smooth : float, optional HCOMPRESS smooth parameter quantize_level : float, optional Floating point quantization level; see note below quantize_method : int, optional Floating point quantization dithering method; can be either ``NO_DITHER`` (-1; default), ``SUBTRACTIVE_DITHER_1`` (1), or ``SUBTRACTIVE_DITHER_2`` (2); see note below dither_seed : int, optional Random seed to use for dithering; can be either an integer in the range 1 to 1000 (inclusive), ``DITHER_SEED_CLOCK`` (0; default), or ``DITHER_SEED_CHECKSUM`` (-1); see note below Notes ----- The astropy.io.fits package supports 2 methods of image compression: 1) The entire FITS file may be externally compressed with the gzip or pkzip utility programs, producing a ``*.gz`` or ``*.zip`` file, respectively. When reading compressed files of this type, Astropy first uncompresses the entire file into a temporary file before performing the requested read operations. The astropy.io.fits package does not support writing to these types of compressed files. This type of compression is supported in the ``_File`` class, not in the `CompImageHDU` class. The file compression type is recognized by the ``.gz`` or ``.zip`` file name extension. 2) The `CompImageHDU` class supports the FITS tiled image compression convention in which the image is subdivided into a grid of rectangular tiles, and each tile of pixels is individually compressed. The details of this FITS compression convention are described at the `FITS Support Office web site <https://fits.gsfc.nasa.gov/registry/tilecompression.html>`_. Basically, the compressed image tiles are stored in rows of a variable length array column in a FITS binary table. The astropy.io.fits recognizes that this binary table extension contains an image and treats it as if it were an image extension. Under this tile-compression format, FITS header keywords remain uncompressed. At this time, Astropy does not support the ability to extract and uncompress sections of the image without having to uncompress the entire image. The astropy.io.fits package supports 3 general-purpose compression algorithms plus one other special-purpose compression technique that is designed for data masks with positive integer pixel values. The 3 general purpose algorithms are GZIP, Rice, and HCOMPRESS, and the special-purpose technique is the IRAF pixel list compression technique (PLIO). The ``compression_type`` parameter defines the compression algorithm to be used. The FITS image can be subdivided into any desired rectangular grid of compression tiles. With the GZIP, Rice, and PLIO algorithms, the default is to take each row of the image as a tile. The HCOMPRESS algorithm is inherently 2-dimensional in nature, so the default in this case is to take 16 rows of the image per tile. In most cases, it makes little difference what tiling pattern is used, so the default tiles are usually adequate. In the case of very small images, it could be more efficient to compress the whole image as a single tile. Note that the image dimensions are not required to be an integer multiple of the tile dimensions; if not, then the tiles at the edges of the image will be smaller than the other tiles. The ``tile_size`` parameter may be provided as a list of tile sizes, one for each dimension in the image. For example a ``tile_size`` value of ``[100,100]`` would divide a 300 X 300 image into 9 100 X 100 tiles. The 4 supported image compression algorithms are all 'lossless' when applied to integer FITS images; the pixel values are preserved exactly with no loss of information during the compression and uncompression process. In addition, the HCOMPRESS algorithm supports a 'lossy' compression mode that will produce larger amount of image compression. This is achieved by specifying a non-zero value for the ``hcomp_scale`` parameter. Since the amount of compression that is achieved depends directly on the RMS noise in the image, it is usually more convenient to specify the ``hcomp_scale`` factor relative to the RMS noise. Setting ``hcomp_scale = 2.5`` means use a scale factor that is 2.5 times the calculated RMS noise in the image tile. In some cases it may be desirable to specify the exact scaling to be used, instead of specifying it relative to the calculated noise value. This may be done by specifying the negative of the desired scale value (typically in the range -2 to -100). Very high compression factors (of 100 or more) can be achieved by using large ``hcomp_scale`` values, however, this can produce undesirable 'blocky' artifacts in the compressed image. A variation of the HCOMPRESS algorithm (called HSCOMPRESS) can be used in this case to apply a small amount of smoothing of the image when it is uncompressed to help cover up these artifacts. This smoothing is purely cosmetic and does not cause any significant change to the image pixel values. Setting the ``hcomp_smooth`` parameter to 1 will engage the smoothing algorithm. Floating point FITS images (which have ``BITPIX`` = -32 or -64) usually contain too much 'noise' in the least significant bits of the mantissa of the pixel values to be effectively compressed with any lossless algorithm. Consequently, floating point images are first quantized into scaled integer pixel values (and thus throwing away much of the noise) before being compressed with the specified algorithm (either GZIP, RICE, or HCOMPRESS). This technique produces much higher compression factors than simply using the GZIP utility to externally compress the whole FITS file, but it also means that the original floating point value pixel values are not exactly preserved. When done properly, this integer scaling technique will only discard the insignificant noise while still preserving all the real information in the image. The amount of precision that is retained in the pixel values is controlled by the ``quantize_level`` parameter. Larger values will result in compressed images whose pixels more closely match the floating point pixel values, but at the same time the amount of compression that is achieved will be reduced. Users should experiment with different values for this parameter to determine the optimal value that preserves all the useful information in the image, without needlessly preserving all the 'noise' which will hurt the compression efficiency. The default value for the ``quantize_level`` scale factor is 16, which means that scaled integer pixel values will be quantized such that the difference between adjacent integer values will be 1/16th of the noise level in the image background. An optimized algorithm is used to accurately estimate the noise in the image. As an example, if the RMS noise in the background pixels of an image = 32.0, then the spacing between adjacent scaled integer pixel values will equal 2.0 by default. Note that the RMS noise is independently calculated for each tile of the image, so the resulting integer scaling factor may fluctuate slightly for each tile. In some cases, it may be desirable to specify the exact quantization level to be used, instead of specifying it relative to the calculated noise value. This may be done by specifying the negative of desired quantization level for the value of ``quantize_level``. In the previous example, one could specify ``quantize_level = -2.0`` so that the quantized integer levels differ by 2.0. Larger negative values for ``quantize_level`` means that the levels are more coarsely-spaced, and will produce higher compression factors. The quantization algorithm can also apply one of two random dithering methods in order to reduce bias in the measured intensity of background regions. The default method, specified with the constant ``SUBTRACTIVE_DITHER_1`` adds dithering to the zero-point of the quantization array itself rather than adding noise to the actual image. The random noise is added on a pixel-by-pixel basis, so in order restore each pixel from its integer value to its floating point value it is necessary to replay the same sequence of random numbers for each pixel (see below). The other method, ``SUBTRACTIVE_DITHER_2``, is exactly like the first except that before dithering any pixel with a floating point value of ``0.0`` is replaced with the special integer value ``-2147483647``. When the image is uncompressed, pixels with this value are restored back to ``0.0`` exactly. Finally, a value of ``NO_DITHER`` disables dithering entirely. As mentioned above, when using the subtractive dithering algorithm it is necessary to be able to generate a (pseudo-)random sequence of noise for each pixel, and replay that same sequence upon decompressing. To facilitate this, a random seed between 1 and 10000 (inclusive) is used to seed a random number generator, and that seed is stored in the ``ZDITHER0`` keyword in the header of the compressed HDU. In order to use that seed to generate the same sequence of random numbers the same random number generator must be used at compression and decompression time; for that reason the tiled image convention provides an implementation of a very simple pseudo-random number generator. The seed itself can be provided in one of three ways, controllable by the ``dither_seed`` argument: It may be specified manually, or it may be generated arbitrarily based on the system's clock (``DITHER_SEED_CLOCK``) or based on a checksum of the pixels in the image's first tile (``DITHER_SEED_CHECKSUM``). The clock-based method is the default, and is sufficient to ensure that the value is reasonably "arbitrary" and that the same seed is unlikely to be generated sequentially. The checksum method, on the other hand, ensures that the same seed is used every time for a specific image. This is particularly useful for software testing as it ensures that the same image will always use the same seed. """ if not COMPRESSION_SUPPORTED: # TODO: Raise a more specific Exception type raise Exception( "The astropy.io.fits.compression module is not " "available. Creation of compressed image HDUs is " "disabled." ) compression_type = CMTYPE_ALIASES.get(compression_type, compression_type) if data is DELAYED: # Reading the HDU from a file super().__init__(data=data, header=header) else: # Create at least a skeleton HDU that matches the input # header and data (if any were input) super().__init__(data=None, header=header) # Store the input image data self.data = data # Update the table header (_header) to the compressed # image format and to match the input data (if any); # Create the image header (_image_header) from the input # image header (if any) and ensure it matches the input # data; Create the initially empty table data array to # hold the compressed data. self._update_header_data( header, name, compression_type=compression_type, tile_size=tile_size, hcomp_scale=hcomp_scale, hcomp_smooth=hcomp_smooth, quantize_level=quantize_level, quantize_method=quantize_method, dither_seed=dither_seed, ) # TODO: A lot of this should be passed on to an internal image HDU o # something like that, see ticket #88 self._do_not_scale_image_data = do_not_scale_image_data self._uint = uint self._scale_back = scale_back self._axes = [ self._header.get("ZNAXIS" + str(axis + 1), 0) for axis in range(self._header.get("ZNAXIS", 0)) ] # store any scale factors from the table header if do_not_scale_image_data: self._bzero = 0 self._bscale = 1 else: self._bzero = self._header.get("BZERO", 0) self._bscale = self._header.get("BSCALE", 1) self._bitpix = self._header["ZBITPIX"] self._orig_bzero = self._bzero self._orig_bscale = self._bscale self._orig_bitpix = self._bitpix def _remove_unnecessary_default_extnames(self, header): """Remove default EXTNAME values if they are unnecessary. Some data files (eg from CFHT) can have the default EXTNAME and an explicit value. This method removes the default if a more specific header exists. It also removes any duplicate default values. """ if "EXTNAME" in header: indices = header._keyword_indices["EXTNAME"] # Only continue if there is more than one found n_extname = len(indices) if n_extname > 1: extnames_to_remove = [ index for index in indices if header[index] == self._default_name ] if len(extnames_to_remove) == n_extname: # Keep the first (they are all the same) extnames_to_remove.pop(0) # Remove them all in reverse order to keep the index unchanged. for index in reversed(sorted(extnames_to_remove)): del header[index] @property def name(self): # Convert the value to a string to be flexible in some pathological # cases (see ticket #96) # Similar to base class but uses .header rather than ._header return str(self.header.get("EXTNAME", self._default_name)) @name.setter def name(self, value): # This is a copy of the base class but using .header instead # of ._header to ensure that the name stays in sync. if not isinstance(value, str): raise TypeError("'name' attribute must be a string") if not conf.extension_name_case_sensitive: value = value.upper() if "EXTNAME" in self.header: self.header["EXTNAME"] = value else: self.header["EXTNAME"] = (value, "extension name") @classmethod def match_header(cls, header): card = header.cards[0] if card.keyword != "XTENSION": return False xtension = card.value if isinstance(xtension, str): xtension = xtension.rstrip() if xtension not in ("BINTABLE", "A3DTABLE"): return False if "ZIMAGE" not in header or not header["ZIMAGE"]: return False if COMPRESSION_SUPPORTED and COMPRESSION_ENABLED: return True elif not COMPRESSION_SUPPORTED: warnings.warn( "Failure matching header to a compressed image " "HDU: The compression module is not available.\n" "The HDU will be treated as a Binary Table HDU.", AstropyUserWarning, ) return False else: # Compression is supported but disabled; just pass silently (#92) return False def _update_header_data( self, image_header, name=None, compression_type=None, tile_size=None, hcomp_scale=None, hcomp_smooth=None, quantize_level=None, quantize_method=None, dither_seed=None, ): """ Update the table header (`_header`) to the compressed image format and to match the input data (if any). Create the image header (`_image_header`) from the input image header (if any) and ensure it matches the input data. Create the initially-empty table data array to hold the compressed data. This method is mainly called internally, but a user may wish to call this method after assigning new data to the `CompImageHDU` object that is of a different type. Parameters ---------- image_header : `~astropy.io.fits.Header` header to be associated with the image name : str, optional the ``EXTNAME`` value; if this value is `None`, then the name from the input image header will be used; if there is no name in the input image header then the default name 'COMPRESSED_IMAGE' is used compression_type : str, optional compression algorithm 'RICE_1', 'PLIO_1', 'GZIP_1', 'GZIP_2', 'HCOMPRESS_1'; if this value is `None`, use value already in the header; if no value already in the header, use 'RICE_1' tile_size : sequence of int, optional compression tile sizes as a list; if this value is `None`, use value already in the header; if no value already in the header, treat each row of image as a tile hcomp_scale : float, optional HCOMPRESS scale parameter; if this value is `None`, use the value already in the header; if no value already in the header, use 1 hcomp_smooth : float, optional HCOMPRESS smooth parameter; if this value is `None`, use the value already in the header; if no value already in the header, use 0 quantize_level : float, optional floating point quantization level; if this value is `None`, use the value already in the header; if no value already in header, use 16 quantize_method : int, optional floating point quantization dithering method; can be either NO_DITHER (-1), SUBTRACTIVE_DITHER_1 (1; default), or SUBTRACTIVE_DITHER_2 (2) dither_seed : int, optional random seed to use for dithering; can be either an integer in the range 1 to 1000 (inclusive), DITHER_SEED_CLOCK (0; default), or DITHER_SEED_CHECKSUM (-1) """ # Clean up EXTNAME duplicates self._remove_unnecessary_default_extnames(self._header) image_hdu = ImageHDU(data=self.data, header=self._header) self._image_header = CompImageHeader(self._header, image_hdu.header) self._axes = image_hdu._axes del image_hdu # Determine based on the size of the input data whether to use the Q # column format to store compressed data or the P format. # The Q format is used only if the uncompressed data is larger than # 4 GB. This is not a perfect heuristic, as one can contrive an input # array which, when compressed, the entire binary table representing # the compressed data is larger than 4GB. That said, this is the same # heuristic used by CFITSIO, so this should give consistent results. # And the cases where this heuristic is insufficient are extreme and # almost entirely contrived corner cases, so it will do for now if self._has_data: huge_hdu = self.data.nbytes > 2**32 else: huge_hdu = False # Update the extension name in the table header if not name and "EXTNAME" not in self._header: # Do not sync this with the image header since the default # name is specific to the table header. self._header.set( "EXTNAME", self._default_name, "name of this binary table extension", after="TFIELDS", ) elif name: # Force the name into table and image headers. self.name = name # Set the compression type in the table header. if compression_type: if compression_type not in COMPRESSION_TYPES: warnings.warn( "Unknown compression type provided (supported are {}). " "Default ({}) compression will be used.".format( ", ".join(map(repr, COMPRESSION_TYPES)), DEFAULT_COMPRESSION_TYPE, ), AstropyUserWarning, ) compression_type = DEFAULT_COMPRESSION_TYPE self._header.set( "ZCMPTYPE", compression_type, "compression algorithm", after="TFIELDS" ) else: compression_type = self._header.get("ZCMPTYPE", DEFAULT_COMPRESSION_TYPE) compression_type = CMTYPE_ALIASES.get(compression_type, compression_type) # If the input image header had BSCALE/BZERO cards, then insert # them in the table header. if image_header: bzero = image_header.get("BZERO", 0.0) bscale = image_header.get("BSCALE", 1.0) after_keyword = "EXTNAME" if bscale != 1.0: self._header.set("BSCALE", bscale, after=after_keyword) after_keyword = "BSCALE" if bzero != 0.0: self._header.set("BZERO", bzero, after=after_keyword) try: bitpix_comment = image_header.comments["BITPIX"] except (AttributeError, KeyError): bitpix_comment = "data type of original image" try: naxis_comment = image_header.comments["NAXIS"] except (AttributeError, KeyError): naxis_comment = "dimension of original image" # Set the label for the first column in the table self._header.set( "TTYPE1", "COMPRESSED_DATA", "label for field 1", after="TFIELDS" ) # Set the data format for the first column. It is dependent # on the requested compression type. if compression_type == "PLIO_1": tform1 = "1QI" if huge_hdu else "1PI" else: tform1 = "1QB" if huge_hdu else "1PB" self._header.set( "TFORM1", tform1, "data format of field: variable length array", after="TTYPE1", ) # Create the first column for the table. This column holds the # compressed data. col1 = Column(name=self._header["TTYPE1"], format=tform1) # Create the additional columns required for floating point # data and calculate the width of the output table. zbitpix = self._image_header["BITPIX"] if zbitpix < 0 and quantize_level != 0.0: # floating point image has 'COMPRESSED_DATA', # 'UNCOMPRESSED_DATA', 'ZSCALE', and 'ZZERO' columns (unless using # lossless compression, per CFITSIO) ncols = 4 # CFITSIO 3.28 and up automatically use the GZIP_COMPRESSED_DATA # store floating point data that couldn't be quantized, instead # of the UNCOMPRESSED_DATA column. There's no way to control # this behavior so the only way to determine which behavior will # be employed is via the CFITSIO version ttype2 = "GZIP_COMPRESSED_DATA" # The required format for the GZIP_COMPRESSED_DATA is actually # missing from the standard docs, but CFITSIO suggests it # should be 1PB, which is logical. tform2 = "1QB" if huge_hdu else "1PB" # Set up the second column for the table that will hold any # uncompressable data. self._header.set("TTYPE2", ttype2, "label for field 2", after="TFORM1") self._header.set( "TFORM2", tform2, "data format of field: variable length array", after="TTYPE2", ) col2 = Column(name=ttype2, format=tform2) # Set up the third column for the table that will hold # the scale values for quantized data. self._header.set("TTYPE3", "ZSCALE", "label for field 3", after="TFORM2") self._header.set( "TFORM3", "1D", "data format of field: 8-byte DOUBLE", after="TTYPE3" ) col3 = Column(name=self._header["TTYPE3"], format=self._header["TFORM3"]) # Set up the fourth column for the table that will hold # the zero values for the quantized data. self._header.set("TTYPE4", "ZZERO", "label for field 4", after="TFORM3") self._header.set( "TFORM4", "1D", "data format of field: 8-byte DOUBLE", after="TTYPE4" ) after = "TFORM4" col4 = Column(name=self._header["TTYPE4"], format=self._header["TFORM4"]) # Create the ColDefs object for the table cols = ColDefs([col1, col2, col3, col4]) else: # default table has just one 'COMPRESSED_DATA' column ncols = 1 after = "TFORM1" # remove any header cards for the additional columns that # may be left over from the previous data to_remove = ["TTYPE2", "TFORM2", "TTYPE3", "TFORM3", "TTYPE4", "TFORM4"] for k in to_remove: try: del self._header[k] except KeyError: pass # Create the ColDefs object for the table cols = ColDefs([col1]) # Update the table header with the width of the table, the # number of fields in the table, the indicator for a compressed # image HDU, the data type of the image data and the number of # dimensions in the image data array. self._header.set("NAXIS1", cols.dtype.itemsize, "width of table in bytes") self._header.set( "TFIELDS", ncols, "number of fields in each row", after="GCOUNT" ) self._header.set( "ZIMAGE", True, "extension contains compressed image", after=after ) self._header.set("ZBITPIX", zbitpix, bitpix_comment, after="ZIMAGE") self._header.set( "ZNAXIS", self._image_header["NAXIS"], naxis_comment, after="ZBITPIX" ) # Strip the table header of all the ZNAZISn and ZTILEn keywords # that may be left over from the previous data for idx in itertools.count(1): try: del self._header["ZNAXIS" + str(idx)] del self._header["ZTILE" + str(idx)] except KeyError: break # Verify that any input tile size parameter is the appropriate # size to match the HDU's data. naxis = self._image_header["NAXIS"] if not tile_size: tile_size = [] elif len(tile_size) != naxis: warnings.warn( "Provided tile size not appropriate for the data. " "Default tile size will be used.", AstropyUserWarning, ) tile_size = [] # Set default tile dimensions for HCOMPRESS_1 if compression_type == "HCOMPRESS_1": if self._image_header["NAXIS1"] < 4 or self._image_header["NAXIS2"] < 4: raise ValueError("Hcompress minimum image dimension is 4 pixels") elif tile_size: if tile_size[0] < 4 or tile_size[1] < 4: # user specified tile size is too small raise ValueError("Hcompress minimum tile dimension is 4 pixels") major_dims = len([ts for ts in tile_size if ts > 1]) if major_dims > 2: raise ValueError( "HCOMPRESS can only support 2-dimensional tile sizes." "All but two of the tile_size dimensions must be set " "to 1." ) if tile_size and (tile_size[0] == 0 and tile_size[1] == 0): # compress the whole image as a single tile tile_size[0] = self._image_header["NAXIS1"] tile_size[1] = self._image_header["NAXIS2"] for i in range(2, naxis): # set all higher tile dimensions = 1 tile_size[i] = 1 elif not tile_size: # The Hcompress algorithm is inherently 2D in nature, so the # row by row tiling that is used for other compression # algorithms is not appropriate. If the image has less than 30 # rows, then the entire image will be compressed as a single # tile. Otherwise the tiles will consist of 16 rows of the # image. This keeps the tiles to a reasonable size, and it # also includes enough rows to allow good compression # efficiency. It the last tile of the image happens to contain # less than 4 rows, then find another tile size with between 14 # and 30 rows (preferably even), so that the last tile has at # least 4 rows. # 1st tile dimension is the row length of the image tile_size.append(self._image_header["NAXIS1"]) if self._image_header["NAXIS2"] <= 30: tile_size.append(self._image_header["NAXIS1"]) else: # look for another good tile dimension naxis2 = self._image_header["NAXIS2"] for dim in [16, 24, 20, 30, 28, 26, 22, 18, 14]: if naxis2 % dim == 0 or naxis2 % dim > 3: tile_size.append(dim) break else: tile_size.append(17) for i in range(2, naxis): # set all higher tile dimensions = 1 tile_size.append(1) # check if requested tile size causes the last tile to have # less than 4 pixels remain = self._image_header["NAXIS1"] % tile_size[0] # 1st dimen if remain > 0 and remain < 4: tile_size[0] += 1 # try increasing tile size by 1 remain = self._image_header["NAXIS1"] % tile_size[0] if remain > 0 and remain < 4: raise ValueError( "Last tile along 1st dimension has less than 4 pixels" ) remain = self._image_header["NAXIS2"] % tile_size[1] # 2nd dimen if remain > 0 and remain < 4: tile_size[1] += 1 # try increasing tile size by 1 remain = self._image_header["NAXIS2"] % tile_size[1] if remain > 0 and remain < 4: raise ValueError( "Last tile along 2nd dimension has less than 4 pixels" ) # Set up locations for writing the next cards in the header. last_znaxis = "ZNAXIS" if self._image_header["NAXIS"] > 0: after1 = "ZNAXIS1" else: after1 = "ZNAXIS" # Calculate the number of rows in the output table and # write the ZNAXISn and ZTILEn cards to the table header. nrows = 0 for idx, axis in enumerate(self._axes): naxis = "NAXIS" + str(idx + 1) znaxis = "ZNAXIS" + str(idx + 1) ztile = "ZTILE" + str(idx + 1) if tile_size and len(tile_size) >= idx + 1: ts = tile_size[idx] else: if ztile not in self._header: # Default tile size if not idx: ts = self._image_header["NAXIS1"] else: ts = 1 else: ts = self._header[ztile] tile_size.append(ts) if not nrows: nrows = (axis - 1) // ts + 1 else: nrows *= (axis - 1) // ts + 1 if image_header and naxis in image_header: self._header.set( znaxis, axis, image_header.comments[naxis], after=last_znaxis ) else: self._header.set( znaxis, axis, "length of original image axis", after=last_znaxis ) self._header.set(ztile, ts, "size of tiles to be compressed", after=after1) last_znaxis = znaxis after1 = ztile # Set the NAXIS2 header card in the table hdu to the number of # rows in the table. self._header.set("NAXIS2", nrows, "number of rows in table") self.columns = cols # Set the compression parameters in the table header. # First, setup the values to be used for the compression parameters # in case none were passed in. This will be either the value # already in the table header for that parameter or the default # value. for idx in itertools.count(1): zname = "ZNAME" + str(idx) if zname not in self._header: break zval = "ZVAL" + str(idx) if self._header[zname] == "NOISEBIT": if quantize_level is None: quantize_level = self._header[zval] if self._header[zname] == "SCALE ": if hcomp_scale is None: hcomp_scale = self._header[zval] if self._header[zname] == "SMOOTH ": if hcomp_smooth is None: hcomp_smooth = self._header[zval] if quantize_level is None: quantize_level = DEFAULT_QUANTIZE_LEVEL if hcomp_scale is None: hcomp_scale = DEFAULT_HCOMP_SCALE if hcomp_smooth is None: hcomp_smooth = DEFAULT_HCOMP_SCALE # Next, strip the table header of all the ZNAMEn and ZVALn keywords # that may be left over from the previous data for idx in itertools.count(1): zname = "ZNAME" + str(idx) if zname not in self._header: break zval = "ZVAL" + str(idx) del self._header[zname] del self._header[zval] # Finally, put the appropriate keywords back based on the # compression type. after_keyword = "ZCMPTYPE" idx = 1 if compression_type == "RICE_1": self._header.set( "ZNAME1", "BLOCKSIZE", "compression block size", after=after_keyword ) self._header.set( "ZVAL1", DEFAULT_BLOCK_SIZE, "pixels per block", after="ZNAME1" ) self._header.set( "ZNAME2", "BYTEPIX", "bytes per pixel (1, 2, 4, or 8)", after="ZVAL1" ) if self._header["ZBITPIX"] == 8: bytepix = 1 elif self._header["ZBITPIX"] == 16: bytepix = 2 else: bytepix = DEFAULT_BYTE_PIX self._header.set( "ZVAL2", bytepix, "bytes per pixel (1, 2, 4, or 8)", after="ZNAME2" ) after_keyword = "ZVAL2" idx = 3 elif compression_type == "HCOMPRESS_1": self._header.set( "ZNAME1", "SCALE", "HCOMPRESS scale factor", after=after_keyword ) self._header.set( "ZVAL1", hcomp_scale, "HCOMPRESS scale factor", after="ZNAME1" ) self._header.set( "ZNAME2", "SMOOTH", "HCOMPRESS smooth option", after="ZVAL1" ) self._header.set( "ZVAL2", hcomp_smooth, "HCOMPRESS smooth option", after="ZNAME2" ) after_keyword = "ZVAL2" idx = 3 if self._image_header["BITPIX"] < 0: # floating point image self._header.set( "ZNAME" + str(idx), "NOISEBIT", "floating point quantization level", after=after_keyword, ) self._header.set( "ZVAL" + str(idx), quantize_level, "floating point quantization level", after="ZNAME" + str(idx), ) # Add the dither method and seed if quantize_method: if quantize_method not in [ NO_DITHER, SUBTRACTIVE_DITHER_1, SUBTRACTIVE_DITHER_2, ]: name = QUANTIZE_METHOD_NAMES[DEFAULT_QUANTIZE_METHOD] warnings.warn( "Unknown quantization method provided. " "Default method ({}) used.".format(name) ) quantize_method = DEFAULT_QUANTIZE_METHOD if quantize_method == NO_DITHER: zquantiz_comment = "No dithering during quantization" else: zquantiz_comment = "Pixel Quantization Algorithm" self._header.set( "ZQUANTIZ", QUANTIZE_METHOD_NAMES[quantize_method], zquantiz_comment, after="ZVAL" + str(idx), ) else: # If the ZQUANTIZ keyword is missing the default is to assume # no dithering, rather than whatever DEFAULT_QUANTIZE_METHOD # is set to quantize_method = self._header.get("ZQUANTIZ", NO_DITHER) if isinstance(quantize_method, str): for k, v in QUANTIZE_METHOD_NAMES.items(): if v.upper() == quantize_method: quantize_method = k break else: quantize_method = NO_DITHER if quantize_method == NO_DITHER: if "ZDITHER0" in self._header: # If dithering isn't being used then there's no reason to # keep the ZDITHER0 keyword del self._header["ZDITHER0"] else: if dither_seed: dither_seed = self._generate_dither_seed(dither_seed) elif "ZDITHER0" in self._header: dither_seed = self._header["ZDITHER0"] else: dither_seed = self._generate_dither_seed(DEFAULT_DITHER_SEED) self._header.set( "ZDITHER0", dither_seed, "dithering offset when quantizing floats", after="ZQUANTIZ", ) if image_header: # Move SIMPLE card from the image header to the # table header as ZSIMPLE card. if "SIMPLE" in image_header: self._header.set( "ZSIMPLE", image_header["SIMPLE"], image_header.comments["SIMPLE"], before="ZBITPIX", ) # Move EXTEND card from the image header to the # table header as ZEXTEND card. if "EXTEND" in image_header: self._header.set( "ZEXTEND", image_header["EXTEND"], image_header.comments["EXTEND"] ) # Move BLOCKED card from the image header to the # table header as ZBLOCKED card. if "BLOCKED" in image_header: self._header.set( "ZBLOCKED", image_header["BLOCKED"], image_header.comments["BLOCKED"], ) # Move XTENSION card from the image header to the # table header as ZTENSION card. # Since we only handle compressed IMAGEs, ZTENSION should # always be IMAGE, even if the caller has passed in a header # for some other type of extension. if "XTENSION" in image_header: self._header.set( "ZTENSION", "IMAGE", image_header.comments["XTENSION"], before="ZBITPIX", ) # Move PCOUNT and GCOUNT cards from image header to the table # header as ZPCOUNT and ZGCOUNT cards. if "PCOUNT" in image_header: self._header.set( "ZPCOUNT", image_header["PCOUNT"], image_header.comments["PCOUNT"], after=last_znaxis, ) if "GCOUNT" in image_header: self._header.set( "ZGCOUNT", image_header["GCOUNT"], image_header.comments["GCOUNT"], after="ZPCOUNT", ) # Move CHECKSUM and DATASUM cards from the image header to the # table header as XHECKSUM and XDATASUM cards. if "CHECKSUM" in image_header: self._header.set( "ZHECKSUM", image_header["CHECKSUM"], image_header.comments["CHECKSUM"], ) if "DATASUM" in image_header: self._header.set( "ZDATASUM", image_header["DATASUM"], image_header.comments["DATASUM"], ) else: # Move XTENSION card from the image header to the # table header as ZTENSION card. # Since we only handle compressed IMAGEs, ZTENSION should # always be IMAGE, even if the caller has passed in a header # for some other type of extension. if "XTENSION" in self._image_header: self._header.set( "ZTENSION", "IMAGE", self._image_header.comments["XTENSION"], before="ZBITPIX", ) # Move PCOUNT and GCOUNT cards from image header to the table # header as ZPCOUNT and ZGCOUNT cards. if "PCOUNT" in self._image_header: self._header.set( "ZPCOUNT", self._image_header["PCOUNT"], self._image_header.comments["PCOUNT"], after=last_znaxis, ) if "GCOUNT" in self._image_header: self._header.set( "ZGCOUNT", self._image_header["GCOUNT"], self._image_header.comments["GCOUNT"], after="ZPCOUNT", ) # When we have an image checksum we need to ensure that the same # number of blank cards exist in the table header as there were in # the image header. This allows those blank cards to be carried # over to the image header when the hdu is uncompressed. if "ZHECKSUM" in self._header: required_blanks = image_header._countblanks() image_blanks = self._image_header._countblanks() table_blanks = self._header._countblanks() for _ in range(required_blanks - image_blanks): self._image_header.append() table_blanks += 1 for _ in range(required_blanks - table_blanks): self._header.append() @lazyproperty def data(self): # The data attribute is the image data (not the table data). data = compression.decompress_hdu(self) if data is None: return data # Scale the data if necessary if self._orig_bzero != 0 or self._orig_bscale != 1: new_dtype = self._dtype_for_bitpix() data = np.array(data, dtype=new_dtype) zblank = None if "ZBLANK" in self.compressed_data.columns.names: zblank = self.compressed_data["ZBLANK"] else: if "ZBLANK" in self._header: zblank = np.array(self._header["ZBLANK"], dtype="int32") elif "BLANK" in self._header: zblank = np.array(self._header["BLANK"], dtype="int32") if zblank is not None: blanks = data == zblank if self._bscale != 1: np.multiply(data, self._bscale, data) if self._bzero != 0: # We have to explicitly cast self._bzero to prevent numpy from # raising an error when doing self.data += self._bzero, and we # do this instead of self.data = self.data + self._bzero to # avoid doubling memory usage. np.add(data, self._bzero, out=data, casting="unsafe") if zblank is not None: data = np.where(blanks, np.nan, data) # Right out of _ImageBaseHDU.data self._update_header_scale_info(data.dtype) return data @data.setter def data(self, data): if (data is not None) and ( not isinstance(data, np.ndarray) or data.dtype.fields is not None ): raise TypeError( "CompImageHDU data has incorrect type:{}; dtype.fields = {}".format( type(data), data.dtype.fields ) ) @lazyproperty def compressed_data(self): # First we will get the table data (the compressed # data) from the file, if there is any. compressed_data = super().data if isinstance(compressed_data, np.rec.recarray): # Make sure not to use 'del self.data' so we don't accidentally # go through the self.data.fdel and close the mmap underlying # the compressed_data array del self.__dict__["data"] return compressed_data else: # This will actually set self.compressed_data with the # pre-allocated space for the compression data; this is something I # might do away with in the future self._update_compressed_data() return self.compressed_data @compressed_data.deleter def compressed_data(self): # Deleting the compressed_data attribute has to be handled # with a little care to prevent a reference leak # First delete the ._coldefs attributes under it to break a possible # reference cycle if "compressed_data" in self.__dict__: del self.__dict__["compressed_data"]._coldefs # Now go ahead and delete from self.__dict__; normally # lazyproperty.__delete__ does this for us, but we can prempt it to # do some additional cleanup del self.__dict__["compressed_data"] # If this file was mmap'd, numpy.memmap will hold open a file # handle until the underlying mmap object is garbage-collected; # since this reference leak can sometimes hang around longer than # welcome go ahead and force a garbage collection gc.collect() @property def shape(self): """ Shape of the image array--should be equivalent to ``self.data.shape``. """ # Determine from the values read from the header return tuple(reversed(self._axes)) @lazyproperty def header(self): # The header attribute is the header for the image data. It # is not actually stored in the object dictionary. Instead, # the _image_header is stored. If the _image_header attribute # has already been defined we just return it. If not, we must # create it from the table header (the _header attribute). if hasattr(self, "_image_header"): return self._image_header # Clean up any possible doubled EXTNAME keywords that use # the default. Do this on the original header to ensure # duplicates are removed cleanly. self._remove_unnecessary_default_extnames(self._header) # Start with a copy of the table header. image_header = self._header.copy() # Delete cards that are related to the table. And move # the values of those cards that relate to the image from # their corresponding table cards. These include # ZBITPIX -> BITPIX, ZNAXIS -> NAXIS, and ZNAXISn -> NAXISn. # (Note: Used set here instead of list in case there are any duplicate # keywords, which there may be in some pathological cases: # https://github.com/astropy/astropy/issues/2750 for keyword in set(image_header): if CompImageHeader._is_reserved_keyword(keyword, warn=False): del image_header[keyword] if "ZSIMPLE" in self._header: image_header.set( "SIMPLE", self._header["ZSIMPLE"], self._header.comments["ZSIMPLE"], before=0, ) del image_header["XTENSION"] elif "ZTENSION" in self._header: if self._header["ZTENSION"] != "IMAGE": warnings.warn( "ZTENSION keyword in compressed extension != 'IMAGE'", AstropyUserWarning, ) image_header.set( "XTENSION", "IMAGE", self._header.comments["ZTENSION"], before=0 ) else: image_header.set("XTENSION", "IMAGE", before=0) image_header.set( "BITPIX", self._header["ZBITPIX"], self._header.comments["ZBITPIX"], before=1, ) image_header.set( "NAXIS", self._header["ZNAXIS"], self._header.comments["ZNAXIS"], before=2 ) last_naxis = "NAXIS" for idx in range(image_header["NAXIS"]): znaxis = "ZNAXIS" + str(idx + 1) naxis = znaxis[1:] image_header.set( naxis, self._header[znaxis], self._header.comments[znaxis], after=last_naxis, ) last_naxis = naxis # Delete any other spurious NAXISn keywords: naxis = image_header["NAXIS"] for keyword in list(image_header["NAXIS?*"]): try: n = int(keyword[5:]) except Exception: continue if n > naxis: del image_header[keyword] # Although PCOUNT and GCOUNT are considered mandatory for IMAGE HDUs, # ZPCOUNT and ZGCOUNT are optional, probably because for IMAGE HDUs # their values are always 0 and 1 respectively if "ZPCOUNT" in self._header: image_header.set( "PCOUNT", self._header["ZPCOUNT"], self._header.comments["ZPCOUNT"], after=last_naxis, ) else: image_header.set("PCOUNT", 0, after=last_naxis) if "ZGCOUNT" in self._header: image_header.set( "GCOUNT", self._header["ZGCOUNT"], self._header.comments["ZGCOUNT"], after="PCOUNT", ) else: image_header.set("GCOUNT", 1, after="PCOUNT") if "ZEXTEND" in self._header: image_header.set( "EXTEND", self._header["ZEXTEND"], self._header.comments["ZEXTEND"] ) if "ZBLOCKED" in self._header: image_header.set( "BLOCKED", self._header["ZBLOCKED"], self._header.comments["ZBLOCKED"] ) # Move the ZHECKSUM and ZDATASUM cards to the image header # as CHECKSUM and DATASUM if "ZHECKSUM" in self._header: image_header.set( "CHECKSUM", self._header["ZHECKSUM"], self._header.comments["ZHECKSUM"] ) if "ZDATASUM" in self._header: image_header.set( "DATASUM", self._header["ZDATASUM"], self._header.comments["ZDATASUM"] ) # Remove the EXTNAME card if the value in the table header # is the default value of COMPRESSED_IMAGE. if "EXTNAME" in image_header and image_header["EXTNAME"] == self._default_name: del image_header["EXTNAME"] # Remove the PCOUNT GCOUNT cards if the uncompressed header is # from a primary HDU if "SIMPLE" in image_header: del image_header["PCOUNT"] del image_header["GCOUNT"] # Look to see if there are any blank cards in the table # header. If there are, there should be the same number # of blank cards in the image header. Add blank cards to # the image header to make it so. table_blanks = self._header._countblanks() image_blanks = image_header._countblanks() for _ in range(table_blanks - image_blanks): image_header.append() # Create the CompImageHeader that syncs with the table header, and save # it off to self._image_header so it can be referenced later # unambiguously self._image_header = CompImageHeader(self._header, image_header) return self._image_header def _summary(self): """ Summarize the HDU: name, dimensions, and formats. """ class_name = self.__class__.__name__ # if data is touched, use data info. if self._data_loaded: if self.data is None: _shape, _format = (), "" else: # the shape will be in the order of NAXIS's which is the # reverse of the numarray shape _shape = list(self.data.shape) _format = self.data.dtype.name _shape.reverse() _shape = tuple(_shape) _format = _format[_format.rfind(".") + 1 :] # if data is not touched yet, use header info. else: _shape = () for idx in range(self.header["NAXIS"]): _shape += (self.header["NAXIS" + str(idx + 1)],) _format = BITPIX2DTYPE[self.header["BITPIX"]] return (self.name, self.ver, class_name, len(self.header), _shape, _format) def _update_compressed_data(self): """ Compress the image data so that it may be written to a file. """ # Check to see that the image_header matches the image data image_bitpix = DTYPE2BITPIX[self.data.dtype.name] if image_bitpix != self._orig_bitpix or self.data.shape != self.shape: self._update_header_data(self.header) # TODO: This is copied right out of _ImageBaseHDU._writedata_internal; # it would be cool if we could use an internal ImageHDU and use that to # write to a buffer for compression or something. See ticket #88 # deal with unsigned integer 16, 32 and 64 data old_data = self.data if _is_pseudo_integer(self.data.dtype): # Convert the unsigned array to signed self.data = np.array( self.data - _pseudo_zero(self.data.dtype), dtype=f"=i{self.data.dtype.itemsize}", ) should_swap = False else: should_swap = not self.data.dtype.isnative if should_swap: if self.data.flags.writeable: self.data.byteswap(True) else: # For read-only arrays, there is no way around making # a byteswapped copy of the data. self.data = self.data.byteswap(False) try: nrows = self._header["NAXIS2"] tbsize = self._header["NAXIS1"] * nrows self._header["PCOUNT"] = 0 if "THEAP" in self._header: del self._header["THEAP"] self._theap = tbsize # First delete the original compressed data, if it exists del self.compressed_data # Make sure that the data is contiguous otherwise CFITSIO # will not write the expected data self.data = np.ascontiguousarray(self.data) # Compress the data. # The current implementation of compress_hdu assumes the empty # compressed data table has already been initialized in # self.compressed_data, and writes directly to it # compress_hdu returns the size of the heap for the written # compressed image table heapsize, self.compressed_data = compression.compress_hdu(self) finally: # if data was byteswapped return it to its original order if should_swap: self.data.byteswap(True) self.data = old_data # CFITSIO will write the compressed data in big-endian order dtype = self.columns.dtype.newbyteorder(">") buf = self.compressed_data compressed_data = buf[: self._theap].view(dtype=dtype, type=np.rec.recarray) self.compressed_data = compressed_data.view(FITS_rec) self.compressed_data._coldefs = self.columns self.compressed_data._heapoffset = self._theap self.compressed_data._heapsize = heapsize def scale(self, type=None, option="old", bscale=1, bzero=0): """ Scale image data by using ``BSCALE`` and ``BZERO``. Calling this method will scale ``self.data`` and update the keywords of ``BSCALE`` and ``BZERO`` in ``self._header`` and ``self._image_header``. This method should only be used right before writing to the output file, as the data will be scaled and is therefore not very usable after the call. Parameters ---------- type : str, optional destination data type, use a string representing a numpy dtype name, (e.g. ``'uint8'``, ``'int16'``, ``'float32'`` etc.). If is `None`, use the current data type. option : str, optional how to scale the data: if ``"old"``, use the original ``BSCALE`` and ``BZERO`` values when the data was read/created. If ``"minmax"``, use the minimum and maximum of the data to scale. The option will be overwritten by any user-specified bscale/bzero values. bscale, bzero : int, optional user specified ``BSCALE`` and ``BZERO`` values. """ if self.data is None: return # Determine the destination (numpy) data type if type is None: type = BITPIX2DTYPE[self._bitpix] _type = getattr(np, type) # Determine how to scale the data # bscale and bzero takes priority if bscale != 1 or bzero != 0: _scale = bscale _zero = bzero else: if option == "old": _scale = self._orig_bscale _zero = self._orig_bzero elif option == "minmax": if isinstance(_type, np.floating): _scale = 1 _zero = 0 else: _min = np.minimum.reduce(self.data.flat) _max = np.maximum.reduce(self.data.flat) if _type == np.uint8: # uint8 case _zero = _min _scale = (_max - _min) / (2.0**8 - 1) else: _zero = (_max + _min) / 2.0 # throw away -2^N _scale = (_max - _min) / (2.0 ** (8 * _type.bytes) - 2) # Do the scaling if _zero != 0: # We have to explicitly cast self._bzero to prevent numpy from # raising an error when doing self.data -= _zero, and we # do this instead of self.data = self.data - _zero to # avoid doubling memory usage. np.subtract(self.data, _zero, out=self.data, casting="unsafe") self.header["BZERO"] = _zero else: # Delete from both headers for header in (self.header, self._header): with suppress(KeyError): del header["BZERO"] if _scale != 1: self.data /= _scale self.header["BSCALE"] = _scale else: for header in (self.header, self._header): with suppress(KeyError): del header["BSCALE"] if self.data.dtype.type != _type: self.data = np.array(np.around(self.data), dtype=_type) # 0.7.7.1 # Update the BITPIX Card to match the data self._bitpix = DTYPE2BITPIX[self.data.dtype.name] self._bzero = self.header.get("BZERO", 0) self._bscale = self.header.get("BSCALE", 1) # Update BITPIX for the image header specifically # TODO: Make this more clear by using self._image_header, but only once # this has been fixed so that the _image_header attribute is guaranteed # to be valid self.header["BITPIX"] = self._bitpix # Update the table header to match the scaled data self._update_header_data(self.header) # Since the image has been manually scaled, the current # bitpix/bzero/bscale now serve as the 'original' scaling of the image, # as though the original image has been completely replaced self._orig_bitpix = self._bitpix self._orig_bzero = self._bzero self._orig_bscale = self._bscale def _prewriteto(self, checksum=False, inplace=False): if self._scale_back: self.scale(BITPIX2DTYPE[self._orig_bitpix]) if self._has_data: self._update_compressed_data() # Use methods in the superclass to update the header with # scale/checksum keywords based on the data type of the image data self._update_pseudo_int_scale_keywords() # Shove the image header and data into a new ImageHDU and use that # to compute the image checksum image_hdu = ImageHDU(data=self.data, header=self.header) image_hdu._update_checksum(checksum) if "CHECKSUM" in image_hdu.header: # This will also pass through to the ZHECKSUM keyword and # ZDATASUM keyword self._image_header.set( "CHECKSUM", image_hdu.header["CHECKSUM"], image_hdu.header.comments["CHECKSUM"], ) if "DATASUM" in image_hdu.header: self._image_header.set( "DATASUM", image_hdu.header["DATASUM"], image_hdu.header.comments["DATASUM"], ) # Store a temporary backup of self.data in a different attribute; # see below self._imagedata = self.data # Now we need to perform an ugly hack to set the compressed data as # the .data attribute on the HDU so that the call to _writedata # handles it properly self.__dict__["data"] = self.compressed_data return super()._prewriteto(checksum=checksum, inplace=inplace) def _writeheader(self, fileobj): """ Bypasses `BinTableHDU._writeheader()` which updates the header with metadata about the data that is meaningless here; another reason why this class maybe shouldn't inherit directly from BinTableHDU... """ return ExtensionHDU._writeheader(self, fileobj) def _writedata(self, fileobj): """ Wrap the basic ``_writedata`` method to restore the ``.data`` attribute to the uncompressed image data in the case of an exception. """ try: return super()._writedata(fileobj) finally: # Restore the .data attribute to its rightful value (if any) if hasattr(self, "_imagedata"): self.__dict__["data"] = self._imagedata del self._imagedata else: del self.data def _close(self, closed=True): super()._close(closed=closed) # Also make sure to close access to the compressed data mmaps if ( closed and self._data_loaded and _get_array_mmap(self.compressed_data) is not None ): del self.compressed_data # TODO: This was copied right out of _ImageBaseHDU; get rid of it once we # find a way to rewrite this class as either a subclass or wrapper for an # ImageHDU def _dtype_for_bitpix(self): """ Determine the dtype that the data should be converted to depending on the BITPIX value in the header, and possibly on the BSCALE value as well. Returns None if there should not be any change. """ bitpix = self._orig_bitpix # Handle possible conversion to uints if enabled if self._uint and self._orig_bscale == 1: for bits, dtype in ( (16, np.dtype("uint16")), (32, np.dtype("uint32")), (64, np.dtype("uint64")), ): if bitpix == bits and self._orig_bzero == 1 << (bits - 1): return dtype if bitpix > 16: # scale integers to Float64 return np.dtype("float64") elif bitpix > 0: # scale integers to Float32 return np.dtype("float32") def _update_header_scale_info(self, dtype=None): if not self._do_not_scale_image_data and not ( self._orig_bzero == 0 and self._orig_bscale == 1 ): for keyword in ["BSCALE", "BZERO"]: # Make sure to delete from both the image header and the table # header; later this will be streamlined for header in (self.header, self._header): with suppress(KeyError): del header[keyword] # Since _update_header_scale_info can, currently, be # called *after* _prewriteto(), replace these with # blank cards so the header size doesn't change header.append() if dtype is None: dtype = self._dtype_for_bitpix() if dtype is not None: self.header["BITPIX"] = DTYPE2BITPIX[dtype.name] self._bzero = 0 self._bscale = 1 self._bitpix = self.header["BITPIX"] def _generate_dither_seed(self, seed): if not _is_int(seed): raise TypeError("Seed must be an integer") if not -1 <= seed <= 10000: raise ValueError( "Seed for random dithering must be either between 1 and " "10000 inclusive, 0 for autogeneration from the system " "clock, or -1 for autogeneration from a checksum of the first " "image tile (got {})".format(seed) ) if seed == DITHER_SEED_CHECKSUM: # Determine the tile dimensions from the ZTILEn keywords naxis = self._header["ZNAXIS"] tile_dims = [self._header[f"ZTILE{idx + 1}"] for idx in range(naxis)] tile_dims.reverse() # Get the first tile by using the tile dimensions as the end # indices of slices (starting from 0) first_tile = self.data[tuple(slice(d) for d in tile_dims)] # The checksum algorithm used is literally just the sum of the bytes # of the tile data (not its actual floating point values). Integer # overflow is irrelevant. csum = first_tile.view(dtype="uint8").sum() # Since CFITSIO uses an unsigned long (which may be different on # different platforms) go ahead and truncate the sum to its # unsigned long value and take the result modulo 10000 return (ctypes.c_ulong(csum).value % 10000) + 1 elif seed == DITHER_SEED_CLOCK: # This isn't exactly the same algorithm as CFITSIO, but that's okay # since the result is meant to be arbitrary. The primary difference # is that CFITSIO incorporates the HDU number into the result in # the hopes of heading off the possibility of the same seed being # generated for two HDUs at the same time. Here instead we just # add in the HDU object's id return ( (sum(int(x) for x in math.modf(time.time())) + id(self)) % 10000 ) + 1 else: return seed
ee1bd9b4ab3ab63ed0215542c1a0a5531e4b7543e5ed4789ece9c50651aec6d4
# Licensed under a 3-clause BSD style license - see PYFITS.rst import datetime import os import sys import warnings from contextlib import suppress from inspect import Parameter, signature import numpy as np from astropy.io.fits import conf from astropy.io.fits.file import _File from astropy.io.fits.header import Header, _BasicHeader, _DelayedHeader, _pad_length from astropy.io.fits.util import ( _extract_number, _free_space_check, _get_array_mmap, _is_int, _is_pseudo_integer, _pseudo_zero, decode_ascii, first, itersubclasses, ) from astropy.io.fits.verify import _ErrList, _Verify from astropy.utils import lazyproperty from astropy.utils.exceptions import AstropyUserWarning __all__ = [ "DELAYED", # classes "InvalidHDUException", "ExtensionHDU", "NonstandardExtHDU", ] class _Delayed: pass DELAYED = _Delayed() BITPIX2DTYPE = { 8: "uint8", 16: "int16", 32: "int32", 64: "int64", -32: "float32", -64: "float64", } """Maps FITS BITPIX values to Numpy dtype names.""" DTYPE2BITPIX = { "int8": 8, "uint8": 8, "int16": 16, "uint16": 16, "int32": 32, "uint32": 32, "int64": 64, "uint64": 64, "float32": -32, "float64": -64, } """ Maps Numpy dtype names to FITS BITPIX values (this includes unsigned integers, with the assumption that the pseudo-unsigned integer convention will be used in this case. """ class InvalidHDUException(Exception): """ A custom exception class used mainly to signal to _BaseHDU.__new__ that an HDU cannot possibly be considered valid, and must be assumed to be corrupted. """ def _hdu_class_from_header(cls, header): """ Iterates through the subclasses of _BaseHDU and uses that class's match_header() method to determine which subclass to instantiate. It's important to be aware that the class hierarchy is traversed in a depth-last order. Each match_header() should identify an HDU type as uniquely as possible. Abstract types may choose to simply return False or raise NotImplementedError to be skipped. If any unexpected exceptions are raised while evaluating match_header(), the type is taken to be _CorruptedHDU. Used primarily by _BaseHDU._readfrom_internal and _BaseHDU._from_data to find an appropriate HDU class to use based on values in the header. """ klass = cls # By default, if no subclasses are defined if header: for c in reversed(list(itersubclasses(cls))): try: # HDU classes built into astropy.io.fits are always considered, # but extension HDUs must be explicitly registered if not ( c.__module__.startswith("astropy.io.fits.") or c in cls._hdu_registry ): continue if c.match_header(header): klass = c break except NotImplementedError: continue except Exception as exc: warnings.warn( "An exception occurred matching an HDU header to the " "appropriate HDU type: {}".format(exc), AstropyUserWarning, ) warnings.warn( "The HDU will be treated as corrupted.", AstropyUserWarning ) klass = _CorruptedHDU del exc break return klass # TODO: Come up with a better __repr__ for HDUs (and for HDULists, for that # matter) class _BaseHDU: """Base class for all HDU (header data unit) classes.""" _hdu_registry = set() # This HDU type is part of the FITS standard _standard = True # Byte to use for padding out blocks _padding_byte = "\x00" _default_name = "" # _header uses a descriptor to delay the loading of the fits.Header object # until it is necessary. _header = _DelayedHeader() def __init__(self, data=None, header=None, *args, **kwargs): if header is None: header = Header() self._header = header self._header_str = None self._file = None self._buffer = None self._header_offset = None self._data_offset = None self._data_size = None # This internal variable is used to track whether the data attribute # still points to the same data array as when the HDU was originally # created (this does not track whether the data is actually the same # content-wise) self._data_replaced = False self._data_needs_rescale = False self._new = True self._output_checksum = False if "DATASUM" in self._header and "CHECKSUM" not in self._header: self._output_checksum = "datasum" elif "CHECKSUM" in self._header: self._output_checksum = True def __init_subclass__(cls, **kwargs): # Add the same data.deleter to all HDUs with a data property. # It's unfortunate, but there's otherwise no straightforward way # that a property can inherit setters/deleters of the property of the # same name on base classes. data_prop = cls.__dict__.get("data", None) if isinstance(data_prop, (lazyproperty, property)) and data_prop.fdel is None: # Don't do anything if the class has already explicitly # set the deleter for its data property def data(self): # The deleter if self._file is not None and self._data_loaded: data_refcount = sys.getrefcount(self.data) # Manually delete *now* so that FITS_rec.__del__ # cleanup can happen if applicable del self.__dict__["data"] # Don't even do this unless the *only* reference to the # .data array was the one we're deleting by deleting # this attribute; if any other references to the array # are hanging around (perhaps the user ran ``data = # hdu.data``) don't even consider this: if data_refcount == 2: self._file._maybe_close_mmap() setattr(cls, "data", data_prop.deleter(data)) return super().__init_subclass__(**kwargs) @property def header(self): return self._header @header.setter def header(self, value): self._header = value @property def name(self): # Convert the value to a string to be flexible in some pathological # cases (see ticket #96) return str(self._header.get("EXTNAME", self._default_name)) @name.setter def name(self, value): if not isinstance(value, str): raise TypeError("'name' attribute must be a string") if not conf.extension_name_case_sensitive: value = value.upper() if "EXTNAME" in self._header: self._header["EXTNAME"] = value else: self._header["EXTNAME"] = (value, "extension name") @property def ver(self): return self._header.get("EXTVER", 1) @ver.setter def ver(self, value): if not _is_int(value): raise TypeError("'ver' attribute must be an integer") if "EXTVER" in self._header: self._header["EXTVER"] = value else: self._header["EXTVER"] = (value, "extension value") @property def level(self): return self._header.get("EXTLEVEL", 1) @level.setter def level(self, value): if not _is_int(value): raise TypeError("'level' attribute must be an integer") if "EXTLEVEL" in self._header: self._header["EXTLEVEL"] = value else: self._header["EXTLEVEL"] = (value, "extension level") @property def is_image(self): return self.name == "PRIMARY" or ( "XTENSION" in self._header and ( self._header["XTENSION"] == "IMAGE" or ( self._header["XTENSION"] == "BINTABLE" and "ZIMAGE" in self._header and self._header["ZIMAGE"] is True ) ) ) @property def _data_loaded(self): return "data" in self.__dict__ and self.data is not DELAYED @property def _has_data(self): return self._data_loaded and self.data is not None @classmethod def register_hdu(cls, hducls): cls._hdu_registry.add(hducls) @classmethod def unregister_hdu(cls, hducls): if hducls in cls._hdu_registry: cls._hdu_registry.remove(hducls) @classmethod def match_header(cls, header): raise NotImplementedError @classmethod def fromstring(cls, data, checksum=False, ignore_missing_end=False, **kwargs): """ Creates a new HDU object of the appropriate type from a string containing the HDU's entire header and, optionally, its data. Note: When creating a new HDU from a string without a backing file object, the data of that HDU may be read-only. It depends on whether the underlying string was an immutable Python str/bytes object, or some kind of read-write memory buffer such as a `memoryview`. Parameters ---------- data : str, bytearray, memoryview, ndarray A byte string containing the HDU's header and data. checksum : bool, optional Check the HDU's checksum and/or datasum. ignore_missing_end : bool, optional Ignore a missing end card in the header data. Note that without the end card the end of the header may be ambiguous and resulted in a corrupt HDU. In this case the assumption is that the first 2880 block that does not begin with valid FITS header data is the beginning of the data. **kwargs : optional May consist of additional keyword arguments specific to an HDU type--these correspond to keywords recognized by the constructors of different HDU classes such as `PrimaryHDU`, `ImageHDU`, or `BinTableHDU`. Any unrecognized keyword arguments are simply ignored. """ return cls._readfrom_internal( data, checksum=checksum, ignore_missing_end=ignore_missing_end, **kwargs ) @classmethod def readfrom(cls, fileobj, checksum=False, ignore_missing_end=False, **kwargs): """ Read the HDU from a file. Normally an HDU should be opened with :func:`open` which reads the entire HDU list in a FITS file. But this method is still provided for symmetry with :func:`writeto`. Parameters ---------- fileobj : file-like Input FITS file. The file's seek pointer is assumed to be at the beginning of the HDU. checksum : bool If `True`, verifies that both ``DATASUM`` and ``CHECKSUM`` card values (when present in the HDU header) match the header and data of all HDU's in the file. ignore_missing_end : bool Do not issue an exception when opening a file that is missing an ``END`` card in the last header. """ # TODO: Figure out a way to make it possible for the _File # constructor to be a noop if the argument is already a _File if not isinstance(fileobj, _File): fileobj = _File(fileobj) hdu = cls._readfrom_internal( fileobj, checksum=checksum, ignore_missing_end=ignore_missing_end, **kwargs ) # If the checksum had to be checked the data may have already been read # from the file, in which case we don't want to seek relative fileobj.seek(hdu._data_offset + hdu._data_size, os.SEEK_SET) return hdu def writeto(self, name, output_verify="exception", overwrite=False, checksum=False): """ Write the HDU to a new file. This is a convenience method to provide a user easier output interface if only one HDU needs to be written to a file. Parameters ---------- name : path-like or file-like Output FITS file. If the file object is already opened, it must be opened in a writeable mode. output_verify : str Output verification option. Must be one of ``"fix"``, ``"silentfix"``, ``"ignore"``, ``"warn"``, or ``"exception"``. May also be any combination of ``"fix"`` or ``"silentfix"`` with ``"+ignore"``, ``+warn``, or ``+exception" (e.g. ``"fix+warn"``). See :ref:`astropy:verify` for more info. overwrite : bool, optional If ``True``, overwrite the output file if it exists. Raises an ``OSError`` if ``False`` and the output file exists. Default is ``False``. checksum : bool When `True` adds both ``DATASUM`` and ``CHECKSUM`` cards to the header of the HDU when written to the file. """ from .hdulist import HDUList hdulist = HDUList([self]) hdulist.writeto(name, output_verify, overwrite=overwrite, checksum=checksum) @classmethod def _from_data(cls, data, header, **kwargs): """ Instantiate the HDU object after guessing the HDU class from the FITS Header. """ klass = _hdu_class_from_header(cls, header) return klass(data=data, header=header, **kwargs) @classmethod def _readfrom_internal( cls, data, header=None, checksum=False, ignore_missing_end=False, **kwargs ): """ Provides the bulk of the internal implementation for readfrom and fromstring. For some special cases, supports using a header that was already created, and just using the input data for the actual array data. """ hdu_buffer = None hdu_fileobj = None header_offset = 0 if isinstance(data, _File): if header is None: header_offset = data.tell() try: # First we try to read the header with the fast parser # from _BasicHeader, which will read only the standard # 8 character keywords to get the structural keywords # that are needed to build the HDU object. header_str, header = _BasicHeader.fromfile(data) except Exception: # If the fast header parsing failed, then fallback to # the classic Header parser, which has better support # and reporting for the various issues that can be found # in the wild. data.seek(header_offset) header = Header.fromfile(data, endcard=not ignore_missing_end) hdu_fileobj = data data_offset = data.tell() # *after* reading the header else: try: # Test that the given object supports the buffer interface by # ensuring an ndarray can be created from it np.ndarray((), dtype="ubyte", buffer=data) except TypeError: raise TypeError( "The provided object {!r} does not contain an underlying " "memory buffer. fromstring() requires an object that " "supports the buffer interface such as bytes, buffer, " "memoryview, ndarray, etc. This restriction is to ensure " "that efficient access to the array/table data is possible.".format( data ) ) if header is None: def block_iter(nbytes): idx = 0 while idx < len(data): yield data[idx : idx + nbytes] idx += nbytes header_str, header = Header._from_blocks( block_iter, True, "", not ignore_missing_end, True ) if len(data) > len(header_str): hdu_buffer = data elif data: hdu_buffer = data header_offset = 0 data_offset = len(header_str) # Determine the appropriate arguments to pass to the constructor from # self._kwargs. self._kwargs contains any number of optional arguments # that may or may not be valid depending on the HDU type cls = _hdu_class_from_header(cls, header) sig = signature(cls.__init__) new_kwargs = kwargs.copy() if Parameter.VAR_KEYWORD not in (x.kind for x in sig.parameters.values()): # If __init__ accepts arbitrary keyword arguments, then we can go # ahead and pass all keyword arguments; otherwise we need to delete # any that are invalid for key in kwargs: if key not in sig.parameters: del new_kwargs[key] try: hdu = cls(data=DELAYED, header=header, **new_kwargs) except TypeError: # This may happen because some HDU class (e.g. GroupsHDU) wants # to set a keyword on the header, which is not possible with the # _BasicHeader. While HDU classes should not need to modify the # header in general, sometimes this is needed to fix it. So in # this case we build a full Header and try again to create the # HDU object. if isinstance(header, _BasicHeader): header = Header.fromstring(header_str) hdu = cls(data=DELAYED, header=header, **new_kwargs) else: raise # One of these may be None, depending on whether the data came from a # file or a string buffer--later this will be further abstracted hdu._file = hdu_fileobj hdu._buffer = hdu_buffer hdu._header_offset = header_offset # beginning of the header area hdu._data_offset = data_offset # beginning of the data area # data area size, including padding size = hdu.size hdu._data_size = size + _pad_length(size) if isinstance(hdu._header, _BasicHeader): # Delete the temporary _BasicHeader. # We need to do this before an eventual checksum computation, # since it needs to modify temporarily the header # # The header string is stored in the HDU._header_str attribute, # so that it can be used directly when we need to create the # classic Header object, without having to parse again the file. del hdu._header hdu._header_str = header_str # Checksums are not checked on invalid HDU types if checksum and checksum != "remove" and isinstance(hdu, _ValidHDU): hdu._verify_checksum_datasum() return hdu def _get_raw_data(self, shape, code, offset): """ Return raw array from either the HDU's memory buffer or underlying file. """ if isinstance(shape, int): shape = (shape,) if self._buffer: return np.ndarray(shape, dtype=code, buffer=self._buffer, offset=offset) elif self._file: return self._file.readarray(offset=offset, dtype=code, shape=shape) else: return None # TODO: Rework checksum handling so that it's not necessary to add a # checksum argument here # TODO: The BaseHDU class shouldn't even handle checksums since they're # only implemented on _ValidHDU... def _prewriteto(self, checksum=False, inplace=False): self._update_pseudo_int_scale_keywords() # Handle checksum self._update_checksum(checksum) def _update_pseudo_int_scale_keywords(self): """ If the data is signed int 8, unsigned int 16, 32, or 64, add BSCALE/BZERO cards to header. """ if self._has_data and self._standard and _is_pseudo_integer(self.data.dtype): # CompImageHDUs need TFIELDS immediately after GCOUNT, # so BSCALE has to go after TFIELDS if it exists. if "TFIELDS" in self._header: self._header.set("BSCALE", 1, after="TFIELDS") elif "GCOUNT" in self._header: self._header.set("BSCALE", 1, after="GCOUNT") else: self._header.set("BSCALE", 1) self._header.set("BZERO", _pseudo_zero(self.data.dtype), after="BSCALE") def _update_checksum( self, checksum, checksum_keyword="CHECKSUM", datasum_keyword="DATASUM" ): """Update the 'CHECKSUM' and 'DATASUM' keywords in the header (or keywords with equivalent semantics given by the ``checksum_keyword`` and ``datasum_keyword`` arguments--see for example ``CompImageHDU`` for an example of why this might need to be overridden). """ # If the data is loaded it isn't necessarily 'modified', but we have no # way of knowing for sure modified = self._header._modified or self._data_loaded if checksum == "remove": if checksum_keyword in self._header: del self._header[checksum_keyword] if datasum_keyword in self._header: del self._header[datasum_keyword] elif ( modified or self._new or ( checksum and ( "CHECKSUM" not in self._header or "DATASUM" not in self._header or not self._checksum_valid or not self._datasum_valid ) ) ): if checksum == "datasum": self.add_datasum(datasum_keyword=datasum_keyword) elif checksum: self.add_checksum( checksum_keyword=checksum_keyword, datasum_keyword=datasum_keyword ) def _postwriteto(self): # If data is unsigned integer 16, 32 or 64, remove the # BSCALE/BZERO cards if self._has_data and self._standard and _is_pseudo_integer(self.data.dtype): for keyword in ("BSCALE", "BZERO"): with suppress(KeyError): del self._header[keyword] def _writeheader(self, fileobj): offset = 0 with suppress(AttributeError, OSError): offset = fileobj.tell() self._header.tofile(fileobj) try: size = fileobj.tell() - offset except (AttributeError, OSError): size = len(str(self._header)) return offset, size def _writedata(self, fileobj): size = 0 fileobj.flush() try: offset = fileobj.tell() except (AttributeError, OSError): offset = 0 if self._data_loaded or self._data_needs_rescale: if self.data is not None: size += self._writedata_internal(fileobj) # pad the FITS data block # to avoid a bug in the lustre filesystem client, don't # write zero-byte objects if size > 0 and _pad_length(size) > 0: padding = _pad_length(size) * self._padding_byte # TODO: Not that this is ever likely, but if for some odd # reason _padding_byte is > 0x80 this will fail; but really if # somebody's custom fits format is doing that, they're doing it # wrong and should be reprimanded harshly. fileobj.write(padding.encode("ascii")) size += len(padding) else: # The data has not been modified or does not need need to be # rescaled, so it can be copied, unmodified, directly from an # existing file or buffer size += self._writedata_direct_copy(fileobj) # flush, to make sure the content is written fileobj.flush() # return both the location and the size of the data area return offset, size def _writedata_internal(self, fileobj): """ The beginning and end of most _writedata() implementations are the same, but the details of writing the data array itself can vary between HDU types, so that should be implemented in this method. Should return the size in bytes of the data written. """ fileobj.writearray(self.data) return self.data.size * self.data.itemsize def _writedata_direct_copy(self, fileobj): """Copies the data directly from one file/buffer to the new file. For now this is handled by loading the raw data from the existing data (including any padding) via a memory map or from an already in-memory buffer and using Numpy's existing file-writing facilities to write to the new file. If this proves too slow a more direct approach may be used. """ raw = self._get_raw_data(self._data_size, "ubyte", self._data_offset) if raw is not None: fileobj.writearray(raw) return raw.nbytes else: return 0 # TODO: This is the start of moving HDU writing out of the _File class; # Though right now this is an internal private method (though still used by # HDUList, eventually the plan is to have this be moved into writeto() # somehow... def _writeto(self, fileobj, inplace=False, copy=False): try: dirname = os.path.dirname(fileobj._file.name) except (AttributeError, TypeError): dirname = None with _free_space_check(self, dirname): self._writeto_internal(fileobj, inplace, copy) def _writeto_internal(self, fileobj, inplace, copy): # For now fileobj is assumed to be a _File object if not inplace or self._new: header_offset, _ = self._writeheader(fileobj) data_offset, data_size = self._writedata(fileobj) # Set the various data location attributes on newly-written HDUs if self._new: self._header_offset = header_offset self._data_offset = data_offset self._data_size = data_size return hdrloc = self._header_offset hdrsize = self._data_offset - self._header_offset datloc = self._data_offset datsize = self._data_size if self._header._modified: # Seek to the original header location in the file self._file.seek(hdrloc) # This should update hdrloc with he header location in the new file hdrloc, hdrsize = self._writeheader(fileobj) # If the data is to be written below with self._writedata, that # will also properly update the data location; but it should be # updated here too datloc = hdrloc + hdrsize elif copy: # Seek to the original header location in the file self._file.seek(hdrloc) # Before writing, update the hdrloc with the current file position, # which is the hdrloc for the new file hdrloc = fileobj.tell() fileobj.write(self._file.read(hdrsize)) # The header size is unchanged, but the data location may be # different from before depending on if previous HDUs were resized datloc = fileobj.tell() if self._data_loaded: if self.data is not None: # Seek through the array's bases for an memmap'd array; we # can't rely on the _File object to give us this info since # the user may have replaced the previous mmap'd array if copy or self._data_replaced: # Of course, if we're copying the data to a new file # we don't care about flushing the original mmap; # instead just read it into the new file array_mmap = None else: array_mmap = _get_array_mmap(self.data) if array_mmap is not None: array_mmap.flush() else: self._file.seek(self._data_offset) datloc, datsize = self._writedata(fileobj) elif copy: datsize = self._writedata_direct_copy(fileobj) self._header_offset = hdrloc self._data_offset = datloc self._data_size = datsize self._data_replaced = False def _close(self, closed=True): # If the data was mmap'd, close the underlying mmap (this will # prevent any future access to the .data attribute if there are # not other references to it; if there are other references then # it is up to the user to clean those up if closed and self._data_loaded and _get_array_mmap(self.data) is not None: del self.data # For backwards-compatibility, though nobody should have # been using this directly: _AllHDU = _BaseHDU # For convenience... # TODO: register_hdu could be made into a class decorator which would be pretty # cool, but only once 2.6 support is dropped. register_hdu = _BaseHDU.register_hdu unregister_hdu = _BaseHDU.unregister_hdu class _CorruptedHDU(_BaseHDU): """ A Corrupted HDU class. This class is used when one or more mandatory `Card`s are corrupted (unparsable), such as the ``BITPIX``, ``NAXIS``, or ``END`` cards. A corrupted HDU usually means that the data size cannot be calculated or the ``END`` card is not found. In the case of a missing ``END`` card, the `Header` may also contain the binary data .. note:: In future, it may be possible to decipher where the last block of the `Header` ends, but this task may be difficult when the extension is a `TableHDU` containing ASCII data. """ @property def size(self): """ Returns the size (in bytes) of the HDU's data part. """ # Note: On compressed files this might report a negative size; but the # file is corrupt anyways so I'm not too worried about it. if self._buffer is not None: return len(self._buffer) - self._data_offset return self._file.size - self._data_offset def _summary(self): return (self.name, self.ver, "CorruptedHDU") def verify(self): pass class _NonstandardHDU(_BaseHDU, _Verify): """ A Non-standard HDU class. This class is used for a Primary HDU when the ``SIMPLE`` Card has a value of `False`. A non-standard HDU comes from a file that resembles a FITS file but departs from the standards in some significant way. One example would be files where the numbers are in the DEC VAX internal storage format rather than the standard FITS most significant byte first. The header for this HDU should be valid. The data for this HDU is read from the file as a byte stream that begins at the first byte after the header ``END`` card and continues until the end of the file. """ _standard = False @classmethod def match_header(cls, header): """ Matches any HDU that has the 'SIMPLE' keyword but is not a standard Primary or Groups HDU. """ # The SIMPLE keyword must be in the first card card = header.cards[0] # The check that 'GROUPS' is missing is a bit redundant, since the # match_header for GroupsHDU will always be called before this one. if card.keyword == "SIMPLE": if "GROUPS" not in header and card.value is False: return True else: raise InvalidHDUException else: return False @property def size(self): """ Returns the size (in bytes) of the HDU's data part. """ if self._buffer is not None: return len(self._buffer) - self._data_offset return self._file.size - self._data_offset def _writedata(self, fileobj): """ Differs from the base class :class:`_writedata` in that it doesn't automatically add padding, and treats the data as a string of raw bytes instead of an array. """ offset = 0 size = 0 fileobj.flush() try: offset = fileobj.tell() except OSError: offset = 0 if self.data is not None: fileobj.write(self.data) # flush, to make sure the content is written fileobj.flush() size = len(self.data) # return both the location and the size of the data area return offset, size def _summary(self): return (self.name, self.ver, "NonstandardHDU", len(self._header)) @lazyproperty def data(self): """ Return the file data. """ return self._get_raw_data(self.size, "ubyte", self._data_offset) def _verify(self, option="warn"): errs = _ErrList([], unit="Card") # verify each card for card in self._header.cards: errs.append(card._verify(option)) return errs class _ValidHDU(_BaseHDU, _Verify): """ Base class for all HDUs which are not corrupted. """ def __init__(self, data=None, header=None, name=None, ver=None, **kwargs): super().__init__(data=data, header=header) if header is not None and not isinstance(header, (Header, _BasicHeader)): # TODO: Instead maybe try initializing a new Header object from # whatever is passed in as the header--there are various types # of objects that could work for this... raise ValueError("header must be a Header object") # NOTE: private data members _checksum and _datasum are used by the # utility script "fitscheck" to detect missing checksums. self._checksum = None self._checksum_valid = None self._datasum = None self._datasum_valid = None if name is not None: self.name = name if ver is not None: self.ver = ver @classmethod def match_header(cls, header): """ Matches any HDU that is not recognized as having either the SIMPLE or XTENSION keyword in its header's first card, but is nonetheless not corrupted. TODO: Maybe it would make more sense to use _NonstandardHDU in this case? Not sure... """ return first(header.keys()) not in ("SIMPLE", "XTENSION") @property def size(self): """ Size (in bytes) of the data portion of the HDU. """ return self._header.data_size def filebytes(self): """ Calculates and returns the number of bytes that this HDU will write to a file. """ f = _File() # TODO: Fix this once new HDU writing API is settled on return self._writeheader(f)[1] + self._writedata(f)[1] def fileinfo(self): """ Returns a dictionary detailing information about the locations of this HDU within any associated file. The values are only valid after a read or write of the associated file with no intervening changes to the `HDUList`. Returns ------- dict or None The dictionary details information about the locations of this HDU within an associated file. Returns `None` when the HDU is not associated with a file. Dictionary contents: ========== ================================================ Key Value ========== ================================================ file File object associated with the HDU filemode Mode in which the file was opened (readonly, copyonwrite, update, append, ostream) hdrLoc Starting byte location of header in file datLoc Starting byte location of data block in file datSpan Data size including padding ========== ================================================ """ if hasattr(self, "_file") and self._file: return { "file": self._file, "filemode": self._file.mode, "hdrLoc": self._header_offset, "datLoc": self._data_offset, "datSpan": self._data_size, } else: return None def copy(self): """ Make a copy of the HDU, both header and data are copied. """ if self.data is not None: data = self.data.copy() else: data = None return self.__class__(data=data, header=self._header.copy()) def _verify(self, option="warn"): errs = _ErrList([], unit="Card") is_valid = BITPIX2DTYPE.__contains__ # Verify location and value of mandatory keywords. # Do the first card here, instead of in the respective HDU classes, so # the checking is in order, in case of required cards in wrong order. if isinstance(self, ExtensionHDU): firstkey = "XTENSION" firstval = self._extension else: firstkey = "SIMPLE" firstval = True self.req_cards(firstkey, 0, None, firstval, option, errs) self.req_cards( "BITPIX", 1, lambda v: (_is_int(v) and is_valid(v)), 8, option, errs ) self.req_cards( "NAXIS", 2, lambda v: (_is_int(v) and 0 <= v <= 999), 0, option, errs ) naxis = self._header.get("NAXIS", 0) if naxis < 1000: for ax in range(3, naxis + 3): key = "NAXIS" + str(ax - 2) self.req_cards( key, ax, lambda v: (_is_int(v) and v >= 0), _extract_number(self._header[key], default=1), option, errs, ) # Remove NAXISj cards where j is not in range 1, naxis inclusive. for keyword in self._header: if keyword.startswith("NAXIS") and len(keyword) > 5: try: number = int(keyword[5:]) if number <= 0 or number > naxis: raise ValueError except ValueError: err_text = ( "NAXISj keyword out of range ('{}' when " "NAXIS == {})".format(keyword, naxis) ) def fix(self=self, keyword=keyword): del self._header[keyword] errs.append( self.run_option( option=option, err_text=err_text, fix=fix, fix_text="Deleted.", ) ) # Verify that the EXTNAME keyword exists and is a string if "EXTNAME" in self._header: if not isinstance(self._header["EXTNAME"], str): err_text = "The EXTNAME keyword must have a string value." fix_text = "Converted the EXTNAME keyword to a string value." def fix(header=self._header): header["EXTNAME"] = str(header["EXTNAME"]) errs.append( self.run_option( option, err_text=err_text, fix_text=fix_text, fix=fix ) ) # verify each card for card in self._header.cards: errs.append(card._verify(option)) return errs # TODO: Improve this API a little bit--for one, most of these arguments # could be optional def req_cards(self, keyword, pos, test, fix_value, option, errlist): """ Check the existence, location, and value of a required `Card`. Parameters ---------- keyword : str The keyword to validate pos : int, callable If an ``int``, this specifies the exact location this card should have in the header. Remember that Python is zero-indexed, so this means ``pos=0`` requires the card to be the first card in the header. If given a callable, it should take one argument--the actual position of the keyword--and return `True` or `False`. This can be used for custom evaluation. For example if ``pos=lambda idx: idx > 10`` this will check that the keyword's index is greater than 10. test : callable This should be a callable (generally a function) that is passed the value of the given keyword and returns `True` or `False`. This can be used to validate the value associated with the given keyword. fix_value : str, int, float, complex, bool, None A valid value for a FITS keyword to to use if the given ``test`` fails to replace an invalid value. In other words, this provides a default value to use as a replacement if the keyword's current value is invalid. If `None`, there is no replacement value and the keyword is unfixable. option : str Output verification option. Must be one of ``"fix"``, ``"silentfix"``, ``"ignore"``, ``"warn"``, or ``"exception"``. May also be any combination of ``"fix"`` or ``"silentfix"`` with ``"+ignore"``, ``+warn``, or ``+exception" (e.g. ``"fix+warn"``). See :ref:`astropy:verify` for more info. errlist : list A list of validation errors already found in the FITS file; this is used primarily for the validation system to collect errors across multiple HDUs and multiple calls to `req_cards`. Notes ----- If ``pos=None``, the card can be anywhere in the header. If the card does not exist, the new card will have the ``fix_value`` as its value when created. Also check the card's value by using the ``test`` argument. """ errs = errlist fix = None try: index = self._header.index(keyword) except ValueError: index = None fixable = fix_value is not None insert_pos = len(self._header) + 1 # If pos is an int, insert at the given position (and convert it to a # lambda) if _is_int(pos): insert_pos = pos pos = lambda x: x == insert_pos # if the card does not exist if index is None: err_text = f"'{keyword}' card does not exist." fix_text = f"Fixed by inserting a new '{keyword}' card." if fixable: # use repr to accommodate both string and non-string types # Boolean is also OK in this constructor card = (keyword, fix_value) def fix(self=self, insert_pos=insert_pos, card=card): self._header.insert(insert_pos, card) errs.append( self.run_option( option, err_text=err_text, fix_text=fix_text, fix=fix, fixable=fixable, ) ) else: # if the supposed location is specified if pos is not None: if not pos(index): err_text = f"'{keyword}' card at the wrong place (card {index})." fix_text = ( f"Fixed by moving it to the right place (card {insert_pos})." ) def fix(self=self, index=index, insert_pos=insert_pos): card = self._header.cards[index] del self._header[index] self._header.insert(insert_pos, card) errs.append( self.run_option( option, err_text=err_text, fix_text=fix_text, fix=fix ) ) # if value checking is specified if test: val = self._header[keyword] if not test(val): err_text = f"'{keyword}' card has invalid value '{val}'." fix_text = f"Fixed by setting a new value '{fix_value}'." if fixable: def fix(self=self, keyword=keyword, val=fix_value): self._header[keyword] = fix_value errs.append( self.run_option( option, err_text=err_text, fix_text=fix_text, fix=fix, fixable=fixable, ) ) return errs def add_datasum(self, when=None, datasum_keyword="DATASUM"): """ Add the ``DATASUM`` card to this HDU with the value set to the checksum calculated for the data. Parameters ---------- when : str, optional Comment string for the card that by default represents the time when the checksum was calculated datasum_keyword : str, optional The name of the header keyword to store the datasum value in; this is typically 'DATASUM' per convention, but there exist use cases in which a different keyword should be used Returns ------- checksum : int The calculated datasum Notes ----- For testing purposes, provide a ``when`` argument to enable the comment value in the card to remain consistent. This will enable the generation of a ``CHECKSUM`` card with a consistent value. """ cs = self._calculate_datasum() if when is None: when = f"data unit checksum updated {self._get_timestamp()}" self._header[datasum_keyword] = (str(cs), when) return cs def add_checksum( self, when=None, override_datasum=False, checksum_keyword="CHECKSUM", datasum_keyword="DATASUM", ): """ Add the ``CHECKSUM`` and ``DATASUM`` cards to this HDU with the values set to the checksum calculated for the HDU and the data respectively. The addition of the ``DATASUM`` card may be overridden. Parameters ---------- when : str, optional comment string for the cards; by default the comments will represent the time when the checksum was calculated override_datasum : bool, optional add the ``CHECKSUM`` card only checksum_keyword : str, optional The name of the header keyword to store the checksum value in; this is typically 'CHECKSUM' per convention, but there exist use cases in which a different keyword should be used datasum_keyword : str, optional See ``checksum_keyword`` Notes ----- For testing purposes, first call `add_datasum` with a ``when`` argument, then call `add_checksum` with a ``when`` argument and ``override_datasum`` set to `True`. This will provide consistent comments for both cards and enable the generation of a ``CHECKSUM`` card with a consistent value. """ if not override_datasum: # Calculate and add the data checksum to the header. data_cs = self.add_datasum(when, datasum_keyword=datasum_keyword) else: # Just calculate the data checksum data_cs = self._calculate_datasum() if when is None: when = f"HDU checksum updated {self._get_timestamp()}" # Add the CHECKSUM card to the header with a value of all zeros. if datasum_keyword in self._header: self._header.set(checksum_keyword, "0" * 16, when, before=datasum_keyword) else: self._header.set(checksum_keyword, "0" * 16, when) csum = self._calculate_checksum(data_cs, checksum_keyword=checksum_keyword) self._header[checksum_keyword] = csum def verify_datasum(self): """ Verify that the value in the ``DATASUM`` keyword matches the value calculated for the ``DATASUM`` of the current HDU data. Returns ------- valid : int - 0 - failure - 1 - success - 2 - no ``DATASUM`` keyword present """ if "DATASUM" in self._header: datasum = self._calculate_datasum() if datasum == int(self._header["DATASUM"]): return 1 else: # Failed return 0 else: return 2 def verify_checksum(self): """ Verify that the value in the ``CHECKSUM`` keyword matches the value calculated for the current HDU CHECKSUM. Returns ------- valid : int - 0 - failure - 1 - success - 2 - no ``CHECKSUM`` keyword present """ if "CHECKSUM" in self._header: if "DATASUM" in self._header: datasum = self._calculate_datasum() else: datasum = 0 checksum = self._calculate_checksum(datasum) if checksum == self._header["CHECKSUM"]: return 1 else: # Failed return 0 else: return 2 def _verify_checksum_datasum(self): """ Verify the checksum/datasum values if the cards exist in the header. Simply displays warnings if either the checksum or datasum don't match. """ if "CHECKSUM" in self._header: self._checksum = self._header["CHECKSUM"] self._checksum_valid = self.verify_checksum() if not self._checksum_valid: warnings.warn( "Checksum verification failed for HDU {}.\n".format( (self.name, self.ver) ), AstropyUserWarning, ) if "DATASUM" in self._header: self._datasum = self._header["DATASUM"] self._datasum_valid = self.verify_datasum() if not self._datasum_valid: warnings.warn( "Datasum verification failed for HDU {}.\n".format( (self.name, self.ver) ), AstropyUserWarning, ) def _get_timestamp(self): """ Return the current timestamp in ISO 8601 format, with microseconds stripped off. Ex.: 2007-05-30T19:05:11 """ return datetime.datetime.now().isoformat()[:19] def _calculate_datasum(self): """ Calculate the value for the ``DATASUM`` card in the HDU. """ if not self._data_loaded: # This is the case where the data has not been read from the file # yet. We find the data in the file, read it, and calculate the # datasum. if self.size > 0: raw_data = self._get_raw_data( self._data_size, "ubyte", self._data_offset ) return self._compute_checksum(raw_data) else: return 0 elif self.data is not None: return self._compute_checksum(self.data.view("ubyte")) else: return 0 def _calculate_checksum(self, datasum, checksum_keyword="CHECKSUM"): """ Calculate the value of the ``CHECKSUM`` card in the HDU. """ old_checksum = self._header[checksum_keyword] self._header[checksum_keyword] = "0" * 16 # Convert the header to bytes. s = self._header.tostring().encode("utf8") # Calculate the checksum of the Header and data. cs = self._compute_checksum(np.frombuffer(s, dtype="ubyte"), datasum) # Encode the checksum into a string. s = self._char_encode(~cs) # Return the header card value. self._header[checksum_keyword] = old_checksum return s def _compute_checksum(self, data, sum32=0): """ Compute the ones-complement checksum of a sequence of bytes. Parameters ---------- data a memory region to checksum sum32 incremental checksum value from another region Returns ------- ones complement checksum """ blocklen = 2880 sum32 = np.uint32(sum32) for i in range(0, len(data), blocklen): length = min(blocklen, len(data) - i) # ???? sum32 = self._compute_hdu_checksum(data[i : i + length], sum32) return sum32 def _compute_hdu_checksum(self, data, sum32=0): """ Translated from FITS Checksum Proposal by Seaman, Pence, and Rots. Use uint32 literals as a hedge against type promotion to int64. This code should only be called with blocks of 2880 bytes Longer blocks result in non-standard checksums with carry overflow Historically, this code *was* called with larger blocks and for that reason still needs to be for backward compatibility. """ u8 = np.uint32(8) u16 = np.uint32(16) uFFFF = np.uint32(0xFFFF) if data.nbytes % 2: last = data[-1] data = data[:-1] else: last = np.uint32(0) data = data.view(">u2") hi = sum32 >> u16 lo = sum32 & uFFFF hi += np.add.reduce(data[0::2], dtype=np.uint64) lo += np.add.reduce(data[1::2], dtype=np.uint64) if (data.nbytes // 2) % 2: lo += last << u8 else: hi += last << u8 hicarry = hi >> u16 locarry = lo >> u16 while hicarry or locarry: hi = (hi & uFFFF) + locarry lo = (lo & uFFFF) + hicarry hicarry = hi >> u16 locarry = lo >> u16 return (hi << u16) + lo # _MASK and _EXCLUDE used for encoding the checksum value into a character # string. _MASK = [0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF] _EXCLUDE = [ 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, ] def _encode_byte(self, byte): """ Encode a single byte. """ quotient = byte // 4 + ord("0") remainder = byte % 4 ch = np.array( [(quotient + remainder), quotient, quotient, quotient], dtype="int32" ) check = True while check: check = False for x in self._EXCLUDE: for j in [0, 2]: if ch[j] == x or ch[j + 1] == x: ch[j] += 1 ch[j + 1] -= 1 check = True return ch def _char_encode(self, value): """ Encodes the checksum ``value`` using the algorithm described in SPR section A.7.2 and returns it as a 16 character string. Parameters ---------- value a checksum Returns ------- ascii encoded checksum """ value = np.uint32(value) asc = np.zeros((16,), dtype="byte") ascii = np.zeros((16,), dtype="byte") for i in range(4): byte = (value & self._MASK[i]) >> ((3 - i) * 8) ch = self._encode_byte(byte) for j in range(4): asc[4 * j + i] = ch[j] for i in range(16): ascii[i] = asc[(i + 15) % 16] return decode_ascii(ascii.tobytes()) class ExtensionHDU(_ValidHDU): """ An extension HDU class. This class is the base class for the `TableHDU`, `ImageHDU`, and `BinTableHDU` classes. """ _extension = "" @classmethod def match_header(cls, header): """ This class should never be instantiated directly. Either a standard extension HDU type should be used for a specific extension, or NonstandardExtHDU should be used. """ raise NotImplementedError def writeto(self, name, output_verify="exception", overwrite=False, checksum=False): """ Works similarly to the normal writeto(), but prepends a default `PrimaryHDU` are required by extension HDUs (which cannot stand on their own). """ from .hdulist import HDUList from .image import PrimaryHDU hdulist = HDUList([PrimaryHDU(), self]) hdulist.writeto(name, output_verify, overwrite=overwrite, checksum=checksum) def _verify(self, option="warn"): errs = super()._verify(option=option) # Verify location and value of mandatory keywords. naxis = self._header.get("NAXIS", 0) self.req_cards( "PCOUNT", naxis + 3, lambda v: (_is_int(v) and v >= 0), 0, option, errs ) self.req_cards( "GCOUNT", naxis + 4, lambda v: (_is_int(v) and v == 1), 1, option, errs ) return errs # For backwards compatibility, though this needs to be deprecated # TODO: Mark this as deprecated _ExtensionHDU = ExtensionHDU class NonstandardExtHDU(ExtensionHDU): """ A Non-standard Extension HDU class. This class is used for an Extension HDU when the ``XTENSION`` `Card` has a non-standard value. In this case, Astropy can figure out how big the data is but not what it is. The data for this HDU is read from the file as a byte stream that begins at the first byte after the header ``END`` card and continues until the beginning of the next header or the end of the file. """ _standard = False @classmethod def match_header(cls, header): """ Matches any extension HDU that is not one of the standard extension HDU types. """ card = header.cards[0] xtension = card.value if isinstance(xtension, str): xtension = xtension.rstrip() # A3DTABLE is not really considered a 'standard' extension, as it was # sort of the prototype for BINTABLE; however, since our BINTABLE # implementation handles A3DTABLE HDUs it is listed here. standard_xtensions = ("IMAGE", "TABLE", "BINTABLE", "A3DTABLE") # The check that xtension is not one of the standard types should be # redundant. return card.keyword == "XTENSION" and xtension not in standard_xtensions def _summary(self): axes = tuple(self.data.shape) return (self.name, self.ver, "NonstandardExtHDU", len(self._header), axes) @lazyproperty def data(self): """ Return the file data. """ return self._get_raw_data(self.size, "ubyte", self._data_offset) # TODO: Mark this as deprecated _NonstandardExtHDU = NonstandardExtHDU
26f9e1af56a56b4a18fb59091b0e7e50fcd10b3d6257d056bb7083733a152ca5
# Licensed under a 3-clause BSD style license - see PYFITS.rst import gzip import itertools import os import re import shutil import sys import warnings import numpy as np from astropy.io.fits.file import FILE_MODES, _File from astropy.io.fits.header import _pad_length from astropy.io.fits.util import ( _free_space_check, _get_array_mmap, _is_int, _tmp_name, fileobj_closed, fileobj_mode, ignore_sigint, isfile, ) from astropy.io.fits.verify import VerifyError, VerifyWarning, _ErrList, _Verify from astropy.utils import indent # NOTE: Python can be built without bz2. from astropy.utils.compat.optional_deps import HAS_BZ2 from astropy.utils.exceptions import AstropyUserWarning from . import compressed from .base import ExtensionHDU, _BaseHDU, _NonstandardHDU, _ValidHDU from .groups import GroupsHDU from .image import ImageHDU, PrimaryHDU if HAS_BZ2: import bz2 __all__ = ["HDUList", "fitsopen"] # FITS file signature as per RFC 4047 FITS_SIGNATURE = b"SIMPLE = T" def fitsopen( name, mode="readonly", memmap=None, save_backup=False, cache=True, lazy_load_hdus=None, ignore_missing_simple=False, *, use_fsspec=None, fsspec_kwargs=None, **kwargs, ): """Factory function to open a FITS file and return an `HDUList` object. Parameters ---------- name : str, file-like or `pathlib.Path` File to be opened. mode : str, optional Open mode, 'readonly', 'update', 'append', 'denywrite', or 'ostream'. Default is 'readonly'. If ``name`` is a file object that is already opened, ``mode`` must match the mode the file was opened with, readonly (rb), update (rb+), append (ab+), ostream (w), denywrite (rb)). memmap : bool, optional Is memory mapping to be used? This value is obtained from the configuration item ``astropy.io.fits.Conf.use_memmap``. Default is `True`. save_backup : bool, optional If the file was opened in update or append mode, this ensures that a backup of the original file is saved before any changes are flushed. The backup has the same name as the original file with ".bak" appended. If "file.bak" already exists then "file.bak.1" is used, and so on. Default is `False`. cache : bool, optional If the file name is a URL, `~astropy.utils.data.download_file` is used to open the file. This specifies whether or not to save the file locally in Astropy's download cache. Default is `True`. lazy_load_hdus : bool, optional To avoid reading all the HDUs and headers in a FITS file immediately upon opening. This is an optimization especially useful for large files, as FITS has no way of determining the number and offsets of all the HDUs in a file without scanning through the file and reading all the headers. Default is `True`. To disable lazy loading and read all HDUs immediately (the old behavior) use ``lazy_load_hdus=False``. This can lead to fewer surprises--for example with lazy loading enabled, ``len(hdul)`` can be slow, as it means the entire FITS file needs to be read in order to determine the number of HDUs. ``lazy_load_hdus=False`` ensures that all HDUs have already been loaded after the file has been opened. .. versionadded:: 1.3 uint : bool, optional Interpret signed integer data where ``BZERO`` is the central value and ``BSCALE == 1`` as unsigned integer data. For example, ``int16`` data with ``BZERO = 32768`` and ``BSCALE = 1`` would be treated as ``uint16`` data. Default is `True` so that the pseudo-unsigned integer convention is assumed. ignore_missing_end : bool, optional Do not raise an exception when opening a file that is missing an ``END`` card in the last header. Default is `False`. ignore_missing_simple : bool, optional Do not raise an exception when the SIMPLE keyword is missing. Note that io.fits will raise a warning if a SIMPLE card is present but written in a way that does not follow the FITS Standard. Default is `False`. .. versionadded:: 4.2 checksum : bool, str, optional If `True`, verifies that both ``DATASUM`` and ``CHECKSUM`` card values (when present in the HDU header) match the header and data of all HDU's in the file. Updates to a file that already has a checksum will preserve and update the existing checksums unless this argument is given a value of 'remove', in which case the CHECKSUM and DATASUM values are not checked, and are removed when saving changes to the file. Default is `False`. disable_image_compression : bool, optional If `True`, treats compressed image HDU's like normal binary table HDU's. Default is `False`. do_not_scale_image_data : bool, optional If `True`, image data is not scaled using BSCALE/BZERO values when read. Default is `False`. character_as_bytes : bool, optional Whether to return bytes for string columns, otherwise unicode strings are returned, but this does not respect memory mapping and loads the whole column in memory when accessed. Default is `False`. ignore_blank : bool, optional If `True`, the BLANK keyword is ignored if present. Default is `False`. scale_back : bool, optional If `True`, when saving changes to a file that contained scaled image data, restore the data to the original type and reapply the original BSCALE/BZERO values. This could lead to loss of accuracy if scaling back to integer values after performing floating point operations on the data. Default is `False`. output_verify : str Output verification option. Must be one of ``"fix"``, ``"silentfix"``, ``"ignore"``, ``"warn"``, or ``"exception"``. May also be any combination of ``"fix"`` or ``"silentfix"`` with ``"+ignore"``, ``+warn``, or ``+exception" (e.g. ``"fix+warn"``). See :ref:`astropy:verify` for more info. use_fsspec : bool, optional Use `fsspec.open` to open the file? Defaults to `False` unless ``name`` starts with the Amazon S3 storage prefix ``s3://`` or the Google Cloud Storage prefix ``gs://``. Can also be used for paths with other prefixes (e.g., ``http://``) but in this case you must explicitely pass ``use_fsspec=True``. Use of this feature requires the optional ``fsspec`` package. A ``ModuleNotFoundError`` will be raised if the dependency is missing. .. versionadded:: 5.2 fsspec_kwargs : dict, optional Keyword arguments passed on to `fsspec.open`. This can be used to configure cloud storage credentials and caching behavior. For example, pass ``fsspec_kwargs={"anon": True}`` to enable anonymous access to Amazon S3 open data buckets. See ``fsspec``'s documentation for available parameters. .. versionadded:: 5.2 Returns ------- hdulist : `HDUList` `HDUList` containing all of the header data units in the file. """ from astropy.io.fits import conf if memmap is None: # distinguish between True (kwarg explicitly set) # and None (preference for memmap in config, might be ignored) memmap = None if conf.use_memmap else False else: memmap = bool(memmap) if lazy_load_hdus is None: lazy_load_hdus = conf.lazy_load_hdus else: lazy_load_hdus = bool(lazy_load_hdus) if "uint" not in kwargs: kwargs["uint"] = conf.enable_uint if not name: raise ValueError(f"Empty filename: {name!r}") return HDUList.fromfile( name, mode, memmap, save_backup, cache, lazy_load_hdus, ignore_missing_simple, use_fsspec=use_fsspec, fsspec_kwargs=fsspec_kwargs, **kwargs, ) class HDUList(list, _Verify): """ HDU list class. This is the top-level FITS object. When a FITS file is opened, a `HDUList` object is returned. """ def __init__(self, hdus=[], file=None): """ Construct a `HDUList` object. Parameters ---------- hdus : BaseHDU or sequence thereof, optional The HDU object(s) to comprise the `HDUList`. Should be instances of HDU classes like `ImageHDU` or `BinTableHDU`. file : file-like, bytes, optional The opened physical file associated with the `HDUList` or a bytes object containing the contents of the FITS file. """ if isinstance(file, bytes): self._data = file self._file = None else: self._file = file self._data = None # For internal use only--the keyword args passed to fitsopen / # HDUList.fromfile/string when opening the file self._open_kwargs = {} self._in_read_next_hdu = False # If we have read all the HDUs from the file or not # The assumes that all HDUs have been written when we first opened the # file; we do not currently support loading additional HDUs from a file # while it is being streamed to. In the future that might be supported # but for now this is only used for the purpose of lazy-loading of # existing HDUs. if file is None: self._read_all = True elif self._file is not None: # Should never attempt to read HDUs in ostream mode self._read_all = self._file.mode == "ostream" else: self._read_all = False if hdus is None: hdus = [] # can take one HDU, as well as a list of HDU's as input if isinstance(hdus, _ValidHDU): hdus = [hdus] elif not isinstance(hdus, (HDUList, list)): raise TypeError("Invalid input for HDUList.") for idx, hdu in enumerate(hdus): if not isinstance(hdu, _BaseHDU): raise TypeError(f"Element {idx} in the HDUList input is not an HDU.") super().__init__(hdus) if file is None: # Only do this when initializing from an existing list of HDUs # When initializing from a file, this will be handled by the # append method after the first HDU is read self.update_extend() def __len__(self): if not self._in_read_next_hdu: self.readall() return super().__len__() def __repr__(self): # Special case: if the FITS file is located on a remote file system # and has not been fully read yet, we return a simplified repr to # avoid downloading the entire file. We can tell that a file is remote # from the fact that the ``fsspec`` package was used to open it. is_fsspec_file = self._file and "fsspec" in str( self._file._file.__class__.__bases__ ) if not self._read_all and is_fsspec_file: return f"{type(self)} (partially read)" # In order to correctly repr an HDUList we need to load all the # HDUs as well self.readall() return super().__repr__() def __iter__(self): # While effectively this does the same as: # for idx in range(len(self)): # yield self[idx] # the more complicated structure is here to prevent the use of len(), # which would break the lazy loading for idx in itertools.count(): try: yield self[idx] except IndexError: break def __getitem__(self, key): """ Get an HDU from the `HDUList`, indexed by number or name. """ # If the key is a slice we need to make sure the necessary HDUs # have been loaded before passing the slice on to super. if isinstance(key, slice): max_idx = key.stop # Check for and handle the case when no maximum was # specified (e.g. [1:]). if max_idx is None: # We need all of the HDUs, so load them # and reset the maximum to the actual length. max_idx = len(self) # Just in case the max_idx is negative... max_idx = self._positive_index_of(max_idx) number_loaded = super().__len__() if max_idx >= number_loaded: # We need more than we have, try loading up to and including # max_idx. Note we do not try to be clever about skipping HDUs # even though key.step might conceivably allow it. for i in range(number_loaded, max_idx): # Read until max_idx or to the end of the file, whichever # comes first. if not self._read_next_hdu(): break try: hdus = super().__getitem__(key) except IndexError as e: # Raise a more helpful IndexError if the file was not fully read. if self._read_all: raise e else: raise IndexError( "HDU not found, possibly because the index " "is out of range, or because the file was " "closed before all HDUs were read" ) else: return HDUList(hdus) # Originally this used recursion, but hypothetically an HDU with # a very large number of HDUs could blow the stack, so use a loop # instead try: return self._try_while_unread_hdus( super().__getitem__, self._positive_index_of(key) ) except IndexError as e: # Raise a more helpful IndexError if the file was not fully read. if self._read_all: raise e else: raise IndexError( "HDU not found, possibly because the index " "is out of range, or because the file was " "closed before all HDUs were read" ) def __contains__(self, item): """ Returns `True` if ``item`` is an ``HDU`` _in_ ``self`` or a valid extension specification (e.g., integer extension number, extension name, or a tuple of extension name and an extension version) of a ``HDU`` in ``self``. """ try: self._try_while_unread_hdus(self.index_of, item) except (KeyError, ValueError): return False return True def __setitem__(self, key, hdu): """ Set an HDU to the `HDUList`, indexed by number or name. """ _key = self._positive_index_of(key) if isinstance(hdu, (slice, list)): if _is_int(_key): raise ValueError("An element in the HDUList must be an HDU.") for item in hdu: if not isinstance(item, _BaseHDU): raise ValueError(f"{item} is not an HDU.") else: if not isinstance(hdu, _BaseHDU): raise ValueError(f"{hdu} is not an HDU.") try: self._try_while_unread_hdus(super().__setitem__, _key, hdu) except IndexError: raise IndexError(f"Extension {key} is out of bound or not found.") self._resize = True self._truncate = False def __delitem__(self, key): """ Delete an HDU from the `HDUList`, indexed by number or name. """ if isinstance(key, slice): end_index = len(self) else: key = self._positive_index_of(key) end_index = len(self) - 1 self._try_while_unread_hdus(super().__delitem__, key) if key == end_index or key == -1 and not self._resize: self._truncate = True else: self._truncate = False self._resize = True # Support the 'with' statement def __enter__(self): return self def __exit__(self, type, value, traceback): output_verify = self._open_kwargs.get("output_verify", "exception") self.close(output_verify=output_verify) @classmethod def fromfile( cls, fileobj, mode=None, memmap=None, save_backup=False, cache=True, lazy_load_hdus=True, ignore_missing_simple=False, **kwargs, ): """ Creates an `HDUList` instance from a file-like object. The actual implementation of ``fitsopen()``, and generally shouldn't be used directly. Use :func:`open` instead (and see its documentation for details of the parameters accepted by this method). """ return cls._readfrom( fileobj=fileobj, mode=mode, memmap=memmap, save_backup=save_backup, cache=cache, ignore_missing_simple=ignore_missing_simple, lazy_load_hdus=lazy_load_hdus, **kwargs, ) @classmethod def fromstring(cls, data, **kwargs): """ Creates an `HDUList` instance from a string or other in-memory data buffer containing an entire FITS file. Similar to :meth:`HDUList.fromfile`, but does not accept the mode or memmap arguments, as they are only relevant to reading from a file on disk. This is useful for interfacing with other libraries such as CFITSIO, and may also be useful for streaming applications. Parameters ---------- data : str, buffer-like, etc. A string or other memory buffer containing an entire FITS file. Buffer-like objects include :class:`~bytes`, :class:`~bytearray`, :class:`~memoryview`, and :class:`~numpy.ndarray`. It should be noted that if that memory is read-only (such as a Python string) the returned :class:`HDUList`'s data portions will also be read-only. **kwargs : dict Optional keyword arguments. See :func:`astropy.io.fits.open` for details. Returns ------- hdul : HDUList An :class:`HDUList` object representing the in-memory FITS file. """ try: # Test that the given object supports the buffer interface by # ensuring an ndarray can be created from it np.ndarray((), dtype="ubyte", buffer=data) except TypeError: raise TypeError( "The provided object {} does not contain an underlying " "memory buffer. fromstring() requires an object that " "supports the buffer interface such as bytes, buffer, " "memoryview, ndarray, etc. This restriction is to ensure " "that efficient access to the array/table data is possible." "".format(data) ) return cls._readfrom(data=data, **kwargs) def fileinfo(self, index): """ Returns a dictionary detailing information about the locations of the indexed HDU within any associated file. The values are only valid after a read or write of the associated file with no intervening changes to the `HDUList`. Parameters ---------- index : int Index of HDU for which info is to be returned. Returns ------- fileinfo : dict or None The dictionary details information about the locations of the indexed HDU within an associated file. Returns `None` when the HDU is not associated with a file. Dictionary contents: ========== ======================================================== Key Value ========== ======================================================== file File object associated with the HDU filename Name of associated file object filemode Mode in which the file was opened (readonly, update, append, denywrite, ostream) resized Flag that when `True` indicates that the data has been resized since the last read/write so the returned values may not be valid. hdrLoc Starting byte location of header in file datLoc Starting byte location of data block in file datSpan Data size including padding ========== ======================================================== """ if self._file is not None: output = self[index].fileinfo() if not output: # OK, the HDU associated with this index is not yet # tied to the file associated with the HDUList. The only way # to get the file object is to check each of the HDU's in the # list until we find the one associated with the file. f = None for hdu in self: info = hdu.fileinfo() if info: f = info["file"] fm = info["filemode"] break output = { "file": f, "filemode": fm, "hdrLoc": None, "datLoc": None, "datSpan": None, } output["filename"] = self._file.name output["resized"] = self._wasresized() else: output = None return output def __copy__(self): """ Return a shallow copy of an HDUList. Returns ------- copy : `HDUList` A shallow copy of this `HDUList` object. """ return self[:] # Syntactic sugar for `__copy__()` magic method copy = __copy__ def __deepcopy__(self, memo=None): return HDUList([hdu.copy() for hdu in self]) def pop(self, index=-1): """Remove an item from the list and return it. Parameters ---------- index : int, str, tuple of (string, int), optional An integer value of ``index`` indicates the position from which ``pop()`` removes and returns an HDU. A string value or a tuple of ``(string, int)`` functions as a key for identifying the HDU to be removed and returned. If ``key`` is a tuple, it is of the form ``(key, ver)`` where ``ver`` is an ``EXTVER`` value that must match the HDU being searched for. If the key is ambiguous (e.g. there are multiple 'SCI' extensions) the first match is returned. For a more precise match use the ``(name, ver)`` pair. If even the ``(name, ver)`` pair is ambiguous the numeric index must be used to index the duplicate HDU. Returns ------- hdu : BaseHDU The HDU object at position indicated by ``index`` or having name and version specified by ``index``. """ # Make sure that HDUs are loaded before attempting to pop self.readall() list_index = self.index_of(index) return super().pop(list_index) def insert(self, index, hdu): """ Insert an HDU into the `HDUList` at the given ``index``. Parameters ---------- index : int Index before which to insert the new HDU. hdu : BaseHDU The HDU object to insert """ if not isinstance(hdu, _BaseHDU): raise ValueError(f"{hdu} is not an HDU.") num_hdus = len(self) if index == 0 or num_hdus == 0: if num_hdus != 0: # We are inserting a new Primary HDU so we need to # make the current Primary HDU into an extension HDU. if isinstance(self[0], GroupsHDU): raise ValueError( "The current Primary HDU is a GroupsHDU. " "It can't be made into an extension HDU, " "so another HDU cannot be inserted before it." ) hdu1 = ImageHDU(self[0].data, self[0].header) # Insert it into position 1, then delete HDU at position 0. super().insert(1, hdu1) super().__delitem__(0) if not isinstance(hdu, (PrimaryHDU, _NonstandardHDU)): # You passed in an Extension HDU but we need a Primary HDU. # If you provided an ImageHDU then we can convert it to # a primary HDU and use that. if isinstance(hdu, ImageHDU): hdu = PrimaryHDU(hdu.data, hdu.header) else: # You didn't provide an ImageHDU so we create a # simple Primary HDU and append that first before # we append the new Extension HDU. phdu = PrimaryHDU() super().insert(0, phdu) index = 1 else: if isinstance(hdu, GroupsHDU): raise ValueError("A GroupsHDU must be inserted as a Primary HDU.") if isinstance(hdu, PrimaryHDU): # You passed a Primary HDU but we need an Extension HDU # so create an Extension HDU from the input Primary HDU. hdu = ImageHDU(hdu.data, hdu.header) super().insert(index, hdu) hdu._new = True self._resize = True self._truncate = False # make sure the EXTEND keyword is in primary HDU if there is extension self.update_extend() def append(self, hdu): """ Append a new HDU to the `HDUList`. Parameters ---------- hdu : BaseHDU HDU to add to the `HDUList`. """ if not isinstance(hdu, _BaseHDU): raise ValueError("HDUList can only append an HDU.") if len(self) > 0: if isinstance(hdu, GroupsHDU): raise ValueError("Can't append a GroupsHDU to a non-empty HDUList") if isinstance(hdu, PrimaryHDU): # You passed a Primary HDU but we need an Extension HDU # so create an Extension HDU from the input Primary HDU. # TODO: This isn't necessarily sufficient to copy the HDU; # _header_offset and friends need to be copied too. hdu = ImageHDU(hdu.data, hdu.header) else: if not isinstance(hdu, (PrimaryHDU, _NonstandardHDU)): # You passed in an Extension HDU but we need a Primary # HDU. # If you provided an ImageHDU then we can convert it to # a primary HDU and use that. if isinstance(hdu, ImageHDU): hdu = PrimaryHDU(hdu.data, hdu.header) else: # You didn't provide an ImageHDU so we create a # simple Primary HDU and append that first before # we append the new Extension HDU. phdu = PrimaryHDU() super().append(phdu) super().append(hdu) hdu._new = True self._resize = True self._truncate = False # make sure the EXTEND keyword is in primary HDU if there is extension self.update_extend() def index_of(self, key): """ Get the index of an HDU from the `HDUList`. Parameters ---------- key : int, str, tuple of (string, int) or BaseHDU The key identifying the HDU. If ``key`` is a tuple, it is of the form ``(name, ver)`` where ``ver`` is an ``EXTVER`` value that must match the HDU being searched for. If the key is ambiguous (e.g. there are multiple 'SCI' extensions) the first match is returned. For a more precise match use the ``(name, ver)`` pair. If even the ``(name, ver)`` pair is ambiguous (it shouldn't be but it's not impossible) the numeric index must be used to index the duplicate HDU. When ``key`` is an HDU object, this function returns the index of that HDU object in the ``HDUList``. Returns ------- index : int The index of the HDU in the `HDUList`. Raises ------ ValueError If ``key`` is an HDU object and it is not found in the ``HDUList``. KeyError If an HDU specified by the ``key`` that is an extension number, extension name, or a tuple of extension name and version is not found in the ``HDUList``. """ if _is_int(key): return key elif isinstance(key, tuple): _key, _ver = key elif isinstance(key, _BaseHDU): return self.index(key) else: _key = key _ver = None if not isinstance(_key, str): raise KeyError( "{} indices must be integers, extension names as strings, " "or (extname, version) tuples; got {}" "".format(self.__class__.__name__, _key) ) _key = (_key.strip()).upper() found = None for idx, hdu in enumerate(self): name = hdu.name if isinstance(name, str): name = name.strip().upper() # 'PRIMARY' should always work as a reference to the first HDU if (name == _key or (_key == "PRIMARY" and idx == 0)) and ( _ver is None or _ver == hdu.ver ): found = idx break if found is None: raise KeyError(f"Extension {key!r} not found.") else: return found def _positive_index_of(self, key): """ Same as index_of, but ensures always returning a positive index or zero. (Really this should be called non_negative_index_of but it felt too long.) This means that if the key is a negative integer, we have to convert it to the corresponding positive index. This means knowing the length of the HDUList, which in turn means loading all HDUs. Therefore using negative indices on HDULists is inherently inefficient. """ index = self.index_of(key) if index >= 0: return index if abs(index) > len(self): raise IndexError(f"Extension {index} is out of bound or not found.") return len(self) + index def readall(self): """ Read data of all HDUs into memory. """ while self._read_next_hdu(): pass @ignore_sigint def flush(self, output_verify="fix", verbose=False): """ Force a write of the `HDUList` back to the file (for append and update modes only). Parameters ---------- output_verify : str Output verification option. Must be one of ``"fix"``, ``"silentfix"``, ``"ignore"``, ``"warn"``, or ``"exception"``. May also be any combination of ``"fix"`` or ``"silentfix"`` with ``"+ignore"``, ``+warn``, or ``+exception" (e.g. ``"fix+warn"``). See :ref:`astropy:verify` for more info. verbose : bool When `True`, print verbose messages """ if self._file.mode not in ("append", "update", "ostream"): warnings.warn( f"Flush for '{self._file.mode}' mode is not supported.", AstropyUserWarning, ) return save_backup = self._open_kwargs.get("save_backup", False) if save_backup and self._file.mode in ("append", "update"): filename = self._file.name if os.path.exists(filename): # The the file doesn't actually exist anymore for some reason # then there's no point in trying to make a backup backup = filename + ".bak" idx = 1 while os.path.exists(backup): backup = filename + ".bak." + str(idx) idx += 1 warnings.warn( f"Saving a backup of {filename} to {backup}.", AstropyUserWarning, ) try: shutil.copy(filename, backup) except OSError as exc: raise OSError( "Failed to save backup to destination {}: {}".format( filename, exc ) ) self.verify(option=output_verify) if self._file.mode in ("append", "ostream"): for hdu in self: if verbose: try: extver = str(hdu._header["extver"]) except KeyError: extver = "" # only append HDU's which are "new" if hdu._new: hdu._prewriteto(checksum=hdu._output_checksum) with _free_space_check(self): hdu._writeto(self._file) if verbose: print("append HDU", hdu.name, extver) hdu._new = False hdu._postwriteto() elif self._file.mode == "update": self._flush_update() def update_extend(self): """ Make sure that if the primary header needs the keyword ``EXTEND`` that it has it and it is correct. """ if not len(self): return if not isinstance(self[0], PrimaryHDU): # A PrimaryHDU will be automatically inserted at some point, but it # might not have been added yet return hdr = self[0].header def get_first_ext(): try: return self[1] except IndexError: return None if "EXTEND" in hdr: if not hdr["EXTEND"] and get_first_ext() is not None: hdr["EXTEND"] = True elif get_first_ext() is not None: if hdr["NAXIS"] == 0: hdr.set("EXTEND", True, after="NAXIS") else: n = hdr["NAXIS"] hdr.set("EXTEND", True, after="NAXIS" + str(n)) def writeto( self, fileobj, output_verify="exception", overwrite=False, checksum=False ): """ Write the `HDUList` to a new file. Parameters ---------- fileobj : str, file-like or `pathlib.Path` File to write to. If a file object, must be opened in a writeable mode. output_verify : str Output verification option. Must be one of ``"fix"``, ``"silentfix"``, ``"ignore"``, ``"warn"``, or ``"exception"``. May also be any combination of ``"fix"`` or ``"silentfix"`` with ``"+ignore"``, ``+warn``, or ``+exception" (e.g. ``"fix+warn"``). See :ref:`astropy:verify` for more info. overwrite : bool, optional If ``True``, overwrite the output file if it exists. Raises an ``OSError`` if ``False`` and the output file exists. Default is ``False``. checksum : bool When `True` adds both ``DATASUM`` and ``CHECKSUM`` cards to the headers of all HDU's written to the file. """ if len(self) == 0: warnings.warn("There is nothing to write.", AstropyUserWarning) return self.verify(option=output_verify) # make sure the EXTEND keyword is there if there is extension self.update_extend() # make note of whether the input file object is already open, in which # case we should not close it after writing (that should be the job # of the caller) closed = isinstance(fileobj, str) or fileobj_closed(fileobj) mode = FILE_MODES[fileobj_mode(fileobj)] if isfile(fileobj) else "ostream" # This can accept an open file object that's open to write only, or in # append/update modes but only if the file doesn't exist. fileobj = _File(fileobj, mode=mode, overwrite=overwrite) hdulist = self.fromfile(fileobj) try: dirname = os.path.dirname(hdulist._file.name) except (AttributeError, TypeError): dirname = None try: with _free_space_check(self, dirname=dirname): for hdu in self: hdu._prewriteto(checksum=checksum) hdu._writeto(hdulist._file) hdu._postwriteto() finally: hdulist.close(output_verify=output_verify, closed=closed) def close(self, output_verify="exception", verbose=False, closed=True): """ Close the associated FITS file and memmap object, if any. Parameters ---------- output_verify : str Output verification option. Must be one of ``"fix"``, ``"silentfix"``, ``"ignore"``, ``"warn"``, or ``"exception"``. May also be any combination of ``"fix"`` or ``"silentfix"`` with ``"+ignore"``, ``+warn``, or ``+exception" (e.g. ``"fix+warn"``). See :ref:`astropy:verify` for more info. verbose : bool When `True`, print out verbose messages. closed : bool When `True`, close the underlying file object. """ try: if ( self._file and self._file.mode in ("append", "update") and not self._file.closed ): self.flush(output_verify=output_verify, verbose=verbose) finally: if self._file and closed and hasattr(self._file, "close"): self._file.close() # Give individual HDUs an opportunity to do on-close cleanup for hdu in self: hdu._close(closed=closed) def info(self, output=None): """ Summarize the info of the HDUs in this `HDUList`. Note that this function prints its results to the console---it does not return a value. Parameters ---------- output : file-like or bool, optional A file-like object to write the output to. If `False`, does not output to a file and instead returns a list of tuples representing the HDU info. Writes to ``sys.stdout`` by default. """ if output is None: output = sys.stdout if self._file is None: name = "(No file associated with this HDUList)" else: name = self._file.name results = [ f"Filename: {name}", "No. Name Ver Type Cards Dimensions Format", ] format = "{:3d} {:10} {:3} {:11} {:5d} {} {} {}" default = ("", "", "", 0, (), "", "") for idx, hdu in enumerate(self): summary = hdu._summary() if len(summary) < len(default): summary += default[len(summary) :] summary = (idx,) + summary if output: results.append(format.format(*summary)) else: results.append(summary) if output: output.write("\n".join(results)) output.write("\n") output.flush() else: return results[2:] def filename(self): """ Return the file name associated with the HDUList object if one exists. Otherwise returns None. Returns ------- filename : str A string containing the file name associated with the HDUList object if an association exists. Otherwise returns None. """ if self._file is not None: if hasattr(self._file, "name"): return self._file.name return None @classmethod def _readfrom( cls, fileobj=None, data=None, mode=None, memmap=None, cache=True, lazy_load_hdus=True, ignore_missing_simple=False, *, use_fsspec=None, fsspec_kwargs=None, **kwargs, ): """ Provides the implementations from HDUList.fromfile and HDUList.fromstring, both of which wrap this method, as their implementations are largely the same. """ if fileobj is not None: if not isinstance(fileobj, _File): # instantiate a FITS file object (ffo) fileobj = _File( fileobj, mode=mode, memmap=memmap, cache=cache, use_fsspec=use_fsspec, fsspec_kwargs=fsspec_kwargs, ) # The Astropy mode is determined by the _File initializer if the # supplied mode was None mode = fileobj.mode hdulist = cls(file=fileobj) else: if mode is None: # The default mode mode = "readonly" hdulist = cls(file=data) # This method is currently only called from HDUList.fromstring and # HDUList.fromfile. If fileobj is None then this must be the # fromstring case; the data type of ``data`` will be checked in the # _BaseHDU.fromstring call. if ( not ignore_missing_simple and hdulist._file and hdulist._file.mode != "ostream" and hdulist._file.size > 0 ): pos = hdulist._file.tell() # FITS signature is supposed to be in the first 30 bytes, but to # allow reading various invalid files we will check in the first # card (80 bytes). simple = hdulist._file.read(80) match_sig = simple[:29] == FITS_SIGNATURE[:-1] and simple[29:30] in ( b"T", b"F", ) if not match_sig: # Check the SIMPLE card is there but not written correctly match_sig_relaxed = re.match(rb"SIMPLE\s*=\s*[T|F]", simple) if match_sig_relaxed: warnings.warn( "Found a SIMPLE card but its format doesn't" " respect the FITS Standard", VerifyWarning, ) else: if hdulist._file.close_on_error: hdulist._file.close() raise OSError( "No SIMPLE card found, this file does not appear to " "be a valid FITS file. If this is really a FITS file, " "try with ignore_missing_simple=True" ) hdulist._file.seek(pos) # Store additional keyword args that were passed to fits.open hdulist._open_kwargs = kwargs if fileobj is not None and fileobj.writeonly: # Output stream--not interested in reading/parsing # the HDUs--just writing to the output file return hdulist # Make sure at least the PRIMARY HDU can be read read_one = hdulist._read_next_hdu() # If we're trying to read only and no header units were found, # raise an exception if not read_one and mode in ("readonly", "denywrite"): # Close the file if necessary (issue #6168) if hdulist._file.close_on_error: hdulist._file.close() raise OSError("Empty or corrupt FITS file") if not lazy_load_hdus or kwargs.get("checksum") is True: # Go ahead and load all HDUs while hdulist._read_next_hdu(): pass # initialize/reset attributes to be used in "update/append" mode hdulist._resize = False hdulist._truncate = False return hdulist def _try_while_unread_hdus(self, func, *args, **kwargs): """ Attempt an operation that accesses an HDU by index/name that can fail if not all HDUs have been read yet. Keep reading HDUs until the operation succeeds or there are no more HDUs to read. """ while True: try: return func(*args, **kwargs) except Exception: if self._read_next_hdu(): continue else: raise def _read_next_hdu(self): """ Lazily load a single HDU from the fileobj or data string the `HDUList` was opened from, unless no further HDUs are found. Returns True if a new HDU was loaded, or False otherwise. """ if self._read_all: return False saved_compression_enabled = compressed.COMPRESSION_ENABLED fileobj, data, kwargs = self._file, self._data, self._open_kwargs if fileobj is not None and fileobj.closed: return False try: self._in_read_next_hdu = True if ( "disable_image_compression" in kwargs and kwargs["disable_image_compression"] ): compressed.COMPRESSION_ENABLED = False # read all HDUs try: if fileobj is not None: try: # Make sure we're back to the end of the last read # HDU if len(self) > 0: last = self[len(self) - 1] if last._data_offset is not None: offset = last._data_offset + last._data_size fileobj.seek(offset, os.SEEK_SET) hdu = _BaseHDU.readfrom(fileobj, **kwargs) except EOFError: self._read_all = True return False except OSError: # Close the file: see # https://github.com/astropy/astropy/issues/6168 # if self._file.close_on_error: self._file.close() if fileobj.writeonly: self._read_all = True return False else: raise else: if not data: self._read_all = True return False hdu = _BaseHDU.fromstring(data, **kwargs) self._data = data[hdu._data_offset + hdu._data_size :] super().append(hdu) if len(self) == 1: # Check for an extension HDU and update the EXTEND # keyword of the primary HDU accordingly self.update_extend() hdu._new = False if "checksum" in kwargs: hdu._output_checksum = kwargs["checksum"] # check in the case there is extra space after the last HDU or # corrupted HDU except (VerifyError, ValueError) as exc: warnings.warn( "Error validating header for HDU #{} (note: Astropy " "uses zero-based indexing).\n{}\n" "There may be extra bytes after the last HDU or the " "file is corrupted.".format(len(self), indent(str(exc))), VerifyWarning, ) del exc self._read_all = True return False finally: compressed.COMPRESSION_ENABLED = saved_compression_enabled self._in_read_next_hdu = False return True def _verify(self, option="warn"): errs = _ErrList([], unit="HDU") # the first (0th) element must be a primary HDU if ( len(self) > 0 and (not isinstance(self[0], PrimaryHDU)) and (not isinstance(self[0], _NonstandardHDU)) ): err_text = "HDUList's 0th element is not a primary HDU." fix_text = "Fixed by inserting one as 0th HDU." def fix(self=self): self.insert(0, PrimaryHDU()) err = self.run_option(option, err_text=err_text, fix_text=fix_text, fix=fix) errs.append(err) if len(self) > 1 and ( "EXTEND" not in self[0].header or self[0].header["EXTEND"] is not True ): err_text = ( "Primary HDU does not contain an EXTEND keyword " "equal to T even though there are extension HDUs." ) fix_text = "Fixed by inserting or updating the EXTEND keyword." def fix(header=self[0].header): naxis = header["NAXIS"] if naxis == 0: after = "NAXIS" else: after = "NAXIS" + str(naxis) header.set("EXTEND", value=True, after=after) errs.append( self.run_option(option, err_text=err_text, fix_text=fix_text, fix=fix) ) # each element calls their own verify for idx, hdu in enumerate(self): if idx > 0 and (not isinstance(hdu, ExtensionHDU)): err_text = f"HDUList's element {str(idx)} is not an extension HDU." err = self.run_option(option, err_text=err_text, fixable=False) errs.append(err) else: result = hdu._verify(option) if result: errs.append(result) return errs def _flush_update(self): """Implements flushing changes to a file in update mode.""" for hdu in self: # Need to all _prewriteto() for each HDU first to determine if # resizing will be necessary hdu._prewriteto(checksum=hdu._output_checksum, inplace=True) try: self._wasresized() # if the HDUList is resized, need to write out the entire contents of # the hdulist to the file. if self._resize or self._file.compression: self._flush_resize() else: # if not resized, update in place for hdu in self: hdu._writeto(self._file, inplace=True) # reset the modification attributes after updating for hdu in self: hdu._header._modified = False finally: for hdu in self: hdu._postwriteto() def _flush_resize(self): """ Implements flushing changes in update mode when parts of one or more HDU need to be resized. """ old_name = self._file.name old_memmap = self._file.memmap name = _tmp_name(old_name) if not self._file.file_like: old_mode = os.stat(old_name).st_mode # The underlying file is an actual file object. The HDUList is # resized, so we need to write it to a tmp file, delete the # original file, and rename the tmp file to the original file. if self._file.compression == "gzip": new_file = gzip.GzipFile(name, mode="ab+") elif self._file.compression == "bzip2": if not HAS_BZ2: raise ModuleNotFoundError( "This Python installation does not provide the bz2 module." ) new_file = bz2.BZ2File(name, mode="w") else: new_file = name with self.fromfile(new_file, mode="append") as hdulist: for hdu in self: hdu._writeto(hdulist._file, inplace=True, copy=True) if sys.platform.startswith("win"): # Collect a list of open mmaps to the data; this well be # used later. See below. mmaps = [ (idx, _get_array_mmap(hdu.data), hdu.data) for idx, hdu in enumerate(self) if hdu._has_data ] hdulist._file.close() self._file.close() if sys.platform.startswith("win"): # Close all open mmaps to the data. This is only necessary on # Windows, which will not allow a file to be renamed or deleted # until all handles to that file have been closed. for idx, mmap, arr in mmaps: if mmap is not None: mmap.close() os.remove(self._file.name) # reopen the renamed new file with "update" mode os.rename(name, old_name) os.chmod(old_name, old_mode) if isinstance(new_file, gzip.GzipFile): old_file = gzip.GzipFile(old_name, mode="rb+") else: old_file = old_name ffo = _File(old_file, mode="update", memmap=old_memmap) self._file = ffo for hdu in self: # Need to update the _file attribute and close any open mmaps # on each HDU if hdu._has_data and _get_array_mmap(hdu.data) is not None: del hdu.data hdu._file = ffo if sys.platform.startswith("win"): # On Windows, all the original data mmaps were closed above. # However, it's possible that the user still has references to # the old data which would no longer work (possibly even cause # a segfault if they try to access it). This replaces the # buffers used by the original arrays with the buffers of mmap # arrays created from the new file. This seems to work, but # it's a flaming hack and carries no guarantees that it won't # lead to odd behavior in practice. Better to just not keep # references to data from files that had to be resized upon # flushing (on Windows--again, this is no problem on Linux). for idx, mmap, arr in mmaps: if mmap is not None: # https://github.com/numpy/numpy/issues/8628 with warnings.catch_warnings(): warnings.simplefilter("ignore", category=DeprecationWarning) arr.data = self[idx].data.data del mmaps # Just to be sure else: # The underlying file is not a file object, it is a file like # object. We can't write out to a file, we must update the file # like object in place. To do this, we write out to a temporary # file, then delete the contents in our file like object, then # write the contents of the temporary file to the now empty file # like object. self.writeto(name) hdulist = self.fromfile(name) ffo = self._file ffo.truncate(0) ffo.seek(0) for hdu in hdulist: hdu._writeto(ffo, inplace=True, copy=True) # Close the temporary file and delete it. hdulist.close() os.remove(hdulist._file.name) # reset the resize attributes after updating self._resize = False self._truncate = False for hdu in self: hdu._header._modified = False hdu._new = False hdu._file = ffo def _wasresized(self, verbose=False): """ Determine if any changes to the HDUList will require a file resize when flushing the file. Side effect of setting the objects _resize attribute. """ if not self._resize: # determine if any of the HDU is resized for hdu in self: # Header: nbytes = len(str(hdu._header)) if nbytes != (hdu._data_offset - hdu._header_offset): self._resize = True self._truncate = False if verbose: print("One or more header is resized.") break # Data: if not hdu._has_data: continue nbytes = hdu.size nbytes = nbytes + _pad_length(nbytes) if nbytes != hdu._data_size: self._resize = True self._truncate = False if verbose: print("One or more data area is resized.") break if self._truncate: try: self._file.truncate(hdu._data_offset + hdu._data_size) except OSError: self._resize = True self._truncate = False return self._resize
94020cdcaf26b609ba95d32c35be3d942558c6db7688132106ccbc67fc57c0de
# Licensed under a 3-clause BSD style license - see PYFITS.rst import gzip import os from astropy.io.fits.file import _File from astropy.io.fits.header import _pad_length from astropy.io.fits.util import fileobj_name from .base import BITPIX2DTYPE, _BaseHDU from .hdulist import HDUList from .image import PrimaryHDU class StreamingHDU: """ A class that provides the capability to stream data to a FITS file instead of requiring data to all be written at once. The following pseudocode illustrates its use:: header = astropy.io.fits.Header() for all the cards you need in the header: header[key] = (value, comment) shdu = astropy.io.fits.StreamingHDU('filename.fits', header) for each piece of data: shdu.write(data) shdu.close() """ def __init__(self, name, header): """ Construct a `StreamingHDU` object given a file name and a header. Parameters ---------- name : path-like or file-like The file to which the header and data will be streamed. If opened, the file object must be opened in a writeable binary mode such as 'wb' or 'ab+'. header : `Header` instance The header object associated with the data to be written to the file. Notes ----- The file will be opened and the header appended to the end of the file. If the file does not already exist, it will be created, and if the header represents a Primary header, it will be written to the beginning of the file. If the file does not exist and the provided header is not a Primary header, a default Primary HDU will be inserted at the beginning of the file and the provided header will be added as the first extension. If the file does already exist, but the provided header represents a Primary header, the header will be modified to an image extension header and appended to the end of the file. """ if isinstance(name, gzip.GzipFile): raise TypeError("StreamingHDU not supported for GzipFile objects.") self._header = header.copy() # handle a file object instead of a file name filename = fileobj_name(name) or "" filename = os.path.expanduser(filename) # Check if the file already exists. If it does not, check to see # if we were provided with a Primary Header. If not we will need # to prepend a default PrimaryHDU to the file before writing the # given header. newfile = False if filename: if not os.path.exists(filename) or os.path.getsize(filename) == 0: newfile = True elif hasattr(name, "len") and name.len == 0: newfile = True if newfile: if "SIMPLE" not in self._header: hdulist = HDUList([PrimaryHDU()]) hdulist.writeto(name, "exception") else: # This will not be the first extension in the file so we # must change the Primary header provided into an image # extension header. if "SIMPLE" in self._header: self._header.set("XTENSION", "IMAGE", "Image extension", after="SIMPLE") del self._header["SIMPLE"] if "PCOUNT" not in self._header: dim = self._header["NAXIS"] if dim == 0: dim = "" else: dim = str(dim) self._header.set( "PCOUNT", 0, "number of parameters", after="NAXIS" + dim ) if "GCOUNT" not in self._header: self._header.set("GCOUNT", 1, "number of groups", after="PCOUNT") self._ffo = _File(name, "append") # TODO : Fix this once the HDU writing API is cleaned up tmp_hdu = _BaseHDU() # Passing self._header as an argument to _BaseHDU() will cause its # values to be modified in undesired ways...need to have a better way # of doing this tmp_hdu._header = self._header self._header_offset = tmp_hdu._writeheader(self._ffo)[0] self._data_offset = self._ffo.tell() self._size = self.size if self._size != 0: self.writecomplete = False else: self.writecomplete = True # Support the 'with' statement def __enter__(self): return self def __exit__(self, type, value, traceback): self.close() def write(self, data): """ Write the given data to the stream. Parameters ---------- data : ndarray Data to stream to the file. Returns ------- writecomplete : int Flag that when `True` indicates that all of the required data has been written to the stream. Notes ----- Only the amount of data specified in the header provided to the class constructor may be written to the stream. If the provided data would cause the stream to overflow, an `OSError` exception is raised and the data is not written. Once sufficient data has been written to the stream to satisfy the amount specified in the header, the stream is padded to fill a complete FITS block and no more data will be accepted. An attempt to write more data after the stream has been filled will raise an `OSError` exception. If the dtype of the input data does not match what is expected by the header, a `TypeError` exception is raised. """ size = self._ffo.tell() - self._data_offset if self.writecomplete or size + data.nbytes > self._size: raise OSError( "Attempt to write more data to the stream than the header specified." ) if BITPIX2DTYPE[self._header["BITPIX"]] != data.dtype.name: raise TypeError( "Supplied data does not match the type specified in the header." ) if data.dtype.str[0] != ">": # byteswap little endian arrays before writing output = data.byteswap() else: output = data self._ffo.writearray(output) if self._ffo.tell() - self._data_offset == self._size: # the stream is full so pad the data to the next FITS block self._ffo.write(_pad_length(self._size) * "\0") self.writecomplete = True self._ffo.flush() return self.writecomplete @property def size(self): """ Return the size (in bytes) of the data portion of the HDU. """ size = 0 naxis = self._header.get("NAXIS", 0) if naxis > 0: simple = self._header.get("SIMPLE", "F") random_groups = self._header.get("GROUPS", "F") if simple == "T" and random_groups == "T": groups = 1 else: groups = 0 size = 1 for idx in range(groups, naxis): size = size * self._header["NAXIS" + str(idx + 1)] bitpix = self._header["BITPIX"] gcount = self._header.get("GCOUNT", 1) pcount = self._header.get("PCOUNT", 0) size = abs(bitpix) * gcount * (pcount + size) // 8 return size def close(self): """ Close the physical FITS file. """ self._ffo.close()
6875e63f60cd07fb647db4b5ae0c51d9a60cb46fc7b8e1f09fab546da6c6b5cd
# Licensed under a 3-clause BSD style license - see PYFITS.rst import contextlib import copy import gc import pickle import re import sys import warnings import numpy as np import pytest from numpy import char as chararray try: import objgraph HAVE_OBJGRAPH = True except ImportError: HAVE_OBJGRAPH = False from astropy.io import fits from astropy.io.fits.column import NUMPY2FITS, ColumnAttribute, Delayed from astropy.io.fits.util import decode_ascii from astropy.io.fits.verify import VerifyError from astropy.table import Table from astropy.units import Unit, UnitsWarning, UnrecognizedUnit from astropy.utils.compat import NUMPY_LT_1_22, NUMPY_LT_1_22_1 from astropy.utils.exceptions import AstropyDeprecationWarning, AstropyUserWarning from .conftest import FitsTestCase def comparefloats(a, b): """ Compare two float scalars or arrays and see if they are consistent Consistency is determined ensuring the difference is less than the expected amount. Return True if consistent, False if any differences. """ aa = a bb = b # compute expected precision if aa.dtype.name == "float32" or bb.dtype.name == "float32": precision = 0.000001 else: precision = 0.0000000000000001 precision = 0.00001 # until precision problem is fixed in astropy.io.fits diff = np.absolute(aa - bb) mask0 = aa == 0 masknz = aa != 0.0 if np.any(mask0): if diff[mask0].max() != 0.0: return False if np.any(masknz): if (diff[masknz] / np.absolute(aa[masknz])).max() > precision: return False return True def comparerecords(a, b): """ Compare two record arrays Does this field by field, using approximation testing for float columns (Complex not yet handled.) Column names not compared, but column types and sizes are. """ nfieldsa = len(a.dtype.names) nfieldsb = len(b.dtype.names) if nfieldsa != nfieldsb: print("number of fields don't match") return False for i in range(nfieldsa): fielda = a.field(i) fieldb = b.field(i) if fielda.dtype.char == "S": fielda = decode_ascii(fielda) if fieldb.dtype.char == "S": fieldb = decode_ascii(fieldb) if not isinstance(fielda, type(fieldb)) and not isinstance( fieldb, type(fielda) ): print("type(fielda): ", type(fielda), " fielda: ", fielda) print("type(fieldb): ", type(fieldb), " fieldb: ", fieldb) print(f"field {i} type differs") return False if len(fielda) and isinstance(fielda[0], np.floating): if not comparefloats(fielda, fieldb): print("fielda: ", fielda) print("fieldb: ", fieldb) print(f"field {i} differs") return False elif isinstance(fielda, fits.column._VLF) or isinstance( fieldb, fits.column._VLF ): for row in range(len(fielda)): if np.any(fielda[row] != fieldb[row]): print(f"fielda[{row}]: {fielda[row]}") print(f"fieldb[{row}]: {fieldb[row]}") print(f"field {i} differs in row {row}") else: if np.any(fielda != fieldb): print("fielda: ", fielda) print("fieldb: ", fieldb) print(f"field {i} differs") return False return True def _assert_attr_col(new_tbhdu, tbhdu): """ Helper function to compare column attributes """ # Double check that the headers are equivalent assert tbhdu.columns.names == new_tbhdu.columns.names attrs = [ k for k, v in fits.Column.__dict__.items() if isinstance(v, ColumnAttribute) ] for name in tbhdu.columns.names: col = tbhdu.columns[name] new_col = new_tbhdu.columns[name] for attr in attrs: if getattr(col, attr) and getattr(new_col, attr): assert getattr(col, attr) == getattr(new_col, attr) class TestTableFunctions(FitsTestCase): def test_constructor_copies_header(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/153 Ensure that a header from one HDU is copied when used to initialize new HDU. This is like the test of the same name in test_image, but tests this for tables as well. """ ifd = fits.HDUList([fits.PrimaryHDU(), fits.BinTableHDU()]) thdr = ifd[1].header thdr["FILENAME"] = "labq01i3q_rawtag.fits" thdu = fits.BinTableHDU(header=thdr) ofd = fits.HDUList(thdu) ofd[0].header["FILENAME"] = "labq01i3q_flt.fits" # Original header should be unchanged assert thdr["FILENAME"] == "labq01i3q_rawtag.fits" def test_open(self, home_is_data): # open some existing FITS files: tt = fits.open(self.data("tb.fits")) fd = fits.open(self.data("test0.fits")) # create some local arrays a1 = chararray.array(["abc", "def", "xx"]) r1 = np.array([11.0, 12.0, 13.0], dtype=np.float32) # create a table from scratch, using a mixture of columns from existing # tables and locally created arrays: # first, create individual column definitions c1 = fits.Column(name="abc", format="3A", array=a1) c2 = fits.Column(name="def", format="E", array=r1) a3 = np.array([3, 4, 5], dtype="i2") c3 = fits.Column(name="xyz", format="I", array=a3) a4 = np.array([1, 2, 3], dtype="i2") c4 = fits.Column(name="t1", format="I", array=a4) a5 = np.array([3 + 3j, 4 + 4j, 5 + 5j], dtype="c8") c5 = fits.Column(name="t2", format="C", array=a5) # Note that X format must be two-D array a6 = np.array([[0], [1], [0]], dtype=np.uint8) c6 = fits.Column(name="t3", format="X", array=a6) a7 = np.array([101, 102, 103], dtype="i4") c7 = fits.Column(name="t4", format="J", array=a7) a8 = np.array( [ [1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1], [0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0], [1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1], ], dtype=np.uint8, ) c8 = fits.Column(name="t5", format="11X", array=a8) # second, create a column-definitions object for all columns in a table x = fits.ColDefs([c1, c2, c3, c4, c5, c6, c7, c8]) tbhdu = fits.BinTableHDU.from_columns(x) # another way to create a table is by using existing table's # information: x2 = fits.ColDefs(tt[1]) t2 = fits.BinTableHDU.from_columns(x2, nrows=2) ra = np.rec.array( [(1, "abc", 3.7000002861022949, 0), (2, "xy ", 6.6999998092651367, 1)], names="c1, c2, c3, c4", ) assert comparerecords(t2.data, ra) # the table HDU's data is a subclass of a record array, so we can # access one row like this: assert tbhdu.data[1][0] == a1[1] assert tbhdu.data[1][1] == r1[1] assert tbhdu.data[1][2] == a3[1] assert tbhdu.data[1][3] == a4[1] assert tbhdu.data[1][4] == a5[1] assert (tbhdu.data[1][5] == a6[1].view("bool")).all() assert tbhdu.data[1][6] == a7[1] assert (tbhdu.data[1][7] == a8[1]).all() # and a column like this: assert str(tbhdu.data.field("abc")) == "['abc' 'def' 'xx']" # An alternative way to create a column-definitions object is from an # existing table. _ = fits.ColDefs(tt[1]) # now we write out the newly created table HDU to a FITS file: fout = fits.HDUList(fits.PrimaryHDU()) fout.append(tbhdu) fout.writeto(self.temp("tableout1.fits"), overwrite=True) with fits.open(self.temp("tableout1.fits")) as f2: temp = f2[1].data.field(7) assert ( temp[0] == [ True, True, False, True, False, True, True, True, False, False, True, ] ).all() # An alternative way to create an output table FITS file: fout2 = fits.open(self.temp("tableout2.fits"), "append") fout2.append(fd[0]) fout2.append(tbhdu) fout2.close() tt.close() fd.close() def test_binary_table(self): # binary table: t = fits.open(self.data("tb.fits")) assert t[1].header["tform1"] == "1J" info = { "name": ["c1", "c2", "c3", "c4"], "format": ["1J", "3A", "1E", "1L"], "unit": ["", "", "", ""], "null": [-2147483647, "", "", ""], "bscale": ["", "", 3, ""], "bzero": ["", "", 0.4, ""], "disp": ["I11", "A3", "G15.7", "L6"], "start": ["", "", "", ""], "dim": ["", "", "", ""], "coord_inc": ["", "", "", ""], "coord_type": ["", "", "", ""], "coord_unit": ["", "", "", ""], "coord_ref_point": ["", "", "", ""], "coord_ref_value": ["", "", "", ""], "time_ref_pos": ["", "", "", ""], } assert t[1].columns.info(output=False) == info ra = np.rec.array( [(1, "abc", 3.7000002861022949, 0), (2, "xy ", 6.6999998092651367, 1)], names="c1, c2, c3, c4", ) assert comparerecords(t[1].data, ra[:2]) # Change scaled field and scale back to the original array t[1].data.field("c4")[0] = 1 t[1].data._scale_back() assert str(np.rec.recarray.field(t[1].data, "c4")) == "[84 84]" # look at data column-wise assert (t[1].data.field(0) == np.array([1, 2])).all() # When there are scaled columns, the raw data are in data._parent t.close() def test_ascii_table(self): # ASCII table a = fits.open(self.data("ascii.fits")) ra1 = np.rec.array( [ (10.123000144958496, 37), (5.1999998092651367, 23), (15.609999656677246, 17), (0.0, 0), (345.0, 345), ], names="c1, c2", ) assert comparerecords(a[1].data, ra1) # Test slicing a2 = a[1].data[2:][2:] ra2 = np.rec.array([(345.0, 345)], names="c1, c2") assert comparerecords(a2, ra2) assert (a2.field(1) == np.array([345])).all() ra3 = np.rec.array( [(10.123000144958496, 37), (15.609999656677246, 17), (345.0, 345)], names="c1, c2", ) assert comparerecords(a[1].data[::2], ra3) # Test Start Column a1 = chararray.array(["abcd", "def"]) r1 = np.array([11.0, 12.0]) c1 = fits.Column(name="abc", format="A3", start=19, array=a1) c2 = fits.Column(name="def", format="E", start=3, array=r1) c3 = fits.Column(name="t1", format="I", array=[91, 92, 93]) hdu = fits.TableHDU.from_columns([c2, c1, c3]) assert dict(hdu.data.dtype.fields) == { "abc": (np.dtype("|S3"), 18), "def": (np.dtype("|S15"), 2), "t1": (np.dtype("|S10"), 21), } hdu.writeto(self.temp("toto.fits"), overwrite=True) hdul = fits.open(self.temp("toto.fits")) assert comparerecords(hdu.data, hdul[1].data) hdul.close() # Test Scaling r1 = np.array([11.0, 12.0]) c2 = fits.Column(name="def", format="D", array=r1, bscale=2.3, bzero=0.6) hdu = fits.TableHDU.from_columns([c2]) hdu.writeto(self.temp("toto.fits"), overwrite=True) with open(self.temp("toto.fits")) as f: assert "4.95652173913043548D+00" in f.read() with fits.open(self.temp("toto.fits")) as hdul: assert comparerecords(hdu.data, hdul[1].data) # Test Integer precision according to width c1 = fits.Column(name="t2", format="I2", array=[91, 92, 93]) c2 = fits.Column(name="t4", format="I5", array=[91, 92, 93]) c3 = fits.Column(name="t8", format="I10", array=[91, 92, 93]) hdu = fits.TableHDU.from_columns([c1, c2, c3]) assert c1.array.dtype == np.int16 assert c2.array.dtype == np.int32 assert c3.array.dtype == np.int64 hdu.writeto(self.temp("toto.fits"), overwrite=True) with fits.open(self.temp("toto.fits")) as hdul: assert comparerecords(hdu.data, hdul[1].data) a.close() def test_endianness(self): x = np.ndarray((1,), dtype=object) channelsIn = np.array([3], dtype="uint8") x[0] = channelsIn col = fits.Column(name="Channels", format="PB()", array=x) cols = fits.ColDefs([col]) tbhdu = fits.BinTableHDU.from_columns(cols) tbhdu.name = "RFI" tbhdu.writeto(self.temp("testendian.fits"), overwrite=True) hduL = fits.open(self.temp("testendian.fits")) rfiHDU = hduL["RFI"] data = rfiHDU.data channelsOut = data.field("Channels")[0] assert (channelsIn == channelsOut).all() hduL.close() def test_column_endianness(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/77 (Astropy doesn't preserve byte order of non-native order column arrays) """ a = [1.0, 2.0, 3.0, 4.0] a1 = np.array(a, dtype="<f8") a2 = np.array(a, dtype=">f8") col1 = fits.Column(name="a", format="D", array=a1) col2 = fits.Column(name="b", format="D", array=a2) cols = fits.ColDefs([col1, col2]) tbhdu = fits.BinTableHDU.from_columns(cols) assert (tbhdu.data["a"] == a1).all() assert (tbhdu.data["b"] == a2).all() # Double check that the array is converted to the correct byte-order # for FITS (big-endian). tbhdu.writeto(self.temp("testendian.fits"), overwrite=True) with fits.open(self.temp("testendian.fits")) as hdul: assert (hdul[1].data["a"] == a2).all() assert (hdul[1].data["b"] == a2).all() def test_recarray_to_bintablehdu(self): bright = np.rec.array( [ (1, "Serius", -1.45, "A1V"), (2, "Canopys", -0.73, "F0Ib"), (3, "Rigil Kent", -0.1, "G2V"), ], formats="int16,a20,float32,a10", names="order,name,mag,Sp", ) hdu = fits.BinTableHDU(bright) assert comparerecords(hdu.data, bright) hdu.writeto(self.temp("toto.fits"), overwrite=True) hdul = fits.open(self.temp("toto.fits")) assert comparerecords(hdu.data, hdul[1].data) assert comparerecords(bright, hdul[1].data) hdul.close() def test_numpy_ndarray_to_bintablehdu(self): desc = np.dtype( { "names": ["order", "name", "mag", "Sp"], "formats": ["int", "S20", "float32", "S10"], } ) a = np.array( [ (1, "Serius", -1.45, "A1V"), (2, "Canopys", -0.73, "F0Ib"), (3, "Rigil Kent", -0.1, "G2V"), ], dtype=desc, ) hdu = fits.BinTableHDU(a) assert comparerecords(hdu.data, a.view(fits.FITS_rec)) hdu.writeto(self.temp("toto.fits"), overwrite=True) hdul = fits.open(self.temp("toto.fits")) assert comparerecords(hdu.data, hdul[1].data) hdul.close() def test_numpy_ndarray_to_bintablehdu_with_unicode(self): desc = np.dtype( { "names": ["order", "name", "mag", "Sp"], "formats": ["int", "U20", "float32", "U10"], } ) a = np.array( [ (1, "Serius", -1.45, "A1V"), (2, "Canopys", -0.73, "F0Ib"), (3, "Rigil Kent", -0.1, "G2V"), ], dtype=desc, ) hdu = fits.BinTableHDU(a) assert comparerecords(hdu.data, a.view(fits.FITS_rec)) hdu.writeto(self.temp("toto.fits"), overwrite=True) hdul = fits.open(self.temp("toto.fits")) assert comparerecords(hdu.data, hdul[1].data) hdul.close() def test_new_table_from_recarray(self): bright = np.rec.array( [ (1, "Serius", -1.45, "A1V"), (2, "Canopys", -0.73, "F0Ib"), (3, "Rigil Kent", -0.1, "G2V"), ], formats="int16,a20,float64,a10", names="order,name,mag,Sp", ) hdu = fits.TableHDU.from_columns(bright, nrows=2) # Verify that all ndarray objects within the HDU reference the # same ndarray. assert id(hdu.data._coldefs.columns[0].array) == id( hdu.data._coldefs._arrays[0] ) assert id(hdu.data._coldefs.columns[0].array) == id( hdu.columns.columns[0].array ) assert id(hdu.data._coldefs.columns[0].array) == id(hdu.columns._arrays[0]) # Ensure I can change the value of one data element and it effects # all of the others. hdu.data[0][0] = 213 assert hdu.data[0][0] == 213 assert hdu.data._coldefs._arrays[0][0] == 213 assert hdu.data._coldefs.columns[0].array[0] == 213 assert hdu.columns._arrays[0][0] == 213 assert hdu.columns.columns[0].array[0] == 213 hdu.data._coldefs._arrays[0][0] = 100 assert hdu.data[0][0] == 100 assert hdu.data._coldefs._arrays[0][0] == 100 assert hdu.data._coldefs.columns[0].array[0] == 100 assert hdu.columns._arrays[0][0] == 100 assert hdu.columns.columns[0].array[0] == 100 hdu.data._coldefs.columns[0].array[0] = 500 assert hdu.data[0][0] == 500 assert hdu.data._coldefs._arrays[0][0] == 500 assert hdu.data._coldefs.columns[0].array[0] == 500 assert hdu.columns._arrays[0][0] == 500 assert hdu.columns.columns[0].array[0] == 500 hdu.columns._arrays[0][0] = 600 assert hdu.data[0][0] == 600 assert hdu.data._coldefs._arrays[0][0] == 600 assert hdu.data._coldefs.columns[0].array[0] == 600 assert hdu.columns._arrays[0][0] == 600 assert hdu.columns.columns[0].array[0] == 600 hdu.columns.columns[0].array[0] = 800 assert hdu.data[0][0] == 800 assert hdu.data._coldefs._arrays[0][0] == 800 assert hdu.data._coldefs.columns[0].array[0] == 800 assert hdu.columns._arrays[0][0] == 800 assert hdu.columns.columns[0].array[0] == 800 assert (hdu.data.field(0) == np.array([800, 2], dtype=np.int16)).all() assert hdu.data[0][1] == "Serius" assert hdu.data[1][1] == "Canopys" assert (hdu.data.field(2) == np.array([-1.45, -0.73], dtype=np.float64)).all() assert hdu.data[0][3] == "A1V" assert hdu.data[1][3] == "F0Ib" hdu.writeto(self.temp("toto.fits"), overwrite=True) with fits.open(self.temp("toto.fits")) as hdul: assert (hdul[1].data.field(0) == np.array([800, 2], dtype=np.int16)).all() assert hdul[1].data[0][1] == "Serius" assert hdul[1].data[1][1] == "Canopys" assert ( hdul[1].data.field(2) == np.array([-1.45, -0.73], dtype=np.float64) ).all() assert hdul[1].data[0][3] == "A1V" assert hdul[1].data[1][3] == "F0Ib" del hdul hdu = fits.BinTableHDU.from_columns(bright, nrows=2) tmp = np.rec.array( [(1, "Serius", -1.45, "A1V"), (2, "Canopys", -0.73, "F0Ib")], formats="int16,a20,float64,a10", names="order,name,mag,Sp", ) assert comparerecords(hdu.data, tmp) hdu.writeto(self.temp("toto.fits"), overwrite=True) with fits.open(self.temp("toto.fits")) as hdul: assert comparerecords(hdu.data, hdul[1].data) def test_new_fitsrec(self): """ Tests creating a new FITS_rec object from a multi-field ndarray. """ with fits.open(self.data("tb.fits")) as h: data = h[1].data new_data = np.array([(3, "qwe", 4.5, False)], dtype=data.dtype) appended = np.append(data, new_data).view(fits.FITS_rec) assert repr(appended).startswith("FITS_rec(") # This test used to check the entire string representation of FITS_rec, # but that has problems between different numpy versions. Instead just # check that the FITS_rec was created, and we'll let subsequent tests # worry about checking values and such def test_appending_a_column(self): counts = np.array([312, 334, 308, 317]) names = np.array(["NGC1", "NGC2", "NGC3", "NCG4"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[1, 0, 1, 1]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu = fits.BinTableHDU.from_columns(coldefs) tbhdu.writeto(self.temp("table1.fits")) counts = np.array([412, 434, 408, 417]) names = np.array(["NGC5", "NGC6", "NGC7", "NCG8"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[0, 1, 0, 0]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu = fits.BinTableHDU.from_columns(coldefs) tbhdu.writeto(self.temp("table2.fits")) # Append the rows of table 2 after the rows of table 1 # The column definitions are assumed to be the same # Open the two files we want to append t1 = fits.open(self.temp("table1.fits")) t2 = fits.open(self.temp("table2.fits")) # Get the number of rows in the table from the first file nrows1 = t1[1].data.shape[0] # Get the total number of rows in the resulting appended table nrows = t1[1].data.shape[0] + t2[1].data.shape[0] assert t1[1].columns._arrays[1] is t1[1].columns.columns[1].array # Create a new table that consists of the data from the first table # but has enough space in the ndarray to hold the data from both tables hdu = fits.BinTableHDU.from_columns(t1[1].columns, nrows=nrows) # For each column in the tables append the data from table 2 after the # data from table 1. for i in range(len(t1[1].columns)): hdu.data.field(i)[nrows1:] = t2[1].data.field(i) hdu.writeto(self.temp("newtable.fits")) info = [ (0, "PRIMARY", 1, "PrimaryHDU", 4, (), "", ""), (1, "", 1, "BinTableHDU", 19, "8R x 5C", "[10A, J, 10A, 5E, L]", ""), ] assert fits.info(self.temp("newtable.fits"), output=False) == info z = np.array([0.0, 0.0, 0.0, 0.0, 0.0], dtype=np.float32) array = np.rec.array( [ ("NGC1", 312, "", z, True), ("NGC2", 334, "", z, False), ("NGC3", 308, "", z, True), ("NCG4", 317, "", z, True), ("NGC5", 412, "", z, False), ("NGC6", 434, "", z, True), ("NGC7", 408, "", z, False), ("NCG8", 417, "", z, False), ], formats="a10,u4,a10,5f4,l", ) assert comparerecords(hdu.data, array) # Verify that all of the references to the data point to the same # numarray hdu.data[0][1] = 300 assert hdu.data._coldefs._arrays[1][0] == 300 assert hdu.data._coldefs.columns[1].array[0] == 300 assert hdu.columns._arrays[1][0] == 300 assert hdu.columns.columns[1].array[0] == 300 assert hdu.data[0][1] == 300 hdu.data._coldefs._arrays[1][0] = 200 assert hdu.data._coldefs._arrays[1][0] == 200 assert hdu.data._coldefs.columns[1].array[0] == 200 assert hdu.columns._arrays[1][0] == 200 assert hdu.columns.columns[1].array[0] == 200 assert hdu.data[0][1] == 200 hdu.data._coldefs.columns[1].array[0] = 100 assert hdu.data._coldefs._arrays[1][0] == 100 assert hdu.data._coldefs.columns[1].array[0] == 100 assert hdu.columns._arrays[1][0] == 100 assert hdu.columns.columns[1].array[0] == 100 assert hdu.data[0][1] == 100 hdu.columns._arrays[1][0] = 90 assert hdu.data._coldefs._arrays[1][0] == 90 assert hdu.data._coldefs.columns[1].array[0] == 90 assert hdu.columns._arrays[1][0] == 90 assert hdu.columns.columns[1].array[0] == 90 assert hdu.data[0][1] == 90 hdu.columns.columns[1].array[0] = 80 assert hdu.data._coldefs._arrays[1][0] == 80 assert hdu.data._coldefs.columns[1].array[0] == 80 assert hdu.columns._arrays[1][0] == 80 assert hdu.columns.columns[1].array[0] == 80 assert hdu.data[0][1] == 80 # Same verification from the file hdul = fits.open(self.temp("newtable.fits")) hdu = hdul[1] hdu.data[0][1] = 300 assert hdu.data._coldefs._arrays[1][0] == 300 assert hdu.data._coldefs.columns[1].array[0] == 300 assert hdu.columns._arrays[1][0] == 300 assert hdu.columns.columns[1].array[0] == 300 assert hdu.data[0][1] == 300 hdu.data._coldefs._arrays[1][0] = 200 assert hdu.data._coldefs._arrays[1][0] == 200 assert hdu.data._coldefs.columns[1].array[0] == 200 assert hdu.columns._arrays[1][0] == 200 assert hdu.columns.columns[1].array[0] == 200 assert hdu.data[0][1] == 200 hdu.data._coldefs.columns[1].array[0] = 100 assert hdu.data._coldefs._arrays[1][0] == 100 assert hdu.data._coldefs.columns[1].array[0] == 100 assert hdu.columns._arrays[1][0] == 100 assert hdu.columns.columns[1].array[0] == 100 assert hdu.data[0][1] == 100 hdu.columns._arrays[1][0] = 90 assert hdu.data._coldefs._arrays[1][0] == 90 assert hdu.data._coldefs.columns[1].array[0] == 90 assert hdu.columns._arrays[1][0] == 90 assert hdu.columns.columns[1].array[0] == 90 assert hdu.data[0][1] == 90 hdu.columns.columns[1].array[0] = 80 assert hdu.data._coldefs._arrays[1][0] == 80 assert hdu.data._coldefs.columns[1].array[0] == 80 assert hdu.columns._arrays[1][0] == 80 assert hdu.columns.columns[1].array[0] == 80 assert hdu.data[0][1] == 80 t1.close() t2.close() hdul.close() def test_adding_a_column(self): # Tests adding a column to a table. counts = np.array([312, 334, 308, 317]) names = np.array(["NGC1", "NGC2", "NGC3", "NCG4"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[1, 0, 1, 1]) coldefs = fits.ColDefs([c1, c2, c3, c4]) tbhdu = fits.BinTableHDU.from_columns(coldefs) assert tbhdu.columns.names == ["target", "counts", "notes", "spectrum"] coldefs1 = coldefs + c5 tbhdu1 = fits.BinTableHDU.from_columns(coldefs1) assert tbhdu1.columns.names == ["target", "counts", "notes", "spectrum", "flag"] z = np.array([0.0, 0.0, 0.0, 0.0, 0.0], dtype=np.float32) array = np.rec.array( [ ("NGC1", 312, "", z, True), ("NGC2", 334, "", z, False), ("NGC3", 308, "", z, True), ("NCG4", 317, "", z, True), ], formats="a10,u4,a10,5f4,l", ) assert comparerecords(tbhdu1.data, array) def test_adding_a_column_inplace(self): # Tests adding a column to a table. counts = np.array([312, 334, 308, 317]) names = np.array(["NGC1", "NGC2", "NGC3", "NCG4"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[1, 0, 1, 1]) coldefs = fits.ColDefs([c1, c2, c3, c4]) tbhdu = fits.BinTableHDU.from_columns(coldefs) assert tbhdu.columns.names == ["target", "counts", "notes", "spectrum"] tbhdu.columns.add_col(c5) assert tbhdu.columns.names == ["target", "counts", "notes", "spectrum", "flag"] z = np.array([0.0, 0.0, 0.0, 0.0, 0.0], dtype=np.float32) array = np.rec.array( [ ("NGC1", 312, "", z, True), ("NGC2", 334, "", z, False), ("NGC3", 308, "", z, True), ("NCG4", 317, "", z, True), ], formats="a10,u4,a10,5f4,l", ) assert comparerecords(tbhdu.data, array) def test_adding_a_column_to_file(self): hdul = fits.open(self.data("table.fits")) tbhdu = hdul[1] col = fits.Column(name="a", array=np.array([1, 2]), format="K") tbhdu.columns.add_col(col) assert tbhdu.columns.names == ["target", "V_mag", "a"] array = np.rec.array( [("NGC1001", 11.1, 1), ("NGC1002", 12.3, 2), ("NGC1003", 15.2, 0)], formats="a20,f4,i8", ) assert comparerecords(tbhdu.data, array) hdul.close() def test_removing_a_column_inplace(self): # Tests adding a column to a table. counts = np.array([312, 334, 308, 317]) names = np.array(["NGC1", "NGC2", "NGC3", "NCG4"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[1, 0, 1, 1]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu = fits.BinTableHDU.from_columns(coldefs) assert tbhdu.columns.names == ["target", "counts", "notes", "spectrum", "flag"] tbhdu.columns.del_col("flag") assert tbhdu.columns.names == ["target", "counts", "notes", "spectrum"] z = np.array([0.0, 0.0, 0.0, 0.0, 0.0], dtype=np.float32) array = np.rec.array( [ ("NGC1", 312, "", z), ("NGC2", 334, "", z), ("NGC3", 308, "", z), ("NCG4", 317, "", z), ], formats="a10,u4,a10,5f4", ) assert comparerecords(tbhdu.data, array) tbhdu.columns.del_col("counts") tbhdu.columns.del_col("notes") assert tbhdu.columns.names == ["target", "spectrum"] array = np.rec.array( [("NGC1", z), ("NGC2", z), ("NGC3", z), ("NCG4", z)], formats="a10,5f4" ) assert comparerecords(tbhdu.data, array) def test_removing_a_column_from_file(self): hdul = fits.open(self.data("table.fits")) tbhdu = hdul[1] tbhdu.columns.del_col("V_mag") assert tbhdu.columns.names == ["target"] array = np.rec.array([("NGC1001",), ("NGC1002",), ("NGC1003",)], formats="a20") assert comparerecords(tbhdu.data, array) hdul.close() def test_merge_tables(self): counts = np.array([312, 334, 308, 317]) names = np.array(["NGC1", "NGC2", "NGC3", "NCG4"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[1, 0, 1, 1]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu = fits.BinTableHDU.from_columns(coldefs) tbhdu.writeto(self.temp("table1.fits")) counts = np.array([412, 434, 408, 417]) names = np.array(["NGC5", "NGC6", "NGC7", "NCG8"]) c1 = fits.Column(name="target1", format="10A", array=names) c2 = fits.Column(name="counts1", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes1", format="A10") c4 = fits.Column(name="spectrum1", format="5E") c5 = fits.Column(name="flag1", format="L", array=[0, 1, 0, 0]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu = fits.BinTableHDU.from_columns(coldefs) tbhdu.writeto(self.temp("table2.fits")) # Merge the columns of table 2 after the columns of table 1 # The column names are assumed to be different # Open the two files we want to append t1 = fits.open(self.temp("table1.fits")) t2 = fits.open(self.temp("table2.fits")) hdu = fits.BinTableHDU.from_columns(t1[1].columns + t2[1].columns) z = np.array([0.0, 0.0, 0.0, 0.0, 0.0], dtype=np.float32) array = np.rec.array( [ ("NGC1", 312, "", z, True, "NGC5", 412, "", z, False), ("NGC2", 334, "", z, False, "NGC6", 434, "", z, True), ("NGC3", 308, "", z, True, "NGC7", 408, "", z, False), ("NCG4", 317, "", z, True, "NCG8", 417, "", z, False), ], formats="a10,u4,a10,5f4,l,a10,u4,a10,5f4,l", ) assert comparerecords(hdu.data, array) hdu.writeto(self.temp("newtable.fits")) # Verify that all of the references to the data point to the same # numarray hdu.data[0][1] = 300 assert hdu.data._coldefs._arrays[1][0] == 300 assert hdu.data._coldefs.columns[1].array[0] == 300 assert hdu.columns._arrays[1][0] == 300 assert hdu.columns.columns[1].array[0] == 300 assert hdu.data[0][1] == 300 hdu.data._coldefs._arrays[1][0] = 200 assert hdu.data._coldefs._arrays[1][0] == 200 assert hdu.data._coldefs.columns[1].array[0] == 200 assert hdu.columns._arrays[1][0] == 200 assert hdu.columns.columns[1].array[0] == 200 assert hdu.data[0][1] == 200 hdu.data._coldefs.columns[1].array[0] = 100 assert hdu.data._coldefs._arrays[1][0] == 100 assert hdu.data._coldefs.columns[1].array[0] == 100 assert hdu.columns._arrays[1][0] == 100 assert hdu.columns.columns[1].array[0] == 100 assert hdu.data[0][1] == 100 hdu.columns._arrays[1][0] = 90 assert hdu.data._coldefs._arrays[1][0] == 90 assert hdu.data._coldefs.columns[1].array[0] == 90 assert hdu.columns._arrays[1][0] == 90 assert hdu.columns.columns[1].array[0] == 90 assert hdu.data[0][1] == 90 hdu.columns.columns[1].array[0] = 80 assert hdu.data._coldefs._arrays[1][0] == 80 assert hdu.data._coldefs.columns[1].array[0] == 80 assert hdu.columns._arrays[1][0] == 80 assert hdu.columns.columns[1].array[0] == 80 assert hdu.data[0][1] == 80 info = [ (0, "PRIMARY", 1, "PrimaryHDU", 4, (), "", ""), ( 1, "", 1, "BinTableHDU", 30, "4R x 10C", "[10A, J, 10A, 5E, L, 10A, J, 10A, 5E, L]", "", ), ] assert fits.info(self.temp("newtable.fits"), output=False) == info hdul = fits.open(self.temp("newtable.fits")) hdu = hdul[1] assert hdu.columns.names == [ "target", "counts", "notes", "spectrum", "flag", "target1", "counts1", "notes1", "spectrum1", "flag1", ] z = np.array([0.0, 0.0, 0.0, 0.0, 0.0], dtype=np.float32) array = np.rec.array( [ ("NGC1", 312, "", z, True, "NGC5", 412, "", z, False), ("NGC2", 334, "", z, False, "NGC6", 434, "", z, True), ("NGC3", 308, "", z, True, "NGC7", 408, "", z, False), ("NCG4", 317, "", z, True, "NCG8", 417, "", z, False), ], formats="a10,u4,a10,5f4,l,a10,u4,a10,5f4,l", ) assert comparerecords(hdu.data, array) # Same verification from the file hdu.data[0][1] = 300 assert hdu.data._coldefs._arrays[1][0] == 300 assert hdu.data._coldefs.columns[1].array[0] == 300 assert hdu.columns._arrays[1][0] == 300 assert hdu.columns.columns[1].array[0] == 300 assert hdu.data[0][1] == 300 hdu.data._coldefs._arrays[1][0] = 200 assert hdu.data._coldefs._arrays[1][0] == 200 assert hdu.data._coldefs.columns[1].array[0] == 200 assert hdu.columns._arrays[1][0] == 200 assert hdu.columns.columns[1].array[0] == 200 assert hdu.data[0][1] == 200 hdu.data._coldefs.columns[1].array[0] = 100 assert hdu.data._coldefs._arrays[1][0] == 100 assert hdu.data._coldefs.columns[1].array[0] == 100 assert hdu.columns._arrays[1][0] == 100 assert hdu.columns.columns[1].array[0] == 100 assert hdu.data[0][1] == 100 hdu.columns._arrays[1][0] = 90 assert hdu.data._coldefs._arrays[1][0] == 90 assert hdu.data._coldefs.columns[1].array[0] == 90 assert hdu.columns._arrays[1][0] == 90 assert hdu.columns.columns[1].array[0] == 90 assert hdu.data[0][1] == 90 hdu.columns.columns[1].array[0] = 80 assert hdu.data._coldefs._arrays[1][0] == 80 assert hdu.data._coldefs.columns[1].array[0] == 80 assert hdu.columns._arrays[1][0] == 80 assert hdu.columns.columns[1].array[0] == 80 assert hdu.data[0][1] == 80 t1.close() t2.close() hdul.close() def test_modify_column_attributes(self): """Regression test for https://github.com/astropy/astropy/issues/996 This just tests one particular use case, but it should apply pretty well to other similar cases. """ NULLS = {"a": 2, "b": "b", "c": 2.3} data = np.array( list(zip([1, 2, 3, 4], ["a", "b", "c", "d"], [2.3, 4.5, 6.7, 8.9])), dtype=[("a", int), ("b", "S1"), ("c", float)], ) b = fits.BinTableHDU(data=data) for col in b.columns: col.null = NULLS[col.name] b.writeto(self.temp("test.fits"), overwrite=True) with fits.open(self.temp("test.fits")) as hdul: header = hdul[1].header assert header["TNULL1"] == 2 assert header["TNULL2"] == "b" assert header["TNULL3"] == 2.3 def test_multidimension_table_from_numpy_rec_columns(self): """Regression test for https://github.com/astropy/astropy/issues/5280 and https://github.com/astropy/astropy/issues/5287 multidimentional tables can now be written with the correct TDIM. Author: Stephen Bailey. """ dtype = [ ("x", (str, 5)), # 1D column of 5-character strings ("y", (str, 3), (4,)), # 2D column; each row is four 3-char strings ] data = np.zeros(2, dtype=dtype) data["x"] = ["abcde", "xyz"] data["y"][0] = ["A", "BC", "DEF", "123"] data["y"][1] = ["X", "YZ", "PQR", "999"] table = Table(data) # Test convenience functions io.fits.writeto / getdata fits.writeto(self.temp("test.fits"), data) dx = fits.getdata(self.temp("test.fits")) assert data["x"].dtype == dx["x"].dtype assert data["y"].dtype == dx["y"].dtype assert np.all(data["x"] == dx["x"]), "x: {} != {}".format(data["x"], dx["x"]) assert np.all(data["y"] == dx["y"]), "y: {} != {}".format(data["y"], dx["y"]) # Test fits.BinTableHDU(data) and avoid convenience functions hdu0 = fits.PrimaryHDU() hdu1 = fits.BinTableHDU(data) hx = fits.HDUList([hdu0, hdu1]) hx.writeto(self.temp("test2.fits")) fx = fits.open(self.temp("test2.fits")) dx = fx[1].data fx.close() assert data["x"].dtype == dx["x"].dtype assert data["y"].dtype == dx["y"].dtype assert np.all(data["x"] == dx["x"]), "x: {} != {}".format(data["x"], dx["x"]) assert np.all(data["y"] == dx["y"]), "y: {} != {}".format(data["y"], dx["y"]) # Test Table write and read table.write(self.temp("test3.fits")) tx = Table.read(self.temp("test3.fits"), character_as_bytes=False) assert table["x"].dtype == tx["x"].dtype assert table["y"].dtype == tx["y"].dtype assert np.all(table["x"] == tx["x"]), "x: {} != {}".format(table["x"], tx["x"]) assert np.all(table["y"] == tx["y"]), "y: {} != {}".format(table["y"], tx["y"]) def test_mask_array(self): t = fits.open(self.data("table.fits")) tbdata = t[1].data mask = tbdata.field("V_mag") > 12 newtbdata = tbdata[mask] hdu = fits.BinTableHDU(newtbdata) hdu.writeto(self.temp("newtable.fits")) hdul = fits.open(self.temp("newtable.fits")) # match to a regex rather than a specific string. expect = r"\[\('NGC1002',\s+12.3[0-9]*\) \(\'NGC1003\',\s+15.[0-9]+\)\]" assert re.match(expect, str(hdu.data)) assert re.match(expect, str(hdul[1].data)) t.close() hdul.close() def test_slice_a_row(self): counts = np.array([312, 334, 308, 317]) names = np.array(["NGC1", "NGC2", "NGC3", "NCG4"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[1, 0, 1, 1]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu = fits.BinTableHDU.from_columns(coldefs) tbhdu.writeto(self.temp("table1.fits")) t1 = fits.open(self.temp("table1.fits")) row = t1[1].data[2] assert row["counts"] == 308 a, b, c = row[1:4] assert a == counts[2] assert b == "" assert (c == np.array([0.0, 0.0, 0.0, 0.0, 0.0], dtype=np.float32)).all() row["counts"] = 310 assert row["counts"] == 310 row[1] = 315 assert row["counts"] == 315 assert row[1:4]["counts"] == 315 pytest.raises(KeyError, lambda r: r[1:4]["flag"], row) row[1:4]["counts"] = 300 assert row[1:4]["counts"] == 300 assert row["counts"] == 300 row[1:4][0] = 400 assert row[1:4]["counts"] == 400 row[1:4]["counts"] = 300 assert row[1:4]["counts"] == 300 # Test stepping for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/59 row[1:4][::-1][-1] = 500 assert row[1:4]["counts"] == 500 row[1:4:2][0] = 300 assert row[1:4]["counts"] == 300 pytest.raises(KeyError, lambda r: r[1:4]["flag"], row) assert row[1:4].field(0) == 300 assert row[1:4].field("counts") == 300 pytest.raises(KeyError, row[1:4].field, "flag") row[1:4].setfield("counts", 500) assert row[1:4].field(0) == 500 pytest.raises(KeyError, row[1:4].setfield, "flag", False) assert t1[1].data._coldefs._arrays[1][2] == 500 assert t1[1].data._coldefs.columns[1].array[2] == 500 assert t1[1].columns._arrays[1][2] == 500 assert t1[1].columns.columns[1].array[2] == 500 assert t1[1].data[2][1] == 500 t1.close() def test_fits_record_len(self): counts = np.array([312, 334, 308, 317]) names = np.array(["NGC1", "NGC2", "NGC3", "NCG4"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[1, 0, 1, 1]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu = fits.BinTableHDU.from_columns(coldefs) tbhdu.writeto(self.temp("table1.fits")) t1 = fits.open(self.temp("table1.fits")) assert len(t1[1].data[0]) == 5 assert len(t1[1].data[0][0:4]) == 4 assert len(t1[1].data[0][0:5]) == 5 assert len(t1[1].data[0][0:6]) == 5 assert len(t1[1].data[0][0:7]) == 5 assert len(t1[1].data[0][1:4]) == 3 assert len(t1[1].data[0][1:5]) == 4 assert len(t1[1].data[0][1:6]) == 4 assert len(t1[1].data[0][1:7]) == 4 t1.close() def test_add_data_by_rows(self): counts = np.array([312, 334, 308, 317]) names = np.array(["NGC1", "NGC2", "NGC3", "NCG4"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[1, 0, 1, 1]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu1 = fits.BinTableHDU.from_columns(coldefs) c1 = fits.Column(name="target", format="10A") c2 = fits.Column(name="counts", format="J", unit="DN") c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L") coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu = fits.BinTableHDU.from_columns(coldefs, nrows=5) # Test assigning data to a tables row using a FITS_record tbhdu.data[0] = tbhdu1.data[0] tbhdu.data[4] = tbhdu1.data[3] # Test assigning data to a tables row using a tuple tbhdu.data[2] = ( "NGC1", 312, "A Note", np.array([1.1, 2.2, 3.3, 4.4, 5.5], dtype=np.float32), True, ) # Test assigning data to a tables row using a list tbhdu.data[3] = [ "JIM1", "33", "A Note", np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype=np.float32), True, ] # Verify that all ndarray objects within the HDU reference the # same ndarray. assert id(tbhdu.data._coldefs.columns[0].array) == id( tbhdu.data._coldefs._arrays[0] ) assert id(tbhdu.data._coldefs.columns[0].array) == id( tbhdu.columns.columns[0].array ) assert id(tbhdu.data._coldefs.columns[0].array) == id(tbhdu.columns._arrays[0]) assert tbhdu.data[0][1] == 312 assert tbhdu.data._coldefs._arrays[1][0] == 312 assert tbhdu.data._coldefs.columns[1].array[0] == 312 assert tbhdu.columns._arrays[1][0] == 312 assert tbhdu.columns.columns[1].array[0] == 312 assert tbhdu.columns.columns[0].array[0] == "NGC1" assert tbhdu.columns.columns[2].array[0] == "" assert ( tbhdu.columns.columns[3].array[0] == np.array([0.0, 0.0, 0.0, 0.0, 0.0], dtype=np.float32) ).all() assert ( isinstance( v := (tbhdu.columns.columns[4].array[0] == np.True_), (bool, np.bool_) ) and v ) assert tbhdu.data[3][1] == 33 assert tbhdu.data._coldefs._arrays[1][3] == 33 assert tbhdu.data._coldefs.columns[1].array[3] == 33 assert tbhdu.columns._arrays[1][3] == 33 assert tbhdu.columns.columns[1].array[3] == 33 assert tbhdu.columns.columns[0].array[3] == "JIM1" assert tbhdu.columns.columns[2].array[3] == "A Note" assert ( tbhdu.columns.columns[3].array[3] == np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype=np.float32) ).all() assert ( isinstance( v := (tbhdu.columns.columns[4].array[3] == np.True_), (bool, np.bool_) ) and v ) def test_assign_multiple_rows_to_table(self): counts = np.array([312, 334, 308, 317]) names = np.array(["NGC1", "NGC2", "NGC3", "NCG4"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[1, 0, 1, 1]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu1 = fits.BinTableHDU.from_columns(coldefs) counts = np.array([112, 134, 108, 117]) names = np.array(["NGC5", "NGC6", "NGC7", "NCG8"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[0, 1, 0, 0]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu = fits.BinTableHDU.from_columns(coldefs) tbhdu.data[0][3] = np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype=np.float32) tbhdu2 = fits.BinTableHDU.from_columns(tbhdu1.data, nrows=9) # Assign the 4 rows from the second table to rows 5 thru 8 of the # new table. Note that the last row of the new table will still be # initialized to the default values. tbhdu2.data[4:] = tbhdu.data # Verify that all ndarray objects within the HDU reference the # same ndarray. assert id(tbhdu2.data._coldefs.columns[0].array) == id( tbhdu2.data._coldefs._arrays[0] ) assert id(tbhdu2.data._coldefs.columns[0].array) == id( tbhdu2.columns.columns[0].array ) assert id(tbhdu2.data._coldefs.columns[0].array) == id( tbhdu2.columns._arrays[0] ) assert tbhdu2.data[0][1] == 312 assert tbhdu2.data._coldefs._arrays[1][0] == 312 assert tbhdu2.data._coldefs.columns[1].array[0] == 312 assert tbhdu2.columns._arrays[1][0] == 312 assert tbhdu2.columns.columns[1].array[0] == 312 assert tbhdu2.columns.columns[0].array[0] == "NGC1" assert tbhdu2.columns.columns[2].array[0] == "" assert ( tbhdu2.columns.columns[3].array[0] == np.array([0.0, 0.0, 0.0, 0.0, 0.0], dtype=np.float32) ).all() assert ( isinstance( v := (tbhdu2.columns.columns[4].array[0] == np.True_), (bool, np.bool_) ) and v ) assert tbhdu2.data[4][1] == 112 assert tbhdu2.data._coldefs._arrays[1][4] == 112 assert tbhdu2.data._coldefs.columns[1].array[4] == 112 assert tbhdu2.columns._arrays[1][4] == 112 assert tbhdu2.columns.columns[1].array[4] == 112 assert tbhdu2.columns.columns[0].array[4] == "NGC5" assert tbhdu2.columns.columns[2].array[4] == "" assert ( tbhdu2.columns.columns[3].array[4] == np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype=np.float32) ).all() assert ( isinstance( v := (tbhdu2.columns.columns[4].array[4] == np.False_), (bool, np.bool_) ) and v ) assert tbhdu2.columns.columns[1].array[8] == 0 assert tbhdu2.columns.columns[0].array[8] == "" assert tbhdu2.columns.columns[2].array[8] == "" assert ( tbhdu2.columns.columns[3].array[8] == np.array([0.0, 0.0, 0.0, 0.0, 0.0], dtype=np.float32) ).all() assert ( isinstance( v := (tbhdu2.columns.columns[4].array[8] == np.False_), (bool, np.bool_) ) and v ) def test_verify_data_references(self): counts = np.array([312, 334, 308, 317]) names = np.array(["NGC1", "NGC2", "NGC3", "NCG4"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[1, 0, 1, 1]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu = fits.BinTableHDU.from_columns(coldefs) # Verify that original ColDefs object has independent Column # objects. assert id(coldefs.columns[0]) != id(c1) # Verify that original ColDefs object has independent ndarray # objects. assert id(coldefs.columns[0].array) != id(names) # Verify that original ColDefs object references the same data # object as the original Column object. assert id(coldefs.columns[0].array) == id(c1.array) assert id(coldefs.columns[0].array) == id(coldefs._arrays[0]) # Verify new HDU has an independent ColDefs object. assert id(coldefs) != id(tbhdu.columns) # Verify new HDU has independent Column objects. assert id(coldefs.columns[0]) != id(tbhdu.columns.columns[0]) # Verify new HDU has independent ndarray objects. assert id(coldefs.columns[0].array) != id(tbhdu.columns.columns[0].array) # Verify that both ColDefs objects in the HDU reference the same # Coldefs object. assert id(tbhdu.columns) == id(tbhdu.data._coldefs) # Verify that all ndarray objects within the HDU reference the # same ndarray. assert id(tbhdu.data._coldefs.columns[0].array) == id( tbhdu.data._coldefs._arrays[0] ) assert id(tbhdu.data._coldefs.columns[0].array) == id( tbhdu.columns.columns[0].array ) assert id(tbhdu.data._coldefs.columns[0].array) == id(tbhdu.columns._arrays[0]) tbhdu.writeto(self.temp("table1.fits")) t1 = fits.open(self.temp("table1.fits")) t1[1].data[0][1] = 213 assert t1[1].data[0][1] == 213 assert t1[1].data._coldefs._arrays[1][0] == 213 assert t1[1].data._coldefs.columns[1].array[0] == 213 assert t1[1].columns._arrays[1][0] == 213 assert t1[1].columns.columns[1].array[0] == 213 t1[1].data._coldefs._arrays[1][0] = 100 assert t1[1].data[0][1] == 100 assert t1[1].data._coldefs._arrays[1][0] == 100 assert t1[1].data._coldefs.columns[1].array[0] == 100 assert t1[1].columns._arrays[1][0] == 100 assert t1[1].columns.columns[1].array[0] == 100 t1[1].data._coldefs.columns[1].array[0] = 500 assert t1[1].data[0][1] == 500 assert t1[1].data._coldefs._arrays[1][0] == 500 assert t1[1].data._coldefs.columns[1].array[0] == 500 assert t1[1].columns._arrays[1][0] == 500 assert t1[1].columns.columns[1].array[0] == 500 t1[1].columns._arrays[1][0] = 600 assert t1[1].data[0][1] == 600 assert t1[1].data._coldefs._arrays[1][0] == 600 assert t1[1].data._coldefs.columns[1].array[0] == 600 assert t1[1].columns._arrays[1][0] == 600 assert t1[1].columns.columns[1].array[0] == 600 t1[1].columns.columns[1].array[0] = 800 assert t1[1].data[0][1] == 800 assert t1[1].data._coldefs._arrays[1][0] == 800 assert t1[1].data._coldefs.columns[1].array[0] == 800 assert t1[1].columns._arrays[1][0] == 800 assert t1[1].columns.columns[1].array[0] == 800 t1.close() def test_new_table_with_ndarray(self): counts = np.array([312, 334, 308, 317]) names = np.array(["NGC1", "NGC2", "NGC3", "NCG4"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[1, 0, 1, 1]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu = fits.BinTableHDU.from_columns(coldefs) tbhdu1 = fits.BinTableHDU.from_columns(tbhdu.data.view(np.ndarray)) # Verify that all ndarray objects within the HDU reference the # same ndarray. assert id(tbhdu1.data._coldefs.columns[0].array) == id( tbhdu1.data._coldefs._arrays[0] ) assert id(tbhdu1.data._coldefs.columns[0].array) == id( tbhdu1.columns.columns[0].array ) assert id(tbhdu1.data._coldefs.columns[0].array) == id( tbhdu1.columns._arrays[0] ) # Ensure I can change the value of one data element and it effects # all of the others. tbhdu1.data[0][1] = 213 assert tbhdu1.data[0][1] == 213 assert tbhdu1.data._coldefs._arrays[1][0] == 213 assert tbhdu1.data._coldefs.columns[1].array[0] == 213 assert tbhdu1.columns._arrays[1][0] == 213 assert tbhdu1.columns.columns[1].array[0] == 213 tbhdu1.data._coldefs._arrays[1][0] = 100 assert tbhdu1.data[0][1] == 100 assert tbhdu1.data._coldefs._arrays[1][0] == 100 assert tbhdu1.data._coldefs.columns[1].array[0] == 100 assert tbhdu1.columns._arrays[1][0] == 100 assert tbhdu1.columns.columns[1].array[0] == 100 tbhdu1.data._coldefs.columns[1].array[0] = 500 assert tbhdu1.data[0][1] == 500 assert tbhdu1.data._coldefs._arrays[1][0] == 500 assert tbhdu1.data._coldefs.columns[1].array[0] == 500 assert tbhdu1.columns._arrays[1][0] == 500 assert tbhdu1.columns.columns[1].array[0] == 500 tbhdu1.columns._arrays[1][0] = 600 assert tbhdu1.data[0][1] == 600 assert tbhdu1.data._coldefs._arrays[1][0] == 600 assert tbhdu1.data._coldefs.columns[1].array[0] == 600 assert tbhdu1.columns._arrays[1][0] == 600 assert tbhdu1.columns.columns[1].array[0] == 600 tbhdu1.columns.columns[1].array[0] = 800 assert tbhdu1.data[0][1] == 800 assert tbhdu1.data._coldefs._arrays[1][0] == 800 assert tbhdu1.data._coldefs.columns[1].array[0] == 800 assert tbhdu1.columns._arrays[1][0] == 800 assert tbhdu1.columns.columns[1].array[0] == 800 tbhdu1.writeto(self.temp("table1.fits")) t1 = fits.open(self.temp("table1.fits")) t1[1].data[0][1] = 213 assert t1[1].data[0][1] == 213 assert t1[1].data._coldefs._arrays[1][0] == 213 assert t1[1].data._coldefs.columns[1].array[0] == 213 assert t1[1].columns._arrays[1][0] == 213 assert t1[1].columns.columns[1].array[0] == 213 t1[1].data._coldefs._arrays[1][0] = 100 assert t1[1].data[0][1] == 100 assert t1[1].data._coldefs._arrays[1][0] == 100 assert t1[1].data._coldefs.columns[1].array[0] == 100 assert t1[1].columns._arrays[1][0] == 100 assert t1[1].columns.columns[1].array[0] == 100 t1[1].data._coldefs.columns[1].array[0] = 500 assert t1[1].data[0][1] == 500 assert t1[1].data._coldefs._arrays[1][0] == 500 assert t1[1].data._coldefs.columns[1].array[0] == 500 assert t1[1].columns._arrays[1][0] == 500 assert t1[1].columns.columns[1].array[0] == 500 t1[1].columns._arrays[1][0] = 600 assert t1[1].data[0][1] == 600 assert t1[1].data._coldefs._arrays[1][0] == 600 assert t1[1].data._coldefs.columns[1].array[0] == 600 assert t1[1].columns._arrays[1][0] == 600 assert t1[1].columns.columns[1].array[0] == 600 t1[1].columns.columns[1].array[0] = 800 assert t1[1].data[0][1] == 800 assert t1[1].data._coldefs._arrays[1][0] == 800 assert t1[1].data._coldefs.columns[1].array[0] == 800 assert t1[1].columns._arrays[1][0] == 800 assert t1[1].columns.columns[1].array[0] == 800 t1.close() def test_new_table_with_fits_rec(self): counts = np.array([312, 334, 308, 317]) names = np.array(["NGC1", "NGC2", "NGC3", "NCG4"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[1, 0, 1, 1]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu = fits.BinTableHDU.from_columns(coldefs) tbhdu.data[0][1] = 213 assert tbhdu.data[0][1] == 213 assert tbhdu.data._coldefs._arrays[1][0] == 213 assert tbhdu.data._coldefs.columns[1].array[0] == 213 assert tbhdu.columns._arrays[1][0] == 213 assert tbhdu.columns.columns[1].array[0] == 213 tbhdu.data._coldefs._arrays[1][0] = 100 assert tbhdu.data[0][1] == 100 assert tbhdu.data._coldefs._arrays[1][0] == 100 assert tbhdu.data._coldefs.columns[1].array[0] == 100 assert tbhdu.columns._arrays[1][0] == 100 assert tbhdu.columns.columns[1].array[0] == 100 tbhdu.data._coldefs.columns[1].array[0] = 500 assert tbhdu.data[0][1] == 500 assert tbhdu.data._coldefs._arrays[1][0] == 500 assert tbhdu.data._coldefs.columns[1].array[0] == 500 assert tbhdu.columns._arrays[1][0] == 500 assert tbhdu.columns.columns[1].array[0] == 500 tbhdu.columns._arrays[1][0] = 600 assert tbhdu.data[0][1] == 600 assert tbhdu.data._coldefs._arrays[1][0] == 600 assert tbhdu.data._coldefs.columns[1].array[0] == 600 assert tbhdu.columns._arrays[1][0] == 600 assert tbhdu.columns.columns[1].array[0] == 600 tbhdu.columns.columns[1].array[0] = 800 assert tbhdu.data[0][1] == 800 assert tbhdu.data._coldefs._arrays[1][0] == 800 assert tbhdu.data._coldefs.columns[1].array[0] == 800 assert tbhdu.columns._arrays[1][0] == 800 assert tbhdu.columns.columns[1].array[0] == 800 tbhdu.columns.columns[1].array[0] = 312 tbhdu.writeto(self.temp("table1.fits")) t1 = fits.open(self.temp("table1.fits")) t1[1].data[0][1] = 1 fr = t1[1].data assert t1[1].data[0][1] == 1 assert t1[1].data._coldefs._arrays[1][0] == 1 assert t1[1].data._coldefs.columns[1].array[0] == 1 assert t1[1].columns._arrays[1][0] == 1 assert t1[1].columns.columns[1].array[0] == 1 assert fr[0][1] == 1 assert fr._coldefs._arrays[1][0] == 1 assert fr._coldefs.columns[1].array[0] == 1 fr._coldefs.columns[1].array[0] = 312 tbhdu1 = fits.BinTableHDU.from_columns(fr) i = 0 for row in tbhdu1.data: for j in range(len(row)): if isinstance(row[j], np.ndarray): assert (row[j] == tbhdu.data[i][j]).all() else: assert row[j] == tbhdu.data[i][j] i = i + 1 tbhdu1.data[0][1] = 213 assert t1[1].data[0][1] == 312 assert t1[1].data._coldefs._arrays[1][0] == 312 assert t1[1].data._coldefs.columns[1].array[0] == 312 assert t1[1].columns._arrays[1][0] == 312 assert t1[1].columns.columns[1].array[0] == 312 assert fr[0][1] == 312 assert fr._coldefs._arrays[1][0] == 312 assert fr._coldefs.columns[1].array[0] == 312 assert tbhdu1.data[0][1] == 213 assert tbhdu1.data._coldefs._arrays[1][0] == 213 assert tbhdu1.data._coldefs.columns[1].array[0] == 213 assert tbhdu1.columns._arrays[1][0] == 213 assert tbhdu1.columns.columns[1].array[0] == 213 t1[1].data[0][1] = 10 assert t1[1].data[0][1] == 10 assert t1[1].data._coldefs._arrays[1][0] == 10 assert t1[1].data._coldefs.columns[1].array[0] == 10 assert t1[1].columns._arrays[1][0] == 10 assert t1[1].columns.columns[1].array[0] == 10 assert fr[0][1] == 10 assert fr._coldefs._arrays[1][0] == 10 assert fr._coldefs.columns[1].array[0] == 10 assert tbhdu1.data[0][1] == 213 assert tbhdu1.data._coldefs._arrays[1][0] == 213 assert tbhdu1.data._coldefs.columns[1].array[0] == 213 assert tbhdu1.columns._arrays[1][0] == 213 assert tbhdu1.columns.columns[1].array[0] == 213 tbhdu1.data._coldefs._arrays[1][0] = 666 assert t1[1].data[0][1] == 10 assert t1[1].data._coldefs._arrays[1][0] == 10 assert t1[1].data._coldefs.columns[1].array[0] == 10 assert t1[1].columns._arrays[1][0] == 10 assert t1[1].columns.columns[1].array[0] == 10 assert fr[0][1] == 10 assert fr._coldefs._arrays[1][0] == 10 assert fr._coldefs.columns[1].array[0] == 10 assert tbhdu1.data[0][1] == 666 assert tbhdu1.data._coldefs._arrays[1][0] == 666 assert tbhdu1.data._coldefs.columns[1].array[0] == 666 assert tbhdu1.columns._arrays[1][0] == 666 assert tbhdu1.columns.columns[1].array[0] == 666 t1.close() def test_bin_table_hdu_constructor(self): counts = np.array([312, 334, 308, 317]) names = np.array(["NGC1", "NGC2", "NGC3", "NCG4"]) c1 = fits.Column(name="target", format="10A", array=names) c2 = fits.Column(name="counts", format="J", unit="DN", array=counts) c3 = fits.Column(name="notes", format="A10") c4 = fits.Column(name="spectrum", format="5E") c5 = fits.Column(name="flag", format="L", array=[1, 0, 1, 1]) coldefs = fits.ColDefs([c1, c2, c3, c4, c5]) tbhdu1 = fits.BinTableHDU.from_columns(coldefs) hdu = fits.BinTableHDU(tbhdu1.data) # Verify that all ndarray objects within the HDU reference the # same ndarray. assert id(hdu.data._coldefs.columns[0].array) == id( hdu.data._coldefs._arrays[0] ) assert id(hdu.data._coldefs.columns[0].array) == id( hdu.columns.columns[0].array ) assert id(hdu.data._coldefs.columns[0].array) == id(hdu.columns._arrays[0]) # Verify that the references in the original HDU are the same as the # references in the new HDU. assert id(tbhdu1.data._coldefs.columns[0].array) == id( hdu.data._coldefs._arrays[0] ) # Verify that a change in the new HDU is reflected in both the new # and original HDU. hdu.data[0][1] = 213 assert hdu.data[0][1] == 213 assert hdu.data._coldefs._arrays[1][0] == 213 assert hdu.data._coldefs.columns[1].array[0] == 213 assert hdu.columns._arrays[1][0] == 213 assert hdu.columns.columns[1].array[0] == 213 assert tbhdu1.data[0][1] == 213 assert tbhdu1.data._coldefs._arrays[1][0] == 213 assert tbhdu1.data._coldefs.columns[1].array[0] == 213 assert tbhdu1.columns._arrays[1][0] == 213 assert tbhdu1.columns.columns[1].array[0] == 213 hdu.data._coldefs._arrays[1][0] = 100 assert hdu.data[0][1] == 100 assert hdu.data._coldefs._arrays[1][0] == 100 assert hdu.data._coldefs.columns[1].array[0] == 100 assert hdu.columns._arrays[1][0] == 100 assert hdu.columns.columns[1].array[0] == 100 assert tbhdu1.data[0][1] == 100 assert tbhdu1.data._coldefs._arrays[1][0] == 100 assert tbhdu1.data._coldefs.columns[1].array[0] == 100 assert tbhdu1.columns._arrays[1][0] == 100 assert tbhdu1.columns.columns[1].array[0] == 100 hdu.data._coldefs.columns[1].array[0] = 500 assert hdu.data[0][1] == 500 assert hdu.data._coldefs._arrays[1][0] == 500 assert hdu.data._coldefs.columns[1].array[0] == 500 assert hdu.columns._arrays[1][0] == 500 assert hdu.columns.columns[1].array[0] == 500 assert tbhdu1.data[0][1] == 500 assert tbhdu1.data._coldefs._arrays[1][0] == 500 assert tbhdu1.data._coldefs.columns[1].array[0] == 500 assert tbhdu1.columns._arrays[1][0] == 500 assert tbhdu1.columns.columns[1].array[0] == 500 hdu.columns._arrays[1][0] = 600 assert hdu.data[0][1] == 600 assert hdu.data._coldefs._arrays[1][0] == 600 assert hdu.data._coldefs.columns[1].array[0] == 600 assert hdu.columns._arrays[1][0] == 600 assert hdu.columns.columns[1].array[0] == 600 assert tbhdu1.data[0][1] == 600 assert tbhdu1.data._coldefs._arrays[1][0] == 600 assert tbhdu1.data._coldefs.columns[1].array[0] == 600 assert tbhdu1.columns._arrays[1][0] == 600 assert tbhdu1.columns.columns[1].array[0] == 600 hdu.columns.columns[1].array[0] = 800 assert hdu.data[0][1] == 800 assert hdu.data._coldefs._arrays[1][0] == 800 assert hdu.data._coldefs.columns[1].array[0] == 800 assert hdu.columns._arrays[1][0] == 800 assert hdu.columns.columns[1].array[0] == 800 assert tbhdu1.data[0][1] == 800 assert tbhdu1.data._coldefs._arrays[1][0] == 800 assert tbhdu1.data._coldefs.columns[1].array[0] == 800 assert tbhdu1.columns._arrays[1][0] == 800 assert tbhdu1.columns.columns[1].array[0] == 800 def test_constructor_name_arg(self): """testConstructorNameArg Passing name='...' to the BinTableHDU and TableHDU constructors should set the .name attribute and 'EXTNAME' header keyword, and override any name in an existing 'EXTNAME' value. """ for hducls in [fits.BinTableHDU, fits.TableHDU]: # First test some default assumptions hdu = hducls() assert hdu.name == "" assert "EXTNAME" not in hdu.header hdu.name = "FOO" assert hdu.name == "FOO" assert hdu.header["EXTNAME"] == "FOO" # Passing name to constructor hdu = hducls(name="FOO") assert hdu.name == "FOO" assert hdu.header["EXTNAME"] == "FOO" # And overriding a header with a different extname hdr = fits.Header() hdr["EXTNAME"] = "EVENTS" hdu = hducls(header=hdr, name="FOO") assert hdu.name == "FOO" assert hdu.header["EXTNAME"] == "FOO" def test_constructor_ver_arg(self): for hducls in [fits.BinTableHDU, fits.TableHDU]: # First test some default assumptions hdu = hducls() assert hdu.ver == 1 assert "EXTVER" not in hdu.header hdu.ver = 2 assert hdu.ver == 2 assert hdu.header["EXTVER"] == 2 # Passing name to constructor hdu = hducls(ver=3) assert hdu.ver == 3 assert hdu.header["EXTVER"] == 3 # And overriding a header with a different extver hdr = fits.Header() hdr["EXTVER"] = 4 hdu = hducls(header=hdr, ver=5) assert hdu.ver == 5 assert hdu.header["EXTVER"] == 5 def test_unicode_colname(self): """ Regression test for https://github.com/astropy/astropy/issues/5204 "Handle unicode FITS BinTable column names on Python 2" """ col = fits.Column(name="spam", format="E", array=[42.0]) # This used to raise a TypeError, now it works fits.BinTableHDU.from_columns([col]) def test_bin_table_with_logical_array(self): c1 = fits.Column(name="flag", format="2L", array=[[True, False], [False, True]]) coldefs = fits.ColDefs([c1]) tbhdu1 = fits.BinTableHDU.from_columns(coldefs) assert ( tbhdu1.data.field("flag")[0] == np.array([True, False], dtype=bool) ).all() assert ( tbhdu1.data.field("flag")[1] == np.array([False, True], dtype=bool) ).all() tbhdu = fits.BinTableHDU.from_columns(tbhdu1.data) assert ( tbhdu.data.field("flag")[0] == np.array([True, False], dtype=bool) ).all() assert ( tbhdu.data.field("flag")[1] == np.array([False, True], dtype=bool) ).all() def test_fits_rec_column_access(self): tbdata = fits.getdata(self.data("table.fits")) assert (tbdata.V_mag == tbdata.field("V_mag")).all() assert (tbdata.V_mag == tbdata["V_mag"]).all() # Table with scaling (c3) and tnull (c1) tbdata = fits.getdata(self.data("tb.fits")) for col in ("c1", "c2", "c3", "c4"): data = getattr(tbdata, col) assert (data == tbdata.field(col)).all() assert (data == tbdata[col]).all() # ascii table tbdata = fits.getdata(self.data("ascii.fits")) for col in ("a", "b"): data = getattr(tbdata, col) assert (data == tbdata.field(col)).all() assert (data == tbdata[col]).all() # with VLA column col1 = fits.Column( name="x", format="PI()", array=np.array([[45, 56], [11, 12, 13]], dtype=np.object_), ) hdu = fits.BinTableHDU.from_columns([col1]) assert type(hdu.data["x"]) == type(hdu.data.x) assert (hdu.data["x"][0] == hdu.data.x[0]).all() assert (hdu.data["x"][1] == hdu.data.x[1]).all() def test_table_with_zero_width_column(self): hdul = fits.open(self.data("zerowidth.fits")) tbhdu = hdul[2] # This HDU contains a zero-width column 'ORBPARM' assert "ORBPARM" in tbhdu.columns.names # The ORBPARM column should not be in the data, though the data should # be readable assert "ORBPARM" in tbhdu.data.names assert "ORBPARM" in tbhdu.data.dtype.names # Verify that some of the data columns are still correctly accessible # by name assert tbhdu.data[0]["ANNAME"] == "VLA:_W16" assert comparefloats( tbhdu.data[0]["STABXYZ"], np.array([499.85566663, -1317.99231554, -735.18866164], dtype=np.float64), ) assert tbhdu.data[0]["NOSTA"] == 1 assert tbhdu.data[0]["MNTSTA"] == 0 assert tbhdu.data[-1]["ANNAME"] == "VPT:_OUT" assert comparefloats( tbhdu.data[-1]["STABXYZ"], np.array([0.0, 0.0, 0.0], dtype=np.float64) ) assert tbhdu.data[-1]["NOSTA"] == 29 assert tbhdu.data[-1]["MNTSTA"] == 0 hdul.writeto(self.temp("newtable.fits")) hdul.close() hdul = fits.open(self.temp("newtable.fits")) tbhdu = hdul[2] # Verify that the previous tests still hold after writing assert "ORBPARM" in tbhdu.columns.names assert "ORBPARM" in tbhdu.data.names assert "ORBPARM" in tbhdu.data.dtype.names assert tbhdu.data[0]["ANNAME"] == "VLA:_W16" assert comparefloats( tbhdu.data[0]["STABXYZ"], np.array([499.85566663, -1317.99231554, -735.18866164], dtype=np.float64), ) assert tbhdu.data[0]["NOSTA"] == 1 assert tbhdu.data[0]["MNTSTA"] == 0 assert tbhdu.data[-1]["ANNAME"] == "VPT:_OUT" assert comparefloats( tbhdu.data[-1]["STABXYZ"], np.array([0.0, 0.0, 0.0], dtype=np.float64) ) assert tbhdu.data[-1]["NOSTA"] == 29 assert tbhdu.data[-1]["MNTSTA"] == 0 hdul.close() def test_string_column_padding(self): a = ["img1", "img2", "img3a", "p"] s = ( "img1\x00\x00\x00\x00\x00\x00" "img2\x00\x00\x00\x00\x00\x00" "img3a\x00\x00\x00\x00\x00" "p\x00\x00\x00\x00\x00\x00\x00\x00\x00" ) acol = fits.Column(name="MEMNAME", format="A10", array=chararray.array(a)) ahdu = fits.BinTableHDU.from_columns([acol]) assert ahdu.data.tobytes().decode("raw-unicode-escape") == s ahdu.writeto(self.temp("newtable.fits")) with fits.open(self.temp("newtable.fits")) as hdul: assert hdul[1].data.tobytes().decode("raw-unicode-escape") == s assert (hdul[1].data["MEMNAME"] == a).all() del hdul ahdu = fits.TableHDU.from_columns([acol]) ahdu.writeto(self.temp("newtable.fits"), overwrite=True) with fits.open(self.temp("newtable.fits")) as hdul: assert hdul[1].data.tobytes().decode("raw-unicode-escape") == s.replace( "\x00", " " ) assert (hdul[1].data["MEMNAME"] == a).all() ahdu = fits.BinTableHDU.from_columns(hdul[1].data.copy()) del hdul # Now serialize once more as a binary table; padding bytes should # revert to zeroes ahdu.writeto(self.temp("newtable.fits"), overwrite=True) with fits.open(self.temp("newtable.fits")) as hdul: assert hdul[1].data.tobytes().decode("raw-unicode-escape") == s assert (hdul[1].data["MEMNAME"] == a).all() def test_multi_dimensional_columns(self): """ Tests the multidimensional column implementation with both numeric arrays and string arrays. """ data = np.rec.array( [ ([0, 1, 2, 3, 4, 5], "row1" * 2), ([6, 7, 8, 9, 0, 1], "row2" * 2), ([2, 3, 4, 5, 6, 7], "row3" * 2), ], formats="6i4,a8", ) thdu = fits.BinTableHDU.from_columns(data) thdu.writeto(self.temp("newtable.fits")) with fits.open(self.temp("newtable.fits"), mode="update") as hdul: # Modify the TDIM fields to my own specification hdul[1].header["TDIM1"] = "(2,3)" hdul[1].header["TDIM2"] = "(4,2)" with fits.open(self.temp("newtable.fits")) as hdul: thdu = hdul[1] c1 = thdu.data.field(0) c2 = thdu.data.field(1) assert c1.shape == (3, 3, 2) assert c2.shape == (3, 2) assert ( c1 == np.array( [ [[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9], [0, 1]], [[2, 3], [4, 5], [6, 7]], ] ) ).all() assert ( c2 == np.array([["row1", "row1"], ["row2", "row2"], ["row3", "row3"]]) ).all() del c1 del c2 del thdu del hdul # Test setting the TDIMn header based on the column data data = np.zeros(3, dtype=[("x", "f4"), ("s", "S5", 4)]) data["x"] = 1, 2, 3 data["s"] = "ok" fits.writeto(self.temp("newtable.fits"), data, overwrite=True) t = fits.getdata(self.temp("newtable.fits")) assert t.field(1).dtype.str[-1] == "5" assert t.field(1).shape == (3, 4) # Like the previous test, but with an extra dimension (a bit more # complicated) data = np.zeros(3, dtype=[("x", "f4"), ("s", "S5", (4, 3))]) data["x"] = 1, 2, 3 data["s"] = "ok" del t fits.writeto(self.temp("newtable.fits"), data, overwrite=True) t = fits.getdata(self.temp("newtable.fits")) assert t.field(1).dtype.str[-1] == "5" assert t.field(1).shape == (3, 4, 3) def test_oned_array_single_element(self): # a table with rows that are 1d arrays of a single value data = np.array([(1,), (2,)], dtype=([("x", "i4", (1,))])) thdu = fits.BinTableHDU.from_columns(data) thdu.writeto(self.temp("onedtable.fits")) with fits.open(self.temp("onedtable.fits")) as hdul: thdu = hdul[1] c = thdu.data.field(0) assert c.shape == (2, 1) assert thdu.header["TDIM1"] == "(1)" def test_bin_table_init_from_string_array_column(self): """ Tests two ways of creating a new `BinTableHDU` from a column of string arrays. This tests for a couple different regressions, and ensures that both BinTableHDU(data=arr) and BinTableHDU.from_columns(arr) work equivalently. Some of this is redundant with the following test, but checks some subtly different cases. """ data = [[b"abcd", b"efgh"], [b"ijkl", b"mnop"], [b"qrst", b"uvwx"]] arr = np.array( [(data,), (data,), (data,), (data,), (data,)], dtype=[("S", "(3, 2)S4")] ) tbhdu1 = fits.BinTableHDU(data=arr) def test_dims_and_roundtrip(tbhdu): assert tbhdu.data["S"].shape == (5, 3, 2) assert tbhdu.data["S"].dtype.str.endswith("U4") tbhdu.writeto(self.temp("test.fits"), overwrite=True) with fits.open(self.temp("test.fits")) as hdul: tbhdu2 = hdul[1] assert tbhdu2.header["TDIM1"] == "(4,2,3)" assert tbhdu2.data["S"].shape == (5, 3, 2) assert tbhdu.data["S"].dtype.str.endswith("U4") assert np.all(tbhdu2.data["S"] == tbhdu.data["S"]) test_dims_and_roundtrip(tbhdu1) tbhdu2 = fits.BinTableHDU.from_columns(arr) test_dims_and_roundtrip(tbhdu2) def test_columns_with_truncating_tdim(self): """ According to the FITS standard (section 7.3.2): If the number of elements in the array implied by the TDIMn is less than the allocated size of the ar- ray in the FITS file, then the unused trailing elements should be interpreted as containing undefined fill values. *deep sigh* What this means is if a column has a repeat count larger than the number of elements indicated by its TDIM (ex: TDIM1 = '(2,2)', but TFORM1 = 6I), then instead of this being an outright error we are to take the first 4 elements as implied by the TDIM and ignore the additional two trailing elements. """ # It's hard to even successfully create a table like this. I think # it *should* be difficult, but once created it should at least be # possible to read. arr1 = [[b"ab", b"cd"], [b"ef", b"gh"], [b"ij", b"kl"]] arr2 = [1, 2, 3, 4, 5] arr = np.array( [(arr1, arr2), (arr1, arr2)], dtype=[("a", "(3, 2)S2"), ("b", "5i8")] ) tbhdu = fits.BinTableHDU(data=arr) tbhdu.writeto(self.temp("test.fits")) with open(self.temp("test.fits"), "rb") as f: raw_bytes = f.read() # Artificially truncate TDIM in the header; this seems to be the # easiest way to do this while getting around Astropy's insistence on the # data and header matching perfectly; again, we have no interest in # making it possible to write files in this format, only read them with open(self.temp("test.fits"), "wb") as f: f.write(raw_bytes.replace(b"(2,2,3)", b"(2,2,2)")) with fits.open(self.temp("test.fits")) as hdul: tbhdu2 = hdul[1] assert tbhdu2.header["TDIM1"] == "(2,2,2)" assert tbhdu2.header["TFORM1"] == "12A" for row in tbhdu2.data: assert np.all(row["a"] == [["ab", "cd"], ["ef", "gh"]]) assert np.all(row["b"] == [1, 2, 3, 4, 5]) def test_string_array_round_trip(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/201""" data = [["abc", "def", "ghi"], ["jkl", "mno", "pqr"], ["stu", "vwx", "yz "]] recarr = np.rec.array([(data,), (data,)], formats=["(3,3)S3"]) t = fits.BinTableHDU(data=recarr) t.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as h: assert "TDIM1" in h[1].header assert h[1].header["TDIM1"] == "(3,3,3)" assert len(h[1].data) == 2 assert len(h[1].data[0]) == 1 assert ( h[1].data.field(0)[0] == np.char.decode(recarr.field(0)[0], "ascii") ).all() with fits.open(self.temp("test.fits")) as h: # Access the data; I think this is necessary to exhibit the bug # reported in https://aeon.stsci.edu/ssb/trac/pyfits/ticket/201 h[1].data[:] h.writeto(self.temp("test2.fits")) with fits.open(self.temp("test2.fits")) as h: assert "TDIM1" in h[1].header assert h[1].header["TDIM1"] == "(3,3,3)" assert len(h[1].data) == 2 assert len(h[1].data[0]) == 1 assert ( h[1].data.field(0)[0] == np.char.decode(recarr.field(0)[0], "ascii") ).all() def test_new_table_with_nd_column(self): """Regression test for https://github.com/spacetelescope/PyFITS/issues/3 """ arra = np.array(["a", "b"], dtype="|S1") arrb = np.array([["a", "bc"], ["cd", "e"]], dtype="|S2") arrc = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) cols = [ fits.Column(name="str", format="1A", array=arra), fits.Column(name="strarray", format="4A", dim="(2,2)", array=arrb), fits.Column(name="intarray", format="4I", dim="(2, 2)", array=arrc), ] hdu = fits.BinTableHDU.from_columns(fits.ColDefs(cols)) hdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as h: # Need to force string arrays to byte arrays in order to compare # correctly on Python 3 assert (h[1].data["str"].encode("ascii") == arra).all() assert (h[1].data["strarray"].encode("ascii") == arrb).all() assert (h[1].data["intarray"] == arrc).all() def test_mismatched_tform_and_tdim(self): """Normally the product of the dimensions listed in a TDIMn keyword must be less than or equal to the repeat count in the TFORMn keyword. This tests that this works if less than (treating the trailing bytes as unspecified fill values per the FITS standard) and fails if the dimensions specified by TDIMn are greater than the repeat count. """ arra = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) arrb = np.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]]) cols = [ fits.Column(name="a", format="20I", dim="(2,2)", array=arra), fits.Column(name="b", format="4I", dim="(2,2)", array=arrb), ] # The first column has the mismatched repeat count hdu = fits.BinTableHDU.from_columns(fits.ColDefs(cols)) hdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as h: assert h[1].header["TFORM1"] == "20I" assert h[1].header["TFORM2"] == "4I" assert h[1].header["TDIM1"] == h[1].header["TDIM2"] == "(2,2)" assert (h[1].data["a"] == arra).all() assert (h[1].data["b"] == arrb).all() assert h[1].data.itemsize == 48 # 16-bits times 24 # If dims is more than the repeat count in the format specifier raise # an error pytest.raises( VerifyError, fits.Column, name="a", format="2I", dim="(2,2)", array=arra ) def test_tdim_of_size_one(self): """Regression test for https://github.com/astropy/astropy/pull/3580""" with fits.open(self.data("tdim.fits")) as hdulist: assert hdulist[1].data["V_mag"].shape == (3, 1, 1) def test_slicing(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/52""" with fits.open(self.data("table.fits")) as f: data = f[1].data targets = data.field("target") s = data[:] assert (s.field("target") == targets).all() for n in range(len(targets) + 2): s = data[:n] assert (s.field("target") == targets[:n]).all() s = data[n:] assert (s.field("target") == targets[n:]).all() s = data[::2] assert (s.field("target") == targets[::2]).all() s = data[::-1] assert (s.field("target") == targets[::-1]).all() def test_array_slicing(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/55""" with fits.open(self.data("table.fits")) as f: data = f[1].data s1 = data[data["target"] == "NGC1001"] s2 = data[np.where(data["target"] == "NGC1001")] s3 = data[[0]] s4 = data[:1] for s in [s1, s2, s3, s4]: assert isinstance(s, fits.FITS_rec) assert comparerecords(s1, s2) assert comparerecords(s2, s3) assert comparerecords(s3, s4) def test_array_broadcasting(self): """ Regression test for https://github.com/spacetelescope/PyFITS/pull/48 """ with fits.open(self.data("table.fits")) as hdu: data = hdu[1].data data["V_mag"] = 0 assert np.all(data["V_mag"] == 0) data["V_mag"] = 1 assert np.all(data["V_mag"] == 1) for container in (list, tuple, np.array): data["V_mag"] = container([1, 2, 3]) assert np.array_equal(data["V_mag"], np.array([1, 2, 3])) def test_array_slicing_readonly(self): """ Like test_array_slicing but with the file opened in 'readonly' mode. Regression test for a crash when slicing readonly memmap'd tables. """ with fits.open(self.data("table.fits"), mode="readonly") as f: data = f[1].data s1 = data[data["target"] == "NGC1001"] s2 = data[np.where(data["target"] == "NGC1001")] s3 = data[[0]] s4 = data[:1] for s in [s1, s2, s3, s4]: assert isinstance(s, fits.FITS_rec) assert comparerecords(s1, s2) assert comparerecords(s2, s3) assert comparerecords(s3, s4) @pytest.mark.parametrize("tablename", ["table.fits", "tb.fits"]) def test_dump_load_round_trip(self, tablename): """ A simple test of the dump/load methods; dump the data, column, and header files and try to reload the table from them. """ with fits.open(self.data(tablename)) as hdul: tbhdu = hdul[1] datafile = self.temp("data.txt") cdfile = self.temp("coldefs.txt") hfile = self.temp("header.txt") tbhdu.dump(datafile, cdfile, hfile) new_tbhdu = fits.BinTableHDU.load(datafile, cdfile, hfile) assert comparerecords(tbhdu.data, new_tbhdu.data) _assert_attr_col(new_tbhdu, hdul[1]) def test_dump_load_array_colums(self): """ Regression test for https://github.com/spacetelescope/PyFITS/issues/22 Ensures that a table containing a multi-value array column can be dumped and loaded successfully. """ data = np.rec.array( [("a", [1, 2, 3, 4], 0.1), ("b", [5, 6, 7, 8], 0.2)], formats="a1,4i4,f8" ) tbhdu = fits.BinTableHDU.from_columns(data) datafile = self.temp("data.txt") cdfile = self.temp("coldefs.txt") hfile = self.temp("header.txt") tbhdu.dump(datafile, cdfile, hfile) new_tbhdu = fits.BinTableHDU.load(datafile, cdfile, hfile) assert comparerecords(tbhdu.data, new_tbhdu.data) assert str(tbhdu.header) == str(new_tbhdu.header) def test_load_guess_format(self): """ Tests loading a table dump with no supplied coldefs or header, so that the table format has to be guessed at. There is of course no exact science to this; the table that's produced simply uses sensible guesses for that format. Ideally this should never have to be used. """ # Create a table containing a variety of data types. a0 = np.array([False, True, False], dtype=bool) c0 = fits.Column(name="c0", format="L", array=a0) # Format X currently not supported by the format # a1 = np.array([[0], [1], [0]], dtype=np.uint8) # c1 = fits.Column(name='c1', format='X', array=a1) a2 = np.array([1, 128, 255], dtype=np.uint8) c2 = fits.Column(name="c2", format="B", array=a2) a3 = np.array([-30000, 1, 256], dtype=np.int16) c3 = fits.Column(name="c3", format="I", array=a3) a4 = np.array([-123123123, 1234, 123123123], dtype=np.int32) c4 = fits.Column(name="c4", format="J", array=a4) a5 = np.array(["a", "abc", "ab"]) c5 = fits.Column(name="c5", format="A3", array=a5) a6 = np.array([1.1, 2.2, 3.3], dtype=np.float64) c6 = fits.Column(name="c6", format="D", array=a6) a7 = np.array([1.1 + 2.2j, 3.3 + 4.4j, 5.5 + 6.6j], dtype=np.complex128) c7 = fits.Column(name="c7", format="M", array=a7) a8 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.int32) c8 = fits.Column(name="c8", format="PJ()", array=a8) tbhdu = fits.BinTableHDU.from_columns([c0, c2, c3, c4, c5, c6, c7, c8]) datafile = self.temp("data.txt") tbhdu.dump(datafile) new_tbhdu = fits.BinTableHDU.load(datafile) # In this particular case the record data at least should be equivalent assert comparerecords(tbhdu.data, new_tbhdu.data) def test_attribute_field_shadowing(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/86 Numpy recarray objects have a poorly-considered feature of allowing field access by attribute lookup. However, if a field name coincides with an existing attribute/method of the array, the existing name takes presence (making the attribute-based field lookup completely unreliable in general cases). This ensures that any FITS_rec attributes still work correctly even when there is a field with the same name as that attribute. """ c1 = fits.Column(name="names", format="I", array=[1]) c2 = fits.Column(name="formats", format="I", array=[2]) c3 = fits.Column(name="other", format="I", array=[3]) t = fits.BinTableHDU.from_columns([c1, c2, c3]) assert t.data.names == ["names", "formats", "other"] assert t.data.formats == ["I"] * 3 assert (t.data["names"] == [1]).all() assert (t.data["formats"] == [2]).all() assert (t.data.other == [3]).all() def test_table_from_bool_fields(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/113 Tests creating a table from a recarray containing numpy.bool columns. """ array = np.rec.array([(True, False), (False, True)], formats="|b1,|b1") thdu = fits.BinTableHDU.from_columns(array) assert thdu.columns.formats == ["L", "L"] assert comparerecords(thdu.data, array) # Test round trip thdu.writeto(self.temp("table.fits")) data = fits.getdata(self.temp("table.fits"), ext=1) assert thdu.columns.formats == ["L", "L"] assert comparerecords(data, array) def test_table_from_bool_fields2(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/215 Tests the case where a multi-field ndarray (not a recarray) containing a bool field is used to initialize a `BinTableHDU`. """ arr = np.array([(False,), (True,), (False,)], dtype=[("a", "?")]) hdu = fits.BinTableHDU(data=arr) assert (hdu.data["a"] == arr["a"]).all() def test_bool_column_update(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/139""" c1 = fits.Column("F1", "L", array=[True, False]) c2 = fits.Column("F2", "L", array=[False, True]) thdu = fits.BinTableHDU.from_columns(fits.ColDefs([c1, c2])) thdu.writeto(self.temp("table.fits")) with fits.open(self.temp("table.fits"), mode="update") as hdul: hdul[1].data["F1"][1] = True hdul[1].data["F2"][0] = True with fits.open(self.temp("table.fits")) as hdul: assert (hdul[1].data["F1"] == [True, True]).all() assert (hdul[1].data["F2"] == [True, True]).all() def test_missing_tnull(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/197""" c = fits.Column( "F1", "A3", null="---", array=np.array(["1.0", "2.0", "---", "3.0"]), ascii=True, ) table = fits.TableHDU.from_columns([c]) table.writeto(self.temp("test.fits")) # Now let's delete the TNULL1 keyword, making this essentially # unreadable with fits.open(self.temp("test.fits"), mode="update") as h: h[1].header["TFORM1"] = "E3" del h[1].header["TNULL1"] with fits.open(self.temp("test.fits")) as h: pytest.raises(ValueError, lambda: h[1].data["F1"]) try: with fits.open(self.temp("test.fits")) as h: h[1].data["F1"] except ValueError as e: assert str(e).endswith( "the header may be missing the necessary TNULL1 " "keyword or the table contains invalid data" ) def test_blank_field_zero(self): """Regression test for https://github.com/astropy/astropy/issues/5134 Blank values in numerical columns of ASCII tables should be replaced with zeros, so they can be loaded into numpy arrays. When a TNULL value is set and there are blank fields not equal to that value, they should be replaced with zeros. """ # Test an integer column with blank string as null nullval1 = " " c1 = fits.Column( "F1", format="I8", null=nullval1, array=np.array([0, 1, 2, 3, 4]), ascii=True, ) table = fits.TableHDU.from_columns([c1]) table.writeto(self.temp("ascii_null.fits")) # Replace the 1st col, 3rd row, with a null field. with open(self.temp("ascii_null.fits"), mode="r+") as h: nulled = h.read().replace("2 ", " ") h.seek(0) h.write(nulled) with fits.open(self.temp("ascii_null.fits"), memmap=True) as f: assert f[1].data[2][0] == 0 # Test a float column with a null value set and blank fields. nullval2 = "NaN" c2 = fits.Column( "F1", format="F12.8", null=nullval2, array=np.array([1.0, 2.0, 3.0, 4.0]), ascii=True, ) table = fits.TableHDU.from_columns([c2]) table.writeto(self.temp("ascii_null2.fits")) # Replace the 1st col, 3rd row, with a null field. with open(self.temp("ascii_null2.fits"), mode="r+") as h: nulled = h.read().replace("3.00000000", " ") h.seek(0) h.write(nulled) with fits.open(self.temp("ascii_null2.fits"), memmap=True) as f: # (Currently it should evaluate to 0.0, but if a TODO in fitsrec is # completed, then it should evaluate to NaN.) assert f[1].data[2][0] == 0.0 or np.isnan(f[1].data[2][0]) def test_column_array_type_mismatch(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/218""" arr = [-99] * 20 col = fits.Column("mag", format="E", array=arr) assert (arr == col.array).all() def test_table_none(self): """Regression test for https://github.com/spacetelescope/PyFITS/issues/27 """ with fits.open(self.data("tb.fits")) as h: h[1].data h[1].data = None assert isinstance(h[1].data, fits.FITS_rec) assert len(h[1].data) == 0 h[1].writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as h: assert h[1].header["NAXIS"] == 2 assert h[1].header["NAXIS1"] == 12 assert h[1].header["NAXIS2"] == 0 assert isinstance(h[1].data, fits.FITS_rec) assert len(h[1].data) == 0 def test_unncessary_table_load(self): """Test unnecessary parsing and processing of FITS tables when writing directly from one FITS file to a new file without first reading the data for user manipulation. In other words, it should be possible to do a direct copy of the raw data without unnecessary processing of the data. """ with fits.open(self.data("table.fits")) as h: h[1].writeto(self.temp("test.fits")) # Since this was a direct copy the h[1].data attribute should not have # even been accessed (since this means the data was read and parsed) assert "data" not in h[1].__dict__ with fits.open(self.data("table.fits")) as h1: with fits.open(self.temp("test.fits")) as h2: assert str(h1[1].header) == str(h2[1].header) assert comparerecords(h1[1].data, h2[1].data) def test_table_from_columns_of_other_table(self): """Tests a rare corner case where the columns of an existing table are used to create a new table with the new_table function. In this specific case, however, the existing table's data has not been read yet, so new_table has to get at it through the Delayed proxy. Note: Although this previously tested new_table it now uses BinTableHDU.from_columns directly, around which new_table is a mere wrapper. """ hdul = fits.open(self.data("table.fits")) # Make sure the column array is in fact delayed... assert isinstance(hdul[1].columns._arrays[0], Delayed) # Create a new table... t = fits.BinTableHDU.from_columns(hdul[1].columns) # The original columns should no longer be delayed... assert not isinstance(hdul[1].columns._arrays[0], Delayed) t.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as hdul2: assert comparerecords(hdul[1].data, hdul2[1].data) hdul.close() def test_bintable_to_asciitable(self): """Tests initializing a TableHDU with the data from a BinTableHDU.""" with fits.open(self.data("tb.fits")) as hdul: tbdata = hdul[1].data tbhdu = fits.TableHDU(data=tbdata) tbhdu.writeto(self.temp("test.fits"), overwrite=True) with fits.open(self.temp("test.fits")) as hdul2: tbdata2 = hdul2[1].data assert np.all(tbdata["c1"] == tbdata2["c1"]) assert np.all(tbdata["c2"] == tbdata2["c2"]) # c3 gets converted from float32 to float64 when writing # test.fits, so cast to float32 before testing that the correct # value is retrieved assert np.all( tbdata["c3"].astype(np.float32) == tbdata2["c3"].astype(np.float32) ) # c4 is a boolean column in the original table; we want ASCII # columns to convert these to columns of 'T'/'F' strings assert np.all(np.where(tbdata["c4"], "T", "F") == tbdata2["c4"]) def test_pickle(self): """ Regression test for https://github.com/astropy/astropy/issues/1597 Tests for pickling FITS_rec objects """ # open existing FITS tables (images pickle by default, no test needed): with fits.open(self.data("tb.fits")) as btb: # Test column array is delayed and can pickle assert isinstance(btb[1].columns._arrays[0], Delayed) btb_pd = pickle.dumps(btb[1].data) btb_pl = pickle.loads(btb_pd) # It should not be delayed any more assert not isinstance(btb[1].columns._arrays[0], Delayed) assert comparerecords(btb_pl, btb[1].data) with fits.open(self.data("ascii.fits")) as asc: asc_pd = pickle.dumps(asc[1].data) asc_pl = pickle.loads(asc_pd) assert comparerecords(asc_pl, asc[1].data) with fits.open(self.data("random_groups.fits")) as rgr: rgr_pd = pickle.dumps(rgr[0].data) rgr_pl = pickle.loads(rgr_pd) assert comparerecords(rgr_pl, rgr[0].data) with fits.open(self.data("zerowidth.fits")) as zwc: # Doesn't pickle zero-width (_phanotm) column 'ORBPARM' zwc_pd = pickle.dumps(zwc[2].data) zwc_pl = pickle.loads(zwc_pd) with pytest.warns(UserWarning, match="Field 2 has a repeat count of 0"): assert comparerecords(zwc_pl, zwc[2].data) def test_zero_length_table(self): array = np.array([], dtype=[("a", "i8"), ("b", "S64"), ("c", ("i4", (3, 2)))]) hdu = fits.BinTableHDU(array) assert hdu.header["NAXIS1"] == 96 assert hdu.header["NAXIS2"] == 0 assert hdu.header["TDIM3"] == "(2,3)" field = hdu.data.field(1) assert field.shape == (0,) def test_dim_column_byte_order_mismatch(self): """ When creating a table column with non-trivial TDIMn, and big-endian array data read from an existing FITS file, the data should not be unnecessarily byteswapped. Regression test for https://github.com/astropy/astropy/issues/3561 """ data = fits.getdata(self.data("random_groups.fits"))["DATA"] col = fits.Column(name="TEST", array=data, dim="(3,1,128,1,1)", format="1152E") thdu = fits.BinTableHDU.from_columns([col]) thdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as hdul: assert np.all(hdul[1].data["TEST"] == data) def test_fits_rec_from_existing(self): """ Tests creating a `FITS_rec` object with `FITS_rec.from_columns` from an existing `FITS_rec` object read from a FITS file. This ensures that the per-column arrays are updated properly. Regression test for https://github.com/spacetelescope/PyFITS/issues/99 """ # The use case that revealed this problem was trying to create a new # table from an existing table, but with additional rows so that we can # append data from a second table (with the same column structure) data1 = fits.getdata(self.data("tb.fits")) data2 = fits.getdata(self.data("tb.fits")) nrows = len(data1) + len(data2) merged = fits.FITS_rec.from_columns(data1, nrows=nrows) merged[len(data1) :] = data2 mask = merged["c1"] > 1 masked = merged[mask] # The test table only has two rows, only the second of which is > 1 for # the 'c1' column assert comparerecords(data1[1:], masked[:1]) assert comparerecords(data1[1:], masked[1:]) # Double check that the original data1 table hasn't been affected by # its use in creating the "merged" table assert comparerecords(data1, fits.getdata(self.data("tb.fits"))) def test_update_string_column_inplace(self): """ Regression test for https://github.com/astropy/astropy/issues/4452 Ensure that changes to values in a string column are saved when a file is opened in ``mode='update'``. """ data = np.array([("abc",)], dtype=[("a", "S3")]) fits.writeto(self.temp("test.fits"), data) with fits.open(self.temp("test.fits"), mode="update") as hdul: hdul[1].data["a"][0] = "XYZ" assert hdul[1].data["a"][0] == "XYZ" with fits.open(self.temp("test.fits")) as hdul: assert hdul[1].data["a"][0] == "XYZ" # Test update but with a non-trivial TDIMn data = np.array( [([["abc", "def", "geh"], ["ijk", "lmn", "opq"]],)], dtype=[("a", ("S3", (2, 3)))], ) fits.writeto(self.temp("test2.fits"), data) expected = [["abc", "def", "geh"], ["ijk", "XYZ", "opq"]] with fits.open(self.temp("test2.fits"), mode="update") as hdul: assert hdul[1].header["TDIM1"] == "(3,3,2)" # Note: Previously I wrote data['a'][0][1, 1] to address # the single row. However, this is broken for chararray because # data['a'][0] does *not* return a view of the original array--this # is a bug in chararray though and not a bug in any FITS-specific # code so we'll roll with it for now... # (by the way the bug in question is fixed in newer Numpy versions) hdul[1].data["a"][0, 1, 1] = "XYZ" assert np.all(hdul[1].data["a"][0] == expected) with fits.open(self.temp("test2.fits")) as hdul: assert hdul[1].header["TDIM1"] == "(3,3,2)" assert np.all(hdul[1].data["a"][0] == expected) @pytest.mark.skipif(not HAVE_OBJGRAPH, reason="requires objgraph") def test_reference_leak(self): """Regression test for https://github.com/astropy/astropy/pull/520""" def readfile(filename): with fits.open(filename) as hdul: data = hdul[1].data.copy() for colname in data.dtype.names: data[colname] with _refcounting("FITS_rec"): readfile(self.data("memtest.fits")) @pytest.mark.skipif(not HAVE_OBJGRAPH, reason="requires objgraph") @pytest.mark.slow def test_reference_leak2(self, tmp_path): """ Regression test for https://github.com/astropy/astropy/pull/4539 This actually re-runs a small set of tests that I found, during careful testing, exhibited the reference leaks fixed by #4539, but now with reference counting around each test to ensure that the leaks are fixed. """ from .test_connect import TestMultipleHDU from .test_core import TestCore t1 = TestCore() t1.setup_method() try: with _refcounting("FITS_rec"): t1.test_add_del_columns2() finally: t1.teardown_method() del t1 t2 = self.__class__() for test_name in [ "test_recarray_to_bintablehdu", "test_numpy_ndarray_to_bintablehdu", "test_new_table_from_recarray", "test_new_fitsrec", ]: t2.setup_method() try: with _refcounting("FITS_rec"): getattr(t2, test_name)() finally: t2.teardown_method() del t2 t3 = TestMultipleHDU() t3.setup_class() try: with _refcounting("FITS_rec"): t3.test_read(tmp_path) finally: t3.teardown_class() del t3 def test_dump_overwrite(self): with fits.open(self.data("table.fits")) as hdul: tbhdu = hdul[1] datafile = self.temp("data.txt") cdfile = self.temp("coldefs.txt") hfile = self.temp("header.txt") tbhdu.dump(datafile, cdfile, hfile) msg = ( r"File .* already exists\. File .* already exists\. File " r".* already exists\. If you mean to replace the " r"file\(s\) then use the argument 'overwrite=True'\." ) with pytest.raises(OSError, match=msg): tbhdu.dump(datafile, cdfile, hfile) tbhdu.dump(datafile, cdfile, hfile, overwrite=True) def test_pseudo_unsigned_ints(self): """ Tests updating a table column containing pseudo-unsigned ints. """ data = np.array([1, 2, 3], dtype=np.uint32) col = fits.Column(name="A", format="1J", bzero=2**31, array=data) thdu = fits.BinTableHDU.from_columns([col]) thdu.writeto(self.temp("test.fits")) # Test that the file wrote out correctly with fits.open(self.temp("test.fits"), uint=True) as hdul: hdu = hdul[1] assert "TZERO1" in hdu.header assert hdu.header["TZERO1"] == 2**31 assert hdu.data["A"].dtype == np.dtype("uint32") assert np.all(hdu.data["A"] == data) # Test updating the unsigned int data hdu.data["A"][0] = 99 hdu.writeto(self.temp("test2.fits")) with fits.open(self.temp("test2.fits"), uint=True) as hdul: hdu = hdul[1] assert "TZERO1" in hdu.header assert hdu.header["TZERO1"] == 2**31 assert hdu.data["A"].dtype == np.dtype("uint32") assert np.all(hdu.data["A"] == [99, 2, 3]) def test_column_with_scaling(self): """Check that a scaled column if correctly saved once it is modified. Regression test for https://github.com/astropy/astropy/issues/6887 """ c1 = fits.Column( name="c1", array=np.array([1], dtype=">i2"), format="1I", bscale=1, bzero=32768, ) S = fits.HDUList([fits.PrimaryHDU(), fits.BinTableHDU.from_columns([c1])]) # Change value in memory S[1].data["c1"][0] = 2 S.writeto(self.temp("a.fits")) assert S[1].data["c1"] == 2 # Read and change value in memory with fits.open(self.temp("a.fits")) as X: X[1].data["c1"][0] = 10 assert X[1].data["c1"][0] == 10 # Write back to file X.writeto(self.temp("b.fits")) # Now check the file with fits.open(self.temp("b.fits")) as hdul: assert hdul[1].data["c1"][0] == 10 def test_ascii_inttypes(self): """ Test correct integer dtypes according to ASCII table field widths. Regression for https://github.com/astropy/astropy/issues/9899 """ i08 = np.array([2**3, 2**23, -(2**22), 10, 2**23], dtype="i4") i10 = np.array([2**8, 2**31 - 1, -(2**29), 30, 2**31 - 1], dtype="i8") i20 = np.array([2**16, 2**63 - 1, -(2**63), 40, 2**63 - 1], dtype="i8") i02 = np.array([2**8, 2**13, -(2**9), 50, 2**13], dtype="i2") t0 = Table([i08, i08 * 2, i10, i20, i02]) t1 = Table.read(self.data("ascii_i4-i20.fits")) assert t1.dtype == t0.dtype assert comparerecords(t1, t0) def test_ascii_floattypes(self): """Test different float formats.""" col1 = fits.Column( name="a", format="D", array=np.array([11.1, 12.2]), ascii=True ) col2 = fits.Column( name="b", format="D16", array=np.array([15.5, 16.6]), ascii=True ) col3 = fits.Column( name="c", format="D16.7", array=np.array([1.1, 2.2]), ascii=True ) hdu = fits.TableHDU.from_columns([col1, col2, col3]) hdu.writeto(self.temp("foo.fits")) with fits.open(self.temp("foo.fits"), memmap=False) as hdul: assert comparerecords(hdul[1].data, hdu.data) @contextlib.contextmanager def _refcounting(type_): """ Perform the body of a with statement with reference counting for the given type (given by class name)--raises an assertion error if there are more unfreed objects of the given type than when we entered the with statement. """ gc.collect() refcount = len(objgraph.by_type(type_)) yield refcount gc.collect() assert ( len(objgraph.by_type(type_)) <= refcount ), "More {0!r} objects still in memory than before." class TestVLATables(FitsTestCase): """Tests specific to tables containing variable-length arrays.""" def test_variable_length_columns(self): def test(format_code): col = fits.Column( name="QUAL_SPE", format=format_code, array=[[0] * 1571] * 225 ) tb_hdu = fits.BinTableHDU.from_columns([col]) pri_hdu = fits.PrimaryHDU() hdu_list = fits.HDUList([pri_hdu, tb_hdu]) hdu_list.writeto(self.temp("toto.fits"), overwrite=True) with fits.open(self.temp("toto.fits")) as toto: q = toto[1].data.field("QUAL_SPE") assert (q[0][4:8] == np.array([0, 0, 0, 0], dtype=np.uint8)).all() assert toto[1].columns[0].format.endswith("J(1571)") for code in ("PJ()", "QJ()"): test(code) def test_extend_variable_length_array(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/54""" def test(format_code): arr = [[1] * 10] * 10 col1 = fits.Column(name="TESTVLF", format=format_code, array=arr) col2 = fits.Column(name="TESTSCA", format="J", array=[1] * 10) tb_hdu = fits.BinTableHDU.from_columns([col1, col2], nrows=15) # This asserts that the normal 'scalar' column's length was extended assert len(tb_hdu.data["TESTSCA"]) == 15 # And this asserts that the VLF column was extended in the same manner assert len(tb_hdu.data["TESTVLF"]) == 15 # We can't compare the whole array since the _VLF is an array of # objects, but comparing just the edge case rows should suffice assert (tb_hdu.data["TESTVLF"][0] == arr[0]).all() assert (tb_hdu.data["TESTVLF"][9] == arr[9]).all() assert (tb_hdu.data["TESTVLF"][10] == ([0] * 10)).all() assert (tb_hdu.data["TESTVLF"][-1] == ([0] * 10)).all() for code in ("PJ()", "QJ()"): test(code) def test_variable_length_table_format_pd_from_object_array(self): def test(format_code): a = np.array( [np.array([7.2e-20, 7.3e-20]), np.array([0.0]), np.array([0.0])], "O" ) acol = fits.Column(name="testa", format=format_code, array=a) tbhdu = fits.BinTableHDU.from_columns([acol]) tbhdu.writeto(self.temp("newtable.fits"), overwrite=True) with fits.open(self.temp("newtable.fits")) as tbhdu1: assert tbhdu1[1].columns[0].format.endswith("D(2)") for j in range(3): for i in range(len(a[j])): assert tbhdu1[1].data.field(0)[j][i] == a[j][i] for code in ("PD()", "QD()"): test(code) def test_variable_length_table_format_pd_from_list(self): def test(format_code): a = [np.array([7.2e-20, 7.3e-20]), np.array([0.0]), np.array([0.0])] acol = fits.Column(name="testa", format=format_code, array=a) tbhdu = fits.BinTableHDU.from_columns([acol]) tbhdu.writeto(self.temp("newtable.fits"), overwrite=True) with fits.open(self.temp("newtable.fits")) as tbhdu1: assert tbhdu1[1].columns[0].format.endswith("D(2)") for j in range(3): for i in range(len(a[j])): assert tbhdu1[1].data.field(0)[j][i] == a[j][i] for code in ("PD()", "QD()"): test(code) def test_variable_length_table_format_pa_from_object_array(self): def test(format_code): a = np.array( [np.array(["a", "b", "c"]), np.array(["d", "e"]), np.array(["f"])], "O" ) acol = fits.Column(name="testa", format=format_code, array=a) tbhdu = fits.BinTableHDU.from_columns([acol]) tbhdu.writeto(self.temp("newtable.fits"), overwrite=True) with fits.open(self.temp("newtable.fits")) as hdul: assert hdul[1].columns[0].format.endswith("A(3)") for j in range(3): for i in range(len(a[j])): assert hdul[1].data.field(0)[j][i] == a[j][i] for code in ("PA()", "QA()"): test(code) def test_variable_length_table_format_pa_from_list(self): def test(format_code): a = ["a", "ab", "abc"] acol = fits.Column(name="testa", format=format_code, array=a) tbhdu = fits.BinTableHDU.from_columns([acol]) tbhdu.writeto(self.temp("newtable.fits"), overwrite=True) with fits.open(self.temp("newtable.fits")) as hdul: assert hdul[1].columns[0].format.endswith("A(3)") for j in range(3): for i in range(len(a[j])): assert hdul[1].data.field(0)[j][i] == a[j][i] for code in ("PA()", "QA()"): test(code) def test_getdata_vla(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/200""" def test(format_code): col = fits.Column( name="QUAL_SPE", format=format_code, array=[np.arange(1572)] * 225 ) tb_hdu = fits.BinTableHDU.from_columns([col]) pri_hdu = fits.PrimaryHDU() hdu_list = fits.HDUList([pri_hdu, tb_hdu]) hdu_list.writeto(self.temp("toto.fits"), overwrite=True) data = fits.getdata(self.temp("toto.fits")) # Need to compare to the original data row by row since the FITS_rec # returns an array of _VLA objects for row_a, row_b in zip(data["QUAL_SPE"], col.array): assert (row_a == row_b).all() for code in ("PJ()", "QJ()"): test(code) @pytest.mark.skipif( not NUMPY_LT_1_22 and NUMPY_LT_1_22_1 and sys.platform == "win32", reason="https://github.com/numpy/numpy/issues/20699", ) def test_copy_vla(self): """ Regression test for https://github.com/spacetelescope/PyFITS/issues/47 """ # Make a file containing a couple of VLA tables arr1 = [np.arange(n + 1) for n in range(255)] arr2 = [np.arange(255, 256 + n) for n in range(255)] # A dummy non-VLA column needed to reproduce issue #47 c = fits.Column("test", format="J", array=np.arange(255)) c1 = fits.Column("A", format="PJ", array=arr1) c2 = fits.Column("B", format="PJ", array=arr2) t1 = fits.BinTableHDU.from_columns([c, c1]) t2 = fits.BinTableHDU.from_columns([c, c2]) hdul = fits.HDUList([fits.PrimaryHDU(), t1, t2]) hdul.writeto(self.temp("test.fits"), overwrite=True) # Just test that the test file wrote out correctly with fits.open(self.temp("test.fits")) as h: assert h[1].header["TFORM2"] == "PJ(255)" assert h[2].header["TFORM2"] == "PJ(255)" assert comparerecords(h[1].data, t1.data) assert comparerecords(h[2].data, t2.data) # Try copying the second VLA and writing to a new file with fits.open(self.temp("test.fits")) as h: new_hdu = fits.BinTableHDU(data=h[2].data, header=h[2].header) new_hdu.writeto(self.temp("test3.fits")) with fits.open(self.temp("test3.fits")) as h2: assert comparerecords(h2[1].data, t2.data) new_hdul = fits.HDUList([fits.PrimaryHDU()]) new_hdul.writeto(self.temp("test2.fits")) # Open several copies of the test file and append copies of the second # VLA table with fits.open(self.temp("test2.fits"), mode="append") as new_hdul: for _ in range(2): with fits.open(self.temp("test.fits")) as h: new_hdul.append(h[2]) new_hdul.flush() # Test that all the VLA copies wrote correctly with fits.open(self.temp("test2.fits")) as new_hdul: for idx in range(1, 3): assert comparerecords(new_hdul[idx].data, t2.data) def test_vla_with_gap(self): hdul = fits.open(self.data("theap-gap.fits")) data = hdul[1].data assert data.shape == (500,) assert data["i"][497] == 497 assert np.array_equal(data["arr"][497], [0, 1, 2, 3, 4]) hdul.close() def test_tolist(self): col = fits.Column( name="var", format="PI()", array=np.array([[1, 2, 3], [11, 12]], dtype=np.object_), ) hdu = fits.BinTableHDU.from_columns([col]) assert hdu.data.tolist() == [[[1, 2, 3]], [[11, 12]]] assert hdu.data["var"].tolist() == [[1, 2, 3], [11, 12]] def test_tolist_from_file(self): filename = self.data("variable_length_table.fits") with fits.open(filename) as hdul: hdu = hdul[1] assert hdu.data.tolist() == [[[45, 56], [11, 3]], [[11, 12, 13], [12, 4]]] assert hdu.data["var"].tolist() == [[45, 56], [11, 12, 13]] @pytest.mark.skipif(sys.maxsize < 2**32, reason="requires 64-bit system") @pytest.mark.skipif(sys.platform == "win32", reason="Cannot test on Windows") @pytest.mark.hugemem def test_heapsize_P_limit(self): """ Regression test for https://github.com/astropy/astropy/issues/10812 Check if the error is raised when the heap size is bigger than what can be indexed with a 32 bit signed int. """ # a matrix with variable length array elements is created nelem = 2**28 matrix = np.zeros(1, dtype=np.object_) matrix[0] = np.arange(0.0, float(nelem + 1)) col = fits.Column(name="MATRIX", format=f"PD({nelem})", unit="", array=matrix) t = fits.BinTableHDU.from_columns([col]) t.name = "MATRIX" with pytest.raises( ValueError, match="Please consider using the 'Q' format for your file." ): t.writeto(self.temp("matrix.fits")) def test_empty_vla_raw_data(self): """ Regression test for https://github.com/astropy/astropy/issues/12881 Check if empty vla are correctly read. """ columns = [ fits.Column(name="integer", format="B", array=(1, 2)), fits.Column(name="empty", format="PJ", array=([], [])), ] fits.BinTableHDU.from_columns(columns).writeto(self.temp("bug.fits")) with fits.open(self.temp("bug.fits")) as hdu: np.array_equal( hdu[1].data["empty"], [np.array([], dtype=np.int32), np.array([], dtype=np.int32)], ) # These are tests that solely test the Column and ColDefs interfaces and # related functionality without directly involving full tables; currently there # are few of these but I expect there to be more as I improve the test coverage class TestColumnFunctions(FitsTestCase): def test_column_format_interpretation(self): """ Test to ensure that when Numpy-style record formats are passed in to the Column constructor for the format argument, they are recognized so long as it's unambiguous (where "unambiguous" here is questionable since Numpy is case insensitive when parsing the format codes. But their "proper" case is lower-case, so we can accept that. Basically, actually, any key in the NUMPY2FITS dict should be accepted. """ for recformat, fitsformat in NUMPY2FITS.items(): c = fits.Column("TEST", np.dtype(recformat)) c.format == fitsformat c = fits.Column("TEST", recformat) c.format == fitsformat c = fits.Column("TEST", fitsformat) c.format == fitsformat # Test a few cases that are ambiguous in that they *are* valid binary # table formats though not ones that are likely to be used, but are # also valid common ASCII table formats c = fits.Column("TEST", "I4") assert c.format == "I4" assert c.format.format == "I" assert c.format.width == 4 c = fits.Column("TEST", "F15.8") assert c.format == "F15.8" assert c.format.format == "F" assert c.format.width == 15 assert c.format.precision == 8 c = fits.Column("TEST", "E15.8") assert c.format.format == "E" assert c.format.width == 15 assert c.format.precision == 8 c = fits.Column("TEST", "D15.8") assert c.format.format == "D" assert c.format.width == 15 assert c.format.precision == 8 # zero-precision should be allowed as well, for float types # https://github.com/astropy/astropy/issues/3422 c = fits.Column("TEST", "F10.0") assert c.format.format == "F" assert c.format.width == 10 assert c.format.precision == 0 c = fits.Column("TEST", "E10.0") assert c.format.format == "E" assert c.format.width == 10 assert c.format.precision == 0 c = fits.Column("TEST", "D10.0") assert c.format.format == "D" assert c.format.width == 10 assert c.format.precision == 0 # These are a couple cases where the format code is a valid binary # table format, and is not strictly a valid ASCII table format but # could be *interpreted* as one by appending a default width. This # will only happen either when creating an ASCII table or when # explicitly specifying ascii=True when the column is created c = fits.Column("TEST", "I") assert c.format == "I" assert c.format.recformat == "i2" c = fits.Column("TEST", "I", ascii=True) assert c.format == "I10" assert c.format.recformat == "i4" # With specified widths, integer precision should be set appropriately c = fits.Column("TEST", "I4", ascii=True) assert c.format == "I4" assert c.format.recformat == "i2" c = fits.Column("TEST", "I9", ascii=True) assert c.format == "I9" assert c.format.recformat == "i4" c = fits.Column("TEST", "I12", ascii=True) assert c.format == "I12" assert c.format.recformat == "i8" c = fits.Column("TEST", "E") assert c.format == "E" assert c.format.recformat == "f4" c = fits.Column("TEST", "E", ascii=True) assert c.format == "E15.7" # F is not a valid binary table format so it should be unambiguously # treated as an ASCII column c = fits.Column("TEST", "F") assert c.format == "F16.7" c = fits.Column("TEST", "D") assert c.format == "D" assert c.format.recformat == "f8" c = fits.Column("TEST", "D", ascii=True) assert c.format == "D25.17" def test_zero_precision_float_column(self): """ Regression test for https://github.com/astropy/astropy/issues/3422 """ c = fits.Column("TEST", "F5.0", array=[1.1, 2.2, 3.3]) # The decimal places will be clipped t = fits.TableHDU.from_columns([c]) t.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as hdul: assert hdul[1].header["TFORM1"] == "F5.0" assert hdul[1].data["TEST"].dtype == np.dtype("float64") assert np.all(hdul[1].data["TEST"] == [1.0, 2.0, 3.0]) # Check how the raw data looks raw = np.rec.recarray.field(hdul[1].data, "TEST") assert raw.tobytes() == b" 1. 2. 3." def test_column_array_type_mismatch(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/218""" arr = [-99] * 20 col = fits.Column("mag", format="E", array=arr) assert (arr == col.array).all() def test_new_coldefs_with_invalid_seqence(self): """Test that a TypeError is raised when a ColDefs is instantiated with a sequence of non-Column objects. """ pytest.raises(TypeError, fits.ColDefs, [1, 2, 3]) def test_coldefs_init_from_array(self): """Test that ColDefs._init_from_array works with single element data- types as well as multi-element data-types """ nd_array = np.ndarray((1,), dtype=[("A", "<u4", (2,)), ("B", ">u2")]) col_defs = fits.column.ColDefs(nd_array) assert 2**31 == col_defs["A"].bzero assert 2**15 == col_defs["B"].bzero def test_pickle(self): """ Regression test for https://github.com/astropy/astropy/issues/1597 Tests for pickling FITS_rec objects """ # open existing FITS tables (images pickle by default, no test needed): with fits.open(self.data("tb.fits")) as btb: # Test column array is delayed and can pickle assert isinstance(btb[1].columns._arrays[0], Delayed) btb_pd = pickle.dumps(btb[1].data) btb_pl = pickle.loads(btb_pd) # It should not be delayed any more assert not isinstance(btb[1].columns._arrays[0], Delayed) assert comparerecords(btb_pl, btb[1].data) with fits.open(self.data("ascii.fits")) as asc: asc_pd = pickle.dumps(asc[1].data) asc_pl = pickle.loads(asc_pd) assert comparerecords(asc_pl, asc[1].data) with fits.open(self.data("random_groups.fits")) as rgr: rgr_pd = pickle.dumps(rgr[0].data) rgr_pl = pickle.loads(rgr_pd) assert comparerecords(rgr_pl, rgr[0].data) with fits.open(self.data("zerowidth.fits")) as zwc: # Doesn't pickle zero-width (_phanotm) column 'ORBPARM' zwc_pd = pickle.dumps(zwc[2].data) zwc_pl = pickle.loads(zwc_pd) with pytest.warns( UserWarning, match=r"Field 2 has a repeat count " r"of 0 in its format code", ): assert comparerecords(zwc_pl, zwc[2].data) def test_column_lookup_by_name(self): """Tests that a `ColDefs` can be indexed by column name.""" a = fits.Column(name="a", format="D") b = fits.Column(name="b", format="D") cols = fits.ColDefs([a, b]) assert cols["a"] == cols[0] assert cols["b"] == cols[1] def test_column_attribute_change_after_removal(self): """ This is a test of the column attribute change notification system. After a column has been removed from a table (but other references are kept to that same column) changes to that column's attributes should not trigger a notification on the table it was removed from. """ # One way we can check this is to ensure there are no further changes # to the header table = fits.BinTableHDU.from_columns( [fits.Column("a", format="D"), fits.Column("b", format="D")] ) b = table.columns["b"] table.columns.del_col("b") assert table.data.dtype.names == ("a",) b.name = "HELLO" assert b.name == "HELLO" assert "TTYPE2" not in table.header assert table.header["TTYPE1"] == "a" assert table.columns.names == ["a"] with pytest.raises(KeyError): table.columns["b"] # Make sure updates to the remaining column still work table.columns.change_name("a", "GOODBYE") with pytest.raises(KeyError): table.columns["a"] assert table.columns["GOODBYE"].name == "GOODBYE" assert table.data.dtype.names == ("GOODBYE",) assert table.columns.names == ["GOODBYE"] assert table.data.columns.names == ["GOODBYE"] table.columns["GOODBYE"].name = "foo" with pytest.raises(KeyError): table.columns["GOODBYE"] assert table.columns["foo"].name == "foo" assert table.data.dtype.names == ("foo",) assert table.columns.names == ["foo"] assert table.data.columns.names == ["foo"] def test_x_column_deepcopy(self): """ Regression test for https://github.com/astropy/astropy/pull/4514 Tests that columns with the X (bit array) format can be deep-copied. """ c = fits.Column("xcol", format="5X", array=[1, 0, 0, 1, 0]) c2 = copy.deepcopy(c) assert c2.name == c.name assert c2.format == c.format assert np.all(c2.array == c.array) def test_p_column_deepcopy(self): """ Regression test for https://github.com/astropy/astropy/pull/4514 Tests that columns with the P/Q formats (variable length arrays) can be deep-copied. """ c = fits.Column("pcol", format="PJ", array=[[1, 2], [3, 4, 5]]) c2 = copy.deepcopy(c) assert c2.name == c.name assert c2.format == c.format assert np.all(c2.array[0] == c.array[0]) assert np.all(c2.array[1] == c.array[1]) c3 = fits.Column("qcol", format="QJ", array=[[1, 2], [3, 4, 5]]) c4 = copy.deepcopy(c3) assert c4.name == c3.name assert c4.format == c3.format assert np.all(c4.array[0] == c3.array[0]) assert np.all(c4.array[1] == c3.array[1]) def test_column_verify_keywords(self): """ Test that the keyword arguments used to initialize a Column, specifically those that typically read from a FITS header (so excluding array), are verified to have a valid value. """ with pytest.raises(AssertionError) as err: _ = fits.Column(1, format="I", array=[1, 2, 3, 4, 5]) assert "Column name must be a string able to fit" in str(err.value) with pytest.raises(VerifyError) as err: _ = fits.Column( "col", format=0, null="Nan", disp=1, coord_type=1, coord_unit=2, coord_inc="1", time_ref_pos=1, coord_ref_point="1", coord_ref_value="1", ) err_msgs = [ "keyword arguments to Column were invalid", "TFORM", "TNULL", "TDISP", "TCTYP", "TCUNI", "TCRPX", "TCRVL", "TCDLT", "TRPOS", ] for msg in err_msgs: assert msg in str(err.value) def test_column_verify_start(self): """ Regression test for https://github.com/astropy/astropy/pull/6359 Test the validation of the column start position option (ASCII table only), corresponding to ``TBCOL`` keyword. Test whether the VerifyError message generated is the one with highest priority, i.e. the order of error messages to be displayed is maintained. """ with pytest.raises(VerifyError) as err: _ = fits.Column("a", format="B", start="a", array=[1, 2, 3]) assert "start option (TBCOLn) is not allowed for binary table columns" in str( err.value ) with pytest.raises(VerifyError) as err: _ = fits.Column("a", format="I", start="a", array=[1, 2, 3]) assert "start option (TBCOLn) must be a positive integer (got 'a')." in str( err.value ) with pytest.raises(VerifyError) as err: _ = fits.Column("a", format="I", start="-56", array=[1, 2, 3]) assert "start option (TBCOLn) must be a positive integer (got -56)." in str( err.value ) @pytest.mark.parametrize( "keys", [ {"TFORM": "Z", "TDISP": "E"}, {"TFORM": "2", "TDISP": "2E"}, {"TFORM": 3, "TDISP": 6.3}, {"TFORM": float, "TDISP": np.float64}, {"TFORM": "", "TDISP": "E.5"}, ], ) def test_column_verify_formats(self, keys): """ Additional tests for verification of 'TFORM' and 'TDISP' keyword arguments used to initialize a Column. """ with pytest.raises(VerifyError) as err: _ = fits.Column("col", format=keys["TFORM"], disp=keys["TDISP"]) for key in keys.keys(): assert key in str(err.value) assert str(keys[key]) in str(err.value) def test_regression_5383(): # Regression test for an undefined variable x = np.array([1, 2, 3]) col = fits.Column(name="a", array=x, format="E") hdu = fits.BinTableHDU.from_columns([col]) del hdu._header["TTYPE1"] hdu.columns[0].name = "b" def test_table_to_hdu(): from astropy.table import Table table = Table( [[1, 2, 3], ["a", "b", "c"], [2.3, 4.5, 6.7]], names=["a", "b", "c"], dtype=["i", "U1", "f"], ) table["a"].unit = "m/s" table["b"].unit = "not-a-unit" table.meta["foo"] = "bar" with pytest.warns( UnitsWarning, match="'not-a-unit' did not parse as fits unit" ) as w: hdu = fits.BinTableHDU(table, header=fits.Header({"TEST": 1})) assert len(w) == 1 for name in "abc": assert np.array_equal(table[name], hdu.data[name]) # Check that TUNITn cards appear in the correct order # (https://github.com/astropy/astropy/pull/5720) assert hdu.header.index("TUNIT1") < hdu.header.index("TTYPE2") assert hdu.header["FOO"] == "bar" assert hdu.header["TEST"] == 1 def test_regression_scalar_indexing(): # Indexing a FITS_rec with a tuple that returns a scalar record # should work x = np.array([(1.0, 2), (3.0, 4)], dtype=[("x", float), ("y", int)]).view( fits.FITS_rec ) x1a = x[1] # this should succeed. x1b = x[(1,)] # FITS_record does not define __eq__; so test elements. assert all(a == b for a, b in zip(x1a, x1b)) def test_new_column_attributes_preserved(tmp_path): # Regression test for https://github.com/astropy/astropy/issues/7145 # This makes sure that for now we don't clear away keywords that have # newly been recognized (in Astropy 3.0) as special column attributes but # instead just warn that we might do so in future. The new keywords are: # TCTYP, TCUNI, TCRPX, TCRVL, TCDLT, TRPOS col = [] col.append(fits.Column(name="TIME", format="1E", unit="s")) col.append(fits.Column(name="RAWX", format="1I", unit="pixel")) col.append(fits.Column(name="RAWY", format="1I")) cd = fits.ColDefs(col) hdr = fits.Header() # Keywords that will get ignored in favor of these in the data hdr["TUNIT1"] = "pixel" hdr["TUNIT2"] = "m" hdr["TUNIT3"] = "m" # Keywords that were added in Astropy 3.0 that should eventually be # ignored and set on the data instead hdr["TCTYP2"] = "RA---TAN" hdr["TCTYP3"] = "ANGLE" hdr["TCRVL2"] = -999.0 hdr["TCRVL3"] = -999.0 hdr["TCRPX2"] = 1.0 hdr["TCRPX3"] = 1.0 hdr["TALEN2"] = 16384 hdr["TALEN3"] = 1024 hdr["TCUNI2"] = "angstrom" hdr["TCUNI3"] = "deg" # Other non-relevant keywords hdr["RA"] = 1.5 hdr["DEC"] = 3.0 with pytest.warns(AstropyDeprecationWarning) as warning_list: hdu = fits.BinTableHDU.from_columns(cd, hdr) assert str(warning_list[0].message).startswith( "The following keywords are now recognized as special" ) # First, check that special keywords such as TUNIT are ignored in the header # We may want to change that behavior in future, but this is the way it's # been for a while now. assert hdu.columns[0].unit == "s" assert hdu.columns[1].unit == "pixel" assert hdu.columns[2].unit is None assert hdu.header["TUNIT1"] == "s" assert hdu.header["TUNIT2"] == "pixel" assert "TUNIT3" not in hdu.header # TUNIT3 was removed # Now, check that the new special keywords are actually still there # but weren't used to set the attributes on the data assert hdu.columns[0].coord_type is None assert hdu.columns[1].coord_type is None assert hdu.columns[2].coord_type is None assert "TCTYP1" not in hdu.header assert hdu.header["TCTYP2"] == "RA---TAN" assert hdu.header["TCTYP3"] == "ANGLE" # Make sure that other keywords are still there assert hdu.header["RA"] == 1.5 assert hdu.header["DEC"] == 3.0 # Now we can write this HDU to a file and re-load. Re-loading *should* # cause the special column attribtues to be picked up (it's just that when a # header is manually specified, these values are ignored) filename = tmp_path / "test.fits" hdu.writeto(filename) # Make sure we don't emit a warning in this case with warnings.catch_warnings(record=True) as warning_list: with fits.open(filename) as hdul: hdu2 = hdul[1] assert len(warning_list) == 0 # Check that column attributes are now correctly set assert hdu2.columns[0].unit == "s" assert hdu2.columns[1].unit == "pixel" assert hdu2.columns[2].unit is None assert hdu2.header["TUNIT1"] == "s" assert hdu2.header["TUNIT2"] == "pixel" assert "TUNIT3" not in hdu2.header # TUNIT3 was removed # Now, check that the new special keywords are actually still there # but weren't used to set the attributes on the data assert hdu2.columns[0].coord_type is None assert hdu2.columns[1].coord_type == "RA---TAN" assert hdu2.columns[2].coord_type == "ANGLE" assert "TCTYP1" not in hdu2.header assert hdu2.header["TCTYP2"] == "RA---TAN" assert hdu2.header["TCTYP3"] == "ANGLE" # Make sure that other keywords are still there assert hdu2.header["RA"] == 1.5 assert hdu2.header["DEC"] == 3.0 def test_empty_table(tmp_path): ofile = tmp_path / "emptytable.fits" hdu = fits.BinTableHDU(header=None, data=None, name="TEST") hdu.writeto(ofile) with fits.open(ofile) as hdul: assert hdul["TEST"].data.size == 0 ofile = tmp_path / "emptytable.fits.gz" hdu = fits.BinTableHDU(header=None, data=None, name="TEST") hdu.writeto(ofile, overwrite=True) with fits.open(ofile) as hdul: assert hdul["TEST"].data.size == 0 def test_a3dtable(tmp_path): testfile = tmp_path / "test.fits" hdu = fits.BinTableHDU.from_columns( [fits.Column(name="FOO", format="J", array=np.arange(10))] ) hdu.header["XTENSION"] = "A3DTABLE" hdu.writeto(testfile, output_verify="ignore") with fits.open(testfile) as hdul: assert hdul[1].header["XTENSION"] == "A3DTABLE" with pytest.warns(AstropyUserWarning) as w: hdul.verify("fix") assert str(w[0].message) == "Verification reported errors:" assert str(w[2].message).endswith("Converted the XTENSION keyword to BINTABLE.") assert hdul[1].header["XTENSION"] == "BINTABLE" def test_invalid_file(tmp_path): hdu = fits.BinTableHDU() # little trick to write an invalid card ... hdu.header["FOO"] = None hdu.header.cards["FOO"]._value = np.nan testfile = tmp_path / "test.fits" hdu.writeto(testfile, output_verify="ignore") with fits.open(testfile) as hdul: assert hdul[1].data is not None def test_unit_parse_strict(tmp_path): path = tmp_path / "invalid_unit.fits" # this is a unit parseable by the generic format but invalid for FITS invalid_unit = "1 / (MeV sr s)" unit = Unit(invalid_unit) t = Table({"a": [1, 2, 3]}) t.write(path) with fits.open(path, mode="update") as hdul: hdul[1].header["TUNIT1"] = invalid_unit # default is "warn" with pytest.warns(UnitsWarning): t = Table.read(path) assert isinstance(t["a"].unit, UnrecognizedUnit) t = Table.read(path, unit_parse_strict="silent") assert isinstance(t["a"].unit, UnrecognizedUnit) with pytest.raises(ValueError): Table.read(path, unit_parse_strict="raise") with pytest.warns(UnitsWarning): Table.read(path, unit_parse_strict="warn")
90c11319ffd434c1a3b5a77ee078d0d251f7c00a7295b1d58cf0bb8c6fd00394
# Licensed under a 3-clause BSD style license - see PYFITS.rst import sys import warnings import numpy as np import pytest from astropy.io import fits from astropy.io.fits.hdu.base import _ValidHDU from .conftest import FitsTestCase from .test_table import comparerecords class TestChecksumFunctions(FitsTestCase): # All checksums have been verified against CFITSIO def setup_method(self): super().setup_method() self._oldfilters = warnings.filters[:] warnings.filterwarnings("error", message="Checksum verification failed") warnings.filterwarnings("error", message="Datasum verification failed") # Monkey-patch the _get_timestamp method so that the checksum # timestamps (and hence the checksum themselves) are always the same self._old_get_timestamp = _ValidHDU._get_timestamp _ValidHDU._get_timestamp = lambda self: "2013-12-20T13:36:10" def teardown_method(self): super().teardown_method() warnings.filters = self._oldfilters _ValidHDU._get_timestamp = self._old_get_timestamp def test_sample_file(self): hdul = fits.open(self.data("checksum.fits"), checksum=True) assert hdul._read_all hdul.close() def test_image_create(self): n = np.arange(100, dtype=np.int64) hdu = fits.PrimaryHDU(n) hdu.writeto(self.temp("tmp.fits"), overwrite=True, checksum=True) with fits.open(self.temp("tmp.fits"), checksum=True) as hdul: assert (hdu.data == hdul[0].data).all() assert "CHECKSUM" in hdul[0].header assert "DATASUM" in hdul[0].header if not sys.platform.startswith("win32"): # The checksum ends up being different on Windows, possibly due # to slight floating point differences assert hdul[0].header["CHECKSUM"] == "ZHMkeGKjZGKjbGKj" assert hdul[0].header["DATASUM"] == "4950" def test_scaled_data(self): with fits.open(self.data("scale.fits")) as hdul: orig_data = hdul[0].data.copy() hdul[0].scale("int16", "old") hdul.writeto(self.temp("tmp.fits"), overwrite=True, checksum=True) with fits.open(self.temp("tmp.fits"), checksum=True) as hdul1: assert (hdul1[0].data == orig_data).all() assert "CHECKSUM" in hdul1[0].header assert hdul1[0].header["CHECKSUM"] == "cUmaeUjZcUjacUjW" assert "DATASUM" in hdul1[0].header assert hdul1[0].header["DATASUM"] == "1891563534" def test_scaled_data_auto_rescale(self): """ Regression test for https://github.com/astropy/astropy/issues/3883#issuecomment-115122647 Ensure that when scaled data is automatically rescaled on opening/writing a file that the checksum and datasum are computed for the rescaled array. """ with fits.open(self.data("scale.fits")) as hdul: # Write out a copy of the data with the rescaling applied hdul.writeto(self.temp("rescaled.fits")) # Reopen the new file and save it back again with a checksum with fits.open(self.temp("rescaled.fits")) as hdul: hdul.writeto(self.temp("rescaled2.fits"), overwrite=True, checksum=True) # Now do like in the first writeto but use checksum immediately with fits.open(self.data("scale.fits")) as hdul: hdul.writeto(self.temp("rescaled3.fits"), checksum=True) # Also don't rescale the data but add a checksum with fits.open(self.data("scale.fits"), do_not_scale_image_data=True) as hdul: hdul.writeto(self.temp("scaled.fits"), checksum=True) # Must used nested with statements to support older Python versions # (but contextlib.nested is not available in newer Pythons :( with fits.open(self.temp("rescaled2.fits")) as hdul1: with fits.open(self.temp("rescaled3.fits")) as hdul2: with fits.open(self.temp("scaled.fits")) as hdul3: hdr1 = hdul1[0].header hdr2 = hdul2[0].header hdr3 = hdul3[0].header assert hdr1["DATASUM"] == hdr2["DATASUM"] assert hdr1["CHECKSUM"] == hdr2["CHECKSUM"] assert hdr1["DATASUM"] != hdr3["DATASUM"] assert hdr1["CHECKSUM"] != hdr3["CHECKSUM"] def test_uint16_data(self): checksums = [ ("aDcXaCcXaCcXaCcX", "0"), ("oYiGqXi9oXiEoXi9", "1746888714"), ("VhqQWZoQVfoQVZoQ", "0"), ("4cPp5aOn4aOn4aOn", "0"), ("8aCN8X9N8aAN8W9N", "1756785133"), ("UhqdUZnbUfnbUZnb", "0"), ("4cQJ5aN94aNG4aN9", "0"), ] with fits.open(self.data("o4sp040b0_raw.fits"), uint=True) as hdul: hdul.writeto(self.temp("tmp.fits"), overwrite=True, checksum=True) with fits.open(self.temp("tmp.fits"), uint=True, checksum=True) as hdul1: for idx, (hdu_a, hdu_b) in enumerate(zip(hdul, hdul1)): if hdu_a.data is None or hdu_b.data is None: assert hdu_a.data is hdu_b.data else: assert (hdu_a.data == hdu_b.data).all() assert "CHECKSUM" in hdul[idx].header assert hdul[idx].header["CHECKSUM"] == checksums[idx][0] assert "DATASUM" in hdul[idx].header assert hdul[idx].header["DATASUM"] == checksums[idx][1] def test_groups_hdu_data(self): imdata = np.arange(100.0) imdata.shape = (10, 1, 1, 2, 5) pdata1 = np.arange(10) + 0.1 pdata2 = 42 x = fits.hdu.groups.GroupData( imdata, parnames=["abc", "xyz"], pardata=[pdata1, pdata2], bitpix=-32 ) hdu = fits.GroupsHDU(x) hdu.writeto(self.temp("tmp.fits"), overwrite=True, checksum=True) with fits.open(self.temp("tmp.fits"), checksum=True) as hdul: assert comparerecords(hdul[0].data, hdu.data) assert "CHECKSUM" in hdul[0].header assert hdul[0].header["CHECKSUM"] == "3eDQAZDO4dDOAZDO" assert "DATASUM" in hdul[0].header assert hdul[0].header["DATASUM"] == "2797758084" def test_binary_table_data(self): a1 = np.array(["NGC1001", "NGC1002", "NGC1003"]) a2 = np.array([11.1, 12.3, 15.2]) col1 = fits.Column(name="target", format="20A", array=a1) col2 = fits.Column(name="V_mag", format="E", array=a2) cols = fits.ColDefs([col1, col2]) tbhdu = fits.BinTableHDU.from_columns(cols) tbhdu.writeto(self.temp("tmp.fits"), overwrite=True, checksum=True) with fits.open(self.temp("tmp.fits"), checksum=True) as hdul: assert comparerecords(tbhdu.data, hdul[1].data) assert "CHECKSUM" in hdul[0].header assert hdul[0].header["CHECKSUM"] == "D8iBD6ZAD6fAD6ZA" assert "DATASUM" in hdul[0].header assert hdul[0].header["DATASUM"] == "0" assert "CHECKSUM" in hdul[1].header assert hdul[1].header["CHECKSUM"] == "aD1Oa90MaC0Ma90M" assert "DATASUM" in hdul[1].header assert hdul[1].header["DATASUM"] == "1062205743" def test_variable_length_table_data(self): c1 = fits.Column( name="var", format="PJ()", array=np.array([[45.0, 56], np.array([11, 12, 13])], "O"), ) c2 = fits.Column(name="xyz", format="2I", array=[[11, 3], [12, 4]]) tbhdu = fits.BinTableHDU.from_columns([c1, c2]) tbhdu.writeto(self.temp("tmp.fits"), overwrite=True, checksum=True) with fits.open(self.temp("tmp.fits"), checksum=True) as hdul: assert comparerecords(tbhdu.data, hdul[1].data) assert "CHECKSUM" in hdul[0].header assert hdul[0].header["CHECKSUM"] == "D8iBD6ZAD6fAD6ZA" assert "DATASUM" in hdul[0].header assert hdul[0].header["DATASUM"] == "0" assert "CHECKSUM" in hdul[1].header assert hdul[1].header["CHECKSUM"] == "YIGoaIEmZIEmaIEm" assert "DATASUM" in hdul[1].header assert hdul[1].header["DATASUM"] == "1507485" def test_ascii_table_data(self): a1 = np.array(["abc", "def"]) r1 = np.array([11.0, 12.0]) c1 = fits.Column(name="abc", format="A3", array=a1) # This column used to be E format, but the single-precision float lost # too much precision when scaling so it was changed to a D c2 = fits.Column(name="def", format="D", array=r1, bscale=2.3, bzero=0.6) c3 = fits.Column(name="t1", format="I", array=[91, 92, 93]) x = fits.ColDefs([c1, c2, c3]) hdu = fits.TableHDU.from_columns(x) hdu.writeto(self.temp("tmp.fits"), overwrite=True, checksum=True) with fits.open(self.temp("tmp.fits"), checksum=True) as hdul: assert comparerecords(hdu.data, hdul[1].data) assert "CHECKSUM" in hdul[0].header assert hdul[0].header["CHECKSUM"] == "D8iBD6ZAD6fAD6ZA" assert "DATASUM" in hdul[0].header assert hdul[0].header["DATASUM"] == "0" if not sys.platform.startswith("win32"): # The checksum ends up being different on Windows, possibly due # to slight floating point differences assert "CHECKSUM" in hdul[1].header assert hdul[1].header["CHECKSUM"] == "3rKFAoI94oICAoI9" assert "DATASUM" in hdul[1].header assert hdul[1].header["DATASUM"] == "1914653725" def test_compressed_image_data(self): with fits.open(self.data("comp.fits")) as h1: h1.writeto(self.temp("tmp.fits"), overwrite=True, checksum=True) with fits.open(self.temp("tmp.fits"), checksum=True) as h2: assert np.all(h1[1].data == h2[1].data) assert "CHECKSUM" in h2[0].header assert h2[0].header["CHECKSUM"] == "D8iBD6ZAD6fAD6ZA" assert "DATASUM" in h2[0].header assert h2[0].header["DATASUM"] == "0" assert "CHECKSUM" in h2[1].header assert h2[1].header["CHECKSUM"] == "ZeAbdb8aZbAabb7a" assert "DATASUM" in h2[1].header assert h2[1].header["DATASUM"] == "113055149" def test_failing_compressed_datasum(self): """ Regression test for https://github.com/astropy/astropy/issues/4587 """ n = np.ones((10, 10), dtype="float32") comp_hdu = fits.CompImageHDU(n) comp_hdu.writeto(self.temp("tmp.fits"), checksum=True) with fits.open(self.temp("tmp.fits"), checksum=True) as hdul: assert np.all(hdul[1].data == comp_hdu.data) def test_compressed_image_data_int16(self): n = np.arange(100, dtype="int16") hdu = fits.ImageHDU(n) comp_hdu = fits.CompImageHDU(hdu.data, hdu.header) comp_hdu.writeto(self.temp("tmp.fits"), checksum=True) hdu.writeto(self.temp("uncomp.fits"), checksum=True) with fits.open(self.temp("tmp.fits"), checksum=True) as hdul: assert np.all(hdul[1].data == comp_hdu.data) assert np.all(hdul[1].data == hdu.data) assert "CHECKSUM" in hdul[0].header assert hdul[0].header["CHECKSUM"] == "D8iBD6ZAD6fAD6ZA" assert "DATASUM" in hdul[0].header assert hdul[0].header["DATASUM"] == "0" assert "CHECKSUM" in hdul[1].header assert hdul[1]._header["CHECKSUM"] == "J5cCJ5c9J5cAJ5c9" assert "DATASUM" in hdul[1].header assert hdul[1]._header["DATASUM"] == "2453673070" assert "CHECKSUM" in hdul[1].header with fits.open(self.temp("uncomp.fits"), checksum=True) as hdul2: header_comp = hdul[1]._header header_uncomp = hdul2[1].header assert "ZHECKSUM" in header_comp assert "CHECKSUM" in header_uncomp assert header_uncomp["CHECKSUM"] == "ZE94eE91ZE91bE91" assert header_comp["ZHECKSUM"] == header_uncomp["CHECKSUM"] assert "ZDATASUM" in header_comp assert "DATASUM" in header_uncomp assert header_uncomp["DATASUM"] == "160565700" assert header_comp["ZDATASUM"] == header_uncomp["DATASUM"] def test_compressed_image_data_float32(self): n = np.arange(100, dtype="float32") hdu = fits.ImageHDU(n) comp_hdu = fits.CompImageHDU(hdu.data, hdu.header) comp_hdu.writeto(self.temp("tmp.fits"), checksum=True) hdu.writeto(self.temp("uncomp.fits"), checksum=True) with fits.open(self.temp("tmp.fits"), checksum=True) as hdul: assert np.all(hdul[1].data == comp_hdu.data) assert np.all(hdul[1].data == hdu.data) assert "CHECKSUM" in hdul[0].header assert hdul[0].header["CHECKSUM"] == "D8iBD6ZAD6fAD6ZA" assert "DATASUM" in hdul[0].header assert hdul[0].header["DATASUM"] == "0" assert "CHECKSUM" in hdul[1].header assert "DATASUM" in hdul[1].header # The checksum ends up being different on Windows and s390/bigendian, # possibly due to slight floating point differences? See gh-10921. # TODO fix these so they work on all platforms; otherwise pointless. # assert hdul[1]._header['CHECKSUM'] == 'eATIf3SHe9SHe9SH' # assert hdul[1]._header['DATASUM'] == '1277667818' with fits.open(self.temp("uncomp.fits"), checksum=True) as hdul2: header_comp = hdul[1]._header header_uncomp = hdul2[1].header assert "ZHECKSUM" in header_comp assert "CHECKSUM" in header_uncomp assert header_uncomp["CHECKSUM"] == "Cgr5FZo2Cdo2CZo2" assert header_comp["ZHECKSUM"] == header_uncomp["CHECKSUM"] assert "ZDATASUM" in header_comp assert "DATASUM" in header_uncomp assert header_uncomp["DATASUM"] == "2393636889" assert header_comp["ZDATASUM"] == header_uncomp["DATASUM"] def test_open_with_no_keywords(self): hdul = fits.open(self.data("arange.fits"), checksum=True) hdul.close() def test_append(self): hdul = fits.open(self.data("tb.fits")) hdul.writeto(self.temp("tmp.fits"), overwrite=True) n = np.arange(100) fits.append(self.temp("tmp.fits"), n, checksum=True) hdul.close() hdul = fits.open(self.temp("tmp.fits"), checksum=True) assert hdul[0]._checksum is None hdul.close() def test_writeto_convenience(self): n = np.arange(100) fits.writeto(self.temp("tmp.fits"), n, overwrite=True, checksum=True) hdul = fits.open(self.temp("tmp.fits"), checksum=True) self._check_checksums(hdul[0]) hdul.close() def test_hdu_writeto(self): n = np.arange(100, dtype="int16") hdu = fits.ImageHDU(n) hdu.writeto(self.temp("tmp.fits"), checksum=True) hdul = fits.open(self.temp("tmp.fits"), checksum=True) self._check_checksums(hdul[0]) hdul.close() def test_hdu_writeto_existing(self): """ Tests that when using writeto with checksum=True, a checksum and datasum are added to HDUs that did not previously have one. Regression test for https://github.com/spacetelescope/PyFITS/issues/8 """ with fits.open(self.data("tb.fits")) as hdul: hdul.writeto(self.temp("test.fits"), checksum=True) with fits.open(self.temp("test.fits")) as hdul: assert "CHECKSUM" in hdul[0].header # These checksums were verified against CFITSIO assert hdul[0].header["CHECKSUM"] == "7UgqATfo7TfoATfo" assert "DATASUM" in hdul[0].header assert hdul[0].header["DATASUM"] == "0" assert "CHECKSUM" in hdul[1].header assert hdul[1].header["CHECKSUM"] == "99daD8bX98baA8bU" assert "DATASUM" in hdul[1].header assert hdul[1].header["DATASUM"] == "1829680925" def test_datasum_only(self): n = np.arange(100, dtype="int16") hdu = fits.ImageHDU(n) hdu.writeto(self.temp("tmp.fits"), overwrite=True, checksum="datasum") with fits.open(self.temp("tmp.fits"), checksum=True) as hdul: if not (hasattr(hdul[0], "_datasum") and hdul[0]._datasum): pytest.fail(msg="Missing DATASUM keyword") if not (hasattr(hdul[0], "_checksum") and not hdul[0]._checksum): pytest.fail(msg="Non-empty CHECKSUM keyword") def test_open_update_mode_preserve_checksum(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/148 where checksums are being removed from headers when a file is opened in update mode, even though no changes were made to the file. """ self.copy_file("checksum.fits") with fits.open(self.temp("checksum.fits")) as hdul: data = hdul[1].data.copy() hdul = fits.open(self.temp("checksum.fits"), mode="update") hdul.close() with fits.open(self.temp("checksum.fits")) as hdul: assert "CHECKSUM" in hdul[1].header assert "DATASUM" in hdul[1].header assert comparerecords(data, hdul[1].data) def test_open_update_mode_update_checksum(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/148, part 2. This ensures that if a file contains a checksum, the checksum is updated when changes are saved to the file, even if the file was opened with the default of checksum=False. An existing checksum and/or datasum are only stripped if the file is opened with checksum='remove'. """ self.copy_file("checksum.fits") with fits.open(self.temp("checksum.fits")) as hdul: header = hdul[1].header.copy() data = hdul[1].data.copy() with fits.open(self.temp("checksum.fits"), mode="update") as hdul: hdul[1].header["FOO"] = "BAR" hdul[1].data[0]["TIME"] = 42 with fits.open(self.temp("checksum.fits")) as hdul: header2 = hdul[1].header data2 = hdul[1].data assert header2[:-3] == header[:-2] assert "CHECKSUM" in header2 assert "DATASUM" in header2 assert header2["FOO"] == "BAR" assert (data2["TIME"][1:] == data["TIME"][1:]).all() assert data2["TIME"][0] == 42 with fits.open( self.temp("checksum.fits"), mode="update", checksum="remove" ) as hdul: pass with fits.open(self.temp("checksum.fits")) as hdul: header2 = hdul[1].header data2 = hdul[1].data assert header2[:-1] == header[:-2] assert "CHECKSUM" not in header2 assert "DATASUM" not in header2 assert header2["FOO"] == "BAR" assert (data2["TIME"][1:] == data["TIME"][1:]).all() assert data2["TIME"][0] == 42 def test_overwrite_invalid(self): """ Tests that invalid checksum or datasum are overwritten when the file is saved. """ reffile = self.temp("ref.fits") with fits.open(self.data("tb.fits")) as hdul: hdul.writeto(reffile, checksum=True) testfile = self.temp("test.fits") with fits.open(self.data("tb.fits")) as hdul: hdul[0].header["DATASUM"] = "1 " hdul[0].header["CHECKSUM"] = "8UgqATfo7TfoATfo" hdul[1].header["DATASUM"] = "2349680925" hdul[1].header["CHECKSUM"] = "11daD8bX98baA8bU" hdul.writeto(testfile) with fits.open(testfile) as hdul: hdul.writeto(self.temp("test2.fits"), checksum=True) with fits.open(self.temp("test2.fits")) as hdul: with fits.open(reffile) as ref: assert "CHECKSUM" in hdul[0].header # These checksums were verified against CFITSIO assert hdul[0].header["CHECKSUM"] == ref[0].header["CHECKSUM"] assert "DATASUM" in hdul[0].header assert hdul[0].header["DATASUM"] == "0" assert "CHECKSUM" in hdul[1].header assert hdul[1].header["CHECKSUM"] == ref[1].header["CHECKSUM"] assert "DATASUM" in hdul[1].header assert hdul[1].header["DATASUM"] == ref[1].header["DATASUM"] def _check_checksums(self, hdu): if not (hasattr(hdu, "_datasum") and hdu._datasum): pytest.fail(msg="Missing DATASUM keyword") if not (hasattr(hdu, "_checksum") and hdu._checksum): pytest.fail(msg="Missing CHECKSUM keyword")
fc707b96a27b523e00c901a222d883fae232adc62b55bd1a5334d050a79da22c
# Licensed under a 3-clause BSD style license - see PYFITS.rst import numpy as np from astropy.io import fits from .conftest import FitsTestCase class TestDivisionFunctions(FitsTestCase): """Test code units that rely on correct integer division.""" def test_rec_from_string(self): with fits.open(self.data("tb.fits")) as t1: s = t1[1].data.tobytes() np.rec.array( s, dtype=np.dtype( [("c1", ">i4"), ("c2", "|S3"), ("c3", ">f4"), ("c4", "|i1")] ), shape=len(s) // 12, ) def test_card_with_continue(self): h = fits.PrimaryHDU() h.header["abc"] = "abcdefg" * 20 def test_valid_hdu_size(self): with fits.open(self.data("tb.fits")) as t1: assert type(t1[1].size) is int def test_hdu_get_size(self): with fits.open(self.data("tb.fits")) as _: pass def test_section(self, capsys): # section testing with fits.open(self.data("arange.fits")) as fs: assert np.all(fs[0].section[3, 2, 5] == np.array([357]))
72fb86e6245e7e6ef45a614428d592410070eec0fdf8159985dd7886b1f33a58
# Licensed under a 3-clause BSD style license - see LICENSE.rst import os import time import numpy as np import pytest from astropy.io import fits from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH from .conftest import FitsTestCase from .test_table import comparerecords class TestGroupsFunctions(FitsTestCase): def test_open(self): with fits.open(self.data("random_groups.fits")) as hdul: assert isinstance(hdul[0], fits.GroupsHDU) naxes = (3, 1, 128, 1, 1) parameters = ["UU", "VV", "WW", "BASELINE", "DATE"] info = [ ( 0, "PRIMARY", 1, "GroupsHDU", 147, naxes, "float32", "3 Groups 5 Parameters", ) ] assert hdul.info(output=False) == info ghdu = hdul[0] assert ghdu.parnames == parameters assert list(ghdu.data.dtype.names) == parameters + ["DATA"] assert isinstance(ghdu.data, fits.GroupData) # The data should be equal to the number of groups assert ghdu.header["GCOUNT"] == len(ghdu.data) assert ghdu.data.data.shape == (len(ghdu.data),) + naxes[::-1] assert ghdu.data.parnames == parameters assert isinstance(ghdu.data[0], fits.Group) assert len(ghdu.data[0]) == len(parameters) + 1 assert ghdu.data[0].data.shape == naxes[::-1] assert ghdu.data[0].parnames == parameters def test_open_groups_in_update_mode(self): """ Test that opening a file containing a groups HDU in update mode and then immediately closing it does not result in any unnecessary file modifications. Similar to test_image.TestImageFunctions.test_open_scaled_in_update_mode(). """ # Copy the original file before making any possible changes to it self.copy_file("random_groups.fits") mtime = os.stat(self.temp("random_groups.fits")).st_mtime time.sleep(1) fits.open(self.temp("random_groups.fits"), mode="update", memmap=False).close() # Ensure that no changes were made to the file merely by immediately # opening and closing it. assert mtime == os.stat(self.temp("random_groups.fits")).st_mtime def test_random_groups_data_update(self): """ Regression test for https://github.com/astropy/astropy/issues/3730 and for https://github.com/spacetelescope/PyFITS/issues/102 """ self.copy_file("random_groups.fits") with fits.open(self.temp("random_groups.fits"), mode="update") as h: h[0].data["UU"] = 0.42 with fits.open(self.temp("random_groups.fits"), mode="update") as h: assert np.all(h[0].data["UU"] == 0.42) def test_parnames_round_trip(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/130 Ensures that opening a random groups file in update mode or writing it to a new file does not cause any change to the parameter names. """ # Because this test tries to update the random_groups.fits file, let's # make a copy of it first (so that the file doesn't actually get # modified in the off chance that the test fails self.copy_file("random_groups.fits") parameters = ["UU", "VV", "WW", "BASELINE", "DATE"] with fits.open(self.temp("random_groups.fits"), mode="update") as h: assert h[0].parnames == parameters h.flush() # Open again just in read-only mode to ensure the parnames didn't # change with fits.open(self.temp("random_groups.fits")) as h: assert h[0].parnames == parameters h.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as h: assert h[0].parnames == parameters def test_groupdata_slice(self): """ A simple test to ensure that slicing GroupData returns a new, smaller GroupData object, as is the case with a normal FITS_rec. This is a regression test for an as-of-yet unreported issue where slicing GroupData returned a single Group record. """ with fits.open(self.data("random_groups.fits")) as hdul: s = hdul[0].data[1:] assert isinstance(s, fits.GroupData) assert len(s) == 2 assert hdul[0].data.parnames == s.parnames def test_group_slice(self): """ Tests basic slicing a single group record. """ # A very basic slice test with fits.open(self.data("random_groups.fits")) as hdul: g = hdul[0].data[0] s = g[2:4] assert len(s) == 2 assert s[0] == g[2] assert s[-1] == g[-3] s = g[::-1] assert len(s) == 6 assert (s[0] == g[-1]).all() assert s[-1] == g[0] s = g[::2] assert len(s) == 3 assert s[0] == g[0] assert s[1] == g[2] assert s[2] == g[4] def test_create_groupdata(self, home_is_temp): """ Basic test for creating GroupData from scratch. """ imdata = np.arange(100.0) imdata.shape = (10, 1, 1, 2, 5) pdata1 = np.arange(10, dtype=np.float32) + 0.1 pdata2 = 42.0 x = fits.hdu.groups.GroupData( imdata, parnames=["abc", "xyz"], pardata=[pdata1, pdata2], bitpix=-32 ) assert x.parnames == ["abc", "xyz"] assert (x.par("abc") == pdata1).all() assert (x.par("xyz") == ([pdata2] * len(x))).all() assert (x.data == imdata).all() # Test putting the data into a GroupsHDU and round-tripping it ghdu = fits.GroupsHDU(data=x) assert ghdu.parnames == ["abc", "xyz"] assert ghdu.header["GCOUNT"] == 10 filename = self.temp("test.fits") ghdu.writeto(filename) # Exercise the `overwrite` flag with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): ghdu.writeto(filename, overwrite=False) ghdu.writeto(filename, overwrite=True) with fits.open(filename) as h: hdr = h[0].header assert hdr["GCOUNT"] == 10 assert hdr["PCOUNT"] == 2 assert hdr["NAXIS"] == 5 assert hdr["NAXIS1"] == 0 assert hdr["NAXIS2"] == 5 assert hdr["NAXIS3"] == 2 assert hdr["NAXIS4"] == 1 assert hdr["NAXIS5"] == 1 assert h[0].data.parnames == ["abc", "xyz"] assert comparerecords(h[0].data, x) def test_duplicate_parameter(self): """ Tests support for multiple parameters of the same name, and ensures that the data in duplicate parameters are returned as a single summed value. """ imdata = np.arange(100.0) imdata.shape = (10, 1, 1, 2, 5) pdata1 = np.arange(10, dtype=np.float32) + 1 pdata2 = 42.0 x = fits.hdu.groups.GroupData( imdata, parnames=["abc", "xyz", "abc"], pardata=[pdata1, pdata2, pdata1], bitpix=-32, ) assert x.parnames == ["abc", "xyz", "abc"] assert (x.par("abc") == pdata1 * 2).all() assert x[0].par("abc") == 2 # Test setting a parameter x[0].setpar(0, 2) assert x[0].par("abc") == 3 pytest.raises(ValueError, x[0].setpar, "abc", 2) x[0].setpar("abc", (2, 3)) assert x[0].par("abc") == 5 assert x.par("abc")[0] == 5 assert (x.par("abc")[1:] == pdata1[1:] * 2).all() # Test round-trip ghdu = fits.GroupsHDU(data=x) ghdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as h: hdr = h[0].header assert hdr["PCOUNT"] == 3 assert hdr["PTYPE1"] == "abc" assert hdr["PTYPE2"] == "xyz" assert hdr["PTYPE3"] == "abc" assert x.parnames == ["abc", "xyz", "abc"] assert x.dtype.names == ("abc", "xyz", "_abc", "DATA") assert x.par("abc")[0] == 5 assert (x.par("abc")[1:] == pdata1[1:] * 2).all() def test_group_bad_naxis(self): """Test file without NAXIS1 keyword. Regression test for https://github.com/astropy/astropy/issues/9709 """ testfile = os.path.join("invalid", "group_invalid.fits") with fits.open(self.data(testfile)) as hdul: assert len(hdul) == 1 assert hdul[0].header["GROUPS"] assert hdul[0].data is None
39f459b19043af0552ee092bd59e37eec31a9512f4d33401fe3b15bee616ebe6
# Licensed under a 3-clause BSD style license - see PYFITS.rst import numpy as np from astropy.io import fits from .conftest import FitsTestCase class TestNonstandardHdus(FitsTestCase): def test_create_fitshdu(self): """ A round trip test of creating a FitsHDU, adding a FITS file to it, writing the FitsHDU out as part of a new FITS file, and then reading it and recovering the original FITS file. """ self._test_create_fitshdu(compression=False) def test_create_fitshdu_with_compression(self): """Same as test_create_fitshdu but with gzip compression enabled.""" self._test_create_fitshdu(compression=True) def test_create_fitshdu_from_filename(self): """Regression test on `FitsHDU.fromfile`""" # Build up a simple test FITS file a = np.arange(100) phdu = fits.PrimaryHDU(data=a) phdu.header["TEST1"] = "A" phdu.header["TEST2"] = "B" imghdu = fits.ImageHDU(data=a + 1) phdu.header["TEST3"] = "C" phdu.header["TEST4"] = "D" hdul = fits.HDUList([phdu, imghdu]) hdul.writeto(self.temp("test.fits")) fitshdu = fits.FitsHDU.fromfile(self.temp("test.fits")) hdul2 = fitshdu.hdulist assert len(hdul2) == 2 assert fits.FITSDiff(hdul, hdul2).identical def _test_create_fitshdu(self, compression=False): hdul_orig = fits.open(self.data("test0.fits"), do_not_scale_image_data=True) fitshdu = fits.FitsHDU.fromhdulist(hdul_orig, compress=compression) # Just to be meta, let's append to the same hdulist that the fitshdu # encapuslates hdul_orig.append(fitshdu) hdul_orig.writeto(self.temp("tmp.fits"), overwrite=True) del hdul_orig[-1] hdul = fits.open(self.temp("tmp.fits")) assert isinstance(hdul[-1], fits.FitsHDU) wrapped = hdul[-1].hdulist assert isinstance(wrapped, fits.HDUList) assert hdul_orig.info(output=False) == wrapped.info(output=False) assert (hdul[1].data == wrapped[1].data).all() assert (hdul[2].data == wrapped[2].data).all() assert (hdul[3].data == wrapped[3].data).all() assert (hdul[4].data == wrapped[4].data).all() hdul_orig.close() hdul.close()
810bd61f2c16dea28d55758f015d3430f255ca2b9582607906a254aa657ddafb
# Licensed under a 3-clause BSD style license - see PYFITS.rst import copy import io import os import subprocess import sys import numpy as np import pytest from astropy.io import fits from astropy.io.fits.hdu.base import _NonstandardHDU, _ValidHDU from astropy.io.fits.verify import VerifyError, VerifyWarning from astropy.utils.data import get_pkg_data_filenames from astropy.utils.exceptions import AstropyUserWarning from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH from .conftest import FitsTestCase class TestHDUListFunctions(FitsTestCase): def test_update_name(self): with fits.open(self.data("o4sp040b0_raw.fits")) as hdul: hdul[4].name = "Jim" hdul[4].ver = 9 assert hdul[("JIM", 9)].header["extname"] == "JIM" def test_hdu_file_bytes(self): with fits.open(self.data("checksum.fits")) as hdul: res = hdul[0].filebytes() assert res == 11520 res = hdul[1].filebytes() assert res == 8640 def test_hdulist_file_info(self): def test_fileinfo(**kwargs): assert res["datSpan"] == kwargs.get("datSpan", 2880) assert res["resized"] == kwargs.get("resized", False) assert res["filename"] == self.data("checksum.fits") assert res["datLoc"] == kwargs.get("datLoc", 8640) assert res["hdrLoc"] == kwargs.get("hdrLoc", 0) assert res["filemode"] == "readonly" with fits.open(self.data("checksum.fits")) as hdul: res = hdul.fileinfo(0) res = hdul.fileinfo(1) test_fileinfo(datLoc=17280, hdrLoc=11520) hdu = fits.ImageHDU(data=hdul[0].data) hdul.insert(1, hdu) res = hdul.fileinfo(0) test_fileinfo(resized=True) res = hdul.fileinfo(1) test_fileinfo(datSpan=None, resized=True, datLoc=None, hdrLoc=None) res = hdul.fileinfo(2) test_fileinfo(resized=1, datLoc=17280, hdrLoc=11520) def test_create_from_multiple_primary(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/145 Ensure that a validation error occurs when saving an HDUList containing multiple PrimaryHDUs. """ hdul = fits.HDUList([fits.PrimaryHDU(), fits.PrimaryHDU()]) pytest.raises( VerifyError, hdul.writeto, self.temp("temp.fits"), output_verify="exception" ) def test_append_primary_to_empty_list(self): # Tests appending a Simple PrimaryHDU to an empty HDUList. hdul = fits.HDUList() hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32)) hdul.append(hdu) info = [(0, "PRIMARY", 1, "PrimaryHDU", 5, (100,), "int32", "")] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-append.fits")) assert fits.info(self.temp("test-append.fits"), output=False) == info def test_append_extension_to_empty_list(self): """Tests appending a Simple ImageHDU to an empty HDUList.""" hdul = fits.HDUList() hdu = fits.ImageHDU(np.arange(100, dtype=np.int32)) hdul.append(hdu) info = [(0, "PRIMARY", 1, "PrimaryHDU", 4, (100,), "int32", "")] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-append.fits")) assert fits.info(self.temp("test-append.fits"), output=False) == info def test_append_table_extension_to_empty_list(self): """Tests appending a Simple Table ExtensionHDU to a empty HDUList.""" hdul = fits.HDUList() with fits.open(self.data("tb.fits")) as hdul1: hdul.append(hdul1[1]) info = [ (0, "PRIMARY", 1, "PrimaryHDU", 4, (), "", ""), (1, "", 1, "BinTableHDU", 24, "2R x 4C", "[1J, 3A, 1E, 1L]", ""), ] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-append.fits")) assert fits.info(self.temp("test-append.fits"), output=False) == info def test_append_groupshdu_to_empty_list(self): """Tests appending a Simple GroupsHDU to an empty HDUList.""" hdul = fits.HDUList() hdu = fits.GroupsHDU() hdul.append(hdu) info = [(0, "PRIMARY", 1, "GroupsHDU", 8, (), "", "1 Groups 0 Parameters")] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-append.fits")) assert fits.info(self.temp("test-append.fits"), output=False) == info def test_append_primary_to_non_empty_list(self): """Tests appending a Simple PrimaryHDU to a non-empty HDUList.""" with fits.open(self.data("arange.fits")) as hdul: hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32)) hdul.append(hdu) info = [ (0, "PRIMARY", 1, "PrimaryHDU", 7, (11, 10, 7), "int32", ""), (1, "", 1, "ImageHDU", 6, (100,), "int32", ""), ] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-append.fits")) assert fits.info(self.temp("test-append.fits"), output=False) == info def test_append_extension_to_non_empty_list(self): """Tests appending a Simple ExtensionHDU to a non-empty HDUList.""" with fits.open(self.data("tb.fits")) as hdul: hdul.append(hdul[1]) info = [ (0, "PRIMARY", 1, "PrimaryHDU", 11, (), "", ""), (1, "", 1, "BinTableHDU", 24, "2R x 4C", "[1J, 3A, 1E, 1L]", ""), (2, "", 1, "BinTableHDU", 24, "2R x 4C", "[1J, 3A, 1E, 1L]", ""), ] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-append.fits")) assert fits.info(self.temp("test-append.fits"), output=False) == info def test_append_groupshdu_to_non_empty_list(self): """Tests appending a Simple GroupsHDU to an empty HDUList.""" hdul = fits.HDUList() hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32)) hdul.append(hdu) hdu = fits.GroupsHDU() with pytest.raises(ValueError): hdul.append(hdu) def test_insert_primary_to_empty_list(self): """Tests inserting a Simple PrimaryHDU to an empty HDUList.""" hdul = fits.HDUList() hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32)) hdul.insert(0, hdu) info = [(0, "PRIMARY", 1, "PrimaryHDU", 5, (100,), "int32", "")] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-insert.fits")) assert fits.info(self.temp("test-insert.fits"), output=False) == info def test_insert_extension_to_empty_list(self): """Tests inserting a Simple ImageHDU to an empty HDUList.""" hdul = fits.HDUList() hdu = fits.ImageHDU(np.arange(100, dtype=np.int32)) hdul.insert(0, hdu) info = [(0, "PRIMARY", 1, "PrimaryHDU", 4, (100,), "int32", "")] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-insert.fits")) assert fits.info(self.temp("test-insert.fits"), output=False) == info def test_insert_table_extension_to_empty_list(self): """Tests inserting a Simple Table ExtensionHDU to a empty HDUList.""" hdul = fits.HDUList() with fits.open(self.data("tb.fits")) as hdul1: hdul.insert(0, hdul1[1]) info = [ (0, "PRIMARY", 1, "PrimaryHDU", 4, (), "", ""), (1, "", 1, "BinTableHDU", 24, "2R x 4C", "[1J, 3A, 1E, 1L]", ""), ] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-insert.fits")) assert fits.info(self.temp("test-insert.fits"), output=False) == info def test_insert_groupshdu_to_empty_list(self): """Tests inserting a Simple GroupsHDU to an empty HDUList.""" hdul = fits.HDUList() hdu = fits.GroupsHDU() hdul.insert(0, hdu) info = [(0, "PRIMARY", 1, "GroupsHDU", 8, (), "", "1 Groups 0 Parameters")] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-insert.fits")) assert fits.info(self.temp("test-insert.fits"), output=False) == info def test_insert_primary_to_non_empty_list(self): """Tests inserting a Simple PrimaryHDU to a non-empty HDUList.""" with fits.open(self.data("arange.fits")) as hdul: hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32)) hdul.insert(1, hdu) info = [ (0, "PRIMARY", 1, "PrimaryHDU", 7, (11, 10, 7), "int32", ""), (1, "", 1, "ImageHDU", 6, (100,), "int32", ""), ] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-insert.fits")) assert fits.info(self.temp("test-insert.fits"), output=False) == info def test_insert_extension_to_non_empty_list(self): """Tests inserting a Simple ExtensionHDU to a non-empty HDUList.""" with fits.open(self.data("tb.fits")) as hdul: hdul.insert(1, hdul[1]) info = [ (0, "PRIMARY", 1, "PrimaryHDU", 11, (), "", ""), (1, "", 1, "BinTableHDU", 24, "2R x 4C", "[1J, 3A, 1E, 1L]", ""), (2, "", 1, "BinTableHDU", 24, "2R x 4C", "[1J, 3A, 1E, 1L]", ""), ] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-insert.fits")) assert fits.info(self.temp("test-insert.fits"), output=False) == info def test_insert_groupshdu_to_non_empty_list(self): """Tests inserting a Simple GroupsHDU to an empty HDUList.""" hdul = fits.HDUList() hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32)) hdul.insert(0, hdu) hdu = fits.GroupsHDU() with pytest.raises(ValueError): hdul.insert(1, hdu) info = [ (0, "PRIMARY", 1, "GroupsHDU", 8, (), "", "1 Groups 0 Parameters"), (1, "", 1, "ImageHDU", 6, (100,), "int32", ""), ] hdul.insert(0, hdu) assert hdul.info(output=False) == info hdul.writeto(self.temp("test-insert.fits")) assert fits.info(self.temp("test-insert.fits"), output=False) == info def test_insert_groupshdu_to_begin_of_hdulist_with_groupshdu(self): """ Tests inserting a Simple GroupsHDU to the beginning of an HDUList that that already contains a GroupsHDU. """ hdul = fits.HDUList() hdu = fits.GroupsHDU() hdul.insert(0, hdu) with pytest.raises(ValueError): hdul.insert(0, hdu) def test_insert_extension_to_primary_in_non_empty_list(self): # Tests inserting a Simple ExtensionHDU to a non-empty HDUList. with fits.open(self.data("tb.fits")) as hdul: hdul.insert(0, hdul[1]) info = [ (0, "PRIMARY", 1, "PrimaryHDU", 4, (), "", ""), (1, "", 1, "BinTableHDU", 24, "2R x 4C", "[1J, 3A, 1E, 1L]", ""), (2, "", 1, "ImageHDU", 12, (), "", ""), (3, "", 1, "BinTableHDU", 24, "2R x 4C", "[1J, 3A, 1E, 1L]", ""), ] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-insert.fits")) assert fits.info(self.temp("test-insert.fits"), output=False) == info def test_insert_image_extension_to_primary_in_non_empty_list(self): """ Tests inserting a Simple Image ExtensionHDU to a non-empty HDUList as the primary HDU. """ with fits.open(self.data("tb.fits")) as hdul: hdu = fits.ImageHDU(np.arange(100, dtype=np.int32)) hdul.insert(0, hdu) info = [ (0, "PRIMARY", 1, "PrimaryHDU", 5, (100,), "int32", ""), (1, "", 1, "ImageHDU", 12, (), "", ""), (2, "", 1, "BinTableHDU", 24, "2R x 4C", "[1J, 3A, 1E, 1L]", ""), ] assert hdul.info(output=False) == info hdul.writeto(self.temp("test-insert.fits")) assert fits.info(self.temp("test-insert.fits"), output=False) == info def test_filename(self, home_is_data): """Tests the HDUList filename method.""" with fits.open(self.data("tb.fits")) as hdul: name = hdul.filename() assert name == os.path.expanduser(self.data("tb.fits")) def test_file_like(self): """ Tests the use of a file like object with no tell or seek methods in HDUList.writeto(), HDULIST.flush() or astropy.io.fits.writeto() """ hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32)) hdul = fits.HDUList() hdul.append(hdu) tmpfile = open(self.temp("tmpfile.fits"), "wb") hdul.writeto(tmpfile) tmpfile.close() info = [(0, "PRIMARY", 1, "PrimaryHDU", 5, (100,), "int32", "")] assert fits.info(self.temp("tmpfile.fits"), output=False) == info def test_file_like_2(self): hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32)) tmpfile = open(self.temp("tmpfile.fits"), "wb") hdul = fits.open(tmpfile, mode="ostream") hdul.append(hdu) hdul.flush() tmpfile.close() hdul.close() info = [(0, "PRIMARY", 1, "PrimaryHDU", 5, (100,), "int32", "")] assert fits.info(self.temp("tmpfile.fits"), output=False) == info def test_file_like_3(self): tmpfile = open(self.temp("tmpfile.fits"), "wb") fits.writeto(tmpfile, np.arange(100, dtype=np.int32)) tmpfile.close() info = [(0, "PRIMARY", 1, "PrimaryHDU", 5, (100,), "int32", "")] assert fits.info(self.temp("tmpfile.fits"), output=False) == info def test_shallow_copy(self): """ Tests that `HDUList.__copy__()` and `HDUList.copy()` return a shallow copy (regression test for #7211). """ n = np.arange(10.0) primary_hdu = fits.PrimaryHDU(n) hdu = fits.ImageHDU(n) hdul = fits.HDUList([primary_hdu, hdu]) for hdulcopy in (hdul.copy(), copy.copy(hdul)): assert isinstance(hdulcopy, fits.HDUList) assert hdulcopy is not hdul assert hdulcopy[0] is hdul[0] assert hdulcopy[1] is hdul[1] def test_deep_copy(self): """ Tests that `HDUList.__deepcopy__()` returns a deep copy. """ n = np.arange(10.0) primary_hdu = fits.PrimaryHDU(n) hdu = fits.ImageHDU(n) hdul = fits.HDUList([primary_hdu, hdu]) hdulcopy = copy.deepcopy(hdul) assert isinstance(hdulcopy, fits.HDUList) assert hdulcopy is not hdul for index in range(len(hdul)): assert hdulcopy[index] is not hdul[index] assert hdulcopy[index].header == hdul[index].header np.testing.assert_array_equal(hdulcopy[index].data, hdul[index].data) def test_new_hdu_extname(self): """ Tests that new extension HDUs that are added to an HDUList can be properly indexed by their EXTNAME/EXTVER (regression test for ticket:48). """ with fits.open(self.data("test0.fits")) as f: hdul = fits.HDUList() hdul.append(f[0].copy()) hdu = fits.ImageHDU(header=f[1].header) hdul.append(hdu) assert hdul[1].header["EXTNAME"] == "SCI" assert hdul[1].header["EXTVER"] == 1 assert hdul.index_of(("SCI", 1)) == 1 assert hdul.index_of(hdu) == len(hdul) - 1 def test_update_filelike(self): """Test opening a file-like object in update mode and resizing the HDU. """ sf = io.BytesIO() arr = np.zeros((100, 100)) hdu = fits.PrimaryHDU(data=arr) hdu.writeto(sf) sf.seek(0) arr = np.zeros((200, 200)) hdul = fits.open(sf, mode="update") hdul[0].data = arr hdul.flush() sf.seek(0) hdul = fits.open(sf) assert len(hdul) == 1 assert (hdul[0].data == arr).all() def test_flush_readonly(self): """Test flushing changes to a file opened in a read only mode.""" oldmtime = os.stat(self.data("test0.fits")).st_mtime with fits.open(self.data("test0.fits")) as hdul: hdul[0].header["FOO"] = "BAR" with pytest.warns(AstropyUserWarning, match="mode is not supported") as w: hdul.flush() assert len(w) == 1 assert oldmtime == os.stat(self.data("test0.fits")).st_mtime def test_fix_extend_keyword(self): hdul = fits.HDUList() hdul.append(fits.PrimaryHDU()) hdul.append(fits.ImageHDU()) del hdul[0].header["EXTEND"] hdul.verify("silentfix") assert "EXTEND" in hdul[0].header assert hdul[0].header["EXTEND"] is True def test_fix_malformed_naxisj(self): """ Tests that malformed NAXISj values are fixed sensibly. """ hdu = fits.open(self.data("arange.fits")) # Malform NAXISj header data hdu[0].header["NAXIS1"] = 11.0 hdu[0].header["NAXIS2"] = "10.0" hdu[0].header["NAXIS3"] = "7" # Axes cache needs to be malformed as well hdu[0]._axes = [11.0, "10.0", "7"] # Perform verification including the fix hdu.verify("silentfix") # Check that malformed data was converted assert hdu[0].header["NAXIS1"] == 11 assert hdu[0].header["NAXIS2"] == 10 assert hdu[0].header["NAXIS3"] == 7 hdu.close() def test_fix_wellformed_naxisj(self): """ Tests that wellformed NAXISj values are not modified. """ hdu = fits.open(self.data("arange.fits")) # Fake new NAXISj header data hdu[0].header["NAXIS1"] = 768 hdu[0].header["NAXIS2"] = 64 hdu[0].header["NAXIS3"] = 8 # Axes cache needs to be faked as well hdu[0]._axes = [768, 64, 8] # Perform verification including the fix hdu.verify("silentfix") # Check that malformed data was converted assert hdu[0].header["NAXIS1"] == 768 assert hdu[0].header["NAXIS2"] == 64 assert hdu[0].header["NAXIS3"] == 8 hdu.close() def test_new_hdulist_extend_keyword(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/114 Tests that adding a PrimaryHDU to a new HDUList object updates the EXTEND keyword on that HDU. """ h0 = fits.Header() hdu = fits.PrimaryHDU(header=h0) sci = fits.ImageHDU(data=np.array(10)) image = fits.HDUList([hdu, sci]) image.writeto(self.temp("temp.fits")) assert "EXTEND" in hdu.header assert hdu.header["EXTEND"] is True def test_replace_memmaped_array(self, home_is_temp): # Copy the original before we modify it with fits.open(self.data("test0.fits")) as hdul: hdul.writeto(self.temp("temp.fits")) hdul = fits.open(self.temp("temp.fits"), mode="update", memmap=True) old_data = hdul[1].data.copy() hdul[1].data = hdul[1].data + 1 hdul.close() with fits.open(self.temp("temp.fits"), memmap=True) as hdul: assert ((old_data + 1) == hdul[1].data).all() def test_open_file_with_bad_file_padding(self): """ Test warning when opening files with extra padding at the end. See https://github.com/astropy/astropy/issues/4351 """ # write some arbitrary data to a FITS file fits.writeto(self.temp("temp.fits"), np.arange(100)) # append some arbitrary number of zeros to the end with open(self.temp("temp.fits"), "ab") as fobj: fobj.write(b"\x00" * 1234) with pytest.warns( AstropyUserWarning, match="Unexpected extra padding at the end of the file." ) as w: with fits.open(self.temp("temp.fits")) as fobj: fobj.info() assert len(w) == 1 @pytest.mark.filterwarnings("ignore:Unexpected extra padding") def test_open_file_with_end_padding(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/106 Open files with end padding bytes. """ with fits.open(self.data("test0.fits"), do_not_scale_image_data=True) as hdul: info = hdul.info(output=False) hdul.writeto(self.temp("temp.fits")) with open(self.temp("temp.fits"), "ab") as f: f.seek(0, os.SEEK_END) f.write(b"\0" * 2880) assert info == fits.info( self.temp("temp.fits"), output=False, do_not_scale_image_data=True ) def test_open_file_with_bad_header_padding(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/136 Open files with nulls for header block padding instead of spaces. """ a = np.arange(100).reshape(10, 10) hdu = fits.PrimaryHDU(data=a) hdu.writeto(self.temp("temp.fits")) # Figure out where the header padding begins and fill it with nulls end_card_pos = str(hdu.header).index("END" + " " * 77) padding_start = end_card_pos + 80 padding_len = 2880 - padding_start with open(self.temp("temp.fits"), "r+b") as f: f.seek(padding_start) f.write(b"\0" * padding_len) with pytest.warns( AstropyUserWarning, match="contains null bytes instead of spaces" ) as w: with fits.open(self.temp("temp.fits")) as hdul: assert (hdul[0].data == a).all() assert len(w) == 1 assert len(hdul) == 1 assert str(hdul[0].header) == str(hdu.header) def test_update_with_truncated_header(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/148 Test that saving an update where the header is shorter than the original header doesn't leave a stump from the old header in the file. """ data = np.arange(100) hdu = fits.PrimaryHDU(data=data) idx = 1 while len(hdu.header) < 34: hdu.header[f"TEST{idx}"] = idx idx += 1 hdu.writeto(self.temp("temp.fits"), checksum=True) with fits.open(self.temp("temp.fits"), mode="update") as hdul: # Modify the header, forcing it to be rewritten hdul[0].header["TEST1"] = 2 with fits.open(self.temp("temp.fits")) as hdul: assert (hdul[0].data == data).all() def test_update_resized_header(self, home_is_temp): """ Test saving updates to a file where the header is one block smaller than before, and in the case where the heade ris one block larger than before. """ data = np.arange(100) hdu = fits.PrimaryHDU(data=data) idx = 1 while len(str(hdu.header)) <= 2880: hdu.header[f"TEST{idx}"] = idx idx += 1 orig_header = hdu.header.copy() hdu.writeto(self.temp("temp.fits")) with fits.open(self.temp("temp.fits"), mode="update") as hdul: while len(str(hdul[0].header)) > 2880: del hdul[0].header[-1] with fits.open(self.temp("temp.fits")) as hdul: assert hdul[0].header == orig_header[:-1] assert (hdul[0].data == data).all() with fits.open(self.temp("temp.fits"), mode="update") as hdul: idx = 101 while len(str(hdul[0].header)) <= 2880 * 2: hdul[0].header[f"TEST{idx}"] = idx idx += 1 # Touch something in the data too so that it has to be rewritten hdul[0].data[0] = 27 with fits.open(self.temp("temp.fits")) as hdul: assert hdul[0].header[:-37] == orig_header[:-1] assert hdul[0].data[0] == 27 assert (hdul[0].data[1:] == data[1:]).all() def test_update_resized_header2(self, home_is_temp): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/150 This is similar to test_update_resized_header, but specifically tests a case of multiple consecutive flush() calls on the same HDUList object, where each flush() requires a resize. """ data1 = np.arange(100) data2 = np.arange(100) + 100 phdu = fits.PrimaryHDU(data=data1) hdu = fits.ImageHDU(data=data2) phdu.writeto(self.temp("temp.fits")) with fits.open(self.temp("temp.fits"), mode="append") as hdul: hdul.append(hdu) with fits.open(self.temp("temp.fits"), mode="update") as hdul: idx = 1 while len(str(hdul[0].header)) <= 2880 * 2: hdul[0].header[f"TEST{idx}"] = idx idx += 1 hdul.flush() hdul.append(hdu) with fits.open(self.temp("temp.fits")) as hdul: assert (hdul[0].data == data1).all() assert hdul[1].header == hdu.header assert (hdul[1].data == data2).all() assert (hdul[2].data == data2).all() def test_hdul_fromstring(self): """ Test creating the HDUList structure in memory from a string containing an entire FITS file. This is similar to test_hdu_fromstring but for an entire multi-extension FITS file at once. """ # Tests HDUList.fromstring for all of Astropy's built in test files def test_fromstring(filename): with fits.open(filename) as hdul: orig_info = hdul.info(output=False) with open(filename, "rb") as f: dat = f.read() hdul2 = fits.HDUList.fromstring(dat) assert orig_info == hdul2.info(output=False) for idx in range(len(hdul)): assert hdul[idx].header == hdul2[idx].header if hdul[idx].data is None or hdul2[idx].data is None: assert hdul[idx].data == hdul2[idx].data elif hdul[idx].data.dtype.fields and hdul2[idx].data.dtype.fields: # Compare tables for n in hdul[idx].data.names: c1 = hdul[idx].data[n] c2 = hdul2[idx].data[n] assert (c1 == c2).all() elif any(dim == 0 for dim in hdul[idx].data.shape) or any( dim == 0 for dim in hdul2[idx].data.shape ): # For some reason some combinations of Python and Numpy # on Windows result in MemoryErrors when trying to work # on memmap arrays with more than one dimension but # some dimensions of size zero, so include a special # case for that return hdul[idx].data.shape == hdul2[idx].data.shape else: np.testing.assert_array_equal(hdul[idx].data, hdul2[idx].data) for filename in get_pkg_data_filenames("data", pattern="*.fits"): if sys.platform == "win32" and filename.endswith("zerowidth.fits"): # Running this test on this file causes a crash in some # versions of Numpy on Windows. See ticket: # https://aeon.stsci.edu/ssb/trac/pyfits/ticket/174 continue elif filename.endswith(("variable_length_table.fits", "theap-gap.fits")): # Comparing variable length arrays is non-trivial and thus # skipped at this point. # TODO: That's probably possible, so one could make it work. continue test_fromstring(filename) # Test that creating an HDUList from something silly raises a TypeError pytest.raises(TypeError, fits.HDUList.fromstring, ["a", "b", "c"]) @pytest.mark.filterwarnings("ignore:Saving a backup") def test_save_backup(self, home_is_temp): """Test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/121 Save backup of file before flushing changes. """ self.copy_file("scale.fits") with fits.open( self.temp("scale.fits"), mode="update", save_backup=True ) as hdul: # Make some changes to the original file to force its header # and data to be rewritten hdul[0].header["TEST"] = "TEST" # This emits warning that needs to be ignored at the # pytest.mark.filterwarnings level. hdul[0].data[0] = 0 assert os.path.exists(os.path.expanduser(self.temp("scale.fits.bak"))) with fits.open(self.data("scale.fits"), do_not_scale_image_data=True) as hdul1: with fits.open( self.temp("scale.fits.bak"), do_not_scale_image_data=True ) as hdul2: assert hdul1[0].header == hdul2[0].header assert (hdul1[0].data == hdul2[0].data).all() with fits.open( self.temp("scale.fits"), mode="update", save_backup=True ) as hdul: # One more time to see if multiple backups are made hdul[0].header["TEST2"] = "TEST" hdul[0].data[0] = 1 assert os.path.exists(os.path.expanduser(self.temp("scale.fits.bak"))) assert os.path.exists(os.path.expanduser(self.temp("scale.fits.bak.1"))) def test_replace_mmap_data(self): """Regression test for https://github.com/spacetelescope/PyFITS/issues/25 Replacing the mmap'd data of one file with mmap'd data from a different file should work. """ arr_a = np.arange(10) arr_b = arr_a * 2 def test(mmap_a, mmap_b): hdu_a = fits.PrimaryHDU(data=arr_a) hdu_a.writeto(self.temp("test_a.fits"), overwrite=True) hdu_b = fits.PrimaryHDU(data=arr_b) hdu_b.writeto(self.temp("test_b.fits"), overwrite=True) with fits.open( self.temp("test_a.fits"), mode="update", memmap=mmap_a ) as hdul_a: with fits.open(self.temp("test_b.fits"), memmap=mmap_b) as hdul_b: hdul_a[0].data = hdul_b[0].data with fits.open(self.temp("test_a.fits")) as hdul_a: assert np.all(hdul_a[0].data == arr_b) test(True, True) # Repeat the same test but this time don't mmap A test(False, True) # Finally, without mmaping B test(True, False) def test_replace_mmap_data_2(self): """Regression test for https://github.com/spacetelescope/PyFITS/issues/25 Replacing the mmap'd data of one file with mmap'd data from a different file should work. Like test_replace_mmap_data but with table data instead of image data. """ arr_a = np.arange(10) arr_b = arr_a * 2 def test(mmap_a, mmap_b): col_a = fits.Column(name="a", format="J", array=arr_a) col_b = fits.Column(name="b", format="J", array=arr_b) hdu_a = fits.BinTableHDU.from_columns([col_a]) hdu_a.writeto(self.temp("test_a.fits"), overwrite=True) hdu_b = fits.BinTableHDU.from_columns([col_b]) hdu_b.writeto(self.temp("test_b.fits"), overwrite=True) with fits.open( self.temp("test_a.fits"), mode="update", memmap=mmap_a ) as hdul_a: with fits.open(self.temp("test_b.fits"), memmap=mmap_b) as hdul_b: hdul_a[1].data = hdul_b[1].data with fits.open(self.temp("test_a.fits")) as hdul_a: assert "b" in hdul_a[1].columns.names assert "a" not in hdul_a[1].columns.names assert np.all(hdul_a[1].data["b"] == arr_b) test(True, True) # Repeat the same test but this time don't mmap A test(False, True) # Finally, without mmaping B test(True, False) def test_extname_in_hdulist(self): """ Tests to make sure that the 'in' operator works. Regression test for https://github.com/astropy/astropy/issues/3060 """ with fits.open(self.data("o4sp040b0_raw.fits")) as hdulist: hdulist.append(fits.ImageHDU(name="a")) assert "a" in hdulist assert "A" in hdulist assert ("a", 1) in hdulist assert ("A", 1) in hdulist assert "b" not in hdulist assert ("a", 2) not in hdulist assert ("b", 1) not in hdulist assert ("b", 2) not in hdulist assert hdulist[0] in hdulist assert fits.ImageHDU() not in hdulist def test_overwrite(self, home_is_temp): hdulist = fits.HDUList([fits.PrimaryHDU()]) hdulist.writeto(self.temp("test_overwrite.fits")) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): hdulist.writeto(self.temp("test_overwrite.fits"), overwrite=False) hdulist.writeto(self.temp("test_overwrite.fits"), overwrite=True) def test_invalid_hdu_key_in_contains(self): """ Make sure invalid keys in the 'in' operator return False. Regression test for https://github.com/astropy/astropy/issues/5583 """ hdulist = fits.HDUList(fits.PrimaryHDU()) hdulist.append(fits.ImageHDU()) hdulist.append(fits.ImageHDU()) # A more or less random assortment of things which are not valid keys. bad_keys = [None, 3.5, {}] for key in bad_keys: assert not (key in hdulist) def test_iteration_of_lazy_loaded_hdulist(self): """ Regression test for https://github.com/astropy/astropy/issues/5585 """ hdulist = fits.HDUList(fits.PrimaryHDU()) hdulist.append(fits.ImageHDU(name="SCI")) hdulist.append(fits.ImageHDU(name="SCI")) hdulist.append(fits.ImageHDU(name="nada")) hdulist.append(fits.ImageHDU(name="SCI")) filename = self.temp("many_extension.fits") hdulist.writeto(filename) f = fits.open(filename) # Check that all extensions are read if f is not sliced all_exts = [ext for ext in f] assert len(all_exts) == 5 # Reload the file to ensure we are still lazy loading f.close() f = fits.open(filename) # Try a simple slice with no conditional on the ext. This is essentially # the reported failure. all_exts_but_zero = [ext for ext in f[1:]] assert len(all_exts_but_zero) == 4 # Reload the file to ensure we are still lazy loading f.close() f = fits.open(filename) # Check whether behavior is proper if the upper end of the slice is not # omitted. read_exts = [ext for ext in f[1:4] if ext.header["EXTNAME"] == "SCI"] assert len(read_exts) == 2 f.close() def test_read_non_standard_hdu(self): filename = self.temp("bad-fits.fits") hdu = fits.PrimaryHDU() hdu.header["FOO"] = "BAR" buf = io.BytesIO() hdu.writeto(buf) buf.seek(0) hdustr = buf.read() hdustr = hdustr.replace( b"SIMPLE = T", b"SIMPLE = F" ) with open(filename, mode="wb") as f: f.write(hdustr) with fits.open(filename) as hdul: assert isinstance(hdul[0], _NonstandardHDU) assert hdul[0].header["FOO"] == "BAR" def test_proper_error_raised_on_non_fits_file(self): filename = self.temp("not-fits.fits") with open(filename, mode="w", encoding="utf=8") as f: f.write("Not a FITS file") match = ( "No SIMPLE card found, this file does not appear to be a valid FITS file" ) # This should raise an OSError because there is no end card. with pytest.raises(OSError, match=match): fits.open(filename) with pytest.raises(OSError, match=match): fits.open(filename, mode="append") with pytest.raises(OSError, match=match): fits.open(filename, mode="update") def test_proper_error_raised_on_invalid_fits_file(self): filename = self.temp("bad-fits.fits") hdu = fits.PrimaryHDU() hdu.header["FOO"] = "BAR" buf = io.BytesIO() hdu.writeto(buf) # write 80 additional bytes so the block will have the correct size buf.write(b" " * 80) buf.seek(0) buf.seek(80) # now remove the SIMPLE card with open(filename, mode="wb") as f: f.write(buf.read()) match = ( "No SIMPLE card found, this file does not appear to be a valid FITS file" ) # This should raise an OSError because there is no end card. with pytest.raises(OSError, match=match): fits.open(filename) with pytest.raises(OSError, match=match): fits.open(filename, mode="append") with pytest.raises(OSError, match=match): fits.open(filename, mode="update") with fits.open(filename, ignore_missing_simple=True) as hdul: assert isinstance(hdul[0], _ValidHDU) assert hdul[0].header["FOO"] == "BAR" def test_warning_raised_on_non_standard_simple_card(self): filename = self.temp("bad-fits.fits") hdu = fits.PrimaryHDU() hdu.header["FOO"] = "BAR" buf = io.BytesIO() hdu.writeto(buf) # change the simple card format buf.seek(0) buf.write(b"SIMPLE = T ") buf.seek(0) with open(filename, mode="wb") as f: f.write(buf.read()) match = "Found a SIMPLE card but its format doesn't respect the FITS Standard" with pytest.warns(VerifyWarning, match=match): fits.open(filename) with pytest.warns(VerifyWarning, match=match): fits.open(filename, mode="append") with pytest.warns(VerifyWarning, match=match): fits.open(filename, mode="update") with fits.open(filename, ignore_missing_simple=True) as hdul: assert isinstance(hdul[0], _ValidHDU) assert hdul[0].header["FOO"] == "BAR" # change the simple card format buf.seek(0) buf.write(b"SIMPLE = T / This is a FITS file") buf.seek(0) with open(filename, mode="wb") as f: f.write(buf.read()) with pytest.warns(VerifyWarning, match=match): fits.open(filename) def test_proper_error_raised_on_non_fits_file_with_unicode(self): """ Regression test for https://github.com/astropy/astropy/issues/5594 The failure shows up when (in python 3+) you try to open a file with unicode content that is not actually a FITS file. See: https://github.com/astropy/astropy/issues/5594#issuecomment-266583218 """ filename = self.temp("not-fits-with-unicode.fits") with open(filename, mode="w", encoding="utf=8") as f: f.write("Ce\xe7i ne marche pas") # This should raise an OSError because there is no end card. with pytest.raises( OSError, match=( "No SIMPLE card found, this file " "does not appear to be a valid FITS file" ), ): fits.open(filename) def test_no_resource_warning_raised_on_non_fits_file(self): """ Regression test for https://github.com/astropy/astropy/issues/6168 The ResourceWarning shows up when (in python 3+) you try to open a non-FITS file when using a filename. """ # To avoid creating the file multiple times the tests are # all included in one test file. See the discussion to the # PR at https://github.com/astropy/astropy/issues/6168 # filename = self.temp("not-fits.fits") with open(filename, mode="w") as f: f.write("# header line\n") f.write("0.1 0.2\n") # Opening the file should raise an OSError however the file # is opened (there are two distinct code paths, depending on # whether ignore_missing_end is True or False). # # Explicit tests are added to make sure the file handle is not # closed when passed in to fits.open. In this case the ResourceWarning # was not raised. # Make sure that files opened by the user are not closed with open(filename, mode="rb") as f: with pytest.raises(OSError): fits.open(f, ignore_missing_end=False) assert not f.closed with open(filename, mode="rb") as f: with pytest.raises(OSError), pytest.warns(VerifyWarning): fits.open(f, ignore_missing_end=True) assert not f.closed with pytest.raises(OSError): fits.open(filename, ignore_missing_end=False) with pytest.raises(OSError), pytest.warns(VerifyWarning): fits.open(filename, ignore_missing_end=True) def test_pop_with_lazy_load(self): filename = self.data("checksum.fits") with fits.open(filename) as hdul: # Try popping the hdulist before doing anything else. This makes sure # that https://github.com/astropy/astropy/issues/7185 is fixed. hdu = hdul.pop() assert len(hdul) == 1 # Read the file again and try popping from the beginning with fits.open(filename) as hdul2: hdu2 = hdul2.pop(0) assert len(hdul2) == 1 # Just a sanity check with fits.open(filename) as hdul3: assert len(hdul3) == 2 assert hdul3[0].header == hdu2.header assert hdul3[1].header == hdu.header def test_pop_extname(self): with fits.open(self.data("o4sp040b0_raw.fits")) as hdul: assert len(hdul) == 7 hdu1 = hdul[1] hdu4 = hdul[4] hdu_popped = hdul.pop(("SCI", 2)) assert len(hdul) == 6 assert hdu_popped is hdu4 hdu_popped = hdul.pop("SCI") assert len(hdul) == 5 assert hdu_popped is hdu1 # Skip due to https://github.com/astropy/astropy/issues/8916 @pytest.mark.skipif( sys.platform.startswith("win32"), reason="Cannot test on Windows" ) def test_write_hdulist_to_stream(self): """ Unit test for https://github.com/astropy/astropy/issues/7435 to ensure that an HDUList can be written to a stream. """ data = np.array([[1, 2, 3], [4, 5, 6]]) hdu = fits.PrimaryHDU(data) hdulist = fits.HDUList([hdu]) with open(self.temp("test.fits"), "wb") as fout: with subprocess.Popen(["cat"], stdin=subprocess.PIPE, stdout=fout) as p: hdulist.writeto(p.stdin) def test_output_verify(self): hdul = fits.HDUList([fits.PrimaryHDU()]) hdul[0].header["FOOBAR"] = 42 hdul.writeto(self.temp("test.fits")) with open(self.temp("test.fits"), "rb") as f: data = f.read() # create invalid card data = data.replace(b"FOOBAR =", b"FOOBAR = ") with open(self.temp("test2.fits"), "wb") as f: f.write(data) with pytest.raises(VerifyError): with fits.open(self.temp("test2.fits"), mode="update") as hdul: hdul[0].header["MORE"] = "here" with pytest.warns(VerifyWarning) as ww: with fits.open( self.temp("test2.fits"), mode="update", output_verify="fix+warn" ) as hdul: hdul[0].header["MORE"] = "here" assert len(ww) == 6 msg = "Card 'FOOBAR ' is not FITS standard (equal sign not at column 8)" assert msg in str(ww[3].message)
f19da1f064d14d6f73bf8702a3daad9e1d0ea15872351ef1ad5b6a8bd92aa56a
# Licensed under a 3-clause BSD style license - see LICENSE.rst import re import pytest from astropy import __version__ as version from astropy.io import fits from astropy.io.fits.scripts import fitscheck from astropy.utils.exceptions import AstropyUserWarning from .conftest import FitsTestCase class TestFitscheck(FitsTestCase): def test_help(self): with pytest.raises(SystemExit) as e: fitscheck.main(["-h"]) assert e.value.code == 0 def test_version(self, capsys): with pytest.raises(SystemExit) as e: fitscheck.main(["--version"]) out = capsys.readouterr()[0] assert out == f"fitscheck {version}" assert e.value.code == 0 def test_missing_file(self, capsys): assert fitscheck.main(["missing.fits"]) == 1 stdout, stderr = capsys.readouterr() assert "No such file or directory" in stderr def test_valid_file(self, capsys): testfile = self.data("checksum.fits") assert fitscheck.main([testfile]) == 0 assert fitscheck.main([testfile, "--compliance"]) == 0 assert fitscheck.main([testfile, "-v"]) == 0 stdout, stderr = capsys.readouterr() assert "OK" in stderr def test_remove_checksums(self, capsys): self.copy_file("checksum.fits") testfile = self.temp("checksum.fits") assert fitscheck.main([testfile, "--checksum", "remove"]) == 1 assert fitscheck.main([testfile]) == 1 stdout, stderr = capsys.readouterr() assert "MISSING" in stderr def test_no_checksums(self, capsys): testfile = self.data("arange.fits") assert fitscheck.main([testfile]) == 1 stdout, stderr = capsys.readouterr() assert "Checksum not found" in stderr assert fitscheck.main([testfile, "--ignore-missing"]) == 0 stdout, stderr = capsys.readouterr() assert stderr == "" def test_overwrite_invalid(self, caplog): """ Tests that invalid checksum or datasum are overwritten when the file is saved. """ reffile = self.temp("ref.fits") with fits.open(self.data("tb.fits")) as hdul: hdul.writeto(reffile, checksum=True) # replace checksums with wrong ones testfile = self.temp("test.fits") with fits.open(self.data("tb.fits")) as hdul: hdul[0].header["DATASUM"] = "1 " hdul[0].header["CHECKSUM"] = "8UgqATfo7TfoATfo" hdul[1].header["DATASUM"] = "2349680925" hdul[1].header["CHECKSUM"] = "11daD8bX98baA8bU" hdul.writeto(testfile) assert fitscheck.main([testfile]) == 1 assert re.match( r"BAD.*Checksum verification failed for HDU", caplog.records[0].message ) caplog.clear() with pytest.warns(AstropyUserWarning): assert fitscheck.main([testfile, "--write", "--force"]) == 1 assert re.match( r"BAD.*Checksum verification failed for HDU", caplog.records[0].message ) caplog.clear() # check that the file was fixed assert fitscheck.main([testfile]) == 0
afe0e595412d502b5d6b4fcc48d629205d3e82958dffd4d4a43227245893f81a
# Licensed under a 3-clause BSD style license - see PYFITS.rst
bc053407e583a4ee9baafc96414662f80291f4ea7b2ead3e5306b5ab6c5a99d1
# Tests related to writing dask arrays to FITS files in an efficient way import numpy as np import pytest from astropy.io import fits from astropy.io.fits import ImageHDU, PrimaryHDU da = pytest.importorskip("dask.array") @pytest.fixture def dask_array_in_mem(): return da.random.uniform(-1000, 1000, (1322, 755)).rechunk((59, 55)) def test_construct_image_hdu(dask_array_in_mem): hdu = ImageHDU(data=dask_array_in_mem) assert isinstance(hdu.data, da.Array) def test_construct_hdulist(dask_array_in_mem): hdu = ImageHDU(data=dask_array_in_mem) hdulist = fits.HDUList([hdu]) assert isinstance(hdulist[0].data, da.Array) def test_save_primary_hdu(dask_array_in_mem, tmp_path): # Saving a Primary HDU directly filename = tmp_path / "test.fits" hdu = PrimaryHDU(data=dask_array_in_mem) hdu.writeto(filename) with fits.open(filename) as hdulist_new: assert isinstance(hdulist_new[0].data, np.ndarray) np.testing.assert_allclose(hdulist_new[0].data, dask_array_in_mem.compute()) def test_save_image_hdu(dask_array_in_mem, tmp_path): # Saving an image HDU directly filename = tmp_path / "test.fits" hdu = ImageHDU(data=dask_array_in_mem) hdu.writeto(filename) with fits.open(filename) as hdulist_new: assert isinstance(hdulist_new[1].data, np.ndarray) np.testing.assert_allclose(hdulist_new[1].data, dask_array_in_mem.compute()) def test_save_hdulist(dask_array_in_mem, tmp_path): # Saving an HDUList filename = tmp_path / "test.fits" hdu1 = PrimaryHDU(data=dask_array_in_mem) hdu2 = ImageHDU(data=np.random.random((128, 128))) hdu3 = ImageHDU(data=dask_array_in_mem * 2) hdulist = fits.HDUList([hdu1, hdu2, hdu3]) assert isinstance(hdulist[0].data, da.Array) hdulist.writeto(filename) with fits.open(filename) as hdulist_new: assert isinstance(hdulist_new[0].data, np.ndarray) np.testing.assert_allclose(hdulist_new[0].data, dask_array_in_mem.compute()) assert isinstance(hdulist_new[1].data, np.ndarray) np.testing.assert_allclose(hdulist_new[1].data, hdu2.data) assert isinstance(hdulist_new[2].data, np.ndarray) np.testing.assert_allclose(hdulist_new[2].data, dask_array_in_mem.compute() * 2) def test_long_header(dask_array_in_mem, tmp_path): # Make sure things work correctly if there is a long header in the HDU. filename = tmp_path / "test.fits" # NOTE: we deliberately set up a long header here rather than add the # keys one by one to hdu.header as adding the header in one go used to # cause issues, so this acts as a regression test. header = fits.Header() for index in range(2048): header[f"KEY{index:x}"] = 0.0 hdu = PrimaryHDU(data=dask_array_in_mem, header=header) hdu.writeto(filename) with fits.open(filename) as hdulist_new: assert len(hdulist_new[0].header) == 2053 assert isinstance(hdulist_new[0].data, np.ndarray) np.testing.assert_allclose(hdulist_new[0].data, dask_array_in_mem.compute()) VALID_DTYPES = (">i2", "<i2", ">i4", "<i4", ">i8", "<i8", ">f4", "<f4", ">f8", "<f8") @pytest.mark.parametrize("dtype", VALID_DTYPES) def test_dtypes(dask_array_in_mem, tmp_path, dtype): filename = tmp_path / "test.fits" array = dask_array_in_mem.astype(dtype) hdu = PrimaryHDU(data=array) hdu.writeto(filename) with fits.open(filename) as hdulist_new: assert isinstance(hdulist_new[0].data, np.ndarray) np.testing.assert_allclose(hdulist_new[0].data, array.compute()) def test_scaled(dask_array_in_mem, tmp_path): filename = tmp_path / "test.fits" hdu = PrimaryHDU(data=dask_array_in_mem) hdu.scale("int32", bzero=-1000, bscale=1e-6) hdu.writeto(filename) with fits.open(filename) as hdulist_new: assert isinstance(hdulist_new[0].data, np.ndarray) np.testing.assert_allclose( hdulist_new[0].data, dask_array_in_mem.compute(), atol=1e-5 ) def test_scaled_minmax(dask_array_in_mem, tmp_path): filename = tmp_path / "test.fits" hdu = PrimaryHDU(data=dask_array_in_mem) hdu.scale("int32", option="minmax") hdu.writeto(filename) with fits.open(filename) as hdulist_new: assert isinstance(hdulist_new[0].data, np.ndarray) np.testing.assert_allclose( hdulist_new[0].data, dask_array_in_mem.compute(), atol=1e-5 ) def test_append(dask_array_in_mem, tmp_path): # Test append mode filename = tmp_path / "test.fits" fits.append(filename, dask_array_in_mem) fits.append(filename, np.arange(10)) with fits.open(filename) as hdulist_new: assert isinstance(hdulist_new[0].data, np.ndarray) np.testing.assert_allclose(hdulist_new[0].data, dask_array_in_mem.compute()) assert isinstance(hdulist_new[1].data, np.ndarray) np.testing.assert_allclose(hdulist_new[1].data, np.arange(10)) # @pytest.mark.parametrize('mode', ['rb+', 'ab', 'ab+', 'wb', 'wb+']) @pytest.mark.parametrize("mode", ["wb", "wb+"]) def test_file_handle(mode, dask_array_in_mem, tmp_path): filename = tmp_path / "test.fits" hdu1 = PrimaryHDU(data=dask_array_in_mem) hdu2 = ImageHDU(data=np.arange(10)) hdulist = fits.HDUList([hdu1, hdu2]) with filename.open(mode=mode) as fp: hdulist.writeto(fp) with fits.open(filename) as hdulist_new: assert isinstance(hdulist_new[0].data, np.ndarray) np.testing.assert_allclose(hdulist_new[0].data, dask_array_in_mem.compute()) assert isinstance(hdulist_new[1].data, np.ndarray) np.testing.assert_allclose(hdulist_new[1].data, np.arange(10))
c695465d0e3021a3bf982fb4cc08db1fa1c32dda91b04f6b868c87c0fe3d92bd
# Licensed under a 3-clause BSD style license - see PYFITS.rst import os import pathlib import shutil import stat import tempfile import time import pytest from astropy.io import fits @pytest.fixture( params=[False, "str", "pathlib"], ids=["", "home_is_data", "home_is_data, pathlib"] ) def home_is_data(request, monkeypatch): """ Pytest fixture to run a test case both with and without tilde paths. In the tilde-path case, calls like self.data('filename.fits') will produce '~/filename.fits', and environment variables will be temporarily modified so that '~' resolves to the data directory. """ # This checks the value specified in the fixture annotation if request.param: # `request.instance` refers to the test case that's using this fixture. request.instance.monkeypatch = monkeypatch request.instance.set_home_as_data() request.instance.set_paths_via_pathlib(request.param == "pathlib") @pytest.fixture( params=[False, "str", "pathlib"], ids=["", "home_is_data", "home_is_data, pathlib"] ) def home_is_temp(request, monkeypatch): """ Pytest fixture to run a test case both with and without tilde paths. In the tilde-path case, calls like self.temp('filename.fits') will produce '~/filename.fits', and environment variables will be temporarily modified so that '~' resolves to the temp directory. These files will also be tracked so that, after the test case, we can verify no files were written to a literal tilde path. """ # This checks the value specified in the fixture annotation if request.param: # `request.instance` refers to the test case that's using this fixture. request.instance.monkeypatch = monkeypatch request.instance.set_home_as_temp() request.instance.set_paths_via_pathlib(request.param == "pathlib") class FitsTestCase: def setup_method(self): self.data_dir = os.path.join(os.path.dirname(__file__), "data") self.temp_dir = tempfile.mkdtemp(prefix="fits-test-") self.home_is_data = False self.home_is_temp = False self.temp_files_used = set() self.use_pathlib = False # Restore global settings to defaults # TODO: Replace this when there's a better way to in the config API to # force config values to their defaults fits.conf.enable_record_valued_keyword_cards = True fits.conf.extension_name_case_sensitive = False fits.conf.strip_header_whitespace = True fits.conf.use_memmap = True def teardown_method(self): if self.home_is_temp: # Verify that no files were written to a literal tilde path for temp_file, temp_file_no_tilde in self.temp_files_used: assert not os.path.exists(temp_file) assert os.path.exists(temp_file_no_tilde) if hasattr(self, "temp_dir") and os.path.exists(self.temp_dir): tries = 3 while tries: try: shutil.rmtree(self.temp_dir) break except OSError: # Probably couldn't delete the file because for whatever # reason a handle to it is still open/hasn't been # garbage-collected time.sleep(0.5) tries -= 1 fits.conf.reset("enable_record_valued_keyword_cards") fits.conf.reset("extension_name_case_sensitive") fits.conf.reset("strip_header_whitespace") fits.conf.reset("use_memmap") def copy_file(self, filename): """Copies a backup of a test data file to the temp dir and sets its mode to writeable. """ shutil.copy( os.path.expanduser(self.data(filename)), os.path.expanduser(self.temp(filename)), ) os.chmod(os.path.expanduser(self.temp(filename)), stat.S_IREAD | stat.S_IWRITE) def data(self, filename): """Returns the path to a test data file.""" if self.home_is_data: prefix = "~" else: prefix = self.data_dir if self.use_pathlib: return pathlib.Path(prefix, filename) return os.path.join(prefix, filename) def temp(self, filename): """Returns the full path to a file in the test temp dir.""" real_target = os.path.join(self.temp_dir, filename) if self.home_is_temp: prefix = "~" # Record the '~' path and the intended path, for use # in `home_is_temp` self.temp_files_used.add((os.path.join(prefix, filename), real_target)) else: prefix = self.temp_dir if self.use_pathlib: return pathlib.Path(prefix, filename) return os.path.join(prefix, filename) def set_home_as_data(self): """ This overrides the HOME environment variable, so that paths beginning with '~/' expand to the data directory. Used by the `home_is_data` fixture. """ self.home_is_data = True # For Unix self.monkeypatch.setenv("HOME", self.data_dir) # For Windows self.monkeypatch.setenv("USERPROFILE", self.data_dir) def set_home_as_temp(self): """ This overrides the HOME environment variable, so that paths beginning with '~/' expand to the temp directory. In conjunction with self.temp(), temporary files are tracked as they are created, so we can verify they end up in the temporary directory and not unexpected places in the filesystem. Used by the `home_is_temp` fixture. """ self.home_is_temp = True # For Unix self.monkeypatch.setenv("HOME", self.temp_dir) # For Windows self.monkeypatch.setenv("USERPROFILE", self.temp_dir) def set_paths_via_pathlib(self, use_pathlib): self.use_pathlib = use_pathlib
5cb4673ed069c1d55bcc238f8701b9b23762a47ad23bd79b8f08ee82a60bab79
# Licensed under a 3-clause BSD style license - see PYFITS.rst import io import os import pathlib import warnings import numpy as np import pytest from numpy.testing import assert_array_equal from astropy import units as u from astropy.io import fits from astropy.io.fits import printdiff from astropy.io.fits.connect import REMOVE_KEYWORDS from astropy.io.fits.tests.test_table import _assert_attr_col from astropy.table import Table from astropy.utils.exceptions import AstropyUserWarning from .conftest import FitsTestCase class TestConvenience(FitsTestCase): def test_resource_warning(self): warnings.simplefilter("always", ResourceWarning) _ = fits.getdata(self.data("test0.fits")) _ = fits.getheader(self.data("test0.fits")) def test_fileobj_not_closed(self): """ Tests that file-like objects are not closed after being passed to convenience functions. Regression test for https://github.com/astropy/astropy/issues/5063 """ f = open(self.data("test0.fits"), "rb") _ = fits.getdata(f) assert not f.closed f.seek(0) _ = fits.getheader(f) assert not f.closed f.close() # Close it now def test_table_to_hdu(self): table = Table( [[1, 2, 3], ["a", "b", "c"], [2.3, 4.5, 6.7]], names=["a", "b", "c"], dtype=["i", "U1", "f"], ) table["a"].unit = "m/s" table["b"].unit = "not-a-unit" with pytest.warns( u.UnitsWarning, match="'not-a-unit' did not parse as fits unit" ) as w: hdu = fits.table_to_hdu(table) assert len(w) == 1 # Check that TUNITn cards appear in the correct order # (https://github.com/astropy/astropy/pull/5720) assert hdu.header.index("TUNIT1") < hdu.header.index("TTYPE2") assert isinstance(hdu, fits.BinTableHDU) filename = self.temp("test_table_to_hdu.fits") hdu.writeto(filename, overwrite=True) def test_masked_table_to_hdu(self): i = np.ma.MaskedArray([1, 2, 3], mask=[True, False, False]) s = np.ma.MaskedArray(["a", "b", "c"], mask=[False, True, True]) c = np.ma.MaskedArray([2.3 + 1j, 4.5 + 0j, 6.7 - 1j], mask=[True, False, True]) f = np.ma.MaskedArray([2.3, 4.5, 6.7], mask=[True, False, True]) table = Table([i, s, c, f], names=["i", "s", "c", "f"]) # Check that FITS standard is used in replacing masked values. hdu = fits.table_to_hdu(table) assert isinstance(hdu, fits.BinTableHDU) assert hdu.header["TNULL1"] == i.fill_value assert_array_equal(hdu.data["i"], i.filled()) assert_array_equal(hdu.data["s"], s.filled("")) assert_array_equal(hdu.data["c"], c.filled(np.nan)) assert_array_equal(hdu.data["c"].real, c.real.filled(np.nan)) assert_array_equal(hdu.data["c"].imag, c.imag.filled(np.nan)) assert_array_equal(hdu.data["c"], c.filled(complex(np.nan, np.nan))) assert_array_equal(hdu.data["f"], f.filled(np.nan)) filename = self.temp("test_table_to_hdu.fits") hdu.writeto(filename, overwrite=True) def test_table_non_stringifyable_unit_to_hdu(self): table = Table( [[1, 2, 3], ["a", "b", "c"], [2.3, 4.5, 6.7]], names=["a", "b", "c"], dtype=["i", "U1", "f"], ) table["a"].unit = u.core.IrreducibleUnit("test") with pytest.warns( AstropyUserWarning, match="The unit 'test' could not be saved" ) as w: fits.table_to_hdu(table) assert len(w) == 1 def test_table_to_hdu_convert_comment_convention(self): """ Regression test for https://github.com/astropy/astropy/issues/6079 """ table = Table( [[1, 2, 3], ["a", "b", "c"], [2.3, 4.5, 6.7]], names=["a", "b", "c"], dtype=["i", "U1", "f"], ) table.meta["comments"] = ["This", "is", "a", "comment"] hdu = fits.table_to_hdu(table) assert hdu.header.get("comment") == ["This", "is", "a", "comment"] with pytest.raises(ValueError): hdu.header.index("comments") def test_table_to_hdu_filter_reserved(self): """ Regression test for https://github.com/astropy/astropy/issues/9387 """ diag = "be ignored since it conflicts with a FITS reserved keyword" ins_cards = { "EXPTIME": 32.1, "XTENSION": "NEWTABLE", "NAXIS": 1, "NAXIS1": 3, "NAXIS2": 9, "PCOUNT": 42, "OBSERVER": "Adams", } table = Table( [[1, 2, 3], ["a", "b", "c"], [2.3, 4.5, 6.7]], names=["a", "b", "c"], dtype=["i4", "U1", "f8"], ) table.meta.update(ins_cards) with pytest.warns( AstropyUserWarning, match=rf"Meta-data keyword \w+ will {diag}" ) as w: hdu = fits.table_to_hdu(table) # This relies on the warnings being raised in the order of the # meta dict (note that the first and last card are legitimate keys) assert len(w) == len(ins_cards) - 2 for i, key in enumerate(list(ins_cards)[1:-1]): assert f"Meta-data keyword {key}" in str(w[i].message) assert hdu.header.get("XTENSION") == "BINTABLE" assert hdu.header.get("NAXIS") == 2 assert hdu.header.get("NAXIS1") == 13 assert hdu.header.get("NAXIS2") == 3 assert hdu.header.get("PCOUNT") == 0 np.testing.assert_almost_equal(hdu.header.get("EXPTIME"), 3.21e1) @pytest.mark.parametrize("card", REMOVE_KEYWORDS) def test_table_to_hdu_warn_reserved(self, card): """ Test warning for each keyword in ..connect.REMOVE_KEYWORDS, 1 by 1 """ diag = "be ignored since it conflicts with a FITS reserved keyword" res_cards = { "XTENSION": "BINTABLE", "BITPIX": 8, "NAXIS": 2, "NAXIS1": 12, "NAXIS2": 3, "PCOUNT": 0, "GCOUNT": 1, "TFIELDS": 2, "THEAP": None, } ins_cards = { "XTENSION": "TABLE", "BITPIX": 16, "NAXIS": 1, "NAXIS1": 2, "NAXIS2": 6, "PCOUNT": 2, "GCOUNT": 2, "TFIELDS": 4, "THEAP": 36, } table = Table( [[1.0, 2.0, 3.0], [2.3, 4.5, 6.7]], names=["wavelength", "flux"], dtype=["f8", "f4"], ) table.meta["ORIGIN"] = "Min.Silly Walks" table.meta[card] = ins_cards[card] assert table.meta.get(card) != res_cards[card] with pytest.warns( AstropyUserWarning, match=f"Meta-data keyword {card} will {diag}" ): hdu = fits.table_to_hdu(table) assert hdu.header.get(card) == res_cards[card] assert hdu.header.get("ORIGIN") == "Min.Silly Walks" def test_table_to_hdu_filter_incompatible(self): """ Test removal of unsupported data types from header """ table = Table( [[1, 2, 3], ["a", "b", "c"], [2.3, 4.5, 6.7]], names=["a", "b", "c"], dtype=["i4", "U1", "f8"], ) table.meta.update( { "OBSDATE": "2001-05-26", "RAMP": np.arange(5), "TARGETS": {"PRIMARY": 1, "SECONDAR": 3}, } ) with pytest.warns( AstropyUserWarning, match=r"Attribute \S+ of type " r".+ cannot be added to FITS Header - skipping", ): hdu = fits.table_to_hdu(table) assert hdu.header.get("OBSDATE") == "2001-05-26" assert "RAMP" not in hdu.header assert "TARGETS" not in hdu.header def test_table_writeto_header(self): """ Regression test for https://github.com/astropy/astropy/issues/5988 """ data = np.zeros((5,), dtype=[("x", float), ("y", int)]) h_in = fits.Header() h_in["ANSWER"] = (42.0, "LTU&E") filename = self.temp("tabhdr42.fits") fits.writeto(filename, data=data, header=h_in, overwrite=True) h_out = fits.getheader(filename, ext=1) assert h_out["ANSWER"] == 42 def test_image_extension_update_header(self, home_is_temp): """ Test that _makehdu correctly includes the header. For example in the fits.update convenience function. """ filename = self.temp("twoextension.fits") hdus = [fits.PrimaryHDU(np.zeros((10, 10))), fits.ImageHDU(np.zeros((10, 10)))] # Try to update a non-existant file with pytest.raises(FileNotFoundError, match="No such file"): fits.update( filename, np.zeros((10, 10)), header=fits.Header([("WHAT", 100)]), ext=1 ) fits.HDUList(hdus).writeto(filename) fits.update( filename, np.zeros((10, 10)), header=fits.Header([("WHAT", 100)]), ext=1 ) h_out = fits.getheader(filename, ext=1) assert h_out["WHAT"] == 100 def test_printdiff(self): """ Test that FITSDiff can run the different inputs without crashing. """ # Testing different string input options assert printdiff(self.data("arange.fits"), self.data("blank.fits")) is None assert ( printdiff(self.data("arange.fits"), self.data("blank.fits"), ext=0) is None ) assert ( printdiff( self.data("o4sp040b0_raw.fits"), self.data("o4sp040b0_raw.fits"), extname="sci", ) is None ) # This may seem weird, but check printdiff to see, need to test # incorrect second file with pytest.raises(OSError): printdiff("o4sp040b0_raw.fits", "fakefile.fits", extname="sci") # Test HDU object inputs with fits.open(self.data("stddata.fits"), mode="readonly") as in1: with fits.open(self.data("checksum.fits"), mode="readonly") as in2: assert printdiff(in1[0], in2[0]) is None with pytest.raises(ValueError): printdiff(in1[0], in2[0], ext=0) assert printdiff(in1, in2) is None with pytest.raises(NotImplementedError): printdiff(in1, in2, 0) def test_tabledump(self): """ A simple test of the dump method. Also regression test for https://github.com/astropy/astropy/issues/6937 """ datastr = ( '" 1" "abc" " 3.70000007152557" " 0"\n' '" 2" "xy " " 6.69999971389771" " 1"\n' ) cdstr = ( 'c1 1J I11 "" ""' ' -2147483647 "" "" \n' 'c2 3A A3 "" ""' ' "" "" "" \n' 'c3 1E G15.7 "" ""' ' "" 3 0.4 \n' 'c4 1L L6 "" ""' ' "" "" "" \n' ) # copy fits file to the temp directory self.copy_file("tb.fits") # test without datafile fits.tabledump(self.temp("tb.fits")) assert os.path.isfile(self.temp("tb_1.txt")) # test with datafile fits.tabledump(self.temp("tb.fits"), datafile=self.temp("test_tb.txt")) assert os.path.isfile(self.temp("test_tb.txt")) # test with datafile and cdfile datafile = self.temp("data.txt") cdfile = self.temp("coldefs.txt") fits.tabledump(self.temp("tb.fits"), datafile, cdfile) assert os.path.isfile(datafile) with open(datafile) as data: assert data.read() == datastr with open(cdfile) as coldefs: assert coldefs.read() == cdstr @pytest.mark.parametrize("tablename", ["table.fits", "tb.fits"]) def test_dump_load_round_trip(self, tablename): """ A simple test of the dump/load methods; dump the data, column, and header files and try to reload the table from them. """ # copy fits file to the temp directory self.copy_file(tablename) datafile = self.temp("data.txt") cdfile = self.temp("coldefs.txt") hfile = self.temp("header.txt") fits.tabledump(self.temp(tablename), datafile, cdfile, hfile) new_tbhdu = fits.tableload(datafile, cdfile, hfile) with fits.open(self.temp(tablename)) as hdul: _assert_attr_col(new_tbhdu, hdul[1]) def test_append_filename(self, home_is_temp): """ Test fits.append with a filename argument. """ data = np.arange(6) testfile = self.temp("test_append_1.fits") # Test case 1: creation of file fits.append(testfile, data=data, checksum=True) # Test case 2: append to existing file, with verify=True # Also test that additional keyword can be passed to fitsopen fits.append(testfile, data=data * 2, checksum=True, ignore_blank=True) # Test case 3: append to existing file, with verify=False fits.append(testfile, data=data * 3, checksum=True, verify=False) with fits.open(testfile, checksum=True) as hdu1: np.testing.assert_array_equal(hdu1[0].data, data) np.testing.assert_array_equal(hdu1[1].data, data * 2) np.testing.assert_array_equal(hdu1[2].data, data * 3) @pytest.mark.parametrize("mode", ["wb", "wb+", "ab", "ab+"]) def test_append_filehandle(self, tmp_path, mode): """ Test fits.append with a file handle argument. """ append_file = tmp_path / "append.fits" with append_file.open(mode) as handle: fits.append(filename=handle, data=np.ones((4, 4))) def test_append_with_header(self): """ Test fits.append with a fits Header, which triggers detection of the HDU class. Regression test for https://github.com/astropy/astropy/issues/8660 """ testfile = self.temp("test_append_1.fits") with fits.open(self.data("test0.fits")) as hdus: for hdu in hdus: fits.append(testfile, hdu.data, hdu.header, checksum=True) with fits.open(testfile, checksum=True) as hdus: assert len(hdus) == 5 def test_pathlib(self): testfile = pathlib.Path(self.temp("test.fits")) data = np.arange(10) hdulist = fits.HDUList([fits.PrimaryHDU(data)]) hdulist.writeto(testfile) with fits.open(testfile) as hdul: np.testing.assert_array_equal(hdul[0].data, data) def test_getdata_ext_given(self): prihdu = fits.PrimaryHDU(data=np.zeros((5, 5), dtype=int)) exthdu1 = fits.ImageHDU(data=np.ones((5, 5), dtype=int)) exthdu2 = fits.ImageHDU(data=2 * np.ones((5, 5), dtype=int)) hdulist = fits.HDUList([prihdu, exthdu1, exthdu2]) buf = io.BytesIO() hdulist.writeto(buf) for ext in [0, 1, 2]: buf.seek(0) data = fits.getdata(buf, ext=ext) assert data[0, 0] == ext def test_getdata_ext_given_nodata(self): prihdu = fits.PrimaryHDU(data=np.zeros((5, 5), dtype=int)) exthdu1 = fits.ImageHDU(data=np.ones((5, 5), dtype=int)) exthdu2 = fits.ImageHDU(data=None) hdulist = fits.HDUList([prihdu, exthdu1, exthdu2]) buf = io.BytesIO() hdulist.writeto(buf) buf.seek(0) with pytest.raises(IndexError, match="No data in HDU #2."): fits.getdata(buf, ext=2) def test_getdata_ext_not_given_with_data_in_primary(self): prihdu = fits.PrimaryHDU(data=np.zeros((5, 5), dtype=int)) exthdu1 = fits.ImageHDU(data=None) exthdu2 = fits.ImageHDU(data=None) hdulist = fits.HDUList([prihdu, exthdu1, exthdu2]) buf = io.BytesIO() hdulist.writeto(buf) buf.seek(0) data = fits.getdata(buf) assert data[0, 0] == 0 def test_getdata_ext_not_given_with_data_in_ext(self): # tests fallback mechanism prihdu = fits.PrimaryHDU(data=None) exthdu1 = fits.ImageHDU(data=np.ones((5, 5), dtype=int)) exthdu2 = fits.ImageHDU(data=None) hdulist = fits.HDUList([prihdu, exthdu1, exthdu2]) buf = io.BytesIO() hdulist.writeto(buf) buf.seek(0) data = fits.getdata(buf) assert data[0, 0] == 1 def test_getdata_ext_not_given_nodata_any(self): # tests exception raised when there is no data in either # Primary HDU or first extension HDU prihdu = fits.PrimaryHDU(data=None) exthdu1 = fits.ImageHDU(data=None) exthdu2 = fits.ImageHDU(data=np.ones((5, 5), dtype=int)) hdulist = fits.HDUList([prihdu, exthdu1, exthdu2]) buf = io.BytesIO() hdulist.writeto(buf) buf.seek(0) with pytest.raises( IndexError, match="No data in either Primary or first extension HDUs." ): fits.getdata(buf) def test_getdata_ext_not_given_nodata_noext(self): # tests exception raised when there is no data in the # Primary HDU and there are no extension HDUs prihdu = fits.PrimaryHDU(data=None) hdulist = fits.HDUList([prihdu]) buf = io.BytesIO() hdulist.writeto(buf) buf.seek(0) with pytest.raises( IndexError, match="No data in Primary HDU and no extension HDU found." ): fits.getdata(buf)
35aa7d074cb338a19cef2f0d886067fbd2ce9b5235265ab91a5f511ea047fac9
# Licensed under a 3-clause BSD style license - see LICENSE.rst """Can `astropy.io.fits.open` access (remote) data using the fsspec package? """ import numpy as np import pytest from numpy.testing import assert_allclose, assert_array_equal from astropy.io import fits from astropy.nddata import Cutout2D from astropy.utils.compat.optional_deps import HAS_FSSPEC, HAS_S3FS from astropy.utils.data import get_pkg_data_filename if HAS_FSSPEC: import fsspec @pytest.mark.skipif(not HAS_FSSPEC, reason="requires fsspec") def test_fsspec_local(): """Can we use fsspec to read a local file?""" fn = get_pkg_data_filename("data/test0.fits") with fits.open(fn) as hdulist_classic: with fits.open(fn, use_fsspec=True) as hdulist_fsspec: assert_array_equal(hdulist_classic[2].data, hdulist_fsspec[2].data) assert_array_equal( hdulist_classic[2].section[3:5], hdulist_fsspec[2].section[3:5] ) @pytest.mark.skipif(not HAS_FSSPEC, reason="requires fsspec") def test_fsspec_local_write(tmp_path): """Can we write to a local file that was opened using fsspec?""" fn = get_pkg_data_filename("data/test0.fits") fn_tmp = tmp_path / "tmp.fits" with fits.open(fn, use_fsspec=True) as hdul: # writing to a section is never allowed with pytest.raises(TypeError): hdul[1].section[0, 0] = -999 # however writing to .data should work hdul[1].data[2, 3] = -999 assert hdul[1].data[2, 3] == -999 hdul.writeto(fn_tmp) # Is the new value present when we re-open the file? with fits.open(fn_tmp) as hdul: assert hdul[1].data[2, 3] == -999 @pytest.mark.skipif(not HAS_FSSPEC, reason="requires fsspec") def test_fsspec_cutout2d(): """Does Cutout2D work with data loaded lazily using fsspec and .section?""" fn = get_pkg_data_filename("data/test0.fits") with fits.open(fn, use_fsspec=True) as hdul: position = (10, 20) size = (2, 3) cutout1 = Cutout2D(hdul[1].data, position, size) cutout2 = Cutout2D(hdul[1].section, position, size) assert_allclose(cutout1.data, cutout2.data) @pytest.mark.skipif(not HAS_FSSPEC, reason="requires fsspec") def test_fsspec_compressed(): """Does fsspec support compressed data correctly?""" # comp.fits[1] is a compressed image with shape (440, 300) fn = get_pkg_data_filename("data/comp.fits") with fits.open(fn, use_fsspec=True) as hdul: # The .data attribute should work as normal assert hdul[1].data[0, 0] == 7 # However the .section attribute does not support compressed data with pytest.raises( AttributeError, match="'CompImageHDU' object has no attribute 'section'" ) as excinfo: hdul[1].section[1, 2] @pytest.mark.remote_data class TestFsspecRemote: """Test obtaining cutouts from FITS files via HTTP (from MAST) and S3 (from Amazon).""" def setup_class(self): # The test file (ibxl50020_jif.fits) is a Hubble jitter FITS file (*.jif) # rather than a real image, because jitter files are less likely to # change due to reprocessing. self.http_url = "https://mast.stsci.edu/api/v0.1/Download/file/?uri=mast:HST/product/ibxl50020_jif.fits" self.s3_uri = "s3://stpubdata/hst/public/ibxl/ibxl50020/ibxl50020_jif.fits" # Random slice was selected for testing: self.slice = (slice(31, 33), slice(27, 30)) # The expected cutout array below was obtained by downloading the URIs # listed above to a local path and and executing: # with fits.open(local_path) as hdul: # expected_cutout = hdul[1].data[31:33, 27:30] self.expected_cutout = np.array([[24, 88, 228], [35, 132, 305]], dtype=np.int32) @pytest.mark.skipif(not HAS_FSSPEC, reason="requires fsspec") def test_fsspec_http(self): """Can we use fsspec to open a remote FITS file via http?""" with fits.open(self.http_url, use_fsspec=True) as hdul: # Do we retrieve the expected array? assert_array_equal(hdul[1].section[self.slice], self.expected_cutout) # The file has multiple extensions which are not yet downloaded; # the repr and string representation should reflect this. assert "partially read" in repr(hdul) assert "partially read" in str(hdul) # Can the user also pass an fsspec file object directly to fits open? with fsspec.open(self.http_url) as fileobj: with fits.open(fileobj) as hdul2: assert_array_equal(hdul2[1].section[self.slice], self.expected_cutout) assert "partially read" in repr(hdul) assert "partially read" in str(hdul) @pytest.mark.skipif(not HAS_S3FS, reason="requires s3fs") def test_fsspec_s3(self): """Can we use fsspec to open a FITS file in a public Amazon S3 bucket?""" with fits.open( self.s3_uri, fsspec_kwargs={"anon": True} ) as hdul: # s3:// paths should default to use_fsspec=True # Do we retrieve the expected array? assert_array_equal(hdul[1].section[self.slice], self.expected_cutout) # The file has multiple extensions which are not yet downloaded; # the repr and string representation should reflect this. assert "partially read" in repr(hdul) assert "partially read" in str(hdul) # Can the user also pass an fsspec file object directly to fits open? with fsspec.open(self.s3_uri, anon=True) as fileobj: with fits.open(fileobj) as hdul2: assert_array_equal(hdul2[1].section[self.slice], self.expected_cutout) assert "partially read" in repr(hdul) assert "partially read" in str(hdul)
dc22fb1e8c04dea7bb65b590c3fb8a97e7719f9f0846ff33c16b314b8e2c2b01
# Licensed under a 3-clause BSD style license - see LICENSE.rst import gzip import os import signal import sys import numpy as np import pytest from numpy.testing import assert_equal from astropy.io.fits import util from astropy.io.fits.util import _rstrip_inplace, ignore_sigint from astropy.utils.compat.optional_deps import HAS_PIL if HAS_PIL: from PIL import Image from .conftest import FitsTestCase class TestUtils(FitsTestCase): @pytest.mark.skipif(sys.platform.startswith("win"), reason="Cannot test on Windows") def test_ignore_sigint(self): @ignore_sigint def test(): with pytest.warns(UserWarning) as w: pid = os.getpid() os.kill(pid, signal.SIGINT) # One more time, for good measure os.kill(pid, signal.SIGINT) assert len(w) == 2 assert ( str(w[0].message) == "KeyboardInterrupt ignored until test is complete!" ) pytest.raises(KeyboardInterrupt, test) def test_realign_dtype(self): """ Tests a few corner-cases for numpy dtype creation. These originally were the reason for having a realign_dtype hack. """ dt = np.dtype([("a", np.int32), ("b", np.int16)]) names = dt.names formats = [dt.fields[name][0] for name in names] dt2 = np.dtype({"names": names, "formats": formats, "offsets": [0, 0]}) assert dt2.itemsize == 4 dt2 = np.dtype({"names": names, "formats": formats, "offsets": [0, 1]}) assert dt2.itemsize == 4 dt2 = np.dtype({"names": names, "formats": formats, "offsets": [1, 0]}) assert dt2.itemsize == 5 dt = np.dtype([("a", np.float64), ("b", np.int8), ("c", np.int8)]) names = dt.names formats = [dt.fields[name][0] for name in names] dt2 = np.dtype({"names": names, "formats": formats, "offsets": [0, 0, 0]}) assert dt2.itemsize == 8 dt2 = np.dtype({"names": names, "formats": formats, "offsets": [0, 0, 1]}) assert dt2.itemsize == 8 dt2 = np.dtype({"names": names, "formats": formats, "offsets": [0, 0, 27]}) assert dt2.itemsize == 28 class TestUtilMode(FitsTestCase): """ The high-level tests are partially covered by test_core.TestConvenienceFunctions.test_fileobj_mode_guessing but added some low-level tests as well. """ def test_mode_strings(self): # A string signals that the file should be opened so the function # should return None, because it's simply not opened yet. assert util.fileobj_mode("tmp1.fits") is None @pytest.mark.skipif(not HAS_PIL, reason="requires pil") def test_mode_pil_image(self): img = np.random.randint(0, 255, (5, 5, 3)).astype(np.uint8) result = Image.fromarray(img) result.save(self.temp("test_simple.jpg")) # PIL doesn't support append mode. So it will always use binary read. with Image.open(self.temp("test_simple.jpg")) as fileobj: assert util.fileobj_mode(fileobj) == "rb" def test_mode_gzip(self): # Open a gzip in every possible (gzip is binary or "touch" only) way # and check if the mode was correctly identified. # The lists consist of tuples: filenumber, given mode, identified mode # The filenumber must be given because read expects the file to exist # and x expects it to NOT exist. num_mode_resmode = [ (0, "a", "ab"), (0, "ab", "ab"), (0, "w", "wb"), (0, "wb", "wb"), (1, "x", "xb"), (1, "r", "rb"), (1, "rb", "rb"), ] for num, mode, res in num_mode_resmode: filename = self.temp(f"test{num}.gz") with gzip.GzipFile(filename, mode) as fileobj: assert util.fileobj_mode(fileobj) == res def test_mode_normal_buffering(self): # Use the python IO with buffering parameter. Binary mode only: # see "test_mode_gzip" for explanation of tuple meanings. num_mode_resmode = [ (0, "ab", "ab"), (0, "wb", "wb"), (1, "xb", "xb"), (1, "rb", "rb"), ] for num, mode, res in num_mode_resmode: filename = self.temp(f"test1{num}.dat") with open(filename, mode, buffering=0) as fileobj: assert util.fileobj_mode(fileobj) == res def test_mode_normal_no_buffering(self): # Python IO without buffering # see "test_mode_gzip" for explanation of tuple meanings. num_mode_resmode = [ (0, "a", "a"), (0, "ab", "ab"), (0, "w", "w"), (0, "wb", "wb"), (1, "x", "x"), (1, "r", "r"), (1, "rb", "rb"), ] for num, mode, res in num_mode_resmode: filename = self.temp(f"test2{num}.dat") with open(filename, mode) as fileobj: assert util.fileobj_mode(fileobj) == res def test_mode_normalization(self): # Use the normal python IO in append mode with all possible permutation # of the "mode" letters. # Tuple gives a file name suffix, the given mode and the functions # return. The filenumber is only for consistency with the other # test functions. Append can deal with existing and not existing files. for num, mode, res in [ (0, "a", "a"), (0, "a+", "a+"), (0, "ab", "ab"), (0, "a+b", "ab+"), (0, "ab+", "ab+"), ]: filename = self.temp(f"test3{num}.dat") with open(filename, mode) as fileobj: assert util.fileobj_mode(fileobj) == res def test_rstrip_inplace(): # Incorrect type s = np.array([1, 2, 3]) with pytest.raises(TypeError) as exc: _rstrip_inplace(s) assert exc.value.args[0] == "This function can only be used on string arrays" # Bytes array s = np.array(["a ", " b", " c c "], dtype="S6") _rstrip_inplace(s) assert_equal(s, np.array(["a", " b", " c c"], dtype="S6")) # Unicode array s = np.array(["a ", " b", " c c "], dtype="U6") _rstrip_inplace(s) assert_equal(s, np.array(["a", " b", " c c"], dtype="U6")) # 2-dimensional array s = np.array([["a ", " b"], [" c c ", " a "]], dtype="S6") _rstrip_inplace(s) assert_equal(s, np.array([["a", " b"], [" c c", " a"]], dtype="S6")) # 3-dimensional array s = np.repeat(" a a ", 24).reshape((2, 3, 4)) _rstrip_inplace(s) assert_equal(s, " a a") # 3-dimensional non-contiguous array s = np.repeat(" a a ", 1000).reshape((10, 10, 10))[:2, :3, :4] _rstrip_inplace(s) assert_equal(s, " a a")
5dd0e5b319931484d91f39dcaf4f0494fca3b7204ba877376926a20a7a2b9a81
# Licensed under a 3-clause BSD style license - see LICENSE.rst import numpy as np import pytest from astropy.io import fits from astropy.io.fits.column import Column from astropy.io.fits.diff import ( FITSDiff, HDUDiff, HeaderDiff, ImageDataDiff, TableDataDiff, ) from astropy.io.fits.hdu import HDUList, ImageHDU, PrimaryHDU from astropy.io.fits.hdu.base import NonstandardExtHDU from astropy.io.fits.hdu.table import BinTableHDU from astropy.io.fits.header import Header from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH from .conftest import FitsTestCase class DummyNonstandardExtHDU(NonstandardExtHDU): def __init__(self, data=None, *args, **kwargs): super().__init__(self, *args, **kwargs) self._buffer = np.asarray(data).tobytes() self._data_offset = 0 @property def size(self): return len(self._buffer) class TestDiff(FitsTestCase): def test_identical_headers(self): ha = Header([("A", 1), ("B", 2), ("C", 3)]) hb = ha.copy() assert HeaderDiff(ha, hb).identical assert HeaderDiff(ha.tostring(), hb.tostring()).identical with pytest.raises(TypeError): HeaderDiff(1, 2) def test_slightly_different_headers(self): ha = Header([("A", 1), ("B", 2), ("C", 3)]) hb = ha.copy() hb["C"] = 4 assert not HeaderDiff(ha, hb).identical def test_common_keywords(self): ha = Header([("A", 1), ("B", 2), ("C", 3)]) hb = ha.copy() hb["C"] = 4 hb["D"] = (5, "Comment") assert HeaderDiff(ha, hb).common_keywords == ["A", "B", "C"] def test_different_keyword_count(self): ha = Header([("A", 1), ("B", 2), ("C", 3)]) hb = ha.copy() del hb["B"] diff = HeaderDiff(ha, hb) assert not diff.identical assert diff.diff_keyword_count == (3, 2) # But make sure the common keywords are at least correct assert diff.common_keywords == ["A", "C"] def test_different_keywords(self): ha = Header([("A", 1), ("B", 2), ("C", 3)]) hb = ha.copy() hb["C"] = 4 hb["D"] = (5, "Comment") ha["E"] = (6, "Comment") ha["F"] = (7, "Comment") diff = HeaderDiff(ha, hb) assert not diff.identical assert diff.diff_keywords == (["E", "F"], ["D"]) def test_different_keyword_values(self): ha = Header([("A", 1), ("B", 2), ("C", 3)]) hb = ha.copy() hb["C"] = 4 diff = HeaderDiff(ha, hb) assert not diff.identical assert diff.diff_keyword_values == {"C": [(3, 4)]} def test_different_keyword_comments(self): ha = Header([("A", 1), ("B", 2), ("C", 3, "comment 1")]) hb = ha.copy() hb.comments["C"] = "comment 2" diff = HeaderDiff(ha, hb) assert not diff.identical assert diff.diff_keyword_comments == {"C": [("comment 1", "comment 2")]} def test_different_keyword_values_with_duplicate(self): ha = Header([("A", 1), ("B", 2), ("C", 3)]) hb = ha.copy() ha.append(("C", 4)) hb.append(("C", 5)) diff = HeaderDiff(ha, hb) assert not diff.identical assert diff.diff_keyword_values == {"C": [None, (4, 5)]} def test_asymmetric_duplicate_keywords(self): ha = Header([("A", 1), ("B", 2), ("C", 3)]) hb = ha.copy() ha.append(("A", 2, "comment 1")) ha.append(("A", 3, "comment 2")) hb.append(("B", 4, "comment 3")) hb.append(("C", 5, "comment 4")) diff = HeaderDiff(ha, hb) assert not diff.identical assert diff.diff_keyword_values == {} assert diff.diff_duplicate_keywords == {"A": (3, 1), "B": (1, 2), "C": (1, 2)} report = diff.report() assert ( "Inconsistent duplicates of keyword 'A' :\n" " Occurs 3 time(s) in a, 1 times in (b)" in report ) def test_floating_point_rtol(self): ha = Header([("A", 1), ("B", 2.00001), ("C", 3.000001)]) hb = ha.copy() hb["B"] = 2.00002 hb["C"] = 3.000002 diff = HeaderDiff(ha, hb) assert not diff.identical assert diff.diff_keyword_values == { "B": [(2.00001, 2.00002)], "C": [(3.000001, 3.000002)], } diff = HeaderDiff(ha, hb, rtol=1e-6) assert not diff.identical assert diff.diff_keyword_values == {"B": [(2.00001, 2.00002)]} diff = HeaderDiff(ha, hb, rtol=1e-5) assert diff.identical def test_floating_point_atol(self): ha = Header([("A", 1), ("B", 1.0), ("C", 0.0)]) hb = ha.copy() hb["B"] = 1.00001 hb["C"] = 0.000001 diff = HeaderDiff(ha, hb, rtol=1e-6) assert not diff.identical assert diff.diff_keyword_values == { "B": [(1.0, 1.00001)], "C": [(0.0, 0.000001)], } diff = HeaderDiff(ha, hb, rtol=1e-5) assert not diff.identical assert diff.diff_keyword_values == {"C": [(0.0, 0.000001)]} diff = HeaderDiff(ha, hb, atol=1e-6) assert not diff.identical assert diff.diff_keyword_values == {"B": [(1.0, 1.00001)]} diff = HeaderDiff(ha, hb, atol=1e-5) # strict inequality assert not diff.identical assert diff.diff_keyword_values == {"B": [(1.0, 1.00001)]} diff = HeaderDiff(ha, hb, rtol=1e-5, atol=1e-5) assert diff.identical diff = HeaderDiff(ha, hb, atol=1.1e-5) assert diff.identical diff = HeaderDiff(ha, hb, rtol=1e-6, atol=1e-6) assert not diff.identical def test_ignore_blanks(self): with fits.conf.set_temp("strip_header_whitespace", False): ha = Header([("A", 1), ("B", 2), ("C", "A ")]) hb = ha.copy() hb["C"] = "A" assert ha["C"] != hb["C"] diff = HeaderDiff(ha, hb) # Trailing blanks are ignored by default assert diff.identical assert diff.diff_keyword_values == {} # Don't ignore blanks diff = HeaderDiff(ha, hb, ignore_blanks=False) assert not diff.identical assert diff.diff_keyword_values == {"C": [("A ", "A")]} @pytest.mark.parametrize("differ", [HeaderDiff, HDUDiff, FITSDiff]) def test_ignore_blank_cards(self, differ): """Test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/152 Ignore blank cards. """ ha = Header([("A", 1), ("B", 2), ("C", 3)]) hb = Header([("A", 1), ("", ""), ("B", 2), ("", ""), ("C", 3)]) hc = ha.copy() if differ is HeaderDiff: hc.append() hc.append() else: # Ensure blanks are not at the end as they are stripped by HDUs hc.add_blank(after=-2) hc.add_blank(after=-2) if differ in (HDUDiff, FITSDiff): # wrap it in a PrimaryHDU ha, hb, hc = (PrimaryHDU(np.arange(10), h) for h in (ha, hb, hc)) hc_header = hc.header if differ is FITSDiff: # wrap it in a HDUList ha, hb, hc = (HDUList([h]) for h in (ha, hb, hc)) hc_header = hc[0].header # We now have a header with interleaved blanks, and a header with end # blanks, both of which should ignore the blanks assert differ(ha, hb).identical assert differ(ha, hc).identical assert differ(hb, hc).identical assert not differ(ha, hb, ignore_blank_cards=False).identical assert not differ(ha, hc, ignore_blank_cards=False).identical # Both hb and hc have the same number of blank cards; since order is # currently ignored, these should still be identical even if blank # cards are not ignored assert differ(hb, hc, ignore_blank_cards=False).identical if differ is HeaderDiff: hc.append() else: # Ensure blanks are not at the end as they are stripped by HDUs hc_header.add_blank(after=-2) # But now there are different numbers of blanks, so they should not be # ignored: assert not differ(hb, hc, ignore_blank_cards=False).identical def test_ignore_hdus(self): a = np.arange(100).reshape(10, 10) b = a.copy() ha = Header([("A", 1), ("B", 2), ("C", 3)]) xa = np.array([(1.0, 1), (3.0, 4)], dtype=[("x", float), ("y", int)]) xb = np.array([(1.0, 2), (3.0, 5)], dtype=[("x", float), ("y", int)]) phdu = PrimaryHDU(header=ha) ihdua = ImageHDU(data=a, name="SCI") ihdub = ImageHDU(data=b, name="SCI") bhdu1 = BinTableHDU(data=xa, name="ASDF") bhdu2 = BinTableHDU(data=xb, name="ASDF") hdula = HDUList([phdu, ihdua, bhdu1]) hdulb = HDUList([phdu, ihdub, bhdu2]) # ASDF extension should be different diff = FITSDiff(hdula, hdulb) assert not diff.identical assert diff.diff_hdus[0][0] == 2 # ASDF extension should be ignored diff = FITSDiff(hdula, hdulb, ignore_hdus=["ASDF"]) assert diff.identical, diff.report() diff = FITSDiff(hdula, hdulb, ignore_hdus=["ASD*"]) assert diff.identical, diff.report() # SCI extension should be different hdulb["SCI"].data += 1 diff = FITSDiff(hdula, hdulb, ignore_hdus=["ASDF"]) assert not diff.identical # SCI and ASDF extensions should be ignored diff = FITSDiff(hdula, hdulb, ignore_hdus=["SCI", "ASDF"]) assert diff.identical, diff.report() # All EXTVER of SCI should be ignored ihduc = ImageHDU(data=a, name="SCI", ver=2) hdulb.append(ihduc) diff = FITSDiff(hdula, hdulb, ignore_hdus=["SCI", "ASDF"]) assert not any(diff.diff_hdus), diff.report() assert any(diff.diff_hdu_count), diff.report() def test_ignore_keyword_values(self): ha = Header([("A", 1), ("B", 2), ("C", 3)]) hb = ha.copy() hb["B"] = 4 hb["C"] = 5 diff = HeaderDiff(ha, hb, ignore_keywords=["*"]) assert diff.identical diff = HeaderDiff(ha, hb, ignore_keywords=["B"]) assert not diff.identical assert diff.diff_keyword_values == {"C": [(3, 5)]} report = diff.report() assert "Keyword B has different values" not in report assert "Keyword C has different values" in report # Test case-insensitivity diff = HeaderDiff(ha, hb, ignore_keywords=["b"]) assert not diff.identical assert diff.diff_keyword_values == {"C": [(3, 5)]} def test_ignore_keyword_comments(self): ha = Header([("A", 1, "A"), ("B", 2, "B"), ("C", 3, "C")]) hb = ha.copy() hb.comments["B"] = "D" hb.comments["C"] = "E" diff = HeaderDiff(ha, hb, ignore_comments=["*"]) assert diff.identical diff = HeaderDiff(ha, hb, ignore_comments=["B"]) assert not diff.identical assert diff.diff_keyword_comments == {"C": [("C", "E")]} report = diff.report() assert "Keyword B has different comments" not in report assert "Keyword C has different comments" in report # Test case-insensitivity diff = HeaderDiff(ha, hb, ignore_comments=["b"]) assert not diff.identical assert diff.diff_keyword_comments == {"C": [("C", "E")]} def test_trivial_identical_images(self): ia = np.arange(100).reshape(10, 10) ib = np.arange(100).reshape(10, 10) diff = ImageDataDiff(ia, ib) assert diff.identical assert diff.diff_total == 0 def test_identical_within_relative_tolerance(self): ia = np.ones((10, 10)) - 0.00001 ib = np.ones((10, 10)) - 0.00002 diff = ImageDataDiff(ia, ib, rtol=1.0e-4) assert diff.identical assert diff.diff_total == 0 def test_identical_within_absolute_tolerance(self): ia = np.zeros((10, 10)) - 0.00001 ib = np.zeros((10, 10)) - 0.00002 diff = ImageDataDiff(ia, ib, rtol=1.0e-4) assert not diff.identical assert diff.diff_total == 100 diff = ImageDataDiff(ia, ib, atol=1.0e-4) assert diff.identical assert diff.diff_total == 0 def test_identical_within_rtol_and_atol(self): ia = np.zeros((10, 10)) - 0.00001 ib = np.zeros((10, 10)) - 0.00002 diff = ImageDataDiff(ia, ib, rtol=1.0e-5, atol=1.0e-5) assert diff.identical assert diff.diff_total == 0 def test_not_identical_within_rtol_and_atol(self): ia = np.zeros((10, 10)) - 0.00001 ib = np.zeros((10, 10)) - 0.00002 diff = ImageDataDiff(ia, ib, rtol=1.0e-5, atol=1.0e-6) assert not diff.identical assert diff.diff_total == 100 def test_identical_comp_image_hdus(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/189 For this test we mostly just care that comparing to compressed images does not crash, and returns the correct results. Two compressed images will be considered identical if the decompressed data is the same. Obviously we test whether or not the same compression was used by looking for (or ignoring) header differences. """ data = np.arange(100.0).reshape(10, 10) hdu = fits.CompImageHDU(data=data) hdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as hdula, fits.open( self.temp("test.fits") ) as hdulb: diff = FITSDiff(hdula, hdulb) assert diff.identical def test_different_dimensions(self): ia = np.arange(100).reshape(10, 10) ib = np.arange(100) - 1 # Although ib could be reshaped into the same dimensions, for now the # data is not compared anyways diff = ImageDataDiff(ia, ib) assert not diff.identical assert diff.diff_dimensions == ((10, 10), (100,)) assert diff.diff_total == 0 report = diff.report() assert "Data dimensions differ" in report assert "a: 10 x 10" in report assert "b: 100" in report assert "No further data comparison performed." def test_different_pixels(self): ia = np.arange(100).reshape(10, 10) ib = np.arange(100).reshape(10, 10) ib[0, 0] = 10 ib[5, 5] = 20 diff = ImageDataDiff(ia, ib) assert not diff.identical assert diff.diff_dimensions == () assert diff.diff_total == 2 assert diff.diff_ratio == 0.02 assert diff.diff_pixels == [((0, 0), (0, 10)), ((5, 5), (55, 20))] def test_identical_tables(self): c1 = Column("A", format="L", array=[True, False]) c2 = Column("B", format="X", array=[[0], [1]]) c3 = Column("C", format="4I", dim="(2, 2)", array=[[0, 1, 2, 3], [4, 5, 6, 7]]) c4 = Column("D", format="J", bscale=2.0, array=[0, 1]) c5 = Column("E", format="A3", array=["abc", "def"]) c6 = Column("F", format="E", unit="m", array=[0.0, 1.0]) c7 = Column("G", format="D", bzero=-0.1, array=[0.0, 1.0]) c8 = Column("H", format="C", array=[0.0 + 1.0j, 2.0 + 3.0j]) c9 = Column("I", format="M", array=[4.0 + 5.0j, 6.0 + 7.0j]) c10 = Column("J", format="PI(2)", array=[[0, 1], [2, 3]]) columns = [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10] ta = BinTableHDU.from_columns(columns) tb = BinTableHDU.from_columns([c.copy() for c in columns]) diff = TableDataDiff(ta.data, tb.data) assert diff.identical assert len(diff.common_columns) == 10 assert diff.common_column_names == set("abcdefghij") assert diff.diff_ratio == 0 assert diff.diff_total == 0 def test_diff_empty_tables(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/178 Ensure that diffing tables containing empty data doesn't crash. """ c1 = Column("D", format="J") c2 = Column("E", format="J") thdu = BinTableHDU.from_columns([c1, c2], nrows=0) hdula = fits.HDUList([thdu]) hdulb = fits.HDUList([thdu]) diff = FITSDiff(hdula, hdulb) assert diff.identical def test_ignore_table_fields(self): c1 = Column("A", format="L", array=[True, False]) c2 = Column("B", format="X", array=[[0], [1]]) c3 = Column("C", format="4I", dim="(2, 2)", array=[[0, 1, 2, 3], [4, 5, 6, 7]]) c4 = Column("B", format="X", array=[[1], [0]]) c5 = Column("C", format="4I", dim="(2, 2)", array=[[1, 2, 3, 4], [5, 6, 7, 8]]) ta = BinTableHDU.from_columns([c1, c2, c3]) tb = BinTableHDU.from_columns([c1, c4, c5]) diff = TableDataDiff(ta.data, tb.data, ignore_fields=["B", "C"]) assert diff.identical # The only common column should be c1 assert len(diff.common_columns) == 1 assert diff.common_column_names == {"a"} assert diff.diff_ratio == 0 assert diff.diff_total == 0 def test_different_table_field_names(self): ca = Column("A", format="L", array=[True, False]) cb = Column("B", format="L", array=[True, False]) cc = Column("C", format="L", array=[True, False]) ta = BinTableHDU.from_columns([ca, cb]) tb = BinTableHDU.from_columns([ca, cc]) diff = TableDataDiff(ta.data, tb.data) assert not diff.identical assert len(diff.common_columns) == 1 assert diff.common_column_names == {"a"} assert diff.diff_column_names == (["B"], ["C"]) assert diff.diff_ratio == 0 assert diff.diff_total == 0 report = diff.report() assert "Extra column B of format L in a" in report assert "Extra column C of format L in b" in report def test_different_table_field_counts(self): """ Test tables with some common columns, but different number of columns overall. """ ca = Column("A", format="L", array=[True, False]) cb = Column("B", format="L", array=[True, False]) cc = Column("C", format="L", array=[True, False]) ta = BinTableHDU.from_columns([cb]) tb = BinTableHDU.from_columns([ca, cb, cc]) diff = TableDataDiff(ta.data, tb.data) assert not diff.identical assert diff.diff_column_count == (1, 3) assert len(diff.common_columns) == 1 assert diff.common_column_names == {"b"} assert diff.diff_column_names == ([], ["A", "C"]) assert diff.diff_ratio == 0 assert diff.diff_total == 0 report = diff.report() assert " Tables have different number of columns:" in report assert " a: 1\n b: 3" in report def test_different_table_rows(self): """ Test tables that are otherwise identical but one has more rows than the other. """ ca1 = Column("A", format="L", array=[True, False]) cb1 = Column("B", format="L", array=[True, False]) ca2 = Column("A", format="L", array=[True, False, True]) cb2 = Column("B", format="L", array=[True, False, True]) ta = BinTableHDU.from_columns([ca1, cb1]) tb = BinTableHDU.from_columns([ca2, cb2]) diff = TableDataDiff(ta.data, tb.data) assert not diff.identical assert diff.diff_column_count == () assert len(diff.common_columns) == 2 assert diff.diff_rows == (2, 3) assert diff.diff_values == [] report = diff.report() assert "Table rows differ" in report assert "a: 2" in report assert "b: 3" in report assert "No further data comparison performed." def test_different_table_data(self): """ Test diffing table data on columns of several different data formats and dimensions. """ ca1 = Column("A", format="L", array=[True, False]) ca2 = Column("B", format="X", array=[[0], [1]]) ca3 = Column("C", format="4I", dim="(2, 2)", array=[[0, 1, 2, 3], [4, 5, 6, 7]]) ca4 = Column("D", format="J", bscale=2.0, array=[0.0, 2.0]) ca5 = Column("E", format="A3", array=["abc", "def"]) ca6 = Column("F", format="E", unit="m", array=[0.0, 1.0]) ca7 = Column("G", format="D", bzero=-0.1, array=[0.0, 1.0]) ca8 = Column("H", format="C", array=[0.0 + 1.0j, 2.0 + 3.0j]) ca9 = Column("I", format="M", array=[4.0 + 5.0j, 6.0 + 7.0j]) ca10 = Column("J", format="PI(2)", array=[[0, 1], [2, 3]]) cb1 = Column("A", format="L", array=[False, False]) cb2 = Column("B", format="X", array=[[0], [0]]) cb3 = Column("C", format="4I", dim="(2, 2)", array=[[0, 1, 2, 3], [5, 6, 7, 8]]) cb4 = Column("D", format="J", bscale=2.0, array=[2.0, 2.0]) cb5 = Column("E", format="A3", array=["abc", "ghi"]) cb6 = Column("F", format="E", unit="m", array=[1.0, 2.0]) cb7 = Column("G", format="D", bzero=-0.1, array=[2.0, 3.0]) cb8 = Column("H", format="C", array=[1.0 + 1.0j, 2.0 + 3.0j]) cb9 = Column("I", format="M", array=[5.0 + 5.0j, 6.0 + 7.0j]) cb10 = Column("J", format="PI(2)", array=[[1, 2], [3, 4]]) ta = BinTableHDU.from_columns( [ca1, ca2, ca3, ca4, ca5, ca6, ca7, ca8, ca9, ca10] ) tb = BinTableHDU.from_columns( [cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9, cb10] ) diff = TableDataDiff(ta.data, tb.data, numdiffs=20) assert not diff.identical # The column definitions are the same, but not the column values assert diff.diff_columns == () assert diff.diff_values[0] == (("A", 0), (True, False)) assert diff.diff_values[1] == (("B", 1), ([1], [0])) assert diff.diff_values[2][0] == ("C", 1) assert (diff.diff_values[2][1][0] == [[4, 5], [6, 7]]).all() assert (diff.diff_values[2][1][1] == [[5, 6], [7, 8]]).all() assert diff.diff_values[3] == (("D", 0), (0, 2.0)) assert diff.diff_values[4] == (("E", 1), ("def", "ghi")) assert diff.diff_values[5] == (("F", 0), (0.0, 1.0)) assert diff.diff_values[6] == (("F", 1), (1.0, 2.0)) assert diff.diff_values[7] == (("G", 0), (0.0, 2.0)) assert diff.diff_values[8] == (("G", 1), (1.0, 3.0)) assert diff.diff_values[9] == (("H", 0), (0.0 + 1.0j, 1.0 + 1.0j)) assert diff.diff_values[10] == (("I", 0), (4.0 + 5.0j, 5.0 + 5.0j)) assert diff.diff_values[11][0] == ("J", 0) assert (diff.diff_values[11][1][0] == [0, 1]).all() assert (diff.diff_values[11][1][1] == [1, 2]).all() assert diff.diff_values[12][0] == ("J", 1) assert (diff.diff_values[12][1][0] == [2, 3]).all() assert (diff.diff_values[12][1][1] == [3, 4]).all() assert diff.diff_total == 13 assert diff.diff_ratio == 0.65 report = diff.report() assert "Column A data differs in row 0:\n a> True\n b> False" in report assert "...and at 1 more indices.\n Column D data differs in row 0:" in report assert "13 different table data element(s) found (65.00% different)" in report assert report.count("more indices") == 1 def test_identical_files_basic(self): """Test identicality of two simple, extensionless files.""" a = np.arange(100).reshape(10, 10) hdu = PrimaryHDU(data=a) hdu.writeto(self.temp("testa.fits")) hdu.writeto(self.temp("testb.fits")) diff = FITSDiff(self.temp("testa.fits"), self.temp("testb.fits")) assert diff.identical report = diff.report() # Primary HDUs should contain no differences assert "Primary HDU" not in report assert "Extension HDU" not in report assert "No differences found." in report a = np.arange(10) ehdu = ImageHDU(data=a) diff = HDUDiff(ehdu, ehdu) assert diff.identical report = diff.report() assert "No differences found." in report def test_partially_identical_files1(self): """ Test files that have some identical HDUs but a different extension count. """ a = np.arange(100).reshape(10, 10) phdu = PrimaryHDU(data=a) ehdu = ImageHDU(data=a) hdula = HDUList([phdu, ehdu]) hdulb = HDUList([phdu, ehdu, ehdu]) diff = FITSDiff(hdula, hdulb) assert not diff.identical assert diff.diff_hdu_count == (2, 3) # diff_hdus should be empty, since the third extension in hdulb # has nothing to compare against assert diff.diff_hdus == [] report = diff.report() assert "Files contain different numbers of HDUs" in report assert "a: 2\n b: 3" in report assert "No differences found between common HDUs" in report def test_partially_identical_files2(self): """ Test files that have some identical HDUs but one different HDU. """ a = np.arange(100).reshape(10, 10) phdu = PrimaryHDU(data=a) ehdu = ImageHDU(data=a) ehdu2 = ImageHDU(data=(a + 1)) hdula = HDUList([phdu, ehdu, ehdu]) hdulb = HDUList([phdu, ehdu2, ehdu]) diff = FITSDiff(hdula, hdulb) assert not diff.identical assert diff.diff_hdu_count == () assert len(diff.diff_hdus) == 1 assert diff.diff_hdus[0][0] == 1 hdudiff = diff.diff_hdus[0][1] assert not hdudiff.identical assert hdudiff.diff_extnames == () assert hdudiff.diff_extvers == () assert hdudiff.diff_extension_types == () assert hdudiff.diff_headers.identical assert hdudiff.diff_data is not None datadiff = hdudiff.diff_data assert isinstance(datadiff, ImageDataDiff) assert not datadiff.identical assert datadiff.diff_dimensions == () assert datadiff.diff_pixels == [((0, y), (y, y + 1)) for y in range(10)] assert datadiff.diff_ratio == 1.0 assert datadiff.diff_total == 100 report = diff.report() # Primary HDU and 2nd extension HDU should have no differences assert "Primary HDU" not in report assert "Extension HDU 2" not in report assert "Extension HDU 1" in report assert "Headers contain differences" not in report assert "Data contains differences" in report for y in range(10): assert f"Data differs at [{y + 1}, 1]" in report assert "100 different pixels found (100.00% different)." in report def test_partially_identical_files3(self): """ Test files that have some identical HDUs but a different extension name. """ phdu = PrimaryHDU() ehdu = ImageHDU(name="FOO") hdula = HDUList([phdu, ehdu]) ehdu = BinTableHDU(name="BAR") ehdu.header["EXTVER"] = 2 ehdu.header["EXTLEVEL"] = 3 hdulb = HDUList([phdu, ehdu]) diff = FITSDiff(hdula, hdulb) assert not diff.identical assert diff.diff_hdus[0][0] == 1 hdu_diff = diff.diff_hdus[0][1] assert hdu_diff.diff_extension_types == ("IMAGE", "BINTABLE") assert hdu_diff.diff_extnames == ("FOO", "BAR") assert hdu_diff.diff_extvers == (1, 2) assert hdu_diff.diff_extlevels == (1, 3) report = diff.report() assert "Extension types differ" in report assert "a: IMAGE\n b: BINTABLE" in report assert "Extension names differ" in report assert "a: FOO\n b: BAR" in report assert "Extension versions differ" in report assert "a: 1\n b: 2" in report assert "Extension levels differ" in report assert "a: 1\n b: 2" in report def test_diff_nans(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/204 """ # First test some arrays that should be equivalent.... arr = np.empty((10, 10), dtype=np.float64) arr[:5] = 1.0 arr[5:] = np.nan arr2 = arr.copy() table = np.rec.array( [(1.0, 2.0), (3.0, np.nan), (np.nan, np.nan)], names=["cola", "colb"] ).view(fits.FITS_rec) table2 = table.copy() assert ImageDataDiff(arr, arr2).identical assert TableDataDiff(table, table2).identical # Now let's introduce some differences, where there are nans and where # there are not nans arr2[0][0] = 2.0 arr2[5][0] = 2.0 table2[0][0] = 2.0 table2[1][1] = 2.0 diff = ImageDataDiff(arr, arr2) assert not diff.identical assert diff.diff_pixels[0] == ((0, 0), (1.0, 2.0)) assert diff.diff_pixels[1][0] == (5, 0) assert np.isnan(diff.diff_pixels[1][1][0]) assert diff.diff_pixels[1][1][1] == 2.0 diff = TableDataDiff(table, table2) assert not diff.identical assert diff.diff_values[0] == (("cola", 0), (1.0, 2.0)) assert diff.diff_values[1][0] == ("colb", 1) assert np.isnan(diff.diff_values[1][1][0]) assert diff.diff_values[1][1][1] == 2.0 def test_file_output_from_path_string(self): outpath = self.temp("diff_output.txt") ha = Header([("A", 1), ("B", 2), ("C", 3)]) hb = ha.copy() hb["C"] = 4 diffobj = HeaderDiff(ha, hb) diffobj.report(fileobj=outpath) report_as_string = diffobj.report() with open(outpath) as fout: assert fout.read() == report_as_string def test_file_output_overwrite_safety(self): outpath = self.temp("diff_output.txt") ha = Header([("A", 1), ("B", 2), ("C", 3)]) hb = ha.copy() hb["C"] = 4 diffobj = HeaderDiff(ha, hb) diffobj.report(fileobj=outpath) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): diffobj.report(fileobj=outpath) def test_file_output_overwrite_success(self): outpath = self.temp("diff_output.txt") ha = Header([("A", 1), ("B", 2), ("C", 3)]) hb = ha.copy() hb["C"] = 4 diffobj = HeaderDiff(ha, hb) diffobj.report(fileobj=outpath) report_as_string = diffobj.report() diffobj.report(fileobj=outpath, overwrite=True) with open(outpath) as fout: assert ( fout.read() == report_as_string ), "overwritten output file is not identical to report string" def test_rawdatadiff_nodiff(self): a = np.arange(100, dtype="uint8").reshape(10, 10) b = a.copy() hdu_a = DummyNonstandardExtHDU(data=a) hdu_b = DummyNonstandardExtHDU(data=b) diff = HDUDiff(hdu_a, hdu_b) assert diff.identical report = diff.report() assert "No differences found." in report def test_rawdatadiff_dimsdiff(self): a = np.arange(100, dtype="uint8") + 10 b = a[:80].copy() hdu_a = DummyNonstandardExtHDU(data=a) hdu_b = DummyNonstandardExtHDU(data=b) diff = HDUDiff(hdu_a, hdu_b) assert not diff.identical report = diff.report() assert "Data sizes differ:" in report assert "a: 100 bytes" in report assert "b: 80 bytes" in report assert "No further data comparison performed." in report def test_rawdatadiff_bytesdiff(self): a = np.arange(100, dtype="uint8") + 10 b = a.copy() changes = [(30, 200), (89, 170)] for i, v in changes: b[i] = v hdu_a = DummyNonstandardExtHDU(data=a) hdu_b = DummyNonstandardExtHDU(data=b) diff = HDUDiff(hdu_a, hdu_b) assert not diff.identical diff_bytes = diff.diff_data.diff_bytes assert len(changes) == len(diff_bytes) for j, (i, v) in enumerate(changes): assert diff_bytes[j] == (i, (i + 10, v)) report = diff.report() assert "Data contains differences:" in report for i, _ in changes: assert f"Data differs at byte {i}:" in report assert "2 different bytes found (2.00% different)." in report def test_fitsdiff_hdu_name(tmp_path): """Make sure diff report reports HDU name and ver if same in files""" path1 = tmp_path / "test1.fits" path2 = tmp_path / "test2.fits" hdulist = HDUList([PrimaryHDU(), ImageHDU(data=np.zeros(5), name="SCI")]) hdulist.writeto(path1) hdulist[1].data[0] = 1 hdulist.writeto(path2) diff = FITSDiff(path1, path2) assert "Extension HDU 1 (SCI, 1):" in diff.report() def test_fitsdiff_no_hdu_name(tmp_path): """Make sure diff report doesn't report HDU name if not in files""" path1 = tmp_path / "test1.fits" path2 = tmp_path / "test2.fits" hdulist = HDUList([PrimaryHDU(), ImageHDU(data=np.zeros(5))]) hdulist.writeto(path1) hdulist[1].data[0] = 1 hdulist.writeto(path2) diff = FITSDiff(path1, path2) assert "Extension HDU 1:" in diff.report() def test_fitsdiff_with_names(tmp_path): """Make sure diff report doesn't report HDU name if not same in files""" path1 = tmp_path / "test1.fits" path2 = tmp_path / "test2.fits" hdulist = HDUList([PrimaryHDU(), ImageHDU(data=np.zeros(5), name="SCI", ver=1)]) hdulist.writeto(path1) hdulist[1].name = "ERR" hdulist.writeto(path2) diff = FITSDiff(path1, path2) assert "Extension HDU 1:" in diff.report() def test_rawdatadiff_diff_with_rtol(tmp_path): """Regression test for https://github.com/astropy/astropy/issues/13330""" path1 = tmp_path / "test1.fits" path2 = tmp_path / "test2.fits" a = np.zeros((10, 2), dtype="float32") a[:, 0] = np.arange(10, dtype="float32") + 10 a[:, 1] = np.arange(10, dtype="float32") + 20 b = a.copy() changes = [(3, 13.1, 23.1), (8, 20.5, 30.5)] for i, v, w in changes: b[i, 0] = v b[i, 1] = w ca = Column("A", format="20E", array=[a]) cb = Column("A", format="20E", array=[b]) hdu_a = BinTableHDU.from_columns([ca]) hdu_a.writeto(path1, overwrite=True) hdu_b = BinTableHDU.from_columns([cb]) hdu_b.writeto(path2, overwrite=True) with fits.open(path1) as fits1: with fits.open(path2) as fits2: diff = FITSDiff(fits1, fits2, atol=0, rtol=0.001) str1 = diff.report(fileobj=None, indent=0) diff = FITSDiff(fits1, fits2, atol=0, rtol=0.01) str2 = diff.report(fileobj=None, indent=0) assert "...and at 1 more indices." in str1 assert "...and at 1 more indices." not in str2
afe90b6fa54343720ef6658c35419e12df00c4d34c402553b5ce246ec28391e8
# Licensed under a 3-clause BSD style license - see PYFITS.rst import collections import copy import warnings from io import BytesIO, StringIO import numpy as np import pytest from astropy.io import fits from astropy.io.fits.card import _pad from astropy.io.fits.header import _pad_length from astropy.io.fits.util import encode_ascii from astropy.io.fits.verify import VerifyError, VerifyWarning from astropy.utils.exceptions import AstropyUserWarning from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH from .conftest import FitsTestCase def test_shallow_copy(): """Make sure that operations on a shallow copy do not alter the original. #4990.""" original_header = fits.Header([("a", 1), ("b", 1)]) copied_header = copy.copy(original_header) # Modifying the original dict should not alter the copy original_header["c"] = 100 assert "c" not in copied_header # and changing the copy should not change the original. copied_header["a"] = 0 assert original_header["a"] == 1 def test_init_with_header(): """Make sure that creating a Header from another Header makes a copy if copy is True.""" original_header = fits.Header([("a", 10)]) new_header = fits.Header(original_header, copy=True) original_header["a"] = 20 assert new_header["a"] == 10 new_header["a"] = 0 assert original_header["a"] == 20 def test_init_with_dict(): dict1 = {"a": 11, "b": 12, "c": 13, "d": 14, "e": 15} h1 = fits.Header(dict1) for i in dict1: assert dict1[i] == h1[i] def test_init_with_ordereddict(): # Create a list of tuples. Each tuple consisting of a letter and the number list1 = [(i, j) for j, i in enumerate("abcdefghijklmnopqrstuvwxyz")] # Create an ordered dictionary and a header from this dictionary dict1 = collections.OrderedDict(list1) h1 = fits.Header(dict1) # Check that the order is preserved of the initial list assert all(h1[val] == list1[i][1] for i, val in enumerate(h1)) class TestHeaderFunctions(FitsTestCase): """Test Header and Card objects.""" def test_rename_keyword(self): """Test renaming keyword with rename_keyword.""" header = fits.Header([("A", "B", "C"), ("D", "E", "F")]) header.rename_keyword("A", "B") assert "A" not in header assert "B" in header assert header[0] == "B" assert header["B"] == "B" assert header.comments["B"] == "C" @pytest.mark.parametrize("key", ["A", "a"]) def test_indexing_case(self, key): """Check that indexing is case insensitive""" header = fits.Header([("A", "B", "C"), ("D", "E", "F")]) assert key in header assert header[key] == "B" assert header.get(key) == "B" assert header.index(key) == 0 assert header.comments[key] == "C" assert header.count(key) == 1 header.remove(key, ignore_missing=False) def test_card_constructor_default_args(self): """Test Card constructor with default argument values.""" c = fits.Card() assert "" == c.keyword def test_card_from_bytes(self): """ Test loading a Card from a `bytes` object (assuming latin-1 encoding). """ c = fits.Card.fromstring(b"ABC = 'abc'") assert c.keyword == "ABC" assert c.value == "abc" def test_string_value_card(self): """Test Card constructor with string value""" c = fits.Card("abc", "<8 ch") assert str(c) == _pad("ABC = '<8 ch '") c = fits.Card("nullstr", "") assert str(c) == _pad("NULLSTR = ''") def test_boolean_value_card(self): """Test Card constructor with boolean value""" c = fits.Card("abc", True) assert str(c) == _pad("ABC = T") c = fits.Card.fromstring("ABC = F") assert c.value is False def test_long_integer_value_card(self): """Test Card constructor with long integer value""" c = fits.Card("long_int", -467374636747637647347374734737437) assert str(c) == _pad("LONG_INT= -467374636747637647347374734737437") def test_floating_point_value_card(self): """Test Card constructor with floating point value""" c = fits.Card("floatnum", -467374636747637647347374734737437.0) if str(c) != _pad("FLOATNUM= -4.6737463674763E+32") and str(c) != _pad( "FLOATNUM= -4.6737463674763E+032" ): assert str(c) == _pad("FLOATNUM= -4.6737463674763E+32") def test_complex_value_card(self): """Test Card constructor with complex value""" c = fits.Card("abc", (1.2345377437887837487e88 + 6324767364763746367e-33j)) f1 = _pad("ABC = (1.23453774378878E+88, 6.32476736476374E-15)") f2 = _pad("ABC = (1.2345377437887E+088, 6.3247673647637E-015)") f3 = _pad("ABC = (1.23453774378878E+88, 6.32476736476374E-15)") if str(c) != f1 and str(c) != f2: assert str(c) == f3 def test_card_image_constructed_too_long(self): """Test that over-long cards truncate the comment""" # card image constructed from key/value/comment is too long # (non-string value) c = fits.Card("abc", 9, "abcde" * 20) with pytest.warns(fits.verify.VerifyWarning): assert ( str(c) == "ABC = 9 " "/ abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeab" ) c = fits.Card("abc", "a" * 68, "abcdefg") with pytest.warns(fits.verify.VerifyWarning): assert str(c) == f"ABC = '{'a' * 68}'" def test_constructor_filter_illegal_data_structures(self): """Test that Card constructor raises exceptions on bad arguments""" pytest.raises(ValueError, fits.Card, ("abc",), {"value": (2, 3)}) pytest.raises(ValueError, fits.Card, "key", [], "comment") def test_keyword_too_long(self): """Test that long Card keywords are allowed, but with a warning""" pytest.warns(UserWarning, fits.Card, "abcdefghi", "long") def test_illegal_characters_in_key(self): """ Test that Card constructor allows illegal characters in the keyword, but creates a HIERARCH card. """ # This test used to check that a ValueError was raised, because a # keyword like 'abc+' was simply not allowed. Now it should create a # HIERARCH card. with pytest.warns(AstropyUserWarning) as w: c = fits.Card("abc+", 9) assert len(w) == 1 assert c.image == _pad("HIERARCH abc+ = 9") def test_add_history(self): header = fits.Header( [ ("A", "B", "C"), ("HISTORY", 1), ("HISTORY", 2), ("HISTORY", 3), ("", "", ""), ("", "", ""), ] ) header.add_history(4) # One of the blanks should get used, so the length shouldn't change assert len(header) == 6 assert header.cards[4].value == 4 assert header["HISTORY"] == [1, 2, 3, 4] assert repr(header["HISTORY"]) == "1\n2\n3\n4" header.add_history(0, after="A") assert len(header) == 6 assert header.cards[1].value == 0 assert header["HISTORY"] == [0, 1, 2, 3, 4] def test_add_blank(self): header = fits.Header( [("A", "B", "C"), ("", 1), ("", 2), ("", 3), ("", "", ""), ("", "", "")] ) header.add_blank(4) # This time a new blank should be added, and the existing blanks don't # get used... (though this is really kinda sketchy--there's a # distinction between truly blank cards, and cards with blank keywords # that isn't currently made int he code) assert len(header) == 7 assert header.cards[6].value == 4 assert header[""] == [1, 2, 3, "", "", 4] assert repr(header[""]) == "1\n2\n3\n\n\n4" header.add_blank(0, after="A") assert len(header) == 8 assert header.cards[1].value == 0 assert header[""] == [0, 1, 2, 3, "", "", 4] header[""] = 5 header[" "] = 6 assert header[""] == [0, 1, 2, 3, "", "", 4, 5, 6] assert header[" "] == [0, 1, 2, 3, "", "", 4, 5, 6] def test_update(self): class FakeHeader(list): def keys(self): return [l[0] for l in self] def __getitem__(self, key): return next(l[1:] for l in self if l[0] == key) header = fits.Header() header.update({"FOO": ("BAR", "BAZ")}) header.update(FakeHeader([("A", 1), ("B", 2, "comment")])) assert set(header.keys()) == {"FOO", "A", "B"} assert header.comments["B"] == "comment" # test that comments are preserved tmphdr = fits.Header() tmphdr["HELLO"] = (1, "this is a comment") header.update(tmphdr) assert set(header.keys()) == {"FOO", "A", "B", "HELLO"} assert header.comments["HELLO"] == "this is a comment" header.update(NAXIS1=100, NAXIS2=100) assert set(header.keys()) == {"FOO", "A", "B", "HELLO", "NAXIS1", "NAXIS2"} assert set(header.values()) == {"BAR", 1, 2, 100, 100} def test_update_comment(self): hdul = fits.open(self.data("arange.fits")) hdul[0].header.update({"FOO": ("BAR", "BAZ")}) assert hdul[0].header["FOO"] == "BAR" assert hdul[0].header.comments["FOO"] == "BAZ" with pytest.raises(ValueError): hdul[0].header.update({"FOO2": ("BAR", "BAZ", "EXTRA")}) hdul.writeto(self.temp("test.fits")) hdul.close() hdul = fits.open(self.temp("test.fits"), mode="update") hdul[0].header.comments["FOO"] = "QUX" hdul.close() hdul = fits.open(self.temp("test.fits")) assert hdul[0].header.comments["FOO"] == "QUX" hdul[0].header.add_comment(0, after="FOO") assert str(hdul[0].header.cards[-1]).strip() == "COMMENT 0" hdul.close() def test_commentary_cards(self): # commentary cards val = "A commentary card's value has no quotes around it." c = fits.Card("HISTORY", val) assert str(c) == _pad("HISTORY " + val) val = "A commentary card has no comment." c = fits.Card("COMMENT", val, "comment") assert str(c) == _pad("COMMENT " + val) def test_commentary_card_created_by_fromstring(self): # commentary card created by fromstring() c = fits.Card.fromstring( "COMMENT card has no comments. " "/ text after slash is still part of the value." ) assert ( c.value == "card has no comments. " "/ text after slash is still part of the value." ) assert c.comment == "" def test_commentary_card_will_not_parse_numerical_value(self): # commentary card will not parse the numerical value c = fits.Card.fromstring("HISTORY (1, 2)") assert str(c) == _pad("HISTORY (1, 2)") def test_equal_sign_after_column8(self): # equal sign after column 8 of a commentary card will be part of the # string value c = fits.Card.fromstring("HISTORY = (1, 2)") assert str(c) == _pad("HISTORY = (1, 2)") def test_blank_keyword(self): c = fits.Card("", " / EXPOSURE INFORMATION") assert str(c) == _pad(" / EXPOSURE INFORMATION") c = fits.Card.fromstring(str(c)) assert c.keyword == "" assert c.value == " / EXPOSURE INFORMATION" def test_specify_undefined_value(self): # this is how to specify an undefined value c = fits.Card("undef", fits.card.UNDEFINED) assert str(c) == _pad("UNDEF =") def test_complex_number_using_string_input(self): # complex number using string input c = fits.Card.fromstring("ABC = (8, 9)") assert str(c) == _pad("ABC = (8, 9)") def test_fixable_non_standard_fits_card(self, capsys): # fixable non-standard FITS card will keep the original format c = fits.Card.fromstring("abc = + 2.1 e + 12") assert c.value == 2100000000000.0 with pytest.warns( fits.verify.VerifyWarning, match=r"Verification reported errors" ): assert str(c) == _pad("ABC = +2.1E+12") def test_fixable_non_fsc(self): # fixable non-FSC: if the card is not parsable, it's value will be # assumed # to be a string and everything after the first slash will be comment c = fits.Card.fromstring( "no_quote= this card's value has no quotes / let's also try the comment" ) with pytest.warns( fits.verify.VerifyWarning, match=r"Verification reported errors" ): assert ( str(c) == "NO_QUOTE= 'this card''s value has no quotes' " "/ let's also try the comment " ) def test_undefined_value_using_string_input(self): # undefined value using string input c = fits.Card.fromstring("ABC = ") assert str(c) == _pad("ABC =") def test_undefined_keys_values(self): header = fits.Header() header["FOO"] = "BAR" header["UNDEF"] = None assert list(header.values()) == ["BAR", None] assert list(header.items()) == [("FOO", "BAR"), ("UNDEF", None)] def test_mislocated_equal_sign(self, capsys): # test mislocated "=" sign c = fits.Card.fromstring("XYZ= 100") assert c.keyword == "XYZ" assert c.value == 100 with pytest.warns( fits.verify.VerifyWarning, match=r"Verification reported errors" ): assert str(c) == _pad("XYZ = 100") def test_equal_only_up_to_column_10(self, capsys): # the test of "=" location is only up to column 10 # This test used to check if Astropy rewrote this card to a new format, # something like "HISTO = '= (1, 2)". But since ticket #109 if the # format is completely wrong we don't make any assumptions and the card # should be left alone c = fits.Card.fromstring("HISTO = (1, 2)") with pytest.warns(AstropyUserWarning, match=r"header keyword is invalid"): assert str(c) == _pad("HISTO = (1, 2)") # Likewise this card should just be left in its original form and # we shouldn't guess how to parse it or rewrite it. c = fits.Card.fromstring(" HISTORY (1, 2)") with pytest.warns(AstropyUserWarning, match=r"header keyword is invalid"): assert str(c) == _pad(" HISTORY (1, 2)") def test_verify_invalid_equal_sign(self): # verification c = fits.Card.fromstring("ABC= a6") with pytest.warns(AstropyUserWarning) as w: c.verify() err_text1 = "Card 'ABC' is not FITS standard (equal sign not at column 8)" err_text2 = "Card 'ABC' is not FITS standard (invalid value string: 'a6'" assert len(w) == 4 assert err_text1 in str(w[1].message) assert err_text2 in str(w[2].message) def test_fix_invalid_equal_sign(self): fix_text = "Fixed 'ABC' card to meet the FITS standard." c = fits.Card.fromstring("ABC= a6") with pytest.warns(AstropyUserWarning, match=fix_text) as w: c.verify("fix") assert len(w) == 4 assert str(c) == _pad("ABC = 'a6 '") def test_long_string_value(self): # test long string value c = fits.Card("abc", "long string value " * 10, "long comment " * 10) assert ( str(c) == "ABC = 'long string value long string value long string value long string &' " "CONTINUE 'value long string value long string value long string value long &' " "CONTINUE 'string value long string value long string value &' " "CONTINUE '&' / long comment long comment long comment long comment long " "CONTINUE '&' / comment long comment long comment long comment long comment " "CONTINUE '' / long comment " ) def test_long_string_value_with_multiple_long_words(self): """ Regression test for https://github.com/astropy/astropy/issues/11298 """ c = fits.Card( "WHATEVER", "SuperCalibrationParameters_XXXX_YYYY_ZZZZZ_KK_01_02_" "03)-AAABBBCCC.n.h5 SuperNavigationParameters_XXXX_YYYY" "_ZZZZZ_KK_01_02_03)-AAABBBCCC.n.xml", ) assert ( str(c) == "WHATEVER= 'SuperCalibrationParameters_XXXX_YYYY_ZZZZZ_KK_01_02_03)-AAABBBCCC.n&'" "CONTINUE '.h5 &' " "CONTINUE 'SuperNavigationParameters_XXXX_YYYY_ZZZZZ_KK_01_02_03)-AAABBBCCC.n.&'" "CONTINUE 'xml' " ) def test_long_unicode_string(self): """Regression test for https://github.com/spacetelescope/PyFITS/issues/1 So long as a unicode string can be converted to ASCII it should have no different behavior in this regard from a byte string. """ h1 = fits.Header() h1["TEST"] = "abcdefg" * 30 h2 = fits.Header() h2["TEST"] = "abcdefg" * 30 assert str(h1) == str(h2) def test_long_string_repr(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/193 Ensure that the __repr__() for cards represented with CONTINUE cards is split across multiple lines (broken at each *physical* card). """ header = fits.Header() header["TEST1"] = ("Regular value", "Regular comment") header["TEST2"] = ("long string value " * 10, "long comment " * 10) header["TEST3"] = ("Regular value", "Regular comment") assert repr(header).splitlines() == [ str(fits.Card("TEST1", "Regular value", "Regular comment")), "TEST2 = 'long string value long string value long string value long string &' ", "CONTINUE 'value long string value long string value long string value long &' ", "CONTINUE 'string value long string value long string value &' ", "CONTINUE '&' / long comment long comment long comment long comment long ", "CONTINUE '&' / comment long comment long comment long comment long comment ", "CONTINUE '' / long comment ", str(fits.Card("TEST3", "Regular value", "Regular comment")), ] def test_blank_keyword_long_value(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/194 Test that a blank keyword ('') can be assigned a too-long value that is continued across multiple cards with blank keywords, just like COMMENT and HISTORY cards. """ value = "long string value " * 10 header = fits.Header() header[""] = value assert len(header) == 3 assert " ".join(header[""]) == value.rstrip() # Ensure that this works like other commentary keywords header["COMMENT"] = value header["HISTORY"] = value assert header["COMMENT"] == header["HISTORY"] assert header["COMMENT"] == header[""] def test_long_string_from_file(self): c = fits.Card("abc", "long string value " * 10, "long comment " * 10) hdu = fits.PrimaryHDU() hdu.header.append(c) hdu.writeto(self.temp("test_new.fits")) hdul = fits.open(self.temp("test_new.fits")) c = hdul[0].header.cards["abc"] hdul.close() assert ( str(c) == "ABC = 'long string value long string value long string value long string &' " "CONTINUE 'value long string value long string value long string value long &' " "CONTINUE 'string value long string value long string value &' " "CONTINUE '&' / long comment long comment long comment long comment long " "CONTINUE '&' / comment long comment long comment long comment long comment " "CONTINUE '' / long comment " ) def test_word_in_long_string_too_long(self): # if a word in a long string is too long, it will be cut in the middle c = fits.Card("abc", "longstringvalue" * 10, "longcomment" * 10) assert ( str(c) == "ABC = 'longstringvaluelongstringvaluelongstringvaluelongstringvaluelongstr&'" "CONTINUE 'ingvaluelongstringvaluelongstringvaluelongstringvaluelongstringvalu&'" "CONTINUE 'elongstringvalue&' " "CONTINUE '&' / longcommentlongcommentlongcommentlongcommentlongcommentlongcomme" "CONTINUE '' / ntlongcommentlongcommentlongcommentlongcomment " ) def test_long_string_value_via_fromstring(self, capsys): # long string value via fromstring() method c = fits.Card.fromstring( _pad("abc = 'longstring''s testing & ' / comments in line 1") + _pad( "continue 'continue with long string but without the " "ampersand at the end' /" ) + _pad( "continue 'continue must have string value (with quotes)' " "/ comments with ''. " ) ) with pytest.warns( fits.verify.VerifyWarning, match=r"Verification reported errors" ): assert ( str(c) == "ABC = 'longstring''s testing continue with long string but without the &' " "CONTINUE 'ampersand at the endcontinue must have string value (with quotes)&' " "CONTINUE '' / comments in line 1 comments with ''. " ) def test_continue_card_with_equals_in_value(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/117 """ c = fits.Card.fromstring( _pad( "EXPR = '/grp/hst/cdbs//grid/pickles/dat_uvk/pickles_uk_10.fits * &'" ) + _pad("CONTINUE '5.87359e-12 * MWAvg(Av=0.12)&'") + _pad("CONTINUE '&' / pysyn expression") ) assert c.keyword == "EXPR" assert ( c.value == "/grp/hst/cdbs//grid/pickles/dat_uvk/pickles_uk_10.fits " "* 5.87359e-12 * MWAvg(Av=0.12)" ) assert c.comment == "pysyn expression" def test_final_continue_card_lacks_ampersand(self): """ Regression test for https://github.com/astropy/astropy/issues/3282 """ h = fits.Header() h["SVALUE"] = "A" * 69 assert repr(h).splitlines()[-1] == _pad("CONTINUE 'AA'") def test_final_continue_card_ampersand_removal_on_long_comments(self): """ Regression test for https://github.com/astropy/astropy/issues/3282 """ c = fits.Card("TEST", "long value" * 10, "long comment &" * 10) assert ( str(c) == "TEST = 'long valuelong valuelong valuelong valuelong valuelong valuelong &' " "CONTINUE 'valuelong valuelong valuelong value&' " "CONTINUE '&' / long comment &long comment &long comment &long comment &long " "CONTINUE '&' / comment &long comment &long comment &long comment &long comment " "CONTINUE '' / &long comment & " ) def test_hierarch_card_creation(self): # Test automatic upgrade to hierarch card with pytest.warns( AstropyUserWarning, match="HIERARCH card will be created" ) as w: c = fits.Card( "ESO INS SLIT2 Y1FRML", "ENC=OFFSET+RESOL*acos((WID-(MAX+MIN))/(MAX-MIN)", ) assert len(w) == 1 assert ( str(c) == "HIERARCH ESO INS SLIT2 Y1FRML= " "'ENC=OFFSET+RESOL*acos((WID-(MAX+MIN))/(MAX-MIN)'" ) # Test manual creation of hierarch card c = fits.Card("hierarch abcdefghi", 10) assert str(c) == _pad("HIERARCH abcdefghi = 10") c = fits.Card( "HIERARCH ESO INS SLIT2 Y1FRML", "ENC=OFFSET+RESOL*acos((WID-(MAX+MIN))/(MAX-MIN)", ) assert ( str(c) == "HIERARCH ESO INS SLIT2 Y1FRML= " "'ENC=OFFSET+RESOL*acos((WID-(MAX+MIN))/(MAX-MIN)'" ) def test_hierarch_with_abbrev_value_indicator(self): """Regression test for https://github.com/spacetelescope/PyFITS/issues/5 """ c = fits.Card.fromstring("HIERARCH key.META_4='calFileVersion'") assert c.keyword == "key.META_4" assert c.value == "calFileVersion" assert c.comment == "" def test_hierarch_not_warn(self): """Check that compressed image headers do not issue HIERARCH warnings.""" filename = fits.util.get_testdata_filepath("compressed_image.fits") with fits.open(filename) as hdul: header = hdul[1].header with warnings.catch_warnings(record=True) as warning_list: header["HIERARCH LONG KEYWORD"] = 42 assert len(warning_list) == 0 assert header["LONG KEYWORD"] == 42 assert header["HIERARCH LONG KEYWORD"] == 42 # Check that it still warns if we do not use HIERARCH with pytest.warns( fits.verify.VerifyWarning, match=r"greater than 8 characters" ): header["LONG KEYWORD2"] = 1 assert header["LONG KEYWORD2"] == 1 def test_hierarch_keyword_whitespace(self): """ Regression test for https://github.com/spacetelescope/PyFITS/issues/6 Make sure any leading or trailing whitespace around HIERARCH keywords is stripped from the actual keyword value. """ c = fits.Card.fromstring("HIERARCH key.META_4 = 'calFileVersion'") assert c.keyword == "key.META_4" assert c.value == "calFileVersion" assert c.comment == "" # Test also with creation via the Card constructor c = fits.Card("HIERARCH key.META_4", "calFileVersion") assert c.keyword == "key.META_4" assert c.value == "calFileVersion" assert c.comment == "" def test_verify_mixed_case_hierarch(self): """Regression test for https://github.com/spacetelescope/PyFITS/issues/7 Assures that HIERARCH keywords with lower-case characters and other normally invalid keyword characters are not considered invalid. """ c = fits.Card("HIERARCH WeirdCard.~!@#_^$%&", "The value", "a comment") # This should not raise any exceptions c.verify("exception") assert c.keyword == "WeirdCard.~!@#_^$%&" assert c.value == "The value" assert c.comment == "a comment" # Test also the specific case from the original bug report header = fits.Header( [ ("simple", True), ("BITPIX", 8), ("NAXIS", 0), ("EXTEND", True, "May contain datasets"), ("HIERARCH key.META_0", "detRow"), ] ) hdu = fits.PrimaryHDU(header=header) hdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as hdul: header2 = hdul[0].header assert str(header.cards[header.index("key.META_0")]) == str( header2.cards[header2.index("key.META_0")] ) def test_missing_keyword(self): """Test that accessing a non-existent keyword raises a KeyError.""" header = fits.Header() # De-referencing header through the inline function should behave # identically to accessing it in the pytest.raises context below. pytest.raises(KeyError, lambda k: header[k], "NAXIS") # Test exception with message with pytest.raises(KeyError, match=r"Keyword 'NAXIS' not found."): header["NAXIS"] def test_hierarch_card_lookup(self): header = fits.Header() header["hierarch abcdefghi"] = 10 assert "abcdefghi" in header assert header["abcdefghi"] == 10 # This used to be assert_false, but per ticket # https://aeon.stsci.edu/ssb/trac/pyfits/ticket/155 hierarch keywords # should be treated case-insensitively when performing lookups assert "ABCDEFGHI" in header def test_hierarch_card_delete(self): header = fits.Header() header["hierarch abcdefghi"] = 10 del header["hierarch abcdefghi"] def test_hierarch_card_insert_delete(self): header = fits.Header() with pytest.warns( fits.verify.VerifyWarning, match=r"greater than 8 characters" ): header["abcdefghi"] = 10 header["abcdefgh"] = 10 header["abcdefg"] = 10 with pytest.warns( fits.verify.VerifyWarning, match=r"greater than 8 characters" ): header.insert(2, ("abcdefghij", 10)) del header["abcdefghij"] with pytest.warns( fits.verify.VerifyWarning, match=r"greater than 8 characters" ): header.insert(2, ("abcdefghij", 10)) del header[2] assert list(header.keys())[2] == "abcdefg".upper() def test_hierarch_create_and_update(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/158 Tests several additional use cases for working with HIERARCH cards. """ msg = "a HIERARCH card will be created" header = fits.Header() with pytest.warns(VerifyWarning) as w: header.update({"HIERARCH BLAH BLAH": "TESTA"}) assert len(w) == 0 assert "BLAH BLAH" in header assert header["BLAH BLAH"] == "TESTA" header.update({"HIERARCH BLAH BLAH": "TESTB"}) assert len(w) == 0 assert header["BLAH BLAH"], "TESTB" # Update without explicitly stating 'HIERARCH': header.update({"BLAH BLAH": "TESTC"}) assert len(w) == 1 assert len(header) == 1 assert header["BLAH BLAH"], "TESTC" # Test case-insensitivity header.update({"HIERARCH blah blah": "TESTD"}) assert len(w) == 1 assert len(header) == 1 assert header["blah blah"], "TESTD" header.update({"blah blah": "TESTE"}) assert len(w) == 2 assert len(header) == 1 assert header["blah blah"], "TESTE" # Create a HIERARCH card > 8 characters without explicitly stating # 'HIERARCH' header.update({"BLAH BLAH BLAH": "TESTA"}) assert len(w) == 3 assert msg in str(w[0].message) header.update({"HIERARCH BLAH BLAH BLAH": "TESTB"}) assert len(w) == 3 assert header["BLAH BLAH BLAH"], "TESTB" # Update without explicitly stating 'HIERARCH': header.update({"BLAH BLAH BLAH": "TESTC"}) assert len(w) == 4 assert header["BLAH BLAH BLAH"], "TESTC" # Test case-insensitivity header.update({"HIERARCH blah blah blah": "TESTD"}) assert len(w) == 4 assert header["blah blah blah"], "TESTD" header.update({"blah blah blah": "TESTE"}) assert len(w) == 5 assert header["blah blah blah"], "TESTE" def test_short_hierarch_create_and_update(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/158 Tests several additional use cases for working with HIERARCH cards, specifically where the keyword is fewer than 8 characters, but contains invalid characters such that it can only be created as a HIERARCH card. """ msg = "a HIERARCH card will be created" header = fits.Header() with pytest.warns(VerifyWarning) as w: header.update({"HIERARCH BLA BLA": "TESTA"}) assert len(w) == 0 assert "BLA BLA" in header assert header["BLA BLA"] == "TESTA" header.update({"HIERARCH BLA BLA": "TESTB"}) assert len(w) == 0 assert header["BLA BLA"], "TESTB" # Update without explicitly stating 'HIERARCH': header.update({"BLA BLA": "TESTC"}) assert len(w) == 1 assert header["BLA BLA"], "TESTC" # Test case-insensitivity header.update({"HIERARCH bla bla": "TESTD"}) assert len(w) == 1 assert len(header) == 1 assert header["bla bla"], "TESTD" header.update({"bla bla": "TESTE"}) assert len(w) == 2 assert len(header) == 1 assert header["bla bla"], "TESTE" header = fits.Header() with pytest.warns(VerifyWarning) as w: # Create a HIERARCH card containing invalid characters without # explicitly stating 'HIERARCH' header.update({"BLA BLA": "TESTA"}) print([x.category for x in w]) assert len(w) == 1 assert msg in str(w[0].message) header.update({"HIERARCH BLA BLA": "TESTB"}) assert len(w) == 1 assert header["BLA BLA"], "TESTB" # Update without explicitly stating 'HIERARCH': header.update({"BLA BLA": "TESTC"}) assert len(w) == 2 assert header["BLA BLA"], "TESTC" # Test case-insensitivity header.update({"HIERARCH bla bla": "TESTD"}) assert len(w) == 2 assert len(header) == 1 assert header["bla bla"], "TESTD" header.update({"bla bla": "TESTE"}) assert len(w) == 3 assert len(header) == 1 assert header["bla bla"], "TESTE" def test_header_setitem_invalid(self): header = fits.Header() def test(): header["FOO"] = ("bar", "baz", "qux") pytest.raises(ValueError, test) def test_header_setitem_1tuple(self): header = fits.Header() header["FOO"] = ("BAR",) header["FOO2"] = (None,) assert header["FOO"] == "BAR" assert header["FOO2"] is None assert header[0] == "BAR" assert header.comments[0] == "" assert header.comments["FOO"] == "" def test_header_setitem_2tuple(self): header = fits.Header() header["FOO"] = ("BAR", "BAZ") header["FOO2"] = (None, None) assert header["FOO"] == "BAR" assert header["FOO2"] is None assert header[0] == "BAR" assert header.comments[0] == "BAZ" assert header.comments["FOO"] == "BAZ" assert header.comments["FOO2"] == "" def test_header_set_value_to_none(self): """ Setting the value of a card to None should simply give that card an undefined value. Undefined value should map to None. """ header = fits.Header() header["FOO"] = "BAR" assert header["FOO"] == "BAR" header["FOO"] = None assert header["FOO"] is None # Create a header that contains an undefined value and a defined # value. hstr = "UNDEF = \nDEFINED = 42" header = fits.Header.fromstring(hstr, sep="\n") # Explicitly add a card with an UNDEFINED value c = fits.Card("UNDEF2", fits.card.UNDEFINED) header.extend([c]) # And now assign an undefined value to the header through setitem header["UNDEF3"] = fits.card.UNDEFINED # Tuple assignment header.append(("UNDEF5", None, "Undefined value"), end=True) header.append("UNDEF6") assert header["DEFINED"] == 42 assert header["UNDEF"] is None assert header["UNDEF2"] is None assert header["UNDEF3"] is None assert header["UNDEF5"] is None assert header["UNDEF6"] is None # Assign an undefined value to a new card header["UNDEF4"] = None # Overwrite an existing value with None header["DEFINED"] = None # All headers now should be undefined for c in header.cards: assert c.value == fits.card.UNDEFINED def test_set_comment_only(self): header = fits.Header([("A", "B", "C")]) header.set("A", comment="D") assert header["A"] == "B" assert header.comments["A"] == "D" def test_header_iter(self): header = fits.Header([("A", "B"), ("C", "D")]) assert list(header) == ["A", "C"] def test_header_slice(self): header = fits.Header([("A", "B"), ("C", "D"), ("E", "F")]) newheader = header[1:] assert len(newheader) == 2 assert "A" not in newheader assert "C" in newheader assert "E" in newheader newheader = header[::-1] assert len(newheader) == 3 assert newheader[0] == "F" assert newheader[1] == "D" assert newheader[2] == "B" newheader = header[::2] assert len(newheader) == 2 assert "A" in newheader assert "C" not in newheader assert "E" in newheader def test_header_slice_assignment(self): """ Assigning to a slice should just assign new values to the cards included in the slice. """ header = fits.Header([("A", "B"), ("C", "D"), ("E", "F")]) # Test assigning slice to the same value; this works similarly to numpy # arrays header[1:] = 1 assert header[1] == 1 assert header[2] == 1 # Though strings are iterable they should be treated as a scalar value header[1:] = "GH" assert header[1] == "GH" assert header[2] == "GH" # Now assign via an iterable header[1:] = ["H", "I"] assert header[1] == "H" assert header[2] == "I" def test_header_slice_delete(self): """Test deleting a slice of cards from the header.""" header = fits.Header([("A", "B"), ("C", "D"), ("E", "F")]) del header[1:] assert len(header) == 1 assert header[0] == "B" del header[:] assert len(header) == 0 def test_wildcard_slice(self): """Test selecting a subsection of a header via wildcard matching.""" header = fits.Header([("ABC", 0), ("DEF", 1), ("ABD", 2)]) newheader = header["AB*"] assert len(newheader) == 2 assert newheader[0] == 0 assert newheader[1] == 2 def test_wildcard_with_hyphen(self): """ Regression test for issue where wildcards did not work on keywords containing hyphens. """ header = fits.Header([("DATE", 1), ("DATE-OBS", 2), ("DATE-FOO", 3)]) assert len(header["DATE*"]) == 3 assert len(header["DATE?*"]) == 2 assert len(header["DATE-*"]) == 2 def test_wildcard_slice_assignment(self): """Test assigning to a header slice selected via wildcard matching.""" header = fits.Header([("ABC", 0), ("DEF", 1), ("ABD", 2)]) # Test assigning slice to the same value; this works similarly to numpy # arrays header["AB*"] = 1 assert header[0] == 1 assert header[2] == 1 # Though strings are iterable they should be treated as a scalar value header["AB*"] = "GH" assert header[0] == "GH" assert header[2] == "GH" # Now assign via an iterable header["AB*"] = ["H", "I"] assert header[0] == "H" assert header[2] == "I" def test_wildcard_slice_deletion(self): """Test deleting cards from a header that match a wildcard pattern.""" header = fits.Header([("ABC", 0), ("DEF", 1), ("ABD", 2)]) del header["AB*"] assert len(header) == 1 assert header[0] == 1 def test_header_history(self): header = fits.Header( [ ("ABC", 0), ("HISTORY", 1), ("HISTORY", 2), ("DEF", 3), ("HISTORY", 4), ("HISTORY", 5), ] ) assert header["HISTORY"] == [1, 2, 4, 5] def test_header_clear(self): header = fits.Header([("A", "B"), ("C", "D")]) header.clear() assert "A" not in header assert "C" not in header assert len(header) == 0 @pytest.mark.parametrize("fitsext", [fits.ImageHDU(), fits.CompImageHDU()]) def test_header_clear_write(self, fitsext): hdulist = fits.HDUList([fits.PrimaryHDU(), fitsext]) hdulist[1].header["FOO"] = "BAR" hdulist[1].header.clear() with pytest.raises(VerifyError) as err: hdulist.writeto(self.temp("temp.fits"), overwrite=True) err_msg = "'XTENSION' card does not exist." assert err_msg in str(err.value) def test_header_fromkeys(self): header = fits.Header.fromkeys(["A", "B"]) assert "A" in header assert header["A"] is None assert header.comments["A"] == "" assert "B" in header assert header["B"] is None assert header.comments["B"] == "" def test_header_fromkeys_with_value(self): header = fits.Header.fromkeys(["A", "B"], "C") assert "A" in header assert header["A"] == "C" assert header.comments["A"] == "" assert "B" in header assert header["B"] == "C" assert header.comments["B"] == "" def test_header_fromkeys_with_value_and_comment(self): header = fits.Header.fromkeys(["A"], ("B", "C")) assert "A" in header assert header["A"] == "B" assert header.comments["A"] == "C" def test_header_fromkeys_with_duplicates(self): header = fits.Header.fromkeys(["A", "B", "A"], "C") assert "A" in header assert ("A", 0) in header assert ("A", 1) in header assert ("A", 2) not in header assert header[0] == "C" assert header["A"] == "C" assert header[("A", 0)] == "C" assert header[2] == "C" assert header[("A", 1)] == "C" def test_header_items(self): header = fits.Header([("A", "B"), ("C", "D")]) assert list(header.items()) == [("A", "B"), ("C", "D")] def test_header_iterkeys(self): header = fits.Header([("A", "B"), ("C", "D")]) for a, b in zip(header.keys(), header): assert a == b def test_header_itervalues(self): header = fits.Header([("A", "B"), ("C", "D")]) for a, b in zip(header.values(), ["B", "D"]): assert a == b def test_header_keys(self): with fits.open(self.data("arange.fits")) as hdul: assert list(hdul[0].header) == [ "SIMPLE", "BITPIX", "NAXIS", "NAXIS1", "NAXIS2", "NAXIS3", "EXTEND", ] def test_header_list_like_pop(self): header = fits.Header([("A", "B"), ("C", "D"), ("E", "F"), ("G", "H")]) last = header.pop() assert last == "H" assert len(header) == 3 assert list(header) == ["A", "C", "E"] mid = header.pop(1) assert mid == "D" assert len(header) == 2 assert list(header) == ["A", "E"] first = header.pop(0) assert first == "B" assert len(header) == 1 assert list(header) == ["E"] pytest.raises(IndexError, header.pop, 42) def test_header_dict_like_pop(self): header = fits.Header([("A", "B"), ("C", "D"), ("E", "F"), ("G", "H")]) pytest.raises(TypeError, header.pop, "A", "B", "C") last = header.pop("G") assert last == "H" assert len(header) == 3 assert list(header) == ["A", "C", "E"] mid = header.pop("C") assert mid == "D" assert len(header) == 2 assert list(header) == ["A", "E"] first = header.pop("A") assert first == "B" assert len(header) == 1 assert list(header) == ["E"] default = header.pop("X", "Y") assert default == "Y" assert len(header) == 1 pytest.raises(KeyError, header.pop, "X") def test_popitem(self): header = fits.Header([("A", "B"), ("C", "D"), ("E", "F")]) keyword, value = header.popitem() assert keyword not in header assert len(header) == 2 keyword, value = header.popitem() assert keyword not in header assert len(header) == 1 keyword, value = header.popitem() assert keyword not in header assert len(header) == 0 pytest.raises(KeyError, header.popitem) def test_setdefault(self): header = fits.Header([("A", "B"), ("C", "D"), ("E", "F")]) assert header.setdefault("A") == "B" assert header.setdefault("C") == "D" assert header.setdefault("E") == "F" assert len(header) == 3 assert header.setdefault("G", "H") == "H" assert len(header) == 4 assert "G" in header assert header.setdefault("G", "H") == "H" assert len(header) == 4 def test_update_from_dict(self): """ Test adding new cards and updating existing cards from a dict using Header.update() """ header = fits.Header([("A", "B"), ("C", "D")]) header.update({"A": "E", "F": "G"}) assert header["A"] == "E" assert header[0] == "E" assert "F" in header assert header["F"] == "G" assert header[-1] == "G" # Same as above but this time pass the update dict as keyword arguments header = fits.Header([("A", "B"), ("C", "D")]) header.update(A="E", F="G") assert header["A"] == "E" assert header[0] == "E" assert "F" in header assert header["F"] == "G" assert header[-1] == "G" def test_update_from_iterable(self): """ Test adding new cards and updating existing cards from an iterable of cards and card tuples. """ header = fits.Header([("A", "B"), ("C", "D")]) header.update([("A", "E"), fits.Card("F", "G")]) assert header["A"] == "E" assert header[0] == "E" assert "F" in header assert header["F"] == "G" assert header[-1] == "G" def test_header_extend(self): """ Test extending a header both with and without stripping cards from the extension header. """ hdu = fits.PrimaryHDU() hdu2 = fits.ImageHDU() hdu2.header["MYKEY"] = ("some val", "some comment") hdu.header += hdu2.header assert len(hdu.header) == 5 assert hdu.header[-1] == "some val" # Same thing, but using + instead of += hdu = fits.PrimaryHDU() hdu.header = hdu.header + hdu2.header assert len(hdu.header) == 5 assert hdu.header[-1] == "some val" # Directly append the other header in full--not usually a desirable # operation when the header is coming from another HDU hdu.header.extend(hdu2.header, strip=False) assert len(hdu.header) == 11 assert list(hdu.header)[5] == "XTENSION" assert hdu.header[-1] == "some val" assert ("MYKEY", 1) in hdu.header def test_header_extend_unique(self): """ Test extending the header with and without unique=True. """ hdu = fits.PrimaryHDU() hdu2 = fits.ImageHDU() hdu.header["MYKEY"] = ("some val", "some comment") hdu2.header["MYKEY"] = ("some other val", "some other comment") hdu.header.extend(hdu2.header) assert len(hdu.header) == 6 assert hdu.header[-2] == "some val" assert hdu.header[-1] == "some other val" hdu = fits.PrimaryHDU() hdu2 = fits.ImageHDU() hdu.header["MYKEY"] = ("some val", "some comment") hdu2.header["MYKEY"] = ("some other val", "some other comment") hdu.header.extend(hdu2.header, unique=True) assert len(hdu.header) == 5 assert hdu.header[-1] == "some val" def test_header_extend_unique_commentary(self): """ Test extending header with and without unique=True and commentary cards in the header being added. Issue astropy/astropy#3967 """ for commentary_card in ["", "COMMENT", "HISTORY"]: for is_unique in [True, False]: hdu = fits.PrimaryHDU() # Make sure we are testing the case we want. assert commentary_card not in hdu.header hdu2 = fits.ImageHDU() hdu2.header[commentary_card] = "My text" hdu.header.extend(hdu2.header, unique=is_unique) assert len(hdu.header) == 5 assert hdu.header[commentary_card][0] == "My text" def test_header_extend_update(self): """ Test extending the header with and without update=True. """ hdu = fits.PrimaryHDU() hdu2 = fits.ImageHDU() hdu.header["MYKEY"] = ("some val", "some comment") hdu.header["HISTORY"] = "history 1" hdu2.header["MYKEY"] = ("some other val", "some other comment") hdu2.header["HISTORY"] = "history 1" hdu2.header["HISTORY"] = "history 2" hdu.header.extend(hdu2.header) assert len(hdu.header) == 9 assert ("MYKEY", 0) in hdu.header assert ("MYKEY", 1) in hdu.header assert hdu.header[("MYKEY", 1)] == "some other val" assert len(hdu.header["HISTORY"]) == 3 assert hdu.header[-1] == "history 2" hdu = fits.PrimaryHDU() hdu.header["MYKEY"] = ("some val", "some comment") hdu.header["HISTORY"] = "history 1" hdu.header.extend(hdu2.header, update=True) assert len(hdu.header) == 7 assert ("MYKEY", 0) in hdu.header assert ("MYKEY", 1) not in hdu.header assert hdu.header["MYKEY"] == "some other val" assert len(hdu.header["HISTORY"]) == 2 assert hdu.header[-1] == "history 2" def test_header_extend_update_commentary(self): """ Test extending header with and without unique=True and commentary cards in the header being added. Though not quite the same as astropy/astropy#3967, update=True hits the same if statement as that issue. """ for commentary_card in ["", "COMMENT", "HISTORY"]: for is_update in [True, False]: hdu = fits.PrimaryHDU() # Make sure we are testing the case we want. assert commentary_card not in hdu.header hdu2 = fits.ImageHDU() hdu2.header[commentary_card] = "My text" hdu.header.extend(hdu2.header, update=is_update) assert len(hdu.header) == 5 assert hdu.header[commentary_card][0] == "My text" def test_header_extend_exact(self): """ Test that extending an empty header with the contents of an existing header can exactly duplicate that header, given strip=False and end=True. """ header = fits.getheader(self.data("test0.fits")) header2 = fits.Header() header2.extend(header, strip=False, end=True) assert header == header2 def test_header_count(self): header = fits.Header([("A", "B"), ("C", "D"), ("E", "F")]) assert header.count("A") == 1 assert header.count("C") == 1 assert header.count("E") == 1 header["HISTORY"] = "a" header["HISTORY"] = "b" assert header.count("HISTORY") == 2 pytest.raises(KeyError, header.count, "G") def test_header_append_use_blanks(self): """ Tests that blank cards can be appended, and that future appends will use blank cards when available (unless useblanks=False) """ header = fits.Header([("A", "B"), ("C", "D")]) # Append a couple blanks header.append() header.append() assert len(header) == 4 assert header[-1] == "" assert header[-2] == "" # New card should fill the first blank by default header.append(("E", "F")) assert len(header) == 4 assert header[-2] == "F" assert header[-1] == "" # This card should not use up a blank spot header.append(("G", "H"), useblanks=False) assert len(header) == 5 assert header[-1] == "" assert header[-2] == "H" def test_header_append_keyword_only(self): """ Test appending a new card with just the keyword, and no value or comment given. """ header = fits.Header([("A", "B"), ("C", "D")]) header.append("E") assert len(header) == 3 assert list(header)[-1] == "E" assert header[-1] is None assert header.comments["E"] == "" # Try appending a blank--normally this can be accomplished with just # header.append(), but header.append('') should also work (and is maybe # a little more clear) header.append("") assert len(header) == 4 assert list(header)[-1] == "" assert header[""] == "" assert header.comments[""] == "" def test_header_insert_use_blanks(self): header = fits.Header([("A", "B"), ("C", "D")]) # Append a couple blanks header.append() header.append() # Insert a new card; should use up one of the blanks header.insert(1, ("E", "F")) assert len(header) == 4 assert header[1] == "F" assert header[-1] == "" assert header[-2] == "D" # Insert a new card without using blanks header.insert(1, ("G", "H"), useblanks=False) assert len(header) == 5 assert header[1] == "H" assert header[-1] == "" def test_header_insert_before_keyword(self): """ Test that a keyword name or tuple can be used to insert new keywords. Also tests the ``after`` keyword argument. Regression test for https://github.com/spacetelescope/PyFITS/issues/12 """ header = fits.Header( [("NAXIS1", 10), ("COMMENT", "Comment 1"), ("COMMENT", "Comment 3")] ) header.insert("NAXIS1", ("NAXIS", 2, "Number of axes")) assert list(header.keys())[0] == "NAXIS" assert header[0] == 2 assert header.comments[0] == "Number of axes" header.insert("NAXIS1", ("NAXIS2", 20), after=True) assert list(header.keys())[1] == "NAXIS1" assert list(header.keys())[2] == "NAXIS2" assert header[2] == 20 header.insert(("COMMENT", 1), ("COMMENT", "Comment 2")) assert header["COMMENT"] == ["Comment 1", "Comment 2", "Comment 3"] header.insert(("COMMENT", 2), ("COMMENT", "Comment 4"), after=True) assert header["COMMENT"] == ["Comment 1", "Comment 2", "Comment 3", "Comment 4"] header.insert(-1, ("TEST1", True)) assert list(header.keys())[-2] == "TEST1" header.insert(-1, ("TEST2", True), after=True) assert list(header.keys())[-1] == "TEST2" assert list(header.keys())[-3] == "TEST1" def test_remove(self): header = fits.Header([("A", "B"), ("C", "D")]) # When keyword is present in the header it should be removed. header.remove("C") assert len(header) == 1 assert list(header) == ["A"] assert "C" not in header # When keyword is not present in the header and ignore_missing is # False, KeyError should be raised with pytest.raises(KeyError): header.remove("F") # When keyword is not present and ignore_missing is True, KeyError # will be ignored header.remove("F", ignore_missing=True) assert len(header) == 1 # Test for removing all instances of a keyword header = fits.Header([("A", "B"), ("C", "D"), ("A", "F")]) header.remove("A", remove_all=True) assert "A" not in header assert len(header) == 1 assert list(header) == ["C"] assert header[0] == "D" def test_header_comments(self): header = fits.Header([("A", "B", "C"), ("DEF", "G", "H")]) assert repr(header.comments) == " A C\n DEF H" def test_comment_slices_and_filters(self): header = fits.Header([("AB", "C", "D"), ("EF", "G", "H"), ("AI", "J", "K")]) s = header.comments[1:] assert list(s) == ["H", "K"] s = header.comments[::-1] assert list(s) == ["K", "H", "D"] s = header.comments["A*"] assert list(s) == ["D", "K"] def test_comment_slice_filter_assign(self): header = fits.Header([("AB", "C", "D"), ("EF", "G", "H"), ("AI", "J", "K")]) header.comments[1:] = "L" assert list(header.comments) == ["D", "L", "L"] assert header.cards[header.index("AB")].comment == "D" assert header.cards[header.index("EF")].comment == "L" assert header.cards[header.index("AI")].comment == "L" header.comments[::-1] = header.comments[:] assert list(header.comments) == ["L", "L", "D"] header.comments["A*"] = ["M", "N"] assert list(header.comments) == ["M", "L", "N"] def test_commentary_slicing(self): header = fits.Header() indices = list(range(5)) for idx in indices: header["HISTORY"] = idx # Just a few sample slice types; this won't get all corner cases but if # these all work we should be in good shape assert header["HISTORY"][1:] == indices[1:] assert header["HISTORY"][:3] == indices[:3] assert header["HISTORY"][:6] == indices[:6] assert header["HISTORY"][:-2] == indices[:-2] assert header["HISTORY"][::-1] == indices[::-1] assert header["HISTORY"][1::-1] == indices[1::-1] assert header["HISTORY"][1:5:2] == indices[1:5:2] # Same tests, but copy the values first; as it turns out this is # different from just directly doing an __eq__ as in the first set of # assertions header.insert(0, ("A", "B", "C")) header.append(("D", "E", "F"), end=True) assert list(header["HISTORY"][1:]) == indices[1:] assert list(header["HISTORY"][:3]) == indices[:3] assert list(header["HISTORY"][:6]) == indices[:6] assert list(header["HISTORY"][:-2]) == indices[:-2] assert list(header["HISTORY"][::-1]) == indices[::-1] assert list(header["HISTORY"][1::-1]) == indices[1::-1] assert list(header["HISTORY"][1:5:2]) == indices[1:5:2] def test_update_commentary(self): header = fits.Header() header["FOO"] = "BAR" header["HISTORY"] = "ABC" header["FRED"] = "BARNEY" header["HISTORY"] = "DEF" header["HISTORY"] = "GHI" assert header["HISTORY"] == ["ABC", "DEF", "GHI"] # Single value update header["HISTORY"][0] = "FOO" assert header["HISTORY"] == ["FOO", "DEF", "GHI"] # Single value partial slice update header["HISTORY"][1:] = "BAR" assert header["HISTORY"] == ["FOO", "BAR", "BAR"] # Multi-value update header["HISTORY"][:] = ["BAZ", "QUX"] assert header["HISTORY"] == ["BAZ", "QUX", "BAR"] def test_commentary_comparison(self): """ Regression test for an issue found in *writing* the regression test for https://github.com/astropy/astropy/issues/2363, where comparison of the list of values for a commentary keyword did not always compare correctly with other iterables. """ header = fits.Header() header["HISTORY"] = "hello world" header["HISTORY"] = "hello world" header["COMMENT"] = "hello world" assert header["HISTORY"] != header["COMMENT"] header["COMMENT"] = "hello world" assert header["HISTORY"] == header["COMMENT"] def test_long_commentary_card(self): header = fits.Header() header["FOO"] = "BAR" header["BAZ"] = "QUX" longval = "ABC" * 30 header["HISTORY"] = longval header["FRED"] = "BARNEY" header["HISTORY"] = longval assert len(header) == 7 assert list(header)[2] == "FRED" assert str(header.cards[3]) == "HISTORY " + longval[:72] assert str(header.cards[4]).rstrip() == "HISTORY " + longval[72:] header.set("HISTORY", longval, after="FOO") assert len(header) == 9 assert str(header.cards[1]) == "HISTORY " + longval[:72] assert str(header.cards[2]).rstrip() == "HISTORY " + longval[72:] header = fits.Header() header.update({"FOO": "BAR"}) header.update({"BAZ": "QUX"}) longval = "ABC" * 30 header.add_history(longval) header.update({"FRED": "BARNEY"}) header.add_history(longval) assert len(header.cards) == 7 assert header.cards[2].keyword == "FRED" assert str(header.cards[3]) == "HISTORY " + longval[:72] assert str(header.cards[4]).rstrip() == "HISTORY " + longval[72:] header.add_history(longval, after="FOO") assert len(header.cards) == 9 assert str(header.cards[1]) == "HISTORY " + longval[:72] assert str(header.cards[2]).rstrip() == "HISTORY " + longval[72:] def test_totxtfile(self, home_is_temp): header_filename = self.temp("header.txt") with fits.open(self.data("test0.fits")) as hdul: hdul[0].header.totextfile(header_filename) # Check the `overwrite` flag with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): hdul[0].header.totextfile(header_filename, overwrite=False) hdul[0].header.totextfile(header_filename, overwrite=True) hdu = fits.ImageHDU() hdu.header.update({"MYKEY": "FOO"}) hdu.header.extend( hdu.header.fromtextfile(header_filename), update=True, update_first=True ) # Write the hdu out and read it back in again--it should be recognized # as a PrimaryHDU hdu.writeto(self.temp("test.fits"), output_verify="ignore") with fits.open(self.temp("test.fits")) as hdul: assert isinstance(hdul[0], fits.PrimaryHDU) hdu = fits.ImageHDU() hdu.header.update({"MYKEY": "FOO"}) hdu.header.extend( hdu.header.fromtextfile(header_filename), update=True, update_first=True, strip=False, ) assert "MYKEY" in hdu.header assert "EXTENSION" not in hdu.header assert "SIMPLE" in hdu.header hdu.writeto(self.temp("test.fits"), output_verify="ignore", overwrite=True) with fits.open(self.temp("test.fits")) as hdul2: assert len(hdul2) == 2 assert "MYKEY" in hdul2[1].header def test_tofile(self, home_is_temp): """ Repeat test_totxtfile, but with tofile() """ header_filename = self.temp("header.fits") with fits.open(self.data("test0.fits")) as hdul: hdul[0].header.tofile(header_filename) # Check the `overwrite` flag with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): hdul[0].header.tofile(header_filename, overwrite=False) hdul[0].header.tofile(header_filename, overwrite=True) hdu = fits.ImageHDU() hdu.header.update({"MYKEY": "FOO"}) hdu.header.extend( hdu.header.fromfile(header_filename), update=True, update_first=True ) # Write the hdu out and read it back in again--it should be recognized # as a PrimaryHDU hdu.writeto(self.temp("test.fits"), output_verify="ignore") with fits.open(self.temp("test.fits")) as hdul: assert isinstance(hdul[0], fits.PrimaryHDU) hdu = fits.ImageHDU() hdu.header.update({"MYKEY": "FOO"}) hdu.header.extend( hdu.header.fromfile(header_filename), update=True, update_first=True, strip=False, ) assert "MYKEY" in hdu.header assert "EXTENSION" not in hdu.header assert "SIMPLE" in hdu.header hdu.writeto(self.temp("test.fits"), output_verify="ignore", overwrite=True) with fits.open(self.temp("test.fits")) as hdul2: assert len(hdul2) == 2 assert "MYKEY" in hdul2[1].header def test_fromfile(self): """Regression test for https://github.com/astropy/astropy/issues/8711""" filename = self.data("scale.fits") hdr = fits.Header.fromfile(filename) assert hdr["DATASET"] == "2MASS" def test_header_fromtextfile(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/122 Manually write a text file containing some header cards ending with newlines and ensure that fromtextfile can read them back in. """ header = fits.Header() header["A"] = ("B", "C") header["B"] = ("C", "D") header["C"] = ("D", "E") with open(self.temp("test.hdr"), "w") as f: f.write("\n".join(str(c).strip() for c in header.cards)) header2 = fits.Header.fromtextfile(self.temp("test.hdr")) assert header == header2 def test_header_fromtextfile_with_end_card(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/154 Make sure that when a Header is read from a text file that the END card is ignored. """ header = fits.Header([("A", "B", "C"), ("D", "E", "F")]) # We don't use header.totextfile here because it writes each card with # trailing spaces to pad them out to 80 characters. But this bug only # presents itself when each card ends immediately with a newline, and # no trailing spaces with open(self.temp("test.hdr"), "w") as f: f.write("\n".join(str(c).strip() for c in header.cards)) f.write("\nEND") new_header = fits.Header.fromtextfile(self.temp("test.hdr")) assert "END" not in new_header assert header == new_header def test_append_end_card(self): """ Regression test 2 for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/154 Manually adding an END card to a header should simply result in a ValueError (as was the case in PyFITS 3.0 and earlier). """ header = fits.Header([("A", "B", "C"), ("D", "E", "F")]) def setitem(k, v): header[k] = v pytest.raises(ValueError, setitem, "END", "") pytest.raises(ValueError, header.append, "END") pytest.raises(ValueError, header.append, "END", end=True) pytest.raises(ValueError, header.insert, len(header), "END") pytest.raises(ValueError, header.set, "END") def test_invalid_end_cards(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/217 This tests the case where the END card looks like a normal card like 'END = ' and other similar oddities. As long as a card starts with END and looks like it was intended to be the END card we allow it, but with a warning. """ horig = fits.PrimaryHDU(data=np.arange(100)).header def invalid_header(end, pad): # Build up a goofy invalid header # Start from a seemingly normal header s = horig.tostring(sep="", endcard=False, padding=False) # append the bogus end card s += end # add additional padding if requested if pad: s += " " * _pad_length(len(s)) # This will differ between Python versions if isinstance(s, bytes): return BytesIO(s) else: return StringIO(s) # Basic case motivated by the original issue; it's as if the END card # was appended by software that doesn't know to treat it specially, and # it is given an = after it s = invalid_header("END =", True) with pytest.warns( AstropyUserWarning, match="Unexpected bytes trailing END keyword: ' ='" ) as w: h = fits.Header.fromfile(s) assert h == horig assert len(w) == 1 # A case similar to the last but with more spaces between END and the # =, as though the '= ' value indicator were placed like that of a # normal card s = invalid_header("END = ", True) with pytest.warns( AstropyUserWarning, match="Unexpected bytes trailing END keyword: ' ='" ) as w: h = fits.Header.fromfile(s) assert h == horig assert len(w) == 1 # END card with trailing gibberish s = invalid_header("END$%&%^*%*", True) with pytest.warns( AstropyUserWarning, match=r"Unexpected bytes trailing " r"END keyword: '\$%&%\^\*%\*'", ) as w: h = fits.Header.fromfile(s) assert h == horig assert len(w) == 1 # 'END' at the very end of a truncated file without padding; the way # the block reader works currently this can only happen if the 'END' # is at the very end of the file. s = invalid_header("END", False) with pytest.warns( AstropyUserWarning, match="Missing padding to end of the FITS block" ) as w: # Don't raise an exception on missing padding, but still produce a # warning that the END card is incomplete h = fits.Header.fromfile(s, padding=False) assert h == horig assert len(w) == 1 def test_invalid_characters(self): """ Test header with invalid characters """ # Generate invalid file with non-ASCII character h = fits.Header() h["FOO"] = "BAR" h["COMMENT"] = "hello" hdul = fits.PrimaryHDU(header=h, data=np.arange(5)) hdul.writeto(self.temp("test.fits")) with open(self.temp("test.fits"), "rb") as f: out = f.read() out = out.replace(b"hello", "héllo".encode("latin1")) out = out.replace(b"BAR", "BÀR".encode("latin1")) with open(self.temp("test2.fits"), "wb") as f2: f2.write(out) with pytest.warns( AstropyUserWarning, match="non-ASCII characters are present in the FITS file", ) as w: h = fits.getheader(self.temp("test2.fits")) assert h["FOO"] == "B?R" assert h["COMMENT"] == "h?llo" assert len(w) == 1 def test_unnecessary_move(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/125 Ensures that a header is not modified when setting the position of a keyword that's already in its correct position. """ header = fits.Header([("A", "B"), ("B", "C"), ("C", "D")]) header.set("B", before=2) assert list(header) == ["A", "B", "C"] assert not header._modified header.set("B", after=0) assert list(header) == ["A", "B", "C"] assert not header._modified header.set("B", before="C") assert list(header) == ["A", "B", "C"] assert not header._modified header.set("B", after="A") assert list(header) == ["A", "B", "C"] assert not header._modified header.set("B", before=2) assert list(header) == ["A", "B", "C"] assert not header._modified # 123 is well past the end, and C is already at the end, so it's in the # right place already header.set("C", before=123) assert list(header) == ["A", "B", "C"] assert not header._modified header.set("C", after=123) assert list(header) == ["A", "B", "C"] assert not header._modified def test_invalid_float_cards(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/137""" # Create a header containing two of the problematic cards in the test # case where this came up: hstr = "FOCALLEN= +1.550000000000e+002\nAPERTURE= +0.000000000000e+000" h = fits.Header.fromstring(hstr, sep="\n") # First the case that *does* work prior to fixing this issue assert h["FOCALLEN"] == 155.0 assert h["APERTURE"] == 0.0 # Now if this were reserialized, would new values for these cards be # written with repaired exponent signs? with pytest.warns( fits.verify.VerifyWarning, match=r"Verification reported errors" ): assert str(h.cards["FOCALLEN"]) == _pad("FOCALLEN= +1.550000000000E+002") assert h.cards["FOCALLEN"]._modified with pytest.warns( fits.verify.VerifyWarning, match=r"Verification reported errors" ): assert str(h.cards["APERTURE"]) == _pad("APERTURE= +0.000000000000E+000") assert h.cards["APERTURE"]._modified assert h._modified # This is the case that was specifically causing problems; generating # the card strings *before* parsing the values. Also, the card strings # really should be "fixed" before being returned to the user h = fits.Header.fromstring(hstr, sep="\n") with pytest.warns( fits.verify.VerifyWarning, match=r"Verification reported errors" ): assert str(h.cards["FOCALLEN"]) == _pad("FOCALLEN= +1.550000000000E+002") assert h.cards["FOCALLEN"]._modified with pytest.warns( fits.verify.VerifyWarning, match=r"Verification reported errors" ): assert str(h.cards["APERTURE"]) == _pad("APERTURE= +0.000000000000E+000") assert h.cards["APERTURE"]._modified assert h["FOCALLEN"] == 155.0 assert h["APERTURE"] == 0.0 assert h._modified # For the heck of it, try assigning the identical values and ensure # that the newly fixed value strings are left intact h["FOCALLEN"] = 155.0 h["APERTURE"] = 0.0 assert str(h.cards["FOCALLEN"]) == _pad("FOCALLEN= +1.550000000000E+002") assert str(h.cards["APERTURE"]) == _pad("APERTURE= +0.000000000000E+000") def test_invalid_float_cards2(self, capsys): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/140 """ # The example for this test requires creating a FITS file containing a # slightly misformatted float value. I can't actually even find a way # to do that directly through Astropy--it won't let me. hdu = fits.PrimaryHDU() hdu.header["TEST"] = 5.0022221e-07 hdu.writeto(self.temp("test.fits")) # Here we manually make the file invalid with open(self.temp("test.fits"), "rb+") as f: f.seek(346) # Location of the exponent 'E' symbol f.write(encode_ascii("e")) with fits.open(self.temp("test.fits")) as hdul, pytest.warns( AstropyUserWarning ) as w: hdul.writeto(self.temp("temp.fits"), output_verify="warn") assert len(w) == 5 # The first two warnings are just the headers to the actual warning # message (HDU 0, Card 4). I'm still not sure things like that # should be output as separate warning messages, but that's # something to think about... msg = str(w[3].message) assert "(invalid value string: '5.0022221e-07')" in msg def test_leading_zeros(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/137, part 2 Ticket https://aeon.stsci.edu/ssb/trac/pyfits/ticket/137 also showed that in float values like 0.001 the leading zero was unnecessarily being stripped off when rewriting the header. Though leading zeros should be removed from integer values to prevent misinterpretation as octal by python (for now Astropy will still maintain the leading zeros if now changes are made to the value, but will drop them if changes are made). """ c = fits.Card.fromstring("APERTURE= +0.000000000000E+000") assert str(c) == _pad("APERTURE= +0.000000000000E+000") assert c.value == 0.0 c = fits.Card.fromstring("APERTURE= 0.000000000000E+000") assert str(c) == _pad("APERTURE= 0.000000000000E+000") assert c.value == 0.0 c = fits.Card.fromstring("APERTURE= 017") assert str(c) == _pad("APERTURE= 017") assert c.value == 17 def test_assign_boolean(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/123 Tests assigning Python and Numpy boolean values to keyword values. """ fooimg = _pad("FOO = T") barimg = _pad("BAR = F") h = fits.Header() h["FOO"] = True h["BAR"] = False assert h["FOO"] is True assert h["BAR"] is False assert str(h.cards["FOO"]) == fooimg assert str(h.cards["BAR"]) == barimg h = fits.Header() h["FOO"] = np.bool_(True) h["BAR"] = np.bool_(False) assert h["FOO"] is True assert h["BAR"] is False assert str(h.cards["FOO"]) == fooimg assert str(h.cards["BAR"]) == barimg h = fits.Header() h.append(fits.Card.fromstring(fooimg)) h.append(fits.Card.fromstring(barimg)) assert h["FOO"] is True assert h["BAR"] is False assert str(h.cards["FOO"]) == fooimg assert str(h.cards["BAR"]) == barimg def test_header_method_keyword_normalization(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/149 Basically ensures that all public Header methods are case-insensitive w.r.t. keywords. Provides a reasonably comprehensive test of several methods at once. """ h = fits.Header([("abC", 1), ("Def", 2), ("GeH", 3)]) assert list(h) == ["ABC", "DEF", "GEH"] assert "abc" in h assert "dEf" in h assert h["geh"] == 3 # Case insensitivity of wildcards assert len(h["g*"]) == 1 h["aBc"] = 2 assert h["abc"] == 2 # ABC already existed so assigning to aBc should not have added any new # cards assert len(h) == 3 del h["gEh"] assert list(h) == ["ABC", "DEF"] assert len(h) == 2 assert h.get("def") == 2 h.set("Abc", 3) assert h["ABC"] == 3 h.set("gEh", 3, before="Abc") assert list(h) == ["GEH", "ABC", "DEF"] assert h.pop("abC") == 3 assert len(h) == 2 assert h.setdefault("def", 3) == 2 assert len(h) == 2 assert h.setdefault("aBc", 1) == 1 assert len(h) == 3 assert list(h) == ["GEH", "DEF", "ABC"] h.update({"GeH": 1, "iJk": 4}) assert len(h) == 4 assert list(h) == ["GEH", "DEF", "ABC", "IJK"] assert h["GEH"] == 1 assert h.count("ijk") == 1 assert h.index("ijk") == 3 h.remove("Def") assert len(h) == 3 assert list(h) == ["GEH", "ABC", "IJK"] def test_end_in_comment(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/142 Tests a case where the comment of a card ends with END, and is followed by several blank cards. """ data = np.arange(100).reshape(10, 10) hdu = fits.PrimaryHDU(data=data) hdu.header["TESTKW"] = ("Test val", "This is the END") # Add a couple blanks after the END string hdu.header.append() hdu.header.append() hdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits"), memmap=False) as hdul: # memmap = False to avoid leaving open a mmap to the file when we # access the data--this causes problems on Windows when we try to # overwrite the file later assert "TESTKW" in hdul[0].header assert hdul[0].header == hdu.header assert (hdul[0].data == data).all() # Add blanks until the header is extended to two block sizes while len(hdu.header) < 36: hdu.header.append() hdu.writeto(self.temp("test.fits"), overwrite=True) with fits.open(self.temp("test.fits")) as hdul: assert "TESTKW" in hdul[0].header assert hdul[0].header == hdu.header assert (hdul[0].data == data).all() # Test parsing the same header when it's written to a text file hdu.header.totextfile(self.temp("test.hdr")) header2 = fits.Header.fromtextfile(self.temp("test.hdr")) assert hdu.header == header2 def test_assign_unicode(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/134 Assigning a unicode literal as a header value should not fail silently. If the value can be converted to ASCII then it should just work. Otherwise it should fail with an appropriate value error. Also tests unicode for keywords and comments. """ erikku = "\u30a8\u30ea\u30c3\u30af" def assign(keyword, val): h[keyword] = val h = fits.Header() h["FOO"] = "BAR" assert "FOO" in h assert h["FOO"] == "BAR" assert repr(h) == _pad("FOO = 'BAR '") pytest.raises(ValueError, assign, erikku, "BAR") h["FOO"] = "BAZ" assert h["FOO"] == "BAZ" assert repr(h) == _pad("FOO = 'BAZ '") pytest.raises(ValueError, assign, "FOO", erikku) h["FOO"] = ("BAR", "BAZ") assert h["FOO"] == "BAR" assert h.comments["FOO"] == "BAZ" assert repr(h) == _pad("FOO = 'BAR ' / BAZ") pytest.raises(ValueError, assign, "FOO", ("BAR", erikku)) pytest.raises(ValueError, assign, "FOO", (erikku, "BAZ")) pytest.raises(ValueError, assign, "FOO", (erikku, erikku)) def test_assign_non_ascii(self): """ First regression test for https://github.com/spacetelescope/PyFITS/issues/37 Although test_assign_unicode ensures that `str` objects containing non-ASCII characters cannot be assigned to headers. It should not be possible to assign bytes to a header at all. """ h = fits.Header() with pytest.raises(ValueError, match="Illegal value: b'Hello'."): h.set("TEST", b"Hello") def test_header_strip_whitespace(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/146, and for the solution that is optional stripping of whitespace from the end of a header value. By default extra whitespace is stripped off, but if `fits.conf.strip_header_whitespace` = False it should not be stripped. """ h = fits.Header() h["FOO"] = "Bar " assert h["FOO"] == "Bar" c = fits.Card.fromstring("QUX = 'Bar '") h.append(c) assert h["QUX"] == "Bar" assert h.cards["FOO"].image.rstrip() == "FOO = 'Bar '" assert h.cards["QUX"].image.rstrip() == "QUX = 'Bar '" with fits.conf.set_temp("strip_header_whitespace", False): assert h["FOO"] == "Bar " assert h["QUX"] == "Bar " assert h.cards["FOO"].image.rstrip() == "FOO = 'Bar '" assert h.cards["QUX"].image.rstrip() == "QUX = 'Bar '" assert h["FOO"] == "Bar" assert h["QUX"] == "Bar" assert h.cards["FOO"].image.rstrip() == "FOO = 'Bar '" assert h.cards["QUX"].image.rstrip() == "QUX = 'Bar '" def test_keep_duplicate_history_in_orig_header(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/156 When creating a new HDU from an existing Header read from an existing FITS file, if the original header contains duplicate HISTORY values those duplicates should be preserved just as in the original header. This bug occurred due to naivete in Header.extend. """ history = [ "CCD parameters table ...", " reference table oref$n951041ko_ccd.fits", " INFLIGHT 12/07/2001 25/02/2002", " all bias frames", ] * 3 hdu = fits.PrimaryHDU() # Add the history entries twice for item in history: hdu.header["HISTORY"] = item hdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as hdul: assert hdul[0].header["HISTORY"] == history new_hdu = fits.PrimaryHDU(header=hdu.header) assert new_hdu.header["HISTORY"] == hdu.header["HISTORY"] new_hdu.writeto(self.temp("test2.fits")) with fits.open(self.temp("test2.fits")) as hdul: assert hdul[0].header["HISTORY"] == history def test_invalid_keyword_cards(self): """ Test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/109 Allow opening files with headers containing invalid keywords. """ # Create a header containing a few different types of BAD headers. c1 = fits.Card.fromstring("CLFIND2D: contour = 0.30") c2 = fits.Card.fromstring("Just some random text.") c3 = fits.Card.fromstring("A" * 80) hdu = fits.PrimaryHDU() # This should work with some warnings with pytest.warns(AstropyUserWarning) as w: hdu.header.append(c1) hdu.header.append(c2) hdu.header.append(c3) assert len(w) == 3 hdu.writeto(self.temp("test.fits")) with pytest.warns(AstropyUserWarning) as w: with fits.open(self.temp("test.fits")) as hdul: # Merely opening the file should blast some warnings about the # invalid keywords assert len(w) == 3 header = hdul[0].header assert "CLFIND2D" in header assert "Just som" in header assert "AAAAAAAA" in header assert header["CLFIND2D"] == ": contour = 0.30" assert header["Just som"] == "e random text." assert header["AAAAAAAA"] == "A" * 72 # It should not be possible to assign to the invalid keywords pytest.raises(ValueError, header.set, "CLFIND2D", "foo") pytest.raises(ValueError, header.set, "Just som", "foo") pytest.raises(ValueError, header.set, "AAAAAAAA", "foo") def test_fix_hierarch_with_invalid_value(self, capsys): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/172 Ensures that when fixing a hierarch card it remains a hierarch card. """ c = fits.Card.fromstring("HIERARCH ESO DET CHIP PXSPACE = 5e6") with pytest.warns( fits.verify.VerifyWarning, match=r"Verification reported errors" ): c.verify("fix") assert str(c) == _pad("HIERARCH ESO DET CHIP PXSPACE = 5E6") def test_assign_inf_nan(self): """ Regression test for https://github.com/spacetelescope/PyFITS/issues/11 For the time being it should not be possible to assign the floating point values inf or nan to a header value, since this is not defined by the FITS standard. """ h = fits.Header() pytest.raises(ValueError, h.set, "TEST", float("nan")) pytest.raises(ValueError, h.set, "TEST", np.nan) pytest.raises(ValueError, h.set, "TEST", np.float32("nan")) pytest.raises(ValueError, h.set, "TEST", float("inf")) pytest.raises(ValueError, h.set, "TEST", np.inf) def test_update_bool(self): """ Regression test for an issue where a value of True in a header cannot be updated to a value of 1, and likewise for False/0. """ h = fits.Header([("TEST", True)]) h["TEST"] = 1 assert h["TEST"] is not True assert isinstance(h["TEST"], int) assert h["TEST"] == 1 h["TEST"] = np.bool_(True) assert h["TEST"] is True h["TEST"] = False assert h["TEST"] is False h["TEST"] = np.bool_(False) assert h["TEST"] is False h["TEST"] = 0 assert h["TEST"] is not False assert isinstance(h["TEST"], int) assert h["TEST"] == 0 h["TEST"] = np.bool_(False) assert h["TEST"] is False def test_update_numeric(self): """ Regression test for https://github.com/spacetelescope/PyFITS/issues/49 Ensure that numeric values can be upcast/downcast between int, float, and complex by assigning values that compare equal to the existing value but are a different type. """ h = fits.Header() h["TEST"] = 1 # int -> float h["TEST"] = 1.0 assert isinstance(h["TEST"], float) assert str(h).startswith("TEST = 1.0") # float -> int h["TEST"] = 1 assert isinstance(h["TEST"], int) assert str(h).startswith("TEST = 1") # int -> complex h["TEST"] = 1.0 + 0.0j assert isinstance(h["TEST"], complex) assert str(h).startswith("TEST = (1.0, 0.0)") # complex -> float h["TEST"] = 1.0 assert isinstance(h["TEST"], float) assert str(h).startswith("TEST = 1.0") # float -> complex h["TEST"] = 1.0 + 0.0j assert isinstance(h["TEST"], complex) assert str(h).startswith("TEST = (1.0, 0.0)") # complex -> int h["TEST"] = 1 assert isinstance(h["TEST"], int) assert str(h).startswith("TEST = 1") # Now the same tests but with zeros h["TEST"] = 0 # int -> float h["TEST"] = 0.0 assert isinstance(h["TEST"], float) assert str(h).startswith("TEST = 0.0") # float -> int h["TEST"] = 0 assert isinstance(h["TEST"], int) assert str(h).startswith("TEST = 0") # int -> complex h["TEST"] = 0.0 + 0.0j assert isinstance(h["TEST"], complex) assert str(h).startswith("TEST = (0.0, 0.0)") # complex -> float h["TEST"] = 0.0 assert isinstance(h["TEST"], float) assert str(h).startswith("TEST = 0.0") # float -> complex h["TEST"] = 0.0 + 0.0j assert isinstance(h["TEST"], complex) assert str(h).startswith("TEST = (0.0, 0.0)") # complex -> int h["TEST"] = 0 assert isinstance(h["TEST"], int) assert str(h).startswith("TEST = 0") def test_newlines_in_commentary(self): """ Regression test for https://github.com/spacetelescope/PyFITS/issues/51 Test data extracted from a header in an actual FITS file found in the wild. Names have been changed to protect the innocent. """ # First ensure that we can't assign new keyword values with newlines in # them h = fits.Header() pytest.raises(ValueError, h.set, "HISTORY", "\n") pytest.raises(ValueError, h.set, "HISTORY", "\nabc") pytest.raises(ValueError, h.set, "HISTORY", "abc\n") pytest.raises(ValueError, h.set, "HISTORY", "abc\ndef") test_cards = [ "HISTORY File modified by user 'wilma' with fv on 2013-04-22T21:42:18 " "HISTORY File modified by user ' fred' with fv on 2013-04-23T11:16:29 " "HISTORY File modified by user ' fred' with fv on 2013-11-04T16:59:14 " "HISTORY File modified by user 'wilma' with fv on 2013-04-22T21:42:18\nFile modif" "HISTORY ied by user 'wilma' with fv on 2013-04-23T11:16:29\nFile modified by use" "HISTORY r ' fred' with fv on 2013-11-04T16:59:14 " "HISTORY File modified by user 'wilma' with fv on 2013-04-22T21:42:18\nFile modif" "HISTORY ied by user 'wilma' with fv on 2013-04-23T11:16:29\nFile modified by use" "HISTORY r ' fred' with fv on 2013-11-04T16:59:14\nFile modified by user 'wilma' " "HISTORY with fv on 2013-04-22T21:42:18\nFile modif\nied by user 'wilma' with fv " "HISTORY on 2013-04-23T11:16:29\nFile modified by use\nr ' fred' with fv on 2013-1" "HISTORY 1-04T16:59:14 " ] for card_image in test_cards: c = fits.Card.fromstring(card_image) if "\n" in card_image: pytest.raises(fits.VerifyError, c.verify, "exception") else: c.verify("exception") def test_long_commentary_card_appended_to_header(self): """ If a HISTORY or COMMENT card with a too-long value is appended to a header with Header.append (as opposed to assigning to hdr['HISTORY'] it fails verification. Regression test for https://github.com/astropy/astropy/issues/11486 """ header = fits.Header() value = "abc" * 90 # this is what Table does when saving its history metadata key to a # FITS file header.append(("history", value)) assert len(header.cards) == 1 # Test Card._split() directly since this was the main problem area key, val = header.cards[0]._split() assert key == "HISTORY" and val == value # Try writing adding this header to an HDU and writing it to a file hdu = fits.PrimaryHDU(header=header) hdu.writeto(self.temp("test.fits"), overwrite=True) def test_header_fromstring_bytes(self): """ Test reading a Header from a `bytes` string. See https://github.com/astropy/astropy/issues/8706 """ with open(self.data("test0.fits"), "rb") as fobj: pri_hdr_from_bytes = fits.Header.fromstring(fobj.read()) pri_hdr = fits.getheader(self.data("test0.fits")) assert pri_hdr["NAXIS"] == pri_hdr_from_bytes["NAXIS"] assert pri_hdr == pri_hdr_from_bytes assert pri_hdr.tostring() == pri_hdr_from_bytes.tostring() def test_set_keyword_with_space(self): """ Regression test for https://github.com/astropy/astropy/issues/10479 """ hdr = fits.Header() hdr["KEY2 "] = 2 hdr["KEY2 "] = 4 assert len(hdr) == 1 assert hdr["KEY2"] == 4 assert hdr["KEY2 "] == 4 def test_strip(self): hdr = fits.getheader(self.data("tb.fits"), ext=1) hdr["FOO"] = "bar" hdr.strip() assert set(hdr) == {"HISTORY", "FOO"} hdr = fits.getheader(self.data("tb.fits"), ext=1) hdr["FOO"] = "bar" hdr = hdr.copy(strip=True) assert set(hdr) == {"HISTORY", "FOO"} def test_update_invalid_card(self): """ Regression test for https://github.com/astropy/astropy/issues/5408 Tests updating the value of a card that is malformatted (with an invalid value literal). This tests two ways of reproducing the problem, one working with a Card object directly, and one when reading/writing a header containing such an invalid card. """ card = fits.Card.fromstring("KW = INF / Comment") card.value = "FIXED" assert tuple(card) == ("KW", "FIXED", "Comment") card.verify("fix") assert tuple(card) == ("KW", "FIXED", "Comment") card = fits.Card.fromstring("KW = INF") hdu = fits.PrimaryHDU() # This is a loophole to write a header containing a malformatted card card._verified = True hdu.header.append(card) hdu.header.tofile(self.temp("bogus.fits")) with fits.open(self.temp("bogus.fits")) as hdul: hdul[0].header["KW"] = -1 hdul.writeto(self.temp("bogus_fixed.fits")) with fits.open(self.temp("bogus_fixed.fits")) as hdul: assert hdul[0].header["KW"] == -1 def test_index_numpy_int(self): header = fits.Header([("A", "FOO"), ("B", 2), ("C", "BAR")]) idx = np.int8(2) assert header[idx] == "BAR" header[idx] = "BAZ" assert header[idx] == "BAZ" header.insert(idx, ("D", 42)) assert header[idx] == 42 header.add_comment("HELLO") header.add_comment("WORLD") assert header["COMMENT"][np.int64(1)] == "WORLD" header.append(("C", "BAZBAZ")) assert header[("C", np.int16(0))] == "BAZ" assert header[("C", np.uint32(1))] == "BAZBAZ" def test_header_data_size(self): """ Tests data size calculation (w/o padding) given a Header. """ hdu = fits.PrimaryHDU() header = hdu.header assert header.data_size == 0 header["BITPIX"] = 32 header["NAXIS"] = 2 header["NAXIS1"] = 100 header["NAXIS2"] = 100 assert header.data_size == 40000 assert header.data_size_padded == 40320 class TestRecordValuedKeywordCards(FitsTestCase): """ Tests for handling of record-valued keyword cards as used by the `FITS WCS distortion paper <https://www.atnf.csiro.au/people/mcalabre/WCS/dcs_20040422.pdf>`__. These tests are derived primarily from the release notes for PyFITS 1.4 (in which this feature was first introduced. Note that extra leading spaces in the `value` fields should be parsed on input, but will be stripped in the cards. """ def setup_method(self): super().setup_method() self._test_header = fits.Header() self._test_header.set("DP1", "NAXIS: 2") self._test_header.set("DP1", "AXIS.1: 1") self._test_header.set("DP1", "AXIS.2: 2") self._test_header.set("DP1", "NAUX: 2") self._test_header.set("DP1", "AUX.1.COEFF.0: 0") self._test_header.set("DP1", "AUX.1.POWER.0: 1") self._test_header.set("DP1", "AUX.1.COEFF.1: 0.00048828125") self._test_header.set("DP1", "AUX.1.POWER.1: 1") def test_initialize_rvkc(self): """ Test different methods for initializing a card that should be recognized as a RVKC """ c = fits.Card.fromstring("DP1 = 'NAXIS: 2' / A comment") assert c.keyword == "DP1.NAXIS" assert c.value == 2.0 assert c.field_specifier == "NAXIS" assert c.comment == "A comment" c = fits.Card.fromstring("DP1 = 'NAXIS: 2.1'") assert c.keyword == "DP1.NAXIS" assert c.value == 2.1 assert c.field_specifier == "NAXIS" c = fits.Card.fromstring("DP1 = 'NAXIS: a'") assert c.keyword == "DP1" assert c.value == "NAXIS: a" assert c.field_specifier is None c = fits.Card("DP1", "NAXIS: 2") assert c.keyword == "DP1.NAXIS" assert c.value == 2.0 assert c.field_specifier == "NAXIS" c = fits.Card("DP1", "NAXIS: 2.0") assert c.keyword == "DP1.NAXIS" assert c.value == 2.0 assert c.field_specifier == "NAXIS" c = fits.Card("DP1", "NAXIS: a") assert c.keyword == "DP1" assert c.value == "NAXIS: a" assert c.field_specifier is None c = fits.Card("DP1.NAXIS", 2) assert c.keyword == "DP1.NAXIS" assert c.value == 2.0 assert c.field_specifier == "NAXIS" c = fits.Card("DP1.NAXIS", 2.0) assert c.keyword == "DP1.NAXIS" assert c.value == 2.0 assert c.field_specifier == "NAXIS" with pytest.warns(fits.verify.VerifyWarning): c = fits.Card("DP1.NAXIS", "a") assert c.keyword == "DP1.NAXIS" assert c.value == "a" assert c.field_specifier is None def test_parse_field_specifier(self): """ Tests that the field_specifier can accessed from a card read from a string before any other attributes are accessed. """ c = fits.Card.fromstring("DP1 = 'NAXIS: 2' / A comment") assert c.field_specifier == "NAXIS" assert c.keyword == "DP1.NAXIS" assert c.value == 2.0 assert c.comment == "A comment" def test_update_field_specifier(self): """ Test setting the field_specifier attribute and updating the card image to reflect the new value. """ c = fits.Card.fromstring("DP1 = 'NAXIS: 2' / A comment") assert c.field_specifier == "NAXIS" c.field_specifier = "NAXIS1" assert c.field_specifier == "NAXIS1" assert c.keyword == "DP1.NAXIS1" assert c.value == 2.0 assert c.comment == "A comment" assert str(c).rstrip() == "DP1 = 'NAXIS1: 2' / A comment" def test_field_specifier_case_senstivity(self): """ The keyword portion of an RVKC should still be case-insensitive, but the field-specifier portion should be case-sensitive. """ header = fits.Header() header.set("abc.def", 1) header.set("abc.DEF", 2) assert header["abc.def"] == 1 assert header["ABC.def"] == 1 assert header["aBc.def"] == 1 assert header["ABC.DEF"] == 2 assert "ABC.dEf" not in header def test_get_rvkc_by_index(self): """ Returning a RVKC from a header via index lookup should return the float value of the card. """ assert self._test_header[0] == 2.0 assert isinstance(self._test_header[0], float) assert self._test_header[1] == 1.0 assert isinstance(self._test_header[1], float) def test_get_rvkc_by_keyword(self): """ Returning a RVKC just via the keyword name should return the full value string of the first card with that keyword. This test was changed to reflect the requirement in ticket https://aeon.stsci.edu/ssb/trac/pyfits/ticket/184--previously it required _test_header['DP1'] to return the parsed float value. """ assert self._test_header["DP1"] == "NAXIS: 2" def test_get_rvkc_by_keyword_and_field_specifier(self): """ Returning a RVKC via the full keyword/field-specifier combination should return the floating point value associated with the RVKC. """ assert self._test_header["DP1.NAXIS"] == 2.0 assert isinstance(self._test_header["DP1.NAXIS"], float) assert self._test_header["DP1.AUX.1.COEFF.1"] == 0.00048828125 def test_access_nonexistent_rvkc(self): """ Accessing a nonexistent RVKC should raise an IndexError for index-based lookup, or a KeyError for keyword lookup (like a normal card). """ pytest.raises(IndexError, lambda x: self._test_header[x], 8) # Test exception with message with pytest.raises(KeyError, match=r"Keyword 'DP1\.AXIS\.3' not found."): self._test_header["DP1.AXIS.3"] def test_update_rvkc(self): """A RVKC can be updated either via index or keyword access.""" self._test_header[0] = 3 assert self._test_header["DP1.NAXIS"] == 3.0 assert isinstance(self._test_header["DP1.NAXIS"], float) self._test_header["DP1.AXIS.1"] = 1.1 assert self._test_header["DP1.AXIS.1"] == 1.1 def test_update_rvkc_2(self): """Regression test for an issue that appeared after SVN r2412.""" h = fits.Header() h["D2IM1.EXTVER"] = 1 assert h["D2IM1.EXTVER"] == 1.0 h["D2IM1.EXTVER"] = 2 assert h["D2IM1.EXTVER"] == 2.0 def test_raw_keyword_value(self): c = fits.Card.fromstring("DP1 = 'NAXIS: 2' / A comment") assert c.rawkeyword == "DP1" assert c.rawvalue == "NAXIS: 2" c = fits.Card("DP1.NAXIS", 2) assert c.rawkeyword == "DP1" assert c.rawvalue == "NAXIS: 2.0" c = fits.Card("DP1.NAXIS", 2.0) assert c.rawkeyword == "DP1" assert c.rawvalue == "NAXIS: 2.0" def test_rvkc_insert_after(self): """ It should be possible to insert a new RVKC after an existing one specified by the full keyword/field-specifier combination.""" self._test_header.set("DP1", "AXIS.3: 1", "a comment", after="DP1.AXIS.2") assert self._test_header[3] == 1 assert self._test_header["DP1.AXIS.3"] == 1 def test_rvkc_delete(self): """ Deleting a RVKC should work as with a normal card by using the full keyword/field-spcifier combination. """ del self._test_header["DP1.AXIS.1"] assert len(self._test_header) == 7 assert list(self._test_header)[0] == "DP1.NAXIS" assert self._test_header[0] == 2 assert list(self._test_header)[1] == "DP1.AXIS.2" # Perform a subsequent delete to make sure all the index mappings were # updated del self._test_header["DP1.AXIS.2"] assert len(self._test_header) == 6 assert list(self._test_header)[0] == "DP1.NAXIS" assert self._test_header[0] == 2 assert list(self._test_header)[1] == "DP1.NAUX" assert self._test_header[1] == 2 def test_pattern_matching_keys(self): """Test the keyword filter strings with RVKCs.""" cl = self._test_header["DP1.AXIS.*"] assert isinstance(cl, fits.Header) assert [str(c).strip() for c in cl.cards] == [ "DP1 = 'AXIS.1: 1'", "DP1 = 'AXIS.2: 2'", ] cl = self._test_header["DP1.N*"] assert [str(c).strip() for c in cl.cards] == [ "DP1 = 'NAXIS: 2'", "DP1 = 'NAUX: 2'", ] cl = self._test_header["DP1.AUX..."] assert [str(c).strip() for c in cl.cards] == [ "DP1 = 'AUX.1.COEFF.0: 0'", "DP1 = 'AUX.1.POWER.0: 1'", "DP1 = 'AUX.1.COEFF.1: 0.00048828125'", "DP1 = 'AUX.1.POWER.1: 1'", ] cl = self._test_header["DP?.NAXIS"] assert [str(c).strip() for c in cl.cards] == ["DP1 = 'NAXIS: 2'"] cl = self._test_header["DP1.A*S.*"] assert [str(c).strip() for c in cl.cards] == [ "DP1 = 'AXIS.1: 1'", "DP1 = 'AXIS.2: 2'", ] def test_pattern_matching_key_deletion(self): """Deletion by filter strings should work.""" del self._test_header["DP1.A*..."] assert len(self._test_header) == 2 assert list(self._test_header)[0] == "DP1.NAXIS" assert self._test_header[0] == 2 assert list(self._test_header)[1] == "DP1.NAUX" assert self._test_header[1] == 2 def test_successive_pattern_matching(self): """ A card list returned via a filter string should be further filterable. """ cl = self._test_header["DP1.A*..."] assert [str(c).strip() for c in cl.cards] == [ "DP1 = 'AXIS.1: 1'", "DP1 = 'AXIS.2: 2'", "DP1 = 'AUX.1.COEFF.0: 0'", "DP1 = 'AUX.1.POWER.0: 1'", "DP1 = 'AUX.1.COEFF.1: 0.00048828125'", "DP1 = 'AUX.1.POWER.1: 1'", ] cl2 = cl["*.*AUX..."] assert [str(c).strip() for c in cl2.cards] == [ "DP1 = 'AUX.1.COEFF.0: 0'", "DP1 = 'AUX.1.POWER.0: 1'", "DP1 = 'AUX.1.COEFF.1: 0.00048828125'", "DP1 = 'AUX.1.POWER.1: 1'", ] def test_rvkc_in_cardlist_keys(self): """ The CardList.keys() method should return full keyword/field-spec values for RVKCs. """ cl = self._test_header["DP1.AXIS.*"] assert list(cl) == ["DP1.AXIS.1", "DP1.AXIS.2"] def test_rvkc_in_cardlist_values(self): """ The CardList.values() method should return the values of all RVKCs as floating point values. """ cl = self._test_header["DP1.AXIS.*"] assert list(cl.values()) == [1.0, 2.0] def test_rvkc_value_attribute(self): """ Individual card values should be accessible by the .value attribute (which should return a float). """ cl = self._test_header["DP1.AXIS.*"] assert cl.cards[0].value == 1.0 assert isinstance(cl.cards[0].value, float) def test_overly_permissive_parsing(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/183 Ensures that cards with standard commentary keywords are never treated as RVKCs. Also ensures that cards not strictly matching the RVKC pattern are not treated as such. """ h = fits.Header() h["HISTORY"] = "AXIS.1: 2" h["HISTORY"] = "AXIS.2: 2" assert "HISTORY.AXIS" not in h assert "HISTORY.AXIS.1" not in h assert "HISTORY.AXIS.2" not in h assert h["HISTORY"] == ["AXIS.1: 2", "AXIS.2: 2"] # This is an example straight out of the ticket where everything after # the '2012' in the date value was being ignored, allowing the value to # successfully be parsed as a "float" h = fits.Header() h["HISTORY"] = "Date: 2012-09-19T13:58:53.756061" assert "HISTORY.Date" not in h assert str(h.cards[0]) == _pad("HISTORY Date: 2012-09-19T13:58:53.756061") c = fits.Card.fromstring(" 'Date: 2012-09-19T13:58:53.756061'") assert c.keyword == "" assert c.value == "'Date: 2012-09-19T13:58:53.756061'" assert c.field_specifier is None h = fits.Header() h["FOO"] = "Date: 2012-09-19T13:58:53.756061" assert "FOO.Date" not in h assert str(h.cards[0]) == _pad("FOO = 'Date: 2012-09-19T13:58:53.756061'") def test_overly_aggressive_rvkc_lookup(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/184 Ensures that looking up a RVKC by keyword only (without the field-specifier) in a header returns the full string value of that card without parsing it as a RVKC. Also ensures that a full field-specifier is required to match a RVKC--a partial field-specifier that doesn't explicitly match any record-valued keyword should result in a KeyError. """ c1 = fits.Card.fromstring("FOO = 'AXIS.1: 2'") c2 = fits.Card.fromstring("FOO = 'AXIS.2: 4'") h = fits.Header([c1, c2]) assert h["FOO"] == "AXIS.1: 2" assert h[("FOO", 1)] == "AXIS.2: 4" assert h["FOO.AXIS.1"] == 2.0 assert h["FOO.AXIS.2"] == 4.0 assert "FOO.AXIS" not in h assert "FOO.AXIS." not in h assert "FOO." not in h pytest.raises(KeyError, lambda: h["FOO.AXIS"]) pytest.raises(KeyError, lambda: h["FOO.AXIS."]) pytest.raises(KeyError, lambda: h["FOO."]) def test_fitsheader_script(self): """Tests the basic functionality of the `fitsheader` script.""" from astropy.io.fits.scripts import fitsheader # Can an extension by specified by the EXTNAME keyword? hf = fitsheader.HeaderFormatter(self.data("zerowidth.fits")) output = hf.parse(extensions=["AIPS FQ"]) assert "EXTNAME = 'AIPS FQ" in output assert "BITPIX" in output # Can we limit the display to one specific keyword? output = hf.parse(extensions=["AIPS FQ"], keywords=["EXTNAME"]) assert "EXTNAME = 'AIPS FQ" in output assert "BITPIX =" not in output assert len(output.split("\n")) == 3 # Can we limit the display to two specific keywords? output = hf.parse(extensions=[1], keywords=["EXTNAME", "BITPIX"]) assert "EXTNAME =" in output assert "BITPIX =" in output assert len(output.split("\n")) == 4 # Can we use wildcards for keywords? output = hf.parse(extensions=[1], keywords=["NAXIS*"]) assert "NAXIS =" in output assert "NAXIS1 =" in output assert "NAXIS2 =" in output hf.close() # Can an extension by specified by the EXTNAME+EXTVER keywords? hf = fitsheader.HeaderFormatter(self.data("test0.fits")) assert "EXTNAME = 'SCI" in hf.parse(extensions=["SCI,2"]) hf.close() # Can we print the original header before decompression? hf = fitsheader.HeaderFormatter(self.data("comp.fits")) assert "XTENSION= 'IMAGE" in hf.parse(extensions=[1], compressed=False) assert "XTENSION= 'BINTABLE" in hf.parse(extensions=[1], compressed=True) hf.close() def test_fitsheader_compressed_from_primary_image_ext(self): """Regression test for issue https://github.com/astropy/astropy/issues/7312""" data = np.arange(2 * 2, dtype=np.int8).reshape((2, 2)) phdu = fits.PrimaryHDU(data=data) chdu = fits.CompImageHDU(data=phdu.data, header=phdu.header) chdu.writeto(self.temp("tmp2.fits"), overwrite=True) with fits.open(self.temp("tmp2.fits")) as hdul: assert "XTENSION" not in hdul[1].header assert "PCOUNT" not in hdul[1].header assert "GCOUNT" not in hdul[1].header def test_fitsheader_table_feature(self): """Tests the `--table` feature of the `fitsheader` script.""" from astropy.io import fits from astropy.io.fits.scripts import fitsheader test_filename = self.data("zerowidth.fits") formatter = fitsheader.TableHeaderFormatter(test_filename) with fits.open(test_filename) as fitsobj: # Does the table contain the expected number of rows? mytable = formatter.parse([0]) assert len(mytable) == len(fitsobj[0].header) # Repeat the above test when multiple HDUs are requested mytable = formatter.parse(extensions=["AIPS FQ", 2, "4"]) assert len(mytable) == ( len(fitsobj["AIPS FQ"].header) + len(fitsobj[2].header) + len(fitsobj[4].header) ) # Can we recover the filename and extension name from the table? mytable = formatter.parse(extensions=["AIPS FQ"]) assert np.all(mytable["filename"] == test_filename) assert np.all(mytable["hdu"] == "AIPS FQ") assert mytable["value"][mytable["keyword"] == "EXTNAME"] == "AIPS FQ" # Can we specify a single extension/keyword? mytable = formatter.parse(extensions=["AIPS FQ"], keywords=["EXTNAME"]) assert len(mytable) == 1 assert mytable["hdu"][0] == "AIPS FQ" assert mytable["keyword"][0] == "EXTNAME" assert mytable["value"][0] == "AIPS FQ" # Is an incorrect extension dealt with gracefully? mytable = formatter.parse(extensions=["DOES_NOT_EXIST"]) assert mytable is None # Is an incorrect keyword dealt with gracefully? mytable = formatter.parse(extensions=["AIPS FQ"], keywords=["DOES_NOT_EXIST"]) assert mytable is None formatter.close() @pytest.mark.parametrize("mode", ["wb", "wb+", "ab", "ab+"]) def test_hdu_writeto_mode(self, mode): with open(self.temp("mode.fits"), mode=mode) as ff: hdu = fits.ImageHDU(data=np.ones(5)) hdu.writeto(ff) def test_subclass(): """Check that subclasses don't get ignored on slicing and copying.""" class MyHeader(fits.Header): def append(self, card, *args, **kwargs): if isinstance(card, tuple) and len(card) == 2: # Just for our checks we add a comment if there is none. card += ("no comment",) return super().append(card, *args, **kwargs) my_header = MyHeader( ( ("a", 1.0, "first"), ("b", 2.0, "second"), ( "c", 3.0, ), ) ) assert my_header.comments["a"] == "first" assert my_header.comments["b"] == "second" assert my_header.comments["c"] == "no comment" slice_ = my_header[1:] assert type(slice_) is MyHeader assert slice_.comments["b"] == "second" assert slice_.comments["c"] == "no comment" selection = my_header["c*"] assert type(selection) is MyHeader assert selection.comments["c"] == "no comment" copy_ = my_header.copy() assert type(copy_) is MyHeader assert copy_.comments["b"] == "second" assert copy_.comments["c"] == "no comment" my_header.extend((("d", 4.0),)) assert my_header.comments["d"] == "no comment"
b83f811b7350d876365679641c6c46cc3c9ffd2a40d4528d09ed8963e60c7418
# Licensed under a 3-clause BSD style license - see LICENSE.rst import os import numpy as np import pytest from astropy import __version__ as version from astropy.io import fits from astropy.io.fits import FITSDiff, HDUList, Header, ImageHDU from astropy.io.fits.convenience import writeto from astropy.io.fits.hdu import PrimaryHDU, hdulist from astropy.io.fits.scripts import fitsdiff from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH from .conftest import FitsTestCase class TestFITSDiff_script(FitsTestCase): def test_help(self): with pytest.raises(SystemExit) as e: fitsdiff.main(["-h"]) assert e.value.code == 0 def test_version(self, capsys): with pytest.raises(SystemExit) as e: fitsdiff.main(["--version"]) out = capsys.readouterr()[0] assert out == f"fitsdiff {version}" assert e.value.code == 0 def test_noargs(self): with pytest.raises(SystemExit) as e: fitsdiff.main([""]) assert e.value.code == 2 def test_oneargargs(self): with pytest.raises(SystemExit) as e: fitsdiff.main(["file1"]) assert e.value.code == 2 def test_nodiff(self): a = np.arange(100).reshape(10, 10) hdu_a = PrimaryHDU(data=a) b = a.copy() hdu_b = PrimaryHDU(data=b) tmp_a = self.temp("testa.fits") tmp_b = self.temp("testb.fits") hdu_a.writeto(tmp_a) hdu_b.writeto(tmp_b) numdiff = fitsdiff.main([tmp_a, tmp_b]) assert numdiff == 0 def test_onediff(self): a = np.arange(100).reshape(10, 10) hdu_a = PrimaryHDU(data=a) b = a.copy() b[1, 0] = 12 hdu_b = PrimaryHDU(data=b) tmp_a = self.temp("testa.fits") tmp_b = self.temp("testb.fits") hdu_a.writeto(tmp_a) hdu_b.writeto(tmp_b) numdiff = fitsdiff.main([tmp_a, tmp_b]) assert numdiff == 1 def test_manydiff(self, capsys): a = np.arange(100).reshape(10, 10) hdu_a = PrimaryHDU(data=a) b = a + 1 hdu_b = PrimaryHDU(data=b) tmp_a = self.temp("testa.fits") tmp_b = self.temp("testb.fits") hdu_a.writeto(tmp_a) hdu_b.writeto(tmp_b) numdiff = fitsdiff.main([tmp_a, tmp_b]) out, err = capsys.readouterr() assert numdiff == 1 assert out.splitlines()[-4:] == [ " a> 9", " b> 10", " ...", " 100 different pixels found (100.00% different).", ] numdiff = fitsdiff.main(["-n", "1", tmp_a, tmp_b]) out, err = capsys.readouterr() assert numdiff == 1 assert out.splitlines()[-4:] == [ " a> 0", " b> 1", " ...", " 100 different pixels found (100.00% different).", ] def test_outputfile(self): a = np.arange(100).reshape(10, 10) hdu_a = PrimaryHDU(data=a) b = a.copy() b[1, 0] = 12 hdu_b = PrimaryHDU(data=b) tmp_a = self.temp("testa.fits") tmp_b = self.temp("testb.fits") hdu_a.writeto(tmp_a) hdu_b.writeto(tmp_b) numdiff = fitsdiff.main(["-o", self.temp("diff.txt"), tmp_a, tmp_b]) assert numdiff == 1 with open(self.temp("diff.txt")) as f: out = f.read() assert out.splitlines()[-4:] == [ " Data differs at [1, 2]:", " a> 10", " b> 12", " 1 different pixels found (1.00% different).", ] def test_atol(self): a = np.arange(100, dtype=float).reshape(10, 10) hdu_a = PrimaryHDU(data=a) b = a.copy() b[1, 0] = 11 hdu_b = PrimaryHDU(data=b) tmp_a = self.temp("testa.fits") tmp_b = self.temp("testb.fits") hdu_a.writeto(tmp_a) hdu_b.writeto(tmp_b) numdiff = fitsdiff.main(["-a", "1", tmp_a, tmp_b]) assert numdiff == 0 numdiff = fitsdiff.main(["--exact", "-a", "1", tmp_a, tmp_b]) assert numdiff == 1 def test_rtol(self): a = np.arange(100, dtype=float).reshape(10, 10) hdu_a = PrimaryHDU(data=a) b = a.copy() b[1, 0] = 11 hdu_b = PrimaryHDU(data=b) tmp_a = self.temp("testa.fits") tmp_b = self.temp("testb.fits") hdu_a.writeto(tmp_a) hdu_b.writeto(tmp_b) numdiff = fitsdiff.main(["-r", "1e-1", tmp_a, tmp_b]) assert numdiff == 0 def test_rtol_diff(self, capsys): a = np.arange(100, dtype=float).reshape(10, 10) hdu_a = PrimaryHDU(data=a) b = a.copy() b[1, 0] = 11 hdu_b = PrimaryHDU(data=b) tmp_a = self.temp("testa.fits") tmp_b = self.temp("testb.fits") hdu_a.writeto(tmp_a) hdu_b.writeto(tmp_b) numdiff = fitsdiff.main(["-r", "1e-2", tmp_a, tmp_b]) assert numdiff == 1 out, err = capsys.readouterr() assert ( out == f""" fitsdiff: {version} a: {tmp_a} b: {tmp_b} Maximum number of different data values to be reported: 10 Relative tolerance: 0.01, Absolute tolerance: 0.0 Primary HDU: Data contains differences: Data differs at [1, 2]: a> 10.0 ? ^ b> 11.0 ? ^ 1 different pixels found (1.00% different). """ ) assert err == "" def test_wildcard(self): tmp1 = self.temp("tmp_file1") with pytest.raises(SystemExit) as e: fitsdiff.main([tmp1 + "*", "ACME"]) assert e.value.code == 2 def test_not_quiet(self, capsys): a = np.arange(100).reshape(10, 10) hdu_a = PrimaryHDU(data=a) b = a.copy() hdu_b = PrimaryHDU(data=b) tmp_a = self.temp("testa.fits") tmp_b = self.temp("testb.fits") hdu_a.writeto(tmp_a) hdu_b.writeto(tmp_b) numdiff = fitsdiff.main([tmp_a, tmp_b]) assert numdiff == 0 out, err = capsys.readouterr() assert ( out == f""" fitsdiff: {version} a: {tmp_a} b: {tmp_b} Maximum number of different data values to be reported: 10 Relative tolerance: 0.0, Absolute tolerance: 0.0 No differences found. """ ) assert err == "" def test_quiet(self, capsys): a = np.arange(100).reshape(10, 10) hdu_a = PrimaryHDU(data=a) b = a.copy() hdu_b = PrimaryHDU(data=b) tmp_a = self.temp("testa.fits") tmp_b = self.temp("testb.fits") hdu_a.writeto(tmp_a) hdu_b.writeto(tmp_b) numdiff = fitsdiff.main(["-q", tmp_a, tmp_b]) assert numdiff == 0 out, err = capsys.readouterr() assert out == "" assert err == "" @pytest.mark.slow def test_path(self, capsys): os.mkdir(self.temp("sub/")) tmp_b = self.temp("sub/ascii.fits") tmp_g = self.temp("sub/group.fits") tmp_h = self.data("group.fits") with hdulist.fitsopen(tmp_h) as hdu_b: hdu_b.writeto(tmp_g) writeto(tmp_b, np.arange(100).reshape(10, 10)) # one modified file and a directory assert fitsdiff.main(["-q", self.data_dir, tmp_b]) == 1 assert fitsdiff.main(["-q", tmp_b, self.data_dir]) == 1 # two directories tmp_d = self.temp("sub/") assert fitsdiff.main(["-q", self.data_dir, tmp_d]) == 1 assert fitsdiff.main(["-q", tmp_d, self.data_dir]) == 1 with pytest.warns( UserWarning, match=r"Field 'ORBPARM' has a repeat " r"count of 0 in its format code", ): assert fitsdiff.main(["-q", self.data_dir, self.data_dir]) == 0 # no match tmp_c = self.data("arange.fits") fitsdiff.main([tmp_c, tmp_d]) out, err = capsys.readouterr() assert "'arange.fits' has no match in" in err # globbing with pytest.warns( UserWarning, match=r"Field 'ORBPARM' has a repeat " r"count of 0 in its format code", ): assert fitsdiff.main(["-q", self.data_dir + "/*.fits", self.data_dir]) == 0 assert fitsdiff.main(["-q", self.data_dir + "/g*.fits", tmp_d]) == 0 # one file and a directory tmp_f = self.data("tb.fits") assert fitsdiff.main(["-q", tmp_f, self.data_dir]) == 0 assert fitsdiff.main(["-q", self.data_dir, tmp_f]) == 0 def test_ignore_hdus(self): a = np.arange(100).reshape(10, 10) b = a.copy() + 1 ha = Header([("A", 1), ("B", 2), ("C", 3)]) phdu_a = PrimaryHDU(header=ha) phdu_b = PrimaryHDU(header=ha) ihdu_a = ImageHDU(data=a, name="SCI") ihdu_b = ImageHDU(data=b, name="SCI") hdulist_a = HDUList([phdu_a, ihdu_a]) hdulist_b = HDUList([phdu_b, ihdu_b]) tmp_a = self.temp("testa.fits") tmp_b = self.temp("testb.fits") hdulist_a.writeto(tmp_a) hdulist_b.writeto(tmp_b) numdiff = fitsdiff.main([tmp_a, tmp_b]) assert numdiff == 1 numdiff = fitsdiff.main([tmp_a, tmp_b, "-u", "SCI"]) assert numdiff == 0 def test_ignore_hdus_report(self, capsys): a = np.arange(100).reshape(10, 10) b = a.copy() + 1 ha = Header([("A", 1), ("B", 2), ("C", 3)]) phdu_a = PrimaryHDU(header=ha) phdu_b = PrimaryHDU(header=ha) ihdu_a = ImageHDU(data=a, name="SCI") ihdu_b = ImageHDU(data=b, name="SCI") hdulist_a = HDUList([phdu_a, ihdu_a]) hdulist_b = HDUList([phdu_b, ihdu_b]) tmp_a = self.temp("testa.fits") tmp_b = self.temp("testb.fits") hdulist_a.writeto(tmp_a) hdulist_b.writeto(tmp_b) numdiff = fitsdiff.main([tmp_a, tmp_b, "-u", "SCI"]) assert numdiff == 0 out, err = capsys.readouterr() assert "testa.fits" in out assert "testb.fits" in out @pytest.mark.skip(reason="fails intentionally to show open files (see PR #10159)") def test_fitsdiff_openfile(tmp_path): """Make sure that failing FITSDiff doesn't leave open files.""" path1 = tmp_path / "file1.fits" path2 = tmp_path / "file2.fits" hdulist = HDUList([PrimaryHDU(), ImageHDU(data=np.zeros(5))]) hdulist.writeto(path1) hdulist[1].data[0] = 1 hdulist.writeto(path2) diff = FITSDiff(path1, path2) assert diff.identical, diff.report() class Test_FITSDiff(FitsTestCase): def test_FITSDiff_report(self, home_is_temp): self.copy_file("test0.fits") fits.setval(self.temp("test0.fits"), "TESTKEY", value="testval") d = FITSDiff(self.data("test0.fits"), self.temp("test0.fits")) assert not d.identical d.report(self.temp("diff_report.txt")) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): d.report(self.temp("diff_report.txt"), overwrite=False) d.report(self.temp("diff_report.txt"), overwrite=True) with open(os.path.expanduser(self.temp("diff_report.txt"))) as f: assert "Extra keyword 'TESTKEY' in b: 'testval'" in f.read()
bff5c74b1f4c17100c3adf8be08693e9250d13b038f050170ed1bc446ff015ca
# Licensed under a 3-clause BSD style license - see PYFITS.rst import sys import numpy as np from astropy.io import fits from .conftest import FitsTestCase def compare_arrays(arr1in, arr2in, verbose=False): """ Compare the values field-by-field in two sets of numpy arrays or recarrays. """ arr1 = arr1in.view(np.ndarray) arr2 = arr2in.view(np.ndarray) nfail = 0 for n2 in arr2.dtype.names: n1 = n2 if n1 not in arr1.dtype.names: n1 = n1.lower() if n1 not in arr1.dtype.names: n1 = n1.upper() if n1 not in arr1.dtype.names: raise ValueError(f"field name {n2} not found in array 1") if verbose: sys.stdout.write(f" testing field: '{n2}'\n") sys.stdout.write(" shape...........") if arr2[n2].shape != arr1[n1].shape: nfail += 1 if verbose: sys.stdout.write("shapes differ\n") else: if verbose: sys.stdout.write("OK\n") sys.stdout.write(" elements........") (w,) = np.where(arr1[n1].ravel() != arr2[n2].ravel()) if w.size > 0: nfail += 1 if verbose: sys.stdout.write( f"\n {w.size} elements in field {n2} differ\n" ) else: if verbose: sys.stdout.write("OK\n") if nfail == 0: if verbose: sys.stdout.write("All tests passed\n") return True else: if verbose: sys.stdout.write(f"{nfail} differences found\n") return False def get_test_data(verbose=False): st = np.zeros(3, [("f1", "i4"), ("f2", "S6"), ("f3", ">2f8")]) np.random.seed(35) st["f1"] = [1, 3, 5] st["f2"] = ["hello", "world", "byebye"] st["f3"] = np.random.random(st["f3"].shape) return st class TestStructured(FitsTestCase): def test_structured(self): fname = self.data("stddata.fits") data1, h1 = fits.getdata(fname, ext=1, header=True) data2, h2 = fits.getdata(fname, ext=2, header=True) st = get_test_data() outfile = self.temp("test.fits") fits.writeto(outfile, data1, overwrite=True) fits.append(outfile, data2) fits.append(outfile, st) assert st.dtype.isnative assert np.all(st["f1"] == [1, 3, 5]) data1check, h1check = fits.getdata(outfile, ext=1, header=True) data2check, h2check = fits.getdata(outfile, ext=2, header=True) stcheck, sthcheck = fits.getdata(outfile, ext=3, header=True) assert compare_arrays(data1, data1check, verbose=True) assert compare_arrays(data2, data2check, verbose=True) assert compare_arrays(st, stcheck, verbose=True) # try reading with view dataviewcheck, hviewcheck = fits.getdata( outfile, ext=2, header=True, view=np.ndarray ) assert compare_arrays(data2, dataviewcheck, verbose=True)
1ab3399793c678c8125128cc2c16f269cd9ac197c5050f266b818436520d5179
# Licensed under a 3-clause BSD style license - see PYFITS.rst import numpy as np import pytest from astropy.io import fits from astropy.io.fits.compression import compress_hdu from .conftest import FitsTestCase MAX_INT = np.iinfo(np.intc).max MAX_LONG = np.iinfo(int).max MAX_LONGLONG = np.iinfo(np.longlong).max class TestCompressionFunction(FitsTestCase): def test_wrong_argument_number(self): with pytest.raises(TypeError): compress_hdu(1, 2) def test_unknown_compression_type(self): hdu = fits.CompImageHDU(np.ones((10, 10))) hdu._header["ZCMPTYPE"] = "fun" with pytest.raises(ValueError) as exc: compress_hdu(hdu) assert "Unrecognized compression type: fun" in str(exc.value) def test_zbitpix_unknown(self): hdu = fits.CompImageHDU(np.ones((10, 10))) hdu._header["ZBITPIX"] = 13 with pytest.raises(ValueError) as exc: compress_hdu(hdu) assert "Invalid value for BITPIX: 13" in str(exc.value) def test_data_none(self): hdu = fits.CompImageHDU(np.ones((10, 10))) hdu.data = None with pytest.raises(TypeError) as exc: compress_hdu(hdu) assert "CompImageHDU.data must be a numpy.ndarray" in str(exc.value) def test_missing_internal_header(self): hdu = fits.CompImageHDU(np.ones((10, 10))) del hdu._header with pytest.raises(AttributeError) as exc: compress_hdu(hdu) assert "_header" in str(exc.value) def test_invalid_tform(self): hdu = fits.CompImageHDU(np.ones((10, 10))) hdu._header["TFORM1"] = "TX" with pytest.raises(RuntimeError) as exc: compress_hdu(hdu) assert "TX" in str(exc.value) and "TFORM" in str(exc.value) def test_invalid_zdither(self): hdu = fits.CompImageHDU(np.ones((10, 10)), quantize_method=1) hdu._header["ZDITHER0"] = "a" with pytest.raises(TypeError): compress_hdu(hdu) @pytest.mark.parametrize("kw", ["ZNAXIS", "ZBITPIX"]) def test_header_missing_keyword(self, kw): hdu = fits.CompImageHDU(np.ones((10, 10))) del hdu._header[kw] with pytest.raises(KeyError) as exc: compress_hdu(hdu) assert kw in str(exc.value) @pytest.mark.parametrize("kw", ["ZNAXIS", "ZVAL1", "ZVAL2", "ZBLANK", "BLANK"]) def test_header_value_int_overflow(self, kw): hdu = fits.CompImageHDU(np.ones((10, 10))) hdu._header[kw] = MAX_INT + 1 with pytest.raises(OverflowError): compress_hdu(hdu) @pytest.mark.parametrize("kw", ["ZTILE1", "ZNAXIS1"]) def test_header_value_long_overflow(self, kw): hdu = fits.CompImageHDU(np.ones((10, 10))) hdu._header[kw] = MAX_LONG + 1 with pytest.raises(OverflowError): compress_hdu(hdu) @pytest.mark.parametrize("kw", ["NAXIS1", "NAXIS2", "TNULL1", "PCOUNT", "THEAP"]) def test_header_value_longlong_overflow(self, kw): hdu = fits.CompImageHDU(np.ones((10, 10))) hdu._header[kw] = MAX_LONGLONG + 1 with pytest.raises(OverflowError): compress_hdu(hdu) @pytest.mark.parametrize("kw", ["ZVAL3"]) def test_header_value_float_overflow(self, kw): hdu = fits.CompImageHDU(np.ones((10, 10))) hdu._header[kw] = 1e300 with pytest.raises(OverflowError): compress_hdu(hdu) @pytest.mark.parametrize("kw", ["NAXIS1", "NAXIS2", "TFIELDS", "PCOUNT"]) def test_header_value_negative(self, kw): hdu = fits.CompImageHDU(np.ones((10, 10))) hdu._header[kw] = -1 with pytest.raises(ValueError) as exc: compress_hdu(hdu) assert f"{kw} should not be negative." in str(exc.value) @pytest.mark.parametrize(("kw", "limit"), [("ZNAXIS", 999), ("TFIELDS", 999)]) def test_header_value_exceeds_custom_limit(self, kw, limit): hdu = fits.CompImageHDU(np.ones((10, 10))) hdu._header[kw] = limit + 1 with pytest.raises(ValueError) as exc: compress_hdu(hdu) assert kw in str(exc.value) @pytest.mark.parametrize( "kw", ["TTYPE1", "TFORM1", "ZCMPTYPE", "ZNAME1", "ZQUANTIZ"] ) def test_header_value_no_string(self, kw): hdu = fits.CompImageHDU(np.ones((10, 10))) hdu._header[kw] = 1 with pytest.raises(TypeError): compress_hdu(hdu) @pytest.mark.parametrize("kw", ["TZERO1", "TSCAL1"]) def test_header_value_no_double(self, kw): hdu = fits.CompImageHDU(np.ones((10, 10))) hdu._header[kw] = "1" with pytest.raises(TypeError): compress_hdu(hdu) @pytest.mark.parametrize("kw", ["ZSCALE", "ZZERO"]) def test_header_value_no_double_int_image(self, kw): hdu = fits.CompImageHDU(np.ones((10, 10), dtype=np.int32)) hdu._header[kw] = "1" with pytest.raises(TypeError): compress_hdu(hdu)
500d2b49bcdb7c036ef72a4721ccc72e331d6cbcb7335cf2b19d2c1082248219
# Licensed under a 3-clause BSD style license - see LICENSE.rst import pytest from astropy import __version__ as version from astropy.io.fits.scripts import fitsheader from .conftest import FitsTestCase class TestFITSheader_script(FitsTestCase): def test_help(self): with pytest.raises(SystemExit) as e: fitsheader.main(["-h"]) assert e.value.code == 0 def test_version(self, capsys): with pytest.raises(SystemExit) as e: fitsheader.main(["--version"]) out = capsys.readouterr()[0] assert out == f"fitsheader {version}" assert e.value.code == 0 def test_file_exists(self, capsys): fitsheader.main([self.data("arange.fits")]) out, err = capsys.readouterr() assert out.splitlines()[1].startswith( "SIMPLE = T / conforms to FITS standard" ) assert err == "" def test_by_keyword(self, capsys): fitsheader.main(["-k", "NAXIS", self.data("arange.fits")]) out, err = capsys.readouterr() assert out.splitlines()[1].startswith( "NAXIS = 3 / number of array dimensions" ) fitsheader.main(["-k", "NAXIS*", self.data("arange.fits")]) out, err = capsys.readouterr() out = out.splitlines() assert len(out) == 5 assert out[1].startswith("NAXIS") assert out[2].startswith("NAXIS1") assert out[3].startswith("NAXIS2") assert out[4].startswith("NAXIS3") fitsheader.main(["-k", "RANDOMKEY", self.data("arange.fits")]) out, err = capsys.readouterr() assert err.startswith("WARNING") and "RANDOMKEY" in err assert not err.startswith("ERROR") def test_by_extension(self, capsys): fitsheader.main(["-e", "1", self.data("test0.fits")]) out, err = capsys.readouterr() assert len(out.splitlines()) == 62 fitsheader.main(["-e", "3", "-k", "BACKGRND", self.data("test0.fits")]) out, err = capsys.readouterr() assert out.splitlines()[1].startswith("BACKGRND= 312.") fitsheader.main(["-e", "0", "-k", "BACKGRND", self.data("test0.fits")]) out, err = capsys.readouterr() assert err.startswith("WARNING") fitsheader.main(["-e", "3", "-k", "FOO", self.data("test0.fits")]) out, err = capsys.readouterr() assert err.startswith("WARNING") def test_table(self, capsys): fitsheader.main(["-t", "-k", "BACKGRND", self.data("test0.fits")]) out, err = capsys.readouterr() out = out.splitlines() assert len(out) == 5 assert out[1].endswith("| 1 | BACKGRND | 316.0 |") assert out[2].endswith("| 2 | BACKGRND | 351.0 |") assert out[3].endswith("| 3 | BACKGRND | 312.0 |") assert out[4].endswith("| 4 | BACKGRND | 323.0 |") fitsheader.main( [ "-t", "-e", "0", "-k", "NAXIS", self.data("arange.fits"), self.data("ascii.fits"), self.data("blank.fits"), ] ) out, err = capsys.readouterr() out = out.splitlines() assert len(out) == 4 assert out[1].endswith("| 0 | NAXIS | 3 |") assert out[2].endswith("| 0 | NAXIS | 0 |") assert out[3].endswith("| 0 | NAXIS | 2 |") def test_fitsort(self, capsys): fitsheader.main( [ "-e", "0", "-f", "-k", "EXPSTART", "-k", "EXPTIME", self.data("test0.fits"), self.data("test1.fits"), ] ) out, err = capsys.readouterr() out = out.splitlines() assert len(out) == 4 assert out[2].endswith("test0.fits 49491.65366175 0.23") assert out[3].endswith("test1.fits 49492.65366175 0.22") fitsheader.main( [ "-e", "0", "-f", "-k", "EXPSTART", "-k", "EXPTIME", self.data("test0.fits"), ] ) out, err = capsys.readouterr() out = out.splitlines() assert len(out) == 3 assert out[2].endswith("test0.fits 49491.65366175 0.23") fitsheader.main( ["-f", "-k", "NAXIS", self.data("tdim.fits"), self.data("test1.fits")] ) out, err = capsys.readouterr() out = out.splitlines() assert len(out) == 4 assert out[0].endswith("0:NAXIS 1:NAXIS 2:NAXIS 3:NAXIS 4:NAXIS") assert out[2].endswith("tdim.fits 0 2 -- -- --") assert out[3].endswith("test1.fits 0 2 2 2 2") # check that files without required keyword are present fitsheader.main( ["-f", "-k", "DATE-OBS", self.data("table.fits"), self.data("test0.fits")] ) out, err = capsys.readouterr() out = out.splitlines() assert len(out) == 4 assert out[2].endswith("table.fits --") assert out[3].endswith("test0.fits 19/05/94") # check that COMMENT and HISTORY are excluded fitsheader.main(["-e", "0", "-f", self.data("tb.fits")]) out, err = capsys.readouterr() out = out.splitlines() assert len(out) == 3 assert out[2].endswith( "tb.fits True 16 0 True STScI-STSDAS/TABLES tb.fits 1" ) def test_fitsort_sorting_keyword_fitsort(self, capsys): """check that sorting by keyword works""" fitsheader.main( [ "-f", "-k", "NAXIS", "-e", "0", self.data("group.fits"), self.data("test0.fits"), ] ) out_unsorted, err_unsorted = capsys.readouterr() out_unsorted = out_unsorted.splitlines() fitsheader.main( [ "-f", "-s", "NAXIS", "-k", "NAXIS", "-e", "0", self.data("group.fits"), self.data("test0.fits"), ] ) out_sorted, err_sorted = capsys.readouterr() out_sorted = out_sorted.splitlines() assert len(out_unsorted) == 4 assert out_unsorted[2].endswith("group.fits 5") assert out_unsorted[3].endswith("test0.fits 0") assert len(out_sorted) == 4 assert out_sorted[2].endswith("test0.fits 0") assert out_sorted[3].endswith("group.fits 5") def test_fitsort_sorting_keyword_complains(self, capsys): with pytest.raises(SystemExit): fitsheader.main( ["-t", "-s", "DUMMY", self.data("group.fits"), self.data("test0.fits")] ) out_table, err_table = capsys.readouterr() assert "only supported in conjunction with -f/--fitsort" in err_table with pytest.raises(SystemExit): fitsheader.main( ["-s", "DUMMY", self.data("group.fits"), self.data("test0.fits")] ) out_default, err_default = capsys.readouterr() assert "only supported in conjunction with -f/--fitsort" in err_default def test_dotkeyword(self, capsys): fitsheader.main(["-e", "0", "-k", "ESO DET ID", self.data("fixed-1890.fits")]) out, err = capsys.readouterr() out = out.splitlines() assert len(out) == 2 assert ( out[1].strip().endswith("HIERARCH ESO DET ID = 'DV13' / Detector system Id") ) fitsheader.main(["-e", "0", "-k", "ESO.DET.ID", self.data("fixed-1890.fits")]) out, err = capsys.readouterr() out = out.splitlines() assert len(out) == 2 assert ( out[1].strip().endswith("HIERARCH ESO DET ID = 'DV13' / Detector system Id") )
655c4b172e1ec3443f1e162b3cc72d5bb51e08ffd71b554ecde99060f73cea06
import gc import warnings import numpy as np import pytest from numpy.testing import assert_allclose, assert_array_equal from astropy import units as u from astropy.io import fits from astropy.io.fits import BinTableHDU, HDUList, ImageHDU, PrimaryHDU, table_to_hdu from astropy.io.fits.column import ( _fortran_to_python_format, _parse_tdisp_format, python_to_tdisp, ) from astropy.io.tests.mixin_columns import compare_attrs, mixin_cols, serialized_names from astropy.table import Column, QTable, Table from astropy.table.table_helpers import simple_table from astropy.time import Time from astropy.units import allclose as quantity_allclose from astropy.units.format.fits import UnitScaleError from astropy.units.quantity import QuantityInfo from astropy.utils.compat import NUMPY_LT_1_22 from astropy.utils.data import get_pkg_data_filename from astropy.utils.exceptions import AstropyDeprecationWarning, AstropyUserWarning from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH # FITS does not preserve precision, in_subfmt, and out_subfmt. time_attrs = ["value", "shape", "format", "scale", "location"] compare_attrs = { name: (time_attrs if isinstance(col, Time) else compare_attrs[name]) for name, col in mixin_cols.items() } # FITS does not support multi-element location, array with object dtype, # or logarithmic quantities. unsupported_cols = { name: col for name, col in mixin_cols.items() if ( isinstance(col, Time) and col.location.shape != () or isinstance(col, np.ndarray) and col.dtype.kind == "O" or isinstance(col, u.LogQuantity) ) } mixin_cols = { name: col for name, col in mixin_cols.items() if name not in unsupported_cols } def equal_data(a, b): for name in a.dtype.names: if not np.all(a[name] == b[name]): return False return True class TestSingleTable: def setup_class(self): self.data = np.array( list(zip([1, 2, 3, 4], ["a", "b", "c", "d"], [2.3, 4.5, 6.7, 8.9])), dtype=[("a", int), ("b", "U1"), ("c", float)], ) def test_simple(self, tmp_path): filename = tmp_path / "test_simple.fts" t1 = Table(self.data) t1.write(filename, overwrite=True) t2 = Table.read(filename) assert equal_data(t1, t2) def test_simple_pathlib(self, tmp_path): filename = tmp_path / "test_simple.fit" t1 = Table(self.data) t1.write(filename, overwrite=True) t2 = Table.read(filename) assert equal_data(t1, t2) def test_simple_meta(self, tmp_path): filename = tmp_path / "test_simple.fits" t1 = Table(self.data) t1.meta["A"] = 1 t1.meta["B"] = 2.3 t1.meta["C"] = "spam" t1.meta["comments"] = ["this", "is", "a", "long", "comment"] t1.meta["HISTORY"] = ["first", "second", "third"] t1.write(filename, overwrite=True) t2 = Table.read(filename) assert equal_data(t1, t2) for key in t1.meta: if isinstance(t1.meta, list): for i in range(len(t1.meta[key])): assert t1.meta[key][i] == t2.meta[key][i] else: assert t1.meta[key] == t2.meta[key] def test_simple_meta_conflicting(self, tmp_path): filename = tmp_path / "test_simple.fits" t1 = Table(self.data) t1.meta["ttype1"] = "spam" with pytest.warns( AstropyUserWarning, match=( "Meta-data keyword ttype1 " "will be ignored since it conflicts with a FITS " "reserved keyword" ), ) as w: t1.write(filename, overwrite=True) assert len(w) == 1 def test_simple_noextension(self, tmp_path): """ Test that file type is recognized without extension """ filename = tmp_path / "test_simple" t1 = Table(self.data) t1.write(filename, overwrite=True, format="fits") t2 = Table.read(filename) assert equal_data(t1, t2) @pytest.mark.parametrize("table_type", (Table, QTable)) def test_with_units(self, table_type, tmp_path): filename = tmp_path / "test_with_units.fits" t1 = table_type(self.data) t1["a"].unit = u.m t1["c"].unit = u.km / u.s t1.write(filename, overwrite=True) t2 = table_type.read(filename) assert equal_data(t1, t2) assert t2["a"].unit == u.m assert t2["c"].unit == u.km / u.s def test_with_custom_units_qtable(self, tmp_path): # Test only for QTable - for Table's Column, new units are dropped # (as is checked in test_write_drop_nonstandard_units). filename = tmp_path / "test_with_units.fits" unit = u.def_unit("bandpass_sol_lum") t = QTable() t["l"] = np.ones(5) * unit with pytest.warns(AstropyUserWarning) as w: t.write(filename, overwrite=True) assert len(w) == 1 assert "bandpass_sol_lum" in str(w[0].message) # Just reading back, the data is fine but the unit is not recognized. with pytest.warns( u.UnitsWarning, match="'bandpass_sol_lum' did not parse" ) as w: t2 = QTable.read(filename) assert len(w) == 1 assert isinstance(t2["l"].unit, u.UnrecognizedUnit) assert str(t2["l"].unit) == "bandpass_sol_lum" assert np.all(t2["l"].value == t["l"].value) # But if we enable the unit, it should be recognized. with u.add_enabled_units(unit): t3 = QTable.read(filename) assert t3["l"].unit is unit assert equal_data(t3, t) # Regression check for #8897; write used to fail when a custom # unit was enabled. with pytest.warns(AstropyUserWarning): t3.write(filename, overwrite=True) # It should also be possible to read the file in using a unit alias, # even to a unit that may not be the same. with u.set_enabled_aliases({"bandpass_sol_lum": u.Lsun}): t3 = QTable.read(filename) assert t3["l"].unit is u.Lsun @pytest.mark.parametrize("table_type", (Table, QTable)) def test_read_with_unit_aliases(self, table_type): hdu = BinTableHDU(self.data) hdu.columns[0].unit = "Angstroms" hdu.columns[2].unit = "ergs/(cm.s.Angstroms)" with u.set_enabled_aliases(dict(Angstroms=u.AA, ergs=u.erg)): t = table_type.read(hdu) assert t["a"].unit == u.AA assert t["c"].unit == u.erg / (u.cm * u.s * u.AA) @pytest.mark.parametrize("table_type", (Table, QTable)) def test_with_format(self, table_type, tmp_path): filename = tmp_path / "test_with_format.fits" t1 = table_type(self.data) t1["a"].format = "{:5d}" t1["b"].format = "{:>20}" t1["c"].format = "{:6.2f}" t1.write(filename, overwrite=True) t2 = table_type.read(filename) assert equal_data(t1, t2) assert t2["a"].format == "{:5d}" assert t2["b"].format == "{:>20}" assert t2["c"].format == "{:6.2f}" def test_masked(self, tmp_path): filename = tmp_path / "test_masked.fits" t1 = Table(self.data, masked=True) t1.mask["a"] = [1, 0, 1, 0] t1.mask["b"] = [1, 0, 0, 1] t1.mask["c"] = [0, 1, 1, 0] t1.write(filename, overwrite=True) t2 = Table.read(filename) assert equal_data(t1, t2) assert np.all(t1["a"].mask == t2["a"].mask) assert np.all(t1["b"].mask == t2["b"].mask) assert np.all(t1["c"].mask == t2["c"].mask) @pytest.mark.parametrize("masked", [True, False]) def test_masked_nan(self, masked, tmp_path): """Check that masked values by default are replaced by NaN. This should work for any shape and be independent of whether the Table is formally masked or not. """ filename = tmp_path / "test_masked_nan.fits" a = np.ma.MaskedArray([5.25, 8.5, 3.75, 6.25], mask=[1, 0, 1, 0]) b = np.ma.MaskedArray([2.5, 4.5, 6.75, 8.875], mask=[1, 0, 0, 1], dtype="f4") c = np.ma.stack([a, b], axis=-1) t1 = Table([a, b, c], names=["a", "b", "c"], masked=masked) t1.write(filename, overwrite=True) t2 = Table.read(filename) assert_array_equal(t2["a"].data, [np.nan, 8.5, np.nan, 6.25]) assert_array_equal(t2["b"].data, [np.nan, 4.5, 6.75, np.nan]) assert_array_equal( t2["c"].data, np.stack([t2["a"].data, t2["b"].data], axis=-1) ) assert np.all(t1["a"].mask == t2["a"].mask) assert np.all(t1["b"].mask == t2["b"].mask) assert np.all(t1["c"].mask == t2["c"].mask) def test_masked_serialize_data_mask(self, tmp_path): filename = tmp_path / "test_masked_nan.fits" a = np.ma.MaskedArray([5.25, 8.5, 3.75, 6.25], mask=[1, 0, 1, 0]) b = np.ma.MaskedArray([2.5, 4.5, 6.75, 8.875], mask=[1, 0, 0, 1]) c = np.ma.stack([a, b], axis=-1) t1 = Table([a, b, c], names=["a", "b", "c"]) t1.write(filename, overwrite=True) t2 = Table.read(filename) assert_array_equal(t2["a"].data, [5.25, 8.5, 3.75, 6.25]) assert_array_equal(t2["b"].data, [2.5, 4.5, 6.75, 8.875]) assert_array_equal( t2["c"].data, np.stack([t2["a"].data, t2["b"].data], axis=-1) ) assert np.all(t1["a"].mask == t2["a"].mask) assert np.all(t1["b"].mask == t2["b"].mask) assert np.all(t1["c"].mask == t2["c"].mask) def test_read_from_fileobj(self, tmp_path): filename = tmp_path / "test_read_from_fileobj.fits" hdu = BinTableHDU(self.data) hdu.writeto(filename, overwrite=True) with open(filename, "rb") as f: t = Table.read(f) assert equal_data(t, self.data) def test_read_with_nonstandard_units(self): hdu = BinTableHDU(self.data) hdu.columns[0].unit = "RADIANS" hdu.columns[1].unit = "spam" hdu.columns[2].unit = "millieggs" with pytest.warns(u.UnitsWarning, match="did not parse as fits unit"): t = Table.read(hdu) assert equal_data(t, self.data) @pytest.mark.parametrize("table_type", (Table, QTable)) def test_write_drop_nonstandard_units(self, table_type, tmp_path): # While we are generous on input (see above), we are strict on # output, dropping units not recognized by the fits standard. filename = tmp_path / "test_nonstandard_units.fits" spam = u.def_unit("spam") t = table_type() t["a"] = [1.0, 2.0, 3.0] * spam with pytest.warns(AstropyUserWarning, match="spam") as w: t.write(filename) assert len(w) == 1 if table_type is Table: assert "cannot be recovered in reading. " in str(w[0].message) else: assert "lost to non-astropy fits readers" in str(w[0].message) with fits.open(filename) as ff: hdu = ff[1] assert "TUNIT1" not in hdu.header def test_memmap(self, tmp_path): filename = tmp_path / "test_simple.fts" t1 = Table(self.data) t1.write(filename, overwrite=True) t2 = Table.read(filename, memmap=False) t3 = Table.read(filename, memmap=True) assert equal_data(t2, t3) # To avoid issues with --open-files, we need to remove references to # data that uses memory mapping and force the garbage collection del t1, t2, t3 gc.collect() @pytest.mark.parametrize("memmap", (False, True)) def test_character_as_bytes(self, tmp_path, memmap): filename = tmp_path / "test_simple.fts" t1 = Table(self.data) t1.write(filename, overwrite=True) t2 = Table.read(filename, character_as_bytes=False, memmap=memmap) t3 = Table.read(filename, character_as_bytes=True, memmap=memmap) assert t2["b"].dtype.kind == "U" assert t3["b"].dtype.kind == "S" assert equal_data(t2, t3) # To avoid issues with --open-files, we need to remove references to # data that uses memory mapping and force the garbage collection del t1, t2, t3 gc.collect() def test_oned_single_element(self, tmp_path): filename = tmp_path / "test_oned_single_element.fits" table = Table({"x": [[1], [2]]}) table.write(filename, overwrite=True) read = Table.read(filename) assert read["x"].shape == (2, 1) assert len(read["x"][0]) == 1 def test_write_append(self, tmp_path): t = Table(self.data) hdu = table_to_hdu(t) def check_equal(filename, expected, start_from=1): with fits.open(filename) as hdu_list: assert len(hdu_list) == expected for hdu_table in hdu_list[start_from:]: assert hdu_table.header == hdu.header assert np.all(hdu_table.data == hdu.data) filename = tmp_path / "test_write_append.fits" t.write(filename, append=True) t.write(filename, append=True) check_equal(filename, 3) # Check the overwrite works correctly. t.write(filename, append=True, overwrite=True) t.write(filename, append=True) check_equal(filename, 3) # Normal write, check it's not appending. t.write(filename, overwrite=True) t.write(filename, overwrite=True) check_equal(filename, 2) # Now write followed by append, with different shaped tables. t2 = Table(np.array([1, 2])) t2.write(filename, overwrite=True) t.write(filename, append=True) check_equal(filename, 3, start_from=2) assert equal_data(t2, Table.read(filename, hdu=1)) def test_write_overwrite(self, tmp_path): t = Table(self.data) filename = tmp_path / "test_write_overwrite.fits" t.write(filename) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): t.write(filename) t.write(filename, overwrite=True) def test_mask_nans_on_read(self, tmp_path): filename = tmp_path / "test_inexact_format_parse_on_read.fits" c1 = fits.Column(name="a", array=np.array([1, 2, np.nan]), format="E") table_hdu = fits.TableHDU.from_columns([c1]) table_hdu.writeto(filename) tab = Table.read(filename) assert any(tab.mask) assert tab.mask[2] tab = Table.read(filename, mask_invalid=False) assert tab.mask is None # using memmap also deactivate the masking tab = Table.read(filename, memmap=True) assert tab.mask is None def test_mask_null_on_read(self, tmp_path): filename = tmp_path / "test_null_format_parse_on_read.fits" col = fits.Column( name="a", array=np.array([1, 2, 99, 60000], dtype="u2"), format="I", null=99, bzero=32768, ) bin_table_hdu = fits.BinTableHDU.from_columns([col]) bin_table_hdu.writeto(filename, overwrite=True) tab = Table.read(filename) assert any(tab.mask) assert tab.mask[2] def test_mask_str_on_read(self, tmp_path): filename = tmp_path / "test_null_format_parse_on_read.fits" col = fits.Column( name="a", array=np.array([b"foo", b"bar", b""], dtype="|S3"), format="A3" ) bin_table_hdu = fits.BinTableHDU.from_columns([col]) bin_table_hdu.writeto(filename, overwrite=True) tab = Table.read(filename) assert any(tab.mask) assert tab.mask[2] tab = Table.read(filename, mask_invalid=False) assert tab.mask is None class TestMultipleHDU: def setup_class(self): self.data1 = np.array( list(zip([1, 2, 3, 4], ["a", "b", "c", "d"], [2.3, 4.5, 6.7, 8.9])), dtype=[("a", int), ("b", "U1"), ("c", float)], ) self.data2 = np.array( list(zip([1.4, 2.3, 3.2, 4.7], [2.3, 4.5, 6.7, 8.9])), dtype=[("p", float), ("q", float)], ) self.data3 = np.array( list(zip([1, 2, 3, 4], [2.3, 4.5, 6.7, 8.9])), dtype=[("A", int), ("B", float)], ) hdu0 = PrimaryHDU() hdu1 = BinTableHDU(self.data1, name="first") hdu2 = BinTableHDU(self.data2, name="second") hdu3 = ImageHDU(np.ones((3, 3)), name="third") hdu4 = BinTableHDU(self.data3) self.hdus = HDUList([hdu0, hdu1, hdu2, hdu3, hdu4]) self.hdusb = HDUList([hdu0, hdu3, hdu2, hdu1]) self.hdus3 = HDUList([hdu0, hdu3, hdu2]) self.hdus2 = HDUList([hdu0, hdu1, hdu3]) self.hdus1 = HDUList([hdu0, hdu1]) def teardown_class(self): del self.hdus def setup_method(self, method): warnings.filterwarnings("always") def test_read(self, tmp_path): filename = tmp_path / "test_read.fits" self.hdus.writeto(filename) with pytest.warns( AstropyUserWarning, match=r"hdu= was not specified but multiple tables " r"are present, reading in first available " r"table \(hdu=1\)", ): t = Table.read(filename) assert equal_data(t, self.data1) filename = tmp_path / "test_read_2.fits" self.hdusb.writeto(filename) with pytest.warns( AstropyUserWarning, match=r"hdu= was not specified but multiple tables " r"are present, reading in first available " r"table \(hdu=2\)", ): t3 = Table.read(filename) assert equal_data(t3, self.data2) def test_read_with_hdu_0(self, tmp_path): filename = tmp_path / "test_read_with_hdu_0.fits" self.hdus.writeto(filename) with pytest.raises(ValueError) as exc: Table.read(filename, hdu=0) assert exc.value.args[0] == "No table found in hdu=0" @pytest.mark.parametrize("hdu", [1, "first"]) def test_read_with_hdu_1(self, tmp_path, hdu): filename = tmp_path / "test_read_with_hdu_1.fits" self.hdus.writeto(filename) t = Table.read(filename, hdu=hdu) assert equal_data(t, self.data1) @pytest.mark.parametrize("hdu", [2, "second"]) def test_read_with_hdu_2(self, tmp_path, hdu): filename = tmp_path / "test_read_with_hdu_2.fits" self.hdus.writeto(filename) t = Table.read(filename, hdu=hdu) assert equal_data(t, self.data2) @pytest.mark.parametrize("hdu", [3, "third"]) def test_read_with_hdu_3(self, tmp_path, hdu): filename = tmp_path / "test_read_with_hdu_3.fits" self.hdus.writeto(filename) with pytest.raises(ValueError, match="No table found in hdu=3"): Table.read(filename, hdu=hdu) def test_read_with_hdu_4(self, tmp_path): filename = tmp_path / "test_read_with_hdu_4.fits" self.hdus.writeto(filename) t = Table.read(filename, hdu=4) assert equal_data(t, self.data3) @pytest.mark.parametrize("hdu", [2, 3, "1", "second", ""]) def test_read_with_hdu_missing(self, tmp_path, hdu): filename = tmp_path / "test_warn_with_hdu_1.fits" self.hdus1.writeto(filename) with pytest.warns( AstropyDeprecationWarning, match=rf"Specified hdu={hdu} not found, " r"reading in first available table \(hdu=1\)", ): t1 = Table.read(filename, hdu=hdu) assert equal_data(t1, self.data1) @pytest.mark.parametrize("hdu", [0, 2, "third"]) def test_read_with_hdu_warning(self, tmp_path, hdu): filename = tmp_path / "test_warn_with_hdu_2.fits" self.hdus2.writeto(filename) with pytest.warns( AstropyDeprecationWarning, match=rf"No table found in specified hdu={hdu}, " r"reading in first available table \(hdu=1\)", ): t2 = Table.read(filename, hdu=hdu) assert equal_data(t2, self.data1) @pytest.mark.parametrize("hdu", [0, 1, "third"]) def test_read_in_last_hdu(self, tmp_path, hdu): filename = tmp_path / "test_warn_with_hdu_3.fits" self.hdus3.writeto(filename) with pytest.warns( AstropyDeprecationWarning, match=rf"No table found in specified hdu={hdu}, " r"reading in first available table \(hdu=2\)", ): t3 = Table.read(filename, hdu=hdu) assert equal_data(t3, self.data2) def test_read_from_hdulist(self): with pytest.warns( AstropyUserWarning, match=r"hdu= was not specified but multiple tables " r"are present, reading in first available " r"table \(hdu=1\)", ): t = Table.read(self.hdus) assert equal_data(t, self.data1) with pytest.warns( AstropyUserWarning, match=r"hdu= was not specified but multiple tables " r"are present, reading in first available " r"table \(hdu=2\)", ): t3 = Table.read(self.hdusb) assert equal_data(t3, self.data2) def test_read_from_hdulist_with_hdu_0(self): with pytest.raises(ValueError) as exc: Table.read(self.hdus, hdu=0) assert exc.value.args[0] == "No table found in hdu=0" @pytest.mark.parametrize("hdu", [1, "first", None]) def test_read_from_hdulist_with_single_table(self, hdu): t = Table.read(self.hdus1, hdu=hdu) assert equal_data(t, self.data1) @pytest.mark.parametrize("hdu", [1, "first"]) def test_read_from_hdulist_with_hdu_1(self, hdu): t = Table.read(self.hdus, hdu=hdu) assert equal_data(t, self.data1) @pytest.mark.parametrize("hdu", [2, "second"]) def test_read_from_hdulist_with_hdu_2(self, hdu): t = Table.read(self.hdus, hdu=hdu) assert equal_data(t, self.data2) @pytest.mark.parametrize("hdu", [3, "third"]) def test_read_from_hdulist_with_hdu_3(self, hdu): with pytest.raises(ValueError, match="No table found in hdu=3"): Table.read(self.hdus, hdu=hdu) @pytest.mark.parametrize("hdu", [0, 2, "third"]) def test_read_from_hdulist_with_hdu_warning(self, hdu): with pytest.warns( AstropyDeprecationWarning, match=rf"No table found in specified hdu={hdu}, " r"reading in first available table \(hdu=1\)", ): t2 = Table.read(self.hdus2, hdu=hdu) assert equal_data(t2, self.data1) @pytest.mark.parametrize("hdu", [2, 3, "1", "second", ""]) def test_read_from_hdulist_with_hdu_missing(self, hdu): with pytest.warns( AstropyDeprecationWarning, match=rf"Specified hdu={hdu} not found, " r"reading in first available table \(hdu=1\)", ): t1 = Table.read(self.hdus1, hdu=hdu) assert equal_data(t1, self.data1) @pytest.mark.parametrize("hdu", [0, 1, "third"]) def test_read_from_hdulist_in_last_hdu(self, hdu): with pytest.warns( AstropyDeprecationWarning, match=rf"No table found in specified hdu={hdu}, " r"reading in first available table \(hdu=2\)", ): t3 = Table.read(self.hdus3, hdu=hdu) assert equal_data(t3, self.data2) @pytest.mark.parametrize("hdu", [None, 1, "first"]) def test_read_from_single_hdu(self, hdu): t = Table.read(self.hdus[1]) assert equal_data(t, self.data1) def test_masking_regression_1795(): """ Regression test for #1795 - this bug originally caused columns where TNULL was not defined to have their first element masked. """ t = Table.read(get_pkg_data_filename("data/tb.fits")) assert np.all(t["c1"].mask == np.array([False, False])) assert not hasattr(t["c2"], "mask") assert not hasattr(t["c3"], "mask") assert not hasattr(t["c4"], "mask") assert np.all(t["c1"].data == np.array([1, 2])) assert np.all(t["c2"].data == np.array([b"abc", b"xy "])) assert_allclose(t["c3"].data, np.array([3.70000007153, 6.6999997139])) assert np.all(t["c4"].data == np.array([False, True])) def test_scale_error(): a = [1, 4, 5] b = [2.0, 5.0, 8.2] c = ["x", "y", "z"] t = Table([a, b, c], names=("a", "b", "c"), meta={"name": "first table"}) t["a"].unit = "1.2" with pytest.raises( UnitScaleError, match=r"The column 'a' could not be " r"stored in FITS format because it has a scale '\(1\.2\)'" r" that is not recognized by the FITS standard\. Either " r"scale the data or change the units\.", ): t.write("t.fits", format="fits", overwrite=True) @pytest.mark.parametrize( "tdisp_str, format_return", [ ("EN10.5", ("EN", "10", "5", None)), ("F6.2", ("F", "6", "2", None)), ("B5.10", ("B", "5", "10", None)), ("E10.5E3", ("E", "10", "5", "3")), ("A21", ("A", "21", None, None)), ], ) def test_parse_tdisp_format(tdisp_str, format_return): assert _parse_tdisp_format(tdisp_str) == format_return @pytest.mark.parametrize( "tdisp_str, format_str_return", [ ("G15.4E2", "{:15.4g}"), ("Z5.10", "{:5x}"), ("I6.5", "{:6d}"), ("L8", "{:>8}"), ("E20.7", "{:20.7e}"), ], ) def test_fortran_to_python_format(tdisp_str, format_str_return): assert _fortran_to_python_format(tdisp_str) == format_str_return @pytest.mark.parametrize( "fmt_str, tdisp_str", [ ("{:3d}", "I3"), ("3d", "I3"), ("7.3f", "F7.3"), ("{:>4}", "A4"), ("{:7.4f}", "F7.4"), ("%5.3g", "G5.3"), ("%10s", "A10"), ("%.4f", "F13.4"), ], ) def test_python_to_tdisp(fmt_str, tdisp_str): assert python_to_tdisp(fmt_str) == tdisp_str def test_logical_python_to_tdisp(): assert python_to_tdisp("{:>7}", logical_dtype=True) == "L7" def test_bool_column(tmp_path): """ Regression test for https://github.com/astropy/astropy/issues/1953 Ensures that Table columns of bools are properly written to a FITS table. """ arr = np.ones(5, dtype=bool) arr[::2] == np.False_ t = Table([arr]) t.write(tmp_path / "test.fits", overwrite=True) with fits.open(tmp_path / "test.fits") as hdul: assert hdul[1].data["col0"].dtype == np.dtype("bool") assert np.all(hdul[1].data["col0"] == arr) def test_unicode_column(tmp_path): """ Test that a column of unicode strings is still written as one byte-per-character in the FITS table (so long as the column can be ASCII encoded). Regression test for one of the issues fixed in https://github.com/astropy/astropy/pull/4228 """ t = Table([np.array(["a", "b", "cd"])]) t.write(tmp_path / "test.fits", overwrite=True) with fits.open(tmp_path / "test.fits") as hdul: assert np.all(hdul[1].data["col0"] == ["a", "b", "cd"]) assert hdul[1].header["TFORM1"] == "2A" t2 = Table([np.array(["\N{SNOWMAN}"])]) with pytest.raises(UnicodeEncodeError): t2.write(tmp_path / "test.fits", overwrite=True) def test_unit_warnings_read_write(tmp_path): filename = tmp_path / "test_unit.fits" t1 = Table([[1, 2], [3, 4]], names=["a", "b"]) t1["a"].unit = "m/s" t1["b"].unit = "not-a-unit" with pytest.warns( u.UnitsWarning, match="'not-a-unit' did not parse as fits unit" ) as w: t1.write(filename, overwrite=True) assert len(w) == 1 with pytest.warns( u.UnitsWarning, match="'not-a-unit' did not parse as fits unit" ) as w: Table.read(filename, hdu=1) def test_convert_comment_convention(): """ Regression test for https://github.com/astropy/astropy/issues/6079 """ filename = get_pkg_data_filename("data/stddata.fits") with pytest.warns( AstropyUserWarning, match=r"hdu= was not specified but " r"multiple tables are present", ): t = Table.read(filename) assert t.meta["comments"] == [ "", " *** End of mandatory fields ***", "", "", " *** Column names ***", "", "", " *** Column formats ***", "", ] def assert_objects_equal(obj1, obj2, attrs, compare_class=True): if compare_class: assert obj1.__class__ is obj2.__class__ info_attrs = [ "info.name", "info.format", "info.unit", "info.description", "info.meta", "info.dtype", ] for attr in attrs + info_attrs: a1 = obj1 a2 = obj2 for subattr in attr.split("."): try: a1 = getattr(a1, subattr) a2 = getattr(a2, subattr) except AttributeError: a1 = a1[subattr] a2 = a2[subattr] # Mixin info.meta can None instead of empty OrderedDict(), #6720 would # fix this. if attr == "info.meta": if a1 is None: a1 = {} if a2 is None: a2 = {} if isinstance(a1, np.ndarray) and a1.dtype.kind == "f": assert quantity_allclose(a1, a2, rtol=1e-15) elif isinstance(a1, np.dtype): # FITS does not perfectly preserve dtype: byte order can change, and # unicode gets stored as bytes. So, we just check safe casting, to # ensure we do not, e.g., accidentally change integer to float, etc. if NUMPY_LT_1_22 and a1.names: # For old numpy, can_cast does not deal well with structured dtype. assert a1.names == a2.names else: assert np.can_cast(a2, a1, casting="safe") else: assert np.all(a1 == a2) def test_fits_mixins_qtable_to_table(tmp_path): """Test writing as QTable and reading as Table. Ensure correct classes come out. """ filename = tmp_path / "test_simple.fits" names = sorted(mixin_cols) t = QTable([mixin_cols[name] for name in names], names=names) t.write(filename, format="fits") t2 = Table.read(filename, format="fits", astropy_native=True) assert t.colnames == t2.colnames for name, col in t.columns.items(): col2 = t2[name] # Special-case Time, which does not yet support round-tripping # the format. if isinstance(col2, Time): col2.format = col.format attrs = compare_attrs[name] compare_class = True if isinstance(col.info, QuantityInfo): # Downgrade Quantity to Column + unit assert type(col2) is Column # Class-specific attributes like `value` or `wrap_angle` are lost. attrs = ["unit"] compare_class = False # Compare data values here (assert_objects_equal doesn't know how in this case) assert np.all(col.value == col2) assert_objects_equal(col, col2, attrs, compare_class) @pytest.mark.parametrize("table_cls", (Table, QTable)) def test_fits_mixins_as_one(table_cls, tmp_path): """Test write/read all cols at once and validate intermediate column names""" filename = tmp_path / "test_simple.fits" names = sorted(mixin_cols) # FITS stores times directly, so we just get the column back. all_serialized_names = [] for name in sorted(mixin_cols): all_serialized_names.extend( [name] if isinstance(mixin_cols[name], Time) else serialized_names[name] ) t = table_cls([mixin_cols[name] for name in names], names=names) t.meta["C"] = "spam" t.meta["comments"] = ["this", "is", "a", "comment"] t.meta["history"] = ["first", "second", "third"] t.write(filename, format="fits") t2 = table_cls.read(filename, format="fits", astropy_native=True) assert t2.meta["C"] == "spam" assert t2.meta["comments"] == ["this", "is", "a", "comment"] assert t2.meta["HISTORY"] == ["first", "second", "third"] assert t.colnames == t2.colnames # Read directly via fits and confirm column names with fits.open(filename) as hdus: assert hdus[1].columns.names == all_serialized_names @pytest.mark.parametrize("name_col", list(mixin_cols.items())) @pytest.mark.parametrize("table_cls", (Table, QTable)) def test_fits_mixins_per_column(table_cls, name_col, tmp_path): """Test write/read one col at a time and do detailed validation""" filename = tmp_path / "test_simple.fits" name, col = name_col c = [1.0, 2.0] t = table_cls([c, col, c], names=["c1", name, "c2"]) t[name].info.description = "my \n\n\n description" t[name].info.meta = {"list": list(range(50)), "dict": {"a": "b" * 200}} if not t.has_mixin_columns: pytest.skip("column is not a mixin (e.g. Quantity subclass in Table)") t.write(filename, format="fits") t2 = table_cls.read(filename, format="fits", astropy_native=True) if isinstance(col, Time): # FITS Time does not preserve format t2[name].format = col.format assert t.colnames == t2.colnames for colname in t.colnames: compare = ["data"] if colname in ("c1", "c2") else compare_attrs[colname] assert_objects_equal(t[colname], t2[colname], compare) # Special case to make sure Column type doesn't leak into Time class data if name.startswith("tm"): assert t2[name]._time.jd1.__class__ is np.ndarray assert t2[name]._time.jd2.__class__ is np.ndarray @pytest.mark.parametrize("name_col", unsupported_cols.items()) @pytest.mark.xfail(reason="column type unsupported") def test_fits_unsupported_mixin(self, name_col, tmp_path): # Check that we actually fail in writing unsupported columns defined # on top. filename = tmp_path / "test_simple.fits" name, col = name_col Table([col], names=[name]).write(filename, format="fits") def test_info_attributes_with_no_mixins(tmp_path): """Even if there are no mixin columns, if there is metadata that would be lost it still gets serialized """ filename = tmp_path / "test.fits" t = Table([[1.0, 2.0]]) t["col0"].description = "hello" * 40 t["col0"].format = "{:8.4f}" t["col0"].meta["a"] = {"b": "c"} t.write(filename, overwrite=True) t2 = Table.read(filename) assert t2["col0"].description == "hello" * 40 assert t2["col0"].format == "{:8.4f}" assert t2["col0"].meta["a"] == {"b": "c"} @pytest.mark.parametrize("method", ["set_cols", "names", "class"]) def test_round_trip_masked_table_serialize_mask(tmp_path, method): """ Same as previous test but set the serialize_method to 'data_mask' so mask is written out and the behavior is all correct. """ filename = tmp_path / "test.fits" t = simple_table(masked=True) # int, float, and str cols with one masked element # MaskedColumn but no masked elements. See table the MaskedColumnInfo class # _represent_as_dict() method for info about we test a column with no masked elements. t["d"] = [1, 2, 3] if method == "set_cols": for col in t.itercols(): col.info.serialize_method["fits"] = "data_mask" t.write(filename) elif method == "names": t.write( filename, serialize_method={ "a": "data_mask", "b": "data_mask", "c": "data_mask", "d": "data_mask", }, ) elif method == "class": t.write(filename, serialize_method="data_mask") t2 = Table.read(filename) assert t2.masked is False assert t2.colnames == t.colnames for name in t2.colnames: assert np.all(t2[name].mask == t[name].mask) assert np.all(t2[name] == t[name]) # Data under the mask round-trips also (unmask data to show this). t[name].mask = False t2[name].mask = False assert np.all(t2[name] == t[name]) def test_meta_not_modified(tmp_path): filename = tmp_path / "test.fits" t = Table(data=[Column([1, 2], "a", description="spam")]) t.meta["comments"] = ["a", "b"] assert len(t.meta) == 1 t.write(filename) assert len(t.meta) == 1 assert t.meta["comments"] == ["a", "b"]
db0ae7b51a5b9646cbd8dc578df19bb9c7db186e0cb23dcf5adbb60632abb98b
# Licensed under a 3-clause BSD style license - see PYFITS.rst import errno import gzip import io import mmap import os import pathlib import shutil import sys import urllib.request import zipfile from unittest.mock import patch import numpy as np import pytest from astropy.io import fits from astropy.io.fits.convenience import _getext from astropy.io.fits.diff import FITSDiff from astropy.io.fits.file import GZIP_MAGIC, _File from astropy.io.tests import safeio from astropy.utils import data # NOTE: Python can be built without bz2. from astropy.utils.compat.optional_deps import HAS_BZ2 from astropy.utils.data import conf from astropy.utils.exceptions import AstropyUserWarning from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH from .conftest import FitsTestCase if HAS_BZ2: import bz2 class TestCore(FitsTestCase): def test_missing_file(self): with pytest.raises(OSError): fits.open(self.temp("does-not-exist.fits")) def test_naxisj_check(self): with fits.open(self.data("o4sp040b0_raw.fits")) as hdulist: hdulist[1].header["NAXIS3"] = 500 assert "NAXIS3" in hdulist[1].header hdulist.verify("silentfix") assert "NAXIS3" not in hdulist[1].header def test_byteswap(self): p = fits.PrimaryHDU() lst = fits.HDUList() n = np.array([1, 60000, 0], dtype="u2").astype("i2") c = fits.Column(name="foo", format="i2", bscale=1, bzero=32768, array=n) t = fits.BinTableHDU.from_columns([c]) lst.append(p) lst.append(t) lst.writeto(self.temp("test.fits"), overwrite=True) with fits.open(self.temp("test.fits")) as p: assert p[1].data[1]["foo"] == 60000.0 def test_fits_file_path_object(self): """ Testing when fits file is passed as pathlib.Path object #4412. """ fpath = pathlib.Path(self.data("tdim.fits")) with fits.open(fpath) as hdulist: assert hdulist[0].filebytes() == 2880 assert hdulist[1].filebytes() == 5760 with fits.open(self.data("tdim.fits")) as hdulist2: assert FITSDiff(hdulist2, hdulist).identical is True def test_fits_pathlike_object(self): """ Testing when fits file is passed as os.PathLike object #11579. """ class TPath(os.PathLike): def __init__(self, path): self.path = path def __fspath__(self): return str(self.path) fpath = TPath(self.data("tdim.fits")) with fits.open(fpath) as hdulist: assert hdulist[0].filebytes() == 2880 assert hdulist[1].filebytes() == 5760 with fits.open(self.data("tdim.fits")) as hdulist2: assert FITSDiff(hdulist2, hdulist).identical is True def test_fits_file_bytes_object(self): """ Testing when fits file is passed as bytes. """ with fits.open(self.data("tdim.fits").encode()) as hdulist: assert hdulist[0].filebytes() == 2880 assert hdulist[1].filebytes() == 5760 with fits.open(self.data("tdim.fits")) as hdulist2: assert FITSDiff(hdulist2, hdulist).identical is True def test_add_del_columns(self): p = fits.ColDefs([]) p.add_col(fits.Column(name="FOO", format="3J")) p.add_col(fits.Column(name="BAR", format="1I")) assert p.names == ["FOO", "BAR"] p.del_col("FOO") assert p.names == ["BAR"] def test_add_del_columns2(self): hdulist = fits.open(self.data("tb.fits")) table = hdulist[1] assert table.data.dtype.names == ("c1", "c2", "c3", "c4") assert table.columns.names == ["c1", "c2", "c3", "c4"] table.columns.del_col("c1") assert table.data.dtype.names == ("c2", "c3", "c4") assert table.columns.names == ["c2", "c3", "c4"] table.columns.del_col("c3") assert table.data.dtype.names == ("c2", "c4") assert table.columns.names == ["c2", "c4"] table.columns.add_col(fits.Column("foo", "3J")) assert table.data.dtype.names == ("c2", "c4", "foo") assert table.columns.names == ["c2", "c4", "foo"] hdulist.writeto(self.temp("test.fits"), overwrite=True) hdulist.close() # NOTE: If you see a warning, might be related to # https://github.com/spacetelescope/PyFITS/issues/44 with fits.open(self.temp("test.fits")) as hdulist: table = hdulist[1] assert table.data.dtype.names == ("c2", "c4", "foo") assert table.columns.names == ["c2", "c4", "foo"] def test_update_header_card(self): """A very basic test for the Header.update method--I'd like to add a few more cases to this at some point. """ header = fits.Header() comment = "number of bits per data pixel" header["BITPIX"] = (16, comment) assert "BITPIX" in header assert header["BITPIX"] == 16 assert header.comments["BITPIX"] == comment header.update(BITPIX=32) assert header["BITPIX"] == 32 assert header.comments["BITPIX"] == "" def test_set_card_value(self): """Similar to test_update_header_card(), but tests the the `header['FOO'] = 'bar'` method of updating card values. """ header = fits.Header() comment = "number of bits per data pixel" card = fits.Card.fromstring(f"BITPIX = 32 / {comment}") header.append(card) header["BITPIX"] = 32 assert "BITPIX" in header assert header["BITPIX"] == 32 assert header.cards[0].keyword == "BITPIX" assert header.cards[0].value == 32 assert header.cards[0].comment == comment def test_uint(self): filename = self.data("o4sp040b0_raw.fits") with fits.open(filename, uint=False) as hdulist_f: with fits.open(filename, uint=True) as hdulist_i: assert hdulist_f[1].data.dtype == np.float32 assert hdulist_i[1].data.dtype == np.uint16 assert np.all(hdulist_f[1].data == hdulist_i[1].data) def test_fix_missing_card_append(self): hdu = fits.ImageHDU() errs = hdu.req_cards("TESTKW", None, None, "foo", "silentfix", []) assert len(errs) == 1 assert "TESTKW" in hdu.header assert hdu.header["TESTKW"] == "foo" assert hdu.header.cards[-1].keyword == "TESTKW" def test_fix_invalid_keyword_value(self): hdu = fits.ImageHDU() hdu.header["TESTKW"] = "foo" errs = hdu.req_cards("TESTKW", None, lambda v: v == "foo", "foo", "ignore", []) assert len(errs) == 0 # Now try a test that will fail, and ensure that an error will be # raised in 'exception' mode errs = hdu.req_cards( "TESTKW", None, lambda v: v == "bar", "bar", "exception", [] ) assert len(errs) == 1 assert errs[0][1] == "'TESTKW' card has invalid value 'foo'." # See if fixing will work hdu.req_cards("TESTKW", None, lambda v: v == "bar", "bar", "silentfix", []) assert hdu.header["TESTKW"] == "bar" def test_unfixable_missing_card(self): class TestHDU(fits.hdu.base.NonstandardExtHDU): def _verify(self, option="warn"): errs = super()._verify(option) hdu.req_cards("TESTKW", None, None, None, "fix", errs) return errs @classmethod def match_header(cls, header): # Since creating this HDU class adds it to the registry we # don't want the file reader to possibly think any actual # HDU from a file should be handled by this class return False hdu = TestHDU(header=fits.Header()) with pytest.raises(fits.VerifyError): hdu.verify("fix") def test_exception_on_verification_error(self): hdu = fits.ImageHDU() del hdu.header["XTENSION"] with pytest.raises(fits.VerifyError): hdu.verify("exception") def test_ignore_verification_error(self): hdu = fits.ImageHDU() del hdu.header["NAXIS"] # The default here would be to issue a warning; ensure that no warnings # or exceptions are raised hdu.verify("ignore") # Make sure the error wasn't fixed either, silently or otherwise assert "NAXIS" not in hdu.header def test_unrecognized_verify_option(self): hdu = fits.ImageHDU() with pytest.raises(ValueError): hdu.verify("foobarbaz") def test_errlist_basic(self): # Just some tests to make sure that _ErrList is setup correctly. # No arguments error_list = fits.verify._ErrList() assert error_list == [] # Some contents - this is not actually working, it just makes sure they # are kept. error_list = fits.verify._ErrList([1, 2, 3]) assert error_list == [1, 2, 3] def test_combined_verify_options(self): """ Test verify options like fix+ignore. """ def make_invalid_hdu(): hdu = fits.ImageHDU() # Add one keyword to the header that contains a fixable defect, and one # with an unfixable defect c1 = fits.Card.fromstring("test = ' test'") c2 = fits.Card.fromstring("P.I. = ' Hubble'") hdu.header.append(c1) hdu.header.append(c2) return hdu # silentfix+ignore should be completely silent hdu = make_invalid_hdu() hdu.verify("silentfix+ignore") # silentfix+warn should be quiet about the fixed HDU and only warn # about the unfixable one hdu = make_invalid_hdu() with pytest.warns(AstropyUserWarning, match="Illegal keyword name") as w: hdu.verify("silentfix+warn") assert len(w) == 4 # silentfix+exception should only mention the unfixable error in the # exception hdu = make_invalid_hdu() with pytest.raises(fits.VerifyError, match=r"Illegal keyword name") as excinfo: hdu.verify("silentfix+exception") assert "not upper case" not in str(excinfo.value) # fix+ignore is not too useful, but it should warn about the fixed # problems while saying nothing about the unfixable problems hdu = make_invalid_hdu() with pytest.warns(AstropyUserWarning, match="not upper case") as w: hdu.verify("fix+ignore") assert len(w) == 4 # fix+warn hdu = make_invalid_hdu() with pytest.warns(AstropyUserWarning) as w: hdu.verify("fix+warn") assert len(w) == 6 assert "not upper case" in str(w[2].message) assert "Illegal keyword name" in str(w[4].message) # fix+exception hdu = make_invalid_hdu() with pytest.raises(fits.VerifyError, match=r"Illegal keyword name") as excinfo: hdu.verify("fix+exception") assert "not upper case" in str(excinfo.value) def test_getext(self): """ Test the various different ways of specifying an extension header in the convenience functions. """ filename = self.data("test0.fits") hl, ext = _getext(filename, "readonly", 1) assert ext == 1 hl.close() pytest.raises(ValueError, _getext, filename, "readonly", 1, 2) pytest.raises(ValueError, _getext, filename, "readonly", (1, 2)) pytest.raises(ValueError, _getext, filename, "readonly", "sci", "sci") pytest.raises(TypeError, _getext, filename, "readonly", 1, 2, 3) hl, ext = _getext(filename, "readonly", ext=1) assert ext == 1 hl.close() hl, ext = _getext(filename, "readonly", ext=("sci", 2)) assert ext == ("sci", 2) hl.close() pytest.raises( TypeError, _getext, filename, "readonly", 1, ext=("sci", 2), extver=3 ) pytest.raises( TypeError, _getext, filename, "readonly", ext=("sci", 2), extver=3 ) hl, ext = _getext(filename, "readonly", "sci") assert ext == ("sci", 1) hl.close() hl, ext = _getext(filename, "readonly", "sci", 1) assert ext == ("sci", 1) hl.close() hl, ext = _getext(filename, "readonly", ("sci", 1)) assert ext == ("sci", 1) hl.close() hl, ext = _getext( filename, "readonly", "sci", extver=1, do_not_scale_image_data=True ) assert ext == ("sci", 1) hl.close() pytest.raises(TypeError, _getext, filename, "readonly", "sci", ext=1) pytest.raises(TypeError, _getext, filename, "readonly", "sci", 1, extver=2) hl, ext = _getext(filename, "readonly", extname="sci") assert ext == ("sci", 1) hl.close() hl, ext = _getext(filename, "readonly", extname="sci", extver=1) assert ext == ("sci", 1) hl.close() pytest.raises(TypeError, _getext, filename, "readonly", extver=1) def test_extension_name_case_sensitive(self): """ Tests that setting fits.conf.extension_name_case_sensitive at runtime works. """ hdu = fits.ImageHDU() hdu.name = "sCi" assert hdu.name == "SCI" assert hdu.header["EXTNAME"] == "SCI" with fits.conf.set_temp("extension_name_case_sensitive", True): hdu = fits.ImageHDU() hdu.name = "sCi" assert hdu.name == "sCi" assert hdu.header["EXTNAME"] == "sCi" hdu.name = "sCi" assert hdu.name == "SCI" assert hdu.header["EXTNAME"] == "SCI" def test_hdu_fromstring(self): """ Tests creating a fully-formed HDU object from a string containing the bytes of the HDU. """ infile = self.data("test0.fits") outfile = self.temp("test.fits") with open(infile, "rb") as fin: dat = fin.read() offset = 0 with fits.open(infile) as hdul: hdulen = hdul[0]._data_offset + hdul[0]._data_size hdu = fits.PrimaryHDU.fromstring(dat[:hdulen]) assert isinstance(hdu, fits.PrimaryHDU) assert hdul[0].header == hdu.header assert hdu.data is None hdu.header["TEST"] = "TEST" hdu.writeto(outfile) with fits.open(outfile) as hdul: assert isinstance(hdu, fits.PrimaryHDU) assert hdul[0].header[:-1] == hdu.header[:-1] assert hdul[0].header["TEST"] == "TEST" assert hdu.data is None with fits.open(infile) as hdul: for ext_hdu in hdul[1:]: offset += hdulen hdulen = len(str(ext_hdu.header)) + ext_hdu._data_size hdu = fits.ImageHDU.fromstring(dat[offset : offset + hdulen]) assert isinstance(hdu, fits.ImageHDU) assert ext_hdu.header == hdu.header assert (ext_hdu.data == hdu.data).all() def test_nonstandard_hdu(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/157 Tests that "Nonstandard" HDUs with SIMPLE = F are read and written without prepending a superfluous and unwanted standard primary HDU. """ data = np.arange(100, dtype=np.uint8) hdu = fits.PrimaryHDU(data=data) hdu.header["SIMPLE"] = False hdu.writeto(self.temp("test.fits")) info = [(0, "", 1, "NonstandardHDU", 5, (), "", "")] with fits.open(self.temp("test.fits")) as hdul: assert hdul.info(output=False) == info # NonstandardHDUs just treat the data as an unspecified array of # bytes. The first 100 bytes should match the original data we # passed in...the rest should be zeros padding out the rest of the # FITS block assert (hdul[0].data[:100] == data).all() assert (hdul[0].data[100:] == 0).all() def test_extname(self): """Test getting/setting the EXTNAME of an HDU.""" h1 = fits.PrimaryHDU() assert h1.name == "PRIMARY" # Normally a PRIMARY HDU should not have an EXTNAME, though it should # have a default .name attribute assert "EXTNAME" not in h1.header # The current version of the FITS standard does allow PRIMARY HDUs to # have an EXTNAME, however. h1.name = "NOTREAL" assert h1.name == "NOTREAL" assert h1.header.get("EXTNAME") == "NOTREAL" # Updating the EXTNAME in the header should update the .name h1.header["EXTNAME"] = "TOOREAL" assert h1.name == "TOOREAL" # If we delete an EXTNAME keyword from a PRIMARY HDU it should go back # to the default del h1.header["EXTNAME"] assert h1.name == "PRIMARY" # For extension HDUs the situation is a bit simpler: h2 = fits.ImageHDU() assert h2.name == "" assert "EXTNAME" not in h2.header h2.name = "HELLO" assert h2.name == "HELLO" assert h2.header.get("EXTNAME") == "HELLO" h2.header["EXTNAME"] = "GOODBYE" assert h2.name == "GOODBYE" def test_extver_extlevel(self): """Test getting/setting the EXTVER and EXTLEVEL of and HDU.""" # EXTVER and EXTNAME work exactly the same; their semantics are, for # now, to be inferred by the user. Although they should never be less # than 1, the standard does not explicitly forbid any value so long as # it's an integer h1 = fits.PrimaryHDU() assert h1.ver == 1 assert h1.level == 1 assert "EXTVER" not in h1.header assert "EXTLEVEL" not in h1.header h1.ver = 2 assert h1.header.get("EXTVER") == 2 h1.header["EXTVER"] = 3 assert h1.ver == 3 del h1.header["EXTVER"] h1.ver == 1 h1.level = 2 assert h1.header.get("EXTLEVEL") == 2 h1.header["EXTLEVEL"] = 3 assert h1.level == 3 del h1.header["EXTLEVEL"] assert h1.level == 1 pytest.raises(TypeError, setattr, h1, "ver", "FOO") pytest.raises(TypeError, setattr, h1, "level", "BAR") def test_consecutive_writeto(self): """ Regression test for an issue where calling writeto twice on the same HDUList could write a corrupted file. https://github.com/spacetelescope/PyFITS/issues/40 is actually a particular instance of this problem, though isn't unique to sys.stdout. """ with fits.open(self.data("test0.fits")) as hdul1: # Add a bunch of header keywords so that the data will be forced to # new offsets within the file: for idx in range(40): hdul1[1].header[f"TEST{idx}"] = "test" hdul1.writeto(self.temp("test1.fits")) hdul1.writeto(self.temp("test2.fits")) # Open a second handle to the original file and compare it to hdul1 # (We only compare part of the one header that was modified) # Compare also with the second writeto output with fits.open(self.data("test0.fits")) as hdul2: with fits.open(self.temp("test2.fits")) as hdul3: for hdul in (hdul1, hdul3): for idx, hdus in enumerate(zip(hdul2, hdul)): hdu2, hdu = hdus if idx != 1: assert hdu.header == hdu2.header else: assert hdu2.header == hdu.header[: len(hdu2.header)] assert np.all(hdu.data == hdu2.data) class TestConvenienceFunctions(FitsTestCase): def test_writeto(self, home_is_temp): """ Simple test for writing a trivial header and some data to a file with the `writeto()` convenience function. """ filename = self.temp("array.fits") data = np.zeros((100, 100)) header = fits.Header() fits.writeto(filename, data, header=header, overwrite=True) with fits.open(filename) as hdul: assert len(hdul) == 1 assert (data == hdul[0].data).all() def test_writeto_2(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/107 Test of `writeto()` with a trivial header containing a single keyword. """ filename = self.temp("array.fits") data = np.zeros((100, 100)) header = fits.Header() header.set("CRPIX1", 1.0) fits.writeto( filename, data, header=header, overwrite=True, output_verify="silentfix" ) with fits.open(filename) as hdul: assert len(hdul) == 1 assert (data == hdul[0].data).all() assert "CRPIX1" in hdul[0].header assert hdul[0].header["CRPIX1"] == 1.0 def test_writeto_overwrite(self, home_is_temp): """ Ensure the `overwrite` keyword works as it should """ filename = self.temp("array.fits") data = np.zeros((100, 100)) header = fits.Header() fits.writeto(filename, data, header=header) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): fits.writeto(filename, data, header=header, overwrite=False) fits.writeto(filename, data, header=header, overwrite=True) with fits.open(filename) as hdul: assert len(hdul) == 1 assert (data == hdul[0].data).all() class TestFileFunctions(FitsTestCase): """ Tests various basic I/O operations, specifically in the astropy.io.fits.file._File class. """ def test_open_nonexistent(self): """Test that trying to open a non-existent file results in an OSError (and not some other arbitrary exception). """ with pytest.raises(OSError, match=r"No such file or directory"): fits.open(self.temp("foobar.fits")) # But opening in ostream or append mode should be okay, since they # allow writing new files for mode in ("ostream", "append"): with fits.open(self.temp("foobar.fits"), mode=mode) as _: pass assert os.path.exists(self.temp("foobar.fits")) os.remove(self.temp("foobar.fits")) def test_open_file_handle(self): # Make sure we can open a FITS file from an open file handle with open(self.data("test0.fits"), "rb") as handle: with fits.open(handle) as _: pass with open(self.temp("temp.fits"), "wb") as handle: with fits.open(handle, mode="ostream") as _: pass # Opening without explicitly specifying binary mode should fail with pytest.raises(ValueError): with open(self.data("test0.fits")) as handle: with fits.open(handle) as _: pass # All of these read modes should fail for mode in ["r", "rt"]: with pytest.raises(ValueError): with open(self.data("test0.fits"), mode=mode) as handle: with fits.open(handle) as _: pass # These update or write modes should fail as well for mode in ["w", "wt", "w+", "wt+", "r+", "rt+", "a", "at", "a+", "at+"]: with pytest.raises(ValueError): with open(self.temp("temp.fits"), mode=mode) as handle: with fits.open(handle) as _: pass def test_fits_file_handle_mode_combo(self): # This should work fine since no mode is given with open(self.data("test0.fits"), "rb") as handle: with fits.open(handle) as _: pass # This should work fine since the modes are compatible with open(self.data("test0.fits"), "rb") as handle: with fits.open(handle, mode="readonly") as _: pass # This should not work since the modes conflict with pytest.raises(ValueError): with open(self.data("test0.fits"), "rb") as handle: with fits.open(handle, mode="ostream") as _: pass def test_open_from_url(self): file_url = "file:///" + self.data("test0.fits").lstrip("/") with urllib.request.urlopen(file_url) as urlobj: with fits.open(urlobj) as _: pass # It will not be possible to write to a file that is from a URL object for mode in ("ostream", "append", "update"): with pytest.raises(ValueError): with urllib.request.urlopen(file_url) as urlobj: with fits.open(urlobj, mode=mode) as _: pass @pytest.mark.remote_data(source="astropy") def test_open_from_remote_url(self): for dataurl in (conf.dataurl, conf.dataurl_mirror): remote_url = f"{dataurl}/allsky/allsky_rosat.fits" try: with urllib.request.urlopen(remote_url) as urlobj: with fits.open(urlobj) as fits_handle: assert len(fits_handle) == 1 for mode in ("ostream", "append", "update"): with pytest.raises(ValueError): with urllib.request.urlopen(remote_url) as urlobj: with fits.open(urlobj, mode=mode) as fits_handle: assert len(fits_handle) == 1 except (urllib.error.HTTPError, urllib.error.URLError): continue else: break else: raise Exception("Could not download file") def test_open_gzipped(self): gzip_file = self._make_gzip_file() with fits.open(gzip_file) as fits_handle: assert fits_handle._file.compression == "gzip" assert len(fits_handle) == 5 with fits.open(gzip.GzipFile(gzip_file)) as fits_handle: assert fits_handle._file.compression == "gzip" assert len(fits_handle) == 5 def test_open_gzipped_from_handle(self): with open(self._make_gzip_file(), "rb") as handle: with fits.open(handle) as fits_handle: assert fits_handle._file.compression == "gzip" def test_detect_gzipped(self): """Test detection of a gzip file when the extension is not .gz.""" with fits.open(self._make_gzip_file("test0.fz")) as fits_handle: assert fits_handle._file.compression == "gzip" assert len(fits_handle) == 5 def test_writeto_append_mode_gzip(self): """Regression test for https://github.com/spacetelescope/PyFITS/issues/33 Check that a new GzipFile opened in append mode can be used to write out a new FITS file. """ # Note: when opening a GzipFile the 'b+' is superfluous, but this was # still how the original test case looked # Note: with statement not supported on GzipFile in older Python # versions fileobj = gzip.GzipFile(self.temp("test.fits.gz"), "ab+") h = fits.PrimaryHDU() try: h.writeto(fileobj) finally: fileobj.close() with fits.open(self.temp("test.fits.gz")) as hdul: assert hdul[0].header == h.header def test_fits_update_mode_gzip(self): """Test updating a GZipped FITS file""" with fits.open(self._make_gzip_file("update.gz"), mode="update") as fits_handle: hdu = fits.ImageHDU(data=[x for x in range(100)]) fits_handle.append(hdu) with fits.open(self.temp("update.gz")) as new_handle: assert len(new_handle) == 6 assert (new_handle[-1].data == [x for x in range(100)]).all() def test_fits_append_mode_gzip(self): """Make sure that attempting to open an existing GZipped FITS file in 'append' mode raises an error""" with pytest.raises(OSError): with fits.open(self._make_gzip_file("append.gz"), mode="append") as _: pass @pytest.mark.skipif(not HAS_BZ2, reason="Python built without bz2 module") def test_open_bzipped(self): bzip_file = self._make_bzip2_file() with fits.open(bzip_file) as fits_handle: assert fits_handle._file.compression == "bzip2" assert len(fits_handle) == 5 with fits.open(bz2.BZ2File(bzip_file)) as fits_handle: assert fits_handle._file.compression == "bzip2" assert len(fits_handle) == 5 @pytest.mark.skipif(not HAS_BZ2, reason="Python built without bz2 module") def test_open_bzipped_from_handle(self): with open(self._make_bzip2_file(), "rb") as handle: with fits.open(handle) as fits_handle: assert fits_handle._file.compression == "bzip2" assert len(fits_handle) == 5 @pytest.mark.skipif(not HAS_BZ2, reason="Python built without bz2 module") def test_detect_bzipped(self): """Test detection of a bzip2 file when the extension is not .bz2.""" with fits.open(self._make_bzip2_file("test0.xx")) as fits_handle: assert fits_handle._file.compression == "bzip2" assert len(fits_handle) == 5 @pytest.mark.skipif(not HAS_BZ2, reason="Python built without bz2 module") def test_writeto_bzip2_fileobj(self): """Test writing to a bz2.BZ2File file like object""" fileobj = bz2.BZ2File(self.temp("test.fits.bz2"), "w") h = fits.PrimaryHDU() try: h.writeto(fileobj) finally: fileobj.close() with fits.open(self.temp("test.fits.bz2")) as hdul: assert hdul[0].header == h.header @pytest.mark.skipif(not HAS_BZ2, reason="Python built without bz2 module") def test_writeto_bzip2_filename(self): """Test writing to a bzip2 file by name""" filename = self.temp("testname.fits.bz2") h = fits.PrimaryHDU() h.writeto(filename) with fits.open(self.temp("testname.fits.bz2")) as hdul: assert hdul[0].header == h.header def test_open_zipped(self): zip_file = self._make_zip_file() with fits.open(zip_file) as fits_handle: assert fits_handle._file.compression == "zip" assert len(fits_handle) == 5 with fits.open(zipfile.ZipFile(zip_file)) as fits_handle: assert fits_handle._file.compression == "zip" assert len(fits_handle) == 5 def test_open_zipped_from_handle(self): with open(self._make_zip_file(), "rb") as handle: with fits.open(handle) as fits_handle: assert fits_handle._file.compression == "zip" assert len(fits_handle) == 5 def test_detect_zipped(self): """Test detection of a zip file when the extension is not .zip.""" zf = self._make_zip_file(filename="test0.fz") with fits.open(zf) as fits_handle: assert len(fits_handle) == 5 def test_open_zipped_writeable(self): """Opening zipped files in a writeable mode should fail.""" zf = self._make_zip_file() pytest.raises(OSError, fits.open, zf, "update") pytest.raises(OSError, fits.open, zf, "append") zf = zipfile.ZipFile(zf, "a") pytest.raises(OSError, fits.open, zf, "update") pytest.raises(OSError, fits.open, zf, "append") def test_read_open_astropy_gzip_file(self): """ Regression test for https://github.com/astropy/astropy/issues/2774 This tests reading from a ``GzipFile`` object from Astropy's compatibility copy of the ``gzip`` module. """ gf = gzip.GzipFile(self._make_gzip_file()) try: assert len(fits.open(gf)) == 5 finally: gf.close() def test_open_multiple_member_zipfile(self): """ Opening zip files containing more than one member files should fail as there's no obvious way to specify which file is the FITS file to read. """ zfile = zipfile.ZipFile(self.temp("test0.zip"), "w") zfile.write(self.data("test0.fits")) zfile.writestr("foo", "bar") zfile.close() with pytest.raises(OSError): fits.open(zfile.filename) def test_read_open_file(self): """Read from an existing file object.""" with open(self.data("test0.fits"), "rb") as f: assert len(fits.open(f)) == 5 def test_read_closed_file(self): """Read from an existing file object that's been closed.""" f = open(self.data("test0.fits"), "rb") f.close() with fits.open(f) as f2: assert len(f2) == 5 def test_read_open_gzip_file(self): """Read from an open gzip file object.""" gf = gzip.GzipFile(self._make_gzip_file()) try: assert len(fits.open(gf)) == 5 finally: gf.close() def test_open_gzip_file_for_writing(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/195.""" gf = self._make_gzip_file() with fits.open(gf, mode="update") as h: h[0].header["EXPFLAG"] = "ABNORMAL" h[1].data[0, 0] = 1 with fits.open(gf) as h: # Just to make sure the update worked; if updates work # normal writes should work too... assert h[0].header["EXPFLAG"] == "ABNORMAL" assert h[1].data[0, 0] == 1 def test_write_read_gzip_file(self, home_is_temp): """ Regression test for https://github.com/astropy/astropy/issues/2794 Ensure files written through gzip are readable. """ data = np.arange(100) hdu = fits.PrimaryHDU(data=data) hdu.writeto(self.temp("test.fits.gz")) with open(os.path.expanduser(self.temp("test.fits.gz")), "rb") as f: assert f.read(3) == GZIP_MAGIC with fits.open(self.temp("test.fits.gz")) as hdul: assert np.all(hdul[0].data == data) @pytest.mark.parametrize("ext", ["gz", "bz2", "zip"]) def test_compressed_ext_but_not_compressed(self, ext): testfile = self.temp(f"test0.fits.{ext}") shutil.copy(self.data("test0.fits"), testfile) with fits.open(testfile) as hdul: assert len(hdul) == 5 fits.append(testfile, np.arange(5)) with fits.open(testfile) as hdul: assert len(hdul) == 6 def test_read_file_like_object(self): """Test reading a FITS file from a file-like object.""" filelike = io.BytesIO() with open(self.data("test0.fits"), "rb") as f: filelike.write(f.read()) filelike.seek(0) assert len(fits.open(filelike)) == 5 def test_updated_file_permissions(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/79 Tests that when a FITS file is modified in update mode, the file permissions are preserved. """ filename = self.temp("test.fits") hdul = [fits.PrimaryHDU(), fits.ImageHDU()] hdul = fits.HDUList(hdul) hdul.writeto(filename) old_mode = os.stat(filename).st_mode hdul = fits.open(filename, mode="update") hdul.insert(1, fits.ImageHDU()) hdul.flush() hdul.close() assert old_mode == os.stat(filename).st_mode def test_fileobj_mode_guessing(self): """Tests whether a file opened without a specified io.fits mode ('readonly', etc.) is opened in a mode appropriate for the given file object. """ self.copy_file("test0.fits") # Opening in text mode should outright fail for mode in ("r", "w", "a"): with open(self.temp("test0.fits"), mode) as f: pytest.raises(ValueError, fits.HDUList.fromfile, f) # Need to re-copy the file since opening it in 'w' mode blew it away self.copy_file("test0.fits") with open(self.temp("test0.fits"), "rb") as f: with fits.HDUList.fromfile(f) as h: assert h.fileinfo(0)["filemode"] == "readonly" for mode in ("wb", "ab"): with open(self.temp("test0.fits"), mode) as f: with fits.HDUList.fromfile(f) as h: # Basically opening empty files for output streaming assert len(h) == 0 # Need to re-copy the file since opening it in 'w' mode blew it away self.copy_file("test0.fits") with open(self.temp("test0.fits"), "wb+") as f: with fits.HDUList.fromfile(f) as h: # wb+ still causes an existing file to be overwritten so there # are no HDUs assert len(h) == 0 # Need to re-copy the file since opening it in 'w' mode blew it away self.copy_file("test0.fits") with open(self.temp("test0.fits"), "rb+") as f: with fits.HDUList.fromfile(f) as h: assert h.fileinfo(0)["filemode"] == "update" with open(self.temp("test0.fits"), "ab+") as f: with fits.HDUList.fromfile(f) as h: assert h.fileinfo(0)["filemode"] == "append" def test_mmap_unwriteable(self): """Regression test for https://github.com/astropy/astropy/issues/968 Temporarily patches mmap.mmap to exhibit platform-specific bad behavior. """ class MockMmap(mmap.mmap): def flush(self): raise OSError("flush is broken on this platform") old_mmap = mmap.mmap mmap.mmap = MockMmap # Force the mmap test to be rerun _File.__dict__["_mmap_available"]._cache.clear() try: self.copy_file("test0.fits") with pytest.warns( AstropyUserWarning, match=r"mmap\.flush is unavailable" ) as w: with fits.open( self.temp("test0.fits"), mode="update", memmap=True ) as h: h[1].data[0, 0] = 999 assert len(w) == 1 # Double check that writing without mmap still worked with fits.open(self.temp("test0.fits")) as h: assert h[1].data[0, 0] == 999 finally: mmap.mmap = old_mmap _File.__dict__["_mmap_available"]._cache.clear() @pytest.mark.openfiles_ignore def test_mmap_allocate_error(self): """ Regression test for https://github.com/astropy/astropy/issues/1380 Temporarily patches mmap.mmap to raise an OSError if mode is ACCESS_COPY. """ mmap_original = mmap.mmap # We patch mmap here to raise an error if access=mmap.ACCESS_COPY, which # emulates an issue that an OSError is raised if the available address # space is less than the size of the file even if memory mapping is used. def mmap_patched(*args, **kwargs): if kwargs.get("access") == mmap.ACCESS_COPY: exc = OSError() exc.errno = errno.ENOMEM raise exc else: return mmap_original(*args, **kwargs) with fits.open(self.data("test0.fits"), memmap=True) as hdulist: with patch.object(mmap, "mmap", side_effect=mmap_patched) as p: with pytest.warns( AstropyUserWarning, match=r"Could not memory " r"map array with mode='readonly'", ): data = hdulist[1].data p.reset_mock() assert not data.flags.writeable def test_mmap_closing(self): """ Tests that the mmap reference is closed/removed when there aren't any HDU data references left. """ if not _File._mmap_available: pytest.xfail("not expected to work on platforms without mmap support") with fits.open(self.data("test0.fits"), memmap=True) as hdul: assert hdul._file._mmap is None hdul[1].data assert hdul._file._mmap is not None del hdul[1].data # Should be no more references to data in the file so close the # mmap assert hdul._file._mmap is None hdul[1].data hdul[2].data del hdul[1].data # hdul[2].data is still references so keep the mmap open assert hdul._file._mmap is not None del hdul[2].data assert hdul._file._mmap is None assert hdul._file._mmap is None with fits.open(self.data("test0.fits"), memmap=True) as hdul: hdul[1].data # When the only reference to the data is on the hdu object, and the # hdulist it belongs to has been closed, the mmap should be closed as # well assert hdul._file._mmap is None with fits.open(self.data("test0.fits"), memmap=True) as hdul: data = hdul[1].data # also make a copy data_copy = data.copy() # The HDUList is closed; in fact, get rid of it completely del hdul # The data array should still work though... assert np.all(data == data_copy) def test_uncloseable_file(self): """ Regression test for https://github.com/astropy/astropy/issues/2356 Demonstrates that FITS files can still be read from file-like objects that don't have an obvious "open" or "closed" state. """ class MyFileLike: def __init__(self, foobar): self._foobar = foobar def read(self, n): return self._foobar.read(n) def seek(self, offset, whence=os.SEEK_SET): self._foobar.seek(offset, whence) def tell(self): return self._foobar.tell() with open(self.data("test0.fits"), "rb") as f: fileobj = MyFileLike(f) with fits.open(fileobj) as hdul1: with fits.open(self.data("test0.fits")) as hdul2: assert hdul1.info(output=False) == hdul2.info(output=False) for hdu1, hdu2 in zip(hdul1, hdul2): assert hdu1.header == hdu2.header if hdu1.data is not None and hdu2.data is not None: assert np.all(hdu1.data == hdu2.data) def test_write_bytesio_discontiguous(self): """ Regression test related to https://github.com/astropy/astropy/issues/2794#issuecomment-55441539 Demonstrates that writing an HDU containing a discontiguous Numpy array should work properly. """ data = np.arange(100)[::3] hdu = fits.PrimaryHDU(data=data) fileobj = io.BytesIO() hdu.writeto(fileobj) fileobj.seek(0) with fits.open(fileobj) as h: assert np.all(h[0].data == data) def test_write_bytesio(self): """ Regression test for https://github.com/astropy/astropy/issues/2463 Test against `io.BytesIO`. `io.StringIO` is not supported. """ self._test_write_string_bytes_io(io.BytesIO()) @pytest.mark.skipif( sys.platform.startswith("win32"), reason="Cannot test on Windows" ) def test_filename_with_colon(self): """ Test reading and writing a file with a colon in the filename. Regression test for https://github.com/astropy/astropy/issues/3122 """ # Skip on Windows since colons in filenames makes NTFS sad. filename = "APEXHET.2014-04-01T15:18:01.000.fits" hdu = fits.PrimaryHDU(data=np.arange(10)) hdu.writeto(self.temp(filename)) with fits.open(self.temp(filename)) as hdul: assert np.all(hdul[0].data == hdu.data) def test_writeto_full_disk(self, monkeypatch): """ Test that it gives a readable error when trying to write an hdulist to a full disk. """ def _writeto(self, array): raise OSError("Fake error raised when writing file.") def get_free_space_in_dir(path): return 0 with pytest.raises(OSError) as exc: monkeypatch.setattr(fits.hdu.base._BaseHDU, "_writeto", _writeto) monkeypatch.setattr(data, "get_free_space_in_dir", get_free_space_in_dir) n = np.arange(0, 1000, dtype="int64") hdu = fits.PrimaryHDU(n) hdulist = fits.HDUList(hdu) filename = self.temp("test.fits") with open(filename, mode="wb") as fileobj: hdulist.writeto(fileobj) assert ( "Not enough space on disk: requested 8000, available 0. " "Fake error raised when writing file." == exc.value.args[0] ) def test_flush_full_disk(self, monkeypatch): """ Test that it gives a readable error when trying to update an hdulist to a full disk. """ filename = self.temp("test.fits") hdul = [fits.PrimaryHDU(), fits.ImageHDU()] hdul = fits.HDUList(hdul) hdul[0].data = np.arange(0, 1000, dtype="int64") hdul.writeto(filename) def _writedata(self, fileobj): raise OSError("Fake error raised when writing file.") def get_free_space_in_dir(path): return 0 monkeypatch.setattr(fits.hdu.base._BaseHDU, "_writedata", _writedata) monkeypatch.setattr(data, "get_free_space_in_dir", get_free_space_in_dir) with pytest.raises(OSError) as exc: with fits.open(filename, mode="update") as hdul: hdul[0].data = np.arange(0, 1000, dtype="int64") hdul.insert(1, fits.ImageHDU()) hdul.flush() assert ( "Not enough space on disk: requested 8000, available 0. " "Fake error raised when writing file." == exc.value.args[0] ) def _test_write_string_bytes_io(self, fileobj): """ Implemented for both test_write_stringio and test_write_bytesio. """ with fits.open(self.data("test0.fits")) as hdul: hdul.writeto(fileobj) hdul2 = fits.HDUList.fromstring(fileobj.getvalue()) assert FITSDiff(hdul, hdul2).identical def _make_gzip_file(self, filename="test0.fits.gz"): gzfile = self.temp(filename) with open(self.data("test0.fits"), "rb") as f: gz = gzip.open(gzfile, "wb") gz.write(f.read()) gz.close() return gzfile def test_write_overwrite(self, home_is_temp): filename = self.temp("test_overwrite.fits") hdu = fits.PrimaryHDU(data=np.arange(10)) hdu.writeto(filename) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): hdu.writeto(filename) hdu.writeto(filename, overwrite=True) def _make_zip_file(self, mode="copyonwrite", filename="test0.fits.zip"): zfile = zipfile.ZipFile(self.temp(filename), "w") zfile.write(self.data("test0.fits")) zfile.close() return zfile.filename def _make_bzip2_file(self, filename="test0.fits.bz2"): bzfile = self.temp(filename) with open(self.data("test0.fits"), "rb") as f: bz = bz2.BZ2File(bzfile, "w") bz.write(f.read()) bz.close() return bzfile def test_simulateonly(self): """Write to None simulates writing.""" with fits.open(self.data("test0.fits")) as hdul: hdul.writeto(None) hdul[0].writeto(None) hdul[0].header.tofile(None) def test_bintablehdu_zero_bytes(self): """Make sure we don't have any zero-byte writes in BinTableHDU""" bright = np.rec.array( [ (1, "Sirius", -1.45, "A1V"), (2, "Canopus", -0.73, "F0Ib"), (3, "Rigil Kent", -0.1, "G2V"), ], formats="int16,a20,float32,a10", names="order,name,mag,Sp", ) hdu_non_zero = fits.BinTableHDU(bright) # use safeio, a special file handler meant to fail on zero-byte writes fh = safeio.CatchZeroByteWriter(open(self.temp("bright.fits"), mode="wb")) hdu_non_zero.writeto(fh) fh.close() def test_primaryhdu_zero_bytes(self): """ Make sure we don't have any zero-byte writes from an ImageHDU (or other) of `size % BLOCK_SIZE == 0` """ hdu_img_2880 = fits.PrimaryHDU(data=np.arange(720, dtype="i4")) # use safeio, a special file handler meant to fail on zero-byte writes fh = safeio.CatchZeroByteWriter(open(self.temp("image.fits"), mode="wb")) hdu_img_2880.writeto(fh) fh.close() class TestStreamingFunctions(FitsTestCase): """Test functionality of the StreamingHDU class.""" def test_streaming_hdu(self, home_is_temp): shdu = self._make_streaming_hdu(self.temp("new.fits")) assert isinstance(shdu.size, int) assert shdu.size == 100 arr = np.arange(25, dtype=np.int32).reshape((5, 5)) shdu.write(arr) assert shdu.writecomplete shdu.close() with fits.open(self.temp("new.fits")) as hdul: assert len(hdul) == 1 assert (hdul[0].data == arr).all() def test_streaming_hdu_file_wrong_mode(self): """ Test that streaming an HDU to a file opened in the wrong mode fails as expected. """ with pytest.raises(ValueError): with open(self.temp("new.fits"), "wb") as f: header = fits.Header() fits.StreamingHDU(f, header) def test_streaming_hdu_write_file(self): """Test streaming an HDU to an open file object.""" arr = np.zeros((5, 5), dtype=np.int32) with open(self.temp("new.fits"), "ab+") as f: shdu = self._make_streaming_hdu(f) shdu.write(arr) assert shdu.writecomplete assert shdu.size == 100 with fits.open(self.temp("new.fits")) as hdul: assert len(hdul) == 1 assert (hdul[0].data == arr).all() def test_streaming_hdu_write_file_like(self): """Test streaming an HDU to an open file-like object.""" arr = np.zeros((5, 5), dtype=np.int32) # The file-like object underlying a StreamingHDU must be in binary mode sf = io.BytesIO() shdu = self._make_streaming_hdu(sf) shdu.write(arr) assert shdu.writecomplete assert shdu.size == 100 sf.seek(0) hdul = fits.open(sf) assert len(hdul) == 1 assert (hdul[0].data == arr).all() def test_streaming_hdu_append_extension(self): arr = np.zeros((5, 5), dtype=np.int32) with open(self.temp("new.fits"), "ab+") as f: shdu = self._make_streaming_hdu(f) shdu.write(arr) # Doing this again should update the file with an extension with open(self.temp("new.fits"), "ab+") as f: shdu = self._make_streaming_hdu(f) shdu.write(arr) def test_fix_invalid_extname(self, capsys): phdu = fits.PrimaryHDU() ihdu = fits.ImageHDU() ihdu.header["EXTNAME"] = 12345678 hdul = fits.HDUList([phdu, ihdu]) filename = self.temp("temp.fits") pytest.raises( fits.VerifyError, hdul.writeto, filename, output_verify="exception" ) with pytest.warns( fits.verify.VerifyWarning, match=r"Verification reported errors" ): hdul.writeto(filename, output_verify="fix") with fits.open(filename): assert hdul[1].name == "12345678" assert hdul[1].header["EXTNAME"] == "12345678" hdul.close() def _make_streaming_hdu(self, fileobj): hd = fits.Header() hd["SIMPLE"] = (True, "conforms to FITS standard") hd["BITPIX"] = (32, "array data type") hd["NAXIS"] = (2, "number of array dimensions") hd["NAXIS1"] = 5 hd["NAXIS2"] = 5 hd["EXTEND"] = True return fits.StreamingHDU(fileobj, hd) def test_blank_ignore(self): with fits.open(self.data("blank.fits"), ignore_blank=True) as f: assert f[0].data.flat[0] == 2 def test_error_if_memmap_impossible(self): pth = self.data("blank.fits") with fits.open(pth, memmap=True) as hdul: with pytest.raises(ValueError): hdul[0].data # However, it should not fail if do_not_scale_image_data was used: # See https://github.com/astropy/astropy/issues/3766 with fits.open(pth, memmap=True, do_not_scale_image_data=True) as hdul: hdul[0].data # Just make sure it doesn't crash
137117d4b8bf167b284289e06c52b62de6480b9b5428b765d80d2e401a03b49c
# Licensed under a 3-clause BSD style license - see LICENSE.rst import numpy as np import pytest from astropy.coordinates import EarthLocation from astropy.io import fits from astropy.io.fits.fitstime import ( GLOBAL_TIME_INFO, is_time_column_keyword, time_to_fits, ) from astropy.table import Column, QTable, Table from astropy.time import Time, TimeDelta from astropy.time.core import BARYCENTRIC_SCALES from astropy.time.formats import FITS_DEPRECATED_SCALES from astropy.utils.exceptions import AstropyUserWarning from .conftest import FitsTestCase class TestFitsTime(FitsTestCase): def setup_class(self): self.time = np.array(["1999-01-01T00:00:00.123456789", "2010-01-01T00:00:00"]) self.time_3d = np.array([[[1, 2], [1, 3], [3, 4]]]) def test_is_time_column_keyword(self): # Time column keyword without column number assert is_time_column_keyword("TRPOS") is False # Global time column keyword assert is_time_column_keyword("TIMESYS") is False # Valid time column keyword assert is_time_column_keyword("TRPOS12") is True @pytest.mark.parametrize("table_types", (Table, QTable)) def test_time_to_fits_loc(self, table_types): """ Test all the unusual conditions for locations of ``Time`` columns in a ``Table``. """ t = table_types() t["a"] = Time(self.time, format="isot", scale="utc") t["b"] = Time(self.time, format="isot", scale="tt") # Check that vectorized location is stored using Green Bank convention t["a"].location = EarthLocation([1.0, 2.0], [2.0, 3.0], [3.0, 4.0], unit="Mm") with pytest.warns( AstropyUserWarning, match=r'Time Column "b" has no ' r"specified location, but global Time Position is present", ): table, hdr = time_to_fits(t) assert (table["OBSGEO-X"] == t["a"].location.x.to_value(unit="m")).all() assert (table["OBSGEO-Y"] == t["a"].location.y.to_value(unit="m")).all() assert (table["OBSGEO-Z"] == t["a"].location.z.to_value(unit="m")).all() with pytest.warns( AstropyUserWarning, match=r'Time Column "b" has no ' r"specified location, but global Time Position is present", ): t.write(self.temp("time.fits"), format="fits", overwrite=True) # Check that a blank value for the "TRPOSn" keyword is not generated hdr = fits.getheader(self.temp("time.fits"), 1) assert hdr.get("TRPOS2", None) is None with pytest.warns( AstropyUserWarning, match=r"Time column reference position " r'"TRPOSn" is not specified. The default value for it is ' r'"TOPOCENTER", and the observatory position has been specified.', ): tm = table_types.read( self.temp("time.fits"), format="fits", astropy_native=True ) assert (tm["a"].location == t["a"].location).all() assert tm["b"].location == t["b"].location # Check that multiple Time columns with different locations raise an exception t["a"].location = EarthLocation(1, 2, 3) t["b"].location = EarthLocation(2, 3, 4) with pytest.raises(ValueError) as err: table, hdr = time_to_fits(t) assert "Multiple Time Columns with different geocentric" in str(err.value) # Check that Time column with no location specified will assume global location t["b"].location = None with pytest.warns( AstropyUserWarning, match=( 'Time Column "b" has no specified ' "location, but global Time Position is present" ), ) as w: table, hdr = time_to_fits(t) assert len(w) == 1 # Check that multiple Time columns with same location can be written t["b"].location = EarthLocation(1, 2, 3) table, hdr = time_to_fits(t) # Check compatibility of Time Scales and Reference Positions for scale in BARYCENTRIC_SCALES: t.replace_column("a", getattr(t["a"], scale)) with pytest.warns( AstropyUserWarning, match='Earth Location "TOPOCENTER" for Time Column', ) as w: table, hdr = time_to_fits(t) assert len(w) == 1 # Check that multidimensional vectorized location (ndim=3) is stored # using Green Bank convention. t = table_types() location = EarthLocation( [[[1.0, 2.0], [1.0, 3.0], [3.0, 4.0]]], [[[1.0, 2.0], [1.0, 3.0], [3.0, 4.0]]], [[[1.0, 2.0], [1.0, 3.0], [3.0, 4.0]]], unit="Mm", ) t["a"] = Time(self.time_3d, format="jd", location=location) table, hdr = time_to_fits(t) assert (table["OBSGEO-X"] == t["a"].location.x.to_value(unit="m")).all() assert (table["OBSGEO-Y"] == t["a"].location.y.to_value(unit="m")).all() assert (table["OBSGEO-Z"] == t["a"].location.z.to_value(unit="m")).all() t.write(self.temp("time.fits"), format="fits", overwrite=True) tm = table_types.read( self.temp("time.fits"), format="fits", astropy_native=True ) assert (tm["a"].location == t["a"].location).all() # Check that singular location with ndim>1 can be written t["a"] = Time( self.time, location=EarthLocation([[[1.0]]], [[[2.0]]], [[[3.0]]], unit="Mm"), ) table, hdr = time_to_fits(t) assert hdr["OBSGEO-X"] == t["a"].location.x.to_value(unit="m") assert hdr["OBSGEO-Y"] == t["a"].location.y.to_value(unit="m") assert hdr["OBSGEO-Z"] == t["a"].location.z.to_value(unit="m") t.write(self.temp("time.fits"), format="fits", overwrite=True) tm = table_types.read( self.temp("time.fits"), format="fits", astropy_native=True ) assert tm["a"].location == t["a"].location @pytest.mark.parametrize("mask", (False, [True, False])) @pytest.mark.parametrize("serialize_method", ("jd1_jd2", "formatted_value")) def test_time_to_fits_serialize_method(self, serialize_method, mask): """ Test the data returned by ``time_to_fits`` for masked values. """ a = Time(np.ma.MaskedArray(self.time, mask=mask)) b = Time( np.ma.MaskedArray([[1, 2], [3, 4]], mask=np.broadcast_to(mask, (2, 2))), format="cxcsec", ) assert b.masked is a.masked is (mask is not False) t = QTable([a, b], names=["a", "b"]) t.write( self.temp("time.fits"), format="fits", overwrite=True, serialize_method=serialize_method, ) tm = QTable.read(self.temp("time.fits"), format="fits", astropy_native=True) if mask is not False: assert np.all(tm["a"].mask == a.mask) assert np.all(tm["b"].mask == b.mask) if serialize_method == "jd1_jd2": assert isinstance(tm["a"], Time) and np.all(tm["a"] == a) assert isinstance(tm["b"], Time) and np.all(tm["b"] == b) else: # TODO: Should 'formatted_value' not become a Time too, # at least if read with astropy_native=True? assert isinstance(tm["a"], Column) and np.all(tm["a"] == a.value) assert isinstance(tm["b"], Column) and np.all(tm["b"] == b.value) @pytest.mark.parametrize("table_types", (Table, QTable)) def test_time_to_fits_header(self, table_types): """ Test the header and metadata returned by ``time_to_fits``. """ t = table_types() t["a"] = Time( self.time, format="isot", scale="utc", location=EarthLocation(-2446354, 4237210, 4077985, unit="m"), ) t["b"] = Time([1, 2], format="cxcsec", scale="tt") ideal_col_hdr = { "OBSGEO-X": t["a"].location.x.value, "OBSGEO-Y": t["a"].location.y.value, "OBSGEO-Z": t["a"].location.z.value, } with pytest.warns( AstropyUserWarning, match=r'Time Column "b" has no ' r"specified location, but global Time Position is present", ): table, hdr = time_to_fits(t) # Check the global time keywords in hdr for key, value in GLOBAL_TIME_INFO.items(): assert hdr[key] == value[0] assert hdr.comments[key] == value[1] hdr.remove(key) for key, value in ideal_col_hdr.items(): assert hdr[key] == value hdr.remove(key) # Check the column-specific time metadata coord_info = table.meta["__coordinate_columns__"] for colname in coord_info: assert coord_info[colname]["coord_type"] == t[colname].scale.upper() assert coord_info[colname]["coord_unit"] == "d" assert coord_info["a"]["time_ref_pos"] == "TOPOCENTER" assert coord_info["b"]["time_ref_pos"] is None assert len(hdr) == 0 @pytest.mark.parametrize("table_types", (Table, QTable)) def test_fits_to_time_meta(self, table_types): """ Test that the relevant global time metadata is read into ``Table.meta`` as ``Time``. """ t = table_types() t["a"] = Time(self.time, format="isot", scale="utc") t.meta["DATE"] = "1999-01-01T00:00:00" t.meta["MJD-OBS"] = 56670 # Test for default write behavior (full precision) and read it # back using native astropy objects; thus, ensure its round-trip t.write(self.temp("time.fits"), format="fits", overwrite=True) tm = table_types.read( self.temp("time.fits"), format="fits", astropy_native=True ) # Test DATE assert isinstance(tm.meta["DATE"], Time) assert tm.meta["DATE"].value == t.meta["DATE"] assert tm.meta["DATE"].format == "fits" # Default time scale according to the FITS standard is UTC assert tm.meta["DATE"].scale == "utc" # Test MJD-xxx assert isinstance(tm.meta["MJD-OBS"], Time) assert tm.meta["MJD-OBS"].value == t.meta["MJD-OBS"] assert tm.meta["MJD-OBS"].format == "mjd" assert tm.meta["MJD-OBS"].scale == "utc" # Explicitly specified Time Scale t.meta["TIMESYS"] = "ET" t.write(self.temp("time.fits"), format="fits", overwrite=True) tm = table_types.read( self.temp("time.fits"), format="fits", astropy_native=True ) # Test DATE assert isinstance(tm.meta["DATE"], Time) assert tm.meta["DATE"].value == t.meta["DATE"] assert tm.meta["DATE"].scale == "utc" # Test MJD-xxx assert isinstance(tm.meta["MJD-OBS"], Time) assert tm.meta["MJD-OBS"].value == t.meta["MJD-OBS"] assert tm.meta["MJD-OBS"].scale == FITS_DEPRECATED_SCALES[t.meta["TIMESYS"]] # Test for conversion of time data to its value, as defined by its format t["a"].info.serialize_method["fits"] = "formatted_value" t.write(self.temp("time.fits"), format="fits", overwrite=True) tm = table_types.read(self.temp("time.fits"), format="fits") # Test DATE assert not isinstance(tm.meta["DATE"], Time) assert tm.meta["DATE"] == t.meta["DATE"] # Test MJD-xxx assert not isinstance(tm.meta["MJD-OBS"], Time) assert tm.meta["MJD-OBS"] == t.meta["MJD-OBS"] assert (tm["a"] == t["a"].value).all() @pytest.mark.parametrize("table_types", (Table, QTable)) def test_time_loc_unit(self, table_types): """ Test that ``location`` specified by using any valid unit (length/angle) in ``Time`` columns gets stored in FITS as ITRS Cartesian coordinates (X, Y, Z), each in m. Test that it round-trips through FITS. """ t = table_types() t["a"] = Time( self.time, format="isot", scale="utc", location=EarthLocation(1, 2, 3, unit="km"), ) table, hdr = time_to_fits(t) # Check the header assert hdr["OBSGEO-X"] == t["a"].location.x.to_value(unit="m") assert hdr["OBSGEO-Y"] == t["a"].location.y.to_value(unit="m") assert hdr["OBSGEO-Z"] == t["a"].location.z.to_value(unit="m") t.write(self.temp("time.fits"), format="fits", overwrite=True) tm = table_types.read( self.temp("time.fits"), format="fits", astropy_native=True ) # Check the round-trip of location assert (tm["a"].location == t["a"].location).all() assert tm["a"].location.x.value == t["a"].location.x.to_value(unit="m") assert tm["a"].location.y.value == t["a"].location.y.to_value(unit="m") assert tm["a"].location.z.value == t["a"].location.z.to_value(unit="m") @pytest.mark.parametrize("table_types", (Table, QTable)) def test_fits_to_time_index(self, table_types): """ Ensure that fits_to_time works correctly if the time column is also an index. """ t = table_types() t["a"] = Time(self.time, format="isot", scale="utc") t["b"] = [2, 1] t["c"] = [3, 4] # Make it so that the time column is also an index t.add_index("a") t.add_index("b") # Test for default write behavior (full precision) and read it # back using native astropy objects; thus, ensure its round-trip t.write(self.temp("time.fits"), format="fits", overwrite=True) tm = table_types.read( self.temp("time.fits"), format="fits", astropy_native=True ) assert isinstance(tm["a"], Time) # Ensure that indices on original table are preserved but round-trip # table has no indices. (If indices are ever serialized the final two # tests are expected to fail). assert len(t.indices) == 2 assert len(tm.indices) == 0 for name in ("a", "b"): assert len(t[name].info.indices) == 1 assert len(tm[name].info.indices) == 0 @pytest.mark.parametrize("table_types", (Table, QTable)) def test_io_time_read_fits(self, table_types): """ Test that FITS table with time columns (standard compliant) can be read by io.fits as a table with Time columns. This tests the following: 1. The special-case where a column has the name 'TIME' and a time unit 2. Time from Epoch (Reference time) is appropriately converted. 3. Coordinate columns (corresponding to coordinate keywords in the header) other than time, that is, spatial coordinates, are not mistaken to be time. """ filename = self.data("chandra_time.fits") with pytest.warns( AstropyUserWarning, match=r'Time column "time" reference ' r"position will be ignored", ): tm = table_types.read(filename, astropy_native=True) # Test case 1 assert isinstance(tm["time"], Time) assert tm["time"].scale == "tt" assert tm["time"].format == "mjd" non_native = table_types.read(filename) # Test case 2 ref_time = Time( non_native.meta["MJDREF"], format="mjd", scale=non_native.meta["TIMESYS"].lower(), ) delta_time = TimeDelta(non_native["time"]) assert (ref_time + delta_time == tm["time"]).all() # Test case 3 for colname in ["chipx", "chipy", "detx", "dety", "x", "y"]: assert not isinstance(tm[colname], Time) @pytest.mark.parametrize("table_types", (Table, QTable)) def test_io_time_read_fits_datetime(self, table_types): """ Test that ISO-8601 Datetime String Columns are read correctly. """ # Datetime column c = fits.Column( name="datetime", format="A29", coord_type="TCG", time_ref_pos="GEOCENTER", array=self.time, ) # Explicitly create a FITS Binary Table bhdu = fits.BinTableHDU.from_columns([c]) bhdu.writeto(self.temp("time.fits"), overwrite=True) tm = table_types.read(self.temp("time.fits"), astropy_native=True) assert isinstance(tm["datetime"], Time) assert tm["datetime"].scale == "tcg" assert tm["datetime"].format == "fits" assert (tm["datetime"] == self.time).all() @pytest.mark.parametrize("table_types", (Table, QTable)) def test_io_time_read_fits_location(self, table_types): """ Test that geocentric/geodetic observatory position is read properly, as and when it is specified. """ # Datetime column c = fits.Column( name="datetime", format="A29", coord_type="TT", time_ref_pos="TOPOCENTER", array=self.time, ) # Observatory position in ITRS Cartesian coordinates (geocentric) cards = [("OBSGEO-X", -2446354), ("OBSGEO-Y", 4237210), ("OBSGEO-Z", 4077985)] # Explicitly create a FITS Binary Table bhdu = fits.BinTableHDU.from_columns([c], header=fits.Header(cards)) bhdu.writeto(self.temp("time.fits"), overwrite=True) tm = table_types.read(self.temp("time.fits"), astropy_native=True) assert isinstance(tm["datetime"], Time) assert tm["datetime"].location.x.value == -2446354 assert tm["datetime"].location.y.value == 4237210 assert tm["datetime"].location.z.value == 4077985 # Observatory position in geodetic coordinates cards = [("OBSGEO-L", 0), ("OBSGEO-B", 0), ("OBSGEO-H", 0)] # Explicitly create a FITS Binary Table bhdu = fits.BinTableHDU.from_columns([c], header=fits.Header(cards)) bhdu.writeto(self.temp("time.fits"), overwrite=True) tm = table_types.read(self.temp("time.fits"), astropy_native=True) assert isinstance(tm["datetime"], Time) assert tm["datetime"].location.lon.value == 0 assert tm["datetime"].location.lat.value == 0 assert np.isclose(tm["datetime"].location.height.value, 0, rtol=0, atol=1e-9) @pytest.mark.parametrize("table_types", (Table, QTable)) def test_io_time_read_fits_scale(self, table_types): """ Test handling of 'GPS' and 'LOCAL' time scales which are recognized by the FITS standard but are not native to astropy. """ # GPS scale column gps_time = np.array([630720013, 630720014]) c = fits.Column( name="gps_time", format="D", unit="s", coord_type="GPS", coord_unit="s", time_ref_pos="TOPOCENTER", array=gps_time, ) cards = [("OBSGEO-L", 0), ("OBSGEO-B", 0), ("OBSGEO-H", 0)] bhdu = fits.BinTableHDU.from_columns([c], header=fits.Header(cards)) bhdu.writeto(self.temp("time.fits"), overwrite=True) with pytest.warns( AstropyUserWarning, match='FITS recognized time scale value "GPS"' ) as w: tm = table_types.read(self.temp("time.fits"), astropy_native=True) assert len(w) == 1 assert isinstance(tm["gps_time"], Time) assert tm["gps_time"].format == "gps" assert tm["gps_time"].scale == "tai" assert (tm["gps_time"].value == gps_time).all() # LOCAL scale column local_time = np.array([1, 2]) c = fits.Column( name="local_time", format="D", unit="d", coord_type="LOCAL", coord_unit="d", time_ref_pos="RELOCATABLE", array=local_time, ) bhdu = fits.BinTableHDU.from_columns([c]) bhdu.writeto(self.temp("time.fits"), overwrite=True) tm = table_types.read(self.temp("time.fits"), astropy_native=True) assert isinstance(tm["local_time"], Time) assert tm["local_time"].format == "mjd" assert tm["local_time"].scale == "local" assert (tm["local_time"].value == local_time).all() @pytest.mark.parametrize("table_types", (Table, QTable)) def test_io_time_read_fits_location_warnings(self, table_types): """ Test warnings for time column reference position. """ # Time reference position "TOPOCENTER" without corresponding # observatory position. c = fits.Column( name="datetime", format="A29", coord_type="TT", time_ref_pos="TOPOCENTER", array=self.time, ) bhdu = fits.BinTableHDU.from_columns([c]) bhdu.writeto(self.temp("time.fits"), overwrite=True) with pytest.warns( AstropyUserWarning, match="observatory position is not properly specified", ) as w: table_types.read(self.temp("time.fits"), astropy_native=True) assert len(w) == 1 # Warning for default value of time reference position "TOPOCENTER" # not generated when there is no specified observatory position. c = fits.Column(name="datetime", format="A29", coord_type="TT", array=self.time) bhdu = fits.BinTableHDU.from_columns([c]) bhdu.writeto(self.temp("time.fits"), overwrite=True) table_types.read(self.temp("time.fits"), astropy_native=True)
9578e15f088f66d89f332a1001cffb532ba97be40ebe51fecd73ea89b1e44e14
# Licensed under a 3-clause BSD style license - see LICENSE.rst import pytest from astropy import __version__ as version from astropy.io.fits.scripts import fitsinfo from .conftest import FitsTestCase class TestFitsinfo(FitsTestCase): def test_help(self): with pytest.raises(SystemExit) as e: fitsinfo.main(["-h"]) assert e.value.code == 0 def test_version(self, capsys): with pytest.raises(SystemExit) as e: fitsinfo.main(["--version"]) out = capsys.readouterr()[0] assert out == f"fitsinfo {version}" assert e.value.code == 0 def test_onefile(self, capsys): fitsinfo.main([self.data("arange.fits")]) out, err = capsys.readouterr() out = out.splitlines() assert len(out) == 3 assert out[1].startswith( "No. Name Ver Type Cards Dimensions Format" ) assert out[2].startswith( " 0 PRIMARY 1 PrimaryHDU 7 (11, 10, 7) int32" ) def test_multiplefiles(self, capsys): fitsinfo.main([self.data("arange.fits"), self.data("ascii.fits")]) out, err = capsys.readouterr() out = out.splitlines() assert len(out) == 8 assert out[1].startswith( "No. Name Ver Type Cards Dimensions Format" ) assert out[2].startswith( " 0 PRIMARY 1 PrimaryHDU 7 (11, 10, 7) int32" ) assert out[3] == "" assert out[7].startswith( " 1 1 TableHDU 20 5R x 2C [E10.4, I5]" )
01c2acbff3c56a00403c1fc8063f2185a156ead2c1555ec0dd2e714564b0ec9d
# Licensed under a 3-clause BSD style license - see PYFITS.rst import platform import numpy as np import pytest from numpy.testing import assert_array_equal from astropy.io import fits from .conftest import FitsTestCase class TestUintFunctions(FitsTestCase): @classmethod def setup_class(cls): cls.utypes = ("u2", "u4", "u8") cls.utype_map = {"u2": np.uint16, "u4": np.uint32, "u8": np.uint64} cls.itype_map = {"u2": np.int16, "u4": np.int32, "u8": np.int64} cls.format_map = {"u2": "I", "u4": "J", "u8": "K"} # Test of 64-bit compressed image is disabled. cfitsio library doesn't # like it @pytest.mark.parametrize( ("utype", "compressed"), [("u2", False), ("u4", False), ("u8", False), ("u2", True), ("u4", True)], ) # ,('u8',True)]) def test_uint(self, utype, compressed): bits = 8 * int(utype[1]) if platform.architecture()[0] == "64bit" or bits != 64: if compressed: hdu = fits.CompImageHDU( np.array([-3, -2, -1, 0, 1, 2, 3], dtype=np.int64) ) hdu_number = 1 else: hdu = fits.PrimaryHDU( np.array([-3, -2, -1, 0, 1, 2, 3], dtype=np.int64) ) hdu_number = 0 hdu.scale(f"int{bits:d}", "", bzero=2 ** (bits - 1)) hdu.writeto(self.temp("tempfile.fits"), overwrite=True) with fits.open(self.temp("tempfile.fits"), uint=True) as hdul: assert hdul[hdu_number].data.dtype == self.utype_map[utype] assert ( hdul[hdu_number].data == np.array( [(2**bits) - 3, (2**bits) - 2, (2**bits) - 1, 0, 1, 2, 3], dtype=self.utype_map[utype], ) ).all() hdul.writeto(self.temp("tempfile1.fits")) with fits.open(self.temp("tempfile1.fits"), uint16=True) as hdul1: d1 = hdul[hdu_number].data d2 = hdul1[hdu_number].data assert (d1 == d2).all() if not compressed: # TODO: Enable these lines if CompImageHDUs ever grow # .section support sec = hdul[hdu_number].section[:1] assert sec.dtype.name == f"uint{bits}" assert (sec == d1[:1]).all() @pytest.mark.parametrize("utype", ("u2", "u4", "u8")) def test_uint_columns(self, utype): """Test basic functionality of tables with columns containing pseudo-unsigned integers. See https://github.com/astropy/astropy/pull/906 """ bits = 8 * int(utype[1]) if platform.architecture()[0] == "64bit" or bits != 64: bzero = self.utype_map[utype](2 ** (bits - 1)) one = self.utype_map[utype](1) u0 = np.arange(bits + 1, dtype=self.utype_map[utype]) u = 2**u0 - one if bits == 64: u[63] = bzero - one u[64] = u[63] + u[63] + one uu = (u - bzero).view(self.itype_map[utype]) # Construct a table from explicit column col = fits.Column( name=utype, array=u, format=self.format_map[utype], bzero=bzero ) table = fits.BinTableHDU.from_columns([col]) assert (table.data[utype] == u).all() # This used to be table.data.base, but now after adding a table to # a BinTableHDU it gets stored as a view of the original table, # even if the original was already a FITS_rec. So now we need # table.data.base.base assert (table.data.base.base[utype] == uu).all() hdu0 = fits.PrimaryHDU() hdulist = fits.HDUList([hdu0, table]) hdulist.writeto(self.temp("tempfile.fits"), overwrite=True) # Test write of unsigned int del hdulist with fits.open(self.temp("tempfile.fits"), uint=True) as hdulist2: hdudata = hdulist2[1].data assert (hdudata[utype] == u).all() assert hdudata[utype].dtype == self.utype_map[utype] assert (hdudata.base[utype] == uu).all() # Construct recarray then write out that. v = u.view(dtype=[(utype, self.utype_map[utype])]) fits.writeto(self.temp("tempfile2.fits"), v, overwrite=True) with fits.open(self.temp("tempfile2.fits"), uint=True) as hdulist3: hdudata3 = hdulist3[1].data assert (hdudata3.base[utype] == table.data.base.base[utype]).all() assert (hdudata3[utype] == table.data[utype]).all() assert (hdudata3[utype] == u).all() def test_uint_slice(self): """ Fix for https://github.com/astropy/astropy/issues/5490 if data is sliced first, make sure the data is still converted as uint """ # create_data: dataref = np.arange(2**16, dtype=np.uint16) tbhdu = fits.BinTableHDU.from_columns( [ fits.Column( name="a", format="I", array=np.arange(2**16, dtype=np.int16) ), fits.Column( name="b", format="I", bscale=1, bzero=2**15, array=dataref ), ] ) tbhdu.writeto(self.temp("test_scaled_slicing.fits")) with fits.open(self.temp("test_scaled_slicing.fits")) as hdulist: data = hdulist[1].data assert_array_equal(data["b"], dataref) sel = data["a"] >= 0 assert_array_equal(data[sel]["b"], dataref[sel]) assert data[sel]["b"].dtype == dataref[sel].dtype with fits.open(self.temp("test_scaled_slicing.fits")) as hdulist: data = hdulist[1].data assert_array_equal(data[sel]["b"], dataref[sel]) assert data[sel]["b"].dtype == dataref[sel].dtype
be7c804cee0cf1067d16ca5627615a3ed38ba3a97804309f7346d53bafd6dbbc
# Licensed under a 3-clause BSD style license - see PYFITS.rst import os import numpy as np import pytest from astropy.io import fits from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH from .conftest import FitsTestCase class TestTildePaths(FitsTestCase): """ Exercises a few functions, just to ensure they run with tilde paths (i.e. paths like '~/filename.fits'). Also exercises a few subclasses. Most of the rest of the testing of tilde path handling is done by adding `home_is_data` and `home_is_temp` fixtures (defined and explained in __init__.py) to appropriate test cases, so that they are run both with and without tilde paths. """ def test_fits_info(self, home_is_data): fits.info(self.data("tb.fits"), output=False) def test_fits_printdiff(self, home_is_data): fits.printdiff(self.data("test0.fits"), self.data("tb.fits")) def test_fits_get_data(self, home_is_data): fits.getdata(self.data("test0.fits")) fits.getdata(self.data("test0.fits"), header=True) def test_fits_get_header(self, home_is_data): fits.getheader(self.data("test0.fits")) fits.getheader(self.data("tb.fits"), ext=1) def test_fits_get_set_del_val(self, home_is_temp): self.copy_file("test0.fits") filename = self.temp("test0.fits") assert fits.getval(filename, "shutter") == "B" fits.setval(filename, "shutter", value="C") assert fits.getval(filename, "shutter") == "C" with pytest.raises(KeyError): fits.getval(filename, "missing") fits.setval(filename, "missing", value="C") assert fits.getval(filename, "missing") == "C" fits.delval(filename, "missing") with pytest.raises(KeyError): fits.getval(filename, "missing") def test_header_formatter(self, home_is_data): from astropy.io.fits.scripts import fitsheader hf = fitsheader.HeaderFormatter(self.data("zerowidth.fits")) hf.close() thf = fitsheader.TableHeaderFormatter(self.data("zerowidth.fits")) thf.close() def test_BinTableHDU_dump_load(self, home_is_temp): bright = np.rec.array( [ (1, "Serius", -1.45, "A1V"), (2, "Canopys", -0.73, "F0Ib"), (3, "Rigil Kent", -0.1, "G2V"), ], formats="int16,a20,float32,a10", names="order,name,mag,Sp", ) hdu = fits.BinTableHDU(bright) hdu.dump(self.temp("data.txt"), self.temp("cdfile.txt"), self.temp("hfile.txt")) with pytest.raises(OSError, match="already exists"): hdu.dump( self.temp("data.txt"), self.temp("cdfile.txt"), self.temp("hfile.txt"), overwrite=False, ) hdu.dump( self.temp("data.txt"), self.temp("cdfile.txt"), self.temp("hfile.txt"), overwrite=True, ) fits.BinTableHDU.load( self.temp("data.txt"), self.temp("cdfile.txt"), self.temp("hfile.txt") ) self.copy_file("tb.fits") with fits.open(self.temp("tb.fits")) as hdul: hdu = hdul[1] hdu.dump() assert os.path.exists(os.path.expanduser(self.temp("tb.txt"))) def test_BinTableHDU_writeto(self, home_is_temp): bright = np.rec.array( [ (1, "Serius", -1.45, "A1V"), (2, "Canopys", -0.73, "F0Ib"), (3, "Rigil Kent", -0.1, "G2V"), ], formats="int16,a20,float32,a10", names="order,name,mag,Sp", ) hdu = fits.BinTableHDU(bright) hdu.writeto(self.temp("table.fits")) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): hdu.writeto(self.temp("table.fits"), overwrite=False) hdu.writeto(self.temp("table.fits"), overwrite=True) def test_TableHDU_writeto(self, home_is_temp): bright = np.rec.array( [ (1, "Serius", -1.45, "A1V"), (2, "Canopys", -0.73, "F0Ib"), (3, "Rigil Kent", -0.1, "G2V"), ], formats="int16,a20,float32,a10", names="order,name,mag,Sp", ) hdu = fits.TableHDU.from_columns(bright, nrows=2) hdu.writeto(self.temp("table.fits")) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): hdu.writeto(self.temp("table.fits"), overwrite=False) hdu.writeto(self.temp("table.fits"), overwrite=True) def fits_tabledump(self, home_is_temp): self.copy_file("tb.fits") fits.tabledump( self.temp("tb.fits"), self.temp("data.txt"), self.temp("cdfile.txt"), self.temp("hfile.txt"), ) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): fits.tabledump( self.temp("tb.fits"), self.temp("data.txt"), self.temp("cdfile.txt"), self.temp("hfile.txt"), overwrite=False, ) fits.tabledump( self.temp("tb.fits"), self.temp("data.txt"), self.temp("cdfile.txt"), self.temp("hfile.txt"), overwrite=True, ) fits.tableload( self.temp("data.txt"), self.temp("cdfile.txt"), self.temp("hfile.txt") ) def test_ImageHDU_writeto(self, home_is_temp): hdu = fits.ImageHDU(np.arange(100).reshape((10, 10))) hdu.writeto(self.temp("image.fits")) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): hdu.writeto(self.temp("image.fits"), overwrite=False) hdu.writeto(self.temp("image.fits"), overwrite=True) def test_CompImageHDU_writeto(self, home_is_temp): hdu = fits.CompImageHDU(np.arange(100).reshape((10, 10)).astype(np.int32)) hdu.writeto(self.temp("image.fits")) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): hdu.writeto(self.temp("image.fits"), overwrite=False) hdu.writeto(self.temp("image.fits"), overwrite=True)
beea6a8df8a7e7227b83e998b8708022ef62f91989264f96ea38d7cfc81a6afd
# Licensed under a 3-clause BSD style license - see PYFITS.rst import math import os import re import time import numpy as np import pytest from numpy.testing import assert_equal from astropy.io import fits from astropy.io.fits.hdu.compressed import DITHER_SEED_CHECKSUM, SUBTRACTIVE_DITHER_1 from astropy.utils.compat.optional_deps import HAS_SCIPY from astropy.utils.data import get_pkg_data_filename from astropy.utils.exceptions import AstropyUserWarning from .conftest import FitsTestCase from .test_table import comparerecords class TestImageFunctions(FitsTestCase): def test_constructor_name_arg(self): """Like the test of the same name in test_table.py""" hdu = fits.ImageHDU() assert hdu.name == "" assert "EXTNAME" not in hdu.header hdu.name = "FOO" assert hdu.name == "FOO" assert hdu.header["EXTNAME"] == "FOO" # Passing name to constructor hdu = fits.ImageHDU(name="FOO") assert hdu.name == "FOO" assert hdu.header["EXTNAME"] == "FOO" # And overriding a header with a different extname hdr = fits.Header() hdr["EXTNAME"] = "EVENTS" hdu = fits.ImageHDU(header=hdr, name="FOO") assert hdu.name == "FOO" assert hdu.header["EXTNAME"] == "FOO" def test_constructor_ver_arg(self): def assert_ver_is(hdu, reference_ver): assert hdu.ver == reference_ver assert hdu.header["EXTVER"] == reference_ver hdu = fits.ImageHDU() assert hdu.ver == 1 # defaults to 1 assert "EXTVER" not in hdu.header hdu.ver = 1 assert_ver_is(hdu, 1) # Passing name to constructor hdu = fits.ImageHDU(ver=2) assert_ver_is(hdu, 2) # And overriding a header with a different extver hdr = fits.Header() hdr["EXTVER"] = 3 hdu = fits.ImageHDU(header=hdr, ver=4) assert_ver_is(hdu, 4) # The header card is not overridden if ver is None or not passed in hdr = fits.Header() hdr["EXTVER"] = 5 hdu = fits.ImageHDU(header=hdr, ver=None) assert_ver_is(hdu, 5) hdu = fits.ImageHDU(header=hdr) assert_ver_is(hdu, 5) def test_constructor_copies_header(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/153 Ensure that a header from one HDU is copied when used to initialize new HDU. """ ifd = fits.HDUList(fits.PrimaryHDU()) phdr = ifd[0].header phdr["FILENAME"] = "labq01i3q_rawtag.fits" primary_hdu = fits.PrimaryHDU(header=phdr) ofd = fits.HDUList(primary_hdu) ofd[0].header["FILENAME"] = "labq01i3q_flt.fits" # Original header should be unchanged assert phdr["FILENAME"] == "labq01i3q_rawtag.fits" def test_open(self): # The function "open" reads a FITS file into an HDUList object. There # are three modes to open: "readonly" (the default), "append", and # "update". # Open a file read-only (the default mode), the content of the FITS # file are read into memory. r = fits.open(self.data("test0.fits")) # readonly # data parts are latent instantiation, so if we close the HDUList # without touching data, data can not be accessed. r.close() with pytest.raises(IndexError) as exc_info: r[1].data[:2, :2] # Check that the exception message is the enhanced version, not the # default message from list.__getitem__ assert str(exc_info.value) == ( "HDU not found, possibly because the index " "is out of range, or because the file was " "closed before all HDUs were read" ) def test_open_2(self): r = fits.open(self.data("test0.fits")) info = [(0, "PRIMARY", 1, "PrimaryHDU", 138, (), "", "")] + [ (x, "SCI", x, "ImageHDU", 61, (40, 40), "int16", "") for x in range(1, 5) ] try: assert r.info(output=False) == info finally: r.close() def test_open_3(self): # Test that HDUs cannot be accessed after the file was closed r = fits.open(self.data("test0.fits")) r.close() with pytest.raises(IndexError) as exc_info: r[1] # Check that the exception message is the enhanced version, not the # default message from list.__getitem__ assert str(exc_info.value) == ( "HDU not found, possibly because the index " "is out of range, or because the file was " "closed before all HDUs were read" ) # Test that HDUs can be accessed with lazy_load_hdus=False r = fits.open(self.data("test0.fits"), lazy_load_hdus=False) r.close() assert isinstance(r[1], fits.ImageHDU) assert len(r) == 5 with pytest.raises(IndexError) as exc_info: r[6] assert str(exc_info.value) == "list index out of range" # And the same with the global config item assert fits.conf.lazy_load_hdus # True by default fits.conf.lazy_load_hdus = False try: r = fits.open(self.data("test0.fits")) r.close() assert isinstance(r[1], fits.ImageHDU) assert len(r) == 5 finally: fits.conf.lazy_load_hdus = True def test_fortran_array(self): # Test that files are being correctly written+read for "C" and "F" order arrays a = np.arange(21).reshape(3, 7) b = np.asfortranarray(a) afits = self.temp("a_str.fits") bfits = self.temp("b_str.fits") # writing to str specified files fits.PrimaryHDU(data=a).writeto(afits) fits.PrimaryHDU(data=b).writeto(bfits) np.testing.assert_array_equal(fits.getdata(afits), a) np.testing.assert_array_equal(fits.getdata(bfits), a) # writing to fileobjs aafits = self.temp("a_fileobj.fits") bbfits = self.temp("b_fileobj.fits") with open(aafits, mode="wb") as fd: fits.PrimaryHDU(data=a).writeto(fd) with open(bbfits, mode="wb") as fd: fits.PrimaryHDU(data=b).writeto(fd) np.testing.assert_array_equal(fits.getdata(aafits), a) np.testing.assert_array_equal(fits.getdata(bbfits), a) def test_fortran_array_non_contiguous(self): # Test that files are being correctly written+read for 'C' and 'F' order arrays a = np.arange(105).reshape(3, 5, 7) b = np.asfortranarray(a) # writing to str specified files afits = self.temp("a_str_slice.fits") bfits = self.temp("b_str_slice.fits") fits.PrimaryHDU(data=a[::2, ::2]).writeto(afits) fits.PrimaryHDU(data=b[::2, ::2]).writeto(bfits) np.testing.assert_array_equal(fits.getdata(afits), a[::2, ::2]) np.testing.assert_array_equal(fits.getdata(bfits), a[::2, ::2]) # writing to fileobjs aafits = self.temp("a_fileobj_slice.fits") bbfits = self.temp("b_fileobj_slice.fits") with open(aafits, mode="wb") as fd: fits.PrimaryHDU(data=a[::2, ::2]).writeto(fd) with open(bbfits, mode="wb") as fd: fits.PrimaryHDU(data=b[::2, ::2]).writeto(fd) np.testing.assert_array_equal(fits.getdata(aafits), a[::2, ::2]) np.testing.assert_array_equal(fits.getdata(bbfits), a[::2, ::2]) def test_primary_with_extname(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/151 Tests that the EXTNAME keyword works with Primary HDUs as well, and interacts properly with the .name attribute. For convenience hdulist['PRIMARY'] will still refer to the first HDU even if it has an EXTNAME not equal to 'PRIMARY'. """ prihdr = fits.Header([("EXTNAME", "XPRIMARY"), ("EXTVER", 1)]) hdul = fits.HDUList([fits.PrimaryHDU(header=prihdr)]) assert "EXTNAME" in hdul[0].header assert hdul[0].name == "XPRIMARY" assert hdul[0].name == hdul[0].header["EXTNAME"] info = [(0, "XPRIMARY", 1, "PrimaryHDU", 5, (), "", "")] assert hdul.info(output=False) == info assert hdul["PRIMARY"] is hdul["XPRIMARY"] assert hdul["PRIMARY"] is hdul[("XPRIMARY", 1)] hdul[0].name = "XPRIMARY2" assert hdul[0].header["EXTNAME"] == "XPRIMARY2" hdul.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as hdul: assert hdul[0].name == "XPRIMARY2" def test_io_manipulation(self): # Get a keyword value. An extension can be referred by name or by # number. Both extension and keyword names are case insensitive. with fits.open(self.data("test0.fits")) as r: assert r["primary"].header["naxis"] == 0 assert r[0].header["naxis"] == 0 # If there are more than one extension with the same EXTNAME value, # the EXTVER can be used (as the second argument) to distinguish # the extension. assert r["sci", 1].header["detector"] == 1 # append (using "update()") a new card r[0].header["xxx"] = 1.234e56 assert ( "\n".join(str(x) for x in r[0].header.cards[-3:]) == "EXPFLAG = 'NORMAL ' / Exposure interruption indicator \n" "FILENAME= 'vtest3.fits' / File name \n" "XXX = 1.234E+56 " ) # rename a keyword r[0].header.rename_keyword("filename", "fname") pytest.raises(ValueError, r[0].header.rename_keyword, "fname", "history") pytest.raises(ValueError, r[0].header.rename_keyword, "fname", "simple") r[0].header.rename_keyword("fname", "filename") # get a subsection of data assert np.array_equal( r[2].data[:3, :3], np.array( [[349, 349, 348], [349, 349, 347], [347, 350, 349]], dtype=np.int16 ), ) # We can create a new FITS file by opening a new file with "append" # mode. with fits.open(self.temp("test_new.fits"), mode="append") as n: # Append the primary header and the 2nd extension to the new # file. n.append(r[0]) n.append(r[2]) # The flush method will write the current HDUList object back # to the newly created file on disk. The HDUList is still open # and can be further operated. n.flush() assert n[1].data[1, 1] == 349 # modify a data point n[1].data[1, 1] = 99 # When the file is closed, the most recent additions of # extension(s) since last flush() will be appended, but any HDU # already existed at the last flush will not be modified del n # If an existing file is opened with "append" mode, like the # readonly mode, the HDU's will be read into the HDUList which can # be modified in memory but can not be written back to the original # file. A file opened with append mode can only add new HDU's. os.rename(self.temp("test_new.fits"), self.temp("test_append.fits")) with fits.open(self.temp("test_append.fits"), mode="append") as a: # The above change did not take effect since this was made # after the flush(). assert a[1].data[1, 1] == 349 a.append(r[1]) del a # When changes are made to an HDUList which was opened with # "update" mode, they will be written back to the original file # when a flush/close is called. os.rename(self.temp("test_append.fits"), self.temp("test_update.fits")) with fits.open(self.temp("test_update.fits"), mode="update") as u: # When the changes do not alter the size structures of the # original (or since last flush) HDUList, the changes are # written back "in place". assert u[0].header["rootname"] == "U2EQ0201T" u[0].header["rootname"] = "abc" assert u[1].data[1, 1] == 349 u[1].data[1, 1] = 99 u.flush() # If the changes affect the size structure, e.g. adding or # deleting HDU(s), header was expanded or reduced beyond # existing number of blocks (2880 bytes in each block), or # change the data size, the HDUList is written to a temporary # file, the original file is deleted, and the temporary file is # renamed to the original file name and reopened in the update # mode. To a user, these two kinds of updating writeback seem # to be the same, unless the optional argument in flush or # close is set to 1. del u[2] u.flush() # The write method in HDUList class writes the current HDUList, # with all changes made up to now, to a new file. This method # works the same disregard the mode the HDUList was opened # with. u.append(r[3]) u.writeto(self.temp("test_new.fits")) del u # Another useful new HDUList method is readall. It will "touch" the # data parts in all HDUs, so even if the HDUList is closed, we can # still operate on the data. with fits.open(self.data("test0.fits")) as r: r.readall() assert r[1].data[1, 1] == 315 # create an HDU with data only data = np.ones((3, 5), dtype=np.float32) hdu = fits.ImageHDU(data=data, name="SCI") assert np.array_equal( hdu.data, np.array( [ [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], ], dtype=np.float32, ), ) # create an HDU with header and data # notice that the header has the right NAXIS's since it is constructed # with ImageHDU hdu2 = fits.ImageHDU(header=r[1].header, data=np.array([1, 2], dtype="int32")) assert ( "\n".join(str(x) for x in hdu2.header.cards[1:5]) == "BITPIX = 32 / array data type \n" "NAXIS = 1 / number of array dimensions \n" "NAXIS1 = 2 \n" "PCOUNT = 0 / number of parameters " ) def test_memory_mapping(self): # memory mapping f1 = fits.open(self.data("test0.fits"), memmap=1) f1.close() def test_verification_on_output(self): # verification on output # make a defect HDUList first x = fits.ImageHDU() hdu = fits.HDUList(x) # HDUList can take a list or one single HDU with pytest.warns( AstropyUserWarning, match=r"HDUList's 0th element is not a primary HDU\." ) as w: hdu.verify() assert len(w) == 3 with pytest.warns( AstropyUserWarning, match=r"HDUList's 0th element is not a primary HDU\. " r"Fixed by inserting one as 0th HDU\.", ) as w: hdu.writeto(self.temp("test_new2.fits"), "fix") assert len(w) == 3 def test_section(self): # section testing fs = fits.open(self.data("arange.fits")) assert np.array_equal(fs[0].section[3, 2, 5], 357) assert np.array_equal( fs[0].section[3, 2, :], np.array([352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362]), ) assert np.array_equal( fs[0].section[3, 2, 4:], np.array([356, 357, 358, 359, 360, 361, 362]) ) assert np.array_equal( fs[0].section[3, 2, :8], np.array([352, 353, 354, 355, 356, 357, 358, 359]) ) assert np.array_equal( fs[0].section[3, 2, -8:8], np.array([355, 356, 357, 358, 359]) ) assert np.array_equal( fs[0].section[3, 2:5, :], np.array( [ [352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362], [363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373], [374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384], ] ), ) assert np.array_equal( fs[0].section[3, :, :][:3, :3], np.array([[330, 331, 332], [341, 342, 343], [352, 353, 354]]), ) dat = fs[0].data assert np.array_equal(fs[0].section[3, 2:5, :8], dat[3, 2:5, :8]) assert np.array_equal(fs[0].section[3, 2:5, 3], dat[3, 2:5, 3]) assert np.array_equal( fs[0].section[3:6, :, :][:3, :3, :3], np.array( [ [[330, 331, 332], [341, 342, 343], [352, 353, 354]], [[440, 441, 442], [451, 452, 453], [462, 463, 464]], [[550, 551, 552], [561, 562, 563], [572, 573, 574]], ] ), ) assert np.array_equal( fs[0].section[:, :, :][:3, :2, :2], np.array( [[[0, 1], [11, 12]], [[110, 111], [121, 122]], [[220, 221], [231, 232]]] ), ) assert np.array_equal(fs[0].section[:, 2, :], dat[:, 2, :]) assert np.array_equal(fs[0].section[:, 2:5, :], dat[:, 2:5, :]) assert np.array_equal(fs[0].section[3:6, 3, :], dat[3:6, 3, :]) assert np.array_equal(fs[0].section[3:6, 3:7, :], dat[3:6, 3:7, :]) assert np.array_equal(fs[0].section[:, ::2], dat[:, ::2]) assert np.array_equal(fs[0].section[:, [1, 2, 4], 3], dat[:, [1, 2, 4], 3]) bool_index = np.array( [True, False, True, True, False, False, True, True, False, True] ) assert np.array_equal(fs[0].section[:, bool_index, :], dat[:, bool_index, :]) assert np.array_equal(fs[0].section[3:6, 3, :, ...], dat[3:6, 3, :, ...]) assert np.array_equal(fs[0].section[..., ::2], dat[..., ::2]) assert np.array_equal(fs[0].section[..., [1, 2, 4], 3], dat[..., [1, 2, 4], 3]) # Can we use negative indices? assert np.array_equal(fs[0].section[-1], dat[-1]) assert np.array_equal(fs[0].section[-9:-7], dat[-9:-7]) assert np.array_equal(fs[0].section[-4, -6:-3, -1], dat[-4, -6:-3, -1]) fs.close() def test_section_data_single(self): a = np.array([1]) hdu = fits.PrimaryHDU(a) hdu.writeto(self.temp("test_new.fits")) hdul = fits.open(self.temp("test_new.fits")) sec = hdul[0].section dat = hdul[0].data assert np.array_equal(sec[0], dat[0]) assert np.array_equal(sec[...], dat[...]) assert np.array_equal(sec[..., 0], dat[..., 0]) assert np.array_equal(sec[0, ...], dat[0, ...]) hdul.close() def test_section_data_square(self): a = np.arange(4).reshape(2, 2) hdu = fits.PrimaryHDU(a) hdu.writeto(self.temp("test_new.fits")) hdul = fits.open(self.temp("test_new.fits")) d = hdul[0] dat = hdul[0].data assert (d.section[:, :] == dat[:, :]).all() assert (d.section[0, :] == dat[0, :]).all() assert (d.section[1, :] == dat[1, :]).all() assert (d.section[:, 0] == dat[:, 0]).all() assert (d.section[:, 1] == dat[:, 1]).all() assert (d.section[0, 0] == dat[0, 0]).all() assert (d.section[0, 1] == dat[0, 1]).all() assert (d.section[1, 0] == dat[1, 0]).all() assert (d.section[1, 1] == dat[1, 1]).all() assert (d.section[0:1, 0:1] == dat[0:1, 0:1]).all() assert (d.section[0:2, 0:1] == dat[0:2, 0:1]).all() assert (d.section[0:1, 0:2] == dat[0:1, 0:2]).all() assert (d.section[0:2, 0:2] == dat[0:2, 0:2]).all() hdul.close() def test_section_data_cube(self): a = np.arange(18).reshape(2, 3, 3) hdu = fits.PrimaryHDU(a) hdu.writeto(self.temp("test_new.fits")) hdul = fits.open(self.temp("test_new.fits")) d = hdul[0] dat = hdul[0].data assert (d.section[:] == dat[:]).all() assert (d.section[:, :] == dat[:, :]).all() # Test that various combinations of indexing on the section are equal to # indexing the data. # Testing all combinations of scalar-index and [:] for each dimension. for idx1 in [slice(None), 0, 1]: for idx2 in [slice(None), 0, 1, 2]: for idx3 in [slice(None), 0, 1, 2]: nd_idx = (idx1, idx2, idx3) assert (d.section[nd_idx] == dat[nd_idx]).all() # Test all ways to slice the last dimension but keeping the first two. for idx3 in [ slice(0, 1), slice(0, 2), slice(0, 3), slice(1, 2), slice(1, 3), slice(2, 3), ]: nd_idx = (slice(None), slice(None), idx3) assert (d.section[nd_idx] == dat[nd_idx]).all() # Test various combinations (not exhaustive) to slice all dimensions. for idx1 in [slice(0, 1), slice(1, 2)]: for idx2 in [ slice(0, 1), slice(0, 2), slice(0, 3), slice(1, 2), slice(1, 3), ]: for idx3 in [ slice(0, 1), slice(0, 2), slice(0, 3), slice(1, 2), slice(1, 3), slice(2, 3), ]: nd_idx = (idx1, idx2, idx3) assert (d.section[nd_idx] == dat[nd_idx]).all() hdul.close() def test_section_data_four(self): a = np.arange(256).reshape(4, 4, 4, 4) hdu = fits.PrimaryHDU(a) hdu.writeto(self.temp("test_new.fits")) hdul = fits.open(self.temp("test_new.fits")) d = hdul[0] dat = hdul[0].data assert (d.section[:, :, :, :] == dat[:, :, :, :]).all() assert (d.section[:, :, :] == dat[:, :, :]).all() assert (d.section[:, :] == dat[:, :]).all() assert (d.section[:] == dat[:]).all() assert (d.section[0, :, :, :] == dat[0, :, :, :]).all() assert (d.section[0, :, 0, :] == dat[0, :, 0, :]).all() assert (d.section[:, :, 0, :] == dat[:, :, 0, :]).all() assert (d.section[:, 1, 0, :] == dat[:, 1, 0, :]).all() assert (d.section[:, :, :, 1] == dat[:, :, :, 1]).all() hdul.close() def test_section_data_scaled(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/143 This is like test_section_data_square but uses a file containing scaled image data, to test that sections can work correctly with scaled data. """ hdul = fits.open(self.data("scale.fits")) d = hdul[0] dat = hdul[0].data assert (d.section[:, :] == dat[:, :]).all() assert (d.section[0, :] == dat[0, :]).all() assert (d.section[1, :] == dat[1, :]).all() assert (d.section[:, 0] == dat[:, 0]).all() assert (d.section[:, 1] == dat[:, 1]).all() assert (d.section[0, 0] == dat[0, 0]).all() assert (d.section[0, 1] == dat[0, 1]).all() assert (d.section[1, 0] == dat[1, 0]).all() assert (d.section[1, 1] == dat[1, 1]).all() assert (d.section[0:1, 0:1] == dat[0:1, 0:1]).all() assert (d.section[0:2, 0:1] == dat[0:2, 0:1]).all() assert (d.section[0:1, 0:2] == dat[0:1, 0:2]).all() assert (d.section[0:2, 0:2] == dat[0:2, 0:2]).all() hdul.close() # Test without having accessed the full data first hdul = fits.open(self.data("scale.fits")) d = hdul[0] assert (d.section[:, :] == dat[:, :]).all() assert (d.section[0, :] == dat[0, :]).all() assert (d.section[1, :] == dat[1, :]).all() assert (d.section[:, 0] == dat[:, 0]).all() assert (d.section[:, 1] == dat[:, 1]).all() assert (d.section[0, 0] == dat[0, 0]).all() assert (d.section[0, 1] == dat[0, 1]).all() assert (d.section[1, 0] == dat[1, 0]).all() assert (d.section[1, 1] == dat[1, 1]).all() assert (d.section[0:1, 0:1] == dat[0:1, 0:1]).all() assert (d.section[0:2, 0:1] == dat[0:2, 0:1]).all() assert (d.section[0:1, 0:2] == dat[0:1, 0:2]).all() assert (d.section[0:2, 0:2] == dat[0:2, 0:2]).all() assert not d._data_loaded hdul.close() def test_do_not_scale_image_data(self): with fits.open(self.data("scale.fits"), do_not_scale_image_data=True) as hdul: assert hdul[0].data.dtype == np.dtype(">i2") with fits.open(self.data("scale.fits")) as hdul: assert hdul[0].data.dtype == np.dtype("float32") def test_append_uint_data(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/56 (BZERO and BSCALE added in the wrong location when appending scaled data) """ fits.writeto(self.temp("test_new.fits"), data=np.array([], dtype="uint8")) d = np.zeros([100, 100]).astype("uint16") fits.append(self.temp("test_new.fits"), data=d) with fits.open(self.temp("test_new.fits"), uint=True) as f: assert f[1].data.dtype == "uint16" def test_scale_with_explicit_bzero_bscale(self): """ Regression test for https://github.com/astropy/astropy/issues/6399 """ hdu2 = fits.ImageHDU(np.random.rand(100, 100)) # The line below raised an exception in astropy 2.0, so if it does not # raise an error here, that is progress. hdu2.scale(type="uint8", bscale=1, bzero=0) def test_uint_header_consistency(self): """ Regression test for https://github.com/astropy/astropy/issues/2305 This ensures that an HDU containing unsigned integer data always has the appropriate BZERO value in its header. """ for int_size in (16, 32, 64): # Just make an array of some unsigned ints that wouldn't fit in a # signed int array of the same bit width max_uint = (2**int_size) - 1 if int_size == 64: max_uint = np.uint64(int_size) dtype = f"uint{int_size}" arr = np.empty(100, dtype=dtype) arr.fill(max_uint) arr -= np.arange(100, dtype=dtype) uint_hdu = fits.PrimaryHDU(data=arr) assert np.all(uint_hdu.data == arr) assert uint_hdu.data.dtype.name == f"uint{int_size}" assert "BZERO" in uint_hdu.header assert uint_hdu.header["BZERO"] == (2 ** (int_size - 1)) filename = f"uint{int_size}.fits" uint_hdu.writeto(self.temp(filename)) with fits.open(self.temp(filename), uint=True) as hdul: new_uint_hdu = hdul[0] assert np.all(new_uint_hdu.data == arr) assert new_uint_hdu.data.dtype.name == f"uint{int_size}" assert "BZERO" in new_uint_hdu.header assert new_uint_hdu.header["BZERO"] == (2 ** (int_size - 1)) @pytest.mark.parametrize(("from_file"), (False, True)) @pytest.mark.parametrize(("do_not_scale"), (False,)) def test_uint_header_keywords_removed_after_bitpix_change( self, from_file, do_not_scale ): """ Regression test for https://github.com/astropy/astropy/issues/4974 BZERO/BSCALE should be removed if data is converted to a floating point type. Currently excluding the case where do_not_scale_image_data=True because it is not clear what the expectation should be. """ arr = np.zeros(100, dtype="uint16") if from_file: # To generate the proper input file we always want to scale the # data before writing it...otherwise when we open it will be # regular (signed) int data. tmp_uint = fits.PrimaryHDU(arr) filename = "unsigned_int.fits" tmp_uint.writeto(self.temp(filename)) with fits.open( self.temp(filename), do_not_scale_image_data=do_not_scale ) as f: uint_hdu = f[0] # Force a read before we close. _ = uint_hdu.data else: uint_hdu = fits.PrimaryHDU(arr, do_not_scale_image_data=do_not_scale) # Make sure appropriate keywords are in the header. See # https://github.com/astropy/astropy/pull/3916#issuecomment-122414532 # for discussion. assert "BSCALE" in uint_hdu.header assert "BZERO" in uint_hdu.header assert uint_hdu.header["BSCALE"] == 1 assert uint_hdu.header["BZERO"] == 32768 # Convert data to floating point... uint_hdu.data = uint_hdu.data * 1.0 # ...bitpix should be negative. assert uint_hdu.header["BITPIX"] < 0 # BSCALE and BZERO should NOT be in header any more. assert "BSCALE" not in uint_hdu.header assert "BZERO" not in uint_hdu.header # This is the main test...the data values should round trip # as zero. filename = "test_uint_to_float.fits" uint_hdu.writeto(self.temp(filename)) with fits.open(self.temp(filename)) as hdul: assert (hdul[0].data == 0).all() def test_blanks(self): """Test image data with blank spots in it (which should show up as NaNs in the data array. """ arr = np.zeros((10, 10), dtype=np.int32) # One row will be blanks arr[1] = 999 hdu = fits.ImageHDU(data=arr) hdu.header["BLANK"] = 999 hdu.writeto(self.temp("test_new.fits")) with fits.open(self.temp("test_new.fits")) as hdul: assert np.isnan(hdul[1].data[1]).all() def test_invalid_blanks(self): """ Test that invalid use of the BLANK keyword leads to an appropriate warning, and that the BLANK keyword is ignored when returning the HDU data. Regression test for https://github.com/astropy/astropy/issues/3865 """ arr = np.arange(5, dtype=np.float64) hdu = fits.PrimaryHDU(data=arr) hdu.header["BLANK"] = 2 with pytest.warns( AstropyUserWarning, match="Invalid 'BLANK' keyword in header" ) as w: hdu.writeto(self.temp("test_new.fits")) # Allow the HDU to be written, but there should be a warning # when writing a header with BLANK when then data is not # int assert len(w) == 1 # Should also get a warning when opening the file, and the BLANK # value should not be applied with pytest.warns( AstropyUserWarning, match="Invalid 'BLANK' keyword in header" ) as w: with fits.open(self.temp("test_new.fits")) as h: assert np.all(arr == h[0].data) assert len(w) == 1 @pytest.mark.filterwarnings("ignore:Invalid 'BLANK' keyword in header") def test_scale_back_with_blanks(self): """ Test that when auto-rescaling integer data with "blank" values (where the blanks are replaced by NaN in the float data), that the "BLANK" keyword is removed from the header. Further, test that when using the ``scale_back=True`` option the blank values are restored properly. Regression test for https://github.com/astropy/astropy/issues/3865 """ # Make the sample file arr = np.arange(5, dtype=np.int32) hdu = fits.PrimaryHDU(data=arr) hdu.scale("int16", bscale=1.23) # Creating data that uses BLANK is currently kludgy--a separate issue # TODO: Rewrite this test when scaling with blank support is better # supported # Let's just add a value to the data that should be converted to NaN # when it is read back in: filename = self.temp("test.fits") hdu.data[0] = 9999 hdu.header["BLANK"] = 9999 hdu.writeto(filename) with fits.open(filename) as hdul: data = hdul[0].data assert np.isnan(data[0]) with pytest.warns( fits.verify.VerifyWarning, match=r"Invalid 'BLANK' keyword in header" ): hdul.writeto(self.temp("test2.fits")) # Now reopen the newly written file. It should not have a 'BLANK' # keyword with fits.open(self.temp("test2.fits")) as hdul2: assert "BLANK" not in hdul2[0].header data = hdul2[0].data assert np.isnan(data[0]) # Finally, test that scale_back keeps the BLANKs correctly with fits.open(filename, scale_back=True, mode="update") as hdul3: data = hdul3[0].data # This emits warning that pytest cannot catch properly, so we # catch it with pytest.mark.filterwarnings above. assert np.isnan(data[0]) with fits.open(filename, do_not_scale_image_data=True) as hdul4: assert hdul4[0].header["BLANK"] == 9999 assert hdul4[0].header["BSCALE"] == 1.23 assert hdul4[0].data[0] == 9999 def test_bzero_with_floats(self): """Test use of the BZERO keyword in an image HDU containing float data. """ arr = np.zeros((10, 10)) - 1 hdu = fits.ImageHDU(data=arr) hdu.header["BZERO"] = 1.0 hdu.writeto(self.temp("test_new.fits")) with fits.open(self.temp("test_new.fits")) as hdul: arr += 1 assert (hdul[1].data == arr).all() def test_rewriting_large_scaled_image(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/84 and https://aeon.stsci.edu/ssb/trac/pyfits/ticket/101 """ hdul = fits.open(self.data("fixed-1890.fits")) orig_data = hdul[0].data hdul.writeto(self.temp("test_new.fits"), overwrite=True) hdul.close() hdul = fits.open(self.temp("test_new.fits")) assert (hdul[0].data == orig_data).all() hdul.close() # Just as before, but this time don't touch hdul[0].data before writing # back out--this is the case that failed in # https://aeon.stsci.edu/ssb/trac/pyfits/ticket/84 hdul = fits.open(self.data("fixed-1890.fits")) hdul.writeto(self.temp("test_new.fits"), overwrite=True) hdul.close() hdul = fits.open(self.temp("test_new.fits")) assert (hdul[0].data == orig_data).all() hdul.close() # Test opening/closing/reopening a scaled file in update mode hdul = fits.open(self.data("fixed-1890.fits"), do_not_scale_image_data=True) hdul.writeto( self.temp("test_new.fits"), overwrite=True, output_verify="silentfix" ) hdul.close() hdul = fits.open(self.temp("test_new.fits")) orig_data = hdul[0].data hdul.close() hdul = fits.open(self.temp("test_new.fits"), mode="update") hdul.close() hdul = fits.open(self.temp("test_new.fits")) assert (hdul[0].data == orig_data).all() hdul.close() def test_image_update_header(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/105 Replacing the original header to an image HDU and saving should update the NAXISn keywords appropriately and save the image data correctly. """ # Copy the original file before saving to it self.copy_file("test0.fits") with fits.open(self.temp("test0.fits"), mode="update") as hdul: orig_data = hdul[1].data.copy() hdr_copy = hdul[1].header.copy() del hdr_copy["NAXIS*"] hdul[1].header = hdr_copy with fits.open(self.temp("test0.fits")) as hdul: assert (orig_data == hdul[1].data).all() # The test below raised a `ResourceWarning: unclosed transport` exception # due to a bug in Python <=3.10 (cf. cpython#90476) @pytest.mark.filterwarnings("ignore:unclosed transport <asyncio.sslproto") def test_open_scaled_in_update_mode(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/119 (Don't update scaled image data if the data is not read) This ensures that merely opening and closing a file containing scaled image data does not cause any change to the data (or the header). Changes should only occur if the data is accessed. """ # Copy the original file before making any possible changes to it self.copy_file("scale.fits") mtime = os.stat(self.temp("scale.fits")).st_mtime time.sleep(1) fits.open(self.temp("scale.fits"), mode="update").close() # Ensure that no changes were made to the file merely by immediately # opening and closing it. assert mtime == os.stat(self.temp("scale.fits")).st_mtime # Insert a slight delay to ensure the mtime does change when the file # is changed time.sleep(1) hdul = fits.open(self.temp("scale.fits"), "update") orig_data = hdul[0].data hdul.close() # Now the file should be updated with the rescaled data assert mtime != os.stat(self.temp("scale.fits")).st_mtime hdul = fits.open(self.temp("scale.fits"), mode="update") assert hdul[0].data.dtype == np.dtype(">f4") assert hdul[0].header["BITPIX"] == -32 assert "BZERO" not in hdul[0].header assert "BSCALE" not in hdul[0].header assert (orig_data == hdul[0].data).all() # Try reshaping the data, then closing and reopening the file; let's # see if all the changes are preserved properly hdul[0].data.shape = (42, 10) hdul.close() hdul = fits.open(self.temp("scale.fits")) assert hdul[0].shape == (42, 10) assert hdul[0].data.dtype == np.dtype(">f4") assert hdul[0].header["BITPIX"] == -32 assert "BZERO" not in hdul[0].header assert "BSCALE" not in hdul[0].header hdul.close() def test_scale_back(self): """A simple test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/120 The scale_back feature for image HDUs. """ self.copy_file("scale.fits") with fits.open(self.temp("scale.fits"), mode="update", scale_back=True) as hdul: orig_bitpix = hdul[0].header["BITPIX"] orig_bzero = hdul[0].header["BZERO"] orig_bscale = hdul[0].header["BSCALE"] orig_data = hdul[0].data.copy() hdul[0].data[0] = 0 with fits.open(self.temp("scale.fits"), do_not_scale_image_data=True) as hdul: assert hdul[0].header["BITPIX"] == orig_bitpix assert hdul[0].header["BZERO"] == orig_bzero assert hdul[0].header["BSCALE"] == orig_bscale zero_point = int(math.floor(-orig_bzero / orig_bscale)) assert (hdul[0].data[0] == zero_point).all() with fits.open(self.temp("scale.fits")) as hdul: assert (hdul[0].data[1:] == orig_data[1:]).all() def test_image_none(self): """ Regression test for https://github.com/spacetelescope/PyFITS/issues/27 """ with fits.open(self.data("test0.fits")) as h: h[1].data h[1].data = None h[1].writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as h: assert h[1].data is None assert h[1].header["NAXIS"] == 0 assert "NAXIS1" not in h[1].header assert "NAXIS2" not in h[1].header def test_invalid_blank(self): """ Regression test for https://github.com/astropy/astropy/issues/2711 If the BLANK keyword contains an invalid value it should be ignored for any calculations (though a warning should be issued). """ data = np.arange(100, dtype=np.float64) hdu = fits.PrimaryHDU(data) hdu.header["BLANK"] = "nan" with pytest.warns( fits.verify.VerifyWarning, match=r"Invalid value for " r"'BLANK' keyword in header: 'nan'", ): hdu.writeto(self.temp("test.fits")) with pytest.warns(AstropyUserWarning) as w: with fits.open(self.temp("test.fits")) as hdul: assert np.all(hdul[0].data == data) assert len(w) == 2 msg = "Invalid value for 'BLANK' keyword in header" assert msg in str(w[0].message) msg = "Invalid 'BLANK' keyword" assert msg in str(w[1].message) def test_scaled_image_fromfile(self): """ Regression test for https://github.com/astropy/astropy/issues/2710 """ # Make some sample data a = np.arange(100, dtype=np.float32) hdu = fits.PrimaryHDU(data=a.copy()) hdu.scale(bscale=1.1) hdu.writeto(self.temp("test.fits")) with open(self.temp("test.fits"), "rb") as f: file_data = f.read() hdul = fits.HDUList.fromstring(file_data) assert np.allclose(hdul[0].data, a) def test_set_data(self): """ Test data assignment - issue #5087 """ im = fits.ImageHDU() ar = np.arange(12) im.data = ar def test_scale_bzero_with_int_data(self): """ Regression test for https://github.com/astropy/astropy/issues/4600 """ a = np.arange(100, 200, dtype=np.int16) hdu1 = fits.PrimaryHDU(data=a.copy()) hdu2 = fits.PrimaryHDU(data=a.copy()) # Previously the following line would throw a TypeError, # now it should be identical to the integer bzero case hdu1.scale("int16", bzero=99.0) hdu2.scale("int16", bzero=99) assert np.allclose(hdu1.data, hdu2.data) def test_scale_back_uint_assignment(self): """ Extend fix for #4600 to assignment to data Suggested by: https://github.com/astropy/astropy/pull/4602#issuecomment-208713748 """ a = np.arange(100, 200, dtype=np.uint16) fits.PrimaryHDU(a).writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits"), mode="update", scale_back=True) as ( hdu, ): hdu.data[:] = 0 assert np.allclose(hdu.data, 0) def test_hdu_creation_with_scalar(self): msg = r"data object array\(1\) should have at least one dimension" with pytest.raises(TypeError, match=msg): fits.ImageHDU(data=1) with pytest.raises(TypeError, match=msg): fits.PrimaryHDU(data=1) class TestCompressedImage(FitsTestCase): def test_empty(self): """ Regression test for https://github.com/astropy/astropy/issues/2595 """ hdu = fits.CompImageHDU() assert hdu.data is None hdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits"), mode="update") as hdul: assert len(hdul) == 2 assert isinstance(hdul[1], fits.CompImageHDU) assert hdul[1].data is None # Now test replacing the empty data with an array and see what # happens hdul[1].data = np.arange(100, dtype=np.int32) with fits.open(self.temp("test.fits")) as hdul: assert len(hdul) == 2 assert isinstance(hdul[1], fits.CompImageHDU) assert np.all(hdul[1].data == np.arange(100, dtype=np.int32)) @pytest.mark.parametrize( ("data", "compression_type", "quantize_level"), [ (np.zeros((2, 10, 10), dtype=np.float32), "RICE_1", 16), (np.zeros((2, 10, 10), dtype=np.float32), "GZIP_1", -0.01), (np.zeros((2, 10, 10), dtype=np.float32), "GZIP_2", -0.01), (np.zeros((100, 100)) + 1, "HCOMPRESS_1", 16), (np.zeros((10, 10)), "PLIO_1", 16), ], ) @pytest.mark.parametrize("byte_order", ["<", ">"]) def test_comp_image(self, data, compression_type, quantize_level, byte_order): data = data.newbyteorder(byte_order) primary_hdu = fits.PrimaryHDU() ofd = fits.HDUList(primary_hdu) chdu = fits.CompImageHDU( data, name="SCI", compression_type=compression_type, quantize_level=quantize_level, ) ofd.append(chdu) ofd.writeto(self.temp("test_new.fits"), overwrite=True) ofd.close() with fits.open(self.temp("test_new.fits")) as fd: assert (fd[1].data == data).all() assert fd[1].header["NAXIS"] == chdu.header["NAXIS"] assert fd[1].header["NAXIS1"] == chdu.header["NAXIS1"] assert fd[1].header["NAXIS2"] == chdu.header["NAXIS2"] assert fd[1].header["BITPIX"] == chdu.header["BITPIX"] @pytest.mark.skipif(not HAS_SCIPY, reason="requires scipy") @pytest.mark.remote_data def test_comp_image_quantize_level(self): """ Regression test for https://github.com/astropy/astropy/issues/5969 Test that quantize_level is used. """ import scipy from astropy.utils import minversion SCIPY_LT_1_10 = not minversion(scipy, "1.10dev0") np.random.seed(42) if SCIPY_LT_1_10: import scipy.misc # No lazy loading for SCIPY_LT_1_9 scipy_data = scipy.misc.ascent() else: scipy_data = scipy.datasets.ascent() data = scipy_data + np.random.randn(512, 512) * 10 fits.ImageHDU(data).writeto(self.temp("im1.fits")) fits.CompImageHDU( data, compression_type="RICE_1", quantize_method=1, quantize_level=-1, dither_seed=5, ).writeto(self.temp("im2.fits")) fits.CompImageHDU( data, compression_type="RICE_1", quantize_method=1, quantize_level=-100, dither_seed=5, ).writeto(self.temp("im3.fits")) im1 = fits.getdata(self.temp("im1.fits")) im2 = fits.getdata(self.temp("im2.fits")) im3 = fits.getdata(self.temp("im3.fits")) assert not np.array_equal(im2, im3) assert np.isclose(np.min(im1 - im2), -0.5, atol=1e-3) assert np.isclose(np.max(im1 - im2), 0.5, atol=1e-3) assert np.isclose(np.min(im1 - im3), -50, atol=1e-1) assert np.isclose(np.max(im1 - im3), 50, atol=1e-1) def test_comp_image_hcompression_1_invalid_data(self): """ Tests compression with the HCOMPRESS_1 algorithm with data that is not 2D and has a non-2D tile size. """ pytest.raises( ValueError, fits.CompImageHDU, np.zeros((2, 10, 10), dtype=np.float32), name="SCI", compression_type="HCOMPRESS_1", quantize_level=16, tile_size=[2, 10, 10], ) def test_comp_image_hcompress_image_stack(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/171 Tests that data containing more than two dimensions can be compressed with HCOMPRESS_1 so long as the user-supplied tile size can be flattened to two dimensions. """ cube = np.arange(300, dtype=np.float32).reshape(3, 10, 10) hdu = fits.CompImageHDU( data=cube, name="SCI", compression_type="HCOMPRESS_1", quantize_level=16, tile_size=[5, 5, 1], ) hdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as hdul: # HCOMPRESSed images are allowed to deviate from the original by # about 1/quantize_level of the RMS in each tile. assert np.abs(hdul["SCI"].data - cube).max() < 1.0 / 15.0 def test_subtractive_dither_seed(self): """ Regression test for https://github.com/spacetelescope/PyFITS/issues/32 Ensure that when floating point data is compressed with the SUBTRACTIVE_DITHER_1 quantization method that the correct ZDITHER0 seed is added to the header, and that the data can be correctly decompressed. """ array = np.arange(100.0).reshape(10, 10) csum = (array[0].view("uint8").sum() % 10000) + 1 hdu = fits.CompImageHDU( data=array, quantize_method=SUBTRACTIVE_DITHER_1, dither_seed=DITHER_SEED_CHECKSUM, ) hdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as hdul: assert isinstance(hdul[1], fits.CompImageHDU) assert "ZQUANTIZ" in hdul[1]._header assert hdul[1]._header["ZQUANTIZ"] == "SUBTRACTIVE_DITHER_1" assert "ZDITHER0" in hdul[1]._header assert hdul[1]._header["ZDITHER0"] == csum assert np.all(hdul[1].data == array) def test_disable_image_compression(self): with fits.open(self.data("comp.fits"), disable_image_compression=True) as hdul: # The compressed image HDU should show up as a BinTableHDU, but # *not* a CompImageHDU assert isinstance(hdul[1], fits.BinTableHDU) assert not isinstance(hdul[1], fits.CompImageHDU) with fits.open(self.data("comp.fits")) as hdul: assert isinstance(hdul[1], fits.CompImageHDU) def test_open_comp_image_in_update_mode(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/167 Similar to test_open_scaled_in_update_mode(), but specifically for compressed images. """ # Copy the original file before making any possible changes to it self.copy_file("comp.fits") mtime = os.stat(self.temp("comp.fits")).st_mtime time.sleep(1) fits.open(self.temp("comp.fits"), mode="update").close() # Ensure that no changes were made to the file merely by immediately # opening and closing it. assert mtime == os.stat(self.temp("comp.fits")).st_mtime @pytest.mark.slow def test_open_scaled_in_update_mode_compressed(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/88 2 Identical to test_open_scaled_in_update_mode() but with a compressed version of the scaled image. """ # Copy+compress the original file before making any possible changes to # it with fits.open(self.data("scale.fits"), do_not_scale_image_data=True) as hdul: chdu = fits.CompImageHDU(data=hdul[0].data, header=hdul[0].header) chdu.writeto(self.temp("scale.fits")) mtime = os.stat(self.temp("scale.fits")).st_mtime time.sleep(1) fits.open(self.temp("scale.fits"), mode="update").close() # Ensure that no changes were made to the file merely by immediately # opening and closing it. assert mtime == os.stat(self.temp("scale.fits")).st_mtime # Insert a slight delay to ensure the mtime does change when the file # is changed time.sleep(1) hdul = fits.open(self.temp("scale.fits"), "update") hdul[1].data hdul.close() # Now the file should be updated with the rescaled data assert mtime != os.stat(self.temp("scale.fits")).st_mtime hdul = fits.open(self.temp("scale.fits"), mode="update") assert hdul[1].data.dtype == np.dtype("float32") assert hdul[1].header["BITPIX"] == -32 assert "BZERO" not in hdul[1].header assert "BSCALE" not in hdul[1].header # Try reshaping the data, then closing and reopening the file; let's # see if all the changes are preserved properly hdul[1].data.shape = (42, 10) hdul.close() hdul = fits.open(self.temp("scale.fits")) assert hdul[1].shape == (42, 10) assert hdul[1].data.dtype == np.dtype("float32") assert hdul[1].header["BITPIX"] == -32 assert "BZERO" not in hdul[1].header assert "BSCALE" not in hdul[1].header hdul.close() def test_write_comp_hdu_direct_from_existing(self): with fits.open(self.data("comp.fits")) as hdul: hdul[1].writeto(self.temp("test.fits")) with fits.open(self.data("comp.fits")) as hdul1: with fits.open(self.temp("test.fits")) as hdul2: assert np.all(hdul1[1].data == hdul2[1].data) assert comparerecords( hdul1[1].compressed_data, hdul2[1].compressed_data ) def test_rewriting_large_scaled_image_compressed(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/88 1 Identical to test_rewriting_large_scaled_image() but with a compressed image. """ with fits.open( self.data("fixed-1890.fits"), do_not_scale_image_data=True ) as hdul: chdu = fits.CompImageHDU(data=hdul[0].data, header=hdul[0].header) chdu.writeto(self.temp("fixed-1890-z.fits")) hdul = fits.open(self.temp("fixed-1890-z.fits")) orig_data = hdul[1].data hdul.writeto(self.temp("test_new.fits"), overwrite=True) hdul.close() hdul = fits.open(self.temp("test_new.fits")) assert (hdul[1].data == orig_data).all() hdul.close() # Just as before, but this time don't touch hdul[0].data before writing # back out--this is the case that failed in # https://aeon.stsci.edu/ssb/trac/pyfits/ticket/84 hdul = fits.open(self.temp("fixed-1890-z.fits")) hdul.writeto(self.temp("test_new.fits"), overwrite=True) hdul.close() hdul = fits.open(self.temp("test_new.fits")) assert (hdul[1].data == orig_data).all() hdul.close() # Test opening/closing/reopening a scaled file in update mode hdul = fits.open(self.temp("fixed-1890-z.fits"), do_not_scale_image_data=True) hdul.writeto( self.temp("test_new.fits"), overwrite=True, output_verify="silentfix" ) hdul.close() hdul = fits.open(self.temp("test_new.fits")) orig_data = hdul[1].data hdul.close() hdul = fits.open(self.temp("test_new.fits"), mode="update") hdul.close() hdul = fits.open(self.temp("test_new.fits")) assert (hdul[1].data == orig_data).all() hdul.close() def test_scale_back_compressed(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/88 3 Identical to test_scale_back() but uses a compressed image. """ # Create a compressed version of the scaled image with fits.open(self.data("scale.fits"), do_not_scale_image_data=True) as hdul: chdu = fits.CompImageHDU(data=hdul[0].data, header=hdul[0].header) chdu.writeto(self.temp("scale.fits")) with fits.open(self.temp("scale.fits"), mode="update", scale_back=True) as hdul: orig_bitpix = hdul[1].header["BITPIX"] orig_bzero = hdul[1].header["BZERO"] orig_bscale = hdul[1].header["BSCALE"] orig_data = hdul[1].data.copy() hdul[1].data[0] = 0 with fits.open(self.temp("scale.fits"), do_not_scale_image_data=True) as hdul: assert hdul[1].header["BITPIX"] == orig_bitpix assert hdul[1].header["BZERO"] == orig_bzero assert hdul[1].header["BSCALE"] == orig_bscale zero_point = int(math.floor(-orig_bzero / orig_bscale)) assert (hdul[1].data[0] == zero_point).all() with fits.open(self.temp("scale.fits")) as hdul: assert (hdul[1].data[1:] == orig_data[1:]).all() # Extra test to ensure that after everything the data is still the # same as in the original uncompressed version of the image with fits.open(self.data("scale.fits")) as hdul2: # Recall we made the same modification to the data in hdul # above hdul2[0].data[0] = 0 assert (hdul[1].data == hdul2[0].data).all() def test_lossless_gzip_compression(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/198""" rng = np.random.default_rng(42) noise = rng.normal(size=(20, 20)) chdu1 = fits.CompImageHDU(data=noise, compression_type="GZIP_1") # First make a test image with lossy compression and make sure it # wasn't compressed perfectly. This shouldn't happen ever, but just to # make sure the test non-trivial. chdu1.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as h: assert np.abs(noise - h[1].data).max() > 0.0 del h chdu2 = fits.CompImageHDU( data=noise, compression_type="GZIP_1", quantize_level=0.0 ) # No quantization chdu2.writeto(self.temp("test.fits"), overwrite=True) with fits.open(self.temp("test.fits")) as h: assert (noise == h[1].data).all() def test_compression_column_tforms(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/199""" # Some interestingly tiled data so that some of it is quantized and # some of it ends up just getting gzip-compressed data2 = (np.arange(1, 8, dtype=np.float32) * 10)[:, np.newaxis] + np.arange( 1, 7 ) np.random.seed(1337) data1 = np.random.uniform(size=(6 * 4, 7 * 4)) data1[: data2.shape[0], : data2.shape[1]] = data2 chdu = fits.CompImageHDU(data1, compression_type="RICE_1", tile_size=(6, 7)) chdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits"), disable_image_compression=True) as h: assert re.match(r"^1PB\(\d+\)$", h[1].header["TFORM1"]) assert re.match(r"^1PB\(\d+\)$", h[1].header["TFORM2"]) def test_compression_update_header(self): """Regression test for https://github.com/spacetelescope/PyFITS/issues/23 """ self.copy_file("comp.fits") with fits.open(self.temp("comp.fits"), mode="update") as hdul: assert isinstance(hdul[1], fits.CompImageHDU) hdul[1].header["test1"] = "test" hdul[1]._header["test2"] = "test2" with fits.open(self.temp("comp.fits")) as hdul: assert "test1" in hdul[1].header assert hdul[1].header["test1"] == "test" assert "test2" in hdul[1].header assert hdul[1].header["test2"] == "test2" # Test update via index now: with fits.open(self.temp("comp.fits"), mode="update") as hdul: hdr = hdul[1].header hdr[hdr.index("TEST1")] = "foo" with fits.open(self.temp("comp.fits")) as hdul: assert hdul[1].header["TEST1"] == "foo" # Test slice updates with fits.open(self.temp("comp.fits"), mode="update") as hdul: hdul[1].header["TEST*"] = "qux" with fits.open(self.temp("comp.fits")) as hdul: assert list(hdul[1].header["TEST*"].values()) == ["qux", "qux"] with fits.open(self.temp("comp.fits"), mode="update") as hdul: hdr = hdul[1].header idx = hdr.index("TEST1") hdr[idx : idx + 2] = "bar" with fits.open(self.temp("comp.fits")) as hdul: assert list(hdul[1].header["TEST*"].values()) == ["bar", "bar"] # Test updating a specific COMMENT card duplicate with fits.open(self.temp("comp.fits"), mode="update") as hdul: hdul[1].header[("COMMENT", 1)] = "I am fire. I am death!" with fits.open(self.temp("comp.fits")) as hdul: assert hdul[1].header["COMMENT"][1] == "I am fire. I am death!" assert hdul[1]._header["COMMENT"][1] == "I am fire. I am death!" # Test deleting by keyword and by slice with fits.open(self.temp("comp.fits"), mode="update") as hdul: hdr = hdul[1].header del hdr["COMMENT"] idx = hdr.index("TEST1") del hdr[idx : idx + 2] with fits.open(self.temp("comp.fits")) as hdul: assert "COMMENT" not in hdul[1].header assert "COMMENT" not in hdul[1]._header assert "TEST1" not in hdul[1].header assert "TEST1" not in hdul[1]._header assert "TEST2" not in hdul[1].header assert "TEST2" not in hdul[1]._header def test_compression_update_header_with_reserved(self): """ Ensure that setting reserved keywords related to the table data structure on CompImageHDU image headers fails. """ def test_set_keyword(hdr, keyword, value): with pytest.warns(UserWarning) as w: hdr[keyword] = value assert len(w) == 1 assert str(w[0].message).startswith(f"Keyword {keyword!r} is reserved") assert keyword not in hdr with fits.open(self.data("comp.fits")) as hdul: hdr = hdul[1].header test_set_keyword(hdr, "TFIELDS", 8) test_set_keyword(hdr, "TTYPE1", "Foo") test_set_keyword(hdr, "ZCMPTYPE", "ASDF") test_set_keyword(hdr, "ZVAL1", "Foo") def test_compression_header_append(self): with fits.open(self.data("comp.fits")) as hdul: imghdr = hdul[1].header tblhdr = hdul[1]._header with pytest.warns(UserWarning, match="Keyword 'TFIELDS' is reserved") as w: imghdr.append("TFIELDS") assert len(w) == 1 assert "TFIELDS" not in imghdr imghdr.append(("FOO", "bar", "qux"), end=True) assert "FOO" in imghdr assert imghdr[-1] == "bar" assert "FOO" in tblhdr assert tblhdr[-1] == "bar" imghdr.append(("CHECKSUM", "abcd1234")) assert "CHECKSUM" in imghdr assert imghdr["CHECKSUM"] == "abcd1234" assert "CHECKSUM" not in tblhdr assert "ZHECKSUM" in tblhdr assert tblhdr["ZHECKSUM"] == "abcd1234" def test_compression_header_append2(self): """ Regression test for issue https://github.com/astropy/astropy/issues/5827 """ with fits.open(self.data("comp.fits")) as hdul: header = hdul[1].header while len(header) < 1000: header.append() # pad with grow room # Append stats to header: header.append(("Q1_OSAVG", 1, "[adu] quadrant 1 overscan mean")) header.append(("Q1_OSSTD", 1, "[adu] quadrant 1 overscan stddev")) header.append(("Q1_OSMED", 1, "[adu] quadrant 1 overscan median")) def test_compression_header_insert(self): with fits.open(self.data("comp.fits")) as hdul: imghdr = hdul[1].header tblhdr = hdul[1]._header # First try inserting a restricted keyword with pytest.warns(UserWarning, match="Keyword 'TFIELDS' is reserved") as w: imghdr.insert(1000, "TFIELDS") assert len(w) == 1 assert "TFIELDS" not in imghdr assert tblhdr.count("TFIELDS") == 1 # First try keyword-relative insert imghdr.insert("TELESCOP", ("OBSERVER", "Phil Plait")) assert "OBSERVER" in imghdr assert imghdr.index("OBSERVER") == imghdr.index("TELESCOP") - 1 assert "OBSERVER" in tblhdr assert tblhdr.index("OBSERVER") == tblhdr.index("TELESCOP") - 1 # Next let's see if an index-relative insert winds up being # sensible idx = imghdr.index("OBSERVER") imghdr.insert("OBSERVER", ("FOO",)) assert "FOO" in imghdr assert imghdr.index("FOO") == idx assert "FOO" in tblhdr assert tblhdr.index("FOO") == tblhdr.index("OBSERVER") - 1 def test_compression_header_set_before_after(self): with fits.open(self.data("comp.fits")) as hdul: imghdr = hdul[1].header tblhdr = hdul[1]._header with pytest.warns(UserWarning, match="Keyword 'ZBITPIX' is reserved ") as w: imghdr.set("ZBITPIX", 77, "asdf", after="XTENSION") assert len(w) == 1 assert "ZBITPIX" not in imghdr assert tblhdr.count("ZBITPIX") == 1 assert tblhdr["ZBITPIX"] != 77 # Move GCOUNT before PCOUNT (not that there's any reason you'd # *want* to do that, but it's just a test...) imghdr.set("GCOUNT", 99, before="PCOUNT") assert imghdr.index("GCOUNT") == imghdr.index("PCOUNT") - 1 assert imghdr["GCOUNT"] == 99 assert tblhdr.index("ZGCOUNT") == tblhdr.index("ZPCOUNT") - 1 assert tblhdr["ZGCOUNT"] == 99 assert tblhdr.index("PCOUNT") == 5 assert tblhdr.index("GCOUNT") == 6 assert tblhdr["GCOUNT"] == 1 imghdr.set("GCOUNT", 2, after="PCOUNT") assert imghdr.index("GCOUNT") == imghdr.index("PCOUNT") + 1 assert imghdr["GCOUNT"] == 2 assert tblhdr.index("ZGCOUNT") == tblhdr.index("ZPCOUNT") + 1 assert tblhdr["ZGCOUNT"] == 2 assert tblhdr.index("PCOUNT") == 5 assert tblhdr.index("GCOUNT") == 6 assert tblhdr["GCOUNT"] == 1 def test_compression_header_append_commentary(self): """ Regression test for https://github.com/astropy/astropy/issues/2363 """ hdu = fits.CompImageHDU(np.array([0], dtype=np.int32)) hdu.header["COMMENT"] = "hello world" assert hdu.header["COMMENT"] == ["hello world"] hdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as hdul: assert hdul[1].header["COMMENT"] == ["hello world"] def test_compression_with_gzip_column(self): """ Regression test for https://github.com/spacetelescope/PyFITS/issues/71 """ arr = np.zeros((2, 7000), dtype="float32") # The first row (which will be the first compressed tile) has a very # wide range of values that will be difficult to quantize, and should # result in use of a GZIP_COMPRESSED_DATA column arr[0] = np.linspace(0, 1, 7000) arr[1] = np.random.normal(size=7000) hdu = fits.CompImageHDU(data=arr) hdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as hdul: comp_hdu = hdul[1] # GZIP-compressed tile should compare exactly assert np.all(comp_hdu.data[0] == arr[0]) # The second tile uses lossy compression and may be somewhat off, # so we don't bother comparing it exactly def test_duplicate_compression_header_keywords(self): """ Regression test for https://github.com/astropy/astropy/issues/2750 Tests that the fake header (for the compressed image) can still be read even if the real header contained a duplicate ZTENSION keyword (the issue applies to any keyword specific to the compression convention, however). """ arr = np.arange(100, dtype=np.int32) hdu = fits.CompImageHDU(data=arr) header = hdu._header # append the duplicate keyword hdu._header.append(("ZTENSION", "IMAGE")) hdu.writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits")) as hdul: assert header == hdul[1]._header # There's no good reason to have a duplicate keyword, but # technically it isn't invalid either :/ assert hdul[1]._header.count("ZTENSION") == 2 def test_scale_bzero_with_compressed_int_data(self): """ Regression test for https://github.com/astropy/astropy/issues/4600 and https://github.com/astropy/astropy/issues/4588 Identical to test_scale_bzero_with_int_data() but uses a compressed image. """ a = np.arange(100, 200, dtype=np.int16) hdu1 = fits.CompImageHDU(data=a.copy()) hdu2 = fits.CompImageHDU(data=a.copy()) # Previously the following line would throw a TypeError, # now it should be identical to the integer bzero case hdu1.scale("int16", bzero=99.0) hdu2.scale("int16", bzero=99) assert np.allclose(hdu1.data, hdu2.data) def test_scale_back_compressed_uint_assignment(self): """ Extend fix for #4600 to assignment to data Identical to test_scale_back_uint_assignment() but uses a compressed image. Suggested by: https://github.com/astropy/astropy/pull/4602#issuecomment-208713748 """ a = np.arange(100, 200, dtype=np.uint16) fits.CompImageHDU(a).writeto(self.temp("test.fits")) with fits.open(self.temp("test.fits"), mode="update", scale_back=True) as hdul: hdul[1].data[:] = 0 assert np.allclose(hdul[1].data, 0) def test_compressed_header_missing_znaxis(self): a = np.arange(100, 200, dtype=np.uint16) comp_hdu = fits.CompImageHDU(a) comp_hdu._header.pop("ZNAXIS") with pytest.raises(KeyError): comp_hdu.compressed_data comp_hdu = fits.CompImageHDU(a) comp_hdu._header.pop("ZBITPIX") with pytest.raises(KeyError): comp_hdu.compressed_data def test_compressed_header_double_extname(self): """Test that a double EXTNAME with one default value does not mask the non-default value.""" with fits.open(self.data("double_ext.fits")) as hdul: hdu = hdul[1] # Raw header has 2 EXTNAME entries indices = hdu._header._keyword_indices["EXTNAME"] assert len(indices) == 2 # The non-default name should be returned. assert hdu.name == "ccd00" assert "EXTNAME" in hdu.header assert hdu.name == hdu.header["EXTNAME"] # There should be 1 non-default EXTNAME entries. indices = hdu.header._keyword_indices["EXTNAME"] assert len(indices) == 1 # Test header sync from property set. new_name = "NEW_NAME" hdu.name = new_name assert hdu.name == new_name assert hdu.header["EXTNAME"] == new_name assert hdu._header["EXTNAME"] == new_name assert hdu._image_header["EXTNAME"] == new_name # Check that setting the header will change the name property. hdu.header["EXTNAME"] = "NEW2" assert hdu.name == "NEW2" hdul.writeto(self.temp("tmp.fits"), overwrite=True) with fits.open(self.temp("tmp.fits")) as hdul1: hdu1 = hdul1[1] assert len(hdu1._header._keyword_indices["EXTNAME"]) == 1 assert hdu1.name == "NEW2" # Check that deleting EXTNAME will and setting the name will # work properly. del hdu.header["EXTNAME"] hdu.name = "RE-ADDED" assert hdu.name == "RE-ADDED" with pytest.raises(TypeError): hdu.name = 42 def test_compressed_header_extname(self): """Test consistent EXTNAME / hdu name interaction.""" name = "FOO" hdu = fits.CompImageHDU(data=np.arange(10), name=name) assert hdu._header["EXTNAME"] == name assert hdu.header["EXTNAME"] == name assert hdu.name == name name = "BAR" hdu.name = name assert hdu._header["EXTNAME"] == name assert hdu.header["EXTNAME"] == name assert hdu.name == name assert len(hdu._header._keyword_indices["EXTNAME"]) == 1 def test_compressed_header_minimal(self): """ Regression test for https://github.com/astropy/astropy/issues/11694 Tests that CompImageHDU can be initialized with a Header that contains few or no cards, and doesn't require specific cards such as 'BITPIX' or 'NAXIS'. """ fits.CompImageHDU(data=np.arange(10), header=fits.Header()) header = fits.Header({"HELLO": "world"}) hdu = fits.CompImageHDU(data=np.arange(10), header=header) assert hdu.header["HELLO"] == "world" @pytest.mark.parametrize( ("keyword", "dtype", "expected"), [ ("BSCALE", np.uint8, np.float32), ("BSCALE", np.int16, np.float32), ("BSCALE", np.int32, np.float64), ("BZERO", np.uint8, np.float32), ("BZERO", np.int16, np.float32), ("BZERO", np.int32, np.float64), ], ) def test_compressed_scaled_float(self, keyword, dtype, expected): """ If BSCALE,BZERO is set to floating point values, the image should be floating-point. https://github.com/astropy/astropy/pull/6492 Parameters ---------- keyword : `str` Keyword to set to a floating-point value to trigger floating-point pixels. dtype : `numpy.dtype` Type of original array. expected : `numpy.dtype` Expected type of uncompressed array. """ value = 1.23345 # A floating-point value hdu = fits.CompImageHDU(np.arange(0, 10, dtype=dtype)) hdu.header[keyword] = value hdu.writeto(self.temp("test.fits")) del hdu with fits.open(self.temp("test.fits")) as hdu: assert hdu[1].header[keyword] == value assert hdu[1].data.dtype == expected @pytest.mark.parametrize( "dtype", (np.uint8, np.int16, np.uint16, np.int32, np.uint32) ) def test_compressed_integers(self, dtype): """Test that the various integer dtypes are correctly written and read. Regression test for https://github.com/astropy/astropy/issues/9072 """ mid = np.iinfo(dtype).max // 2 data = np.arange(mid - 50, mid + 50, dtype=dtype) testfile = self.temp("test.fits") hdu = fits.CompImageHDU(data=data) hdu.writeto(testfile, overwrite=True) new = fits.getdata(testfile) np.testing.assert_array_equal(data, new) def test_write_non_contiguous_data(self): """ Regression test for https://github.com/astropy/astropy/issues/2150 """ orig = np.arange(100, dtype=float).reshape((10, 10), order="f") assert not orig.flags.contiguous primary = fits.PrimaryHDU() hdu = fits.CompImageHDU(orig) hdulist = fits.HDUList([primary, hdu]) hdulist.writeto(self.temp("test.fits")) actual = fits.getdata(self.temp("test.fits")) assert_equal(orig, actual) def test_slice_and_write_comp_hdu(self): """ Regression test for https://github.com/astropy/astropy/issues/9955 """ with fits.open(self.data("comp.fits")) as hdul: hdul[1].data = hdul[1].data[:200, :100] assert not hdul[1].data.flags.contiguous hdul[1].writeto(self.temp("test.fits")) with fits.open(self.data("comp.fits")) as hdul1: with fits.open(self.temp("test.fits")) as hdul2: assert_equal(hdul1[1].data[:200, :100], hdul2[1].data) def test_comphdu_bscale(tmp_path): """ Regression test for a bug that caused extensions that used BZERO and BSCALE that got turned into CompImageHDU to end up with BZERO/BSCALE before the TFIELDS. """ filename1 = tmp_path / "3hdus.fits" filename2 = tmp_path / "3hdus_comp.fits" x = np.random.random((100, 100)) * 100 x0 = fits.PrimaryHDU() x1 = fits.ImageHDU(np.array(x - 50, dtype=int), uint=True) x1.header["BZERO"] = 20331 x1.header["BSCALE"] = 2.3 hdus = fits.HDUList([x0, x1]) hdus.writeto(filename1) # fitsverify (based on cfitsio) should fail on this file, only seeing the # first HDU. with fits.open(filename1) as hdus: hdus[1] = fits.CompImageHDU( data=hdus[1].data.astype(np.uint32), header=hdus[1].header ) hdus.writeto(filename2) # open again and verify with fits.open(filename2) as hdus: hdus[1].verify("exception") def test_scale_implicit_casting(): # Regression test for an issue that occurred because Numpy now does not # allow implicit type casting during inplace operations. hdu = fits.ImageHDU(np.array([1], dtype=np.int32)) hdu.scale(bzero=1.3) def test_bzero_implicit_casting_compressed(): # Regression test for an issue that occurred because Numpy now does not # allow implicit type casting during inplace operations. Astropy is # actually not able to produce a file that triggers the failure - the # issue occurs when using unsigned integer types in the FITS file, in which # case BZERO should be 32768. But if the keyword is stored as 32768.0, then # it was possible to trigger the implicit casting error. filename = get_pkg_data_filename("data/compressed_float_bzero.fits") with fits.open(filename) as hdul: hdu = hdul[1] hdu.data def test_bzero_mishandled_info(tmp_path): # Regression test for #5507: # Calling HDUList.info() on a dataset which applies a zeropoint # from BZERO but which astropy.io.fits does not think it needs # to resize to a new dtype results in an AttributeError. filename = tmp_path / "floatimg_with_bzero.fits" hdu = fits.ImageHDU(np.zeros((10, 10))) hdu.header["BZERO"] = 10 hdu.writeto(filename, overwrite=True) with fits.open(filename) as hdul: hdul.info() def test_image_write_readonly(tmp_path): # Regression test to make sure that we can write out read-only arrays (#5512) x = np.array([1, 2, 3]) x.setflags(write=False) ghdu = fits.ImageHDU(data=x) ghdu.add_datasum() filename = tmp_path / "test.fits" ghdu.writeto(filename) with fits.open(filename) as hdulist: assert_equal(hdulist[1].data, [1, 2, 3]) # Same for compressed HDU x = np.array([1.0, 2.0, 3.0]) x.setflags(write=False) ghdu = fits.CompImageHDU(data=x) # add_datasum does not work for CompImageHDU # ghdu.add_datasum() filename = tmp_path / "test2.fits" ghdu.writeto(filename) with fits.open(filename) as hdulist: assert_equal(hdulist[1].data, [1.0, 2.0, 3.0]) def test_int8(tmp_path): """Test for int8 support, https://github.com/astropy/astropy/issues/11995""" img = np.arange(-50, 50, dtype=np.int8).reshape(10, 10) hdu = fits.PrimaryHDU(img) hdu.writeto(tmp_path / "int8.fits") with fits.open(tmp_path / "int8.fits") as hdul: assert hdul[0].header["BITPIX"] == 8 assert hdul[0].header["BZERO"] == -128 assert hdul[0].header["BSCALE"] == 1.0 assert_equal(hdul[0].data, img) assert hdul[0].data.dtype == img.dtype
93acff9e5ede8f269bf9d4415b9a83d3f298ee6cfb26494a43dcff6c17d2b783
import numpy as np import pytest from astropy.io import ascii from astropy.io.ascii.qdp import _get_lines_from_file, _read_table_qdp, _write_table_qdp from astropy.table import Column, MaskedColumn, Table from astropy.utils.exceptions import AstropyUserWarning def test_get_tables_from_qdp_file(tmp_path): example_qdp = """ ! Swift/XRT hardness ratio of trigger: XXXX, name: BUBU X-2 ! Columns are as labelled READ TERR 1 READ SERR 2 ! WT -- hard data !MJD Err (pos) Err(neg) Rate Error 53000.123456 2.37847222222222e-05 -2.37847222222222e-05 -0.212439 0.212439 55045.099887 1.14467592592593e-05 -1.14467592592593e-05 0.000000 0.000000 NO NO NO NO NO ! WT -- soft data !MJD Err (pos) Err(neg) Rate Error 53000.123456 2.37847222222222e-05 -2.37847222222222e-05 0.726155 0.583890 55045.099887 1.14467592592593e-05 -1.14467592592593e-05 2.410935 1.393592 NO NO NO NO NO ! WT -- hardness ratio !MJD Err (pos) Err(neg) Rate Error 53000.123456 2.37847222222222e-05 -2.37847222222222e-05 -0.292553 -0.374935 55045.099887 1.14467592592593e-05 -1.14467592592593e-05 0.000000 -nan """ path = tmp_path / "test.qdp" with open(path, "w") as fp: print(example_qdp, file=fp) table0 = _read_table_qdp(fp.name, names=["MJD", "Rate"], table_id=0) assert table0.meta["initial_comments"][0].startswith("Swift") assert table0.meta["comments"][0].startswith("WT -- hard data") table2 = _read_table_qdp(fp.name, names=["MJD", "Rate"], table_id=2) assert table2.meta["initial_comments"][0].startswith("Swift") assert table2.meta["comments"][0].startswith("WT -- hardness") assert np.isclose(table2["MJD_nerr"][0], -2.37847222222222e-05) def test_roundtrip(tmp_path): example_qdp = """ ! Swift/XRT hardness ratio of trigger: XXXX, name: BUBU X-2 ! Columns are as labelled READ TERR 1 READ SERR 2 ! WT -- hard data !MJD Err (pos) Err(neg) Rate Error 53000.123456 2.37847222222222e-05 -2.37847222222222e-05 NO 0.212439 55045.099887 1.14467592592593e-05 -1.14467592592593e-05 0.000000 0.000000 NO NO NO NO NO ! WT -- soft data !MJD Err (pos) Err(neg) Rate Error 53000.123456 2.37847222222222e-05 -2.37847222222222e-05 0.726155 0.583890 55045.099887 1.14467592592593e-05 -1.14467592592593e-05 2.410935 1.393592 NO NO NO NO NO ! WT -- hardness ratio !MJD Err (pos) Err(neg) Rate Error 53000.123456 2.37847222222222e-05 -2.37847222222222e-05 -0.292553 -0.374935 55045.099887 1.14467592592593e-05 -1.14467592592593e-05 0.000000 NO ! Add command, just to raise the warning. READ TERR 1 ! WT -- whatever !MJD Err (pos) Err(neg) Rate Error 53000.123456 2.37847222222222e-05 -2.37847222222222e-05 -0.292553 -0.374935 NO 1.14467592592593e-05 -1.14467592592593e-05 0.000000 NO """ path = str(tmp_path / "test.qdp") path2 = str(tmp_path / "test2.qdp") with open(path, "w") as fp: print(example_qdp, file=fp) with pytest.warns(AstropyUserWarning) as record: table = _read_table_qdp(path, names=["MJD", "Rate"], table_id=0) assert np.any( [ "This file contains multiple command blocks" in r.message.args[0] for r in record ] ) _write_table_qdp(table, path2) new_table = _read_table_qdp(path2, names=["MJD", "Rate"], table_id=0) for col in new_table.colnames: is_masked = np.array([np.ma.is_masked(val) for val in new_table[col]]) if np.any(is_masked): # All NaN values are read as such. assert np.ma.is_masked(table[col][is_masked]) is_nan = np.array( [(not np.ma.is_masked(val) and np.isnan(val)) for val in new_table[col]] ) # All non-NaN values are the same assert np.allclose(new_table[col][~is_nan], table[col][~is_nan]) if np.any(is_nan): # All NaN values are read as such. assert np.isnan(table[col][is_nan]) assert np.allclose(new_table["MJD_perr"], [2.378472e-05, 1.1446759e-05]) for meta_name in ["initial_comments", "comments"]: assert meta_name in new_table.meta def test_read_example(): example_qdp = """ ! Initial comment line 1 ! Initial comment line 2 READ TERR 1 READ SERR 3 ! Table 0 comment !a a(pos) a(neg) b c ce d 53000.5 0.25 -0.5 1 1.5 3.5 2 54000.5 1.25 -1.5 2 2.5 4.5 3 NO NO NO NO NO ! Table 1 comment !a a(pos) a(neg) b c ce d 54000.5 2.25 -2.5 NO 3.5 5.5 5 55000.5 3.25 -3.5 4 4.5 6.5 nan """ dat = ascii.read(example_qdp, format="qdp", table_id=1, names=["a", "b", "c", "d"]) t = Table.read( example_qdp, format="ascii.qdp", table_id=1, names=["a", "b", "c", "d"] ) assert np.allclose(t["a"], [54000, 55000]) assert t["c_err"][0] == 5.5 assert np.ma.is_masked(t["b"][0]) assert np.isnan(t["d"][1]) for col1, col2 in zip(t.itercols(), dat.itercols()): assert np.allclose(col1, col2, equal_nan=True) def test_roundtrip_example(tmp_path): example_qdp = """ ! Initial comment line 1 ! Initial comment line 2 READ TERR 1 READ SERR 3 ! Table 0 comment !a a(pos) a(neg) b c ce d 53000.5 0.25 -0.5 1 1.5 3.5 2 54000.5 1.25 -1.5 2 2.5 4.5 3 NO NO NO NO NO ! Table 1 comment !a a(pos) a(neg) b c ce d 54000.5 2.25 -2.5 NO 3.5 5.5 5 55000.5 3.25 -3.5 4 4.5 6.5 nan """ test_file = tmp_path / "test.qdp" t = Table.read( example_qdp, format="ascii.qdp", table_id=1, names=["a", "b", "c", "d"] ) t.write(test_file, err_specs={"terr": [1], "serr": [3]}) t2 = Table.read(test_file, names=["a", "b", "c", "d"], table_id=0) for col1, col2 in zip(t.itercols(), t2.itercols()): assert np.allclose(col1, col2, equal_nan=True) def test_roundtrip_example_comma(tmp_path): example_qdp = """ ! Initial comment line 1 ! Initial comment line 2 READ TERR 1 READ SERR 3 ! Table 0 comment !a,a(pos),a(neg),b,c,ce,d 53000.5,0.25,-0.5,1,1.5,3.5,2 54000.5,1.25,-1.5,2,2.5,4.5,3 NO,NO,NO,NO,NO ! Table 1 comment !a,a(pos),a(neg),b,c,ce,d 54000.5,2.25,-2.5,NO,3.5,5.5,5 55000.5,3.25,-3.5,4,4.5,6.5,nan """ test_file = tmp_path / "test.qdp" t = Table.read( example_qdp, format="ascii.qdp", table_id=1, names=["a", "b", "c", "d"], sep="," ) t.write(test_file, err_specs={"terr": [1], "serr": [3]}) t2 = Table.read(test_file, names=["a", "b", "c", "d"], table_id=0) # t.values_equal(t2) for col1, col2 in zip(t.itercols(), t2.itercols()): assert np.allclose(col1, col2, equal_nan=True) def test_read_write_simple(tmp_path): test_file = tmp_path / "test.qdp" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3, 4])) t1.add_column( MaskedColumn( data=[4.0, np.nan, 3.0, 1.0], name="b", mask=[False, False, False, True] ) ) t1.write(test_file, format="ascii.qdp") with pytest.warns(UserWarning) as record: t2 = Table.read(test_file, format="ascii.qdp") assert np.any( [ "table_id not specified. Reading the first available table" in r.message.args[0] for r in record ] ) assert np.allclose(t2["col1"], t1["a"]) assert np.all(t2["col1"] == t1["a"]) good = ~np.isnan(t1["b"]) assert np.allclose(t2["col2"][good], t1["b"][good]) def test_read_write_simple_specify_name(tmp_path): test_file = tmp_path / "test.qdp" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) # Give a non-None err_specs t1.write(test_file, format="ascii.qdp") t2 = Table.read(test_file, table_id=0, format="ascii.qdp", names=["a"]) assert np.all(t2["a"] == t1["a"]) def test_get_lines_from_qdp(tmp_path): test_file = str(tmp_path / "test.qdp") text_string = "A\nB" text_output = _get_lines_from_file(text_string) with open(test_file, "w") as fobj: print(text_string, file=fobj) file_output = _get_lines_from_file(test_file) list_output = _get_lines_from_file(["A", "B"]) for i, line in enumerate(["A", "B"]): assert file_output[i] == line assert list_output[i] == line assert text_output[i] == line
0fa50e914cd3c78deec1ff43238248ea6e8d24a4b4a6a2140e32ad3279087dd0
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ This module tests some methods related to ``CDS`` format reader/writer. Requires `pyyaml <https://pyyaml.org/>`_ to be installed. """ from io import StringIO import numpy as np import pytest from astropy import units as u from astropy.coordinates import SkyCoord from astropy.io import ascii from astropy.table import Column, MaskedColumn, Table from astropy.time import Time from astropy.utils.data import get_pkg_data_filename from astropy.utils.exceptions import AstropyWarning from .common import assert_almost_equal test_dat = [ "names e d s i", "HD81809 1E-7 22.25608 +2 67", "HD103095 -31.6e5 +27.2500 -9E34 -30", ] def test_roundtrip_mrt_table(): """ Tests whether or not the CDS writer can roundtrip a table, i.e. read a table to ``Table`` object and write it exactly as it is back to a file. Since, presently CDS uses a MRT format template while writing, only the Byte-By-Byte and the data section of the table can be compared between original and the newly written table. Further, the CDS Reader does not have capability to recognize column format from the header of a CDS/MRT table, so this test can work for a limited set of simple tables, which don't have whitespaces in the column values or mix-in columns. Because of this the written table output cannot be directly matched with the original file and have to be checked against a list of lines. Masked columns are read properly though, and thus are being tested during round-tripping. The difference between ``cdsFunctional2.dat`` file and ``exp_output`` is the following: * Metadata is different because MRT template is used for writing. * Spacing between ``Label`` and ``Explanations`` column in the Byte-By-Byte. * Units are written as ``[cm.s-2]`` and not ``[cm/s2]``, since both are valid according to CDS/MRT standard. """ exp_output = [ "================================================================================", "Byte-by-byte Description of file: table.dat", "--------------------------------------------------------------------------------", " Bytes Format Units Label Explanations", "--------------------------------------------------------------------------------", " 1- 7 A7 --- ID Star ID ", " 9-12 I4 K Teff [4337/4654] Effective temperature ", "14-17 F4.2 [cm.s-2] logg [0.77/1.28] Surface gravity ", "19-22 F4.2 km.s-1 vturb [1.23/1.82] Micro-turbulence velocity", "24-28 F5.2 [-] [Fe/H] [-2.11/-1.5] Metallicity ", "30-33 F4.2 [-] e_[Fe/H] ? rms uncertainty on [Fe/H] ", "--------------------------------------------------------------------------------", "Notes:", "--------------------------------------------------------------------------------", "S05-5 4337 0.77 1.80 -2.07 ", "S08-229 4625 1.23 1.23 -1.50 ", "S05-10 4342 0.91 1.82 -2.11 0.14", "S05-47 4654 1.28 1.74 -1.64 0.16", ] dat = get_pkg_data_filename( "data/cdsFunctional2.dat", package="astropy.io.ascii.tests" ) t = Table.read(dat, format="ascii.mrt") out = StringIO() t.write(out, format="ascii.mrt") lines = out.getvalue().splitlines() i_bbb = lines.index("=" * 80) lines = lines[i_bbb:] # Select Byte-By-Byte section and later lines. assert lines == exp_output def test_write_byte_by_byte_units(): t = ascii.read(test_dat) col_units = [None, u.C, u.kg, u.m / u.s, u.year] t._set_column_attribute("unit", col_units) # Add a column with magnitude units. # Note that magnitude has to be assigned for each value explicitly. t["magnitude"] = [u.Magnitude(25), u.Magnitude(-9)] col_units.append(u.mag) out = StringIO() t.write(out, format="ascii.mrt") # Read written table. tRead = ascii.read(out.getvalue(), format="cds") assert [tRead[col].unit for col in tRead.columns] == col_units def test_write_readme_with_default_options(): exp_output = [ "Title:", "Authors:", "Table:", "================================================================================", "Byte-by-byte Description of file: table.dat", "--------------------------------------------------------------------------------", " Bytes Format Units Label Explanations", "--------------------------------------------------------------------------------", " 1- 8 A8 --- names Description of names ", "10-14 E5.1 --- e [-3160000.0/0.01] Description of e", "16-23 F8.5 --- d [22.25/27.25] Description of d ", "25-31 E7.1 --- s [-9e+34/2.0] Description of s ", "33-35 I3 --- i [-30/67] Description of i ", "--------------------------------------------------------------------------------", "Notes:", "--------------------------------------------------------------------------------", "HD81809 1e-07 22.25608 2e+00 67", "HD103095 -3e+06 27.25000 -9e+34 -30", ] t = ascii.read(test_dat) out = StringIO() t.write(out, format="ascii.mrt") assert out.getvalue().splitlines() == exp_output def test_write_empty_table(): out = StringIO() import pytest with pytest.raises(NotImplementedError): Table().write(out, format="ascii.mrt") def test_write_null_data_values(): exp_output = [ "HD81809 1e-07 22.25608 2.0e+00 67", "HD103095 -3e+06 27.25000 -9.0e+34 -30", "Sun 5.3e+27 ", ] t = ascii.read(test_dat) t.add_row( ["Sun", "3.25", "0", "5.3e27", "2"], mask=[False, True, True, False, True] ) out = StringIO() t.write(out, format="ascii.mrt") lines = out.getvalue().splitlines() i_secs = [i for i, s in enumerate(lines) if s.startswith(("------", "======="))] lines = lines[i_secs[-1] + 1 :] # Last section is the data. assert lines == exp_output def test_write_byte_by_byte_for_masked_column(): """ This test differs from the ``test_write_null_data_values`` above in that it tests the column value limits in the Byte-By-Byte description section for columns whose values are masked. It also checks the description for columns with same values. """ exp_output = [ "================================================================================", "Byte-by-byte Description of file: table.dat", "--------------------------------------------------------------------------------", " Bytes Format Units Label Explanations", "--------------------------------------------------------------------------------", " 1- 8 A8 --- names Description of names ", "10-14 E5.1 --- e [0.0/0.01]? Description of e ", "16-17 F2.0 --- d ? Description of d ", "19-25 E7.1 --- s [-9e+34/2.0] Description of s ", "27-29 I3 --- i [-30/67] Description of i ", "31-33 F3.1 --- sameF [5.0/5.0] Description of sameF", "35-36 I2 --- sameI [20] Description of sameI ", "--------------------------------------------------------------------------------", "Notes:", "--------------------------------------------------------------------------------", "HD81809 1e-07 2e+00 67 5.0 20", "HD103095 -9e+34 -30 5.0 20", ] t = ascii.read(test_dat) t.add_column([5.0, 5.0], name="sameF") t.add_column([20, 20], name="sameI") t["e"] = MaskedColumn(t["e"], mask=[False, True]) t["d"] = MaskedColumn(t["d"], mask=[True, True]) out = StringIO() t.write(out, format="ascii.mrt") lines = out.getvalue().splitlines() i_bbb = lines.index("=" * 80) lines = lines[i_bbb:] # Select Byte-By-Byte section and later lines. assert lines == exp_output exp_coord_cols_output = dict( # fmt: off generic=[ '================================================================================', 'Byte-by-byte Description of file: table.dat', '--------------------------------------------------------------------------------', ' Bytes Format Units Label Explanations', '--------------------------------------------------------------------------------', ' 1- 8 A8 --- names Description of names ', '10-14 E5.1 --- e [-3160000.0/0.01] Description of e', '16-23 F8.5 --- d [22.25/27.25] Description of d ', '25-31 E7.1 --- s [-9e+34/2.0] Description of s ', '33-35 I3 --- i [-30/67] Description of i ', '37-39 F3.1 --- sameF [5.0/5.0] Description of sameF ', '41-42 I2 --- sameI [20] Description of sameI ', '44-45 I2 h RAh Right Ascension (hour) ', '47-48 I2 min RAm Right Ascension (minute) ', '50-62 F13.10 s RAs Right Ascension (second) ', ' 64 A1 --- DE- Sign of Declination ', '65-66 I2 deg DEd Declination (degree) ', '68-69 I2 arcmin DEm Declination (arcmin) ', '71-82 F12.9 arcsec DEs Declination (arcsec) ', '--------------------------------------------------------------------------------', 'Notes:', '--------------------------------------------------------------------------------', 'HD81809 1e-07 22.25608 2e+00 67 5.0 20 22 02 15.4500000000 -61 39 34.599996000', 'HD103095 -3e+06 27.25000 -9e+34 -30 5.0 20 12 48 15.2244072000 +17 46 26.496624000', ], positive_de=[ '================================================================================', 'Byte-by-byte Description of file: table.dat', '--------------------------------------------------------------------------------', ' Bytes Format Units Label Explanations', '--------------------------------------------------------------------------------', ' 1- 8 A8 --- names Description of names ', '10-14 E5.1 --- e [-3160000.0/0.01] Description of e', '16-23 F8.5 --- d [22.25/27.25] Description of d ', '25-31 E7.1 --- s [-9e+34/2.0] Description of s ', '33-35 I3 --- i [-30/67] Description of i ', '37-39 F3.1 --- sameF [5.0/5.0] Description of sameF ', '41-42 I2 --- sameI [20] Description of sameI ', '44-45 I2 h RAh Right Ascension (hour) ', '47-48 I2 min RAm Right Ascension (minute) ', '50-62 F13.10 s RAs Right Ascension (second) ', ' 64 A1 --- DE- Sign of Declination ', '65-66 I2 deg DEd Declination (degree) ', '68-69 I2 arcmin DEm Declination (arcmin) ', '71-82 F12.9 arcsec DEs Declination (arcsec) ', '--------------------------------------------------------------------------------', 'Notes:', '--------------------------------------------------------------------------------', 'HD81809 1e-07 22.25608 2e+00 67 5.0 20 12 48 15.2244072000 +17 46 26.496624000', 'HD103095 -3e+06 27.25000 -9e+34 -30 5.0 20 12 48 15.2244072000 +17 46 26.496624000', ], # fmt: on galactic=[ "================================================================================", "Byte-by-byte Description of file: table.dat", "--------------------------------------------------------------------------------", " Bytes Format Units Label Explanations", "--------------------------------------------------------------------------------", " 1- 8 A8 --- names Description of names ", "10-14 E5.1 --- e [-3160000.0/0.01] Description of e", "16-23 F8.5 --- d [22.25/27.25] Description of d ", "25-31 E7.1 --- s [-9e+34/2.0] Description of s ", "33-35 I3 --- i [-30/67] Description of i ", "37-39 F3.1 --- sameF [5.0/5.0] Description of sameF ", "41-42 I2 --- sameI [20] Description of sameI ", "44-59 F16.12 deg GLON Galactic Longitude ", "61-76 F16.12 deg GLAT Galactic Latitude ", "--------------------------------------------------------------------------------", "Notes:", "--------------------------------------------------------------------------------", "HD81809 1e-07 22.25608 2e+00 67 5.0 20 330.071639591690 -45.548080484609", "HD103095 -3e+06 27.25000 -9e+34 -30 5.0 20 330.071639591690 -45.548080484609", ], ecliptic=[ "================================================================================", "Byte-by-byte Description of file: table.dat", "--------------------------------------------------------------------------------", " Bytes Format Units Label Explanations", "--------------------------------------------------------------------------------", " 1- 8 A8 --- names Description of names ", "10-14 E5.1 --- e [-3160000.0/0.01] Description of e ", "16-23 F8.5 --- d [22.25/27.25] Description of d ", "25-31 E7.1 --- s [-9e+34/2.0] Description of s ", "33-35 I3 --- i [-30/67] Description of i ", "37-39 F3.1 --- sameF [5.0/5.0] Description of sameF ", "41-42 I2 --- sameI [20] Description of sameI ", "44-59 F16.12 deg ELON Ecliptic Longitude (geocentrictrueecliptic)", "61-76 F16.12 deg ELAT Ecliptic Latitude (geocentrictrueecliptic) ", "--------------------------------------------------------------------------------", "Notes:", "--------------------------------------------------------------------------------", "HD81809 1e-07 22.25608 2e+00 67 5.0 20 306.224208650096 -45.621789850825", "HD103095 -3e+06 27.25000 -9e+34 -30 5.0 20 306.224208650096 -45.621789850825", ], ) def test_write_coord_cols(): """ There can only be one such coordinate column in a single table, because division of columns into individual component columns requires iterating over the table columns, which will have to be done again if additional such coordinate columns are present. """ t = ascii.read(test_dat) t.add_column([5.0, 5.0], name="sameF") t.add_column([20, 20], name="sameI") # Coordinates of ASASSN-15lh coord = SkyCoord(330.564375, -61.65961111, unit=u.deg) # Coordinates of ASASSN-14li coordp = SkyCoord(192.06343503, 17.77402684, unit=u.deg) cols = [ Column([coord, coordp]), # Generic coordinate column coordp, # Coordinate column with positive DEC coord.galactic, # Galactic coordinates coord.geocentrictrueecliptic, # Ecliptic coordinates ] # Loop through different types of coordinate columns. for col, coord_type in zip(cols, exp_coord_cols_output): exp_output = exp_coord_cols_output[coord_type] t["coord"] = col out = StringIO() t.write(out, format="ascii.mrt") lines = out.getvalue().splitlines() i_bbb = lines.index("=" * 80) lines = lines[i_bbb:] # Select Byte-By-Byte section and later lines. # Check the written table. assert lines == exp_output # Check if the original table columns remains unmodified. assert t.colnames == ["names", "e", "d", "s", "i", "sameF", "sameI", "coord"] def test_write_byte_by_byte_bytes_col_format(): """ Tests the alignment of Byte counts with respect to hyphen in the Bytes column of Byte-By-Byte. The whitespace around the hyphen is govered by the number of digits in the total Byte count. Single Byte columns should have a single Byte count without the hyphen. """ exp_output = [ "================================================================================", "Byte-by-byte Description of file: table.dat", "--------------------------------------------------------------------------------", " Bytes Format Units Label Explanations", "--------------------------------------------------------------------------------", " 1- 8 A8 --- names Description of names ", "10-21 E12.6 --- e [-3160000.0/0.01] Description of e", "23-30 F8.5 --- d [22.25/27.25] Description of d ", "32-38 E7.1 --- s [-9e+34/2.0] Description of s ", "40-42 I3 --- i [-30/67] Description of i ", "44-46 F3.1 --- sameF [5.0/5.0] Description of sameF ", "48-49 I2 --- sameI [20] Description of sameI ", " 51 I1 --- singleByteCol [2] Description of singleByteCol ", "53-54 I2 h RAh Right Ascension (hour) ", "56-57 I2 min RAm Right Ascension (minute) ", "59-71 F13.10 s RAs Right Ascension (second) ", " 73 A1 --- DE- Sign of Declination ", "74-75 I2 deg DEd Declination (degree) ", "77-78 I2 arcmin DEm Declination (arcmin) ", "80-91 F12.9 arcsec DEs Declination (arcsec) ", "--------------------------------------------------------------------------------", ] t = ascii.read(test_dat) t.add_column([5.0, 5.0], name="sameF") t.add_column([20, 20], name="sameI") t["coord"] = SkyCoord(330.564375, -61.65961111, unit=u.deg) t["singleByteCol"] = [2, 2] t["e"].format = ".5E" out = StringIO() t.write(out, format="ascii.mrt") lines = out.getvalue().splitlines() i_secs = [i for i, s in enumerate(lines) if s.startswith(("------", "======="))] # Select only the Byte-By-Byte section. lines = lines[i_secs[0] : i_secs[-2]] lines.append("-" * 80) # Append a separator line. assert lines == exp_output def test_write_byte_by_byte_wrapping(): """ Test line wrapping in the description column of the Byte-By-Byte section of the ReadMe. """ exp_output = """\ ================================================================================ Byte-by-byte Description of file: table.dat -------------------------------------------------------------------------------- Bytes Format Units Label Explanations -------------------------------------------------------------------------------- 1- 8 A8 --- thisIsALongColumnLabel This is a tediously long description. But they do sometimes have them. Better to put extra details in the notes. This is a tediously long description. But they do sometimes have them. Better to put extra details in the notes. 10-14 E5.1 --- e [-3160000.0/0.01] Description of e 16-23 F8.5 --- d [22.25/27.25] Description of d -------------------------------------------------------------------------------- """ t = ascii.read(test_dat) t.remove_columns(["s", "i"]) description = ( "This is a tediously long description." + " But they do sometimes have them." + " Better to put extra details in the notes. " ) t["names"].description = description * 2 t["names"].name = "thisIsALongColumnLabel" out = StringIO() t.write(out, format="ascii.mrt") lines = out.getvalue().splitlines() i_secs = [i for i, s in enumerate(lines) if s.startswith(("------", "======="))] # Select only the Byte-By-Byte section. lines = lines[i_secs[0] : i_secs[-2]] lines.append("-" * 80) # Append a separator line. assert lines == exp_output.splitlines() def test_write_mixin_and_broken_cols(): """ Tests convertion to string values for ``mix-in`` columns other than ``SkyCoord`` and for columns with only partial ``SkyCoord`` values. """ # fmt: off exp_output = [ '================================================================================', 'Byte-by-byte Description of file: table.dat', '--------------------------------------------------------------------------------', ' Bytes Format Units Label Explanations', '--------------------------------------------------------------------------------', ' 1- 7 A7 --- name Description of name ', ' 9- 74 A66 --- Unknown Description of Unknown', ' 76-114 A39 --- Unknown Description of Unknown', '116-138 A23 --- Unknown Description of Unknown', '--------------------------------------------------------------------------------', 'Notes:', '--------------------------------------------------------------------------------', 'HD81809 <SkyCoord (ICRS): (ra, dec) in deg', ' (330.564375, -61.65961111)> (0.41342785, -0.23329341, -0.88014294) 2019-01-01 00:00:00.000', 'random 12 (0.41342785, -0.23329341, -0.88014294) 2019-01-01 00:00:00.000', ] # fmt: on t = Table() t["name"] = ["HD81809"] coord = SkyCoord(330.564375, -61.65961111, unit=u.deg) t["coord"] = Column(coord) t.add_row(["random", 12]) t["cart"] = coord.cartesian t["time"] = Time("2019-1-1") out = StringIO() t.write(out, format="ascii.mrt") lines = out.getvalue().splitlines() i_bbb = lines.index("=" * 80) lines = lines[i_bbb:] # Select Byte-By-Byte section and later lines. # Check the written table. assert lines == exp_output def test_write_extra_skycoord_cols(): """ Tests output for cases when table contains multiple ``SkyCoord`` columns. """ exp_output = [ "================================================================================", "Byte-by-byte Description of file: table.dat", "--------------------------------------------------------------------------------", " Bytes Format Units Label Explanations", "--------------------------------------------------------------------------------", " 1- 7 A7 --- name Description of name ", " 9-10 I2 h RAh Right Ascension (hour) ", "12-13 I2 min RAm Right Ascension (minute)", "15-27 F13.10 s RAs Right Ascension (second)", " 29 A1 --- DE- Sign of Declination ", "30-31 I2 deg DEd Declination (degree) ", "33-34 I2 arcmin DEm Declination (arcmin) ", "36-47 F12.9 arcsec DEs Declination (arcsec) ", "49-62 A14 --- coord2 Description of coord2 ", "--------------------------------------------------------------------------------", "Notes:", "--------------------------------------------------------------------------------", "HD4760 0 49 39.9000000000 +06 24 07.999200000 12.4163 6.407 ", "HD81809 22 02 15.4500000000 -61 39 34.599996000 330.564 -61.66", ] t = Table() t["name"] = ["HD4760", "HD81809"] t["coord1"] = SkyCoord([12.41625, 330.564375], [6.402222, -61.65961111], unit=u.deg) t["coord2"] = SkyCoord([12.41630, 330.564400], [6.407, -61.66], unit=u.deg) out = StringIO() with pytest.warns( UserWarning, match=r"column 2 is being skipped with designation of a " r"string valued column `coord2`", ): t.write(out, format="ascii.mrt") lines = out.getvalue().splitlines() i_bbb = lines.index("=" * 80) lines = lines[i_bbb:] # Select Byte-By-Byte section and following lines. # Check the written table. assert lines[:-2] == exp_output[:-2] for a, b in zip(lines[-2:], exp_output[-2:]): assert a[:18] == b[:18] assert a[30:42] == b[30:42] assert_almost_equal( np.fromstring(a[2:], sep=" "), np.fromstring(b[2:], sep=" ") ) def test_write_skycoord_with_format(): """ Tests output with custom setting for ``SkyCoord`` (second) columns. """ exp_output = [ "================================================================================", "Byte-by-byte Description of file: table.dat", "--------------------------------------------------------------------------------", " Bytes Format Units Label Explanations", "--------------------------------------------------------------------------------", " 1- 7 A7 --- name Description of name ", " 9-10 I2 h RAh Right Ascension (hour) ", "12-13 I2 min RAm Right Ascension (minute)", "15-19 F5.2 s RAs Right Ascension (second)", " 21 A1 --- DE- Sign of Declination ", "22-23 I2 deg DEd Declination (degree) ", "25-26 I2 arcmin DEm Declination (arcmin) ", "28-31 F4.1 arcsec DEs Declination (arcsec) ", "--------------------------------------------------------------------------------", "Notes:", "--------------------------------------------------------------------------------", "HD4760 0 49 39.90 +06 24 08.0", "HD81809 22 02 15.45 -61 39 34.6", ] t = Table() t["name"] = ["HD4760", "HD81809"] t["coord"] = SkyCoord([12.41625, 330.564375], [6.402222, -61.65961111], unit=u.deg) out = StringIO() # This will raise a warning because `formats` is checked before the writer creating the # final list of columns is called. with pytest.warns( AstropyWarning, match=r"The key.s. {'[RD][AE]s', '[RD][AE]s'} specified in " r"the formats argument do not match a column name.", ): t.write(out, format="ascii.mrt", formats={"RAs": "05.2f", "DEs": "04.1f"}) lines = out.getvalue().splitlines() i_bbb = lines.index("=" * 80) lines = lines[i_bbb:] # Select Byte-By-Byte section and following lines. # Check the written table. assert lines == exp_output
2370dd6af3bbc965a07ac67260de3d8673c551cfa8776b24c869f64d3849312d
# Licensed under a 3-clause BSD style license - see LICENSE.rst import functools import io import os import re from contextlib import nullcontext from io import BytesIO from textwrap import dedent import numpy as np import pytest from numpy import ma from astropy.io import ascii from astropy.io.ascii.core import ( FastOptionsError, InconsistentTableError, ParameterError, ) from astropy.io.ascii.fastbasic import ( FastBasic, FastCommentedHeader, FastCsv, FastNoHeader, FastRdb, FastTab, ) from astropy.table import MaskedColumn, Table from astropy.utils.data import get_pkg_data_filename from astropy.utils.exceptions import AstropyWarning from .common import assert_almost_equal, assert_equal, assert_true StringIO = lambda x: BytesIO(x.encode("ascii")) CI = os.environ.get("CI", False) def assert_table_equal(t1, t2, check_meta=False, rtol=1.0e-15, atol=1.0e-300): """ Test equality of all columns in a table, with stricter tolerances for float columns than the np.allclose default. """ assert_equal(len(t1), len(t2)) assert_equal(t1.colnames, t2.colnames) if check_meta: assert_equal(t1.meta, t2.meta) for name in t1.colnames: if len(t1) != 0: assert_equal(t1[name].dtype.kind, t2[name].dtype.kind) if not isinstance(t1[name], MaskedColumn): for i, el in enumerate(t1[name]): try: if not isinstance(el, str) and np.isnan(el): assert_true( not isinstance(t2[name][i], str) and np.isnan(t2[name][i]) ) elif isinstance(el, str): assert_equal(el, t2[name][i]) else: assert_almost_equal(el, t2[name][i], rtol=rtol, atol=atol) except (TypeError, NotImplementedError): pass # ignore for now # Use this counter to create a unique filename for each file created in a test # if this function is called more than once in a single test _filename_counter = 0 def _read( tmp_path, table, Reader=None, format=None, parallel=False, check_meta=False, **kwargs, ): # make sure we have a newline so table can't be misinterpreted as a filename global _filename_counter table += "\n" reader = Reader(**kwargs) t1 = reader.read(table) t2 = reader.read(StringIO(table)) t3 = reader.read(table.splitlines()) t4 = ascii.read(table, format=format, guess=False, **kwargs) t5 = ascii.read(table, format=format, guess=False, fast_reader=False, **kwargs) assert_table_equal(t1, t2, check_meta=check_meta) assert_table_equal(t2, t3, check_meta=check_meta) assert_table_equal(t3, t4, check_meta=check_meta) assert_table_equal(t4, t5, check_meta=check_meta) if parallel: if CI: pytest.xfail("Multiprocessing can sometimes fail on CI") t6 = ascii.read( table, format=format, guess=False, fast_reader={"parallel": True}, **kwargs ) assert_table_equal(t1, t6, check_meta=check_meta) filename = tmp_path / f"table{_filename_counter}.txt" _filename_counter += 1 with open(filename, "wb") as f: f.write(table.encode("ascii")) f.flush() t7 = ascii.read(filename, format=format, guess=False, **kwargs) if parallel: t8 = ascii.read( filename, format=format, guess=False, fast_reader={"parallel": True}, **kwargs, ) assert_table_equal(t1, t7, check_meta=check_meta) if parallel: assert_table_equal(t1, t8, check_meta=check_meta) return t1 @pytest.fixture(scope="function") def read_basic(tmp_path, request): return functools.partial(_read, tmp_path, Reader=FastBasic, format="basic") @pytest.fixture(scope="function") def read_csv(tmp_path, request): return functools.partial(_read, tmp_path, Reader=FastCsv, format="csv") @pytest.fixture(scope="function") def read_tab(tmp_path, request): return functools.partial(_read, tmp_path, Reader=FastTab, format="tab") @pytest.fixture(scope="function") def read_commented_header(tmp_path, request): return functools.partial( _read, tmp_path, Reader=FastCommentedHeader, format="commented_header" ) @pytest.fixture(scope="function") def read_rdb(tmp_path, request): return functools.partial(_read, tmp_path, Reader=FastRdb, format="rdb") @pytest.fixture(scope="function") def read_no_header(tmp_path, request): return functools.partial(_read, tmp_path, Reader=FastNoHeader, format="no_header") @pytest.mark.parametrize("delimiter", [",", "\t", " ", "csv"]) @pytest.mark.parametrize("quotechar", ['"', "'"]) @pytest.mark.parametrize("fast", [False, True]) def test_embedded_newlines(delimiter, quotechar, fast): """Test that embedded newlines are supported for io.ascii readers and writers, both fast and Python readers.""" # Start with an assortment of values with different embedded newlines and whitespace dat = [ ["\t a ", " b \n cd ", "\n"], [" 1\n ", '2 \n" \t 3\n4\n5', "1\n '2\n"], [" x,y \nz\t", "\t 12\n\t34\t ", "56\t\n"], ] dat = Table(dat, names=("a", "b", "c")) # Construct a table which is our expected result of writing the table and # reading it back. Certain stripping of whitespace is expected. exp = {} # expected output from reading for col in dat.itercols(): vals = [] for val in col: # Readers and writers both strip whitespace from ends of values val = val.strip(" \t") if not fast: # Pure Python reader has a "feature" where it strips trailing # whitespace from each input line. This means a value like # " x \ny \t\n" gets read as "x\ny". bits = val.splitlines(keepends=True) bits_out = [] for bit in bits: bit = re.sub(r"[ \t]+(\n?)$", r"\1", bit.strip(" \t")) bits_out.append(bit) val = "".join(bits_out) vals.append(val) exp[col.info.name] = vals exp = Table(exp) if delimiter == "csv": format = "csv" delimiter = "," else: format = "basic" # Write the table to `text` fh = io.StringIO() ascii.write( dat, fh, format=format, delimiter=delimiter, quotechar=quotechar, fast_writer=fast, ) text = fh.getvalue() # Read it back and compare to the expected dat_out = ascii.read( text, format=format, guess=False, delimiter=delimiter, quotechar=quotechar, fast_reader=fast, ) eq = dat_out.values_equal(exp) assert all(np.all(col) for col in eq.itercols()) @pytest.mark.parametrize("parallel", [True, False]) def test_simple_data(parallel, read_basic): """ Make sure the fast reader works with basic input data. """ table = read_basic("A B C\n1 2 3\n4 5 6", parallel=parallel) expected = Table([[1, 4], [2, 5], [3, 6]], names=("A", "B", "C")) assert_table_equal(table, expected) def test_read_types(): """ Make sure that the read() function takes filenames, strings, and lists of strings in addition to file-like objects. """ t1 = ascii.read("a b c\n1 2 3\n4 5 6", format="fast_basic", guess=False) # TODO: also read from file t2 = ascii.read(StringIO("a b c\n1 2 3\n4 5 6"), format="fast_basic", guess=False) t3 = ascii.read(["a b c", "1 2 3", "4 5 6"], format="fast_basic", guess=False) assert_table_equal(t1, t2) assert_table_equal(t2, t3) @pytest.mark.parametrize("parallel", [True, False]) def test_supplied_names(parallel, read_basic): """ If passed as a parameter, names should replace any column names found in the header. """ table = read_basic("A B C\n1 2 3\n4 5 6", names=("X", "Y", "Z"), parallel=parallel) expected = Table([[1, 4], [2, 5], [3, 6]], names=("X", "Y", "Z")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_no_header(parallel, read_basic, read_no_header): """ The header should not be read when header_start=None. Unless names is passed, the column names should be auto-generated. """ # Cannot set header_start=None for basic format with pytest.raises(ValueError): read_basic( "A B C\n1 2 3\n4 5 6", header_start=None, data_start=0, parallel=parallel ) t2 = read_no_header("A B C\n1 2 3\n4 5 6", parallel=parallel) expected = Table( [["A", "1", "4"], ["B", "2", "5"], ["C", "3", "6"]], names=("col1", "col2", "col3"), ) assert_table_equal(t2, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_no_header_supplied_names(parallel, read_basic, read_no_header): """ If header_start=None and names is passed as a parameter, header data should not be read and names should be used instead. """ table = read_no_header( "A B C\n1 2 3\n4 5 6", names=("X", "Y", "Z"), parallel=parallel ) expected = Table( [["A", "1", "4"], ["B", "2", "5"], ["C", "3", "6"]], names=("X", "Y", "Z") ) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_comment(parallel, read_basic): """ Make sure that line comments are ignored by the C reader. """ table = read_basic( "# comment\nA B C\n # another comment\n1 2 3\n4 5 6", parallel=parallel ) expected = Table([[1, 4], [2, 5], [3, 6]], names=("A", "B", "C")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_empty_lines(parallel, read_basic): """ Make sure that empty lines are ignored by the C reader. """ table = read_basic("\n\nA B C\n1 2 3\n\n\n4 5 6\n\n\n\n", parallel=parallel) expected = Table([[1, 4], [2, 5], [3, 6]], names=("A", "B", "C")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_lstrip_whitespace(parallel, read_basic): """ Test to make sure the reader ignores whitespace at the beginning of fields. """ text = """ 1, 2, \t3 A,\t\t B, C a, b, c \n""" table = read_basic(text, delimiter=",", parallel=parallel) expected = Table([["A", "a"], ["B", "b"], ["C", "c"]], names=("1", "2", "3")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_rstrip_whitespace(parallel, read_basic): """ Test to make sure the reader ignores whitespace at the end of fields. """ text = " 1 ,2 \t,3 \nA\t,B ,C\t \t \n \ta ,b , c \n" table = read_basic(text, delimiter=",", parallel=parallel) expected = Table([["A", "a"], ["B", "b"], ["C", "c"]], names=("1", "2", "3")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_conversion(parallel, read_basic): """ The reader should try to convert each column to ints. If this fails, the reader should try to convert to floats. Failing this, i.e. on parsing non-numeric input including isolated positive/negative signs, it should fall back to strings. """ text = """ A B C D E F G H 1 a 3 4 5 6 7 8 2. 1 9 -.1e1 10.0 8.7 6 -5.3e4 4 2 -12 .4 +.e1 - + six """ table = read_basic(text, parallel=parallel) assert_equal(table["A"].dtype.kind, "f") assert table["B"].dtype.kind in ("S", "U") assert_equal(table["C"].dtype.kind, "i") assert_equal(table["D"].dtype.kind, "f") assert table["E"].dtype.kind in ("S", "U") assert table["F"].dtype.kind in ("S", "U") assert table["G"].dtype.kind in ("S", "U") assert table["H"].dtype.kind in ("S", "U") @pytest.mark.parametrize("parallel", [True, False]) def test_delimiter(parallel, read_basic): """ Make sure that different delimiters work as expected. """ text = dedent( """ COL1 COL2 COL3 1 A -1 2 B -2 """ ) expected = Table([[1, 2], ["A", "B"], [-1, -2]], names=("COL1", "COL2", "COL3")) for sep in " ,\t#;": table = read_basic(text.replace(" ", sep), delimiter=sep, parallel=parallel) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_include_names(parallel, read_basic): """ If include_names is not None, the parser should read only those columns in include_names. """ table = read_basic( "A B C D\n1 2 3 4\n5 6 7 8", include_names=["A", "D"], parallel=parallel ) expected = Table([[1, 5], [4, 8]], names=("A", "D")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_exclude_names(parallel, read_basic): """ If exclude_names is not None, the parser should exclude the columns in exclude_names. """ table = read_basic( "A B C D\n1 2 3 4\n5 6 7 8", exclude_names=["A", "D"], parallel=parallel ) expected = Table([[2, 6], [3, 7]], names=("B", "C")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_include_exclude_names(parallel, read_basic): """ Make sure that include_names is applied before exclude_names if both are specified. """ text = dedent( """ A B C D E F G H 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 """ ) table = read_basic( text, include_names=["A", "B", "D", "F", "H"], exclude_names=["B", "F"], parallel=parallel, ) expected = Table([[1, 9], [4, 12], [8, 16]], names=("A", "D", "H")) assert_table_equal(table, expected) def test_doubled_quotes(read_csv): """ Test #8283 (fix for #8281), parsing doubled-quotes "ab""cd" in a quoted field was incorrect. """ # fmt: off tbl = '\n'.join(['a,b', '"d""","d""q"', '"""q",""""']) expected = Table([['d"', '"q'], ['d"q', '"']], names=('a', 'b')) # fmt: on dat = read_csv(tbl) assert_table_equal(dat, expected) # In addition to the local read_csv wrapper, check that default # parsing with guessing gives the right answer. for fast_reader in True, False: dat = ascii.read(tbl, fast_reader=fast_reader) assert_table_equal(dat, expected) @pytest.mark.filterwarnings( "ignore:OverflowError converting to IntType in column TIMESTAMP" ) def test_doubled_quotes_segv(): """ Test the exact example from #8281 which resulted in SEGV prior to #8283 (in contrast to the tests above that just gave the wrong answer). Attempts to produce a more minimal example were unsuccessful, so the whole thing is included. """ tbl = dedent( """ "ID","TIMESTAMP","addendum_id","bib_reference","bib_reference_url","client_application","client_category","client_sort_key","color","coordsys","creator","creator_did","data_pixel_bitpix","dataproduct_subtype","dataproduct_type","em_max","em_min","format","hips_builder","hips_copyright","hips_creation_date","hips_creation_date_1","hips_creator","hips_data_range","hips_estsize","hips_frame","hips_glu_tag","hips_hierarchy","hips_initial_dec","hips_initial_fov","hips_initial_ra","hips_lon_asc","hips_master_url","hips_order","hips_order_1","hips_order_4","hips_order_min","hips_overlay","hips_pixel_bitpix","hips_pixel_cut","hips_pixel_scale","hips_progenitor_url","hips_publisher","hips_release_date","hips_release_date_1","hips_rgb_blue","hips_rgb_green","hips_rgb_red","hips_sampling","hips_service_url","hips_service_url_1","hips_service_url_2","hips_service_url_3","hips_service_url_4","hips_service_url_5","hips_service_url_6","hips_service_url_7","hips_service_url_8","hips_skyval","hips_skyval_method","hips_skyval_value","hips_status","hips_status_1","hips_status_2","hips_status_3","hips_status_4","hips_status_5","hips_status_6","hips_status_7","hips_status_8","hips_tile_format","hips_tile_format_1","hips_tile_format_4","hips_tile_width","hips_version","hipsgen_date","hipsgen_date_1","hipsgen_date_10","hipsgen_date_11","hipsgen_date_12","hipsgen_date_2","hipsgen_date_3","hipsgen_date_4","hipsgen_date_5","hipsgen_date_6","hipsgen_date_7","hipsgen_date_8","hipsgen_date_9","hipsgen_params","hipsgen_params_1","hipsgen_params_10","hipsgen_params_11","hipsgen_params_12","hipsgen_params_2","hipsgen_params_3","hipsgen_params_4","hipsgen_params_5","hipsgen_params_6","hipsgen_params_7","hipsgen_params_8","hipsgen_params_9","label","maxOrder","moc_access_url","moc_order","moc_release_date","moc_sky_fraction","obs_ack","obs_collection","obs_copyrigh_url","obs_copyright","obs_copyright_1","obs_copyright_url","obs_copyright_url_1","obs_description","obs_description_url","obs_descrition_url","obs_id","obs_initial_dec","obs_initial_fov","obs_initial_ra","obs_provenance","obs_regime","obs_title","ohips_frame","pixelCut","pixelRange","prov_did","prov_progenitor","prov_progenitor_url","publisher_did","publisher_id","s_pixel_scale","t_max","t_min" "CDS/P/2MASS/H","1524123841000","","2006AJ....131.1163S","http://cdsbib.u-strasbg.fr/cgi-bin/cdsbib?2006AJ....131.1163S","AladinDesktop","Image/Infrared/2MASS","04-001-03","","","","ivo://CDS/P/2MASS/H","","","image","1.798E-6","1.525E-6","","Aladin/HipsGen v9.017","CNRS/Unistra","2013-05-06T20:36Z","","CDS (A.Oberto)","","","equatorial","","mean","","","","","","9","","","","","","0 60","2.236E-4","","","2016-04-22T13:48Z","","","","","","http://alasky.u-strasbg.fr/2MASS/H","https://irsa.ipac.caltech.edu/data/hips/CDS/2MASS/H","http://alaskybis.u-strasbg.fr/2MASS/H","https://alaskybis.u-strasbg.fr/2MASS/H","","","","","","","","","public master clonableOnce","public mirror unclonable","public mirror clonableOnce","public mirror clonableOnce","","","","","","jpeg fits","","","512","1.31","","","","","","","","","","","","","","","","","","","","","","","","","","","","","http://alasky.u-strasbg.fr/2MASS/H/Moc.fits","9","","1","University of Massachusetts & IPAC/Caltech","The Two Micron All Sky Survey - H band (2MASS H)","","University of Massachusetts & IPAC/Caltech","","http://www.ipac.caltech.edu/2mass/","","2MASS has uniformly scanned the entire sky in three near-infrared bands to detect and characterize point sources brighter than about 1 mJy in each band, with signal-to-noise ratio (SNR) greater than 10, using a pixel size of 2.0"". This has achieved an 80,000-fold improvement in sensitivity relative to earlier surveys. 2MASS used two highly-automated 1.3-m telescopes, one at Mt. Hopkins, AZ, and one at CTIO, Chile. Each telescope was equipped with a three-channel camera, each channel consisting of a 256x256 array of HgCdTe detectors, capable of observing the sky simultaneously at J (1.25 microns), H (1.65 microns), and Ks (2.17 microns). The University of Massachusetts (UMass) was responsible for the overall management of the project, and for developing the infrared cameras and on-site computing systems at both facilities. The Infrared Processing and Analysis Center (IPAC) is responsible for all data processing through the Production Pipeline, and construction and distribution of the data products. Funding is provided primarily by NASA and the NSF","","","","+0","0.11451621372724685","0","","Infrared","2MASS H (1.66um)","","","","","IPAC/NASA","","","","","51941","50600" """ ) ascii.read(tbl, format="csv", fast_reader=True, guess=False) @pytest.mark.parametrize("parallel", [True, False]) def test_quoted_fields(parallel, read_basic): """ The character quotechar (default '"') should denote the start of a field which can contain the field delimiter and newlines. """ if parallel: pytest.xfail("Multiprocessing can fail with quoted fields") text = dedent( """ "A B" C D 1.5 2.1 -37.1 a b " c d" """ ) table = read_basic(text, parallel=parallel) expected = Table( [["1.5", "a"], ["2.1", "b"], ["-37.1", "c\nd"]], names=("A B", "C", "D") ) assert_table_equal(table, expected) table = read_basic(text.replace('"', "'"), quotechar="'", parallel=parallel) assert_table_equal(table, expected) @pytest.mark.parametrize( "key,val", [ ("delimiter", ",,"), # multi-char delimiter ("comment", "##"), # multi-char comment ("data_start", None), # data_start=None ("data_start", -1), # data_start negative ("quotechar", "##"), # multi-char quote signifier ("header_start", -1), # negative header_start ( "converters", {i + 1: ascii.convert_numpy(np.uint) for i in range(3)}, ), # passing converters ("Inputter", ascii.ContinuationLinesInputter), # passing Inputter ("header_Splitter", ascii.DefaultSplitter), # passing Splitter ("data_Splitter", ascii.DefaultSplitter), ], ) def test_invalid_parameters(key, val): """ Make sure the C reader raises an error if passed parameters it can't handle. """ with pytest.raises(ParameterError): FastBasic(**{key: val}).read("1 2 3\n4 5 6") with pytest.raises(ParameterError): ascii.read("1 2 3\n4 5 6", format="fast_basic", guess=False, **{key: val}) def test_invalid_parameters_other(): with pytest.raises(TypeError): FastBasic(foo=7).read("1 2 3\n4 5 6") # unexpected argument with pytest.raises(FastOptionsError): # don't fall back on the slow reader ascii.read("1 2 3\n4 5 6", format="basic", fast_reader={"foo": 7}) with pytest.raises(ParameterError): # Outputter cannot be specified in constructor FastBasic(Outputter=ascii.TableOutputter).read("1 2 3\n4 5 6") def test_too_many_cols1(): """ If a row contains too many columns, the C reader should raise an error. """ text = dedent( """ A B C 1 2 3 4 5 6 7 8 9 10 11 12 13 """ ) with pytest.raises(InconsistentTableError) as e: FastBasic().read(text) assert ( "Number of header columns (3) inconsistent with data columns in data line 2" in str(e.value) ) def test_too_many_cols2(): text = """\ aaa,bbb 1,2, 3,4, """ with pytest.raises(InconsistentTableError) as e: FastCsv().read(text) assert ( "Number of header columns (2) inconsistent with data columns in data line 0" in str(e.value) ) def test_too_many_cols3(): text = """\ aaa,bbb 1,2,, 3,4, """ with pytest.raises(InconsistentTableError) as e: FastCsv().read(text) assert ( "Number of header columns (2) inconsistent with data columns in data line 0" in str(e.value) ) def test_too_many_cols4(): # https://github.com/astropy/astropy/issues/9922 with pytest.raises(InconsistentTableError) as e: ascii.read( get_pkg_data_filename("data/conf_py.txt"), fast_reader=True, guess=True ) assert "Unable to guess table format with the guesses listed below" in str(e.value) @pytest.mark.parametrize("parallel", [True, False]) def test_not_enough_cols(parallel, read_csv): """ If a row does not have enough columns, the FastCsv reader should add empty fields while the FastBasic reader should raise an error. """ text = """ A,B,C 1,2,3 4,5 6,7,8 """ table = read_csv(text, parallel=parallel) assert table["B"][1] is not ma.masked assert table["C"][1] is ma.masked with pytest.raises(InconsistentTableError): table = FastBasic(delimiter=",").read(text) @pytest.mark.parametrize("parallel", [True, False]) def test_data_end(parallel, read_basic, read_rdb): """ The parameter data_end should specify where data reading ends. """ text = """ A B C 1 2 3 4 5 6 7 8 9 10 11 12 """ table = read_basic(text, data_end=3, parallel=parallel) expected = Table([[1, 4], [2, 5], [3, 6]], names=("A", "B", "C")) assert_table_equal(table, expected) # data_end supports negative indexing table = read_basic(text, data_end=-2, parallel=parallel) assert_table_equal(table, expected) text = """ A\tB\tC N\tN\tS 1\t2\ta 3\t4\tb 5\t6\tc """ # make sure data_end works with RDB table = read_rdb(text, data_end=-1, parallel=parallel) expected = Table([[1, 3], [2, 4], ["a", "b"]], names=("A", "B", "C")) assert_table_equal(table, expected) # positive index table = read_rdb(text, data_end=3, parallel=parallel) expected = Table([[1], [2], ["a"]], names=("A", "B", "C")) assert_table_equal(table, expected) # empty table if data_end is too small table = read_rdb(text, data_end=1, parallel=parallel) expected = Table([[], [], []], names=("A", "B", "C")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_inf_nan(parallel, read_basic): """ Test that inf and nan-like values are correctly parsed on all platforms. Regression test for https://github.com/astropy/astropy/pull/3525 """ text = dedent( """\ A nan +nan -nan inf infinity +inf +infinity -inf -infinity """ ) expected = Table( { "A": [ np.nan, np.nan, np.nan, np.inf, np.inf, np.inf, np.inf, -np.inf, -np.inf, ] } ) table = read_basic(text, parallel=parallel) assert table["A"].dtype.kind == "f" assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_fill_values(parallel, read_basic): """ Make sure that the parameter fill_values works as intended. If fill_values is not specified, the default behavior should be to convert '' to 0. """ text = """ A, B, C , 2, nan a, -999, -3.4 nan, 5, -9999 8, nan, 7.6e12 """ table = read_basic(text, delimiter=",", parallel=parallel) # The empty value in row A should become a masked '0' assert isinstance(table["A"], MaskedColumn) assert table["A"][0] is ma.masked # '0' rather than 0 because there is a string in the column assert_equal(table["A"].data.data[0], "0") assert table["A"][1] is not ma.masked table = read_basic( text, delimiter=",", fill_values=("-999", "0"), parallel=parallel ) assert isinstance(table["B"], MaskedColumn) assert table["A"][0] is not ma.masked # empty value unaffected assert table["C"][2] is not ma.masked # -9999 is not an exact match assert table["B"][1] is ma.masked # Numeric because the rest of the column contains numeric data assert_equal(table["B"].data.data[1], 0.0) assert table["B"][0] is not ma.masked table = read_basic(text, delimiter=",", fill_values=[], parallel=parallel) # None of the columns should be masked for name in "ABC": assert not isinstance(table[name], MaskedColumn) table = read_basic( text, delimiter=",", fill_values=[("", "0", "A"), ("nan", "999", "A", "C")], parallel=parallel, ) assert np.isnan(table["B"][3]) # nan filling skips column B # should skip masking as well as replacing nan assert table["B"][3] is not ma.masked assert table["A"][0] is ma.masked assert table["A"][2] is ma.masked assert_equal(table["A"].data.data[0], "0") assert_equal(table["A"].data.data[2], "999") assert table["C"][0] is ma.masked assert_almost_equal(table["C"].data.data[0], 999.0) assert_almost_equal(table["C"][1], -3.4) # column is still of type float @pytest.mark.parametrize("parallel", [True, False]) def test_fill_include_exclude_names(parallel, read_csv): """ fill_include_names and fill_exclude_names should filter missing/empty value handling in the same way that include_names and exclude_names filter output columns. """ text = """ A, B, C , 1, 2 3, , 4 5, 5, """ table = read_csv(text, fill_include_names=["A", "B"], parallel=parallel) assert table["A"][0] is ma.masked assert table["B"][1] is ma.masked assert table["C"][2] is not ma.masked # C not in fill_include_names table = read_csv(text, fill_exclude_names=["A", "B"], parallel=parallel) assert table["C"][2] is ma.masked assert table["A"][0] is not ma.masked assert table["B"][1] is not ma.masked # A and B excluded from fill handling table = read_csv( text, fill_include_names=["A", "B"], fill_exclude_names=["B"], parallel=parallel ) assert table["A"][0] is ma.masked # fill_exclude_names applies after fill_include_names assert table["B"][1] is not ma.masked assert table["C"][2] is not ma.masked @pytest.mark.parametrize("parallel", [True, False]) def test_many_rows(parallel, read_basic): """ Make sure memory reallocation works okay when the number of rows is large (so that each column string is longer than INITIAL_COL_SIZE). """ text = "A B C\n" for i in range(500): # create 500 rows text += " ".join([str(i) for i in range(3)]) text += "\n" table = read_basic(text, parallel=parallel) expected = Table([[0] * 500, [1] * 500, [2] * 500], names=("A", "B", "C")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_many_columns(parallel, read_basic): """ Make sure memory reallocation works okay when the number of columns is large (so that each header string is longer than INITIAL_HEADER_SIZE). """ # create a string with 500 columns and two data rows text = " ".join([str(i) for i in range(500)]) text += "\n" + text + "\n" + text table = read_basic(text, parallel=parallel) expected = Table([[i, i] for i in range(500)], names=[str(i) for i in range(500)]) assert_table_equal(table, expected) def test_fast_reader(): """ Make sure that ascii.read() works as expected by default and with fast_reader specified. """ text = "a b c\n1 2 3\n4 5 6" with pytest.raises(ParameterError): # C reader can't handle regex comment ascii.read(text, format="fast_basic", guess=False, comment="##") # Enable multiprocessing and the fast converter try: ascii.read( text, format="basic", guess=False, fast_reader={"parallel": True, "use_fast_converter": True}, ) except NotImplementedError: # Might get this on Windows, try without parallel... if os.name == "nt": ascii.read( text, format="basic", guess=False, fast_reader={"parallel": False, "use_fast_converter": True}, ) else: raise # Should raise an error if fast_reader has an invalid key with pytest.raises(FastOptionsError): ascii.read(text, format="fast_basic", guess=False, fast_reader={"foo": True}) # Use the slow reader instead ascii.read(text, format="basic", guess=False, comment="##", fast_reader=False) # Will try the slow reader afterwards by default ascii.read(text, format="basic", guess=False, comment="##") @pytest.mark.parametrize("parallel", [True, False]) def test_read_tab(parallel, read_tab): """ The fast reader for tab-separated values should not strip whitespace, unlike the basic reader. """ if parallel: pytest.xfail("Multiprocessing can fail with quoted fields") text = '1\t2\t3\n a\t b \t\n c\t" d\n e"\t ' table = read_tab(text, parallel=parallel) assert_equal(table["1"][0], " a") # preserve line whitespace assert_equal(table["2"][0], " b ") # preserve field whitespace assert table["3"][0] is ma.masked # empty value should be masked assert_equal(table["2"][1], " d\n e") # preserve whitespace in quoted fields assert_equal(table["3"][1], " ") # preserve end-of-line whitespace @pytest.mark.parametrize("parallel", [True, False]) def test_default_data_start(parallel, read_basic): """ If data_start is not explicitly passed to read(), data processing should beginning right after the header. """ text = "ignore this line\na b c\n1 2 3\n4 5 6" table = read_basic(text, header_start=1, parallel=parallel) expected = Table([[1, 4], [2, 5], [3, 6]], names=("a", "b", "c")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_commented_header(parallel, read_commented_header): """ The FastCommentedHeader reader should mimic the behavior of the CommentedHeader by overriding the default header behavior of FastBasic. """ text = """ # A B C 1 2 3 4 5 6 """ t1 = read_commented_header(text, parallel=parallel) expected = Table([[1, 4], [2, 5], [3, 6]], names=("A", "B", "C")) assert_table_equal(t1, expected) text = "# first commented line\n # second commented line\n\n" + text t2 = read_commented_header(text, header_start=2, data_start=0, parallel=parallel) assert_table_equal(t2, expected) # negative indexing allowed t3 = read_commented_header(text, header_start=-1, data_start=0, parallel=parallel) assert_table_equal(t3, expected) text += "7 8 9" t4 = read_commented_header(text, header_start=2, data_start=2, parallel=parallel) expected = Table([[7], [8], [9]], names=("A", "B", "C")) assert_table_equal(t4, expected) with pytest.raises(ParameterError): # data_start cannot be negative read_commented_header(text, header_start=-1, data_start=-1, parallel=parallel) @pytest.mark.parametrize("parallel", [True, False]) def test_rdb(parallel, read_rdb): """ Make sure the FastRdb reader works as expected. """ text = """ A\tB\tC 1n\tS\t4N 1\t 9\t4.3 """ table = read_rdb(text, parallel=parallel) expected = Table([[1], [" 9"], [4.3]], names=("A", "B", "C")) assert_table_equal(table, expected) assert_equal(table["A"].dtype.kind, "i") assert table["B"].dtype.kind in ("S", "U") assert_equal(table["C"].dtype.kind, "f") with pytest.raises(ValueError) as e: text = "A\tB\tC\nN\tS\tN\n4\tb\ta" # C column contains non-numeric data read_rdb(text, parallel=parallel) assert "Column C failed to convert" in str(e.value) with pytest.raises(ValueError) as e: text = "A\tB\tC\nN\tN\n1\t2\t3" # not enough types specified read_rdb(text, parallel=parallel) assert "mismatch between number of column names and column types" in str(e.value) with pytest.raises(ValueError) as e: text = "A\tB\tC\nN\tN\t5\n1\t2\t3" # invalid type for column C read_rdb(text, parallel=parallel) assert "type definitions do not all match [num](N|S)" in str(e.value) @pytest.mark.parametrize("parallel", [True, False]) def test_data_start(parallel, read_basic): """ Make sure that data parsing begins at data_start (ignoring empty and commented lines but not taking quoted values into account). """ if parallel: pytest.xfail("Multiprocessing can fail with quoted fields") text = """ A B C 1 2 3 4 5 6 7 8 "9 1" # comment 10 11 12 """ table = read_basic(text, data_start=2, parallel=parallel) expected = Table( [[4, 7, 10], [5, 8, 11], ["6", "9\n1", "12"]], names=("A", "B", "C") ) assert_table_equal(table, expected) table = read_basic(text, data_start=3, parallel=parallel) # ignore empty line expected = Table([[7, 10], [8, 11], ["9\n1", "12"]], names=("A", "B", "C")) assert_table_equal(table, expected) with pytest.raises(InconsistentTableError) as e: # tries to begin in the middle of quoted field read_basic(text, data_start=4, parallel=parallel) assert "header columns (3) inconsistent with data columns in data line 0" in str( e.value ) table = read_basic(text, data_start=5, parallel=parallel) # ignore commented line expected = Table([[10], [11], [12]], names=("A", "B", "C")) assert_table_equal(table, expected) text = """ A B C 1 2 3 4 5 6 7 8 9 # comment 10 11 12 """ # make sure reading works as expected in parallel table = read_basic(text, data_start=2, parallel=parallel) expected = Table([[4, 7, 10], [5, 8, 11], [6, 9, 12]], names=("A", "B", "C")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_quoted_empty_values(parallel, read_basic): """ Quoted empty values spanning multiple lines should be treated correctly. """ if parallel: pytest.xfail("Multiprocessing can fail with quoted fields") text = 'a b c\n1 2 " \n "' table = read_basic(text, parallel=parallel) assert table["c"][0] == "\n" # empty value masked by default @pytest.mark.parametrize("parallel", [True, False]) def test_csv_comment_default(parallel, read_csv): """ Unless the comment parameter is specified, the CSV reader should not treat any lines as comments. """ text = "a,b,c\n#1,2,3\n4,5,6" table = read_csv(text, parallel=parallel) expected = Table([["#1", "4"], [2, 5], [3, 6]], names=("a", "b", "c")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_whitespace_before_comment(parallel, read_tab): """ Readers that don't strip whitespace from data (Tab, RDB) should still treat lines with leading whitespace and then the comment char as comment lines. """ text = "a\tb\tc\n # comment line\n1\t2\t3" table = read_tab(text, parallel=parallel) expected = Table([[1], [2], [3]], names=("a", "b", "c")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_strip_line_trailing_whitespace(parallel, read_basic): """ Readers that strip whitespace from lines should ignore trailing whitespace after the last data value of each row. """ text = "a b c\n1 2 \n3 4 5" with pytest.raises(InconsistentTableError) as e: ascii.read(StringIO(text), format="fast_basic", guess=False) assert "header columns (3) inconsistent with data columns in data line 0" in str( e.value ) text = "a b c\n 1 2 3 \t \n 4 5 6 " table = read_basic(text, parallel=parallel) expected = Table([[1, 4], [2, 5], [3, 6]], names=("a", "b", "c")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_no_data(parallel, read_basic): """ As long as column names are supplied, the C reader should return an empty table in the absence of data. """ table = read_basic("a b c", parallel=parallel) expected = Table([[], [], []], names=("a", "b", "c")) assert_table_equal(table, expected) table = read_basic("a b c\n1 2 3", data_start=2, parallel=parallel) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_line_endings(parallel, read_basic, read_commented_header, read_rdb): """ Make sure the fast reader accepts CR and CR+LF as newlines. """ text = "a b c\n1 2 3\n4 5 6\n7 8 9\n" expected = Table([[1, 4, 7], [2, 5, 8], [3, 6, 9]], names=("a", "b", "c")) for newline in ("\r\n", "\r"): table = read_basic(text.replace("\n", newline), parallel=parallel) assert_table_equal(table, expected) # Make sure the splitlines() method of FileString # works with CR/CR+LF line endings text = "#" + text for newline in ("\r\n", "\r"): table = read_commented_header(text.replace("\n", newline), parallel=parallel) assert_table_equal(table, expected) expected = Table( [MaskedColumn([1, 4, 7]), [2, 5, 8], MaskedColumn([3, 6, 9])], names=("a", "b", "c"), ) expected["a"][0] = np.ma.masked expected["c"][0] = np.ma.masked text = "a\tb\tc\nN\tN\tN\n\t2\t\n4\t5\t6\n7\t8\t9\n" for newline in ("\r\n", "\r"): table = read_rdb(text.replace("\n", newline), parallel=parallel) assert_table_equal(table, expected) assert np.all(table == expected) @pytest.mark.parametrize("parallel", [True, False]) def test_store_comments(parallel, read_basic): """ Make sure that the output Table produced by the fast reader stores any comment lines in its meta attribute. """ text = """ # header comment a b c # comment 2 # comment 3 1 2 3 4 5 6 """ table = read_basic(text, parallel=parallel, check_meta=True) assert_equal(table.meta["comments"], ["header comment", "comment 2", "comment 3"]) @pytest.mark.parametrize("parallel", [True, False]) def test_empty_quotes(parallel, read_basic): """ Make sure the C reader doesn't segfault when the input data contains empty quotes. [#3407] """ table = read_basic('a b\n1 ""\n2 ""', parallel=parallel) expected = Table([[1, 2], [0, 0]], names=("a", "b")) assert_table_equal(table, expected) @pytest.mark.parametrize("parallel", [True, False]) def test_fast_tab_with_names(parallel, read_tab): """ Make sure the C reader doesn't segfault when the header for the first column is missing [#3545] """ content = """# \tdecDeg\tRate_pn_offAxis\tRate_mos2_offAxis\tObsID\tSourceID\tRADeg\tversion\tCounts_pn\tRate_pn\trun\tRate_mos1\tRate_mos2\tInserted_pn\tInserted_mos2\tbeta\tRate_mos1_offAxis\trcArcsec\tname\tInserted\tCounts_mos1\tInserted_mos1\tCounts_mos2\ty\tx\tCounts\toffAxis\tRot -3.007559\t0.0000\t0.0010\t0013140201\t0\t213.462574\t0\t2\t0.0002\t0\t0.0001\t0.0001\t0\t1\t0.66\t0.0217\t3.0\tfakeXMMXCS J1413.8-0300\t3\t1\t2\t1\t398.000\t127.000\t5\t13.9\t72.3\t""" head = [f"A{i}" for i in range(28)] read_tab(content, data_start=1, parallel=parallel, names=head) @pytest.mark.hugemem def test_read_big_table(tmp_path): """Test reading of a huge file. This test generates a huge CSV file (~2.3Gb) before reading it (see https://github.com/astropy/astropy/pull/5319). The test is run only if the ``--run-hugemem`` cli option is given. Note that running the test requires quite a lot of memory (~18Gb when reading the file) !! """ NB_ROWS = 250000 NB_COLS = 500 filename = tmp_path / "big_table.csv" print(f"Creating a {NB_ROWS} rows table ({NB_COLS} columns).") data = np.random.random(NB_ROWS) t = Table(data=[data] * NB_COLS, names=[str(i) for i in range(NB_COLS)]) data = None print(f"Saving the table to {filename}") t.write(filename, format="ascii.csv", overwrite=True) t = None print( "Counting the number of lines in the csv, it should be {NB_ROWS} + 1 (header)." ) with open(filename) as f: assert sum(1 for line in f) == NB_ROWS + 1 print("Reading the file with astropy.") t = Table.read(filename, format="ascii.csv", fast_reader=True) assert len(t) == NB_ROWS @pytest.mark.hugemem def test_read_big_table2(tmp_path): """Test reading of a file with a huge column.""" # (2**32 // 2) : max value for int # // 10 : we use a value for rows that have 10 chars (1e9) # + 5 : add a few lines so the length cannot be stored by an int NB_ROWS = 2**32 // 2 // 10 + 5 filename = tmp_path / "big_table.csv" print(f"Creating a {NB_ROWS} rows table.") data = np.full(NB_ROWS, int(1e9), dtype=np.int32) t = Table(data=[data], names=["a"], copy=False) print(f"Saving the table to {filename}") t.write(filename, format="ascii.csv", overwrite=True) t = None print( "Counting the number of lines in the csv, it should be {NB_ROWS} + 1 (header)." ) with open(filename) as f: assert sum(1 for line in f) == NB_ROWS + 1 print("Reading the file with astropy.") t = Table.read(filename, format="ascii.csv", fast_reader=True) assert len(t) == NB_ROWS # Test these both with guessing turned on and off @pytest.mark.parametrize("guess", [True, False]) # fast_reader configurations: False| 'use_fast_converter'=False|True @pytest.mark.parametrize( "fast_reader", [False, dict(use_fast_converter=False), dict(use_fast_converter=True)], ) @pytest.mark.parametrize("parallel", [False, True]) def test_data_out_of_range(parallel, fast_reader, guess): """ Numbers with exponents beyond float64 range (|~4.94e-324 to 1.7977e+308|) shall be returned as 0 and +-inf respectively by the C parser, just like the Python parser. Test fast converter only to nominal accuracy. """ # Python reader and strtod() are expected to return precise results rtol = 1.0e-30 # Update fast_reader dict; adapt relative precision for fast_converter if fast_reader: fast_reader["parallel"] = parallel if fast_reader.get("use_fast_converter"): rtol = 1.0e-15 elif np.iinfo(np.int_).dtype == np.dtype(np.int32): # On 32bit the standard C parser (strtod) returns strings for these pytest.xfail("C parser cannot handle float64 on 32bit systems") if parallel: if not fast_reader: pytest.skip("Multiprocessing only available in fast reader") elif CI: pytest.xfail("Multiprocessing can sometimes fail on CI") test_for_warnings = fast_reader and not parallel if not parallel and not fast_reader: ctx = nullcontext() else: ctx = pytest.warns() fields = ["10.1E+199", "3.14e+313", "2048e+306", "0.6E-325", "-2.e345"] values = np.array([1.01e200, np.inf, np.inf, 0.0, -np.inf]) # NOTE: Warning behavior varies for the parameters being passed in. with ctx as w: t = ascii.read( StringIO(" ".join(fields)), format="no_header", guess=guess, fast_reader=fast_reader, ) if test_for_warnings: # Assert precision warnings for cols 2-5 assert len(w) == 4 for i in range(len(w)): assert f"OverflowError converting to FloatType in column col{i+2}" in str( w[i].message ) read_values = np.array([col[0] for col in t.itercols()]) assert_almost_equal(read_values, values, rtol=rtol, atol=1.0e-324) # Test some additional corner cases fields = [ ".0101E202", "0.000000314E+314", "1777E+305", "-1799E+305", "0.2e-323", "5200e-327", " 0.0000000000000000000001024E+330", ] values = np.array( [1.01e200, 3.14e307, 1.777e308, -np.inf, 0.0, 4.94e-324, 1.024e308] ) with ctx as w: t = ascii.read( StringIO(" ".join(fields)), format="no_header", guess=guess, fast_reader=fast_reader, ) if test_for_warnings: # Assert precision warnings for cols 4-6 assert len(w) == 3 for i in range(len(w)): assert f"OverflowError converting to FloatType in column col{i+4}" in str( w[i].message ) read_values = np.array([col[0] for col in t.itercols()]) assert_almost_equal(read_values, values, rtol=rtol, atol=1.0e-324) # Test corner cases again with non-standard exponent_style (auto-detection) if fast_reader and fast_reader.get("use_fast_converter"): fast_reader.update({"exponent_style": "A"}) else: pytest.skip("Fortran exponent style only available in fast converter") fields = [ ".0101D202", "0.000000314d+314", "1777+305", "-1799E+305", "0.2e-323", "2500-327", " 0.0000000000000000000001024Q+330", ] with ctx as w: t = ascii.read( StringIO(" ".join(fields)), format="no_header", guess=guess, fast_reader=fast_reader, ) if test_for_warnings: assert len(w) == 3 read_values = np.array([col[0] for col in t.itercols()]) assert_almost_equal(read_values, values, rtol=rtol, atol=1.0e-324) @pytest.mark.parametrize("guess", [True, False]) # fast_reader configurations: False| 'use_fast_converter'=False|True @pytest.mark.parametrize( "fast_reader", [False, dict(use_fast_converter=False), dict(use_fast_converter=True)], ) @pytest.mark.parametrize("parallel", [False, True]) def test_data_at_range_limit(parallel, fast_reader, guess): """ Test parsing of fixed-format float64 numbers near range limits (|~4.94e-324 to 1.7977e+308|) - within limit for full precision (|~2.5e-307| for strtod C parser, factor 10 better for fast_converter) exact numbers shall be returned, beyond that an Overflow warning raised. Input of exactly 0.0 must not raise an OverflowError. """ # Python reader and strtod() are expected to return precise results rtol = 1.0e-30 # Update fast_reader dict; adapt relative precision for fast_converter if fast_reader: fast_reader["parallel"] = parallel if fast_reader.get("use_fast_converter"): rtol = 1.0e-15 elif np.iinfo(np.int_).dtype == np.dtype(np.int32): # On 32bit the standard C parser (strtod) returns strings for these pytest.xfail("C parser cannot handle float64 on 32bit systems") if parallel: if not fast_reader: pytest.skip("Multiprocessing only available in fast reader") elif CI: pytest.xfail("Multiprocessing can sometimes fail on CI") # Test very long fixed-format strings (to strtod range limit w/o Overflow) for D in 99, 202, 305: t = ascii.read( StringIO(99 * "0" + "." + D * "0" + "1"), format="no_header", guess=guess, fast_reader=fast_reader, ) assert_almost_equal(t["col1"][0], 10.0 ** -(D + 1), rtol=rtol, atol=1.0e-324) for D in 99, 202, 308: t = ascii.read( StringIO("1" + D * "0" + ".0"), format="no_header", guess=guess, fast_reader=fast_reader, ) assert_almost_equal(t["col1"][0], 10.0**D, rtol=rtol, atol=1.0e-324) # 0.0 is always exact (no Overflow warning)! for s in "0.0", "0.0e+0", 399 * "0" + "." + 365 * "0": t = ascii.read( StringIO(s), format="no_header", guess=guess, fast_reader=fast_reader ) assert t["col1"][0] == 0.0 # Test OverflowError at precision limit with laxer rtol if parallel: pytest.skip("Catching warnings broken in parallel mode") elif not fast_reader: pytest.skip("Python/numpy reader does not raise on Overflow") with pytest.warns() as warning_lines: t = ascii.read( StringIO("0." + 314 * "0" + "1"), format="no_header", guess=guess, fast_reader=fast_reader, ) n_warns = len(warning_lines) assert n_warns in (0, 1), f"Expected 0 or 1 warning, found {n_warns}" if n_warns == 1: assert ( "OverflowError converting to FloatType in column col1, possibly " "resulting in degraded precision" in str(warning_lines[0].message) ) assert_almost_equal(t["col1"][0], 1.0e-315, rtol=1.0e-10, atol=1.0e-324) @pytest.mark.parametrize("guess", [True, False]) @pytest.mark.parametrize("parallel", [False, True]) def test_int_out_of_range(parallel, guess): """ Integer numbers outside int range shall be returned as string columns consistent with the standard (Python) parser (no 'upcasting' to float). """ imin = np.iinfo(int).min + 1 imax = np.iinfo(int).max - 1 huge = f"{imax+2:d}" text = f"P M S\n {imax:d} {imin:d} {huge:s}" expected = Table([[imax], [imin], [huge]], names=("P", "M", "S")) # NOTE: Warning behavior varies for the parameters being passed in. with pytest.warns() as w: table = ascii.read( text, format="basic", guess=guess, fast_reader={"parallel": parallel} ) if not parallel: assert len(w) == 1 assert ( "OverflowError converting to IntType in column S, reverting to String" in str(w[0].message) ) assert_table_equal(table, expected) # Check with leading zeroes to make sure strtol does not read them as octal text = f"P M S\n000{imax:d} -0{-imin:d} 00{huge:s}" expected = Table([[imax], [imin], ["00" + huge]], names=("P", "M", "S")) with pytest.warns() as w: table = ascii.read( text, format="basic", guess=guess, fast_reader={"parallel": parallel} ) if not parallel: assert len(w) == 1 assert ( "OverflowError converting to IntType in column S, reverting to String" in str(w[0].message) ) assert_table_equal(table, expected) @pytest.mark.parametrize("guess", [True, False]) def test_int_out_of_order(guess): """ Mixed columns should be returned as float, but if the out-of-range integer shows up first, it will produce a string column - with both readers. Broken with the parallel fast_reader. """ imax = np.iinfo(int).max - 1 text = f"A B\n 12.3 {imax:d}0\n {imax:d}0 45.6e7" expected = Table([[12.3, 10.0 * imax], [f"{imax:d}0", "45.6e7"]], names=("A", "B")) with pytest.warns( AstropyWarning, match=r"OverflowError converting to " r"IntType in column B, reverting to String", ): table = ascii.read(text, format="basic", guess=guess, fast_reader=True) assert_table_equal(table, expected) with pytest.warns( AstropyWarning, match=r"OverflowError converting to " r"IntType in column B, reverting to String", ): table = ascii.read(text, format="basic", guess=guess, fast_reader=False) assert_table_equal(table, expected) @pytest.mark.parametrize("guess", [True, False]) @pytest.mark.parametrize("parallel", [False, True]) def test_fortran_reader(parallel, guess): """ Make sure that ascii.read() can read Fortran-style exponential notation using the fast_reader. """ # Check for nominal np.float64 precision rtol = 1.0e-15 atol = 0.0 text = ( "A B C D\n100.01{:s}99 2.0 2.0{:s}-103 3\n" + " 4.2{:s}-1 5.0{:s}-1 0.6{:s}4 .017{:s}+309" ) expc = Table( [[1.0001e101, 0.42], [2, 0.5], [2.0e-103, 6.0e3], [3, 1.7e307]], names=("A", "B", "C", "D"), ) expstyles = { "e": 6 * "E", "D": ("D", "d", "d", "D", "d", "D"), "Q": 3 * ("q", "Q"), "Fortran": ("E", "0", "D", "Q", "d", "0"), } # C strtod (not-fast converter) can't handle Fortran exp with pytest.raises(FastOptionsError) as e: ascii.read( text.format(*(6 * "D")), format="basic", guess=guess, fast_reader={ "use_fast_converter": False, "parallel": parallel, "exponent_style": "D", }, ) assert "fast_reader: exponent_style requires use_fast_converter" in str(e.value) # Enable multiprocessing and the fast converter iterate over # all style-exponent combinations, with auto-detection for s, c in expstyles.items(): table = ascii.read( text.format(*c), guess=guess, fast_reader={"parallel": parallel, "exponent_style": s}, ) assert_table_equal(table, expc, rtol=rtol, atol=atol) # Additional corner-case checks including triple-exponents without # any character and mixed whitespace separators text = ( "A B\t\t C D\n1.0001+101 2.0+000\t 0.0002-099 3\n " + "0.42-000 \t 0.5 6.+003 0.000000000000000000000017+330" ) table = ascii.read( text, guess=guess, fast_reader={"parallel": parallel, "exponent_style": "A"} ) assert_table_equal(table, expc, rtol=rtol, atol=atol) @pytest.mark.parametrize("guess", [True, False]) @pytest.mark.parametrize("parallel", [False, True]) def test_fortran_invalid_exp(parallel, guess): """ Test Fortran-style exponential notation in the fast_reader with invalid exponent-like patterns (no triple-digits) to make sure they are returned as strings instead, as with the standard C parser. """ if parallel and CI: pytest.xfail("Multiprocessing can sometimes fail on CI") formats = {"basic": " ", "tab": "\t", "csv": ","} header = ["S1", "F2", "S2", "F3", "S3", "F4", "F5", "S4", "I1", "F6", "F7"] # Tested entries and expected returns, first for auto-detect, # then for different specified exponents # fmt: off fields = ['1.0001+1', '.42d1', '2.3+10', '0.5', '3+1001', '3000.', '2', '4.56e-2.3', '8000', '4.2-022', '.00000145e314'] vals_e = ['1.0001+1', '.42d1', '2.3+10', 0.5, '3+1001', 3.e3, 2, '4.56e-2.3', 8000, '4.2-022', 1.45e308] vals_d = ['1.0001+1', 4.2, '2.3+10', 0.5, '3+1001', 3.e3, 2, '4.56e-2.3', 8000, '4.2-022', '.00000145e314'] vals_a = ['1.0001+1', 4.2, '2.3+10', 0.5, '3+1001', 3.e3, 2, '4.56e-2.3', 8000, 4.2e-22, 1.45e308] vals_v = ['1.0001+1', 4.2, '2.3+10', 0.5, '3+1001', 3.e3, 2, '4.56e-2.3', 8000, '4.2-022', 1.45e308] # fmt: on # Iterate over supported format types and separators for f, s in formats.items(): t1 = ascii.read( StringIO(s.join(header) + "\n" + s.join(fields)), format=f, guess=guess, fast_reader={"parallel": parallel, "exponent_style": "A"}, ) assert_table_equal(t1, Table([[col] for col in vals_a], names=header)) # Non-basic separators require guessing enabled to be detected if guess: formats["bar"] = "|" else: formats = {"basic": " "} for s in formats.values(): t2 = ascii.read( StringIO(s.join(header) + "\n" + s.join(fields)), guess=guess, fast_reader={"parallel": parallel, "exponent_style": "a"}, ) assert_table_equal(t2, Table([[col] for col in vals_a], names=header)) # Iterate for (default) expchar 'E' for s in formats.values(): t3 = ascii.read( StringIO(s.join(header) + "\n" + s.join(fields)), guess=guess, fast_reader={"parallel": parallel, "use_fast_converter": True}, ) assert_table_equal(t3, Table([[col] for col in vals_e], names=header)) # Iterate for expchar 'D' for s in formats.values(): t4 = ascii.read( StringIO(s.join(header) + "\n" + s.join(fields)), guess=guess, fast_reader={"parallel": parallel, "exponent_style": "D"}, ) assert_table_equal(t4, Table([[col] for col in vals_d], names=header)) # Iterate for regular converter (strtod) for s in formats.values(): t5 = ascii.read( StringIO(s.join(header) + "\n" + s.join(fields)), guess=guess, fast_reader={"parallel": parallel, "use_fast_converter": False}, ) read_values = [col[0] for col in t5.itercols()] if os.name == "nt": # Apparently C strtod() on (some?) MSVC recognizes 'd' exponents! assert read_values == vals_v or read_values == vals_e else: assert read_values == vals_e def test_fortran_reader_notbasic(): """ Check if readers without a fast option raise a value error when a fast_reader is asked for (implies the default 'guess=True'). """ tabstr = dedent( """ a b 1 1.23D4 2 5.67D-8 """ )[1:-1] t1 = ascii.read(tabstr.split("\n"), fast_reader=dict(exponent_style="D")) assert t1["b"].dtype.kind == "f" tabrdb = dedent( """ a\tb # A simple RDB table N\tN 1\t 1.23D4 2\t 5.67-008 """ )[1:-1] t2 = ascii.read( tabrdb.split("\n"), format="rdb", fast_reader=dict(exponent_style="fortran") ) assert t2["b"].dtype.kind == "f" tabrst = dedent( """ = ======= a b = ======= 1 1.23E4 2 5.67E-8 = ======= """ )[1:-1] t3 = ascii.read(tabrst.split("\n"), format="rst") assert t3["b"].dtype.kind == "f" t4 = ascii.read(tabrst.split("\n"), guess=True) assert t4["b"].dtype.kind == "f" # In the special case of fast_converter=True (the default), # incompatibility is ignored t5 = ascii.read(tabrst.split("\n"), format="rst", fast_reader=True) assert t5["b"].dtype.kind == "f" with pytest.raises(ParameterError): ascii.read(tabrst.split("\n"), format="rst", guess=False, fast_reader="force") with pytest.raises(ParameterError): ascii.read( tabrst.split("\n"), format="rst", guess=False, fast_reader=dict(use_fast_converter=False), ) tabrst = tabrst.replace("E", "D") with pytest.raises(ParameterError): ascii.read( tabrst.split("\n"), format="rst", guess=False, fast_reader=dict(exponent_style="D"), ) @pytest.mark.parametrize("guess", [True, False]) @pytest.mark.parametrize( "fast_reader", [dict(exponent_style="D"), dict(exponent_style="A")] ) def test_dict_kwarg_integrity(fast_reader, guess): """ Check if dictionaries passed as kwargs (fast_reader in this test) are left intact by ascii.read() """ expstyle = fast_reader.get("exponent_style", "E") fields = ["10.1D+199", "3.14d+313", "2048d+306", "0.6D-325", "-2.d345"] ascii.read(StringIO(" ".join(fields)), guess=guess, fast_reader=fast_reader) assert fast_reader.get("exponent_style", None) == expstyle @pytest.mark.parametrize( "fast_reader", [False, dict(parallel=True), dict(parallel=False)] ) def test_read_empty_basic_table_with_comments(fast_reader): """ Test for reading a "basic" format table that has no data but has comments. Tests the fix for #8267. """ dat = """ # comment 1 # comment 2 col1 col2 """ t = ascii.read(dat, fast_reader=fast_reader) assert t.meta["comments"] == ["comment 1", "comment 2"] assert len(t) == 0 assert t.colnames == ["col1", "col2"] @pytest.mark.parametrize( "fast_reader", [dict(use_fast_converter=True), dict(exponent_style="A")] ) def test_conversion_fast(fast_reader): """ The reader should try to convert each column to ints. If this fails, the reader should try to convert to floats. Failing this, i.e. on parsing non-numeric input including isolated positive/negative signs, it should fall back to strings. """ text = """ A B C D E F G H 1 a 3 4 5 6 7 8 2. 1 9 -.1e1 10.0 8.7 6 -5.3e4 4 2 -12 .4 +.e1 - + six """ table = ascii.read(text, fast_reader=fast_reader) assert_equal(table["A"].dtype.kind, "f") assert table["B"].dtype.kind in ("S", "U") assert_equal(table["C"].dtype.kind, "i") assert_equal(table["D"].dtype.kind, "f") assert table["E"].dtype.kind in ("S", "U") assert table["F"].dtype.kind in ("S", "U") assert table["G"].dtype.kind in ("S", "U") assert table["H"].dtype.kind in ("S", "U") @pytest.mark.parametrize("delimiter", ["\n", "\r"]) @pytest.mark.parametrize("fast_reader", [False, True, "force"]) def test_newline_as_delimiter(delimiter, fast_reader): """ Check that newline characters are correctly handled as delimiters. Tests the fix for #9928. """ if delimiter == "\r": eol = "\n" else: eol = "\r" inp0 = ["a | b | c ", " 1 | '2' | 3.00000 "] inp1 = "a {0:s} b {0:s}c{1:s} 1 {0:s}'2'{0:s} 3.0".format(delimiter, eol) inp2 = [f"a {delimiter} b{delimiter} c", f"1{delimiter} '2' {delimiter} 3.0"] t0 = ascii.read(inp0, delimiter="|", fast_reader=fast_reader) t1 = ascii.read(inp1, delimiter=delimiter, fast_reader=fast_reader) t2 = ascii.read(inp2, delimiter=delimiter, fast_reader=fast_reader) assert t1.colnames == t2.colnames == ["a", "b", "c"] assert len(t1) == len(t2) == 1 assert t1["b"].dtype.kind in ("S", "U") assert t2["b"].dtype.kind in ("S", "U") assert_table_equal(t1, t0) assert_table_equal(t2, t0) inp0 = 'a {0:s} b {0:s} c{1:s} 1 {0:s}"2"{0:s} 3.0'.format("|", eol) inp1 = 'a {0:s} b {0:s} c{1:s} 1 {0:s}"2"{0:s} 3.0'.format(delimiter, eol) t0 = ascii.read(inp0, delimiter="|", fast_reader=fast_reader) t1 = ascii.read(inp1, delimiter=delimiter, fast_reader=fast_reader) if not fast_reader: pytest.xfail("Quoted fields are not parsed correctly by BaseSplitter") assert_equal(t1["b"].dtype.kind, "i") @pytest.mark.parametrize("delimiter", [" ", "|", "\n", "\r"]) @pytest.mark.parametrize("fast_reader", [False, True, "force"]) def test_single_line_string(delimiter, fast_reader): """ String input without a newline character is interpreted as filename, unless element of an iterable. Maybe not logical, but test that it is at least treated consistently. """ expected = Table([[1], [2], [3.00]], names=("col1", "col2", "col3")) text = "1{0:s}2{0:s}3.0".format(delimiter) if delimiter in ("\r", "\n"): t1 = ascii.read( text, format="no_header", delimiter=delimiter, fast_reader=fast_reader ) assert_table_equal(t1, expected) else: # Windows raises OSError, but not the other OSes. with pytest.raises((FileNotFoundError, OSError)): t1 = ascii.read( text, format="no_header", delimiter=delimiter, fast_reader=fast_reader ) t2 = ascii.read( [text], format="no_header", delimiter=delimiter, fast_reader=fast_reader ) assert_table_equal(t2, expected)
84e37a15bbc9de691c67fa7127b894d32877a289ee9a7779744efb435e8e1602
# Licensed under a 3-clause BSD style license - see LICENSE.rst from io import StringIO import numpy as np import pytest from astropy.io import ascii from astropy.io.ascii.core import InconsistentTableError from .common import assert_almost_equal, assert_equal def assert_equal_splitlines(arg1, arg2): assert_equal(arg1.splitlines(), arg2.splitlines()) def test_read_normal(): """Nice, typical fixed format table""" table = """ # comment (with blank line above) | Col1 | Col2 | | 1.2 | "hello" | | 2.4 |'s worlds| """ reader = ascii.get_reader(Reader=ascii.FixedWidth) dat = reader.read(table) assert_equal(dat.colnames, ["Col1", "Col2"]) assert_almost_equal(dat[1][0], 2.4) assert_equal(dat[0][1], '"hello"') assert_equal(dat[1][1], "'s worlds") def test_read_normal_names(): """Nice, typical fixed format table with col names provided""" table = """ # comment (with blank line above) | Col1 | Col2 | | 1.2 | "hello" | | 2.4 |'s worlds| """ reader = ascii.get_reader(Reader=ascii.FixedWidth, names=("name1", "name2")) dat = reader.read(table) assert_equal(dat.colnames, ["name1", "name2"]) assert_almost_equal(dat[1][0], 2.4) def test_read_normal_names_include(): """Nice, typical fixed format table with col names provided""" table = """ # comment (with blank line above) | Col1 | Col2 | Col3 | | 1.2 | "hello" | 3 | | 2.4 |'s worlds| 7 | """ reader = ascii.get_reader( Reader=ascii.FixedWidth, names=("name1", "name2", "name3"), include_names=("name1", "name3"), ) dat = reader.read(table) assert_equal(dat.colnames, ["name1", "name3"]) assert_almost_equal(dat[1][0], 2.4) assert_equal(dat[0][1], 3) def test_read_normal_exclude(): """Nice, typical fixed format table with col name excluded""" table = """ # comment (with blank line above) | Col1 | Col2 | | 1.2 | "hello" | | 2.4 |'s worlds| """ reader = ascii.get_reader(Reader=ascii.FixedWidth, exclude_names=("Col1",)) dat = reader.read(table) assert_equal(dat.colnames, ["Col2"]) assert_equal(dat[1][0], "'s worlds") def test_read_weird(): """Weird input table with data values chopped by col extent""" table = """ Col1 | Col2 | 1.2 "hello" 2.4 sdf's worlds """ reader = ascii.get_reader(Reader=ascii.FixedWidth) dat = reader.read(table) assert_equal(dat.colnames, ["Col1", "Col2"]) assert_almost_equal(dat[1][0], 2.4) assert_equal(dat[0][1], '"hel') assert_equal(dat[1][1], "df's wo") def test_read_double(): """Table with double delimiters""" table = """ || Name || Phone || TCP|| | John | 555-1234 |192.168.1.10X| | Mary | 555-2134 |192.168.1.12X| | Bob | 555-4527 | 192.168.1.9X| """ dat = ascii.read(table, Reader=ascii.FixedWidth, guess=False) assert_equal(tuple(dat.dtype.names), ("Name", "Phone", "TCP")) assert_equal(dat[1][0], "Mary") assert_equal(dat[0][1], "555-1234") assert_equal(dat[2][2], "192.168.1.9") def test_read_space_delimiter(): """Table with space delimiter""" table = """ Name --Phone- ----TCP----- John 555-1234 192.168.1.10 Mary 555-2134 192.168.1.12 Bob 555-4527 192.168.1.9 """ dat = ascii.read(table, Reader=ascii.FixedWidth, guess=False, delimiter=" ") assert_equal(tuple(dat.dtype.names), ("Name", "--Phone-", "----TCP-----")) assert_equal(dat[1][0], "Mary") assert_equal(dat[0][1], "555-1234") assert_equal(dat[2][2], "192.168.1.9") def test_read_no_header_autocolumn(): """Table with no header row and auto-column naming""" table = """ | John | 555-1234 |192.168.1.10| | Mary | 555-2134 |192.168.1.12| | Bob | 555-4527 | 192.168.1.9| """ dat = ascii.read( table, Reader=ascii.FixedWidth, guess=False, header_start=None, data_start=0 ) assert_equal(tuple(dat.dtype.names), ("col1", "col2", "col3")) assert_equal(dat[1][0], "Mary") assert_equal(dat[0][1], "555-1234") assert_equal(dat[2][2], "192.168.1.9") def test_read_no_header_names(): """Table with no header row and with col names provided. Second and third rows also have hanging spaces after final |.""" table = """ | John | 555-1234 |192.168.1.10| | Mary | 555-2134 |192.168.1.12| | Bob | 555-4527 | 192.168.1.9| """ dat = ascii.read( table, Reader=ascii.FixedWidth, guess=False, header_start=None, data_start=0, names=("Name", "Phone", "TCP"), ) assert_equal(tuple(dat.dtype.names), ("Name", "Phone", "TCP")) assert_equal(dat[1][0], "Mary") assert_equal(dat[0][1], "555-1234") assert_equal(dat[2][2], "192.168.1.9") def test_read_no_header_autocolumn_NoHeader(): """Table with no header row and auto-column naming""" table = """ | John | 555-1234 |192.168.1.10| | Mary | 555-2134 |192.168.1.12| | Bob | 555-4527 | 192.168.1.9| """ dat = ascii.read(table, Reader=ascii.FixedWidthNoHeader) assert_equal(tuple(dat.dtype.names), ("col1", "col2", "col3")) assert_equal(dat[1][0], "Mary") assert_equal(dat[0][1], "555-1234") assert_equal(dat[2][2], "192.168.1.9") def test_read_no_header_names_NoHeader(): """Table with no header row and with col names provided. Second and third rows also have hanging spaces after final |.""" table = """ | John | 555-1234 |192.168.1.10| | Mary | 555-2134 |192.168.1.12| | Bob | 555-4527 | 192.168.1.9| """ dat = ascii.read( table, Reader=ascii.FixedWidthNoHeader, names=("Name", "Phone", "TCP") ) assert_equal(tuple(dat.dtype.names), ("Name", "Phone", "TCP")) assert_equal(dat[1][0], "Mary") assert_equal(dat[0][1], "555-1234") assert_equal(dat[2][2], "192.168.1.9") def test_read_col_starts(): """Table with no delimiter with column start and end values specified.""" table = """ # 5 9 17 18 28 # | | || | John 555- 1234 192.168.1.10 Mary 555- 2134 192.168.1.12 Bob 555- 4527 192.168.1.9 """ dat = ascii.read( table, Reader=ascii.FixedWidthNoHeader, names=("Name", "Phone", "TCP"), col_starts=(0, 9, 18), col_ends=(5, 17, 28), ) assert_equal(tuple(dat.dtype.names), ("Name", "Phone", "TCP")) assert_equal(dat[0][1], "555- 1234") assert_equal(dat[1][0], "Mary") assert_equal(dat[1][2], "192.168.1.") assert_equal(dat[2][2], "192.168.1") # col_end=28 cuts this column off def test_read_detect_col_starts_or_ends(): """Table with no delimiter with only column start or end values specified""" table = """ #1 9 19 <== Column start indexes #| | | <== Column start positions #<------><--------><-------------> <== Inferred column positions John 555- 1234 192.168.1.10 Mary 555- 2134 192.168.1.123 Bob 555- 4527 192.168.1.9 Bill 555-9875 192.255.255.255 """ for kwargs in ({"col_starts": (1, 9, 19)}, {"col_ends": (8, 18, 33)}): dat = ascii.read( table, Reader=ascii.FixedWidthNoHeader, names=("Name", "Phone", "TCP"), **kwargs ) assert_equal(tuple(dat.dtype.names), ("Name", "Phone", "TCP")) assert_equal(dat[0][1], "555- 1234") assert_equal(dat[1][0], "Mary") assert_equal(dat[1][2], "192.168.1.123") assert_equal(dat[3][2], "192.255.255.255") table = """\ | Col1 | Col2 | Col3 | Col4 | | 1.2 | "hello" | 1 | a | | 2.4 | 's worlds | 2 | 2 | """ dat = ascii.read(table, Reader=ascii.FixedWidth) def test_write_normal(): """Write a table as a normal fixed width table.""" out = StringIO() ascii.write(dat, out, Writer=ascii.FixedWidth) assert_equal_splitlines( out.getvalue(), """\ | Col1 | Col2 | Col3 | Col4 | | 1.2 | "hello" | 1 | a | | 2.4 | 's worlds | 2 | 2 | """, ) def test_write_fill_values(): """Write a table as a normal fixed width table.""" out = StringIO() ascii.write(dat, out, Writer=ascii.FixedWidth, fill_values=("a", "N/A")) assert_equal_splitlines( out.getvalue(), """\ | Col1 | Col2 | Col3 | Col4 | | 1.2 | "hello" | 1 | N/A | | 2.4 | 's worlds | 2 | 2 | """, ) def test_write_no_pad(): """Write a table as a fixed width table with no padding.""" out = StringIO() ascii.write(dat, out, Writer=ascii.FixedWidth, delimiter_pad=None) assert_equal_splitlines( out.getvalue(), """\ |Col1| Col2|Col3|Col4| | 1.2| "hello"| 1| a| | 2.4|'s worlds| 2| 2| """, ) def test_write_no_bookend(): """Write a table as a fixed width table with no bookend.""" out = StringIO() ascii.write(dat, out, Writer=ascii.FixedWidth, bookend=False) assert_equal_splitlines( out.getvalue(), """\ Col1 | Col2 | Col3 | Col4 1.2 | "hello" | 1 | a 2.4 | 's worlds | 2 | 2 """, ) def test_write_no_delimiter(): """Write a table as a fixed width table with no delimiter.""" out = StringIO() ascii.write(dat, out, Writer=ascii.FixedWidth, bookend=False, delimiter=None) assert_equal_splitlines( out.getvalue(), """\ Col1 Col2 Col3 Col4 1.2 "hello" 1 a 2.4 's worlds 2 2 """, ) def test_write_noheader_normal(): """Write a table as a normal fixed width table.""" out = StringIO() ascii.write(dat, out, Writer=ascii.FixedWidthNoHeader) assert_equal_splitlines( out.getvalue(), """\ | 1.2 | "hello" | 1 | a | | 2.4 | 's worlds | 2 | 2 | """, ) def test_write_noheader_no_pad(): """Write a table as a fixed width table with no padding.""" out = StringIO() ascii.write(dat, out, Writer=ascii.FixedWidthNoHeader, delimiter_pad=None) assert_equal_splitlines( out.getvalue(), """\ |1.2| "hello"|1|a| |2.4|'s worlds|2|2| """, ) def test_write_noheader_no_bookend(): """Write a table as a fixed width table with no bookend.""" out = StringIO() ascii.write(dat, out, Writer=ascii.FixedWidthNoHeader, bookend=False) assert_equal_splitlines( out.getvalue(), """\ 1.2 | "hello" | 1 | a 2.4 | 's worlds | 2 | 2 """, ) def test_write_noheader_no_delimiter(): """Write a table as a fixed width table with no delimiter.""" out = StringIO() ascii.write( dat, out, Writer=ascii.FixedWidthNoHeader, bookend=False, delimiter=None ) assert_equal_splitlines( out.getvalue(), """\ 1.2 "hello" 1 a 2.4 's worlds 2 2 """, ) def test_write_formats(): """Write a table as a fixed width table with no delimiter.""" out = StringIO() ascii.write( dat, out, Writer=ascii.FixedWidth, formats={"Col1": "%-8.3f", "Col2": "%-15s"} ) assert_equal_splitlines( out.getvalue(), """\ | Col1 | Col2 | Col3 | Col4 | | 1.200 | "hello" | 1 | a | | 2.400 | 's worlds | 2 | 2 | """, ) def test_read_twoline_normal(): """Typical fixed format table with two header lines (with some cruft thrown in to test column positioning""" table = """ Col1 Col2 ---- --------- 1.2xx"hello" 2.4 's worlds """ dat = ascii.read(table, Reader=ascii.FixedWidthTwoLine) assert_equal(dat.dtype.names, ("Col1", "Col2")) assert_almost_equal(dat[1][0], 2.4) assert_equal(dat[0][1], '"hello"') assert_equal(dat[1][1], "'s worlds") def test_read_twoline_ReST(): """Read restructured text table""" table = """ ======= =========== Col1 Col2 ======= =========== 1.2 "hello" 2.4 's worlds ======= =========== """ dat = ascii.read( table, Reader=ascii.FixedWidthTwoLine, header_start=1, position_line=2, data_end=-1, ) assert_equal(dat.dtype.names, ("Col1", "Col2")) assert_almost_equal(dat[1][0], 2.4) assert_equal(dat[0][1], '"hello"') assert_equal(dat[1][1], "'s worlds") def test_read_twoline_human(): """Read text table designed for humans and test having position line before the header line""" table = """ +------+----------+ | Col1 | Col2 | +------|----------+ | 1.2 | "hello" | | 2.4 | 's worlds| +------+----------+ """ dat = ascii.read( table, Reader=ascii.FixedWidthTwoLine, delimiter="+", header_start=1, position_line=0, data_start=3, data_end=-1, ) assert_equal(dat.dtype.names, ("Col1", "Col2")) assert_almost_equal(dat[1][0], 2.4) assert_equal(dat[0][1], '"hello"') assert_equal(dat[1][1], "'s worlds") def test_read_twoline_fail(): """Test failure if too many different character are on position line. The position line shall consist of only one character in addition to the delimiter. """ table = """ | Col1 | Col2 | |------|==========| | 1.2 | "hello" | | 2.4 | 's worlds| """ with pytest.raises(InconsistentTableError) as excinfo: ascii.read(table, Reader=ascii.FixedWidthTwoLine, delimiter="|", guess=False) assert ( "Position line should only contain delimiters and one other character" in str(excinfo.value) ) def test_read_twoline_wrong_marker(): """Test failure when position line uses characters prone to ambiguity Characters in position line must be part an allowed set because normal letters or numbers will lead to ambiguous tables. """ table = """ | Col1 | Col2 | |aaaaaa|aaaaaaaaaa| | 1.2 | "hello" | | 2.4 | 's worlds| """ with pytest.raises(InconsistentTableError) as excinfo: ascii.read(table, Reader=ascii.FixedWidthTwoLine, delimiter="|", guess=False) assert "Characters in position line must be part" in str(excinfo.value) def test_write_twoline_normal(): """Write a table as a normal fixed width table.""" out = StringIO() ascii.write(dat, out, Writer=ascii.FixedWidthTwoLine) assert_equal_splitlines( out.getvalue(), """\ Col1 Col2 Col3 Col4 ---- --------- ---- ---- 1.2 "hello" 1 a 2.4 's worlds 2 2 """, ) def test_write_twoline_no_pad(): """Write a table as a fixed width table with no padding.""" out = StringIO() ascii.write( dat, out, Writer=ascii.FixedWidthTwoLine, delimiter_pad=" ", position_char="=" ) assert_equal_splitlines( out.getvalue(), """\ Col1 Col2 Col3 Col4 ==== ========= ==== ==== 1.2 "hello" 1 a 2.4 's worlds 2 2 """, ) def test_write_twoline_no_bookend(): """Write a table as a fixed width table with no bookend.""" out = StringIO() ascii.write(dat, out, Writer=ascii.FixedWidthTwoLine, bookend=True, delimiter="|") assert_equal_splitlines( out.getvalue(), """\ |Col1| Col2|Col3|Col4| |----|---------|----|----| | 1.2| "hello"| 1| a| | 2.4|'s worlds| 2| 2| """, ) def test_fixedwidthnoheader_splitting(): """Test fix in #8511 where data_start is being ignored""" tbl = """\ AAA y z 1 2 3 4 5 6 7 8 9 """ names = ["a", "b", "c"] dat = ascii.read( tbl, data_start=1, data_end=3, delimiter=" ", names=names, format="fixed_width_no_header", ) assert dat.colnames == names assert np.all(dat["a"] == [1, 4]) assert np.all(dat["b"] == [2, 5]) assert np.all(dat["c"] == [3, 6]) def test_fixed_width_header_rows(): tbl = [ "| int16 | float32 | <U3 | int64 |", "| a | b | c | d |", "| m | | | m / s |", "| | .2f | | |", "| | | C column | |", "| 1 | 1.00 | c | 4 |", "| 2 | 2.00 | d | 5 |", "| 3 | 3.00 | e | 6 |", ] header_rows = ["dtype", "name", "unit", "format", "description"] dat = ascii.read(tbl, format="fixed_width", delimiter="|", header_rows=header_rows) out = StringIO() ascii.write(dat, out, format="fixed_width", delimiter="|", header_rows=header_rows) assert out.getvalue().splitlines() == tbl def test_fixed_width_two_line_header_rows(): tbl = [ "int32 float32 <U2 int64", " m m / s", " .2f ", " C column ", " a b c d", "----- ------- -------- -----", " 1 1.00 c 4", " 2 2.00 d 5", " 3 3.00 e 6", ] header_rows = ["dtype", "unit", "format", "description", "name"] dat = ascii.read(tbl, format="fixed_width_two_line", header_rows=header_rows) out = StringIO() ascii.write(dat, out, format="fixed_width_two_line", header_rows=header_rows) assert out.getvalue().splitlines() == tbl def test_fixed_width_no_header_header_rows(): tbl = [" 1 1.00 c 4"] with pytest.raises(TypeError, match=r"unexpected keyword argument 'header_rows'"): ascii.read(tbl, format="fixed_width_no_header", header_rows=["unit"])
cd324804f53717d6fc803ae206b7c08658cb4344fe0b6456493ad01f3c23d08f
# Licensed under a 3-clause BSD style license - see LICENSE.rst from io import StringIO import pytest from astropy.io import ascii from astropy.io.ascii.core import masked from astropy.io.ascii.ipac import Ipac, IpacFormatError, IpacFormatErrorDBMS from astropy.io.ascii.ui import read from astropy.table import Column, Table DATA = """ | a | b | | char | char | ABBBBBBABBBBBBBA """ def test_ipac_default(): # default should be ignore table = read(DATA, Reader=Ipac) assert table["a"][0] == "BBBBBB" assert table["b"][0] == "BBBBBBB" def test_ipac_ignore(): table = read(DATA, Reader=Ipac, definition="ignore") assert table["a"][0] == "BBBBBB" assert table["b"][0] == "BBBBBBB" def test_ipac_left(): table = read(DATA, Reader=Ipac, definition="left") assert table["a"][0] == "BBBBBBA" assert table["b"][0] == "BBBBBBBA" def test_ipac_right(): table = read(DATA, Reader=Ipac, definition="right") assert table["a"][0] == "ABBBBBB" assert table["b"][0] == "ABBBBBBB" def test_too_long_colname_default(): table = Table([[3]], names=["a1234567890123456789012345678901234567890"]) out = StringIO() with pytest.raises(IpacFormatError): ascii.write(table, out, Writer=Ipac) def test_too_long_colname_strict(): table = Table([[3]], names=["a1234567890123456"]) out = StringIO() with pytest.raises(IpacFormatErrorDBMS): ascii.write(table, out, Writer=Ipac, DBMS=True) def test_too_long_colname_notstrict(): table = Table([[3]], names=["a1234567890123456789012345678901234567890"]) out = StringIO() with pytest.raises(IpacFormatError): ascii.write(table, out, Writer=Ipac, DBMS=False) @pytest.mark.parametrize( ("strict_", "Err"), [(True, IpacFormatErrorDBMS), (False, IpacFormatError)] ) def test_non_alfnum_colname(strict_, Err): table = Table([[3]], names=["a123456789 01234"]) out = StringIO() with pytest.raises(Err): ascii.write(table, out, Writer=Ipac, DBMS=strict_) def test_colname_starswithnumber_strict(): table = Table([[3]], names=["a123456789 01234"]) out = StringIO() with pytest.raises(IpacFormatErrorDBMS): ascii.write(table, out, Writer=Ipac, DBMS=True) def test_double_colname_strict(): table = Table([[3], [1]], names=["DEC", "dec"]) out = StringIO() with pytest.raises(IpacFormatErrorDBMS): ascii.write(table, out, Writer=Ipac, DBMS=True) @pytest.mark.parametrize("colname", ["x", "y", "z", "X", "Y", "Z"]) def test_reserved_colname_strict(colname): table = Table([["reg"]], names=[colname]) out = StringIO() with pytest.raises(IpacFormatErrorDBMS): ascii.write(table, out, Writer=Ipac, DBMS=True) def test_too_long_comment(): with pytest.warns( UserWarning, match=r"Comment string > 78 characters was " r"automatically wrapped\.", ): table = Table([[3]]) table.meta["comments"] = ["a" * 79] out = StringIO() ascii.write(table, out, Writer=Ipac) expected_out = """\ \\ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \\ a |col0| |long| | | |null| 3 """ assert out.getvalue().strip().splitlines() == expected_out.splitlines() def test_out_with_nonstring_null(): """Test a (non-string) fill value. Even for an unmasked tables, the fill_value should show up in the table header. """ table = Table([[3]], masked=True) out = StringIO() ascii.write(table, out, Writer=Ipac, fill_values=[(masked, -99999)]) expected_out = """\ | col0| | long| | | |-99999| 3 """ assert out.getvalue().strip().splitlines() == expected_out.splitlines() def test_include_exclude_names(): table = Table([[1], [2], [3]], names=("A", "B", "C")) out = StringIO() ascii.write(table, out, Writer=Ipac, include_names=("A", "B"), exclude_names=("A",)) # column B should be the only included column in output expected_out = """\ | B| |long| | | |null| 2 """ assert out.getvalue().strip().splitlines() == expected_out.splitlines() def test_short_dtypes(): table = Table( [Column([1.0], dtype="f4"), Column([2], dtype="i2")], names=("float_col", "int_col"), ) out = StringIO() ascii.write(table, out, Writer=Ipac) expected_out = """\ |float_col|int_col| | float| int| | | | | null| null| 1.0 2 """ assert out.getvalue().strip().splitlines() == expected_out.splitlines()
9d587df1296fb665d2a45428a3c41dd22fb582757dfa2cbbedf9a01663dd76c1
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ This module tests some of the methods related to the ``HTML`` reader/writer and aims to document its functionality. Requires `BeautifulSoup <http://www.crummy.com/software/BeautifulSoup/>`_ to be installed. """ from io import StringIO import numpy as np import pytest from astropy.io import ascii from astropy.io.ascii import core, html from astropy.table import Table from astropy.utils.compat.optional_deps import HAS_BLEACH, HAS_BS4 from .common import setup_function, teardown_function # noqa: F401 if HAS_BS4: from bs4 import BeautifulSoup, FeatureNotFound @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_soupstring(): """ Test to make sure the class SoupString behaves properly. """ soup = BeautifulSoup( "<html><head></head><body><p>foo</p></body></html>", "html.parser" ) soup_str = html.SoupString(soup) assert isinstance(soup_str, str) assert isinstance(soup_str, html.SoupString) assert soup_str == "<html><head></head><body><p>foo</p></body></html>" assert soup_str.soup is soup def test_listwriter(): """ Test to make sure the class ListWriter behaves properly. """ lst = [] writer = html.ListWriter(lst) for i in range(5): writer.write(i) for ch in "abcde": writer.write(ch) assert lst == [0, 1, 2, 3, 4, "a", "b", "c", "d", "e"] @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_identify_table(): """ Test to make sure that identify_table() returns whether the given BeautifulSoup tag is the correct table to process. """ # Should return False on non-<table> tags and None soup = BeautifulSoup("<html><body></body></html>", "html.parser") assert html.identify_table(soup, {}, 0) is False assert html.identify_table(None, {}, 0) is False soup = BeautifulSoup( '<table id="foo"><tr><th>A</th></tr><tr><td>B</td></tr></table>', "html.parser", ).table assert html.identify_table(soup, {}, 2) is False assert html.identify_table(soup, {}, 1) is True # Default index of 1 # Same tests, but with explicit parameter assert html.identify_table(soup, {"table_id": 2}, 1) is False assert html.identify_table(soup, {"table_id": 1}, 1) is True # Test identification by string ID assert html.identify_table(soup, {"table_id": "bar"}, 1) is False assert html.identify_table(soup, {"table_id": "foo"}, 1) is True @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_missing_data(): """ Test reading a table with missing data """ # First with default where blank => '0' table_in = [ "<table>", "<tr><th>A</th></tr>", "<tr><td></td></tr>", "<tr><td>1</td></tr>", "</table>", ] dat = Table.read(table_in, format="ascii.html") assert dat.masked is False assert np.all(dat["A"].mask == [True, False]) assert dat["A"].dtype.kind == "i" # Now with a specific value '...' => missing table_in = [ "<table>", "<tr><th>A</th></tr>", "<tr><td>...</td></tr>", "<tr><td>1</td></tr>", "</table>", ] dat = Table.read(table_in, format="ascii.html", fill_values=[("...", "0")]) assert dat.masked is False assert np.all(dat["A"].mask == [True, False]) assert dat["A"].dtype.kind == "i" @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_rename_cols(): """ Test reading a table and renaming cols """ table_in = [ "<table>", "<tr><th>A</th> <th>B</th></tr>", "<tr><td>1</td><td>2</td></tr>", "</table>", ] # Swap column names dat = Table.read(table_in, format="ascii.html", names=["B", "A"]) assert dat.colnames == ["B", "A"] assert len(dat) == 1 # Swap column names and only include A (the renamed version) dat = Table.read( table_in, format="ascii.html", names=["B", "A"], include_names=["A"] ) assert dat.colnames == ["A"] assert len(dat) == 1 assert np.all(dat["A"] == 2) @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_no_names(): """ Test reading a table with no column header """ table_in = ["<table>", "<tr><td>1</td></tr>", "<tr><td>2</td></tr>", "</table>"] dat = Table.read(table_in, format="ascii.html") assert dat.colnames == ["col1"] assert len(dat) == 2 dat = Table.read(table_in, format="ascii.html", names=["a"]) assert dat.colnames == ["a"] assert len(dat) == 2 @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_identify_table_fail(): """ Raise an exception with an informative error message if table_id is not found. """ table_in = ['<table id="foo"><tr><th>A</th></tr>', "<tr><td>B</td></tr></table>"] with pytest.raises(core.InconsistentTableError) as err: Table.read( table_in, format="ascii.html", htmldict={"table_id": "bad_id"}, guess=False ) assert err.match("ERROR: HTML table id 'bad_id' not found$") with pytest.raises(core.InconsistentTableError) as err: Table.read(table_in, format="ascii.html", htmldict={"table_id": 3}, guess=False) assert err.match("ERROR: HTML table number 3 not found$") @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_backend_parsers(): """ Make sure the user can specify which back-end parser to use and that an error is raised if the parser is invalid. """ for parser in ("lxml", "xml", "html.parser", "html5lib"): try: Table.read( "data/html2.html", format="ascii.html", htmldict={"parser": parser}, guess=False, ) except FeatureNotFound: if parser == "html.parser": raise # otherwise ignore if the dependency isn't present # reading should fail if the parser is invalid with pytest.raises(FeatureNotFound): Table.read( "data/html2.html", format="ascii.html", htmldict={"parser": "foo"}, guess=False, ) @pytest.mark.skipif(HAS_BS4, reason="requires no BeautifulSoup4") def test_htmlinputter_no_bs4(): """ This should return an OptionalTableImportError if BeautifulSoup is not installed. """ inputter = html.HTMLInputter() with pytest.raises(core.OptionalTableImportError): inputter.process_lines([]) @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_htmlinputter(): """ Test to ensure that HTMLInputter correctly converts input into a list of SoupStrings representing table elements. """ f = "data/html.html" with open(f) as fd: table = fd.read() inputter = html.HTMLInputter() inputter.html = {} # In absence of table_id, defaults to the first table expected = [ "<tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr>", "<tr><td>1</td><td>a</td><td>1.05</td></tr>", "<tr><td>2</td><td>b</td><td>2.75</td></tr>", "<tr><td>3</td><td>c</td><td>-1.25</td></tr>", ] assert [str(x) for x in inputter.get_lines(table)] == expected # Should raise an InconsistentTableError if the table is not found inputter.html = {"table_id": 4} with pytest.raises(core.InconsistentTableError): inputter.get_lines(table) # Identification by string ID inputter.html["table_id"] = "second" expected = [ "<tr><th>Column A</th><th>Column B</th><th>Column C</th></tr>", "<tr><td>4</td><td>d</td><td>10.5</td></tr>", "<tr><td>5</td><td>e</td><td>27.5</td></tr>", "<tr><td>6</td><td>f</td><td>-12.5</td></tr>", ] assert [str(x) for x in inputter.get_lines(table)] == expected # Identification by integer index inputter.html["table_id"] = 3 expected = [ "<tr><th>C1</th><th>C2</th><th>C3</th></tr>", "<tr><td>7</td><td>g</td><td>105.0</td></tr>", "<tr><td>8</td><td>h</td><td>275.0</td></tr>", "<tr><td>9</td><td>i</td><td>-125.0</td></tr>", ] assert [str(x) for x in inputter.get_lines(table)] == expected @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_htmlsplitter(): """ Test to make sure that HTMLSplitter correctly inputs lines of type SoupString to return a generator that gives all header and data elements. """ splitter = html.HTMLSplitter() lines = [ html.SoupString( BeautifulSoup( "<table><tr><th>Col 1</th><th>Col 2</th></tr></table>", "html.parser" ).tr ), html.SoupString( BeautifulSoup( "<table><tr><td>Data 1</td><td>Data 2</td></tr></table>", "html.parser" ).tr ), ] expected_data = [["Col 1", "Col 2"], ["Data 1", "Data 2"]] assert list(splitter(lines)) == expected_data # Make sure the presence of a non-SoupString triggers a TypeError lines.append("<tr><td>Data 3</td><td>Data 4</td></tr>") with pytest.raises(TypeError): list(splitter(lines)) # Make sure that passing an empty list triggers an error with pytest.raises(core.InconsistentTableError): list(splitter([])) @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_htmlheader_start(): """ Test to ensure that the start_line method of HTMLHeader returns the first line of header data. Uses t/html.html for sample input. """ f = "data/html.html" with open(f) as fd: table = fd.read() inputter = html.HTMLInputter() inputter.html = {} header = html.HTMLHeader() lines = inputter.get_lines(table) assert ( str(lines[header.start_line(lines)]) == "<tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr>" ) inputter.html["table_id"] = "second" lines = inputter.get_lines(table) assert ( str(lines[header.start_line(lines)]) == "<tr><th>Column A</th><th>Column B</th><th>Column C</th></tr>" ) inputter.html["table_id"] = 3 lines = inputter.get_lines(table) assert ( str(lines[header.start_line(lines)]) == "<tr><th>C1</th><th>C2</th><th>C3</th></tr>" ) # start_line should return None if no valid header is found lines = [ html.SoupString( BeautifulSoup("<table><tr><td>Data</td></tr></table>", "html.parser").tr ), html.SoupString(BeautifulSoup("<p>Text</p>", "html.parser").p), ] assert header.start_line(lines) is None # Should raise an error if a non-SoupString is present lines.append("<tr><th>Header</th></tr>") with pytest.raises(TypeError): header.start_line(lines) @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_htmldata(): """ Test to ensure that the start_line and end_lines methods of HTMLData returns the first line of table data. Uses t/html.html for sample input. """ f = "data/html.html" with open(f) as fd: table = fd.read() inputter = html.HTMLInputter() inputter.html = {} data = html.HTMLData() lines = inputter.get_lines(table) assert ( str(lines[data.start_line(lines)]) == "<tr><td>1</td><td>a</td><td>1.05</td></tr>" ) # end_line returns the index of the last data element + 1 assert ( str(lines[data.end_line(lines) - 1]) == "<tr><td>3</td><td>c</td><td>-1.25</td></tr>" ) inputter.html["table_id"] = "second" lines = inputter.get_lines(table) assert ( str(lines[data.start_line(lines)]) == "<tr><td>4</td><td>d</td><td>10.5</td></tr>" ) assert ( str(lines[data.end_line(lines) - 1]) == "<tr><td>6</td><td>f</td><td>-12.5</td></tr>" ) inputter.html["table_id"] = 3 lines = inputter.get_lines(table) assert ( str(lines[data.start_line(lines)]) == "<tr><td>7</td><td>g</td><td>105.0</td></tr>" ) assert ( str(lines[data.end_line(lines) - 1]) == "<tr><td>9</td><td>i</td><td>-125.0</td></tr>" ) # start_line should raise an error if no table data exists lines = [ html.SoupString(BeautifulSoup("<div></div>", "html.parser").div), html.SoupString(BeautifulSoup("<p>Text</p>", "html.parser").p), ] with pytest.raises(core.InconsistentTableError): data.start_line(lines) # end_line should return None if no table data exists assert data.end_line(lines) is None # Should raise an error if a non-SoupString is present lines.append("<tr><td>Data</td></tr>") with pytest.raises(TypeError): data.start_line(lines) with pytest.raises(TypeError): data.end_line(lines) def test_multicolumn_write(): """ Test to make sure that the HTML writer writes multidimensional columns (those with iterable elements) using the colspan attribute of <th>. """ col1 = [1, 2, 3] col2 = [(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)] col3 = [("a", "a", "a"), ("b", "b", "b"), ("c", "c", "c")] table = Table([col1, col2, col3], names=("C1", "C2", "C3")) expected = """\ <html> <head> <meta charset="utf-8"/> <meta content="text/html;charset=UTF-8" http-equiv="Content-type"/> </head> <body> <table> <thead> <tr> <th>C1</th> <th colspan="2">C2</th> <th colspan="3">C3</th> </tr> </thead> <tr> <td>1</td> <td>1.0</td> <td>1.0</td> <td>a</td> <td>a</td> <td>a</td> </tr> <tr> <td>2</td> <td>2.0</td> <td>2.0</td> <td>b</td> <td>b</td> <td>b</td> </tr> <tr> <td>3</td> <td>3.0</td> <td>3.0</td> <td>c</td> <td>c</td> <td>c</td> </tr> </table> </body> </html> """ out = html.HTML().write(table)[0].strip() assert out == expected.strip() @pytest.mark.skipif(not HAS_BLEACH, reason="requires bleach") def test_multicolumn_write_escape(): """ Test to make sure that the HTML writer writes multidimensional columns (those with iterable elements) using the colspan attribute of <th>. """ col1 = [1, 2, 3] col2 = [(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)] col3 = [("<a></a>", "<a></a>", "a"), ("<b></b>", "b", "b"), ("c", "c", "c")] table = Table([col1, col2, col3], names=("C1", "C2", "C3")) expected = """\ <html> <head> <meta charset="utf-8"/> <meta content="text/html;charset=UTF-8" http-equiv="Content-type"/> </head> <body> <table> <thead> <tr> <th>C1</th> <th colspan="2">C2</th> <th colspan="3">C3</th> </tr> </thead> <tr> <td>1</td> <td>1.0</td> <td>1.0</td> <td><a></a></td> <td><a></a></td> <td>a</td> </tr> <tr> <td>2</td> <td>2.0</td> <td>2.0</td> <td><b></b></td> <td>b</td> <td>b</td> </tr> <tr> <td>3</td> <td>3.0</td> <td>3.0</td> <td>c</td> <td>c</td> <td>c</td> </tr> </table> </body> </html> """ out = html.HTML(htmldict={"raw_html_cols": "C3"}).write(table)[0].strip() assert out == expected.strip() def test_write_no_multicols(): """ Test to make sure that the HTML writer will not use multi-dimensional columns if the multicol parameter is False. """ col1 = [1, 2, 3] col2 = [(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)] col3 = [("a", "a", "a"), ("b", "b", "b"), ("c", "c", "c")] table = Table([col1, col2, col3], names=("C1", "C2", "C3")) expected = """\ <html> <head> <meta charset="utf-8"/> <meta content="text/html;charset=UTF-8" http-equiv="Content-type"/> </head> <body> <table> <thead> <tr> <th>C1</th> <th>C2</th> <th>C3</th> </tr> </thead> <tr> <td>1</td> <td>1.0 .. 1.0</td> <td>a .. a</td> </tr> <tr> <td>2</td> <td>2.0 .. 2.0</td> <td>b .. b</td> </tr> <tr> <td>3</td> <td>3.0 .. 3.0</td> <td>c .. c</td> </tr> </table> </body> </html> """ assert html.HTML({"multicol": False}).write(table)[0].strip() == expected.strip() @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_multicolumn_read(): """ Test to make sure that the HTML reader inputs multidimensional columns (those with iterable elements) using the colspan attribute of <th>. Ensure that any string element within a multidimensional column casts all elements to string prior to type conversion operations. """ table = Table.read("data/html2.html", format="ascii.html") str_type = np.dtype((str, 21)) expected = Table( np.array( [(["1", "2.5000000000000000001"], 3), (["1a", "1"], 3.5)], dtype=[("A", str_type, (2,)), ("B", "<f8")], ) ) assert np.all(table == expected) @pytest.mark.skipif(not HAS_BLEACH, reason="requires bleach") def test_raw_html_write(): """ Test that columns can contain raw HTML which is not escaped. """ t = Table([["<em>x</em>"], ["<em>y</em>"]], names=["a", "b"]) # One column contains raw HTML (string input) out = StringIO() t.write(out, format="ascii.html", htmldict={"raw_html_cols": "a"}) expected = """\ <tr> <td><em>x</em></td> <td>&lt;em&gt;y&lt;/em&gt;</td> </tr>""" assert expected in out.getvalue() # One column contains raw HTML (list input) out = StringIO() t.write(out, format="ascii.html", htmldict={"raw_html_cols": ["a"]}) assert expected in out.getvalue() # Two columns contains raw HTML (list input) out = StringIO() t.write(out, format="ascii.html", htmldict={"raw_html_cols": ["a", "b"]}) expected = """\ <tr> <td><em>x</em></td> <td><em>y</em></td> </tr>""" assert expected in out.getvalue() @pytest.mark.skipif(not HAS_BLEACH, reason="requires bleach") def test_raw_html_write_clean(): """ Test that columns can contain raw HTML which is not escaped. """ import bleach t = Table( [["<script>x</script>"], ["<p>y</p>"], ["<em>y</em>"]], names=["a", "b", "c"] ) # Confirm that <script> and <p> get escaped but not <em> out = StringIO() t.write(out, format="ascii.html", htmldict={"raw_html_cols": t.colnames}) expected = """\ <tr> <td>&lt;script&gt;x&lt;/script&gt;</td> <td>&lt;p&gt;y&lt;/p&gt;</td> <td><em>y</em></td> </tr>""" assert expected in out.getvalue() # Confirm that we can whitelist <p> out = StringIO() t.write( out, format="ascii.html", htmldict={ "raw_html_cols": t.colnames, "raw_html_clean_kwargs": {"tags": bleach.ALLOWED_TAGS + ["p"]}, }, ) expected = """\ <tr> <td>&lt;script&gt;x&lt;/script&gt;</td> <td><p>y</p></td> <td><em>y</em></td> </tr>""" assert expected in out.getvalue() def test_write_table_html_fill_values(): """ Test that passing fill_values should replace any matching row """ buffer_output = StringIO() t = Table([[1], [2]], names=("a", "b")) ascii.write(t, buffer_output, fill_values=("1", "Hello world"), format="html") t_expected = Table([["Hello world"], [2]], names=("a", "b")) buffer_expected = StringIO() ascii.write(t_expected, buffer_expected, format="html") assert buffer_output.getvalue() == buffer_expected.getvalue() def test_write_table_html_fill_values_optional_columns(): """ Test that passing optional column in fill_values should only replace matching columns """ buffer_output = StringIO() t = Table([[1], [1]], names=("a", "b")) ascii.write(t, buffer_output, fill_values=("1", "Hello world", "b"), format="html") t_expected = Table([[1], ["Hello world"]], names=("a", "b")) buffer_expected = StringIO() ascii.write(t_expected, buffer_expected, format="html") assert buffer_output.getvalue() == buffer_expected.getvalue() def test_write_table_html_fill_values_masked(): """ Test that passing masked values in fill_values should only replace masked columns or values """ buffer_output = StringIO() t = Table([[1], [1]], names=("a", "b"), masked=True, dtype=("i4", "i8")) t["a"] = np.ma.masked ascii.write(t, buffer_output, fill_values=(ascii.masked, "TEST"), format="html") t_expected = Table([["TEST"], [1]], names=("a", "b")) buffer_expected = StringIO() ascii.write(t_expected, buffer_expected, format="html") assert buffer_output.getvalue() == buffer_expected.getvalue() def test_multicolumn_table_html_fill_values(): """ Test to make sure that the HTML writer writes multidimensional columns with correctly replaced fill_values. """ col1 = [1, 2, 3] col2 = [(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)] col3 = [("a", "a", "a"), ("b", "b", "b"), ("c", "c", "c")] buffer_output = StringIO() t = Table([col1, col2, col3], names=("C1", "C2", "C3")) ascii.write(t, buffer_output, fill_values=("a", "z"), format="html") col1 = [1, 2, 3] col2 = [(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)] col3 = [("z", "z", "z"), ("b", "b", "b"), ("c", "c", "c")] buffer_expected = StringIO() t_expected = Table([col1, col2, col3], names=("C1", "C2", "C3")) ascii.write(t_expected, buffer_expected, format="html") assert buffer_output.getvalue() == buffer_expected.getvalue() def test_multi_column_write_table_html_fill_values_masked(): """ Test that passing masked values in fill_values should only replace masked columns or values for multidimensional tables """ buffer_output = StringIO() t = Table([[1, 2, 3, 4], ["--", "a", "--", "b"]], names=("a", "b"), masked=True) t["a"][0:2] = np.ma.masked t["b"][0:2] = np.ma.masked ascii.write(t, buffer_output, fill_values=[(ascii.masked, "MASKED")], format="html") t_expected = Table( [["MASKED", "MASKED", 3, 4], ["MASKED", "MASKED", "--", "b"]], names=("a", "b") ) buffer_expected = StringIO() ascii.write(t_expected, buffer_expected, format="html") print(buffer_expected.getvalue()) assert buffer_output.getvalue() == buffer_expected.getvalue() def test_write_table_formatted_columns(): """ Test to make sure that the HTML writer writes out using the supplied formatting. """ col1 = [1, 2] col2 = [1.234567e-11, -9.876543e11] formats = {"C1": "04d", "C2": ".2e"} table = Table([col1, col2], names=formats.keys()) expected = """\ <html> <head> <meta charset="utf-8"/> <meta content="text/html;charset=UTF-8" http-equiv="Content-type"/> </head> <body> <table> <thead> <tr> <th>C1</th> <th>C2</th> </tr> </thead> <tr> <td>0001</td> <td>1.23e-11</td> </tr> <tr> <td>0002</td> <td>-9.88e+11</td> </tr> </table> </body> </html> """ with StringIO() as sp: table.write(sp, format="html", formats=formats) out = sp.getvalue().strip() assert out == expected.strip() @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_read_html_unicode(): """ Test reading an HTML table with unicode values """ table_in = [ "<table>", "<tr><td>&#x0394;</td></tr>", "<tr><td>Δ</td></tr>", "</table>", ] dat = Table.read(table_in, format="ascii.html") assert np.all(dat["col1"] == ["Δ", "Δ"])
8f54af3bb9e3ab7f7fd3250ce1a7a61c98cf620ddfa272a96e477c7e4ebe4ce0
# Licensed under a 3-clause BSD style license - see LICENSE.rst import os import numpy as np from astropy.utils.decorators import deprecated __all__ = [ "assert_equal", "assert_almost_equal", "assert_true", "setup_function", "teardown_function", "has_isnan", ] CWD = os.getcwd() TEST_DIR = os.path.dirname(__file__) has_isnan = True try: from math import isnan except ImportError: try: from numpy import isnan # noqa: F401 except ImportError: has_isnan = False print("Tests requiring isnan will fail") def setup_function(function): os.chdir(TEST_DIR) def teardown_function(function): os.chdir(CWD) # Compatibility functions to convert from nose to pytest def assert_equal(a, b): assert a == b def assert_almost_equal(a, b, **kwargs): assert np.allclose(a, b, **kwargs) def assert_true(a): assert a def make_decorator(func): """ Wraps a test decorator so as to properly replicate metadata of the decorated function, including nose's additional stuff (namely, setup and teardown). """ def decorate(newfunc): if hasattr(func, "compat_func_name"): name = func.compat_func_name else: name = func.__name__ newfunc.__dict__ = func.__dict__ newfunc.__doc__ = func.__doc__ newfunc.__module__ = func.__module__ if not hasattr(newfunc, "compat_co_firstlineno"): try: newfunc.compat_co_firstlineno = func.func_code.co_firstlineno except AttributeError: newfunc.compat_co_firstlineno = func.__code__.co_firstlineno try: newfunc.__name__ = name except TypeError: # can't set func name in 2.3 newfunc.compat_func_name = name return newfunc return decorate @deprecated("5.1", alternative="pytest.raises") def raises(*exceptions): """Test must raise one of expected exceptions to pass. Example use:: @raises(TypeError, ValueError) def test_raises_type_error(): raise TypeError("This test passes") @raises(Exception) def test_that_fails_by_passing(): pass """ valid = " or ".join([e.__name__ for e in exceptions]) def decorate(func): name = func.__name__ def newfunc(*arg, **kw): try: func(*arg, **kw) except exceptions: pass else: message = f"{name}() did not raise {valid}" raise AssertionError(message) newfunc = make_decorator(func)(newfunc) return newfunc return decorate
78427b0a8b1252146cfb9316ce94851adf02da042c3e013128748446def1831f
# Licensed under a 3-clause BSD style license - see LICENSE.rst import numpy as np import pytest from astropy.io.ascii import read # NOTE: Python can be built without bz2 or lzma from astropy.utils.compat.optional_deps import HAS_BZ2, HAS_LZMA from astropy.utils.data import get_pkg_data_filename @pytest.mark.parametrize( "filename", ["data/daophot.dat.gz", "data/latex1.tex.gz", "data/short.rdb.gz"] ) def test_gzip(filename): t_comp = read(get_pkg_data_filename(filename)) t_uncomp = read(get_pkg_data_filename(filename.replace(".gz", ""))) assert t_comp.dtype.names == t_uncomp.dtype.names assert np.all(t_comp.as_array() == t_uncomp.as_array()) @pytest.mark.xfail(not HAS_BZ2, reason="requires bz2") @pytest.mark.parametrize("filename", ["data/short.rdb.bz2", "data/ipac.dat.bz2"]) def test_bzip2(filename): t_comp = read(get_pkg_data_filename(filename)) t_uncomp = read(get_pkg_data_filename(filename.replace(".bz2", ""))) assert t_comp.dtype.names == t_uncomp.dtype.names assert np.all(t_comp.as_array() == t_uncomp.as_array()) @pytest.mark.xfail(not HAS_LZMA, reason="requires lzma") @pytest.mark.parametrize("filename", ["data/short.rdb.xz", "data/ipac.dat.xz"]) def test_xz(filename): t_comp = read(get_pkg_data_filename(filename)) t_uncomp = read(get_pkg_data_filename(filename.replace(".xz", ""))) assert t_comp.dtype.names == t_uncomp.dtype.names assert np.all(t_comp.as_array() == t_uncomp.as_array())
35c7bc35a52558c9655f18c472d950c63b5d34bb70b353d8d0669cba3b63ee8f
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ This module tests some of the methods related to the ``ECSV`` reader/writer. """ import copy import os import sys from contextlib import nullcontext from io import StringIO import numpy as np import pytest import yaml from astropy import units as u from astropy.io import ascii from astropy.io.ascii.ecsv import DELIMITERS, InvalidEcsvDatatypeWarning from astropy.io.tests.mixin_columns import compare_attrs, mixin_cols, serialized_names from astropy.table import Column, QTable, Table from astropy.table.column import MaskedColumn from astropy.table.table_helpers import simple_table from astropy.units import QuantityInfo from astropy.units import allclose as quantity_allclose from .common import TEST_DIR DTYPES = [ "bool", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "float16", "float32", "float64", "float128", "str", ] if not hasattr(np, "float128") or os.name == "nt" or sys.maxsize <= 2**32: DTYPES.remove("float128") T_DTYPES = Table() for dtype in DTYPES: if dtype == "bool": data = np.array([False, True, False]) elif dtype == "str": data = np.array(["ab 0", "ab, 1", "ab2"]) else: data = np.arange(3, dtype=dtype) c = Column( data, unit="m / s", description="descr_" + dtype, meta={"meta " + dtype: 1} ) T_DTYPES[dtype] = c T_DTYPES.meta["comments"] = ["comment1", "comment2"] # Corresponds to simple_table() SIMPLE_LINES = [ "# %ECSV 1.0", "# ---", "# datatype:", "# - {name: a, datatype: int64}", "# - {name: b, datatype: float64}", "# - {name: c, datatype: string}", "# schema: astropy-2.0", "a b c", "1 1.0 c", "2 2.0 d", "3 3.0 e", ] def test_write_simple(): """ Write a simple table with common types. This shows the compact version of serialization with one line per column. """ t = simple_table() out = StringIO() t.write(out, format="ascii.ecsv") assert out.getvalue().splitlines() == SIMPLE_LINES def test_write_full(): """ Write a full-featured table with common types and explicitly checkout output """ t = T_DTYPES["bool", "int64", "float64", "str"] lines = [ "# %ECSV 1.0", "# ---", "# datatype:", "# - name: bool", "# unit: m / s", "# datatype: bool", "# description: descr_bool", "# meta: {meta bool: 1}", "# - name: int64", "# unit: m / s", "# datatype: int64", "# description: descr_int64", "# meta: {meta int64: 1}", "# - name: float64", "# unit: m / s", "# datatype: float64", "# description: descr_float64", "# meta: {meta float64: 1}", "# - name: str", "# unit: m / s", "# datatype: string", "# description: descr_str", "# meta: {meta str: 1}", "# meta: !!omap", "# - comments: [comment1, comment2]", "# schema: astropy-2.0", "bool int64 float64 str", 'False 0 0.0 "ab 0"', 'True 1 1.0 "ab, 1"', "False 2 2.0 ab2", ] out = StringIO() t.write(out, format="ascii.ecsv") assert out.getvalue().splitlines() == lines def test_write_read_roundtrip(): """ Write a full-featured table with all types and see that it round-trips on readback. Use both space and comma delimiters. """ t = T_DTYPES for delimiter in DELIMITERS: out = StringIO() t.write(out, format="ascii.ecsv", delimiter=delimiter) t2s = [ Table.read(out.getvalue(), format="ascii.ecsv"), Table.read(out.getvalue(), format="ascii"), ascii.read(out.getvalue()), ascii.read(out.getvalue(), format="ecsv", guess=False), ascii.read(out.getvalue(), format="ecsv"), ] for t2 in t2s: assert t.meta == t2.meta for name in t.colnames: assert t[name].attrs_equal(t2[name]) assert np.all(t[name] == t2[name]) def test_bad_delimiter(): """ Passing a delimiter other than space or comma gives an exception """ out = StringIO() with pytest.raises(ValueError) as err: T_DTYPES.write(out, format="ascii.ecsv", delimiter="|") assert "only space and comma are allowed" in str(err.value) def test_bad_header_start(): """ Bad header without initial # %ECSV x.x """ lines = copy.copy(SIMPLE_LINES) lines[0] = "# %ECV 0.9" with pytest.raises(ascii.InconsistentTableError): Table.read("\n".join(lines), format="ascii.ecsv", guess=False) def test_bad_delimiter_input(): """ Illegal delimiter in input """ lines = copy.copy(SIMPLE_LINES) lines.insert(2, "# delimiter: |") with pytest.raises(ValueError) as err: Table.read("\n".join(lines), format="ascii.ecsv", guess=False) assert "only space and comma are allowed" in str(err.value) def test_multidim_input(): """ Multi-dimensional column in input """ t = Table() t["a"] = np.arange(24).reshape(2, 3, 4) t["a"].info.description = "description" t["a"].info.meta = {1: 2} t["b"] = [1, 2] out = StringIO() t.write(out, format="ascii.ecsv") t2 = Table.read(out.getvalue(), format="ascii.ecsv") assert np.all(t2["a"] == t["a"]) assert t2["a"].shape == t["a"].shape assert t2["a"].dtype == t["a"].dtype assert t2["a"].info.description == t["a"].info.description assert t2["a"].info.meta == t["a"].info.meta assert np.all(t2["b"] == t["b"]) def test_structured_input(): """ Structured column in input. """ t = Table() # Add unit, description and meta to make sure that round-trips as well. t["a"] = Column( [("B", (1.0, [2.0, 3.0])), ("A", (9.0, [8.0, 7.0]))], dtype=[("s", "U1"), ("v", [("p0", "f8"), ("p1", "2f8")])], description="description", format=">", # Most formats do not work with structured! unit="m", # Overall unit should round-trip. meta={1: 2}, ) t["b"] = Column( [[(1.0, 2.0), (9.0, 8.0)], [(3.0, 4.0), (7.0, 6.0)]], dtype="f8,f8", unit=u.Unit("m,s"), # Per part unit should round-trip too. ) out = StringIO() t.write(out, format="ascii.ecsv") t2 = Table.read(out.getvalue(), format="ascii.ecsv") for col in t.colnames: assert np.all(t2[col] == t[col]) assert t2[col].shape == t[col].shape assert t2[col].dtype == t[col].dtype assert t2[col].unit == t[col].unit assert t2[col].format == t[col].format assert t2[col].info.description == t[col].info.description assert t2[col].info.meta == t[col].info.meta def test_round_trip_empty_table(): """Test fix in #5010 for issue #5009 (ECSV fails for empty type with bool type)""" t = Table(dtype=[bool, "i", "f"], names=["a", "b", "c"]) out = StringIO() t.write(out, format="ascii.ecsv") t2 = Table.read(out.getvalue(), format="ascii.ecsv") assert t.dtype == t2.dtype assert len(t2) == 0 def test_csv_ecsv_colnames_mismatch(): """ Test that mismatch in column names from normal CSV header vs. ECSV YAML header raises the expected exception. """ lines = copy.copy(SIMPLE_LINES) header_index = lines.index("a b c") lines[header_index] = "a b d" with pytest.raises(ValueError) as err: ascii.read(lines, format="ecsv") assert "column names from ECSV header ['a', 'b', 'c']" in str(err.value) def test_regression_5604(): """ See https://github.com/astropy/astropy/issues/5604 for more. """ t = Table() t.meta = {"foo": 5 * u.km, "foo2": u.s} t["bar"] = [7] * u.km out = StringIO() t.write(out, format="ascii.ecsv") assert "!astropy.units.Unit" in out.getvalue() assert "!astropy.units.Quantity" in out.getvalue() def assert_objects_equal(obj1, obj2, attrs, compare_class=True): if compare_class: assert obj1.__class__ is obj2.__class__ assert obj1.shape == obj2.shape info_attrs = [ "info.name", "info.format", "info.unit", "info.description", "info.dtype", ] for attr in attrs + info_attrs: a1 = obj1 a2 = obj2 for subattr in attr.split("."): try: a1 = getattr(a1, subattr) a2 = getattr(a2, subattr) except AttributeError: a1 = a1[subattr] a2 = a2[subattr] if isinstance(a1, np.ndarray) and a1.dtype.kind == "f": assert quantity_allclose(a1, a2, rtol=1e-10) else: assert np.all(a1 == a2) # For no attrs that means we just compare directly. if not attrs: if isinstance(obj1, np.ndarray) and obj1.dtype.kind == "f": assert quantity_allclose(obj1, obj2, rtol=1e-15) else: assert np.all(obj1 == obj2) def test_ecsv_mixins_ascii_read_class(): """Ensure that ascii.read(ecsv_file) returns the correct class (QTable if any Quantity subclasses, Table otherwise). """ # Make a table with every mixin type except Quantities t = QTable( { name: col for name, col in mixin_cols.items() if not isinstance(col.info, QuantityInfo) } ) out = StringIO() t.write(out, format="ascii.ecsv") t2 = ascii.read(out.getvalue(), format="ecsv") assert type(t2) is Table # Add a single quantity column t["lon"] = mixin_cols["lon"] out = StringIO() t.write(out, format="ascii.ecsv") t2 = ascii.read(out.getvalue(), format="ecsv") assert type(t2) is QTable def test_ecsv_mixins_qtable_to_table(): """Test writing as QTable and reading as Table. Ensure correct classes come out. """ names = sorted(mixin_cols) t = QTable([mixin_cols[name] for name in names], names=names) out = StringIO() t.write(out, format="ascii.ecsv") t2 = Table.read(out.getvalue(), format="ascii.ecsv") assert t.colnames == t2.colnames for name, col in t.columns.items(): col2 = t2[name] attrs = compare_attrs[name] compare_class = True if isinstance(col.info, QuantityInfo): # Downgrade Quantity to Column + unit assert type(col2) is Column # Class-specific attributes like `value` or `wrap_angle` are lost. attrs = ["unit"] compare_class = False # Compare data values here (assert_objects_equal doesn't know how in this case) assert np.allclose(col.value, col2, rtol=1e-10) assert_objects_equal(col, col2, attrs, compare_class) @pytest.mark.parametrize("table_cls", (Table, QTable)) def test_ecsv_mixins_as_one(table_cls): """Test write/read all cols at once and validate intermediate column names""" names = sorted(mixin_cols) all_serialized_names = [] # ECSV stores times as value by default, so we just get the column back. # One exception is tm3, which is set to serialize via jd1 and jd2. for name in names: s_names = serialized_names[name] if not name.startswith("tm3"): s_names = [ s_name.replace(".jd1", "") for s_name in s_names if not s_name.endswith("jd2") ] all_serialized_names.extend(s_names) t = table_cls([mixin_cols[name] for name in names], names=names) out = StringIO() t.write(out, format="ascii.ecsv") t2 = table_cls.read(out.getvalue(), format="ascii.ecsv") assert t.colnames == t2.colnames # Read as a ascii.basic table (skip all the ECSV junk) t3 = table_cls.read(out.getvalue(), format="ascii.basic") assert t3.colnames == all_serialized_names def make_multidim(col, ndim): """Take a col with length=2 and make it N-d by repeating elements. For the special case of ndim==1 just return the original. The output has shape [3] * ndim. By using 3 we can be sure that repeating the two input elements gives an output that is sufficiently unique for the multidim tests. """ if ndim > 1: import itertools idxs = [idx for idx, _ in zip(itertools.cycle([0, 1]), range(3**ndim))] col = col[idxs].reshape([3] * ndim) return col @pytest.mark.parametrize("name_col", list(mixin_cols.items())) @pytest.mark.parametrize("table_cls", (Table, QTable)) @pytest.mark.parametrize("ndim", (1, 2, 3)) def test_ecsv_mixins_per_column(table_cls, name_col, ndim): """Test write/read one col at a time and do detailed validation. This tests every input column type as 1-d, 2-d and 3-d. """ name, col = name_col c = make_multidim(np.array([1.0, 2.0]), ndim) col = make_multidim(col, ndim) t = table_cls([c, col, c], names=["c1", name, "c2"]) t[name].info.description = "description" out = StringIO() t.write(out, format="ascii.ecsv") t2 = table_cls.read(out.getvalue(), format="ascii.ecsv") assert t.colnames == t2.colnames for colname in t.colnames: assert len(t2[colname].shape) == ndim if colname in ("c1", "c2"): compare = ["data"] else: # Storing Longitude as Column loses wrap_angle. compare = [ attr for attr in compare_attrs[colname] if not (attr == "wrap_angle" and table_cls is Table) ] assert_objects_equal(t[colname], t2[colname], compare) # Special case to make sure Column type doesn't leak into Time class data if name.startswith("tm"): assert t2[name]._time.jd1.__class__ is np.ndarray assert t2[name]._time.jd2.__class__ is np.ndarray def test_round_trip_masked_table_default(tmp_path): """Test (mostly) round-trip of MaskedColumn through ECSV using default serialization that uses an empty string "" to mark NULL values. Note: >>> simple_table(masked=True) <Table masked=True length=3> a b c int64 float64 str1 ----- ------- ---- -- 1.0 c 2 2.0 -- 3 -- e """ filename = tmp_path / "test.ecsv" t = simple_table(masked=True) # int, float, and str cols with one masked element t.write(filename) t2 = Table.read(filename) assert t2.masked is False assert t2.colnames == t.colnames for name in t2.colnames: # From formal perspective the round-trip columns are the "same" assert np.all(t2[name].mask == t[name].mask) assert np.all(t2[name] == t[name]) # But peeking under the mask shows that the underlying data are changed # because by default ECSV uses "" to represent masked elements. t[name].mask = False t2[name].mask = False assert not np.all(t2[name] == t[name]) # Expected diff def test_round_trip_masked_table_serialize_mask(tmp_path): """ Same as prev but set the serialize_method to 'data_mask' so mask is written out """ filename = tmp_path / "test.ecsv" t = simple_table(masked=True) # int, float, and str cols with one masked element t["c"][0] = "" # This would come back as masked for default "" NULL marker # MaskedColumn with no masked elements. See table the MaskedColumnInfo class # _represent_as_dict() method for info about how we test a column with no masked elements. t["d"] = [1, 2, 3] t.write(filename, serialize_method="data_mask") t2 = Table.read(filename) assert t2.masked is False assert t2.colnames == t.colnames for name in t2.colnames: assert np.all(t2[name].mask == t[name].mask) assert np.all(t2[name] == t[name]) # Data under the mask round-trips also (unmask data to show this). t[name].mask = False t2[name].mask = False assert np.all(t2[name] == t[name]) @pytest.mark.parametrize("table_cls", (Table, QTable)) def test_ecsv_round_trip_user_defined_unit(table_cls, tmp_path): """Ensure that we can read-back enabled user-defined units.""" # Test adapted from #8897, where it was noted that this works # but was not tested. filename = tmp_path / "test.ecsv" unit = u.def_unit("bandpass_sol_lum") t = table_cls() t["l"] = np.arange(5) * unit t.write(filename) # without the unit enabled, get UnrecognizedUnit if table_cls is QTable: ctx = pytest.warns(u.UnitsWarning, match=r"'bandpass_sol_lum' did not parse .*") else: ctx = nullcontext() # Note: The read might also generate ResourceWarning, in addition to UnitsWarning with ctx: t2 = table_cls.read(filename) assert isinstance(t2["l"].unit, u.UnrecognizedUnit) assert str(t2["l"].unit) == "bandpass_sol_lum" if table_cls is QTable: assert np.all(t2["l"].value == t["l"].value) else: assert np.all(t2["l"] == t["l"]) # But with it enabled, it works. with u.add_enabled_units(unit): t3 = table_cls.read(filename) assert t3["l"].unit is unit assert np.all(t3["l"] == t["l"]) # Just to be sure, also try writing with unit enabled. filename2 = tmp_path / "test2.ecsv" t3.write(filename2) t4 = table_cls.read(filename) assert t4["l"].unit is unit assert np.all(t4["l"] == t["l"]) def test_read_masked_bool(): txt = """\ # %ECSV 1.0 # --- # datatype: # - {name: col0, datatype: bool} # schema: astropy-2.0 col0 1 0 True "" False """ dat = ascii.read(txt, format="ecsv") col = dat["col0"] assert isinstance(col, MaskedColumn) assert np.all(col.mask == [False, False, False, True, False]) assert np.all(col == [True, False, True, False, False]) @pytest.mark.parametrize("serialize_method", ["null_value", "data_mask"]) @pytest.mark.parametrize("dtype", [np.int64, np.float64, bool, str]) @pytest.mark.parametrize("delimiter", [",", " "]) def test_roundtrip_multidim_masked_array(serialize_method, dtype, delimiter): # TODO also test empty string with null value t = Table() col = MaskedColumn(np.arange(12).reshape(2, 3, 2), dtype=dtype) if dtype is str: # np does something funny and gives a dtype of U21. col = col.astype("U2") col.mask[0, 0, 0] = True col.mask[1, 1, 1] = True t["a"] = col t["b"] = ["x", "y"] # Add another column for kicks out = StringIO() t.write(out, format="ascii.ecsv", serialize_method=serialize_method) t2 = Table.read(out.getvalue(), format="ascii.ecsv") assert t2.masked is False assert t2.colnames == t.colnames for name in t2.colnames: assert t2[name].dtype == t[name].dtype if hasattr(t[name], "mask"): assert np.all(t2[name].mask == t[name].mask) assert np.all(t2[name] == t[name]) @pytest.mark.parametrize("subtype", ["some-user-type", "complex"]) def test_multidim_unknown_subtype(subtype): """Test an ECSV file with a string type but unknown subtype""" txt = f"""\ # %ECSV 1.0 # --- # datatype: # - name: a # datatype: string # subtype: {subtype} # schema: astropy-2.0 a [1,2] [3,4]""" with pytest.warns( InvalidEcsvDatatypeWarning, match=rf"unexpected subtype '{subtype}' set for column 'a'", ): t = ascii.read(txt, format="ecsv") assert t["a"].dtype.kind == "U" assert t["a"][0] == "[1,2]" def test_multidim_bad_shape(): """Test a malformed ECSV file""" txt = """\ # %ECSV 1.0 # --- # datatype: # - name: a # datatype: string # subtype: int64[3] # schema: astropy-2.0 a [1,2] [3,4]""" with pytest.raises( ValueError, match="column 'a' failed to convert: shape mismatch" ): Table.read(txt, format="ascii.ecsv") def test_write_not_json_serializable(): t = Table() t["a"] = np.array([{1, 2}, 1], dtype=object) match = ( "could not convert column 'a' to string: Object of type set is not JSON" " serializable" ) out = StringIO() with pytest.raises(TypeError, match=match): t.write(out, format="ascii.ecsv") def test_read_not_json_serializable(): """Test a malformed ECSV file""" txt = """\ # %ECSV 1.0 # --- # datatype: # - {name: a, datatype: string, subtype: json} # schema: astropy-2.0 a fail [3,4]""" match = "column 'a' failed to convert: column value is not valid JSON" with pytest.raises(ValueError, match=match): Table.read(txt, format="ascii.ecsv") def test_read_bad_datatype(): """Test a malformed ECSV file""" txt = """\ # %ECSV 1.0 # --- # datatype: # - {name: a, datatype: object} # schema: astropy-2.0 a fail [3,4]""" with pytest.warns( InvalidEcsvDatatypeWarning, match="unexpected datatype 'object' of column 'a' is not in allowed", ): t = Table.read(txt, format="ascii.ecsv") assert t["a"][0] == "fail" assert type(t["a"][1]) is str assert type(t["a"].dtype) == np.dtype("O") def test_read_complex(): """Test an ECSV v1.0 file with a complex column""" txt = """\ # %ECSV 1.0 # --- # datatype: # - {name: a, datatype: complex} # schema: astropy-2.0 a 1+1j 2+2j""" with pytest.warns( InvalidEcsvDatatypeWarning, match="unexpected datatype 'complex' of column 'a' is not in allowed", ): t = Table.read(txt, format="ascii.ecsv") assert t["a"].dtype.type is np.complex128 def test_read_str(): """Test an ECSV file with a 'str' instead of 'string' datatype""" txt = """\ # %ECSV 1.0 # --- # datatype: # - {name: a, datatype: str} # schema: astropy-2.0 a sometext S""" # also testing single character text with pytest.warns( InvalidEcsvDatatypeWarning, match="unexpected datatype 'str' of column 'a' is not in allowed", ): t = Table.read(txt, format="ascii.ecsv") assert isinstance(t["a"][1], str) assert isinstance(t["a"][0], np.str_) def test_read_bad_datatype_for_object_subtype(): """Test a malformed ECSV file""" txt = """\ # %ECSV 1.0 # --- # datatype: # - {name: a, datatype: int64, subtype: json} # schema: astropy-2.0 a fail [3,4]""" match = "column 'a' failed to convert: datatype of column 'a' must be \"string\"" with pytest.raises(ValueError, match=match): Table.read(txt, format="ascii.ecsv") def test_full_repr_roundtrip(): """Test round-trip of float values to full precision even with format specified""" t = Table() t["a"] = np.array([np.pi, 1 / 7], dtype=np.float64) t["a"].info.format = ".2f" out = StringIO() t.write(out, format="ascii.ecsv") t2 = Table.read(out.getvalue(), format="ascii.ecsv") assert np.all(t["a"] == t2["a"]) assert t2["a"].info.format == ".2f" ############################################################################# # Define a number of specialized columns for testing and the expected values # of `datatype` for each column. ############################################################################# # First here is some helper code used to make the expected outputs code. def _get_ecsv_header_dict(text): lines = [line.strip() for line in text.splitlines()] lines = [line[2:] for line in lines if line.startswith("#")] lines = lines[2:] # Get rid of the header out = yaml.safe_load("\n".join(lines)) return out def _make_expected_values(cols): from pprint import pformat for name, col in cols.items(): t = Table() t[name] = col out = StringIO() t.write(out, format="ascii.ecsv") hdr = _get_ecsv_header_dict(out.getvalue()) fmt_hdr = pformat(hdr["datatype"]) print(f"exps[{name!r}] =", fmt_hdr[:1]) print(fmt_hdr[1:]) print() # Expected values of `datatype` for each column exps = {} cols = {} # Run of the mill scalar for completeness cols["scalar"] = np.array([1, 2], dtype=np.int16) exps["scalar"] = [{"datatype": "int16", "name": "scalar"}] # Array of lists that works as a 2-d variable array. This is just treated # as an object. cols["2-d variable array lists"] = c = np.empty(shape=(2,), dtype=object) c[0] = [[1, 2], ["a", 4]] c[1] = [[1, 2, 3], [4, 5.25, 6]] exps["2-d variable array lists"] = [ {"datatype": "string", "name": "2-d variable array lists", "subtype": "json"} ] # Array of numpy arrays that is a 2-d variable array cols["2-d variable array numpy"] = c = np.empty(shape=(2,), dtype=object) c[0] = np.array([[1, 2], [3, 4]], dtype=np.float32) c[1] = np.array([[1, 2, 3], [4, 5.5, 6]], dtype=np.float32) exps["2-d variable array numpy"] = [ { "datatype": "string", "name": "2-d variable array numpy", "subtype": "float32[2,null]", } ] cols["1-d variable array lists"] = np.array([[1, 2], [3, 4, 5]], dtype=object) exps["1-d variable array lists"] = [ {"datatype": "string", "name": "1-d variable array lists", "subtype": "json"} ] # Variable-length array cols["1-d variable array numpy"] = np.array( [np.array([1, 2], dtype=np.uint8), np.array([3, 4, 5], dtype=np.uint8)], dtype=object, ) exps["1-d variable array numpy"] = [ {"datatype": "string", "name": "1-d variable array numpy", "subtype": "uint8[null]"} ] cols["1-d variable array numpy str"] = np.array( [np.array(["a", "b"]), np.array(["c", "d", "e"])], dtype=object ) exps["1-d variable array numpy str"] = [ { "datatype": "string", "name": "1-d variable array numpy str", "subtype": "string[null]", } ] cols["1-d variable array numpy bool"] = np.array( [np.array([True, False]), np.array([True, False, True])], dtype=object ) exps["1-d variable array numpy bool"] = [ { "datatype": "string", "name": "1-d variable array numpy bool", "subtype": "bool[null]", } ] cols["1-d regular array"] = np.array([[1, 2], [3, 4]], dtype=np.int8) exps["1-d regular array"] = [ {"datatype": "string", "name": "1-d regular array", "subtype": "int8[2]"} ] cols["2-d regular array"] = np.arange(8, dtype=np.float16).reshape(2, 2, 2) exps["2-d regular array"] = [ {"datatype": "string", "name": "2-d regular array", "subtype": "float16[2,2]"} ] cols["scalar object"] = np.array([{"a": 1}, {"b": 2}], dtype=object) exps["scalar object"] = [ {"datatype": "string", "name": "scalar object", "subtype": "json"} ] cols["1-d object"] = np.array( [[{"a": 1}, {"b": 2}], [{"a": 1}, {"b": 2}]], dtype=object ) exps["1-d object"] = [ {"datatype": "string", "name": "1-d object", "subtype": "json[2]"} ] @pytest.mark.parametrize("name,col,exp", list(zip(cols, cols.values(), exps.values()))) def test_specialized_columns(name, col, exp): """Test variable length lists, multidim columns, object columns.""" t = Table() t[name] = col out = StringIO() t.write(out, format="ascii.ecsv") hdr = _get_ecsv_header_dict(out.getvalue()) assert hdr["datatype"] == exp t2 = Table.read(out.getvalue(), format="ascii.ecsv") assert t2.colnames == t.colnames for name in t2.colnames: assert t2[name].dtype == t[name].dtype for val1, val2 in zip(t2[name], t[name]): if isinstance(val1, np.ndarray): assert val1.dtype == val2.dtype assert np.all(val1 == val2) def test_full_subtypes(): """Read ECSV file created by M. Taylor that includes scalar, fixed array, variable array for all datatypes. This file has missing values for all columns as both per-value null and blank entries for the entire column value. Note: original file was modified to include blank values in f_float and f_double columns. """ t = Table.read(os.path.join(TEST_DIR, "data", "subtypes.ecsv")) colnames = ( "i_index," "s_byte,s_short,s_int,s_long,s_float,s_double,s_string,s_boolean," "f_byte,f_short,f_int,f_long,f_float,f_double,f_string,f_boolean," "v_byte,v_short,v_int,v_long,v_float,v_double,v_string,v_boolean," "m_int,m_double" ).split(",") assert t.colnames == colnames type_map = { "byte": "int8", "short": "int16", "int": "int32", "long": "int64", "float": "float32", "double": "float64", "string": "str", "boolean": "bool", } for col in t.itercols(): info = col.info if info.name == "i_index": continue assert isinstance(col, MaskedColumn) type_name = info.name[2:] # short, int, etc subtype = info.name[:1] if subtype == "s": # Scalar assert col.shape == (16,) if subtype == "f": # Fixed array assert col.shape == (16, 3) if subtype == "v": # Variable array assert col.shape == (16,) assert info.dtype.name == "object" for val in col: assert isinstance(val, np.ndarray) assert val.dtype.name.startswith(type_map[type_name]) assert len(val) in [0, 1, 2, 3] else: assert info.dtype.name.startswith(type_map[type_name]) def test_masked_empty_subtypes(): """Test blank field in subtypes. Similar to previous test but with explicit checks of values""" txt = """ # %ECSV 1.0 # --- # datatype: # - {name: o, datatype: string, subtype: json} # - {name: f, datatype: string, subtype: 'int64[2]'} # - {name: v, datatype: string, subtype: 'int64[null]'} # schema: astropy-2.0 o f v null [0,1] [1] "" "" "" [1,2] [2,3] [2,3] """ t = Table.read(txt, format="ascii.ecsv") assert np.all(t["o"] == np.array([None, -1, [1, 2]], dtype=object)) assert np.all(t["o"].mask == [False, True, False]) exp = np.ma.array([[0, 1], [-1, -1], [2, 3]], mask=[[0, 0], [1, 1], [0, 0]]) assert np.all(t["f"] == exp) assert np.all(t["f"].mask == exp.mask) assert np.all(t["v"][0] == [1]) assert np.all(t["v"][2] == [2, 3]) assert np.all(t["v"].mask == [False, True, False]) def test_masked_vals_in_array_subtypes(): """Test null values in fixed and variable array subtypes.""" t = Table() t["f"] = np.ma.array([[1, 2], [3, 4]], mask=[[0, 1], [1, 0]], dtype=np.int64) t["v"] = np.empty(2, dtype=object) t["v"][0] = np.ma.array([1, 2], mask=[0, 1], dtype=np.int64) t["v"][1] = np.ma.array([3, 4, 5], mask=[1, 0, 0], dtype=np.int64) out = StringIO() t.write(out, format="ascii.ecsv") txt = """ # %ECSV 1.0 # --- # datatype: # - {name: f, datatype: string, subtype: 'int64[2]'} # - {name: v, datatype: string, subtype: 'int64[null]'} # schema: astropy-2.0 f v [1,null] [1,null] [null,4] [null,4,5] """ hdr = _get_ecsv_header_dict(out.getvalue()) hdr_exp = _get_ecsv_header_dict(txt) assert hdr == hdr_exp t2 = Table.read(out.getvalue(), format="ascii.ecsv") assert t2.colnames == t.colnames for name in t2.colnames: assert t2[name].dtype == t[name].dtype assert type(t2[name]) is type(t[name]) for val1, val2 in zip(t2[name], t[name]): if isinstance(val1, np.ndarray): assert val1.dtype == val2.dtype if isinstance(val1, np.ma.MaskedArray): assert np.all(val1.mask == val2.mask) assert np.all(val1 == val2) def test_guess_ecsv_with_one_column(): """Except for ECSV, guessing always requires at least 2 columns""" txt = """ # %ECSV 1.0 # --- # datatype: # - {name: col, datatype: string, description: hello} # schema: astropy-2.0 col 1 2 """ t = ascii.read(txt) assert t["col"].dtype.kind == "U" # would be int with basic format assert t["col"].description == "hello"
8f286df228176533da952cc92dbe96491ca5733817e190b5ae29c3234c3d9e9a
# Licensed under a 3-clause BSD style license - see LICENSE.rst import locale import pathlib import platform import re from collections import OrderedDict from io import BytesIO, StringIO import numpy as np import pytest from astropy import table from astropy.io import ascii from astropy.io.ascii import core from astropy.io.ascii.core import convert_numpy from astropy.io.ascii.ui import _probably_html, get_read_trace from astropy.table import MaskedColumn, Table from astropy.table.table_helpers import simple_table from astropy.units import Unit # NOTE: Python can be built without bz2. from astropy.utils.compat.optional_deps import HAS_BZ2 from astropy.utils.data import get_pkg_data_path from astropy.utils.exceptions import AstropyWarning # setup/teardown function to have the tests run in the correct directory from .common import ( # noqa: F401 assert_almost_equal, assert_equal, assert_true, setup_function, teardown_function, ) def asciiIO(x): return BytesIO(x.encode("ascii")) @pytest.fixture def home_is_data(monkeypatch, request): """ Pytest fixture to run a test case with tilde-prefixed paths. In the tilde-path case, environment variables are temporarily modified so that '~' resolves to the data directory. """ path = get_pkg_data_path("data") # For Unix monkeypatch.setenv("HOME", path) # For Windows monkeypatch.setenv("USERPROFILE", path) @pytest.mark.parametrize( "fast_reader", [True, False, {"use_fast_converter": False}, {"use_fast_converter": True}, "force"], ) def test_convert_overflow(fast_reader): """ Test reading an extremely large integer, which falls through to string due to an overflow error (#2234). The C parsers used to return inf (kind 'f') for this. """ expected_kind = "U" with pytest.warns( AstropyWarning, match="OverflowError converting to IntType in column a" ): dat = ascii.read( ["a", "1" * 10000], format="basic", fast_reader=fast_reader, guess=False ) assert dat["a"].dtype.kind == expected_kind def test_read_specify_converters_with_names(): """ Exact example from #9701: When using ascii.read with both the names and converters arguments, the converters dictionary ignores the user-supplied names and requires that you know the guessed names. """ csv_text = ["a,b,c", "1,2,3", "4,5,6"] names = ["A", "B", "C"] converters = { "A": [ascii.convert_numpy(float)], "B": [ascii.convert_numpy(int)], "C": [ascii.convert_numpy(str)], } t = ascii.read(csv_text, format="csv", names=names, converters=converters) assert t["A"].dtype.kind == "f" assert t["B"].dtype.kind == "i" assert t["C"].dtype.kind == "U" def test_read_remove_and_rename_columns(): csv_text = ["a,b,c", "1,2,3", "4,5,6"] reader = ascii.get_reader(Reader=ascii.Csv) reader.read(csv_text) header = reader.header with pytest.raises(KeyError, match="Column NOT-EXIST does not exist"): header.remove_columns(["NOT-EXIST"]) header.remove_columns(["c"]) assert header.colnames == ("a", "b") header.rename_column("a", "aa") assert header.colnames == ("aa", "b") with pytest.raises(KeyError, match="Column NOT-EXIST does not exist"): header.rename_column("NOT-EXIST", "aa") def test_guess_with_names_arg(): """ Make sure reading a table with guess=True gives the expected result when the names arg is specified. """ # This is a NoHeader format table and so `names` should replace # the default col0, col1 names. It fails as a Basic format # table when guessing because the column names would be '1', '2'. dat = ascii.read(["1,2", "3,4"], names=("a", "b")) assert len(dat) == 2 assert dat.colnames == ["a", "b"] # This is a Basic format table and the first row # gives the column names 'c', 'd', which get replaced by 'a', 'b' dat = ascii.read(["c,d", "3,4"], names=("a", "b")) assert len(dat) == 1 assert dat.colnames == ["a", "b"] # This is also a Basic format table and the first row # gives the column names 'c', 'd', which get replaced by 'a', 'b' dat = ascii.read(["c d", "e f"], names=("a", "b")) assert len(dat) == 1 assert dat.colnames == ["a", "b"] def test_guess_with_format_arg(): """ When the format or Reader is explicitly given then disable the strict column name checking in guessing. """ dat = ascii.read(["1,2", "3,4"], format="basic") assert len(dat) == 1 assert dat.colnames == ["1", "2"] dat = ascii.read(["1,2", "3,4"], names=("a", "b"), format="basic") assert len(dat) == 1 assert dat.colnames == ["a", "b"] dat = ascii.read(["1,2", "3,4"], Reader=ascii.Basic) assert len(dat) == 1 assert dat.colnames == ["1", "2"] dat = ascii.read(["1,2", "3,4"], names=("a", "b"), Reader=ascii.Basic) assert len(dat) == 1 assert dat.colnames == ["a", "b"] # For good measure check the same in the unified I/O interface dat = Table.read(["1,2", "3,4"], format="ascii.basic") assert len(dat) == 1 assert dat.colnames == ["1", "2"] dat = Table.read(["1,2", "3,4"], format="ascii.basic", names=("a", "b")) assert len(dat) == 1 assert dat.colnames == ["a", "b"] def test_guess_with_delimiter_arg(): """ When the delimiter is explicitly given then do not try others in guessing. """ fields = ["10.1E+19", "3.14", "2048", "-23"] values = [1.01e20, 3.14, 2048, -23] # Default guess should recognise CSV with optional spaces t0 = ascii.read(asciiIO(", ".join(fields)), guess=True) for n, v in zip(t0.colnames, values): assert t0[n][0] == v # Forcing space as delimiter produces type str columns ('10.1E+19,') t1 = ascii.read(asciiIO(", ".join(fields)), guess=True, delimiter=" ") for n, v in zip(t1.colnames[:-1], fields[:-1]): assert t1[n][0] == v + "," def test_reading_mixed_delimiter_tabs_spaces(): # Regression test for https://github.com/astropy/astropy/issues/6770 dat = ascii.read("1 2\t3\n1 2\t3", format="no_header", names=list("abc")) assert len(dat) == 2 Table.read(["1 2\t3", "1 2\t3"], format="ascii.no_header", names=["a", "b", "c"]) assert len(dat) == 2 @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_read_with_names_arg(fast_reader): """ Test that a bad value of `names` raises an exception. """ # CParser only uses columns in `names` and thus reports mismatch in num_col with pytest.raises(ascii.InconsistentTableError): ascii.read(["c d", "e f"], names=("a",), guess=False, fast_reader=fast_reader) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) @pytest.mark.parametrize("path_format", ["plain", "tilde-str", "tilde-pathlib"]) def test_read_all_files(fast_reader, path_format, home_is_data): for testfile in get_testfiles(): if testfile.get("skip"): print(f"\n\n******** SKIPPING {testfile['name']}") continue if "tilde" in path_format: if "str" in path_format: testfile["name"] = "~/" + testfile["name"][5:] else: testfile["name"] = pathlib.Path("~/", testfile["name"][5:]) print(f"\n\n******** READING {testfile['name']}") for guess in (True, False): test_opts = testfile["opts"].copy() if "guess" not in test_opts: test_opts["guess"] = guess if ( "Reader" in test_opts and f"fast_{test_opts['Reader']._format_name}" in core.FAST_CLASSES ): # has fast version if "Inputter" not in test_opts: # fast reader doesn't allow this test_opts["fast_reader"] = fast_reader table = ascii.read(testfile["name"], **test_opts) assert_equal(table.dtype.names, testfile["cols"]) for colname in table.dtype.names: assert_equal(len(table[colname]), testfile["nrows"]) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) @pytest.mark.parametrize("path_format", ["plain", "tilde-str", "tilde-pathlib"]) def test_read_all_files_via_table(fast_reader, path_format, home_is_data): for testfile in get_testfiles(): if testfile.get("skip"): print(f"\n\n******** SKIPPING {testfile['name']}") continue if "tilde" in path_format: if "str" in path_format: testfile["name"] = "~/" + testfile["name"][5:] else: testfile["name"] = pathlib.Path("~/", testfile["name"][5:]) print(f"\n\n******** READING {testfile['name']}") for guess in (True, False): test_opts = testfile["opts"].copy() if "guess" not in test_opts: test_opts["guess"] = guess if "Reader" in test_opts: format = f"ascii.{test_opts['Reader']._format_name}" del test_opts["Reader"] else: format = "ascii" if f"fast_{format}" in core.FAST_CLASSES: test_opts["fast_reader"] = fast_reader table = Table.read(testfile["name"], format=format, **test_opts) assert_equal(table.dtype.names, testfile["cols"]) for colname in table.dtype.names: assert_equal(len(table[colname]), testfile["nrows"]) def test_guess_all_files(): for testfile in get_testfiles(): if testfile.get("skip"): print(f"\n\n******** SKIPPING {testfile['name']}") continue if not testfile["opts"].get("guess", True): continue print(f"\n\n******** READING {testfile['name']}") for filter_read_opts in (["Reader", "delimiter", "quotechar"], []): # Copy read options except for those in filter_read_opts guess_opts = { k: v for k, v in testfile["opts"].items() if k not in filter_read_opts } table = ascii.read(testfile["name"], guess=True, **guess_opts) assert_equal(table.dtype.names, testfile["cols"]) for colname in table.dtype.names: assert_equal(len(table[colname]), testfile["nrows"]) def test_validate_read_kwargs(): lines = ["a b", "1 2", "3 4"] # Check that numpy integers are allowed out = ascii.read(lines, data_start=np.int16(2)) assert np.all(out["a"] == [3]) with pytest.raises( TypeError, match=r"read\(\) argument 'data_end' must be a " r"<class 'int'> object, " r"got <class 'str'> instead", ): ascii.read(lines, data_end="needs integer") with pytest.raises( TypeError, match=r"read\(\) argument 'fill_include_names' must " r"be a list-like object, got <class 'str'> instead", ): ascii.read(lines, fill_include_names="ID") def test_daophot_indef(): """Test that INDEF is correctly interpreted as a missing value""" table = ascii.read("data/daophot2.dat", Reader=ascii.Daophot) for col in table.itercols(): # Four columns have all INDEF values and are masked, rest are normal Column if col.name in ("OTIME", "MAG", "MERR", "XAIRMASS"): assert np.all(col.mask) else: assert not hasattr(col, "mask") def test_daophot_types(): """ Test specific data types which are different from what would be inferred automatically based only data values. DAOphot reader uses the header information to assign types. """ table = ascii.read("data/daophot2.dat", Reader=ascii.Daophot) assert table["LID"].dtype.char in "fd" # float or double assert table["MAG"].dtype.char in "fd" # even without any data values assert ( table["PIER"].dtype.char in "US" ) # string (data values are consistent with int) assert table["ID"].dtype.char in "il" # int or long def test_daophot_header_keywords(): table = ascii.read("data/daophot.dat", Reader=ascii.Daophot) expected_keywords = ( ("NSTARFILE", "test.nst.1", "filename", "%-23s"), ("REJFILE", '"hello world"', "filename", "%-23s"), ("SCALE", "1.", "units/pix", "%-23.7g"), ) keywords = table.meta["keywords"] # Ordered dict of keyword structures for name, value, units, format_ in expected_keywords: keyword = keywords[name] assert_equal(keyword["value"], value) assert_equal(keyword["units"], units) assert_equal(keyword["format"], format_) def test_daophot_multiple_aperture(): table = ascii.read("data/daophot3.dat", Reader=ascii.Daophot) assert "MAG5" in table.colnames # MAG5 is one of the newly created column names assert table["MAG5"][4] == 22.13 # A sample entry in daophot3.dat file assert table["MERR2"][0] == 1.171 assert np.all( table["RAPERT5"] == 23.3 ) # assert all the 5th apertures are same 23.3 def test_daophot_multiple_aperture2(): table = ascii.read("data/daophot4.dat", Reader=ascii.Daophot) assert "MAG15" in table.colnames # MAG15 is one of the newly created column name assert table["MAG15"][1] == -7.573 # A sample entry in daophot4.dat file assert table["MERR2"][0] == 0.049 assert np.all(table["RAPERT5"] == 5.0) # assert all the 5th apertures are same 5.0 @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_empty_table_no_header(fast_reader): with pytest.raises(ascii.InconsistentTableError): ascii.read( "data/no_data_without_header.dat", Reader=ascii.NoHeader, guess=False, fast_reader=fast_reader, ) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_wrong_quote(fast_reader): with pytest.raises(ascii.InconsistentTableError): ascii.read("data/simple.txt", guess=False, fast_reader=fast_reader) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_extra_data_col(fast_reader): with pytest.raises(ascii.InconsistentTableError): ascii.read("data/bad.txt", fast_reader=fast_reader) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_extra_data_col2(fast_reader): with pytest.raises(ascii.InconsistentTableError): ascii.read("data/simple5.txt", delimiter="|", fast_reader=fast_reader) def test_missing_file(): with pytest.raises(OSError): ascii.read("does_not_exist") @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_set_names(fast_reader): names = ("c1", "c2", "c3", "c4", "c5", "c6") data = ascii.read( "data/simple3.txt", names=names, delimiter="|", fast_reader=fast_reader ) assert_equal(data.dtype.names, names) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_set_include_names(fast_reader): names = ("c1", "c2", "c3", "c4", "c5", "c6") include_names = ("c1", "c3") data = ascii.read( "data/simple3.txt", names=names, include_names=include_names, delimiter="|", fast_reader=fast_reader, ) assert_equal(data.dtype.names, include_names) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_set_exclude_names(fast_reader): exclude_names = ("Y", "object") data = ascii.read( "data/simple3.txt", exclude_names=exclude_names, delimiter="|", fast_reader=fast_reader, ) assert_equal(data.dtype.names, ("obsid", "redshift", "X", "rad")) def test_include_names_daophot(): include_names = ("ID", "MAG", "PIER") data = ascii.read("data/daophot.dat", include_names=include_names) assert_equal(data.dtype.names, include_names) def test_exclude_names_daophot(): exclude_names = ("ID", "YCENTER", "MERR", "NITER", "CHI", "PERROR") data = ascii.read("data/daophot.dat", exclude_names=exclude_names) assert_equal(data.dtype.names, ("XCENTER", "MAG", "MSKY", "SHARPNESS", "PIER")) def test_custom_process_lines(): def process_lines(lines): bars_at_ends = re.compile(r"^\| | \|$", re.VERBOSE) striplines = (x.strip() for x in lines) return [bars_at_ends.sub("", x) for x in striplines if len(x) > 0] reader = ascii.get_reader(delimiter="|") reader.inputter.process_lines = process_lines data = reader.read("data/bars_at_ends.txt") assert_equal(data.dtype.names, ("obsid", "redshift", "X", "Y", "object", "rad")) assert_equal(len(data), 3) def test_custom_process_line(): def process_line(line): line_out = re.sub(r"^\|\s*", "", line.strip()) return line_out reader = ascii.get_reader(data_start=2, delimiter="|") reader.header.splitter.process_line = process_line reader.data.splitter.process_line = process_line data = reader.read("data/nls1_stackinfo.dbout") cols = get_testfiles("data/nls1_stackinfo.dbout")["cols"] assert_equal(data.dtype.names, cols[1:]) def test_custom_splitters(): reader = ascii.get_reader() reader.header.splitter = ascii.BaseSplitter() reader.data.splitter = ascii.BaseSplitter() f = "data/test4.dat" data = reader.read(f) testfile = get_testfiles(f) assert_equal(data.dtype.names, testfile["cols"]) assert_equal(len(data), testfile["nrows"]) assert_almost_equal(data.field("zabs1.nh")[2], 0.0839710433091) assert_almost_equal(data.field("p1.gamma")[2], 1.25997502704) assert_almost_equal(data.field("p1.ampl")[2], 0.000696444029148) assert_equal(data.field("statname")[2], "chi2modvar") assert_almost_equal(data.field("statval")[2], 497.56468441) def test_start_end(): data = ascii.read("data/test5.dat", header_start=1, data_start=3, data_end=-5) assert_equal(len(data), 13) assert_equal(data.field("statname")[0], "chi2xspecvar") assert_equal(data.field("statname")[-1], "chi2gehrels") def test_set_converters(): converters = { "zabs1.nh": [ascii.convert_numpy("int32"), ascii.convert_numpy("float32")], "p1.gamma": [ascii.convert_numpy("str")], } data = ascii.read("data/test4.dat", converters=converters) assert_equal(str(data["zabs1.nh"].dtype), "float32") assert_equal(data["p1.gamma"][0], "1.26764500000") @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_from_string(fast_reader): f = "data/simple.txt" with open(f) as fd: table = fd.read() testfile = get_testfiles(f)[0] data = ascii.read(table, fast_reader=fast_reader, **testfile["opts"]) assert_equal(data.dtype.names, testfile["cols"]) assert_equal(len(data), testfile["nrows"]) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_from_filelike(fast_reader): f = "data/simple.txt" testfile = get_testfiles(f)[0] with open(f, "rb") as fd: data = ascii.read(fd, fast_reader=fast_reader, **testfile["opts"]) assert_equal(data.dtype.names, testfile["cols"]) assert_equal(len(data), testfile["nrows"]) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_from_lines(fast_reader): f = "data/simple.txt" with open(f) as fd: table = fd.readlines() testfile = get_testfiles(f)[0] data = ascii.read(table, fast_reader=fast_reader, **testfile["opts"]) assert_equal(data.dtype.names, testfile["cols"]) assert_equal(len(data), testfile["nrows"]) def test_comment_lines(): table = ascii.get_reader(Reader=ascii.Rdb) data = table.read("data/apostrophe.rdb") assert_equal(table.comment_lines, ["# first comment", " # second comment"]) assert_equal(data.meta["comments"], ["first comment", "second comment"]) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_fill_values(fast_reader): f = "data/fill_values.txt" testfile = get_testfiles(f) data = ascii.read( f, fill_values=("a", "1"), fast_reader=fast_reader, **testfile["opts"] ) assert_true((data["a"].mask == [False, True]).all()) assert_true((data["a"] == [1, 1]).all()) assert_true((data["b"].mask == [False, True]).all()) assert_true((data["b"] == [2, 1]).all()) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_fill_values_col(fast_reader): f = "data/fill_values.txt" testfile = get_testfiles(f) data = ascii.read( f, fill_values=("a", "1", "b"), fast_reader=fast_reader, **testfile["opts"] ) check_fill_values(data) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_fill_values_include_names(fast_reader): f = "data/fill_values.txt" testfile = get_testfiles(f) data = ascii.read( f, fill_values=("a", "1"), fast_reader=fast_reader, fill_include_names=["b"], **testfile["opts"], ) check_fill_values(data) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_fill_values_exclude_names(fast_reader): f = "data/fill_values.txt" testfile = get_testfiles(f) data = ascii.read( f, fill_values=("a", "1"), fast_reader=fast_reader, fill_exclude_names=["a"], **testfile["opts"], ) check_fill_values(data) def check_fill_values(data): """compare array column by column with expectation""" assert not hasattr(data["a"], "mask") assert_true((data["a"] == ["1", "a"]).all()) assert_true((data["b"].mask == [False, True]).all()) # Check that masked value is "do not care" in comparison assert_true((data["b"] == [2, -999]).all()) data["b"].mask = False # explicitly unmask for comparison assert_true((data["b"] == [2, 1]).all()) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_fill_values_list(fast_reader): f = "data/fill_values.txt" testfile = get_testfiles(f) data = ascii.read( f, fill_values=[("a", "42"), ("1", "42", "a")], fast_reader=fast_reader, **testfile["opts"], ) data["a"].mask = False # explicitly unmask for comparison assert_true((data["a"] == [42, 42]).all()) def test_masking_Cds_Mrt(): f = "data/cds.dat" # Tested for CDS and MRT for testfile in get_testfiles(f): data = ascii.read(f, **testfile["opts"]) assert_true(data["AK"].mask[0]) assert not hasattr(data["Fit"], "mask") def test_null_Ipac(): f = "data/ipac.dat" testfile = get_testfiles(f)[0] data = ascii.read(f, **testfile["opts"]) mask = np.array( [(True, False, True, False, True), (False, False, False, False, False)], dtype=[ ("ra", "|b1"), ("dec", "|b1"), ("sai", "|b1"), ("v2", "|b1"), ("sptype", "|b1"), ], ) assert np.all(data.mask == mask) def test_Ipac_meta(): keywords = OrderedDict( ( ("intval", 1), ("floatval", 2.3e3), ("date", "Wed Sp 20 09:48:36 1995"), ("key_continue", "IPAC keywords can continue across lines"), ) ) comments = ["This is an example of a valid comment"] f = "data/ipac.dat" testfile = get_testfiles(f)[0] data = ascii.read(f, **testfile["opts"]) assert data.meta["keywords"].keys() == keywords.keys() for data_kv, kv in zip(data.meta["keywords"].values(), keywords.values()): assert data_kv["value"] == kv assert data.meta["comments"] == comments def test_set_guess_kwarg(): """Read a file using guess with one of the typical guess_kwargs explicitly set.""" data = ascii.read("data/space_delim_no_header.dat", delimiter=",", guess=True) assert data.dtype.names == ("1 3.4 hello",) assert len(data) == 1 @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_read_rdb_wrong_type(fast_reader): """Read RDB data with inconsistent data type (except failure)""" table = """col1\tcol2 N\tN 1\tHello""" with pytest.raises(ValueError): ascii.read(table, Reader=ascii.Rdb, fast_reader=fast_reader) @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_default_missing(fast_reader): """ Read a table with empty values and ensure that corresponding entries are masked """ table = "\n".join( [ "a,b,c,d", "1,3,,", "2, , 4.0 , ss ", ] ) dat = ascii.read(table, fast_reader=fast_reader) assert dat.masked is False assert dat.pformat() == [ " a b c d ", "--- --- --- ---", " 1 3 -- --", " 2 -- 4.0 ss", ] # Single row table with a single missing element table = """ a \n "" """ dat = ascii.read(table, fast_reader=fast_reader) assert dat.pformat() == [" a ", "---", " --"] assert dat["a"].dtype.kind == "i" # Same test with a fixed width reader table = "\n".join( [ " a b c d ", "--- --- --- ---", " 1 3 ", " 2 4.0 ss", ] ) dat = ascii.read(table, Reader=ascii.FixedWidthTwoLine) assert dat.masked is False assert dat.pformat() == [ " a b c d ", "--- --- --- ---", " 1 3 -- --", " 2 -- 4.0 ss", ] dat = ascii.read(table, Reader=ascii.FixedWidthTwoLine, fill_values=None) assert dat.masked is False assert dat.pformat() == [ " a b c d ", "--- --- --- ---", " 1 3 ", " 2 4.0 ss", ] dat = ascii.read(table, Reader=ascii.FixedWidthTwoLine, fill_values=[]) assert dat.masked is False assert dat.pformat() == [ " a b c d ", "--- --- --- ---", " 1 3 ", " 2 4.0 ss", ] def get_testfiles(name=None): """Set up information about the columns, number of rows, and reader params to read a bunch of test files and verify columns and number of rows.""" testfiles = [ { "cols": ("agasc_id", "n_noids", "n_obs"), "name": "data/apostrophe.rdb", "nrows": 2, "opts": {"Reader": ascii.Rdb}, }, { "cols": ("agasc_id", "n_noids", "n_obs"), "name": "data/apostrophe.tab", "nrows": 2, "opts": {"Reader": ascii.Tab}, }, { "cols": ( "Index", "RAh", "RAm", "RAs", "DE-", "DEd", "DEm", "DEs", "Match", "Class", "AK", "Fit", ), "name": "data/cds.dat", "nrows": 1, "opts": {"Reader": ascii.Cds}, }, { "cols": ( "Index", "RAh", "RAm", "RAs", "DE-", "DEd", "DEm", "DEs", "Match", "Class", "AK", "Fit", ), "name": "data/cds.dat", "nrows": 1, "opts": {"Reader": ascii.Mrt}, }, # Test malformed CDS file (issues #2241 #467) { "cols": ( "Index", "RAh", "RAm", "RAs", "DE-", "DEd", "DEm", "DEs", "Match", "Class", "AK", "Fit", ), "name": "data/cds_malformed.dat", "nrows": 1, "opts": {"Reader": ascii.Cds, "data_start": "guess"}, }, { "cols": ("a", "b", "c"), "name": "data/commented_header.dat", "nrows": 2, "opts": {"Reader": ascii.CommentedHeader}, }, { "cols": ("a", "b", "c"), "name": "data/commented_header2.dat", "nrows": 2, "opts": {"Reader": ascii.CommentedHeader, "header_start": -1}, }, { "cols": ("col1", "col2", "col3", "col4", "col5"), "name": "data/continuation.dat", "nrows": 2, "opts": { "Inputter": ascii.ContinuationLinesInputter, "Reader": ascii.NoHeader, }, }, { "cols": ( "ID", "XCENTER", "YCENTER", "MAG", "MERR", "MSKY", "NITER", "SHARPNESS", "CHI", "PIER", "PERROR", ), "name": "data/daophot.dat", "nrows": 2, "opts": {"Reader": ascii.Daophot}, }, { "cols": ( "NUMBER", "FLUX_ISO", "FLUXERR_ISO", "VALU-ES", "VALU-ES_1", "FLAG", ), "name": "data/sextractor.dat", "nrows": 3, "opts": {"Reader": ascii.SExtractor}, }, { "cols": ("ra", "dec", "sai", "v2", "sptype"), "name": "data/ipac.dat", "nrows": 2, "opts": {"Reader": ascii.Ipac}, }, { "cols": ( "col0", "objID", "osrcid", "xsrcid", "SpecObjID", "ra", "dec", "obsid", "ccdid", "z", "modelMag_i", "modelMagErr_i", "modelMag_r", "modelMagErr_r", "expo", "theta", "rad_ecf_39", "detlim90", "fBlim90", ), "name": "data/nls1_stackinfo.dbout", "nrows": 58, "opts": {"data_start": 2, "delimiter": "|", "guess": False}, }, { "cols": ( "Index", "RAh", "RAm", "RAs", "DE-", "DEd", "DEm", "DEs", "Match", "Class", "AK", "Fit", ), "name": "data/no_data_cds.dat", "nrows": 0, "opts": {"Reader": ascii.Cds}, }, { "cols": ( "Index", "RAh", "RAm", "RAs", "DE-", "DEd", "DEm", "DEs", "Match", "Class", "AK", "Fit", ), "name": "data/no_data_cds.dat", "nrows": 0, "opts": {"Reader": ascii.Mrt}, }, { "cols": ( "ID", "XCENTER", "YCENTER", "MAG", "MERR", "MSKY", "NITER", "SHARPNESS", "CHI", "PIER", "PERROR", ), "name": "data/no_data_daophot.dat", "nrows": 0, "opts": {"Reader": ascii.Daophot}, }, { "cols": ("NUMBER", "FLUX_ISO", "FLUXERR_ISO", "VALUES", "VALUES_1", "FLAG"), "name": "data/no_data_sextractor.dat", "nrows": 0, "opts": {"Reader": ascii.SExtractor}, }, { "cols": ("ra", "dec", "sai", "v2", "sptype"), "name": "data/no_data_ipac.dat", "nrows": 0, "opts": {"Reader": ascii.Ipac}, }, { "cols": ("ra", "v2"), "name": "data/ipac.dat", "nrows": 2, "opts": {"Reader": ascii.Ipac, "include_names": ["ra", "v2"]}, }, { "cols": ("a", "b", "c"), "name": "data/no_data_with_header.dat", "nrows": 0, "opts": {}, }, { "cols": ("agasc_id", "n_noids", "n_obs"), "name": "data/short.rdb", "nrows": 7, "opts": {"Reader": ascii.Rdb}, }, { "cols": ("agasc_id", "n_noids", "n_obs"), "name": "data/short.tab", "nrows": 7, "opts": {"Reader": ascii.Tab}, }, { "cols": ("test 1a", "test2", "test3", "test4"), "name": "data/simple.txt", "nrows": 2, "opts": {"quotechar": "'"}, }, { "cols": ("top1", "top2", "top3", "top4"), "name": "data/simple.txt", "nrows": 1, "opts": {"quotechar": "'", "header_start": 1, "data_start": 2}, }, { "cols": ("top1", "top2", "top3", "top4"), "name": "data/simple.txt", "nrows": 1, "opts": {"quotechar": "'", "header_start": 1}, }, { "cols": ("top1", "top2", "top3", "top4"), "name": "data/simple.txt", "nrows": 2, "opts": {"quotechar": "'", "header_start": 1, "data_start": 1}, }, { "cols": ("obsid", "redshift", "X", "Y", "object", "rad"), "name": "data/simple2.txt", "nrows": 3, "opts": {"delimiter": "|"}, }, { "cols": ("obsid", "redshift", "X", "Y", "object", "rad"), "name": "data/simple3.txt", "nrows": 2, "opts": {"delimiter": "|"}, }, { "cols": ("col1", "col2", "col3", "col4", "col5", "col6"), "name": "data/simple4.txt", "nrows": 3, "opts": {"Reader": ascii.NoHeader, "delimiter": "|"}, }, { "cols": ("col1", "col2", "col3"), "name": "data/space_delim_no_header.dat", "nrows": 2, "opts": {"Reader": ascii.NoHeader}, }, { "cols": ("col1", "col2", "col3"), "name": "data/space_delim_no_header.dat", "nrows": 2, "opts": {"Reader": ascii.NoHeader, "header_start": None}, }, { "cols": ("obsid", "offset", "x", "y", "name", "oaa"), "name": "data/space_delim_blank_lines.txt", "nrows": 3, "opts": {}, }, { "cols": ("zabs1.nh", "p1.gamma", "p1.ampl", "statname", "statval"), "name": "data/test4.dat", "nrows": 9, "opts": {}, }, { "cols": ("a", "b", "c"), "name": "data/fill_values.txt", "nrows": 2, "opts": {"delimiter": ","}, }, { "name": "data/whitespace.dat", "cols": ("quoted colname with tab\tinside", "col2", "col3"), "nrows": 2, "opts": {"delimiter": r"\s"}, }, { "name": "data/simple_csv.csv", "cols": ("a", "b", "c"), "nrows": 2, "opts": {"Reader": ascii.Csv}, }, { "name": "data/simple_csv_missing.csv", "cols": ("a", "b", "c"), "nrows": 2, "skip": True, "opts": {"Reader": ascii.Csv}, }, { "cols": ("cola", "colb", "colc"), "name": "data/latex1.tex", "nrows": 2, "opts": {"Reader": ascii.Latex}, }, { "cols": ("Facility", "Id", "exposure", "date"), "name": "data/latex2.tex", "nrows": 3, "opts": {"Reader": ascii.AASTex}, }, { "cols": ("cola", "colb", "colc"), "name": "data/latex3.tex", "nrows": 2, "opts": {"Reader": ascii.Latex}, }, { "cols": ("Col1", "Col2", "Col3", "Col4"), "name": "data/fixed_width_2_line.txt", "nrows": 2, "opts": {"Reader": ascii.FixedWidthTwoLine}, }, ] try: import bs4 # noqa: F401 testfiles.append( { "cols": ("Column 1", "Column 2", "Column 3"), "name": "data/html.html", "nrows": 3, "opts": {"Reader": ascii.HTML}, } ) except ImportError: pass if name is not None: # If there are multiple matches then return a list, else return just # the one match. out = [x for x in testfiles if x["name"] == name] if len(out) == 1: out = out[0] else: out = testfiles return out def test_header_start_exception(): """Check certain Readers throw an exception if ``header_start`` is set For certain Readers it does not make sense to set the ``header_start``, they throw an exception if you try. This was implemented in response to issue #885. """ for readerclass in [ ascii.NoHeader, ascii.SExtractor, ascii.Ipac, ascii.BaseReader, ascii.FixedWidthNoHeader, ascii.Cds, ascii.Mrt, ascii.Daophot, ]: with pytest.raises(ValueError): ascii.core._get_reader(readerclass, header_start=5) def test_csv_table_read(): """ Check for a regression introduced by #1935. Pseudo-CSV file with commented header line. """ lines = ["# a, b", "1, 2", "3, 4"] t = ascii.read(lines) assert t.colnames == ["a", "b"] @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_overlapping_names(fast_reader): """ Check that the names argument list can overlap with the existing column names. This tests the issue in #1991. """ t = ascii.read(["a b", "1 2"], names=["b", "a"], fast_reader=fast_reader) assert t.colnames == ["b", "a"] def test_sextractor_units(): """ Make sure that the SExtractor reader correctly inputs descriptions and units. """ table = ascii.read("data/sextractor2.dat", Reader=ascii.SExtractor, guess=False) expected_units = [ None, Unit("pix"), Unit("pix"), Unit("mag"), Unit("mag"), None, Unit("pix**2"), Unit("m**(-6)"), Unit("mag * arcsec**(-2)"), ] expected_descrs = [ "Running object number", "Windowed position estimate along x", "Windowed position estimate along y", "Kron-like elliptical aperture magnitude", "RMS error for AUTO magnitude", "Extraction flags", None, "Barycenter position along MAMA x axis", "Peak surface brightness above background", ] for i, colname in enumerate(table.colnames): assert table[colname].unit == expected_units[i] assert table[colname].description == expected_descrs[i] def test_sextractor_last_column_array(): """ Make sure that the SExtractor reader handles the last column correctly when it is array-like. """ table = ascii.read("data/sextractor3.dat", Reader=ascii.SExtractor, guess=False) expected_columns = [ "X_IMAGE", "Y_IMAGE", "ALPHA_J2000", "DELTA_J2000", "MAG_AUTO", "MAGERR_AUTO", "MAG_APER", "MAG_APER_1", "MAG_APER_2", "MAG_APER_3", "MAG_APER_4", "MAG_APER_5", "MAG_APER_6", "MAGERR_APER", "MAGERR_APER_1", "MAGERR_APER_2", "MAGERR_APER_3", "MAGERR_APER_4", "MAGERR_APER_5", "MAGERR_APER_6", ] expected_units = [ Unit("pix"), Unit("pix"), Unit("deg"), Unit("deg"), Unit("mag"), Unit("mag"), Unit("mag"), Unit("mag"), Unit("mag"), Unit("mag"), Unit("mag"), Unit("mag"), Unit("mag"), Unit("mag"), Unit("mag"), Unit("mag"), Unit("mag"), Unit("mag"), Unit("mag"), Unit("mag"), ] expected_descrs = ( [ "Object position along x", None, "Right ascension of barycenter (J2000)", "Declination of barycenter (J2000)", "Kron-like elliptical aperture magnitude", "RMS error for AUTO magnitude", ] + ["Fixed aperture magnitude vector"] * 7 + ["RMS error vector for fixed aperture mag."] * 7 ) for i, colname in enumerate(table.colnames): assert table[colname].name == expected_columns[i] assert table[colname].unit == expected_units[i] assert table[colname].description == expected_descrs[i] def test_list_with_newlines(): """ Check that lists of strings where some strings consist of just a newline ("\n") are parsed correctly. """ t = ascii.read(["abc", "123\n", "456\n", "\n", "\n"]) assert t.colnames == ["abc"] assert len(t) == 2 assert t[0][0] == 123 assert t[1][0] == 456 def test_commented_csv(): """ Check that Csv reader does not have ignore lines with the # comment character which is defined for most Basic readers. """ t = ascii.read(["#a,b", "1,2", "#3,4"], format="csv") assert t.colnames == ["#a", "b"] assert len(t) == 2 assert t["#a"][1] == "#3" def test_meta_comments(): """ Make sure that line comments are included in the ``meta`` attribute of the output Table. """ t = ascii.read(["#comment1", "# comment2 \t", "a,b,c", "1,2,3"]) assert t.colnames == ["a", "b", "c"] assert t.meta["comments"] == ["comment1", "comment2"] def test_guess_fail(): """ Check the error message when guess fails """ with pytest.raises(ascii.InconsistentTableError) as err: ascii.read("asfdasdf\n1 2 3", format="basic") assert "** To figure out why the table did not read, use guess=False and" in str( err.value ) # Test the case with guessing enabled but for a format that has no free params with pytest.raises(ValueError) as err: ascii.read("asfdasdf\n1 2 3", format="ipac") assert ( "At least one header line beginning and ending with delimiter required" in str(err.value) ) # Test the case with guessing enabled but with all params specified with pytest.raises(ValueError) as err: ascii.read( "asfdasdf\n1 2 3", format="basic", quotechar='"', delimiter=" ", fast_reader=False, ) assert "Number of header columns (1) inconsistent with data columns (3)" in str( err.value ) @pytest.mark.xfail(not HAS_BZ2, reason="requires bz2") def test_guessing_file_object(): """ Test guessing a file object. Fixes #3013 and similar issue noted in #3019. """ with open("data/ipac.dat.bz2", "rb") as fd: t = ascii.read(fd) assert t.colnames == ["ra", "dec", "sai", "v2", "sptype"] def test_pformat_roundtrip(): """Check that the screen output of ``print tab`` can be read. See #3025.""" """Read a table with empty values and ensure that corresponding entries are masked""" table = "\n".join(["a,b,c,d", "1,3,1.11,1", "2, 2, 4.0 , ss "]) dat = ascii.read(table) out = ascii.read(dat.pformat()) assert len(dat) == len(out) assert dat.colnames == out.colnames for c in dat.colnames: assert np.all(dat[c] == out[c]) def test_ipac_abbrev(): lines = [ "| c1 | c2 | c3 | c4 | c5| c6 | c7 | c8 | c9|c10|c11|c12|", "| r | rE | rea | real | D | do | dou | f | i | l | da| c |", " 1 2 3 4 5 6 7 8 9 10 11 12 ", ] dat = ascii.read(lines, format="ipac") for name in dat.columns[0:8]: assert dat[name].dtype.kind == "f" for name in dat.columns[8:10]: assert dat[name].dtype.kind == "i" for name in dat.columns[10:12]: assert dat[name].dtype.kind in ("U", "S") def test_almost_but_not_quite_daophot(): """Regression test for #3319. This tables looks so close to a daophot table, that the daophot reader gets quite far before it fails with an AttributeError. Note that this table will actually be read as Commented Header table with the columns ['some', 'header', 'info']. """ lines = [ "# some header info", "#F header info beginning with 'F'", "1 2 3", "4 5 6", "7 8 9", ] dat = ascii.read(lines) assert len(dat) == 3 @pytest.mark.parametrize("fast", [False, "force"]) def test_commented_header_comments(fast): """ Test that comments in commented_header are as expected with header_start at different positions, and that the table round-trips. """ comments = ["comment 1", "comment 2", "comment 3"] lines = ["# a b", "# comment 1", "# comment 2", "# comment 3", "1 2", "3 4"] dat = ascii.read(lines, format="commented_header", fast_reader=fast) assert dat.meta["comments"] == comments assert dat.colnames == ["a", "b"] out = StringIO() ascii.write(dat, out, format="commented_header", fast_writer=fast) assert out.getvalue().splitlines() == lines lines.insert(1, lines.pop(0)) dat = ascii.read(lines, format="commented_header", header_start=1, fast_reader=fast) assert dat.meta["comments"] == comments assert dat.colnames == ["a", "b"] lines.insert(2, lines.pop(1)) dat = ascii.read(lines, format="commented_header", header_start=2, fast_reader=fast) assert dat.meta["comments"] == comments assert dat.colnames == ["a", "b"] dat = ascii.read( lines, format="commented_header", header_start=-2, fast_reader=fast ) assert dat.meta["comments"] == comments assert dat.colnames == ["a", "b"] lines.insert(3, lines.pop(2)) dat = ascii.read( lines, format="commented_header", header_start=-1, fast_reader=fast ) assert dat.meta["comments"] == comments assert dat.colnames == ["a", "b"] lines = ["# a b", "1 2", "3 4"] dat = ascii.read(lines, format="commented_header", fast_reader=fast) assert "comments" not in dat.meta assert dat.colnames == ["a", "b"] def test_probably_html(home_is_data): """ Test the routine for guessing if a table input to ascii.read is probably HTML """ for tabl0 in ( "data/html.html", "~/html.html", "http://blah.com/table.html", "https://blah.com/table.html", "file://blah/table.htm", "ftp://blah.com/table.html", "file://blah.com/table.htm", " <! doctype html > hello world", "junk < table baz> <tr foo > <td bar> </td> </tr> </table> junk", [ "junk < table baz>", " <tr foo >", " <td bar> ", "</td> </tr>", "</table> junk", ], (" <! doctype html > ", " hello world"), ): assert _probably_html(tabl0) is True for tabl0 in ( "data/html.htms", "Xhttp://blah.com/table.html", " https://blah.com/table.htm", "fole://blah/table.htm", " < doctype html > hello world", "junk < tble baz> <tr foo > <td bar> </td> </tr> </table> junk", [ "junk < table baz>", " <t foo >", " <td bar> ", "</td> </tr>", "</table> junk", ], (" <! doctype htm > ", " hello world"), [[1, 2, 3]], ): assert _probably_html(tabl0) is False @pytest.mark.parametrize("fast_reader", [True, False, "force"]) def test_data_header_start(fast_reader): tests = [ ( [ "# comment", "", " ", "skip this line", # line 0 "a b", # line 1 "1 2", ], # line 2 [{"header_start": 1}, {"header_start": 1, "data_start": 2}], ), ( [ "# comment", "", " \t", "skip this line", # line 0 "a b", # line 1 "", " \t", "skip this line", # line 2 "1 2", ], # line 3 [{"header_start": 1, "data_start": 3}], ), ( [ "# comment", "", " ", "a b", # line 0 "", " ", "skip this line", # line 1 "1 2", ], # line 2 [{"header_start": 0, "data_start": 2}, {"data_start": 2}], ), ] for lines, kwargs_list in tests: for kwargs in kwargs_list: t = ascii.read( lines, format="basic", fast_reader=fast_reader, guess=True, **kwargs ) assert t.colnames == ["a", "b"] assert len(t) == 1 assert np.all(t["a"] == [1]) # Sanity check that the expected Reader is being used assert get_read_trace()[-1]["kwargs"]["Reader"] is ( ascii.Basic if (fast_reader is False) else ascii.FastBasic ) def test_table_with_no_newline(): """ Test that an input file which is completely empty fails in the expected way. Test that an input file with one line but no newline succeeds. """ # With guessing table = BytesIO() with pytest.raises(ascii.InconsistentTableError): ascii.read(table) # Without guessing table = BytesIO() with pytest.raises(ValueError) as err: ascii.read(table, guess=False, fast_reader=False, format="basic") assert "No header line found" in str(err.value) table = BytesIO() t = ascii.read(table, guess=False, fast_reader=True, format="fast_basic") assert not t and t.as_array().size == 0 # Put a single line of column names but with no newline for kwargs in [ dict(), dict(guess=False, fast_reader=False, format="basic"), dict(guess=False, fast_reader=True, format="fast_basic"), ]: table = BytesIO() table.write(b"a b") t = ascii.read(table, **kwargs) assert t.colnames == ["a", "b"] assert len(t) == 0 def test_path_object(): fpath = pathlib.Path("data/simple.txt") data = ascii.read(fpath) assert len(data) == 2 assert sorted(list(data.columns)) == ["test 1a", "test2", "test3", "test4"] assert data["test2"][1] == "hat2" def test_column_conversion_error(): """ Test that context information (upstream exception message) from column conversion error is provided. """ ipac = """\ | col0 | | double | 1 2 """ with pytest.raises(ValueError) as err: ascii.read(ipac, guess=False, format="ipac") assert "Column col0 failed to convert:" in str(err.value) with pytest.raises(ValueError) as err: ascii.read(["a b", "1 2"], guess=False, format="basic", converters={"a": []}) assert "no converters" in str(err.value) def test_non_C_locale_with_fast_reader(): """Test code that forces "C" locale while calling fast reader (#4364)""" current = locale.setlocale(locale.LC_ALL) try: if platform.system() == "Darwin": locale.setlocale(locale.LC_ALL, "fr_FR") else: locale.setlocale(locale.LC_ALL, "fr_FR.utf8") for fast_reader in ( True, False, {"use_fast_converter": False}, {"use_fast_converter": True}, ): t = ascii.read( ["a b", "1.5 2"], format="basic", guess=False, fast_reader=fast_reader ) assert t["a"].dtype.kind == "f" except locale.Error as e: pytest.skip(f"Locale error: {e}") finally: locale.setlocale(locale.LC_ALL, current) def test_no_units_for_char_columns(): """Test that a char column of a Table is assigned no unit and not a dimensionless unit.""" t1 = Table([["A"]], names="B") out = StringIO() ascii.write(t1, out, format="ipac") t2 = ascii.read(out.getvalue(), format="ipac", guess=False) assert t2["B"].unit is None def test_initial_column_fill_values(): """Regression test for #5336, #5338.""" class TestHeader(ascii.BasicHeader): def _set_cols_from_names(self): self.cols = [ascii.Column(name=x) for x in self.names] # Set some initial fill values for col in self.cols: col.fill_values = {"--": "0"} class Tester(ascii.Basic): header_class = TestHeader reader = ascii.get_reader(Reader=Tester) assert ( reader.read( """# Column definition is the first uncommented line # Default delimiter is the space character. a b c # Data starts after the header column definition, blank lines ignored -- 2 3 4 5 6 """ )["a"][0] is np.ma.masked ) def test_latex_no_trailing_backslash(): """ Test that latex/aastex file with no trailing backslash can be read. """ lines = r""" \begin{table} \begin{tabular}{ccc} a & b & c \\ 1 & 1.0 & c \\ % comment 3\% & 3.0 & e % comment \end{tabular} \end{table} """ dat = ascii.read(lines, format="latex") assert dat.colnames == ["a", "b", "c"] assert np.all(dat["a"] == ["1", r"3\%"]) assert np.all(dat["c"] == ["c", "e"]) def text_aastex_no_trailing_backslash(): lines = r""" \begin{deluxetable}{ccc} \tablehead{\colhead{a} & \colhead{b} & \colhead{c}} \startdata 1 & 1.0 & c \\ 2 & 2.0 & d \\ % comment 3\% & 3.0 & e % comment \enddata \end{deluxetable} """ dat = ascii.read(lines, format="aastex") assert dat.colnames == ["a", "b", "c"] assert np.all(dat["a"] == ["1", r"3\%"]) assert np.all(dat["c"] == ["c", "e"]) @pytest.mark.parametrize("encoding", ["utf8", "latin1", "cp1252"]) def test_read_with_encoding(tmp_path, encoding): data = {"commented_header": "# à b è \n 1 2 héllo", "csv": "à,b,è\n1,2,héllo"} testfile = tmp_path / "test.txt" for fmt, content in data.items(): with open(testfile, "w", encoding=encoding) as f: f.write(content) table = ascii.read(testfile, encoding=encoding) assert table.pformat() == [" à b è ", "--- --- -----", " 1 2 héllo"] for guess in (True, False): table = ascii.read( testfile, format=fmt, fast_reader=False, encoding=encoding, guess=guess ) assert table["è"].dtype.kind == "U" assert table.pformat() == [ " à b è ", "--- --- -----", " 1 2 héllo", ] def test_unsupported_read_with_encoding(): # Fast reader is not supported, make sure it raises an exception with pytest.raises(ascii.ParameterError): ascii.read( "data/simple3.txt", guess=False, fast_reader="force", encoding="latin1", format="fast_csv", ) def test_read_chunks_input_types(): """ Test chunked reading for different input types: file path, file object, and string input. """ fpath = "data/test5.dat" t1 = ascii.read( fpath, header_start=1, data_start=3, ) with open(fpath) as fd1, open(fpath) as fd2: for fp in (fpath, fd1, fd2.read()): t_gen = ascii.read( fp, header_start=1, data_start=3, guess=False, format="fast_basic", fast_reader={"chunk_size": 400, "chunk_generator": True}, ) ts = list(t_gen) for t in ts: for col, col1 in zip(t.columns.values(), t1.columns.values()): assert col.name == col1.name assert col.dtype.kind == col1.dtype.kind assert len(ts) == 4 t2 = table.vstack(ts) assert np.all(t1 == t2) with open(fpath) as fd1, open(fpath) as fd2: for fp in (fpath, fd1, fd2.read()): # Now read the full table in chunks t3 = ascii.read( fp, header_start=1, data_start=3, fast_reader={"chunk_size": 300} ) assert np.all(t1 == t3) @pytest.mark.parametrize("masked", [True, False]) def test_read_chunks_formats(masked): """ Test different supported formats for chunked reading. """ t1 = simple_table(size=102, cols=10, kinds="fS", masked=masked) for i, name in enumerate(t1.colnames): t1.rename_column(name, f"col{i + 1}") # TO DO commented_header does not currently work due to the special-cased # implementation of header parsing. for format in "tab", "csv", "no_header", "rdb", "basic": out = StringIO() ascii.write(t1, out, format=format) t_gen = ascii.read( out.getvalue(), format=format, fast_reader={"chunk_size": 400, "chunk_generator": True}, ) ts = list(t_gen) for t in ts: for col, col1 in zip(t.columns.values(), t1.columns.values()): assert col.name == col1.name assert col.dtype.kind == col1.dtype.kind assert len(ts) > 4 t2 = table.vstack(ts) assert np.all(t1 == t2) # Now read the full table in chunks t3 = ascii.read(out.getvalue(), format=format, fast_reader={"chunk_size": 400}) assert np.all(t1 == t3) def test_read_chunks_chunk_size_too_small(): fpath = "data/test5.dat" with pytest.raises(ValueError) as err: ascii.read(fpath, header_start=1, data_start=3, fast_reader={"chunk_size": 10}) assert "no newline found in chunk (chunk_size too small?)" in str(err.value) def test_read_chunks_table_changes(): """Column changes type or size between chunks. This also tests the case with no final newline. """ col = ["a b c"] + ["1.12334 xyz a"] * 50 + ["abcdefg 555 abc"] * 50 table = "\n".join(col) t1 = ascii.read(table, guess=False) t2 = ascii.read(table, fast_reader={"chunk_size": 100}) # This also confirms that the dtypes are exactly the same, i.e. # the string itemsizes are the same. assert np.all(t1 == t2) def test_read_non_ascii(): """Test that pure-Python reader is used in case the file contains non-ASCII characters in it. """ table = Table.read(["col1, col2", "\u2119, \u01b4", "1, 2"], format="csv") assert np.all(table["col1"] == ["\u2119", "1"]) assert np.all(table["col2"] == ["\u01b4", "2"]) @pytest.mark.parametrize("enable", [True, False, "force"]) def test_kwargs_dict_guess(enable): """Test that fast_reader dictionary is preserved through guessing sequence.""" # Fails for enable=(True, 'force') - #5578 ascii.read("a\tb\n 1\t2\n3\t 4.0", fast_reader=dict(enable=enable)) assert get_read_trace()[-1]["kwargs"]["Reader"] is ( ascii.Tab if (enable is False) else ascii.FastTab ) for k in get_read_trace(): if not k.get("status", "Disabled").startswith("Disabled"): assert k.get("kwargs").get("fast_reader").get("enable") is enable def _get_lines(rdb): lines = ["a a_2 a_1 a a"] if rdb: lines += ["N N N N N"] lines += ["1 2 3 4 5", "10 20 30 40 50"] if rdb: lines = ["\t".join(line.split()) for line in lines] return lines @pytest.mark.parametrize("rdb", [False, True]) @pytest.mark.parametrize("fast_reader", [False, "force"]) def test_deduplicate_names_basic(rdb, fast_reader): """Test that duplicate column names are successfully de-duplicated for the basic format. Skip the case of rdb=True and fast_reader='force' when selecting include_names, since that fails and is tested below. """ lines = _get_lines(rdb) dat = ascii.read(lines, fast_reader=fast_reader) assert dat.colnames == ["a", "a_2", "a_1", "a_3", "a_4"] assert len(dat) == 2 dat = ascii.read(lines, fast_reader=fast_reader, include_names=["a", "a_2", "a_3"]) assert len(dat) == 2 assert dat.colnames == ["a", "a_2", "a_3"] assert np.all(dat["a"] == [1, 10]) assert np.all(dat["a_2"] == [2, 20]) assert np.all(dat["a_3"] == [4, 40]) dat = ascii.read( lines, fast_reader=fast_reader, names=["b1", "b2", "b3", "b4", "b5"], include_names=["b1", "b2", "a_4", "b4"], ) assert len(dat) == 2 assert dat.colnames == ["b1", "b2", "b4"] assert np.all(dat["b1"] == [1, 10]) assert np.all(dat["b2"] == [2, 20]) assert np.all(dat["b4"] == [4, 40]) dat = ascii.read( lines, fast_reader=fast_reader, names=["b1", "b2", "b3", "b4", "b5"], exclude_names=["b3", "b5", "a_3", "a_4"], ) assert len(dat) == 2 assert dat.colnames == ["b1", "b2", "b4"] assert np.all(dat["b1"] == [1, 10]) assert np.all(dat["b2"] == [2, 20]) assert np.all(dat["b4"] == [4, 40]) def test_include_names_rdb_fast(): """Test that selecting column names via `include_names` works for the RDB format with fast reader. This is testing the fix for a bug identified in #9939. """ lines = _get_lines(True) lines[0] = "a\ta_2\ta_1\ta_3\ta_4" dat = ascii.read(lines, fast_reader="force", include_names=["a", "a_2", "a_3"]) assert len(dat) == 2 assert dat["a"].dtype == int assert dat["a_2"].dtype == int @pytest.mark.parametrize("fast_reader", [False, "force"]) def test_deduplicate_names_with_types(fast_reader): """Test that on selecting column names via `include_names` in the RDB format with different types and duplicate column names type assignment is correctly preserved. """ lines = _get_lines(True) lines[1] = "N\tN\tN\tS\tS" dat = ascii.read(lines, fast_reader=fast_reader, include_names=["a", "a_2", "a_3"]) assert len(dat) == 2 assert dat["a_2"].dtype.kind == "i" assert dat["a_3"].dtype.kind == "U" dat = ascii.read( lines, fast_reader=fast_reader, names=["b1", "b2", "b3", "b4", "b5"], include_names=["a1", "a_2", "b1", "b2", "b4"], ) assert len(dat) == 2 assert dat.colnames == ["b1", "b2", "b4"] assert dat["b2"].dtype.kind == "i" assert dat["b4"].dtype.kind == "U" @pytest.mark.parametrize("rdb", [False, True]) @pytest.mark.parametrize("fast_reader", [False, "force"]) def test_set_invalid_names(rdb, fast_reader): """ Test exceptions for invalid (duplicate or `None`) names specified via argument. """ lines = _get_lines(rdb) if rdb: fmt = "rdb" else: fmt = "basic" with pytest.raises(ValueError) as err: ascii.read( lines, fast_reader=fast_reader, format=fmt, guess=rdb, names=["b1", "b2", "b1", "b4", "b5"], ) assert "Duplicate column names" in str(err.value) with pytest.raises(TypeError) as err: ascii.read( lines, fast_reader=fast_reader, format=fmt, guess=rdb, names=["b1", "b2", "b1", None, None], ) assert "Cannot have None for column name" in str(err.value) def test_read_masked_bool(): txt = """\ col0 col1 1 1 0 2 True 3 "" 4 False 5 """ # Reading without converters returns col0 as a string dat = ascii.read(txt, format="basic") col = dat["col0"] assert isinstance(col, MaskedColumn) assert col.dtype.kind == "U" assert col[0] == "1" # Force col0 to be read as bool converters = {"col0": [convert_numpy(bool)]} dat = ascii.read(txt, format="basic", converters=converters) col = dat["col0"] assert isinstance(col, MaskedColumn) assert col.dtype.kind == "b" assert np.all(col.mask == [False, False, False, True, False]) assert np.all(col == [True, False, True, False, False]) def test_read_converters_wildcard(): """Test converters where the column name is specified with a wildcard. """ converters = {"F*": [ascii.convert_numpy(np.float32)]} t = ascii.read(["Fabc Iabc", "1 2"], converters=converters) assert np.issubdtype(t["Fabc"].dtype, np.float32) assert not np.issubdtype(t["Iabc"].dtype, np.float32) def test_read_converters_simplified(): """Test providing io.ascii read converters as type or dtypes instead of convert_numpy(type) outputs""" t = Table() t["a"] = [1, 2] t["b"] = [3.5, 4] t["c"] = ["True", "False"] t["d"] = ["true", "false"] # Looks kindof like boolean but actually a string t["e"] = [5, 6] out = StringIO() t.write(out, format="ascii.basic") converters = {"a": str, "e": np.float32} t2 = Table.read(out.getvalue(), format="ascii.basic", converters=converters) assert t2.pformat(show_dtype=True) == [ " a b c d e ", "str1 float64 str5 str5 float32", "---- ------- ----- ----- -------", " 1 3.5 True true 5.0", " 2 4.0 False false 6.0", ] converters = {"a": float, "*": [np.int64, float, bool, str]} t2 = Table.read(out.getvalue(), format="ascii.basic", converters=converters) assert t2.pformat_all(show_dtype=True) == [ " a b c d e ", "float64 float64 bool str5 int64", "------- ------- ----- ----- -----", " 1.0 3.5 True true 5", " 2.0 4.0 False false 6", ] # Test failures for converters in ( {"*": [int, 1, bool, str]}, # bad converter type # Tuple converter where 2nd element is not a subclass of NoType {"a": [(int, int)]}, # Tuple converter with 3 elements not 2 {"a": [(int, int, int)]}, ): with pytest.raises(ValueError, match="Error: invalid format for converters"): t2 = Table.read( out.getvalue(), format="ascii.basic", converters=converters, guess=False )
e7abdd9b52b454fe5e10e8392cae05d97f10023f21a52e694c619a5de07bf807
# Licensed under a 3-clause BSD style license - see LICENSE.rst import copy import os import pathlib from contextlib import nullcontext from io import StringIO from itertools import chain import numpy as np import pytest from astropy import table from astropy import units as u from astropy.io import ascii from astropy.table.table_helpers import simple_table from astropy.utils.compat.optional_deps import HAS_BS4 from astropy.utils.exceptions import AstropyWarning from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH from .common import setup_function, teardown_function # noqa: F401 test_defs = [ dict( kwargs=dict(), out="""\ ID XCENTER YCENTER MAG MERR MSKY NITER SHARPNESS CHI PIER PERROR 14 138.538 256.405 15.461 0.003 34.85955 4 -0.032 0.802 0 No_error 18 18.114 280.170 22.329 0.206 30.12784 4 -2.544 1.104 0 No_error """, ), dict( kwargs=dict(delimiter=None), out="""\ ID XCENTER YCENTER MAG MERR MSKY NITER SHARPNESS CHI PIER PERROR 14 138.538 256.405 15.461 0.003 34.85955 4 -0.032 0.802 0 No_error 18 18.114 280.170 22.329 0.206 30.12784 4 -2.544 1.104 0 No_error """, ), dict( kwargs=dict( formats={"XCENTER": "%12.1f", "YCENTER": "{0:.1f}"}, include_names=["XCENTER", "YCENTER"], strip_whitespace=False, ), out="""\ XCENTER YCENTER " 138.5" 256.4 " 18.1" 280.2 """, ), dict( kwargs=dict(Writer=ascii.Rdb, exclude_names=["CHI"]), out="""\ ID\tXCENTER\tYCENTER\tMAG\tMERR\tMSKY\tNITER\tSHARPNESS\tPIER\tPERROR N\tN\tN\tN\tN\tN\tN\tN\tN\tS 14\t138.538\t256.405\t15.461\t0.003\t34.85955\t4\t-0.032\t0\tNo_error 18\t18.114\t280.170\t22.329\t0.206\t30.12784\t4\t-2.544\t0\tNo_error """, ), dict( kwargs=dict(Writer=ascii.Tab), out="""\ ID\tXCENTER\tYCENTER\tMAG\tMERR\tMSKY\tNITER\tSHARPNESS\tCHI\tPIER\tPERROR 14\t138.538\t256.405\t15.461\t0.003\t34.85955\t4\t-0.032\t0.802\t0\tNo_error 18\t18.114\t280.170\t22.329\t0.206\t30.12784\t4\t-2.544\t1.104\t0\tNo_error """, ), dict( kwargs=dict(Writer=ascii.Csv), out="""\ ID,XCENTER,YCENTER,MAG,MERR,MSKY,NITER,SHARPNESS,CHI,PIER,PERROR 14,138.538,256.405,15.461,0.003,34.85955,4,-0.032,0.802,0,No_error 18,18.114,280.170,22.329,0.206,30.12784,4,-2.544,1.104,0,No_error """, ), dict( kwargs=dict(Writer=ascii.NoHeader), out="""\ 14 138.538 256.405 15.461 0.003 34.85955 4 -0.032 0.802 0 No_error 18 18.114 280.170 22.329 0.206 30.12784 4 -2.544 1.104 0 No_error """, ), dict( kwargs=dict(Writer=ascii.CommentedHeader), out="""\ # ID XCENTER YCENTER MAG MERR MSKY NITER SHARPNESS CHI PIER PERROR 14 138.538 256.405 15.461 0.003 34.85955 4 -0.032 0.802 0 No_error 18 18.114 280.170 22.329 0.206 30.12784 4 -2.544 1.104 0 No_error """, ), dict( kwargs=dict(Writer=ascii.CommentedHeader, comment="&"), out="""\ &ID XCENTER YCENTER MAG MERR MSKY NITER SHARPNESS CHI PIER PERROR 14 138.538 256.405 15.461 0.003 34.85955 4 -0.032 0.802 0 No_error 18 18.114 280.170 22.329 0.206 30.12784 4 -2.544 1.104 0 No_error """, ), dict( kwargs=dict(Writer=ascii.Latex), out="""\ \\begin{table} \\begin{tabular}{ccccccccccc} ID & XCENTER & YCENTER & MAG & MERR & MSKY & NITER & SHARPNESS & CHI & PIER & PERROR \\\\ & pixels & pixels & magnitudes & magnitudes & counts & & & & & perrors \\\\ 14 & 138.538 & 256.405 & 15.461 & 0.003 & 34.85955 & 4 & -0.032 & 0.802 & 0 & No_error \\\\ 18 & 18.114 & 280.170 & 22.329 & 0.206 & 30.12784 & 4 & -2.544 & 1.104 & 0 & No_error \\\\ \\end{tabular} \\end{table} """, ), dict( kwargs=dict(Writer=ascii.AASTex), out="""\ \\begin{deluxetable}{ccccccccccc} \\tablehead{\\colhead{ID} & \\colhead{XCENTER} & \\colhead{YCENTER} & \\colhead{MAG} & \\colhead{MERR} & \\colhead{MSKY} & \\colhead{NITER} & \\colhead{SHARPNESS} & \\colhead{CHI} & \\colhead{PIER} & \\colhead{PERROR}\\\\ \\colhead{ } & \\colhead{pixels} & \\colhead{pixels} & \\colhead{magnitudes} & \\colhead{magnitudes} & \\colhead{counts} & \\colhead{ } & \\colhead{ } & \\colhead{ } & \\colhead{ } & \\colhead{perrors}} \\startdata 14 & 138.538 & 256.405 & 15.461 & 0.003 & 34.85955 & 4 & -0.032 & 0.802 & 0 & No_error \\\\ 18 & 18.114 & 280.170 & 22.329 & 0.206 & 30.12784 & 4 & -2.544 & 1.104 & 0 & No_error \\enddata \\end{deluxetable} """, ), dict( kwargs=dict( Writer=ascii.AASTex, caption="Mag values \\label{tab1}", latexdict={ "units": {"MAG": "[mag]", "XCENTER": "[pixel]"}, "tabletype": "deluxetable*", "tablealign": "htpb", }, ), out="""\ \\begin{deluxetable*}{ccccccccccc}[htpb] \\tablecaption{Mag values \\label{tab1}} \\tablehead{\\colhead{ID} & \\colhead{XCENTER} & \\colhead{YCENTER} & \\colhead{MAG} & \\colhead{MERR} & \\colhead{MSKY} & \\colhead{NITER} & \\colhead{SHARPNESS} & \\colhead{CHI} & \\colhead{PIER} & \\colhead{PERROR}\\\\ \\colhead{ } & \\colhead{[pixel]} & \\colhead{pixels} & \\colhead{[mag]} & \\colhead{magnitudes} & \\colhead{counts} & \\colhead{ } & \\colhead{ } & \\colhead{ } & \\colhead{ } & \\colhead{perrors}} \\startdata 14 & 138.538 & 256.405 & 15.461 & 0.003 & 34.85955 & 4 & -0.032 & 0.802 & 0 & No_error \\\\ 18 & 18.114 & 280.170 & 22.329 & 0.206 & 30.12784 & 4 & -2.544 & 1.104 & 0 & No_error \\enddata \\end{deluxetable*} """, ), dict( kwargs=dict( Writer=ascii.Latex, caption="Mag values \\label{tab1}", latexdict={ "preamble": "\\begin{center}", "tablefoot": "\\end{center}", "data_end": ["\\hline", "\\hline"], "units": {"MAG": "[mag]", "XCENTER": "[pixel]"}, "tabletype": "table*", "tablealign": "h", }, col_align="|lcccccccccc|", ), out="""\ \\begin{table*}[h] \\begin{center} \\caption{Mag values \\label{tab1}} \\begin{tabular}{|lcccccccccc|} ID & XCENTER & YCENTER & MAG & MERR & MSKY & NITER & SHARPNESS & CHI & PIER & PERROR \\\\ & [pixel] & pixels & [mag] & magnitudes & counts & & & & & perrors \\\\ 14 & 138.538 & 256.405 & 15.461 & 0.003 & 34.85955 & 4 & -0.032 & 0.802 & 0 & No_error \\\\ 18 & 18.114 & 280.170 & 22.329 & 0.206 & 30.12784 & 4 & -2.544 & 1.104 & 0 & No_error \\\\ \\hline \\hline \\end{tabular} \\end{center} \\end{table*} """, ), dict( kwargs=dict(Writer=ascii.Latex, latexdict=ascii.latexdicts["template"]), out="""\ \\begin{tabletype}[tablealign] preamble \\caption{caption} \\begin{tabular}{col_align} header_start ID & XCENTER & YCENTER & MAG & MERR & MSKY & NITER & SHARPNESS & CHI & PIER & PERROR \\\\ & pixels & pixels & magnitudes & magnitudes & counts & & & & & perrors \\\\ header_end data_start 14 & 138.538 & 256.405 & 15.461 & 0.003 & 34.85955 & 4 & -0.032 & 0.802 & 0 & No_error \\\\ 18 & 18.114 & 280.170 & 22.329 & 0.206 & 30.12784 & 4 & -2.544 & 1.104 & 0 & No_error \\\\ data_end \\end{tabular} tablefoot \\end{tabletype} """, ), dict( kwargs=dict(Writer=ascii.Latex, latexdict={"tabletype": None}), out="""\ \\begin{tabular}{ccccccccccc} ID & XCENTER & YCENTER & MAG & MERR & MSKY & NITER & SHARPNESS & CHI & PIER & PERROR \\\\ & pixels & pixels & magnitudes & magnitudes & counts & & & & & perrors \\\\ 14 & 138.538 & 256.405 & 15.461 & 0.003 & 34.85955 & 4 & -0.032 & 0.802 & 0 & No_error \\\\ 18 & 18.114 & 280.170 & 22.329 & 0.206 & 30.12784 & 4 & -2.544 & 1.104 & 0 & No_error \\\\ \\end{tabular} """, ), dict( kwargs=dict( Writer=ascii.HTML, htmldict={"css": "table,th,td{border:1px solid black;"} ), out="""\ <html> <head> <meta charset="utf-8"/> <meta content="text/html;charset=UTF-8" http-equiv="Content-type"/> <style> table,th,td{border:1px solid black; </style> </head> <body> <table> <thead> <tr> <th>ID</th> <th>XCENTER</th> <th>YCENTER</th> <th>MAG</th> <th>MERR</th> <th>MSKY</th> <th>NITER</th> <th>SHARPNESS</th> <th>CHI</th> <th>PIER</th> <th>PERROR</th> </tr> </thead> <tr> <td>14</td> <td>138.538</td> <td>256.405</td> <td>15.461</td> <td>0.003</td> <td>34.85955</td> <td>4</td> <td>-0.032</td> <td>0.802</td> <td>0</td> <td>No_error</td> </tr> <tr> <td>18</td> <td>18.114</td> <td>280.170</td> <td>22.329</td> <td>0.206</td> <td>30.12784</td> <td>4</td> <td>-2.544</td> <td>1.104</td> <td>0</td> <td>No_error</td> </tr> </table> </body> </html> """, ), dict( kwargs=dict(Writer=ascii.Ipac), out="""\ \\MERGERAD='INDEF' \\IRAF='NOAO/IRAFV2.10EXPORT' \\USER='' \\HOST='tucana' \\DATE='05-28-93' \\TIME='14:46:13' \\PACKAGE='daophot' \\TASK='nstar' \\IMAGE='test' \\GRPFILE='test.psg.1' \\PSFIMAGE='test.psf.1' \\NSTARFILE='test.nst.1' \\REJFILE='"hello world"' \\SCALE='1.' \\DATAMIN='50.' \\DATAMAX='24500.' \\GAIN='1.' \\READNOISE='0.' \\OTIME='00:07:59.0' \\XAIRMASS='1.238106' \\IFILTER='V' \\RECENTER='yes' \\FITSKY='no' \\PSFMAG='16.594' \\PSFRAD='5.' \\FITRAD='3.' \\MAXITER='50' \\MAXGROUP='60' \\FLATERROR='0.75' \\PROFERROR='5.' \\CLIPEXP='6' \\CLIPRANGE='2.5' | ID| XCENTER| YCENTER| MAG| MERR| MSKY| NITER| SHARPNESS| CHI| PIER| PERROR| | long| double| double| double| double| double| long| double| double| long| char| | | pixels| pixels| magnitudes| magnitudes| counts| | | | | perrors| | null| null| null| null| null| null| null| null| null| null| null| 14 138.538 256.405 15.461 0.003 34.85955 4 -0.032 0.802 0 No_error 18 18.114 280.170 22.329 0.206 30.12784 4 -2.544 1.104 0 No_error """, ), ] test_defs_no_data = [ dict( kwargs=dict(Writer=ascii.Ipac), out="""\ \\ This is an example of a valid comment. \\ The 2nd data line is used to verify the exact column parsing \\ (unclear if this is a valid for the IPAC format) \\catalog='sao' \\date='Wed Sp 20 09:48:36 1995' \\mykeyword='Another way for defining keyvalue string' | ra| dec| sai| v2|sptype| |double|double|long|double| char| | unit| unit|unit| unit| ergs| | null| null|null| null| null| """, ), ] tab_to_fill = ["a b c", "1 2 3", "1 1 3"] test_defs_fill_value = [ dict( kwargs=dict(), out="""\ a b c 1 2 3 1 1 3 """, ), dict( kwargs=dict(fill_values=("1", "w")), out="""\ a b c w 2 3 w w 3 """, ), dict( kwargs=dict(fill_values=("1", "w", "b")), out="""\ a b c 1 2 3 1 w 3 """, ), dict( kwargs=dict(fill_values=("1", "w"), fill_include_names=["b"]), out="""\ a b c 1 2 3 1 w 3 """, ), dict( kwargs=dict(fill_values=("1", "w"), fill_exclude_names=["a"]), out="""\ a b c 1 2 3 1 w 3 """, ), dict( kwargs=dict( fill_values=("1", "w"), fill_include_names=["a"], fill_exclude_names=["a", "b"], ), out="""\ a b c 1 2 3 1 1 3 """, ), dict( kwargs=dict(fill_values=[("1", "w")], formats={"a": "%4.2f"}), out="""\ a b c 1.00 2 3 1.00 w 3 """, ), ] test_def_masked_fill_value = [ dict( kwargs=dict(), out="""\ a b c "" 2 3 1 1 "" """, ), dict( kwargs=dict(fill_values=[("1", "w"), (ascii.masked, "X")]), out="""\ a b c X 2 3 w w X """, ), dict( kwargs=dict( fill_values=[("1", "w"), (ascii.masked, "XXX")], formats={"a": "%4.1f"} ), out="""\ a b c XXX 2 3 1.0 w XXX """, ), dict( kwargs=dict(Writer=ascii.Csv), out="""\ a,b,c ,2,3 1,1, """, ), ] @pytest.fixture def home_is_tmpdir(monkeypatch, tmp_path): """ Pytest fixture to run a test case with tilde-prefixed paths. In the tilde-path case, environment variables are temporarily modified so that '~' resolves to the temp directory. """ # For Unix monkeypatch.setenv("HOME", str(tmp_path)) # For Windows monkeypatch.setenv("USERPROFILE", str(tmp_path)) def check_write_table(test_def, table, fast_writer, out=None): if out is None: out = StringIO() try: ascii.write(table, out, fast_writer=fast_writer, **test_def["kwargs"]) except ValueError as e: # if format doesn't have a fast writer, ignore if "not in the list of formats with fast writers" not in str(e.value): raise e return if isinstance(out, StringIO): # Output went to a buffer actual = out.getvalue() else: # Output went to a file if str(out).startswith("~"): # Ensure a file hasn't been accidentally written to a literal tilde # path assert not os.path.exists(out) out = os.path.expanduser(out) assert os.path.exists(out) with open(out) as f: actual = f.read() os.remove(out) print(f"Expected:\n{test_def['out']}") print(f"Actual:\n{actual}") assert [x.strip() for x in actual.strip().splitlines()] == [ x.strip() for x in test_def["out"].strip().splitlines() ] def check_write_table_via_table(test_def, table, fast_writer, out=None): if out is None: out = StringIO() test_def = copy.deepcopy(test_def) if "Writer" in test_def["kwargs"]: format = f"ascii.{test_def['kwargs']['Writer']._format_name}" del test_def["kwargs"]["Writer"] else: format = "ascii" try: table.write(out, format=format, fast_writer=fast_writer, **test_def["kwargs"]) except ValueError as e: # if format doesn't have a fast writer, ignore if "not in the list of formats with fast writers" not in str(e.value): raise e return if isinstance(out, StringIO): # Output went to a buffer actual = out.getvalue() else: # Output went to a file if str(out).startswith("~"): # Ensure a file hasn't been accidentally written to a literal tilde # path assert not os.path.exists(out) out = os.path.expanduser(out) assert os.path.exists(out) with open(out) as f: actual = f.read() os.remove(out) print(f"Expected:\n{test_def['out']}") print(f"Actual:\n{actual}") assert [x.strip() for x in actual.strip().splitlines()] == [ x.strip() for x in test_def["out"].strip().splitlines() ] @pytest.mark.parametrize("fast_writer", [True, False]) @pytest.mark.parametrize( "path_format", ["buffer", "plain", "tilde-str", "tilde-pathlib"] ) def test_write_table(fast_writer, tmp_path, home_is_tmpdir, path_format): table = ascii.get_reader(Reader=ascii.Daophot) data = table.read("data/daophot.dat") if path_format == "buffer": out_name = None elif path_format == "plain": out_name = tmp_path / "table" elif path_format == "tilde-str": out_name = os.path.join("~", "table") else: out_name = pathlib.Path("~", "table") for test_def in test_defs: check_write_table(test_def, data, fast_writer, out=out_name) check_write_table_via_table(test_def, data, fast_writer, out=out_name) @pytest.mark.parametrize("fast_writer", [True, False]) def test_write_fill_values(fast_writer): data = ascii.read(tab_to_fill) for test_def in test_defs_fill_value: check_write_table(test_def, data, fast_writer) @pytest.mark.parametrize("fast_writer", [True, False]) def test_write_fill_masked_different(fast_writer): """see discussion in #2255""" data = ascii.read(tab_to_fill) data = table.Table(data, masked=True) data["a"].mask = [True, False] data["c"].mask = [False, True] for test_def in test_def_masked_fill_value: check_write_table(test_def, data, fast_writer) @pytest.mark.parametrize("fast_writer", [True, False]) def test_write_no_data_ipac(fast_writer): """Write an IPAC table that contains no data.""" table = ascii.get_reader(Reader=ascii.Ipac) data = table.read("data/no_data_ipac.dat") for test_def in test_defs_no_data: check_write_table(test_def, data, fast_writer) check_write_table_via_table(test_def, data, fast_writer) def test_write_invalid_toplevel_meta_ipac(): """Write an IPAC table that contains no data but has invalid (incorrectly specified) metadata stored in the top-level metadata and therefore should raise a warning, and check that the warning has been raised""" table = ascii.get_reader(Reader=ascii.Ipac) data = table.read("data/no_data_ipac.dat") data.meta["blah"] = "extra" out = StringIO() with pytest.warns(AstropyWarning, match=r".*were not written.*") as warn: data.write(out, format="ascii.ipac") assert len(warn) == 1 def test_write_invalid_keyword_meta_ipac(): """Write an IPAC table that contains no data but has invalid (incorrectly specified) metadata stored appropriately in the ``keywords`` section of the metadata but with invalid format and therefore should raise a warning, and check that the warning has been raised""" table = ascii.get_reader(Reader=ascii.Ipac) data = table.read("data/no_data_ipac.dat") data.meta["keywords"]["blah"] = "invalid" out = StringIO() with pytest.warns(AstropyWarning, match=r".*has been skipped.*") as warn: data.write(out, format="ascii.ipac") assert len(warn) == 1 def test_write_valid_meta_ipac(): """Write an IPAC table that contains no data and has *correctly* specified metadata. No warnings should be issued""" table = ascii.get_reader(Reader=ascii.Ipac) data = table.read("data/no_data_ipac.dat") data.meta["keywords"]["blah"] = {"value": "invalid"} out = StringIO() data.write(out, format="ascii.ipac") @pytest.mark.parametrize("fast_writer", [True, False]) def test_write_comments(fast_writer): """Write comments in output originally read by io.ascii.""" data = ascii.read("#c1\n # c2\t\na,b,c\n# c3\n1,2,3") out = StringIO() ascii.write(data, out, format="basic", fast_writer=fast_writer) expected = ["# c1", "# c2", "# c3", "a b c", "1 2 3"] assert out.getvalue().splitlines() == expected # header comes before comments for commented-header out = StringIO() ascii.write(data, out, format="commented_header", fast_writer=fast_writer) expected = ["# a b c", "# c1", "# c2", "# c3", "1 2 3"] assert out.getvalue().splitlines() == expected # setting comment=False should disable comment writing out = StringIO() ascii.write(data, out, format="basic", comment=False, fast_writer=fast_writer) expected = ["a b c", "1 2 3"] assert out.getvalue().splitlines() == expected @pytest.mark.parametrize("fast_writer", [True, False]) @pytest.mark.parametrize("fmt", ["%0.1f", ".1f", "0.1f", "{0:0.1f}"]) def test_write_format(fast_writer, fmt): """Check different formats for a column.""" data = ascii.read("#c1\n # c2\t\na,b,c\n# c3\n1.11,2.22,3.33") out = StringIO() expected = ["# c1", "# c2", "# c3", "a b c", "1.1 2.22 3.33"] data["a"].format = fmt ascii.write(data, out, format="basic", fast_writer=fast_writer) assert out.getvalue().splitlines() == expected @pytest.mark.parametrize("fast_writer", [True, False]) def test_strip_names(fast_writer): """Names should be stripped of whitespace by default.""" data = table.Table([[1], [2], [3]], names=(" A", "B ", " C ")) out = StringIO() ascii.write(data, out, format="csv", fast_writer=fast_writer) assert out.getvalue().splitlines()[0] == "A,B,C" def test_latex_units(): """ Check to make sure that Latex and AASTex writers attempt to fall back on the **unit** attribute of **Column** if the supplied **latexdict** does not specify units. """ t = table.Table( [ table.Column(name="date", data=["a", "b"]), table.Column(name="NUV exp.time", data=[1, 2]), ] ) latexdict = copy.deepcopy(ascii.latexdicts["AA"]) latexdict["units"] = {"NUV exp.time": "s"} out = StringIO() expected = """\ \\begin{table}{cc} \\tablehead{\\colhead{date} & \\colhead{NUV exp.time}\\\\ \\colhead{ } & \\colhead{s}} \\startdata a & 1 \\\\ b & 2 \\enddata \\end{table} """.replace( "\n", os.linesep ) ascii.write(t, out, format="aastex", latexdict=latexdict) assert out.getvalue() == expected # use unit attribute instead t["NUV exp.time"].unit = u.s t["date"].unit = u.yr out = StringIO() ascii.write(t, out, format="aastex", latexdict=ascii.latexdicts["AA"]) assert out.getvalue() == expected.replace( "colhead{s}", r"colhead{$\mathrm{s}$}" ).replace("colhead{ }", r"colhead{$\mathrm{yr}$}") @pytest.mark.parametrize("fast_writer", [True, False]) def test_commented_header_comments(fast_writer): """ Test the fix for #3562 with confusing exception using comment=False for the commented_header writer. """ t = table.Table([[1, 2]]) with pytest.raises(ValueError) as err: out = StringIO() ascii.write( t, out, format="commented_header", comment=False, fast_writer=fast_writer ) assert "for the commented_header writer you must supply a string" in str(err.value) @pytest.mark.parametrize("fast_writer", [True, False]) def test_byte_string_output(fast_writer): """ Test the fix for #4350 where byte strings were output with a leading `b` on Py3. """ t = table.Table([["Hello", "World"]], dtype=["S10"]) out = StringIO() ascii.write(t, out, fast_writer=fast_writer) assert out.getvalue().splitlines() == ["col0", "Hello", "World"] @pytest.mark.parametrize( "names, include_names, exclude_names, formats, issues_warning", [ (["x", "y"], ["x", "y"], ["x"], {"x": "%d", "y": "%f"}, True), (["x", "y"], ["x", "y"], ["y"], {"x": "%d"}, False), (["x", "y"], ["x", "y"], [], {"p": "%d", "q": "%f"}, True), (["x", "y"], ["x", "y"], [], {"z": "%f"}, True), (["x", "y"], ["x", "y"], [], {"x": "%d"}, False), (["x", "y"], ["x", "y"], [], {"p": "%d", "y": "%f"}, True), (["x", "y"], ["x", "y"], [], {}, False), ], ) def test_names_with_formats( names, include_names, exclude_names, formats, issues_warning ): """Test for #4508.""" t = table.Table([[1, 2, 3], [4.1, 5.2, 6.3]]) out = StringIO() if issues_warning: ctx = pytest.warns(AstropyWarning) else: ctx = nullcontext() with ctx as warn: ascii.write( t, out, names=names, include_names=include_names, exclude_names=exclude_names, formats=formats, ) if issues_warning: assert len(warn) == 1 @pytest.mark.parametrize( "formats, issues_warning", [ ({"p": "%d", "y": "%f"}, True), ({"x": "%d", "y": "%f"}, True), ({"z": "%f"}, True), ({}, False), ], ) def test_columns_names_with_formats(formats, issues_warning): """Test the fix for #4508.""" t = table.Table([[1, 2, 3], [4.1, 5.2, 6.3]]) out = StringIO() if issues_warning: ctx = pytest.warns(AstropyWarning) else: ctx = nullcontext() with ctx as warn: ascii.write(t, out, formats=formats) if issues_warning: assert len(warn) == 1 @pytest.mark.parametrize("fast_writer", [True, False]) def test_write_quoted_empty_field(fast_writer): """ Test the fix for #4350 where byte strings were output with a leading `b` on Py3. """ t = table.Table([["Hello", ""], ["", ""]], dtype=["S10", "S10"]) out = StringIO() ascii.write(t, out, fast_writer=fast_writer) assert out.getvalue().splitlines() == ["col0 col1", 'Hello ""', '"" ""'] out = StringIO() ascii.write(t, out, fast_writer=fast_writer, delimiter=",") assert out.getvalue().splitlines() == ["col0,col1", "Hello,", ","] @pytest.mark.parametrize("fast_writer", [True, False]) def test_write_empty_table(fast_writer): """Test writing empty table #8275.""" t = table.Table([[]], dtype=["S2"]) out = StringIO() ascii.write(t, out, fast_writer=fast_writer) assert out.getvalue().splitlines() == ["col0"] @pytest.mark.parametrize( "format", ["ascii", "csv", "html", "latex", "ascii.fixed_width", "html"] ) @pytest.mark.parametrize("fast_writer", [True, False]) @pytest.mark.parametrize("path_format", ["plain", "tilde-str", "tilde-pathlib"]) def test_write_overwrite_ascii( format, fast_writer, tmp_path, home_is_tmpdir, path_format ): """Test overwrite argument for various ASCII writers""" true_filename = tmp_path / "table-tmp.dat" if path_format == "plain": filename = true_filename elif path_format == "tilde-str": filename = os.path.join("~", "table-tmp.dat") else: filename = pathlib.Path("~", "table-tmp.dat") with open(true_filename, "w"): # create empty file pass t = table.Table([["Hello", ""], ["", ""]], dtype=["S10", "S10"]) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): t.write(filename, format=format, fast_writer=fast_writer) t.write(filename, overwrite=True, format=format, fast_writer=fast_writer) # If the output is a file object, overwrite is ignored with open(true_filename, "w") as fp: t.write(fp, overwrite=False, format=format, fast_writer=fast_writer) t.write(fp, overwrite=True, format=format, fast_writer=fast_writer) if "tilde" in path_format: # Ensure no files have been accidentally written to a literal tilde path assert not os.path.exists(filename) fmt_name_classes = list( chain(ascii.core.FAST_CLASSES.items(), ascii.core.FORMAT_CLASSES.items()) ) @pytest.mark.parametrize("fmt_name_class", fmt_name_classes) def test_roundtrip_masked(fmt_name_class): """ Round trip a simple masked table through every writable format and confirm that reading back gives the same result. """ fmt_name, fmt_cls = fmt_name_class if not getattr(fmt_cls, "_io_registry_can_write", True): return # Skip tests for fixed_width or HTML without bs4 if (fmt_name == "html" and not HAS_BS4) or fmt_name == "fixed_width": return if "qdp" in fmt_name: # QDP tables are for numeric values only t = simple_table(masked=True, kinds=["f", "i"]) else: t = simple_table(masked=True) out = StringIO() fast = fmt_name in ascii.core.FAST_CLASSES try: ascii.write(t, out, format=fmt_name, fast_writer=fast) except ImportError: # Some failed dependency, skip test return # No-header formats need to be told the column names kwargs = {"names": t.colnames} if "no_header" in fmt_name else {} if "qdp" in fmt_name: kwargs.update({"table_id": 0, "names": t.colnames}) t2 = ascii.read( out.getvalue(), format=fmt_name, fast_reader=fast, guess=False, **kwargs ) assert t.colnames == t2.colnames for col, col2 in zip(t.itercols(), t2.itercols()): assert col.dtype.kind == col2.dtype.kind assert np.all(col == col2) @pytest.mark.parametrize("fast_writer", [True, False]) def test_write_newlines(fast_writer, tmp_path): # Regression test for https://github.com/astropy/astropy/issues/5126 # On windows, when writing to a filename (not e.g. StringIO), newlines were # \r\r\n instead of \r\n. filename = tmp_path / "test" t = table.Table([["a", "b", "c"]], names=["col"]) ascii.write(t, filename, fast_writer=fast_writer) with open(filename, newline="") as f: content = f.read() assert content == os.linesep.join(["col", "a", "b", "c"]) + os.linesep @pytest.mark.parametrize("fast_writer", [True, False]) def test_write_csv_with_comments(fast_writer): """ Test fix for #7357 where writing a Table with comments to 'csv' fails with a cryptic message. The comments are dropped by default, but when comment='#' is supplied they are still written. """ out = StringIO() t = table.Table([[1, 2], [3, 4]], names=["a", "b"]) t.meta["comments"] = ["hello"] ascii.write(t, out, format="csv", fast_writer=fast_writer) assert out.getvalue().splitlines() == ["a,b", "1,3", "2,4"] out = StringIO() ascii.write(t, out, format="csv", fast_writer=fast_writer, comment="#") assert out.getvalue().splitlines() == ["#hello", "a,b", "1,3", "2,4"] @pytest.mark.parametrize("fast_writer", [True, False]) def test_write_formatted_mixin(fast_writer): """ Test fix for #8680 where writing a QTable with a quantity mixin generates an exception if a format is specified. """ out = StringIO() t = table.QTable([[1, 2], [1, 2] * u.m], names=["a", "b"]) ascii.write(t, out, fast_writer=fast_writer, formats={"a": "%02d", "b": "%.2f"}) assert out.getvalue().splitlines() == ["a b", "01 1.00", "02 2.00"] def test_validate_write_kwargs(): out = StringIO() t = table.QTable([[1, 2], [1, 2]], names=["a", "b"]) with pytest.raises( TypeError, match=r"write\(\) argument 'fast_writer' must be a " r"\(<class 'bool'>, <class 'str'>\) object, " r"got <class 'int'> instead", ): ascii.write(t, out, fast_writer=12) @pytest.mark.parametrize("fmt_name_class", fmt_name_classes) def test_multidim_column_error(fmt_name_class): """ Test that trying to write a multidim column fails in every format except ECSV. """ fmt_name, fmt_cls = fmt_name_class if not getattr(fmt_cls, "_io_registry_can_write", True): return # Skip tests for ecsv or HTML without bs4. See the comment in latex.py # Latex class where max_ndim = None is defined regarding latex and aastex. if (fmt_name == "html" and not HAS_BS4) or fmt_name in ("ecsv", "latex", "aastex"): return out = StringIO() t = table.Table() t["a"] = np.arange(16).reshape(2, 2, 2, 2) t["b"] = [1, 2] fast = fmt_name in ascii.core.FAST_CLASSES with pytest.raises(ValueError, match=r"column\(s\) with dimension"): ascii.write(t, out, format=fmt_name, fast_writer=fast) @pytest.mark.parametrize("fast_writer", [True, False]) def test_write_as_columns(fast_writer): """ Test that writing a set of columns also roundtrips (as long as the table does not have metadata, etc.) """ # Use masked in case that makes it more difficult. data = ascii.read(tab_to_fill) data = table.Table(data, masked=True) data["a"].mask = [True, False] data["c"].mask = [False, True] data = list(data.columns.values()) for test_def in test_def_masked_fill_value: check_write_table(test_def, data, fast_writer)
edde1b9053da08606448cd6984edc26f21894b5fb9231d2b61a8eccf87cccbb3
# Licensed under a 3-clause BSD style license - see LICENSE.rst import numpy as np import pytest from astropy.table import Column, Table from astropy.table.table_helpers import simple_table from astropy.utils.compat.optional_deps import HAS_BS4 from astropy.utils.data import get_pkg_data_filename files = [ "data/cds.dat", "data/ipac.dat", "data/daophot.dat", "data/latex1.tex", "data/simple_csv.csv", ] if HAS_BS4: files.append("data/html.html") @pytest.mark.parametrize("filename", files) def test_read_generic(filename): Table.read(get_pkg_data_filename(filename), format="ascii") def test_write_generic(tmp_path): t = Table() t.add_column(Column(name="a", data=[1, 2, 3])) t.add_column(Column(name="b", data=["a", "b", "c"])) t.write(tmp_path / "test", format="ascii") def test_read_ipac(): Table.read(get_pkg_data_filename("data/ipac.dat"), format="ipac") def test_read_cds(): Table.read(get_pkg_data_filename("data/cds.dat"), format="cds") def test_read_dapphot(): Table.read(get_pkg_data_filename("data/daophot.dat"), format="daophot") def test_read_latex(): Table.read(get_pkg_data_filename("data/latex1.tex"), format="latex") def test_read_latex_noformat(): Table.read(get_pkg_data_filename("data/latex1.tex")) def test_write_latex(tmp_path): t = Table() t.add_column(Column(name="a", data=[1, 2, 3])) t.add_column(Column(name="b", data=["a", "b", "c"])) path = tmp_path / "data.tex" t.write(path, format="latex") def test_write_latex_noformat(tmp_path): t = Table() t.add_column(Column(name="a", data=[1, 2, 3])) t.add_column(Column(name="b", data=["a", "b", "c"])) path = tmp_path / "data.tex" t.write(path) @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_read_html(): Table.read(get_pkg_data_filename("data/html.html"), format="html") @pytest.mark.skipif(not HAS_BS4, reason="requires BeautifulSoup4") def test_read_html_noformat(): Table.read(get_pkg_data_filename("data/html.html")) def test_write_html(tmp_path): t = Table() t.add_column(Column(name="a", data=[1, 2, 3])) t.add_column(Column(name="b", data=["a", "b", "c"])) path = tmp_path / "data.html" t.write(path, format="html") def test_write_html_noformat(tmp_path): t = Table() t.add_column(Column(name="a", data=[1, 2, 3])) t.add_column(Column(name="b", data=["a", "b", "c"])) path = tmp_path / "data.html" t.write(path) def test_read_rdb(): Table.read(get_pkg_data_filename("data/short.rdb"), format="rdb") def test_read_rdb_noformat(): Table.read(get_pkg_data_filename("data/short.rdb")) def test_write_rdb(tmp_path): t = Table() t.add_column(Column(name="a", data=[1, 2, 3])) t.add_column(Column(name="b", data=["a", "b", "c"])) path = tmp_path / "data.rdb" t.write(path, format="rdb") def test_write_rdb_noformat(tmp_path): t = Table() t.add_column(Column(name="a", data=[1, 2, 3])) t.add_column(Column(name="b", data=["a", "b", "c"])) path = tmp_path / "data.rdb" t.write(path) def test_read_csv(): """If properly registered, filename should be sufficient to specify format #3189 """ Table.read(get_pkg_data_filename("data/simple_csv.csv")) def test_write_csv(tmp_path): """If properly registered, filename should be sufficient to specify format #3189 """ t = Table() t.add_column(Column(name="a", data=[1, 2, 3])) t.add_column(Column(name="b", data=["a", "b", "c"])) path = tmp_path / "data.csv" t.write(path) def test_auto_identify_ecsv(tmp_path): tbl = simple_table() tmpfile = tmp_path / "tmpFile.ecsv" tbl.write(tmpfile) tbl2 = Table.read(tmpfile) assert np.all(tbl == tbl2)
112fd09b3565277668cb62fd50776248a2a899e0a7c07f485a41d40204a8d7e7
# Licensed under a 3-clause BSD style license - see LICENSE.rst import numpy as np import pytest from astropy import units as u from astropy.io import ascii from .common import ( # noqa: F401 assert_almost_equal, assert_equal, setup_function, teardown_function, ) def read_table1(readme, data): reader = ascii.Cds(readme) return reader.read(data) def read_table2(readme, data): reader = ascii.get_reader(Reader=ascii.Cds, readme=readme) reader.outputter = ascii.TableOutputter() return reader.read(data) def read_table3(readme, data): return ascii.read(data, readme=readme) def test_description(): readme = "data/cds/description/ReadMe" data = "data/cds/description/table.dat" for read_table in (read_table1, read_table2, read_table3): table = read_table(readme, data) assert_equal(len(table), 2) assert_equal(table["Cluster"].description, "Cluster name") assert_equal(table["Star"].description, "") assert_equal(table["Wave"].description, "wave? Wavelength in Angstroms") assert_equal(table["El"].description, "a") assert_equal( table["ion"].description, "- Ionization stage (1 for neutral element)" ) assert_equal(table["EW"].description, "Equivalent width (in mA)") assert_equal( table["Q"].description, "DAOSPEC quality parameter Q(large values are bad)" ) def test_multi_header(): readme = "data/cds/multi/ReadMe" data = "data/cds/multi/lhs2065.dat" for read_table in (read_table1, read_table2, read_table3): table = read_table(readme, data) assert_equal(len(table), 18) assert_almost_equal(table["Lambda"][-1], 6479.32) assert_equal(table["Fnu"][-1], "0.285937") data = "data/cds/multi/lp944-20.dat" for read_table in (read_table1, read_table2, read_table3): table = read_table(readme, data) assert_equal(len(table), 18) assert_almost_equal(table["Lambda"][0], 6476.09) assert_equal(table["Fnu"][-1], "0.489005") def test_glob_header(): readme = "data/cds/glob/ReadMe" data = "data/cds/glob/lmxbrefs.dat" for read_table in (read_table1, read_table2, read_table3): table = read_table(readme, data) assert_equal(len(table), 291) assert_equal(table["Name"][-1], "J1914+0953") assert_equal(table["BibCode"][-2], "2005A&A...432..235R") def test_header_from_readme(): r = ascii.Cds("data/vizier/ReadMe") table = r.read("data/vizier/table1.dat") assert len(r.data.data_lines) == 15 assert len(table) == 15 assert len(table.keys()) == 18 Bmag = [ 14.79, 15.00, 14.80, 12.38, 12.36, 12.24, 13.75, 13.65, 13.41, 11.59, 11.68, 11.53, 13.92, 14.03, 14.18, ] for i, val in enumerate(table.field("Bmag")): assert val == Bmag[i] table = r.read("data/vizier/table5.dat") assert len(r.data.data_lines) == 49 assert len(table) == 49 assert len(table.keys()) == 10 Q = [ 0.289, 0.325, 0.510, 0.577, 0.539, 0.390, 0.957, 0.736, 1.435, 1.117, 1.473, 0.808, 1.416, 2.209, 0.617, 1.046, 1.604, 1.419, 1.431, 1.183, 1.210, 1.005, 0.706, 0.665, 0.340, 0.323, 0.391, 0.280, 0.343, 0.369, 0.495, 0.828, 1.113, 0.499, 1.038, 0.260, 0.863, 1.638, 0.479, 0.232, 0.627, 0.671, 0.371, 0.851, 0.607, -9.999, 1.958, 1.416, 0.949, ] for i, val in enumerate(table.field("Q")): if val is np.ma.masked: # text value for a missing value in that table assert Q[i] == -9.999 else: assert val == Q[i] @pytest.mark.parametrize("reader_cls", (ascii.Cds, ascii.Mrt)) def test_cds_units(reader_cls): from astropy import units data_and_readme = "data/cds.dat" reader = ascii.get_reader(reader_cls) table = reader.read(data_and_readme) # column unit is GMsun (giga solar masses) # make sure this is parsed correctly, not as a "string" unit assert table["Fit"].to(units.solMass).unit == units.solMass @pytest.mark.parametrize("reader_cls", (ascii.Cds, ascii.Mrt)) def test_cds_function_units(reader_cls): data_and_readme = "data/cdsFunctional.dat" reader = ascii.get_reader(reader_cls) table = reader.read(data_and_readme) assert table["logg"].unit == u.dex(u.cm / u.s**2) assert table["logTe"].unit == u.dex(u.K) assert table["Mass"].unit == u.Msun assert table["e_Mass"].unit == u.Msun assert table["Age"].unit == u.Myr assert table["e_Age"].unit == u.Myr @pytest.mark.parametrize("reader_cls", (ascii.Cds, ascii.Mrt)) def test_cds_function_units2(reader_cls): # This one includes some dimensionless dex. data_and_readme = "data/cdsFunctional2.dat" reader = ascii.get_reader(reader_cls) table = reader.read(data_and_readme) assert table["Teff"].unit == u.K assert table["logg"].unit == u.dex(u.cm / u.s**2) assert table["vturb"].unit == u.km / u.s assert table["[Fe/H]"].unit == u.dex(u.one) assert table["e_[Fe/H]"].unit == u.dex(u.one) assert_almost_equal( table["[Fe/H]"].to(u.one), 10.0 ** (np.array([-2.07, -1.50, -2.11, -1.64])) ) def test_cds_ignore_nullable(): # Make sure CDS Reader does not ignore nullabilty for columns # with a limit specifier readme = "data/cds/null/ReadMe" data = "data/cds/null/table.dat" r = ascii.Cds(readme) r.read(data) assert_equal(r.header.cols[6].description, "Temperature class codified (10)") assert_equal(r.header.cols[8].description, "Luminosity class codified (11)") assert_equal(r.header.cols[5].description, "Pericenter position angle (18)") def test_cds_no_whitespace(): # Make sure CDS Reader only checks null values when an '=' symbol is present, # and read description text even if there is no whitespace after '?'. readme = "data/cds/null/ReadMe1" data = "data/cds/null/table.dat" r = ascii.Cds(readme) r.read(data) assert_equal(r.header.cols[6].description, "Temperature class codified (10)") assert_equal(r.header.cols[6].null, "") assert_equal(r.header.cols[7].description, "Equivalent width (in mA)") assert_equal(r.header.cols[7].null, "-9.9") assert_equal( r.header.cols[10].description, "DAOSPEC quality parameter Q(large values are bad)", ) assert_equal(r.header.cols[10].null, "-9.999") def test_cds_order(): # Make sure CDS Reader does not ignore order specifier that maybe present after # the null specifier '?' readme = "data/cds/null/ReadMe1" data = "data/cds/null/table.dat" r = ascii.Cds(readme) r.read(data) assert_equal(r.header.cols[5].description, "Catalogue Identification Number") assert_equal(r.header.cols[8].description, "Equivalent width (in mA)") assert_equal(r.header.cols[9].description, "Luminosity class codified (11)") if __name__ == "__main__": # run from main directory; not from test/ test_header_from_readme() test_multi_header() test_glob_header() test_description() test_cds_units() test_cds_ignore_nullable() test_cds_no_whitespace() test_cds_order()
43a32e214f1cbdac8397258d7ebf4188536e57962367d8b6f878ec3de8f73f01
# Licensed under a 3-clause BSD style license - see LICENSE.rst from io import StringIO from astropy.io import ascii from .common import assert_almost_equal, assert_equal def assert_equal_splitlines(arg1, arg2): assert_equal(arg1.splitlines(), arg2.splitlines()) def test_read_normal(): """Normal SimpleRST Table""" table = """ # comment (with blank line above) ======= ========= Col1 Col2 ======= ========= 1.2 "hello" 2.4 's worlds ======= ========= """ reader = ascii.get_reader(Reader=ascii.RST) dat = reader.read(table) assert_equal(dat.colnames, ["Col1", "Col2"]) assert_almost_equal(dat[1][0], 2.4) assert_equal(dat[0][1], '"hello"') assert_equal(dat[1][1], "'s worlds") def test_read_normal_names(): """Normal SimpleRST Table with provided column names""" table = """ # comment (with blank line above) ======= ========= Col1 Col2 ======= ========= 1.2 "hello" 2.4 's worlds ======= ========= """ reader = ascii.get_reader(Reader=ascii.RST, names=("name1", "name2")) dat = reader.read(table) assert_equal(dat.colnames, ["name1", "name2"]) assert_almost_equal(dat[1][0], 2.4) def test_read_normal_names_include(): """Normal SimpleRST Table with provided column names""" table = """ # comment (with blank line above) ======= ========== ====== Col1 Col2 Col3 ======= ========== ====== 1.2 "hello" 3 2.4 's worlds 7 ======= ========== ====== """ reader = ascii.get_reader( Reader=ascii.RST, names=("name1", "name2", "name3"), include_names=("name1", "name3"), ) dat = reader.read(table) assert_equal(dat.colnames, ["name1", "name3"]) assert_almost_equal(dat[1][0], 2.4) assert_equal(dat[0][1], 3) def test_read_normal_exclude(): """Nice, typical SimpleRST table with col name excluded""" table = """ ======= ========== Col1 Col2 ======= ========== 1.2 "hello" 2.4 's worlds ======= ========== """ reader = ascii.get_reader(Reader=ascii.RST, exclude_names=("Col1",)) dat = reader.read(table) assert_equal(dat.colnames, ["Col2"]) assert_equal(dat[1][0], "'s worlds") def test_read_unbounded_right_column(): """The right hand column should be allowed to overflow""" table = """ # comment (with blank line above) ===== ===== ==== Col1 Col2 Col3 ===== ===== ==== 1.2 2 Hello 2.4 4 Worlds ===== ===== ==== """ reader = ascii.get_reader(Reader=ascii.RST) dat = reader.read(table) assert_equal(dat[0][2], "Hello") assert_equal(dat[1][2], "Worlds") def test_read_unbounded_right_column_header(): """The right hand column should be allowed to overflow""" table = """ # comment (with blank line above) ===== ===== ==== Col1 Col2 Col3Long ===== ===== ==== 1.2 2 Hello 2.4 4 Worlds ===== ===== ==== """ reader = ascii.get_reader(Reader=ascii.RST) dat = reader.read(table) assert_equal(dat.colnames[-1], "Col3Long") def test_read_right_indented_table(): """We should be able to read right indented tables correctly""" table = """ # comment (with blank line above) ==== ==== ==== Col1 Col2 Col3 ==== ==== ==== 3 3.4 foo 1 4.5 bar ==== ==== ==== """ reader = ascii.get_reader(Reader=ascii.RST) dat = reader.read(table) assert_equal(dat.colnames, ["Col1", "Col2", "Col3"]) assert_equal(dat[0][2], "foo") assert_equal(dat[1][0], 1) def test_trailing_spaces_in_row_definition(): """Trailing spaces in the row definition column shouldn't matter""" table = ( "\n" "# comment (with blank line above)\n" " ==== ==== ==== \n" " Col1 Col2 Col3\n" " ==== ==== ==== \n" " 3 3.4 foo\n" " 1 4.5 bar\n" " ==== ==== ==== \n" ) # make sure no one accidentally deletes the trailing whitespaces in the # table. assert len(table) == 151 reader = ascii.get_reader(Reader=ascii.RST) dat = reader.read(table) assert_equal(dat.colnames, ["Col1", "Col2", "Col3"]) assert_equal(dat[0][2], "foo") assert_equal(dat[1][0], 1) table = """\ ====== =========== ============ =========== Col1 Col2 Col3 Col4 ====== =========== ============ =========== 1.2 "hello" 1 a 2.4 's worlds 2 2 ====== =========== ============ =========== """ dat = ascii.read(table, Reader=ascii.RST) def test_write_normal(): """Write a table as a normal SimpleRST Table""" out = StringIO() ascii.write(dat, out, Writer=ascii.RST) assert_equal_splitlines( out.getvalue(), """\ ==== ========= ==== ==== Col1 Col2 Col3 Col4 ==== ========= ==== ==== 1.2 "hello" 1 a 2.4 's worlds 2 2 ==== ========= ==== ==== """, )
db00a9568b1a6d78acb5efaf37a4f6465a51cfcdbc6875534cfacaaf5119e0a6
# Licensed under a 3-clause BSD style license - see LICENSE.rst from io import StringIO import numpy as np from astropy.io import ascii from .common import assert_equal def test_types_from_dat(): converters = {"a": [ascii.convert_numpy(float)], "e": [ascii.convert_numpy(str)]} dat = ascii.read( ["a b c d e", "1 1 cat 2.1 4.2"], Reader=ascii.Basic, converters=converters ) assert dat["a"].dtype.kind == "f" assert dat["b"].dtype.kind == "i" assert dat["c"].dtype.kind in ("S", "U") assert dat["d"].dtype.kind == "f" assert dat["e"].dtype.kind in ("S", "U") def test_rdb_write_types(): dat = ascii.read(["a b c d", "1 1.0 cat 2.1"], Reader=ascii.Basic) out = StringIO() ascii.write(dat, out, Writer=ascii.Rdb) outs = out.getvalue().splitlines() assert_equal(outs[1], "N\tN\tS\tN") def test_ipac_read_types(): table = r"""\ | ra | dec | sai |-----v2---| sptype | | real | float | l | real | char | | unit | unit | unit | unit | ergs | | null | null | null | null | -999 | 2.09708 2956 73765 2.06000 B8IVpMnHg """ reader = ascii.get_reader(Reader=ascii.Ipac) reader.read(table) types = [ ascii.FloatType, ascii.FloatType, ascii.IntType, ascii.FloatType, ascii.StrType, ] for col, expected_type in zip(reader.cols, types): assert_equal(col.type, expected_type) def test_col_dtype_in_custom_class(): """Test code in BaseOutputter._convert_vals to handle Column.dtype attribute. See discussion in #11895.""" dtypes = [np.float32, np.int8, np.int16] class TestDtypeHeader(ascii.BasicHeader): def get_cols(self, lines): super().get_cols(lines) for col, dtype in zip(self.cols, dtypes): col.dtype = dtype class TestDtype(ascii.Basic): """ Basic table Data Reader with data type alternating float32, int8 """ header_class = TestDtypeHeader txt = """ a b c 1 2 3 """ reader = ascii.get_reader(TestDtype) t = reader.read(txt) for col, dtype in zip(t.itercols(), dtypes): assert col.dtype.type is dtype
e960e0490ca9a3eff924f738144a15099d1524a2d7bc4ccc80c7ec259d8e1dea
# Licensed under a 3-clause BSD style license - see LICENSE.rst # This file connects ASDF to the astropy.table.Table class import warnings from astropy.io import registry as io_registry from astropy.io.misc.asdf.deprecation import create_asdf_deprecation_warning from astropy.table import Table from astropy.utils.compat import optional_deps def read_table(filename, data_key=None, find_table=None, **kwargs): """ Read a `~astropy.table.Table` object from an ASDF file This requires `asdf <https://pypi.org/project/asdf/>`_ to be installed. By default, this function will look for a Table object with the key of ``data`` in the top-level ASDF tree. The parameters ``data_key`` and ``find_key`` can be used to override the default behavior. This function is registered as the Table reader for ASDF files with the unified I/O interface. Parameters ---------- filename : str or :class:`py.lath:local` Name of the file to be read data_key : str Optional top-level key to use for finding the Table in the tree. If not provided, uses ``data`` by default. Use of this parameter is not compatible with ``find_table``. find_table : function Optional function to be used for locating the Table in the tree. The function takes a single parameter, which is a dictionary representing the top of the ASDF tree. The function must return a `~astropy.table.Table` instance. Returns ------- table : `~astropy.table.Table` `~astropy.table.Table` instance """ warnings.warn(create_asdf_deprecation_warning()) try: import asdf except ImportError: raise Exception("The asdf module is required to read and write ASDF files") if data_key and find_table: raise ValueError("Options 'data_key' and 'find_table' are not compatible") with asdf.open(filename, **kwargs) as af: if find_table: return find_table(af.tree) else: return af[data_key or "data"] def write_table(table, filename, data_key=None, make_tree=None, **kwargs): """ Write a `~astropy.table.Table` object to an ASDF file. This requires `asdf <https://pypi.org/project/asdf/>`_ to be installed. By default, this function will write a Table object in the top-level ASDF tree using the key of ``data``. The parameters ``data_key`` and ``make_tree`` can be used to override the default behavior. This function is registered as the Table writer for ASDF files with the unified I/O interface. Parameters ---------- table : `~astropy.table.Table` `~astropy.table.Table` instance to be written filename : str or :class:`py.path:local` Name of the new ASDF file to be created data_key : str Optional top-level key in the ASDF tree to use when writing the Table. If not provided, uses ``data`` by default. Use of this parameter is not compatible with ``make_tree``. make_tree : function Optional function to be used for creating the ASDF tree. The function takes a single parameter, which is the `~astropy.table.Table` instance to be written. The function must return a `dict` representing the ASDF tree to be created. """ warnings.warn(create_asdf_deprecation_warning()) try: import asdf except ImportError: raise Exception("The asdf module is required to read and write ASDF files") if data_key and make_tree: raise ValueError("Options 'data_key' and 'make_tree' are not compatible") if make_tree: tree = make_tree(table) else: tree = {data_key or "data": table} with asdf.AsdfFile(tree) as af: af.write_to(filename, **kwargs) def asdf_identify(origin, filepath, fileobj, *args, **kwargs): try: import asdf # noqa: F401 except ImportError: return False return filepath is not None and filepath.endswith(".asdf") if not optional_deps.HAS_ASDF_ASTROPY: io_registry.register_reader("asdf", Table, read_table) io_registry.register_writer("asdf", Table, write_table) io_registry.register_identifier("asdf", Table, asdf_identify)
e6146ac7a82b10233ba2f7396a140aaff76d528d337fc311eee5aa2e4553c987
from astropy.utils.exceptions import AstropyDeprecationWarning def create_asdf_deprecation_warning(): return AstropyDeprecationWarning( "ASDF functionality for astropy is being moved out of the astropy package to" " the new asdf-astropy package. Please use this package instead." " astropy.io.misc.asdf is deprecated since astropy 5.1 and will be removed in a" " future release." )
9651180d7cfcf401e67c7083737e6bbd8be83278292d3ee0f868b6e76b6482ec
# Licensed under a 3-clause BSD style license - see LICENSE.rst import warnings from asdf.types import CustomType, ExtensionTypeMeta from astropy.io.misc.asdf.deprecation import create_asdf_deprecation_warning __all__ = ["AstropyType", "AstropyAsdfType"] # Names of AstropyType or AstropyAsdfType subclasses that are base classes # and aren't used directly for serialization. _TYPE_BASE_CLASS_NAMES = {"PolynomialTypeBase"} _astropy_types = set() _astropy_asdf_types = set() class AstropyTypeMeta(ExtensionTypeMeta): """ Keeps track of `AstropyType` subclasses that are created so that they can be stored automatically by astropy extensions for ASDF. """ def __new__(mcls, name, bases, attrs): cls = super().__new__(mcls, name, bases, attrs) # Classes using this metaclass are automatically added to the list of # astropy extensions if cls.__name__ not in _TYPE_BASE_CLASS_NAMES: if cls.organization == "astropy.org" and cls.standard == "astropy": _astropy_types.add(cls) elif cls.organization == "stsci.edu" and cls.standard == "asdf": _astropy_asdf_types.add(cls) return cls class AstropyType(CustomType, metaclass=AstropyTypeMeta): """ This class represents types that have schemas and tags that are defined by Astropy. IMPORTANT: This parent class should **not** be used for types that have schemas that are defined by the ASDF standard. """ organization = "astropy.org" standard = "astropy" @classmethod def to_tree_tagged(cls, node, ctx): warnings.warn(create_asdf_deprecation_warning()) return super().to_tree_tagged(node, ctx) @classmethod def from_tree_tagged(cls, tree, ctx): warnings.warn(create_asdf_deprecation_warning()) return super().from_tree_tagged(tree, ctx) class AstropyAsdfType(CustomType, metaclass=AstropyTypeMeta): """ This class represents types that have schemas that are defined in the ASDF standard, but have tags that are implemented within astropy. IMPORTANT: This parent class should **not** be used for types that also have schemas that are defined by astropy. """ organization = "stsci.edu" standard = "asdf" @classmethod def to_tree_tagged(cls, node, ctx): warnings.warn(create_asdf_deprecation_warning()) return super().to_tree_tagged(node, ctx) @classmethod def from_tree_tagged(cls, tree, ctx): warnings.warn(create_asdf_deprecation_warning()) return super().from_tree_tagged(tree, ctx)
307416d8665adcbc7fdb84f22e15f111cb79ab11d63be458976fbb040ab8cf3d
from pathlib import Path from astropy.utils.introspection import minversion def get_asdf_tests(): asdf_dir = Path(__file__).parent.resolve() paths = Path(asdf_dir).rglob("test_*.py") return [str(p.relative_to(asdf_dir)) for p in paths] collect_ignore = get_asdf_tests() try: import asdf except ImportError: pass else: if not minversion(asdf, "3.0.0"): collect_ignore = []
dba8ca362f09cd0514fba49b4b92e1cabcc501837a2424a0448a2741b2d976cc
# Licensed under a 3-clause BSD style license - see LICENSE.rst import os from asdf.extension import AsdfExtension, BuiltinExtension from asdf.util import filepath_to_url # Make sure that all tag implementations are imported by the time we create # the extension class so that _astropy_asdf_types is populated correctly. We # could do this using __init__ files, except it causes pytest import errors in # the case that asdf is not installed. from .tags.coordinates.angle import * # noqa: F401 from .tags.coordinates.earthlocation import * # noqa: F401 from .tags.coordinates.frames import * # noqa: F401 from .tags.coordinates.representation import * # noqa: F401 from .tags.coordinates.skycoord import * # noqa: F401 from .tags.coordinates.spectralcoord import * # noqa: F401 from .tags.fits.fits import * # noqa: F401 from .tags.table.table import * # noqa: F401 from .tags.time.time import * # noqa: F401 from .tags.time.timedelta import * # noqa: F401 from .tags.transform.basic import * # noqa: F401 from .tags.transform.compound import * # noqa: F401 from .tags.transform.functional_models import * # noqa: F401 from .tags.transform.math import * # noqa: F401 from .tags.transform.physical_models import * # noqa: F401 from .tags.transform.polynomial import * # noqa: F401 from .tags.transform.powerlaws import * # noqa: F401 from .tags.transform.projections import * # noqa: F401 from .tags.transform.spline import * # noqa: F401 from .tags.transform.tabular import * # noqa: F401 from .tags.unit.equivalency import * # noqa: F401 from .tags.unit.quantity import * # noqa: F401 from .tags.unit.unit import * # noqa: F401 from .types import _astropy_asdf_types, _astropy_types __all__ = ["AstropyExtension", "AstropyAsdfExtension"] ASTROPY_SCHEMA_URI_BASE = "http://astropy.org/schemas/" SCHEMA_PATH = os.path.abspath( os.path.join(os.path.dirname(__file__), "data", "schemas") ) ASTROPY_URL_MAPPING = [ ( ASTROPY_SCHEMA_URI_BASE, filepath_to_url(os.path.join(SCHEMA_PATH, "astropy.org")) + "/{url_suffix}.yaml", ) ] # This extension is used to register custom types that have both tags and # schemas defined by Astropy. class AstropyExtension(AsdfExtension): @property def types(self): return _astropy_types @property def tag_mapping(self): return [ ("tag:astropy.org:astropy", ASTROPY_SCHEMA_URI_BASE + "astropy{tag_suffix}") ] @property def url_mapping(self): return ASTROPY_URL_MAPPING # This extension is used to register custom tag types that have schemas defined # by ASDF, but have tag implementations defined in astropy. class AstropyAsdfExtension(BuiltinExtension): @property def types(self): return _astropy_asdf_types
4dbbed4644aad680ede7794711f57d2741e7caf47b5819a829b3596c0167c1f0
# Licensed under a 3-clause BSD style license - see LICENSE.rst # This file connects the readers/writers to the astropy.table.Table class import functools import os.path import astropy.io.registry as io_registry from astropy.table import Table from astropy.utils.misc import NOT_OVERWRITING_MSG __all__ = ["PANDAS_FMTS"] # Astropy users normally expect to not have an index, so default to turn # off writing the index. This structure allows for astropy-specific # customization of all options. PANDAS_FMTS = { "csv": {"read": {}, "write": {"index": False}}, "fwf": {"read": {}}, # No writer "html": {"read": {}, "write": {"index": False}}, "json": {"read": {}, "write": {}}, } PANDAS_PREFIX = "pandas." # Imports for reading HTML _IMPORTS = False _HAS_BS4 = False _HAS_LXML = False _HAS_HTML5LIB = False def import_html_libs(): """Try importing dependencies for reading HTML. This is copied from pandas.io.html """ # import things we need # but make this done on a first use basis global _IMPORTS if _IMPORTS: return global _HAS_BS4, _HAS_LXML, _HAS_HTML5LIB from astropy.utils.compat.optional_deps import HAS_BS4 as _HAS_BS4 from astropy.utils.compat.optional_deps import HAS_HTML5LIB as _HAS_HTML5LIB from astropy.utils.compat.optional_deps import HAS_LXML as _HAS_LXML _IMPORTS = True def _pandas_read(fmt, filespec, **kwargs): """Provide io Table connector to read table using pandas.""" try: import pandas except ImportError: raise ImportError("pandas must be installed to use pandas table reader") pandas_fmt = fmt[len(PANDAS_PREFIX) :] # chop the 'pandas.' in front read_func = getattr(pandas, "read_" + pandas_fmt) # Get defaults and then override with user-supplied values read_kwargs = PANDAS_FMTS[pandas_fmt]["read"].copy() read_kwargs.update(kwargs) # Special case: pandas defaults to HTML lxml for reading, but does not attempt # to fall back to bs4 + html5lib. So do that now for convenience if user has # not specifically selected a flavor. If things go wrong the pandas exception # with instruction to install a library will come up. if pandas_fmt == "html" and "flavor" not in kwargs: import_html_libs() if not _HAS_LXML and _HAS_HTML5LIB and _HAS_BS4: read_kwargs["flavor"] = "bs4" df = read_func(filespec, **read_kwargs) # Special case for HTML if pandas_fmt == "html": df = df[0] return Table.from_pandas(df) def _pandas_write(fmt, tbl, filespec, overwrite=False, **kwargs): """Provide io Table connector to write table using pandas.""" pandas_fmt = fmt[len(PANDAS_PREFIX) :] # chop the 'pandas.' in front # Get defaults and then override with user-supplied values write_kwargs = PANDAS_FMTS[pandas_fmt]["write"].copy() write_kwargs.update(kwargs) df = tbl.to_pandas() write_method = getattr(df, "to_" + pandas_fmt) if not overwrite: try: # filespec is not always a path-like exists = os.path.exists(filespec) except TypeError: # skip invalid arguments pass else: if exists: # only error if file already exists raise OSError(NOT_OVERWRITING_MSG.format(filespec)) return write_method(filespec, **write_kwargs) for pandas_fmt, defaults in PANDAS_FMTS.items(): fmt = PANDAS_PREFIX + pandas_fmt # Full format specifier if "read" in defaults: func = functools.partial(_pandas_read, fmt) io_registry.register_reader(fmt, Table, func) if "write" in defaults: func = functools.partial(_pandas_write, fmt) io_registry.register_writer(fmt, Table, func)
840f927796feaa111a6932c1fe9929d1f30d9b9ebf37f0bf683a15b4debb007d
# Licensed under a 3-clause BSD style license - see LICENSE.rst from io import StringIO import numpy as np import pytest from astropy import units as u from astropy.coordinates import SkyCoord from astropy.io import ascii from astropy.io.misc.pandas import connect from astropy.table import QTable, Table from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH # Check dependencies pandas = pytest.importorskip("pandas") connect.import_html_libs() HAS_HTML_DEPS = connect._HAS_LXML or (connect._HAS_BS4 and connect._HAS_HTML5LIB) WRITE_FMTS = [fmt for fmt in connect.PANDAS_FMTS if "write" in connect.PANDAS_FMTS[fmt]] @pytest.mark.parametrize("fmt", WRITE_FMTS) def test_read_write_format(fmt): """ Test round-trip through pandas write/read for supported formats. :param fmt: format name, e.g. csv, html, json :return: """ # Skip the reading tests if fmt == "html" and not HAS_HTML_DEPS: pytest.skip("Missing lxml or bs4 + html5lib for HTML read/write test") pandas_fmt = "pandas." + fmt # Explicitly provide dtype to avoid casting 'a' to int32. # See https://github.com/astropy/astropy/issues/8682 t = Table( [[1, 2, 3], [1.0, 2.5, 5.0], ["a", "b", "c"]], dtype=(np.int64, np.float64, str) ) buf = StringIO() t.write(buf, format=pandas_fmt) buf.seek(0) t2 = Table.read(buf, format=pandas_fmt) assert t.colnames == t2.colnames assert np.all(t == t2) @pytest.mark.parametrize("fmt", WRITE_FMTS) def test_write_overwrite(tmp_path, fmt): """Test overwriting.""" tmpfile = tmp_path / f"test.{fmt}" pandas_fmt = f"pandas.{fmt}" # Explicitly provide dtype to avoid casting 'a' to int32. # See https://github.com/astropy/astropy/issues/8682 t = Table( [[1, 2, 3], [1.0, 2.5, 5.0], ["a", "b", "c"]], dtype=(np.int64, np.float64, str) ) # works when file DNE t.write(tmpfile, format=pandas_fmt) # fails when cannot overwrite with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): t.write(tmpfile, format=pandas_fmt, overwrite=False) # passes when it can t.write(tmpfile, format=pandas_fmt, overwrite=True) def test_read_fixed_width_format(): """Test reading with pandas read_fwf()""" tbl = """\ a b c 1 2.0 a 2 3.0 b""" buf = StringIO() buf.write(tbl) # Explicitly provide converters to avoid casting 'a' to int32. # See https://github.com/astropy/astropy/issues/8682 t = Table.read( tbl, format="ascii", guess=False, converters={"a": [ascii.convert_numpy(np.int64)]}, ) buf.seek(0) t2 = Table.read(buf, format="pandas.fwf") assert t.colnames == t2.colnames assert np.all(t == t2) def test_write_with_mixins(): """Writing a table with mixins just drops them via to_pandas() This also tests passing a kwarg to pandas read and write. """ sc = SkyCoord([1, 2], [3, 4], unit="deg") q = [5, 6] * u.m qt = QTable([[1, 2], q, sc], names=["i", "q", "sc"]) buf = StringIO() qt.write(buf, format="pandas.csv", sep=" ") exp = ["i q sc.ra sc.dec", "1 5.0 1.0 3.0", "2 6.0 2.0 4.0"] assert buf.getvalue().splitlines() == exp # Read it back buf.seek(0) qt2 = Table.read(buf, format="pandas.csv", sep=" ") # Explicitly provide converters to avoid casting 'i' to int32. # See https://github.com/astropy/astropy/issues/8682 exp_t = ascii.read(exp, converters={"i": [ascii.convert_numpy(np.int64)]}) assert qt2.colnames == exp_t.colnames assert np.all(qt2 == exp_t)
a35beb4ef1d85f3558d624cc88788e71746f16cd3e200506508477c95d22eac0
# Licensed under a 3-clause BSD style license - see LICENSE.rst import numpy as np import pytest from astropy import units as u from astropy.coordinates import ( Angle, CartesianRepresentation, EarthLocation, Latitude, Longitude, SkyCoord, SphericalCosLatDifferential, SphericalRepresentation, ) from astropy.io.misc.parquet import get_pyarrow, parquet_identify from astropy.table import Column, NdarrayMixin, QTable, Table from astropy.table.table_helpers import simple_table from astropy.time import Time, TimeDelta from astropy.units import allclose as quantity_allclose from astropy.units.quantity import QuantityInfo from astropy.utils.compat.optional_deps import HAS_PANDAS from astropy.utils.exceptions import AstropyUserWarning from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH # Skip all tests in this file if we cannot import pyarrow pyarrow = pytest.importorskip("pyarrow") ALL_DTYPES = [ np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64, np.float32, np.float64, np.bool_, "|S3", "U3", ] def _default_values(dtype): if dtype == np.bool_: return [0, 1, 1] elif dtype == "|S3": return [b"abc", b"def", b"ghi"] elif dtype == "U3": return ["abc", "def", "ghi"] else: return [1, 2, 3] def test_read_write_simple(tmp_path): """Test writing/reading a simple parquet file.""" test_file = tmp_path / "test.parquet" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file) t2 = Table.read(test_file) assert np.all(t2["a"] == [1, 2, 3]) def test_read_write_existing(tmp_path): """Test writing an existing file without overwriting.""" test_file = tmp_path / "test.parquet" with open(test_file, "w") as f: # create empty file pass t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): t1.write(test_file) def test_read_write_existing_overwrite(tmp_path): """Test overwriting an existing file.""" test_file = tmp_path / "test.parquet" with open(test_file, "w") as f: # create empty file pass t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, overwrite=True) t2 = Table.read(test_file) assert np.all(t2["a"] == [1, 2, 3]) def test_read_fileobj(tmp_path): """Test reading a file object.""" test_file = tmp_path / "test.parquet" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file) import io with io.FileIO(test_file, mode="r") as input_file: t2 = Table.read(input_file) assert np.all(t2["a"] == [1, 2, 3]) def test_read_pathlikeobj(tmp_path): """Test reading a path-like object.""" test_file = tmp_path / "test.parquet" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file) import pathlib p = pathlib.Path(test_file) t2 = Table.read(p) assert np.all(t2["a"] == [1, 2, 3]) def test_read_wrong_fileobj(): """Test reading an incorrect fileobject type.""" class FakeFile: def not_read(self): pass f = FakeFile() with pytest.raises( TypeError, match="pyarrow can only open path-like or file-like objects." ): Table.read(f, format="parquet") def test_identify_wrong_fileobj(): """Test identifying an incorrect fileobj.""" class FakeFile: def not_read(self): pass f = FakeFile() assert not parquet_identify("test", "test", f) def test_identify_file_wrong_extension(): """Test identifying an incorrect extension.""" assert not parquet_identify("test", "test.notparquet", None) def test_identify_file_correct_extension(): """Test identifying an incorrect extension.""" assert parquet_identify("test", "test.parquet", None) assert parquet_identify("test", "test.parq", None) def test_identify_file_noobject_nopath(): """Test running identify with no object or path.""" assert not parquet_identify("test", None, None) def test_write_wrong_type(): """Test writing to a filename of the wrong type.""" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) with pytest.raises(TypeError, match="should be a string"): t1.write(1212, format="parquet") @pytest.mark.parametrize("dtype", ALL_DTYPES) def test_preserve_single_dtypes(tmp_path, dtype): """Test that round-tripping a single column preserves datatypes.""" test_file = tmp_path / "test.parquet" values = _default_values(dtype) t1 = Table() t1.add_column(Column(name="a", data=np.array(values, dtype=dtype))) t1.write(test_file) t2 = Table.read(test_file) assert np.all(t2["a"] == values) assert t2["a"].dtype == dtype def test_preserve_all_dtypes(tmp_path): """Test that round-tripping preserves a table with all the datatypes.""" test_file = tmp_path / "test.parquet" t1 = Table() for dtype in ALL_DTYPES: values = _default_values(dtype) t1.add_column(Column(name=str(dtype), data=np.array(values, dtype=dtype))) t1.write(test_file) t2 = Table.read(test_file) for dtype in ALL_DTYPES: values = _default_values(dtype) assert np.all(t2[str(dtype)] == values) assert t2[str(dtype)].dtype == dtype def test_preserve_meta(tmp_path): """Test that writing/reading preserves metadata.""" test_file = tmp_path / "test.parquet" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.meta["a"] = 1 t1.meta["b"] = "hello" t1.meta["c"] = 3.14159 t1.meta["d"] = True t1.meta["e"] = np.array([1, 2, 3]) t1.write(test_file) t2 = Table.read(test_file) for key in t1.meta: assert np.all(t1.meta[key] == t2.meta[key]) def test_preserve_serialized(tmp_path): """Test that writing/reading preserves unit/format/description.""" test_file = tmp_path / "test.parquet" t1 = Table() t1["a"] = Column(data=[1, 2, 3], unit="s") t1["a"].meta["a0"] = "A0" t1["a"].meta["a1"] = {"a1": [0, 1]} t1["a"].format = "7.3f" t1["a"].description = "A column" t1.meta["b"] = 1 t1.meta["c"] = {"c0": [0, 1]} t1.write(test_file, overwrite=True) t2 = Table.read(test_file) assert t1["a"].unit == t2["a"].unit assert t1["a"].format == t2["a"].format assert t1["a"].description == t2["a"].description assert t1["a"].meta == t2["a"].meta assert t1.meta == t2.meta def test_metadata_very_large(tmp_path): """Test that very large datasets work""" test_file = tmp_path / "test.parquet" t1 = Table() t1["a"] = Column(data=[1, 2, 3], unit="s") t1["a"].meta["a0"] = "A0" t1["a"].meta["a1"] = {"a1": [0, 1]} t1["a"].format = "7.3f" t1["a"].description = "A column" t1.meta["b"] = 1 t1.meta["c"] = {"c0": [0, 1]} t1.meta["meta_big"] = "0" * (2**16 + 1) t1.meta["meta_biggerstill"] = "0" * (2**18) t1.write(test_file, overwrite=True) t2 = Table.read(test_file) assert t1["a"].unit == t2["a"].unit assert t1["a"].format == t2["a"].format assert t1["a"].description == t2["a"].description assert t1["a"].meta == t2["a"].meta assert t1.meta == t2.meta def test_fail_meta_serialize(tmp_path): """Test that we cannot preserve objects in metadata.""" test_file = tmp_path / "test.parquet" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.meta["f"] = str with pytest.raises(Exception) as err: t1.write(test_file) assert "cannot represent an object" in str(err.value) assert "<class 'str'>" in str(err.value) def assert_objects_equal(obj1, obj2, attrs, compare_class=True): """Convenient routine to check objects and attributes match.""" if compare_class: assert obj1.__class__ is obj2.__class__ info_attrs = [ "info.name", "info.format", "info.unit", "info.description", "info.meta", ] for attr in attrs + info_attrs: a1 = obj1 a2 = obj2 for subattr in attr.split("."): try: a1 = getattr(a1, subattr) a2 = getattr(a2, subattr) except AttributeError: a1 = a1[subattr] a2 = a2[subattr] # Mixin info.meta can None instead of empty OrderedDict(), #6720 would # fix this. if attr == "info.meta": if a1 is None: a1 = {} if a2 is None: a2 = {} if isinstance(a1, np.ndarray) and a1.dtype.kind == "f": assert quantity_allclose(a1, a2, rtol=1e-15) else: assert np.all(a1 == a2) # Testing Parquet table read/write with mixins. This is mostly # copied from HDF5/FITS mixin testing, and it might be good to unify it. # Analogous tests also exist for ECSV. el = EarthLocation(x=1 * u.km, y=3 * u.km, z=5 * u.km) el2 = EarthLocation(x=[1, 2] * u.km, y=[3, 4] * u.km, z=[5, 6] * u.km) sr = SphericalRepresentation([0, 1] * u.deg, [2, 3] * u.deg, 1 * u.kpc) cr = CartesianRepresentation([0, 1] * u.pc, [4, 5] * u.pc, [8, 6] * u.pc) sd = SphericalCosLatDifferential( [0, 1] * u.mas / u.yr, [0, 1] * u.mas / u.yr, 10 * u.km / u.s ) srd = SphericalRepresentation(sr, differentials=sd) sc = SkyCoord([1, 2], [3, 4], unit="deg,deg", frame="fk4", obstime="J1990.5") scd = SkyCoord( [1, 2], [3, 4], [5, 6], unit="deg,deg,m", frame="fk4", obstime=["J1990.5", "J1991.5"], ) scdc = scd.copy() scdc.representation_type = "cartesian" scpm = SkyCoord( [1, 2], [3, 4], [5, 6], unit="deg,deg,pc", pm_ra_cosdec=[7, 8] * u.mas / u.yr, pm_dec=[9, 10] * u.mas / u.yr, ) scpmrv = SkyCoord( [1, 2], [3, 4], [5, 6], unit="deg,deg,pc", pm_ra_cosdec=[7, 8] * u.mas / u.yr, pm_dec=[9, 10] * u.mas / u.yr, radial_velocity=[11, 12] * u.km / u.s, ) scrv = SkyCoord( [1, 2], [3, 4], [5, 6], unit="deg,deg,pc", radial_velocity=[11, 12] * u.km / u.s ) tm = Time([2450814.5, 2450815.5], format="jd", scale="tai", location=el) # NOTE: in the test below the name of the column "x" for the Quantity is # important since it tests the fix for #10215 (namespace clash, where "x" # clashes with "el2.x"). mixin_cols = { "tm": tm, "dt": TimeDelta([1, 2] * u.day), "sc": sc, "scd": scd, "scdc": scdc, "scpm": scpm, "scpmrv": scpmrv, "scrv": scrv, "x": [1, 2] * u.m, "qdb": [10, 20] * u.dB(u.mW), "qdex": [4.5, 5.5] * u.dex(u.cm / u.s**2), "qmag": [21, 22] * u.ABmag, "lat": Latitude([1, 2] * u.deg), "lon": Longitude([1, 2] * u.deg, wrap_angle=180.0 * u.deg), "ang": Angle([1, 2] * u.deg), "el2": el2, "sr": sr, "cr": cr, "sd": sd, "srd": srd, } time_attrs = ["value", "shape", "format", "scale", "location"] compare_attrs = { "c1": ["data"], "c2": ["data"], "tm": time_attrs, "dt": ["shape", "value", "format", "scale"], "sc": ["ra", "dec", "representation_type", "frame.name"], "scd": ["ra", "dec", "distance", "representation_type", "frame.name"], "scdc": ["x", "y", "z", "representation_type", "frame.name"], "scpm": [ "ra", "dec", "distance", "pm_ra_cosdec", "pm_dec", "representation_type", "frame.name", ], "scpmrv": [ "ra", "dec", "distance", "pm_ra_cosdec", "pm_dec", "radial_velocity", "representation_type", "frame.name", ], "scrv": [ "ra", "dec", "distance", "radial_velocity", "representation_type", "frame.name", ], "x": ["value", "unit"], "qdb": ["value", "unit"], "qdex": ["value", "unit"], "qmag": ["value", "unit"], "lon": ["value", "unit", "wrap_angle"], "lat": ["value", "unit"], "ang": ["value", "unit"], "el2": ["x", "y", "z", "ellipsoid"], "nd": ["x", "y", "z"], "sr": ["lon", "lat", "distance"], "cr": ["x", "y", "z"], "sd": ["d_lon_coslat", "d_lat", "d_distance"], "srd": [ "lon", "lat", "distance", "differentials.s.d_lon_coslat", "differentials.s.d_lat", "differentials.s.d_distance", ], } def test_parquet_mixins_qtable_to_table(tmp_path): """Test writing as QTable and reading as Table. Ensure correct classes come out. """ filename = tmp_path / "test_simple.parquet" names = sorted(mixin_cols) t = QTable([mixin_cols[name] for name in names], names=names) t.write(filename, format="parquet") t2 = Table.read(filename, format="parquet") assert t.colnames == t2.colnames for name, col in t.columns.items(): col2 = t2[name] # Special-case Time, which does not yet support round-tripping # the format. if isinstance(col2, Time): col2.format = col.format attrs = compare_attrs[name] compare_class = True if isinstance(col.info, QuantityInfo): # Downgrade Quantity to Column + unit assert type(col2) is Column # Class-specific attributes like `value` or `wrap_angle` are lost. attrs = ["unit"] compare_class = False # Compare data values here (assert_objects_equal doesn't know how in this case) assert np.all(col.value == col2) assert_objects_equal(col, col2, attrs, compare_class) @pytest.mark.parametrize("table_cls", (Table, QTable)) def test_parquet_mixins_as_one(table_cls, tmp_path): """Test write/read all cols at once and validate intermediate column names""" filename = tmp_path / "test_simple.parquet" names = sorted(mixin_cols) t = table_cls([mixin_cols[name] for name in names], names=names) t.meta["C"] = "spam" t.meta["comments"] = ["this", "is", "a", "comment"] t.meta["history"] = ["first", "second", "third"] t.write(filename, format="parquet") t2 = table_cls.read(filename, format="parquet") assert t2.meta["C"] == "spam" assert t2.meta["comments"] == ["this", "is", "a", "comment"] assert t2.meta["history"] == ["first", "second", "third"] assert t.colnames == t2.colnames @pytest.mark.parametrize("name_col", list(mixin_cols.items())) @pytest.mark.parametrize("table_cls", (Table, QTable)) def test_parquet_mixins_per_column(table_cls, name_col, tmp_path): """Test write/read one col at a time and do detailed validation""" filename = tmp_path / "test_simple.parquet" name, col = name_col c = [1.0, 2.0] t = table_cls([c, col, c], names=["c1", name, "c2"]) t[name].info.description = "my description" t[name].info.meta = {"list": list(range(50)), "dict": {"a": "b" * 200}} if not t.has_mixin_columns: pytest.skip("column is not a mixin (e.g. Quantity subclass in Table)") if isinstance(t[name], NdarrayMixin): pytest.xfail("NdarrayMixin not supported") t.write(filename, format="parquet") t2 = table_cls.read(filename, format="parquet") assert t.colnames == t2.colnames for colname in t.colnames: assert_objects_equal(t[colname], t2[colname], compare_attrs[colname]) # Special case to make sure Column type doesn't leak into Time class data if name.startswith("tm"): assert t2[name]._time.jd1.__class__ is np.ndarray assert t2[name]._time.jd2.__class__ is np.ndarray def test_round_trip_masked_table_default(tmp_path): """Test round-trip of MaskedColumn through Parquet using default serialization that writes a separate mask column. Note: >>> simple_table(masked=True) <Table masked=True length=3> a b c int64 float64 str1 ----- ------- ---- -- 1.0 c 2 2.0 -- 3 -- e """ filename = tmp_path / "test.parquet" t = simple_table(masked=True) # int, float, and str cols with one masked element t["c"] = [b"c", b"d", b"e"] t["c"].mask[1] = True t.write(filename, format="parquet") t2 = Table.read(filename) assert t2.masked is False assert t2.colnames == t.colnames for name in t2.colnames: assert np.all(t2[name].mask == t[name].mask) assert np.all(t2[name] == t[name]) # Data under the mask round-trips also (unmask data to show this). t[name].mask = False t2[name].mask = False assert np.all(t2[name] == t[name]) @pytest.mark.parametrize("table_cls", (Table, QTable)) def test_parquet_mixins_read_one_name(table_cls, tmp_path): """Test write all cols at once, and read one at a time.""" filename = tmp_path / "test_simple.parquet" names = sorted(mixin_cols) t = table_cls([mixin_cols[name] for name in names], names=names) t.meta["C"] = "spam" t.meta["comments"] = ["this", "is", "a", "comment"] t.meta["history"] = ["first", "second", "third"] t.write(filename, format="parquet") for name in names: t2 = table_cls.read(filename, format="parquet", include_names=[name]) assert t2.meta["C"] == "spam" assert t2.meta["comments"] == ["this", "is", "a", "comment"] assert t2.meta["history"] == ["first", "second", "third"] assert t2.colnames == [name] @pytest.mark.parametrize("table_cls", (Table, QTable)) def test_parquet_mixins_read_exclude_names(table_cls, tmp_path): """Test write all cols at once, and read all but one at a time.""" filename = tmp_path / "test_simple.parquet" names = sorted(mixin_cols) t = table_cls([mixin_cols[name] for name in names], names=names) t.meta["C"] = "spam" t.meta["comments"] = ["this", "is", "a", "comment"] t.meta["history"] = ["first", "second", "third"] t.write(filename, format="parquet") t2 = table_cls.read(filename, format="parquet", exclude_names=names[0:5]) assert t.colnames[5:] == t2.colnames @pytest.mark.parametrize("table_cls", (Table, QTable)) def test_parquet_mixins_read_no_columns(table_cls, tmp_path): """Test write all cols at once, and try to read no valid columns.""" filename = tmp_path / "test_simple.parquet" names = sorted(mixin_cols) t = table_cls([mixin_cols[name] for name in names], names=names) t.meta["C"] = "spam" t.meta["comments"] = ["this", "is", "a", "comment"] t.meta["history"] = ["first", "second", "third"] t.write(filename, format="parquet") with pytest.raises(ValueError, match="No include_names specified"): t2 = table_cls.read( filename, format="parquet", include_names=["not_a_column", "also_not_a_column"], ) @pytest.mark.parametrize("table_cls", (Table, QTable)) def test_parquet_mixins_read_schema(table_cls, tmp_path): """Test write all cols at once, and read the schema.""" filename = tmp_path / "test_simple.parquet" names = sorted(mixin_cols) t = table_cls([mixin_cols[name] for name in names], names=names) t.meta["C"] = "spam" t.meta["comments"] = ["this", "is", "a", "comment"] t.meta["history"] = ["first", "second", "third"] t.write(filename, format="parquet") t2 = table_cls.read(filename, format="parquet", schema_only=True) assert t2.meta["C"] == "spam" assert t2.meta["comments"] == ["this", "is", "a", "comment"] assert t2.meta["history"] == ["first", "second", "third"] assert t.colnames == t2.colnames assert len(t2) == 0 def test_parquet_filter(tmp_path): """Test reading a parquet file with a filter.""" filename = tmp_path / "test_simple.parquet" t1 = Table() t1["a"] = Column(data=np.arange(100), dtype=np.int32) t1["b"] = Column(data=np.arange(100, 0, -1), dtype=np.float64) t1.write(filename, overwrite=True) t2 = Table.read(filename, filters=[("a", "<", 50)]) assert t2["a"].max() < 50 t2 = Table.read(filename, filters=[("b", "<", 50)]) assert t2["b"].max() < 50 def test_parquet_read_generic(tmp_path): """Test reading a generic parquet file.""" filename = tmp_path / "test_generic.parq" t1 = Table() for dtype in ALL_DTYPES: values = _default_values(dtype) t1.add_column(Column(name=str(dtype), data=np.array(values, dtype=dtype))) # Write the table generically via pyarrow.parquet names = t1.dtype.names type_list = [ (name, pyarrow.from_numpy_dtype(t1[name].dtype.type)) for name in names ] schema = pyarrow.schema(type_list) _, parquet, writer_version = get_pyarrow() # We use version='2.0' for full support of datatypes including uint32. with parquet.ParquetWriter(filename, schema, version=writer_version) as writer: arrays = [pyarrow.array(t1[name].data) for name in names] writer.write_table(pyarrow.Table.from_arrays(arrays, schema=schema)) with pytest.warns(AstropyUserWarning, match="No table::len"): t2 = Table.read(filename) for dtype in ALL_DTYPES: values = _default_values(dtype) assert np.all(t2[str(dtype)] == values) assert t2[str(dtype)].dtype == dtype @pytest.mark.skipif(not HAS_PANDAS, reason="requires pandas") def test_parquet_read_pandas(tmp_path): """Test reading a pandas parquet file.""" filename = tmp_path / "test_pandas.parq" t1 = Table() for dtype in ALL_DTYPES: values = _default_values(dtype) t1.add_column(Column(name=str(dtype), data=np.array(values, dtype=dtype))) df = t1.to_pandas() # We use version='2.0' for full support of datatypes including uint32. _, _, writer_version = get_pyarrow() df.to_parquet(filename, version=writer_version) with pytest.warns(AstropyUserWarning, match="No table::len"): t2 = Table.read(filename) for dtype in ALL_DTYPES: values = _default_values(dtype) assert np.all(t2[str(dtype)] == values) assert t2[str(dtype)].dtype == dtype
381b2e1be6207976f83b0a0fdd26b33ebfc880de5a18e70175252a1656df4118
# Licensed under a 3-clause BSD style license - see LICENSE.rst """ This module tests some of the methods related to YAML serialization. """ from io import StringIO import numpy as np import pytest from astropy import units as u from astropy.coordinates import ( Angle, CartesianDifferential, CartesianRepresentation, EarthLocation, Latitude, Longitude, SkyCoord, SphericalCosLatDifferential, SphericalDifferential, SphericalRepresentation, UnitSphericalRepresentation, ) from astropy.coordinates.tests.test_representation import representation_equal from astropy.io.misc.yaml import dump, load, load_all from astropy.table import QTable, SerializedColumn from astropy.time import Time @pytest.mark.parametrize( "c", [ True, np.uint8(8), np.int16(4), np.int32(1), np.int64(3), np.int64(2**63 - 1), 2.0, np.float64(), 3 + 4j, np.complex_(3 + 4j), np.complex64(3 + 4j), np.complex128(1.0 - 2**-52 + 1j * (1.0 - 2**-52)), ], ) def test_numpy_types(c): cy = load(dump(c)) assert c == cy @pytest.mark.parametrize( "c", [u.m, u.m / u.s, u.hPa, u.dimensionless_unscaled, u.Unit("m, (cm, um)")] ) def test_unit(c): cy = load(dump(c)) if isinstance(c, (u.CompositeUnit, u.StructuredUnit)): assert c == cy else: assert c is cy @pytest.mark.parametrize("c", [u.Unit("bakers_dozen", 13 * u.one), u.def_unit("magic")]) def test_custom_unit(c): s = dump(c) with pytest.warns(u.UnitsWarning, match=f"'{c!s}' did not parse") as w: cy = load(s) assert len(w) == 1 assert isinstance(cy, u.UnrecognizedUnit) assert str(cy) == str(c) with u.add_enabled_units(c): cy2 = load(s) assert cy2 is c @pytest.mark.parametrize( "c", [ Angle("1 2 3", unit="deg"), Longitude("1 2 3", unit="deg"), Latitude("1 2 3", unit="deg"), [[1], [3]] * u.m, np.array([[1, 2], [3, 4]], order="F"), np.array([[1, 2], [3, 4]], order="C"), np.array([1, 2, 3, 4])[::2], np.array([(1.0, 2), (3.0, 4)], dtype="f8,i4"), # array with structured dtype. np.array((1.0, 2), dtype="f8,i4"), # array scalar with structured dtype. np.array((1.0, 2), dtype="f8,i4")[()], # numpy void. np.array((1.0, 2.0), dtype="f8,f8") * u.s, # Quantity structured scalar. [ ((1.0, 2.0, 3.0), (4.0, 5.0, 6.0)), # Quantity with structured unit. ((11.0, 12.0, 13.0), (14.0, 15.0, 16.0)), ] * u.Unit("m, m/s"), np.array( [ ((1.0, 2.0, 3.0), (4.0, 5.0, 6.0)), ((11.0, 12.0, 13.0), (14.0, 15.0, 16.0)), ], dtype=[("p", "3f8"), ("v", "3f8")], ) * u.Unit("m, m/s"), ], ) def test_ndarray_subclasses(c): cy = load(dump(c)) assert np.all(c == cy) assert c.shape == cy.shape assert c.dtype == cy.dtype assert type(c) is type(cy) cc = "C_CONTIGUOUS" fc = "F_CONTIGUOUS" if c.flags[cc] or c.flags[fc]: assert c.flags[cc] == cy.flags[cc] assert c.flags[fc] == cy.flags[fc] else: # Original was not contiguous but round-trip version # should be c-contig. assert cy.flags[cc] if hasattr(c, "unit"): assert c.unit == cy.unit def compare_coord(c, cy): assert c.shape == cy.shape assert c.frame.name == cy.frame.name assert list(c.frame_attributes) == list(cy.frame_attributes) for attr in c.frame_attributes: assert getattr(c, attr) == getattr(cy, attr) assert list(c.representation_component_names) == list( cy.representation_component_names ) for name in c.representation_component_names: assert np.all(getattr(c, attr) == getattr(cy, attr)) @pytest.mark.parametrize("frame", ["fk4", "altaz"]) def test_skycoord(frame): c = SkyCoord( [[1, 2], [3, 4]], [[5, 6], [7, 8]], unit="deg", frame=frame, obstime=Time("2016-01-02"), location=EarthLocation(1000, 2000, 3000, unit=u.km), ) cy = load(dump(c)) compare_coord(c, cy) @pytest.mark.parametrize( "rep", [ CartesianRepresentation(1 * u.m, 2.0 * u.m, 3.0 * u.m), SphericalRepresentation( [[1, 2], [3, 4]] * u.deg, [[5, 6], [7, 8]] * u.deg, 10 * u.pc ), UnitSphericalRepresentation(0 * u.deg, 10 * u.deg), SphericalCosLatDifferential( [[1.0], [2.0]] * u.mas / u.yr, [4.0, 5.0] * u.mas / u.yr, [[[10]], [[20]]] * u.km / u.s, ), CartesianDifferential([10, 20, 30] * u.km / u.s), CartesianRepresentation( [1, 2, 3] * u.m, differentials=CartesianDifferential([10, 20, 30] * u.km / u.s), ), SphericalRepresentation( [[1, 2], [3, 4]] * u.deg, [[5, 6], [7, 8]] * u.deg, 10 * u.pc, differentials={ "s": SphericalDifferential( [[0.0, 1.0], [2.0, 3.0]] * u.mas / u.yr, [[4.0, 5.0], [6.0, 7.0]] * u.mas / u.yr, 10 * u.km / u.s, ) }, ), ], ) def test_representations(rep): rrep = load(dump(rep)) assert np.all(representation_equal(rrep, rep)) def _get_time(): t = Time( [[1], [2]], format="cxcsec", location=EarthLocation(1000, 2000, 3000, unit=u.km) ) t.format = "iso" t.precision = 5 t.delta_ut1_utc = np.array([[3.0], [4.0]]) t.delta_tdb_tt = np.array([[5.0], [6.0]]) t.out_subfmt = "date_hm" return t def compare_time(t, ty): assert type(t) is type(ty) assert np.all(t == ty) for attr in ( "shape", "jd1", "jd2", "format", "scale", "precision", "in_subfmt", "out_subfmt", "location", "delta_ut1_utc", "delta_tdb_tt", ): assert np.all(getattr(t, attr) == getattr(ty, attr)) def test_time(): t = _get_time() ty = load(dump(t)) compare_time(t, ty) def test_timedelta(): t = _get_time() dt = t - t + 0.1234556 * u.s dty = load(dump(dt)) assert type(dt) is type(dty) for attr in ("shape", "jd1", "jd2", "format", "scale"): assert np.all(getattr(dt, attr) == getattr(dty, attr)) def test_serialized_column(): sc = SerializedColumn({"name": "hello", "other": 1, "other2": 2.0}) scy = load(dump(sc)) assert sc == scy def test_load_all(): t = _get_time() unit = u.m / u.s c = SkyCoord( [[1, 2], [3, 4]], [[5, 6], [7, 8]], unit="deg", frame="fk4", obstime=Time("2016-01-02"), location=EarthLocation(1000, 2000, 3000, unit=u.km), ) # Make a multi-document stream out = "---\n" + dump(t) + "---\n" + dump(unit) + "---\n" + dump(c) ty, unity, cy = list(load_all(out)) compare_time(t, ty) compare_coord(c, cy) assert unity == unit def test_ecsv_astropy_objects_in_meta(): """ Test that astropy core objects in ``meta`` are serialized. """ t = QTable([[1, 2] * u.m, [4, 5]], names=["a", "b"]) tm = _get_time() c = SkyCoord( [[1, 2], [3, 4]], [[5, 6], [7, 8]], unit="deg", frame="fk4", obstime=Time("2016-01-02"), location=EarthLocation(1000, 2000, 3000, unit=u.km), ) unit = u.m / u.s t.meta = {"tm": tm, "c": c, "unit": unit} out = StringIO() t.write(out, format="ascii.ecsv") t2 = QTable.read(out.getvalue(), format="ascii.ecsv") compare_time(tm, t2.meta["tm"]) compare_coord(c, t2.meta["c"]) assert t2.meta["unit"] == unit
268e73a5fec61e4fb795e70faa9aee85ad5b60ee9eeddeba559ef5cea5fff55e
# Licensed under a 3-clause BSD style license - see LICENSE.rst import os import numpy as np import pytest from astropy.io.misc.hdf5 import meta_path from astropy.table import Column, QTable, Table from astropy.table.table_helpers import simple_table from astropy.units import allclose as quantity_allclose from astropy.units.quantity import QuantityInfo from astropy.utils.compat import NUMPY_LT_1_22 from astropy.utils.compat.optional_deps import HAS_H5PY from astropy.utils.data import get_pkg_data_filename from astropy.utils.exceptions import AstropyUserWarning from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH if HAS_H5PY: import h5py from astropy.io.tests.mixin_columns import compare_attrs, mixin_cols, serialized_names # HDF5 does not support object dtype (since it stores binary representations). unsupported_cols = { name: col for name, col in mixin_cols.items() if (isinstance(col, np.ndarray) and col.dtype.kind == "O") } mixin_cols = { name: col for name, col in mixin_cols.items() if name not in unsupported_cols } ALL_DTYPES = [ np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64, np.float32, np.float64, np.bool_, "|S3", ] def _default_values(dtype): if dtype == np.bool_: return [0, 1, 1] elif dtype == "|S3": return [b"abc", b"def", b"ghi"] else: return [1, 2, 3] @pytest.fixture def home_is_tmpdir(monkeypatch, tmp_path): """ Pytest fixture to run a test case with tilde-prefixed paths. In the tilde-path case, environment variables are temporarily modified so that '~' resolves to the temp directory. """ # For Unix monkeypatch.setenv("HOME", str(tmp_path)) # For Windows monkeypatch.setenv("USERPROFILE", str(tmp_path)) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_write_nopath(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) with pytest.warns( UserWarning, match="table path was not set via the path= argument" ): t1.write(test_file) t1 = Table.read(test_file, path="__astropy_table__") @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_write_nopath_nonempty(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, path="bubu") with pytest.raises(ValueError) as exc: t1.write(test_file, append=True) assert "table path should always be set via the path=" in exc.value.args[0] @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_notable_nopath(tmp_path): test_file = tmp_path / "test.hdf5" h5py.File(test_file, "w").close() # create empty file with pytest.raises(ValueError, match="no table found in HDF5 group /"): Table.read(test_file, path="/", format="hdf5") @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_nopath(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, path="the_table") t2 = Table.read(test_file) assert np.all(t1["a"] == t2["a"]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_nopath_multi_tables(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, path="the_table") t1.write(test_file, path="the_table_but_different", append=True, overwrite=True) with pytest.warns( AstropyUserWarning, match=r"path= was not specified but multiple tables" ): t2 = Table.read(test_file) assert np.all(t1["a"] == t2["a"]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_write_invalid_path(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) with pytest.raises(ValueError) as exc: t1.write(test_file, path="test/") assert exc.value.args[0] == "table path should end with table name, not /" @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_invalid_path(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, path="the_table") with pytest.raises(OSError) as exc: Table.read(test_file, path="test/") assert exc.value.args[0] == "Path test/ does not exist" @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_missing_group(tmp_path): test_file = tmp_path / "test.hdf5" h5py.File(test_file, "w").close() # create empty file with pytest.raises(OSError) as exc: Table.read(test_file, path="test/path/table") assert exc.value.args[0] == "Path test/path/table does not exist" @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_missing_table(tmp_path): test_file = tmp_path / "test.hdf5" with h5py.File(test_file, "w") as f: f.create_group("test").create_group("path") with pytest.raises(OSError) as exc: Table.read(test_file, path="test/path/table") assert exc.value.args[0] == "Path test/path/table does not exist" @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_missing_group_fileobj(tmp_path): test_file = tmp_path / "test.hdf5" with h5py.File(test_file, "w") as f: with pytest.raises(OSError) as exc: Table.read(f, path="test/path/table") assert exc.value.args[0] == "Path test/path/table does not exist" @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_write_simple(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, path="the_table") t2 = Table.read(test_file, path="the_table") assert np.all(t2["a"] == [1, 2, 3]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_write_existing_table(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, path="the_table") with pytest.raises(OSError) as exc: t1.write(test_file, path="the_table", append=True) assert exc.value.args[0] == "Table the_table already exists" @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_write_memory(): with h5py.File("test", "w", driver="core", backing_store=False) as output_file: t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(output_file, path="the_table") t2 = Table.read(output_file, path="the_table") assert np.all(t2["a"] == [1, 2, 3]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_write_existing(tmp_path): test_file = tmp_path / "test.hdf5" h5py.File(test_file, "w").close() # create empty file t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH): t1.write(test_file, path="the_table") @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_write_existing_overwrite(tmp_path): test_file = tmp_path / "test.hdf5" h5py.File(test_file, "w").close() # create empty file t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, path="the_table", overwrite=True) t2 = Table.read(test_file, path="the_table") assert np.all(t2["a"] == [1, 2, 3]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_write_existing_append(tmp_path): test_file = tmp_path / "test.hdf5" h5py.File(test_file, "w").close() # create empty file t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, path="the_table_1", append=True) t1.write(test_file, path="the_table_2", append=True) t2 = Table.read(test_file, path="the_table_1") assert np.all(t2["a"] == [1, 2, 3]) t3 = Table.read(test_file, path="the_table_2") assert np.all(t3["a"] == [1, 2, 3]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_write_existing_append_groups(tmp_path): test_file = tmp_path / "test.hdf5" with h5py.File(test_file, "w") as f: f.create_group("test_1") t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, path="test_1/the_table_1", append=True) t1.write(test_file, path="test_2/the_table_2", append=True) t2 = Table.read(test_file, path="test_1/the_table_1") assert np.all(t2["a"] == [1, 2, 3]) t3 = Table.read(test_file, path="test_2/the_table_2") assert np.all(t3["a"] == [1, 2, 3]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_write_existing_append_overwrite(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, path="table1") t1.write(test_file, path="table2", append=True) t1v2 = Table() t1v2.add_column(Column(name="a", data=[4, 5, 6])) with pytest.raises(OSError) as exc: t1v2.write(test_file, path="table1", append=True) assert exc.value.args[0] == "Table table1 already exists" t1v2.write(test_file, path="table1", append=True, overwrite=True) t2 = Table.read(test_file, path="table1") assert np.all(t2["a"] == [4, 5, 6]) t3 = Table.read(test_file, path="table2") assert np.all(t3["a"] == [1, 2, 3]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_fileobj(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, path="the_table") import h5py with h5py.File(test_file, "r") as input_file: t2 = Table.read(input_file, path="the_table") assert np.all(t2["a"] == [1, 2, 3]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_filobj_path(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, path="path/to/data/the_table") import h5py with h5py.File(test_file, "r") as input_file: t2 = Table.read(input_file, path="path/to/data/the_table") assert np.all(t2["a"] == [1, 2, 3]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_filobj_group_path(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(test_file, path="path/to/data/the_table") import h5py with h5py.File(test_file, "r") as input_file: t2 = Table.read(input_file["path/to"], path="data/the_table") assert np.all(t2["a"] == [1, 2, 3]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_wrong_fileobj(): class FakeFile: def read(self): pass f = FakeFile() with pytest.raises(TypeError, match="h5py can only open regular files"): Table.read(f, format="hdf5") @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_write_fileobj(tmp_path): test_file = tmp_path / "test.hdf5" import h5py with h5py.File(test_file, "w") as output_file: t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(output_file, path="the_table") t2 = Table.read(test_file, path="the_table") assert np.all(t2["a"] == [1, 2, 3]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_write_create_dataset_kwargs(tmp_path): test_file = tmp_path / "test.hdf5" the_path = "the_table" import h5py with h5py.File(test_file, "w") as output_file: t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(output_file, path=the_path, maxshape=(None,)) # A roundabout way of checking this, but the table created above should be # resizable if the kwarg was passed through successfully t2 = Table() t2.add_column(Column(name="a", data=[4, 5])) with h5py.File(test_file, "a") as output_file: output_file[the_path].resize((len(t1) + len(t2),)) output_file[the_path][len(t1) :] = t2.as_array() t3 = Table.read(test_file, path="the_table") assert np.all(t3["a"] == [1, 2, 3, 4, 5]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_write_filobj_group(tmp_path): test_file = tmp_path / "test.hdf5" import h5py with h5py.File(test_file, "w") as output_file: t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(output_file, path="path/to/data/the_table") t2 = Table.read(test_file, path="path/to/data/the_table") assert np.all(t2["a"] == [1, 2, 3]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_write_wrong_type(): t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) with pytest.raises(TypeError) as exc: t1.write(1212, path="path/to/data/the_table", format="hdf5") assert ( exc.value.args[0] == "output should be a string or an h5py File or Group object" ) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") @pytest.mark.parametrize("dtype", ALL_DTYPES) def test_preserve_single_dtypes(tmp_path, dtype): test_file = tmp_path / "test.hdf5" values = _default_values(dtype) t1 = Table() t1.add_column(Column(name="a", data=np.array(values, dtype=dtype))) t1.write(test_file, path="the_table") t2 = Table.read(test_file, path="the_table") assert np.all(t2["a"] == values) assert t2["a"].dtype == dtype @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_preserve_all_dtypes(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() for dtype in ALL_DTYPES: values = _default_values(dtype) t1.add_column(Column(name=str(dtype), data=np.array(values, dtype=dtype))) t1.write(test_file, path="the_table") t2 = Table.read(test_file, path="the_table") for dtype in ALL_DTYPES: values = _default_values(dtype) assert np.all(t2[str(dtype)] == values) assert t2[str(dtype)].dtype == dtype @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_preserve_meta(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.meta["a"] = 1 t1.meta["b"] = "hello" t1.meta["c"] = 3.14159 t1.meta["d"] = True t1.meta["e"] = np.array([1, 2, 3]) t1.write(test_file, path="the_table") t2 = Table.read(test_file, path="the_table") for key in t1.meta: assert np.all(t1.meta[key] == t2.meta[key]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_preserve_serialized(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1["a"] = Column(data=[1, 2, 3], unit="s") t1["a"].meta["a0"] = "A0" t1["a"].meta["a1"] = {"a1": [0, 1]} t1["a"].format = "7.3f" t1["a"].description = "A column" t1.meta["b"] = 1 t1.meta["c"] = {"c0": [0, 1]} t1.write(test_file, path="the_table", serialize_meta=True, overwrite=True) t2 = Table.read(test_file, path="the_table") assert t1["a"].unit == t2["a"].unit assert t1["a"].format == t2["a"].format assert t1["a"].description == t2["a"].description assert t1["a"].meta == t2["a"].meta assert t1.meta == t2.meta # Check that the meta table is fixed-width bytes (see #11299) h5 = h5py.File(test_file, "r") meta_lines = h5[meta_path("the_table")] assert meta_lines.dtype.kind == "S" @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_preserve_serialized_old_meta_format(): """Test the old meta format Only for some files created prior to v4.0, in compatibility mode. """ test_file = get_pkg_data_filename("data/old_meta_example.hdf5") t1 = Table() t1["a"] = Column(data=[1, 2, 3], unit="s") t1["a"].meta["a0"] = "A0" t1["a"].meta["a1"] = {"a1": [0, 1]} t1["a"].format = "7.3f" t1["a"].description = "A column" t1.meta["b"] = 1 t1.meta["c"] = {"c0": [0, 1]} t2 = Table.read(test_file, path="the_table") assert t1["a"].unit == t2["a"].unit assert t1["a"].format == t2["a"].format assert t1["a"].description == t2["a"].description assert t1["a"].meta == t2["a"].meta assert t1.meta == t2.meta @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_preserve_serialized_in_complicated_path(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1["a"] = Column(data=[1, 2, 3], unit="s") t1["a"].meta["a0"] = "A0" t1["a"].meta["a1"] = {"a1": [0, 1]} t1["a"].format = "7.3f" t1["a"].description = "A column" t1.meta["b"] = 1 t1.meta["c"] = {"c0": [0, 1]} t1.write( test_file, path="the_table/complicated/path", serialize_meta=True, overwrite=True, ) t2 = Table.read(test_file, path="the_table/complicated/path") assert t1["a"].format == t2["a"].format assert t1["a"].unit == t2["a"].unit assert t1["a"].description == t2["a"].description assert t1["a"].meta == t2["a"].meta assert t1.meta == t2.meta @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_metadata_very_large(tmp_path): """Test that very large datasets work, now!""" test_file = tmp_path / "test.hdf5" t1 = Table() t1["a"] = Column(data=[1, 2, 3], unit="s") t1["a"].meta["a0"] = "A0" t1["a"].meta["a1"] = {"a1": [0, 1]} t1["a"].format = "7.3f" t1["a"].description = "A column" t1.meta["b"] = 1 t1.meta["c"] = {"c0": [0, 1]} t1.meta["meta_big"] = "0" * (2**16 + 1) t1.meta["meta_biggerstill"] = "0" * (2**18) t1.write(test_file, path="the_table", serialize_meta=True, overwrite=True) t2 = Table.read(test_file, path="the_table") assert t1["a"].unit == t2["a"].unit assert t1["a"].format == t2["a"].format assert t1["a"].description == t2["a"].description assert t1["a"].meta == t2["a"].meta assert t1.meta == t2.meta @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_skip_meta(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.meta["a"] = 1 t1.meta["b"] = "hello" t1.meta["c"] = 3.14159 t1.meta["d"] = True t1.meta["e"] = np.array([1, 2, 3]) t1.meta["f"] = str wtext = ( f"Attribute `f` of type {type(t1.meta['f'])} cannot be written to HDF5 files -" " skipping" ) with pytest.warns(AstropyUserWarning, match=wtext) as w: t1.write(test_file, path="the_table") assert len(w) == 1 @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_fail_meta_serialize(tmp_path): test_file = tmp_path / "test.hdf5" t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.meta["f"] = str with pytest.raises(Exception) as err: t1.write(test_file, path="the_table", serialize_meta=True) assert "cannot represent an object" in str(err.value) assert "<class 'str'>" in str(err.value) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_h5py_objects(tmp_path): # Regression test - ensure that Datasets are recognized automatically test_file = tmp_path / "test.hdf5" import h5py with h5py.File(test_file, "w") as output_file: t1 = Table() t1.add_column(Column(name="a", data=[1, 2, 3])) t1.write(output_file, path="the_table") f = h5py.File(test_file, mode="r") t2 = Table.read(f, path="the_table") assert np.all(t2["a"] == [1, 2, 3]) t3 = Table.read(f["/"], path="the_table") assert np.all(t3["a"] == [1, 2, 3]) t4 = Table.read(f["the_table"]) assert np.all(t4["a"] == [1, 2, 3]) f.close() # don't raise an error in 'test --open-files' @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_write_unicode_to_hdf5(tmp_path): test_file = tmp_path / "test.hdf5" t = Table() t["p"] = ["a", "b", "c"] t["q"] = [1, 2, 3] t["r"] = [b"a", b"b", b"c"] t["s"] = ["\u2119", "\u01b4", "\u2602"] t.write(test_file, path="the_table", overwrite=True) t1 = Table.read(test_file, path="the_table", character_as_bytes=False) for col, col1 in zip(t.itercols(), t1.itercols()): assert np.all(col == col1) assert np.all(t1["p"].info.dtype.kind == "U") assert np.all(t1["q"].info.dtype.kind == "i") assert np.all(t1["r"].info.dtype.kind == "U") assert np.all(t1["s"].info.dtype.kind == "U") # Test default (character_as_bytes=True) t2 = Table.read(test_file, path="the_table") for col, col1 in zip(t.itercols(), t2.itercols()): assert np.all(col == col1) assert np.all(t2["p"].info.dtype.kind == "S") assert np.all(t2["q"].info.dtype.kind == "i") assert np.all(t2["r"].info.dtype.kind == "S") assert np.all(t2["s"].info.dtype.kind == "S") def assert_objects_equal(obj1, obj2, attrs, compare_class=True): if compare_class: assert obj1.__class__ is obj2.__class__ info_attrs = [ "info.name", "info.format", "info.unit", "info.description", "info.meta", "info.dtype", ] for attr in attrs + info_attrs: a1 = obj1 a2 = obj2 for subattr in attr.split("."): try: a1 = getattr(a1, subattr) a2 = getattr(a2, subattr) except AttributeError: a1 = a1[subattr] a2 = a2[subattr] # Mixin info.meta can None instead of empty OrderedDict(), #6720 would # fix this. if attr == "info.meta": if a1 is None: a1 = {} if a2 is None: a2 = {} if isinstance(a1, np.ndarray) and a1.dtype.kind == "f": assert quantity_allclose(a1, a2, rtol=1e-15) elif isinstance(a1, np.dtype): # HDF5 does not perfectly preserve dtype: byte order can change, and # unicode gets stored as bytes. So, we just check safe casting, to # ensure we do not, e.g., accidentally change integer to float, etc. if NUMPY_LT_1_22 and a1.names: # For old numpy, can_cast does not deal well with structured dtype. assert a1.names == a2.names else: assert np.can_cast(a2, a1, casting="safe") else: assert np.all(a1 == a2) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_hdf5_mixins_qtable_to_table(tmp_path): """Test writing as QTable and reading as Table. Ensure correct classes come out. """ filename = tmp_path / "test_simple.hdf5" names = sorted(mixin_cols) t = QTable([mixin_cols[name] for name in names], names=names) t.write(filename, format="hdf5", path="root", serialize_meta=True) t2 = Table.read(filename, format="hdf5", path="root") assert t.colnames == t2.colnames for name, col in t.columns.items(): col2 = t2[name] attrs = compare_attrs[name] compare_class = True if isinstance(col.info, QuantityInfo): # Downgrade Quantity to Column + unit assert type(col2) is Column # Class-specific attributes like `value` or `wrap_angle` are lost. attrs = ["unit"] compare_class = False # Compare data values here (assert_objects_equal doesn't know how in this case) assert np.all(col.value == col2) assert_objects_equal(col, col2, attrs, compare_class) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") @pytest.mark.parametrize("table_cls", (Table, QTable)) def test_hdf5_mixins_as_one(table_cls, tmp_path): """Test write/read all cols at once and validate intermediate column names""" filename = tmp_path / "test_simple.hdf5" names = sorted(mixin_cols) all_serialized_names = [] for name in names: all_serialized_names.extend(serialized_names[name]) t = table_cls([mixin_cols[name] for name in names], names=names) t.meta["C"] = "spam" t.meta["comments"] = ["this", "is", "a", "comment"] t.meta["history"] = ["first", "second", "third"] t.write(filename, format="hdf5", path="root", serialize_meta=True) t2 = table_cls.read(filename, format="hdf5", path="root") assert t2.meta["C"] == "spam" assert t2.meta["comments"] == ["this", "is", "a", "comment"] assert t2.meta["history"] == ["first", "second", "third"] assert t.colnames == t2.colnames # Read directly via hdf5 and confirm column names h5 = h5py.File(filename, "r") h5_names = list(h5["root"].dtype.names) assert h5_names == all_serialized_names h5.close() @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") @pytest.mark.parametrize("name_col", list(mixin_cols.items())) @pytest.mark.parametrize("table_cls", (Table, QTable)) def test_hdf5_mixins_per_column(table_cls, name_col, tmp_path): """Test write/read one col at a time and do detailed validation""" filename = tmp_path / "test_simple.hdf5" name, col = name_col c = [1.0, 2.0] t = table_cls([c, col, c], names=["c1", name, "c2"]) t[name].info.description = "my description" t[name].info.meta = {"list": list(range(50)), "dict": {"a": "b" * 200}} if not t.has_mixin_columns: pytest.skip("column is not a mixin (e.g. Quantity subclass in Table)") t.write(filename, format="hdf5", path="root", serialize_meta=True) t2 = table_cls.read(filename, format="hdf5", path="root") assert t.colnames == t2.colnames for colname in t.colnames: compare = ["data"] if colname in ("c1", "c2") else compare_attrs[colname] assert_objects_equal(t[colname], t2[colname], compare) # Special case to make sure Column type doesn't leak into Time class data if name.startswith("tm"): assert t2[name]._time.jd1.__class__ is np.ndarray assert t2[name]._time.jd2.__class__ is np.ndarray @pytest.mark.parametrize("name_col", unsupported_cols.items()) @pytest.mark.xfail(reason="column type unsupported") def test_fits_unsupported_mixin(self, name_col, tmp_path): # Check that we actually fail in writing unsupported columns defined # on top. filename = tmp_path / "test_simple.fits" name, col = name_col Table([col], names=[name]).write( filename, format="hdf5", path="root", serialize_meta=True ) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_round_trip_masked_table_default(tmp_path): """Test round-trip of MaskedColumn through HDF5 using default serialization that writes a separate mask column. Note: >>> simple_table(masked=True) <Table masked=True length=3> a b c int64 float64 str1 ----- ------- ---- -- 1.0 c 2 2.0 -- 3 -- e """ filename = tmp_path / "test.h5" t = simple_table(masked=True) # int, float, and str cols with one masked element t["c"] = [b"c", b"d", b"e"] t["c"].mask[1] = True t.write(filename, format="hdf5", path="root", serialize_meta=True) t2 = Table.read(filename) assert t2.masked is False assert t2.colnames == t.colnames for name in t2.colnames: assert np.all(t2[name].mask == t[name].mask) assert np.all(t2[name] == t[name]) # Data under the mask round-trips also (unmask data to show this). t[name].mask = False t2[name].mask = False assert np.all(t2[name] == t[name]) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_overwrite_serialized_meta(): # This used to cause an error because the meta data table # was not removed from the existing file. with h5py.File("test_data.h5", "w", driver="core", backing_store=False) as out: t1 = Table() t1.add_column(Column(data=[4, 8, 15], unit="cm")) t1.write(out, path="data", serialize_meta=True) t2 = Table.read(out, path="data") assert all(t1 == t2) assert t1.info(out=None) == t2.info(out=None) t3 = Table() t3.add_column(Column(data=[16, 23, 42], unit="g")) t3.write(out, path="data", serialize_meta=True, append=True, overwrite=True) t2 = Table.read(out, path="data") assert all(t3 == t2) assert t3.info(out=None) == t2.info(out=None) @pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") def test_read_write_tilde_path(home_is_tmpdir): test_file = os.path.join("~", "test.hdf5") t1 = Table() t1["a"] = [1, 2, 3] t1.write(test_file, path="the_table") t1 = Table.read(test_file, path="the_table") t1 = Table.read(test_file, path="the_table", format="hdf5") # Ensure the data wasn't written to the literal tilde-prefixed path assert not os.path.exists(test_file)
62b076083effdfc6bc15a8a0a03892c278e4f5cb430f718ee00ce5254bcbb476
# Licensed under a 3-clause BSD style license - see LICENSE.rst import pytest from astropy.io.misc import fnpickle, fnunpickle def test_fnpickling_simple(tmp_path): """ Tests the `fnpickle` and `fnupickle` functions' basic operation by pickling and unpickling a string, using both a filename and a file. """ fn = str(tmp_path / "test1.pickle") obj1 = "astring" fnpickle(obj1, fn) res = fnunpickle(fn, 0) assert obj1 == res # now try with a file-like object instead of a string with open(fn, "wb") as f: fnpickle(obj1, f) with open(fn, "rb") as f: res = fnunpickle(f) assert obj1 == res class ToBePickled: def __init__(self, item): self.item = item def __eq__(self, other): if isinstance(other, ToBePickled): return self.item == other.item else: return False def test_fnpickling_class(tmp_path): """ Tests the `fnpickle` and `fnupickle` functions' ability to pickle and unpickle custom classes. """ fn = str(tmp_path / "test2.pickle") obj1 = "astring" obj2 = ToBePickled(obj1) fnpickle(obj2, fn) res = fnunpickle(fn) assert res == obj2 def test_fnpickling_protocol(tmp_path): """ Tests the `fnpickle` and `fnupickle` functions' ability to pickle and unpickle pickle files from all protcols. """ import pickle obj1 = "astring" obj2 = ToBePickled(obj1) for p in range(pickle.HIGHEST_PROTOCOL + 1): fn = str(tmp_path / f"testp{p}.pickle") fnpickle(obj2, fn, protocol=p) res = fnunpickle(fn) assert res == obj2 def test_fnpickling_many(tmp_path): """ Tests the `fnpickle` and `fnupickle` functions' ability to pickle and unpickle multiple objects from a single file. """ fn = str(tmp_path / "test3.pickle") # now try multiples obj3 = 328.3432 obj4 = "blahblahfoo" fnpickle(obj3, fn) fnpickle(obj4, fn, append=True) res = fnunpickle(fn, number=-1) assert len(res) == 2 assert res[0] == obj3 assert res[1] == obj4 fnpickle(obj4, fn, append=True) res = fnunpickle(fn, number=2) assert len(res) == 2 with pytest.raises(EOFError): fnunpickle(fn, number=5)
6a4a8b83856ccfb18cc66afc57bc887b5a55fd2f0fc65f83e65bb332d47f210f
# Licensed under a 3-clause BSD style license - see LICENSE.rst # Define a constant to know if the entry points are installed, since this impacts # whether we can run the tests. from importlib.metadata import entry_points import pytest # TODO: Exclusively use select when Python minversion is 3.10 eps = entry_points() if hasattr(eps, "select"): ep = [entry.name for entry in eps.select(group="asdf_extensions")] else: ep = [entry.name for entry in eps.get("asdf_extensions", [])] ASDF_ENTRY_INSTALLED = "astropy" in ep and "astropy-asdf" in ep del entry_points, eps, ep if not ASDF_ENTRY_INSTALLED: pytest.skip( "The astropy asdf entry points are not installed", allow_module_level=True )
a3959c90324e7bd385c67554d6c117657ecbff8accf2e14b3f40e4eedb30e0b4
# Licensed under a 3-clause BSD style license - see LICENSE.rst import pytest asdf = pytest.importorskip("asdf") from astropy.table import Table def make_table(): a = [1, 4, 5] b = [2.0, 5.0, 8.2] c = ["x", "y", "z"] return Table([a, b, c], names=("a", "b", "c"), meta={"name": "first table"}) def test_table_io(tmpdir): tmpfile = str(tmpdir.join("table.asdf")) table = make_table() table.write(tmpfile) # Simple sanity check using ASDF directly with asdf.open(tmpfile) as af: assert "data" in af.keys() assert isinstance(af["data"], Table) assert all(af["data"] == table) # Now test using the table reader new_t = Table.read(tmpfile) assert all(new_t == table) def test_table_io_custom_key(tmpdir): tmpfile = str(tmpdir.join("table.asdf")) table = make_table() table.write(tmpfile, data_key="something") # Simple sanity check using ASDF directly with asdf.open(tmpfile) as af: assert "something" in af.keys() assert "data" not in af.keys() assert isinstance(af["something"], Table) assert all(af["something"] == table) # Now test using the table reader with pytest.raises(KeyError): new_t = Table.read(tmpfile) new_t = Table.read(tmpfile, data_key="something") assert all(new_t == table) def test_table_io_custom_tree(tmpdir): tmpfile = str(tmpdir.join("table.asdf")) table = make_table() def make_custom_tree(tab): return dict(foo=dict(bar=tab)) table.write(tmpfile, make_tree=make_custom_tree) # Simple sanity check using ASDF directly with asdf.open(tmpfile) as af: assert "foo" in af.keys() assert "bar" in af["foo"] assert "data" not in af.keys() assert all(af["foo"]["bar"] == table) # Now test using table reader with pytest.raises(KeyError): new_t = Table.read(tmpfile) def find_table(asdffile): return asdffile["foo"]["bar"] new_t = Table.read(tmpfile, find_table=find_table) assert all(new_t == table)
92a4849101f0dc6cb1f3a92d38afe3d4bccc5560b292f44fd3f49b76fbe9a3b0
# Licensed under a 3-clause BSD style license - see LICENSE.rst from astropy.coordinates.tests.helper import skycoord_equal as _skycoord_equal from astropy.utils.decorators import deprecated __all__ = ["skycoord_equal"] @deprecated("5.1", alternative="astropy.coordinates.tests.helper.skycoord_equal") def skycoord_equal(sc1, sc2): return _skycoord_equal(sc1, sc2)
2abbc8e62a30e80b794dc1ea0f30ea19001761951c7e9e0001d2dcb6c22fbeab
# Licensed under a 3-clause BSD style license - see LICENSE.rst import numpy as np from asdf.tags.core.ndarray import NDArrayType from astropy import table from astropy.io.misc.asdf.types import AstropyAsdfType, AstropyType class TableType: """ This class defines to_tree and from_tree methods that are used by both the AstropyTableType and the AsdfTableType defined below. The behavior is differentiated by the ``_compat`` class attribute. When ``_compat==True``, the behavior will conform to the table schema defined by the ASDF Standard. Otherwise, the behavior will conform to the custom table schema defined by Astropy. """ _compat = False @classmethod def from_tree(cls, node, ctx): # This is getting meta, guys meta = node.get("meta", {}) # This enables us to support files that use the table definition from # the ASDF Standard, rather than the custom one that Astropy defines. if cls._compat: return table.Table(node["columns"], meta=meta) if node.get("qtable", False): t = table.QTable(meta=node.get("meta", {})) else: t = table.Table(meta=node.get("meta", {})) for name, col in zip(node["colnames"], node["columns"]): t[name] = col return t @classmethod def to_tree(cls, data, ctx): columns = [data[name] for name in data.colnames] node = dict(columns=columns) # Files that use the table definition from the ASDF Standard (instead # of the one defined by Astropy) will not contain these fields if not cls._compat: node["colnames"] = data.colnames node["qtable"] = isinstance(data, table.QTable) if data.meta: node["meta"] = data.meta return node @classmethod def assert_equal(cls, old, new): assert old.meta == new.meta try: NDArrayType.assert_equal(np.array(old), np.array(new)) except (AttributeError, TypeError, ValueError): for col0, col1 in zip(old, new): try: NDArrayType.assert_equal(np.array(col0), np.array(col1)) except (AttributeError, TypeError, ValueError): assert col0 == col1 class AstropyTableType(TableType, AstropyType): """ This tag class reads and writes tables that conform to the custom schema that is defined by Astropy (in contrast to the one that is defined by the ASDF Standard). The primary reason for differentiating is to enable the support of Astropy mixin columns, which are not supported by the ASDF Standard. """ name = "table/table" types = ["astropy.table.Table"] requires = ["astropy"] class AsdfTableType(TableType, AstropyAsdfType): """ This tag class allows Astropy to read (and write) ASDF files that use the table definition that is provided by the ASDF Standard (instead of the custom one defined by Astropy). This is important to maintain for cross-compatibility. """ name = "core/table" types = ["astropy.table.Table"] requires = ["astropy"] _compat = True class ColumnType(AstropyAsdfType): name = "core/column" types = ["astropy.table.Column", "astropy.table.MaskedColumn"] requires = ["astropy"] handle_dynamic_subclasses = True @classmethod def from_tree(cls, node, ctx): data = node["data"] name = node["name"] description = node.get("description") unit = node.get("unit") meta = node.get("meta", None) return table.Column( data=data._make_array(), name=name, description=description, unit=unit, meta=meta, ) @classmethod def to_tree(cls, data, ctx): node = { "data": data.data, "name": data.name, } if data.description: node["description"] = data.description if data.unit: node["unit"] = data.unit if data.meta: node["meta"] = data.meta return node @classmethod def assert_equal(cls, old, new): assert old.meta == new.meta assert old.description == new.description assert old.unit == new.unit NDArrayType.assert_equal(np.array(old), np.array(new))
c76ed27d1281383c8a27088da2248c70a588aca6e265350703d6548b7e8866f1
# Licensed under a 3-clause BSD style license - see LICENSE.rst from numpy.testing import assert_array_equal from astropy import table from astropy.io import fits from astropy.io.misc.asdf.types import AstropyAsdfType, AstropyType class FitsType: name = "fits/fits" types = ["astropy.io.fits.HDUList"] requires = ["astropy"] @classmethod def from_tree(cls, data, ctx): hdus = [] first = True for hdu_entry in data: header = fits.Header([fits.Card(*x) for x in hdu_entry["header"]]) data = hdu_entry.get("data") if data is not None: try: data = data.__array__() except ValueError: data = None if first: hdu = fits.PrimaryHDU(data=data, header=header) first = False elif data.dtype.names is not None: hdu = fits.BinTableHDU(data=data, header=header) else: hdu = fits.ImageHDU(data=data, header=header) hdus.append(hdu) hdulist = fits.HDUList(hdus) return hdulist @classmethod def to_tree(cls, hdulist, ctx): units = [] for hdu in hdulist: header_list = [] for card in hdu.header.cards: if card.comment: new_card = [card.keyword, card.value, card.comment] else: if card.value: new_card = [card.keyword, card.value] else: if card.keyword: new_card = [card.keyword] else: new_card = [] header_list.append(new_card) hdu_dict = {} hdu_dict["header"] = header_list if hdu.data is not None: if hdu.data.dtype.names is not None: data = table.Table(hdu.data) else: data = hdu.data hdu_dict["data"] = data units.append(hdu_dict) return units @classmethod def reserve_blocks(cls, data, ctx): for hdu in data: if hdu.data is not None: yield ctx.blocks.find_or_create_block_for_array(hdu.data, ctx) @classmethod def assert_equal(cls, old, new): for hdua, hdub in zip(old, new): assert_array_equal(hdua.data, hdub.data) for carda, cardb in zip(hdua.header.cards, hdub.header.cards): assert tuple(carda) == tuple(cardb) class AstropyFitsType(FitsType, AstropyType): """ This class implements ASDF serialization/deserialization that corresponds to the FITS schema defined by Astropy. It will be used by default when writing new HDUs to ASDF files. """ class AsdfFitsType(FitsType, AstropyAsdfType): """ This class implements ASDF serialization/deserialization that corresponds to the FITS schema defined by the ASDF Standard. It will not be used by default, except when reading files that use the ASDF Standard definition rather than the one defined in Astropy. It will primarily be used for backwards compatibility for reading older files. In the unlikely case that another ASDF implementation uses the FITS schema from the ASDF Standard, this tag could also be used to read a file it generated. """
90ec9e072b9ce25969f8a918d6c7a7f5a3dfff5e104d92adf7e3ad7b0b9d9030
# Licensed under a 3-clause BSD style license - see LICENSE.rst import numpy as np from asdf.versioning import AsdfVersion from astropy.io.misc.asdf.types import AstropyAsdfType, AstropyType from astropy.modeling import functional_models, mappings from astropy.modeling.bounding_box import CompoundBoundingBox, ModelBoundingBox from astropy.modeling.core import CompoundModel from . import _parameter_to_value __all__ = ["TransformType", "IdentityType", "ConstantType"] class TransformType(AstropyAsdfType): version = "1.2.0" requires = ["astropy"] @classmethod def _from_tree_base_transform_members(cls, model, node, ctx): if "name" in node: model.name = node["name"] if "inputs" in node: model.inputs = tuple(node["inputs"]) if "outputs" in node: model.outputs = tuple(node["outputs"]) if "bounding_box" in node: model.bounding_box = node["bounding_box"] elif "selector_args" in node: cbbox_keys = [tuple(key) for key in node["cbbox_keys"]] bbox_dict = dict(zip(cbbox_keys, node["cbbox_values"])) selector_args = node["selector_args"] model.bounding_box = CompoundBoundingBox.validate( model, bbox_dict, selector_args ) param_and_model_constraints = {} for constraint in ["fixed", "bounds"]: if constraint in node: param_and_model_constraints[constraint] = node[constraint] model._initialize_constraints(param_and_model_constraints) if "input_units_equivalencies" in node: # this still writes eqs. for compound, but operates on each sub model if not isinstance(model, CompoundModel): model.input_units_equivalencies = node["input_units_equivalencies"] yield model if "inverse" in node: model.inverse = node["inverse"] @classmethod def from_tree_transform(cls, node, ctx): raise NotImplementedError("Must be implemented in TransformType subclasses") @classmethod def from_tree(cls, node, ctx): model = cls.from_tree_transform(node, ctx) return cls._from_tree_base_transform_members(model, node, ctx) @classmethod def _to_tree_base_transform_members(cls, model, node, ctx): if getattr(model, "_user_inverse", None) is not None: node["inverse"] = model._user_inverse if model.name is not None: node["name"] = model.name node["inputs"] = list(model.inputs) node["outputs"] = list(model.outputs) try: bb = model.bounding_box except NotImplementedError: bb = None if isinstance(bb, ModelBoundingBox): bb = bb.bounding_box(order="C") if model.n_inputs == 1: bb = list(bb) else: bb = [list(item) for item in bb] node["bounding_box"] = bb elif isinstance(bb, CompoundBoundingBox): selector_args = [[sa.index, sa.ignore] for sa in bb.selector_args] node["selector_args"] = selector_args node["cbbox_keys"] = list(bb.bounding_boxes.keys()) bounding_boxes = list(bb.bounding_boxes.values()) if len(model.inputs) - len(selector_args) == 1: node["cbbox_values"] = [ list(sbbox.bounding_box()) for sbbox in bounding_boxes ] else: node["cbbox_values"] = [ [ list(item) for item in sbbox.bounding_box() if np.isfinite(item[0]) ] for sbbox in bounding_boxes ] # model / parameter constraints if not isinstance(model, CompoundModel): fixed_nondefaults = {k: f for k, f in model.fixed.items() if f} if fixed_nondefaults: node["fixed"] = fixed_nondefaults bounds_nondefaults = {k: b for k, b in model.bounds.items() if any(b)} if bounds_nondefaults: node["bounds"] = bounds_nondefaults if not isinstance(model, CompoundModel): if model.input_units_equivalencies: node["input_units_equivalencies"] = model.input_units_equivalencies return node @classmethod def to_tree_transform(cls, model, ctx): raise NotImplementedError("Must be implemented in TransformType subclasses") @classmethod def to_tree(cls, model, ctx): node = cls.to_tree_transform(model, ctx) return cls._to_tree_base_transform_members(model, node, ctx) @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. assert a.name == b.name # TODO: Assert inverses are the same # assert the bounding_boxes are the same assert a.get_bounding_box() == b.get_bounding_box() assert a.inputs == b.inputs assert a.outputs == b.outputs assert a.input_units_equivalencies == b.input_units_equivalencies class IdentityType(TransformType): name = "transform/identity" types = ["astropy.modeling.mappings.Identity"] @classmethod def from_tree_transform(cls, node, ctx): return mappings.Identity(node.get("n_dims", 1)) @classmethod def to_tree_transform(cls, data, ctx): node = {} if data.n_inputs != 1: node["n_dims"] = data.n_inputs return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert ( isinstance(a, mappings.Identity) and isinstance(b, mappings.Identity) and a.n_inputs == b.n_inputs ) class ConstantType(TransformType): name = "transform/constant" version = "1.4.0" supported_versions = ["1.0.0", "1.1.0", "1.2.0", "1.3.0", "1.4.0"] types = [ "astropy.modeling.functional_models.Const1D", "astropy.modeling.functional_models.Const2D", ] @classmethod def from_tree_transform(cls, node, ctx): if cls.version < AsdfVersion("1.4.0"): # The 'dimensions' property was added in 1.4.0, # previously all values were 1D. return functional_models.Const1D(node["value"]) elif node["dimensions"] == 1: return functional_models.Const1D(node["value"]) elif node["dimensions"] == 2: return functional_models.Const2D(node["value"]) else: raise TypeError("Only 1D and 2D constant models are supported.") @classmethod def to_tree_transform(cls, data, ctx): if cls.version < AsdfVersion("1.4.0"): if not isinstance(data, functional_models.Const1D): raise ValueError( f"constant-{cls.version} does not support models with > 1 dimension" ) return {"value": _parameter_to_value(data.amplitude)} else: if isinstance(data, functional_models.Const1D): dimension = 1 elif isinstance(data, functional_models.Const2D): dimension = 2 return { "value": _parameter_to_value(data.amplitude), "dimensions": dimension, } class GenericModel(mappings.Mapping): def __init__(self, n_inputs, n_outputs): mapping = tuple(range(n_inputs)) super().__init__(mapping) self._n_outputs = n_outputs self._outputs = tuple("x" + str(idx) for idx in range(n_outputs)) @property def inverse(self): raise NotImplementedError() class GenericType(TransformType): name = "transform/generic" types = [GenericModel] @classmethod def from_tree_transform(cls, node, ctx): return GenericModel(node["n_inputs"], node["n_outputs"]) @classmethod def to_tree_transform(cls, data, ctx): return { "n_inputs": data.n_inputs, "n_outputs": data.n_outputs, } class UnitsMappingType(AstropyType): name = "transform/units_mapping" version = "1.0.0" types = [mappings.UnitsMapping] @classmethod def to_tree(cls, node, ctx): tree = {} if node.name is not None: tree["name"] = node.name inputs = [] outputs = [] for i, o, m in zip(node.inputs, node.outputs, node.mapping): input = { "name": i, "allow_dimensionless": node.input_units_allow_dimensionless[i], } if m[0] is not None: input["unit"] = m[0] if ( node.input_units_equivalencies is not None and i in node.input_units_equivalencies ): input["equivalencies"] = node.input_units_equivalencies[i] inputs.append(input) output = { "name": o, } if m[-1] is not None: output["unit"] = m[-1] outputs.append(output) tree["unit_inputs"] = inputs tree["unit_outputs"] = outputs return tree @classmethod def from_tree(cls, tree, ctx): mapping = tuple( (i.get("unit"), o.get("unit")) for i, o in zip(tree["unit_inputs"], tree["unit_outputs"]) ) equivalencies = None for i in tree["unit_inputs"]: if "equivalencies" in i: if equivalencies is None: equivalencies = {} equivalencies[i["name"]] = i["equivalencies"] kwargs = { "input_units_equivalencies": equivalencies, "input_units_allow_dimensionless": { i["name"]: i.get("allow_dimensionless", False) for i in tree["unit_inputs"] }, } if "name" in tree: kwargs["name"] = tree["name"] return mappings.UnitsMapping(mapping, **kwargs)
b7efe823d7389e11a9eea90c0d0f69e9d0cf3dd3330dd4d61ba07677adae98d0
from astropy.io.misc.asdf.tags.transform.basic import TransformType from astropy.modeling.models import Spline1D __all__ = ["SplineType"] class SplineType(TransformType): name = "transform/spline1d" version = "1.0.0" types = ["astropy.modeling.spline.Spline1D"] @classmethod def from_tree_transform(cls, node, ctx): return Spline1D( knots=node["knots"], coeffs=node["coefficients"], degree=node["degree"] ) @classmethod def to_tree_transform(cls, model, ctx): return {"knots": model.t, "coefficients": model.c, "degree": model.degree}
2c6b0452457d7e868f79da38cce3ac2f1130d21d4d426e0295c04b6234f1ca7a
# Licensed under a 3-clause BSD style license - see LICENSE.rst from astropy.io.misc.asdf.tags.transform.basic import TransformType from astropy.modeling import math_functions from astropy.modeling.math_functions import * # noqa: F401 from astropy.modeling.math_functions import __all__ as math_classes __all__ = ["NpUfuncType"] class NpUfuncType(TransformType): name = "transform/math_functions" version = "1.0.0" types = ["astropy.modeling.math_functions." + kl for kl in math_classes] @classmethod def from_tree_transform(cls, node, ctx): klass_name = math_functions._make_class_name(node["func_name"]) klass = getattr(math_functions, klass_name) return klass() @classmethod def to_tree_transform(cls, model, ctx): return {"func_name": model.func.__name__}
a229a419abd2864e3ae0d90880d932eca844c113830c665c40f489da301f90d2
# Licensed under a 3-clause BSD style license - see LICENSE.rst from numpy.testing import assert_array_equal from astropy.io.misc.asdf.tags.transform.basic import TransformType from astropy.modeling import functional_models from . import _parameter_to_value __all__ = [ "AiryDisk2DType", "Box1DType", "Box2DType", "Disk2DType", "Ellipse2DType", "Exponential1DType", "Gaussian1DType", "Gaussian2DType", "KingProjectedAnalytic1DType", "Logarithmic1DType", "Lorentz1DType", "Moffat1DType", "Moffat2DType", "Planar2D", "RedshiftScaleFactorType", "RickerWavelet1DType", "RickerWavelet2DType", "Ring2DType", "Sersic1DType", "Sersic2DType", "Sine1DType", "Cosine1DType", "Tangent1DType", "ArcSine1DType", "ArcCosine1DType", "ArcTangent1DType", "Trapezoid1DType", "TrapezoidDisk2DType", "Voigt1DType", ] class AiryDisk2DType(TransformType): name = "transform/airy_disk2d" version = "1.0.0" types = ["astropy.modeling.functional_models.AiryDisk2D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.AiryDisk2D( amplitude=node["amplitude"], x_0=node["x_0"], y_0=node["y_0"], radius=node["radius"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "y_0": _parameter_to_value(model.y_0), "radius": _parameter_to_value(model.radius), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.AiryDisk2D) and isinstance( b, functional_models.AiryDisk2D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.y_0, b.y_0) assert_array_equal(a.radius, b.radius) class Box1DType(TransformType): name = "transform/box1d" version = "1.0.0" types = ["astropy.modeling.functional_models.Box1D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Box1D( amplitude=node["amplitude"], x_0=node["x_0"], width=node["width"] ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "width": _parameter_to_value(model.width), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Box1D) and isinstance( b, functional_models.Box1D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.width, b.width) class Box2DType(TransformType): name = "transform/box2d" version = "1.0.0" types = ["astropy.modeling.functional_models.Box2D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Box2D( amplitude=node["amplitude"], x_0=node["x_0"], x_width=node["x_width"], y_0=node["y_0"], y_width=node["y_width"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "x_width": _parameter_to_value(model.x_width), "y_0": _parameter_to_value(model.y_0), "y_width": _parameter_to_value(model.y_width), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Box2D) and isinstance( b, functional_models.Box2D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.x_width, b.x_width) assert_array_equal(a.y_0, b.y_0) assert_array_equal(a.y_width, b.y_width) class Disk2DType(TransformType): name = "transform/disk2d" version = "1.0.0" types = ["astropy.modeling.functional_models.Disk2D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Disk2D( amplitude=node["amplitude"], x_0=node["x_0"], y_0=node["y_0"], R_0=node["R_0"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "y_0": _parameter_to_value(model.y_0), "R_0": _parameter_to_value(model.R_0), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Disk2D) and isinstance( b, functional_models.Disk2D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.y_0, b.y_0) assert_array_equal(a.R_0, b.R_0) class Ellipse2DType(TransformType): name = "transform/ellipse2d" version = "1.0.0" types = ["astropy.modeling.functional_models.Ellipse2D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Ellipse2D( amplitude=node["amplitude"], x_0=node["x_0"], y_0=node["y_0"], a=node["a"], b=node["b"], theta=node["theta"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "y_0": _parameter_to_value(model.y_0), "a": _parameter_to_value(model.a), "b": _parameter_to_value(model.b), "theta": _parameter_to_value(model.theta), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Ellipse2D) and isinstance( b, functional_models.Ellipse2D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.y_0, b.y_0) assert_array_equal(a.a, b.a) assert_array_equal(a.b, b.b) assert_array_equal(a.theta, b.theta) class Exponential1DType(TransformType): name = "transform/exponential1d" version = "1.0.0" types = ["astropy.modeling.functional_models.Exponential1D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Exponential1D( amplitude=node["amplitude"], tau=node["tau"] ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "tau": _parameter_to_value(model.tau), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Exponential1D) and isinstance( b, functional_models.Exponential1D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.tau, b.tau) class Gaussian1DType(TransformType): name = "transform/gaussian1d" version = "1.0.0" types = ["astropy.modeling.functional_models.Gaussian1D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Gaussian1D( amplitude=node["amplitude"], mean=node["mean"], stddev=node["stddev"] ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "mean": _parameter_to_value(model.mean), "stddev": _parameter_to_value(model.stddev), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Gaussian1D) and isinstance( b, functional_models.Gaussian1D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.mean, b.mean) assert_array_equal(a.stddev, b.stddev) class Gaussian2DType(TransformType): name = "transform/gaussian2d" version = "1.0.0" types = ["astropy.modeling.functional_models.Gaussian2D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Gaussian2D( amplitude=node["amplitude"], x_mean=node["x_mean"], y_mean=node["y_mean"], x_stddev=node["x_stddev"], y_stddev=node["y_stddev"], theta=node["theta"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_mean": _parameter_to_value(model.x_mean), "y_mean": _parameter_to_value(model.y_mean), "x_stddev": _parameter_to_value(model.x_stddev), "y_stddev": _parameter_to_value(model.y_stddev), "theta": _parameter_to_value(model.theta), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Gaussian2D) and isinstance( b, functional_models.Gaussian2D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_mean, b.x_mean) assert_array_equal(a.y_mean, b.y_mean) assert_array_equal(a.x_stddev, b.x_stddev) assert_array_equal(a.y_stddev, b.y_stddev) assert_array_equal(a.theta, b.theta) class KingProjectedAnalytic1DType(TransformType): name = "transform/king_projected_analytic1d" version = "1.0.0" types = ["astropy.modeling.functional_models.KingProjectedAnalytic1D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.KingProjectedAnalytic1D( amplitude=node["amplitude"], r_core=node["r_core"], r_tide=node["r_tide"] ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "r_core": _parameter_to_value(model.r_core), "r_tide": _parameter_to_value(model.r_tide), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.KingProjectedAnalytic1D) and isinstance( b, functional_models.KingProjectedAnalytic1D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.r_core, b.r_core) assert_array_equal(a.r_tide, b.r_tide) class Logarithmic1DType(TransformType): name = "transform/logarithmic1d" version = "1.0.0" types = ["astropy.modeling.functional_models.Logarithmic1D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Logarithmic1D( amplitude=node["amplitude"], tau=node["tau"] ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "tau": _parameter_to_value(model.tau), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Logarithmic1D) and isinstance( b, functional_models.Logarithmic1D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.tau, b.tau) class Lorentz1DType(TransformType): name = "transform/lorentz1d" version = "1.0.0" types = ["astropy.modeling.functional_models.Lorentz1D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Lorentz1D( amplitude=node["amplitude"], x_0=node["x_0"], fwhm=node["fwhm"] ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "fwhm": _parameter_to_value(model.fwhm), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Lorentz1D) and isinstance( b, functional_models.Lorentz1D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.fwhm, b.fwhm) class Moffat1DType(TransformType): name = "transform/moffat1d" version = "1.0.0" types = ["astropy.modeling.functional_models.Moffat1D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Moffat1D( amplitude=node["amplitude"], x_0=node["x_0"], gamma=node["gamma"], alpha=node["alpha"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "gamma": _parameter_to_value(model.gamma), "alpha": _parameter_to_value(model.alpha), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Moffat1D) and isinstance( b, functional_models.Moffat1D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.gamma, b.gamma) assert_array_equal(a.alpha, b.alpha) class Moffat2DType(TransformType): name = "transform/moffat2d" version = "1.0.0" types = ["astropy.modeling.functional_models.Moffat2D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Moffat2D( amplitude=node["amplitude"], x_0=node["x_0"], y_0=node["y_0"], gamma=node["gamma"], alpha=node["alpha"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "y_0": _parameter_to_value(model.y_0), "gamma": _parameter_to_value(model.gamma), "alpha": _parameter_to_value(model.alpha), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Moffat2D) and isinstance( b, functional_models.Moffat2D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.y_0, b.y_0) assert_array_equal(a.gamma, b.gamma) assert_array_equal(a.alpha, b.alpha) class Planar2D(TransformType): name = "transform/planar2d" version = "1.0.0" types = ["astropy.modeling.functional_models.Planar2D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Planar2D( slope_x=node["slope_x"], slope_y=node["slope_y"], intercept=node["intercept"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "slope_x": _parameter_to_value(model.slope_x), "slope_y": _parameter_to_value(model.slope_y), "intercept": _parameter_to_value(model.intercept), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Planar2D) and isinstance( b, functional_models.Planar2D ) assert_array_equal(a.slope_x, b.slope_x) assert_array_equal(a.slope_y, b.slope_y) assert_array_equal(a.intercept, b.intercept) class RedshiftScaleFactorType(TransformType): name = "transform/redshift_scale_factor" version = "1.0.0" types = ["astropy.modeling.functional_models.RedshiftScaleFactor"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.RedshiftScaleFactor(z=node["z"]) @classmethod def to_tree_transform(cls, model, ctx): node = {"z": _parameter_to_value(model.z)} return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.RedshiftScaleFactor) and isinstance( b, functional_models.RedshiftScaleFactor ) assert_array_equal(a.z, b.z) class RickerWavelet1DType(TransformType): name = "transform/ricker_wavelet1d" version = "1.0.0" types = ["astropy.modeling.functional_models.RickerWavelet1D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.RickerWavelet1D( amplitude=node["amplitude"], x_0=node["x_0"], sigma=node["sigma"] ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "sigma": _parameter_to_value(model.sigma), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.RickerWavelet1D) and isinstance( b, functional_models.RickerWavelet1D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.sigma, b.sigma) class RickerWavelet2DType(TransformType): name = "transform/ricker_wavelet2d" version = "1.0.0" types = ["astropy.modeling.functional_models.RickerWavelet2D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.RickerWavelet2D( amplitude=node["amplitude"], x_0=node["x_0"], y_0=node["y_0"], sigma=node["sigma"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "y_0": _parameter_to_value(model.y_0), "sigma": _parameter_to_value(model.sigma), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.RickerWavelet2D) and isinstance( b, functional_models.RickerWavelet2D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.y_0, b.y_0) assert_array_equal(a.sigma, b.sigma) class Ring2DType(TransformType): name = "transform/ring2d" version = "1.0.0" types = ["astropy.modeling.functional_models.Ring2D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Ring2D( amplitude=node["amplitude"], x_0=node["x_0"], y_0=node["y_0"], r_in=node["r_in"], width=node["width"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "y_0": _parameter_to_value(model.y_0), "r_in": _parameter_to_value(model.r_in), "width": _parameter_to_value(model.width), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Ring2D) and isinstance( b, functional_models.Ring2D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.y_0, b.y_0) assert_array_equal(a.r_in, b.r_in) assert_array_equal(a.width, b.width) class Sersic1DType(TransformType): name = "transform/sersic1d" version = "1.0.0" types = ["astropy.modeling.functional_models.Sersic1D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Sersic1D( amplitude=node["amplitude"], r_eff=node["r_eff"], n=node["n"] ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "r_eff": _parameter_to_value(model.r_eff), "n": _parameter_to_value(model.n), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Sersic1D) and isinstance( b, functional_models.Sersic1D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.r_eff, b.r_eff) assert_array_equal(a.n, b.n) class Sersic2DType(TransformType): name = "transform/sersic2d" version = "1.0.0" types = ["astropy.modeling.functional_models.Sersic2D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Sersic2D( amplitude=node["amplitude"], r_eff=node["r_eff"], n=node["n"], x_0=node["x_0"], y_0=node["y_0"], ellip=node["ellip"], theta=node["theta"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "r_eff": _parameter_to_value(model.r_eff), "n": _parameter_to_value(model.n), "x_0": _parameter_to_value(model.x_0), "y_0": _parameter_to_value(model.y_0), "ellip": _parameter_to_value(model.ellip), "theta": _parameter_to_value(model.theta), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Sersic2D) and isinstance( b, functional_models.Sersic2D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.r_eff, b.r_eff) assert_array_equal(a.n, b.n) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.y_0, b.y_0) assert_array_equal(a.ellip, b.ellip) assert_array_equal(a.theta, b.theta) class Trigonometric1DType(TransformType): _model = None @classmethod def from_tree_transform(cls, node, ctx): return cls._model( amplitude=node["amplitude"], frequency=node["frequency"], phase=node["phase"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "frequency": _parameter_to_value(model.frequency), "phase": _parameter_to_value(model.phase), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, cls._model) and isinstance(b, cls._model) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.frequency, b.frequency) assert_array_equal(a.phase, b.phase) class Sine1DType(Trigonometric1DType): name = "transform/sine1d" version = "1.0.0" types = ["astropy.modeling.functional_models.Sine1D"] _model = functional_models.Sine1D class Cosine1DType(Trigonometric1DType): name = "transform/cosine1d" version = "1.0.0" types = ["astropy.modeling.functional_models.Cosine1D"] _model = functional_models.Cosine1D class Tangent1DType(Trigonometric1DType): name = "transform/tangent1d" version = "1.0.0" types = ["astropy.modeling.functional_models.Tangent1D"] _model = functional_models.Tangent1D class ArcSine1DType(Trigonometric1DType): name = "transform/arcsine1d" version = "1.0.0" types = ["astropy.modeling.functional_models.ArcSine1D"] _model = functional_models.ArcSine1D class ArcCosine1DType(Trigonometric1DType): name = "transform/arccosine1d" version = "1.0.0" types = ["astropy.modeling.functional_models.ArcCosine1D"] _model = functional_models.ArcCosine1D class ArcTangent1DType(Trigonometric1DType): name = "transform/arctangent1d" version = "1.0.0" types = ["astropy.modeling.functional_models.ArcTangent1D"] _model = functional_models.ArcTangent1D class Trapezoid1DType(TransformType): name = "transform/trapezoid1d" version = "1.0.0" types = ["astropy.modeling.functional_models.Trapezoid1D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Trapezoid1D( amplitude=node["amplitude"], x_0=node["x_0"], width=node["width"], slope=node["slope"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "width": _parameter_to_value(model.width), "slope": _parameter_to_value(model.slope), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Trapezoid1D) and isinstance( b, functional_models.Trapezoid1D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.width, b.width) assert_array_equal(a.slope, b.slope) class TrapezoidDisk2DType(TransformType): name = "transform/trapezoid_disk2d" version = "1.0.0" types = ["astropy.modeling.functional_models.TrapezoidDisk2D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.TrapezoidDisk2D( amplitude=node["amplitude"], x_0=node["x_0"], y_0=node["y_0"], R_0=node["R_0"], slope=node["slope"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "y_0": _parameter_to_value(model.y_0), "R_0": _parameter_to_value(model.R_0), "slope": _parameter_to_value(model.slope), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.TrapezoidDisk2D) and isinstance( b, functional_models.TrapezoidDisk2D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.y_0, b.y_0) assert_array_equal(a.R_0, b.R_0) assert_array_equal(a.slope, b.slope) class Voigt1DType(TransformType): name = "transform/voigt1d" version = "1.0.0" types = ["astropy.modeling.functional_models.Voigt1D"] @classmethod def from_tree_transform(cls, node, ctx): return functional_models.Voigt1D( x_0=node["x_0"], amplitude_L=node["amplitude_L"], fwhm_L=node["fwhm_L"], fwhm_G=node["fwhm_G"], ) @classmethod def to_tree_transform(cls, model, ctx): node = { "x_0": _parameter_to_value(model.x_0), "amplitude_L": _parameter_to_value(model.amplitude_L), "fwhm_L": _parameter_to_value(model.fwhm_L), "fwhm_G": _parameter_to_value(model.fwhm_G), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, functional_models.Voigt1D) and isinstance( b, functional_models.Voigt1D ) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.amplitude_L, b.amplitude_L) assert_array_equal(a.fwhm_L, b.fwhm_L) assert_array_equal(a.fwhm_G, b.fwhm_G)
393a3378d7b2987d5abb3b8d8ee8eaf7d0c3f93b31a30bdba66d585bba451356
# Licensed under a 3-clause BSD style license - see LICENSE.rst from numpy.testing import assert_array_equal from astropy import modeling from astropy.io.misc.asdf.tags.transform.basic import TransformType from . import _parameter_to_value __all__ = ["AffineType", "Rotate2DType", "Rotate3DType", "RotationSequenceType"] class AffineType(TransformType): name = "transform/affine" version = "1.3.0" types = ["astropy.modeling.projections.AffineTransformation2D"] @classmethod def from_tree_transform(cls, node, ctx): matrix = node["matrix"] translation = node["translation"] if matrix.shape != (2, 2): raise NotImplementedError( "asdf currently only supports 2x2 (2D) rotation transformation matrices" ) if translation.shape != (2,): raise NotImplementedError( "asdf currently only supports 2D translation transformations." ) return modeling.projections.AffineTransformation2D( matrix=matrix, translation=translation ) @classmethod def to_tree_transform(cls, model, ctx): return { "matrix": _parameter_to_value(model.matrix), "translation": _parameter_to_value(model.translation), } @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert a.__class__ == b.__class__ assert_array_equal(a.matrix, b.matrix) assert_array_equal(a.translation, b.translation) class Rotate2DType(TransformType): name = "transform/rotate2d" version = "1.3.0" types = ["astropy.modeling.rotations.Rotation2D"] @classmethod def from_tree_transform(cls, node, ctx): return modeling.rotations.Rotation2D(node["angle"]) @classmethod def to_tree_transform(cls, model, ctx): return {"angle": _parameter_to_value(model.angle)} @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, modeling.rotations.Rotation2D) and isinstance( b, modeling.rotations.Rotation2D ) assert_array_equal(a.angle, b.angle) class Rotate3DType(TransformType): name = "transform/rotate3d" version = "1.3.0" types = [ "astropy.modeling.rotations.RotateNative2Celestial", "astropy.modeling.rotations.RotateCelestial2Native", "astropy.modeling.rotations.EulerAngleRotation", ] @classmethod def from_tree_transform(cls, node, ctx): if node["direction"] == "native2celestial": return modeling.rotations.RotateNative2Celestial( node["phi"], node["theta"], node["psi"] ) elif node["direction"] == "celestial2native": return modeling.rotations.RotateCelestial2Native( node["phi"], node["theta"], node["psi"] ) else: return modeling.rotations.EulerAngleRotation( node["phi"], node["theta"], node["psi"], axes_order=node["direction"] ) @classmethod def to_tree_transform(cls, model, ctx): if isinstance(model, modeling.rotations.RotateNative2Celestial): try: node = { "phi": _parameter_to_value(model.lon), "theta": _parameter_to_value(model.lat), "psi": _parameter_to_value(model.lon_pole), "direction": "native2celestial", } except AttributeError: node = { "phi": model.lon, "theta": model.lat, "psi": model.lon_pole, "direction": "native2celestial", } elif isinstance(model, modeling.rotations.RotateCelestial2Native): try: node = { "phi": _parameter_to_value(model.lon), "theta": _parameter_to_value(model.lat), "psi": _parameter_to_value(model.lon_pole), "direction": "celestial2native", } except AttributeError: node = { "phi": model.lon, "theta": model.lat, "psi": model.lon_pole, "direction": "celestial2native", } else: node = { "phi": _parameter_to_value(model.phi), "theta": _parameter_to_value(model.theta), "psi": _parameter_to_value(model.psi), "direction": model.axes_order, } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert a.__class__ == b.__class__ if a.__class__.__name__ == "EulerAngleRotation": assert_array_equal(a.phi, b.phi) assert_array_equal(a.psi, b.psi) assert_array_equal(a.theta, b.theta) else: assert_array_equal(a.lon, b.lon) assert_array_equal(a.lat, b.lat) assert_array_equal(a.lon_pole, b.lon_pole) class RotationSequenceType(TransformType): name = "transform/rotate_sequence_3d" types = [ "astropy.modeling.rotations.RotationSequence3D", "astropy.modeling.rotations.SphericalRotationSequence", ] version = "1.0.0" @classmethod def from_tree_transform(cls, node, ctx): angles = node["angles"] axes_order = node["axes_order"] rotation_type = node["rotation_type"] if rotation_type == "cartesian": return modeling.rotations.RotationSequence3D(angles, axes_order=axes_order) elif rotation_type == "spherical": return modeling.rotations.SphericalRotationSequence( angles, axes_order=axes_order ) else: raise ValueError(f"Unrecognized rotation_type: {rotation_type}") @classmethod def to_tree_transform(cls, model, ctx): node = {"angles": list(model.angles.value)} node["axes_order"] = model.axes_order if isinstance(model, modeling.rotations.SphericalRotationSequence): node["rotation_type"] = "spherical" elif isinstance(model, modeling.rotations.RotationSequence3D): node["rotation_type"] = "cartesian" else: raise ValueError(f"Cannot serialize model of type {type(model)}") return node @classmethod def assert_equal(cls, a, b): TransformType.assert_equal(a, b) assert a.__class__.__name__ == b.__class__.__name__ assert_array_equal(a.angles, b.angles) assert a.axes_order == b.axes_order class GenericProjectionType(TransformType): @classmethod def from_tree_transform(cls, node, ctx): args = [] for param_name, default in cls.params: args.append(node.get(param_name, default)) if node["direction"] == "pix2sky": return cls.types[0](*args) else: return cls.types[1](*args) @classmethod def to_tree_transform(cls, model, ctx): node = {} if isinstance(model, cls.types[0]): node["direction"] = "pix2sky" else: node["direction"] = "sky2pix" for param_name, default in cls.params: val = getattr(model, param_name).value if val != default: node[param_name] = val return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert a.__class__ == b.__class__ _generic_projections = { "zenithal_perspective": ( "ZenithalPerspective", (("mu", 0.0), ("gamma", 0.0)), "1.3.0", ), "gnomonic": ("Gnomonic", (), None), "stereographic": ("Stereographic", (), None), "slant_orthographic": ("SlantOrthographic", (("xi", 0.0), ("eta", 0.0)), None), "zenithal_equidistant": ("ZenithalEquidistant", (), None), "zenithal_equal_area": ("ZenithalEqualArea", (), None), "airy": ("Airy", (("theta_b", 90.0),), "1.2.0"), "cylindrical_perspective": ( "CylindricalPerspective", (("mu", 0.0), ("lam", 0.0)), "1.3.0", ), "cylindrical_equal_area": ("CylindricalEqualArea", (("lam", 0.0),), "1.3.0"), "plate_carree": ("PlateCarree", (), None), "mercator": ("Mercator", (), None), "sanson_flamsteed": ("SansonFlamsteed", (), None), "parabolic": ("Parabolic", (), None), "molleweide": ("Molleweide", (), None), "hammer_aitoff": ("HammerAitoff", (), None), "conic_perspective": ( "ConicPerspective", (("sigma", 0.0), ("delta", 0.0)), "1.3.0", ), "conic_equal_area": ("ConicEqualArea", (("sigma", 0.0), ("delta", 0.0)), "1.3.0"), "conic_equidistant": ( "ConicEquidistant", (("sigma", 0.0), ("delta", 0.0)), "1.3.0", ), "conic_orthomorphic": ( "ConicOrthomorphic", (("sigma", 0.0), ("delta", 0.0)), "1.3.0", ), "bonne_equal_area": ("BonneEqualArea", (("theta1", 0.0),), "1.3.0"), "polyconic": ("Polyconic", (), None), "tangential_spherical_cube": ("TangentialSphericalCube", (), None), "cobe_quad_spherical_cube": ("COBEQuadSphericalCube", (), None), "quad_spherical_cube": ("QuadSphericalCube", (), None), "healpix": ("HEALPix", (("H", 4.0), ("X", 3.0)), None), "healpix_polar": ("HEALPixPolar", (), None), } def make_projection_types(): for tag_name, (name, params, version) in _generic_projections.items(): class_name = f"{name}Type" types = [ f"astropy.modeling.projections.Pix2Sky_{name}", f"astropy.modeling.projections.Sky2Pix_{name}", ] members = {"name": f"transform/{tag_name}", "types": types, "params": params} if version: members["version"] = version globals()[class_name] = type(str(class_name), (GenericProjectionType,), members) __all__.append(class_name) make_projection_types()
709b69fc3fbcb66d2f085d39d5a48c949d658992ae5ad8f2bd002222e6654626
# Licensed under a 3-clause BSD style license - see LICENSE.rst import numpy as np from asdf.versioning import AsdfVersion from numpy.testing import assert_array_equal import astropy.units as u from astropy import modeling from astropy.io.misc.asdf.tags.transform.basic import TransformType from . import _parameter_to_value __all__ = ["ShiftType", "ScaleType", "Linear1DType"] class ShiftType(TransformType): name = "transform/shift" version = "1.2.0" types = ["astropy.modeling.models.Shift"] @classmethod def from_tree_transform(cls, node, ctx): offset = node["offset"] if not isinstance(offset, u.Quantity) and not np.isscalar(offset): raise NotImplementedError( "Asdf currently only supports scalar inputs to Shift transform." ) return modeling.models.Shift(offset) @classmethod def to_tree_transform(cls, model, ctx): offset = model.offset return {"offset": _parameter_to_value(offset)} @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, modeling.models.Shift) and isinstance( b, modeling.models.Shift ) assert_array_equal(a.offset.value, b.offset.value) class ScaleType(TransformType): name = "transform/scale" version = "1.2.0" types = ["astropy.modeling.models.Scale"] @classmethod def from_tree_transform(cls, node, ctx): factor = node["factor"] if not isinstance(factor, u.Quantity) and not np.isscalar(factor): raise NotImplementedError( "Asdf currently only supports scalar inputs to Scale transform." ) return modeling.models.Scale(factor) @classmethod def to_tree_transform(cls, model, ctx): factor = model.factor return {"factor": _parameter_to_value(factor)} @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, modeling.models.Scale) and isinstance( b, modeling.models.Scale ) assert_array_equal(a.factor, b.factor) class MultiplyType(TransformType): name = "transform/multiplyscale" version = "1.0.0" types = ["astropy.modeling.models.Multiply"] @classmethod def from_tree_transform(cls, node, ctx): factor = node["factor"] return modeling.models.Multiply(factor) @classmethod def to_tree_transform(cls, model, ctx): factor = model.factor return {"factor": _parameter_to_value(factor)} @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, modeling.models.Multiply) and isinstance( b, modeling.models.Multiply ) assert_array_equal(a.factor, b.factor) class PolynomialTypeBase(TransformType): DOMAIN_WINDOW_MIN_VERSION = AsdfVersion("1.2.0") name = "transform/polynomial" types = [ "astropy.modeling.models.Polynomial1D", "astropy.modeling.models.Polynomial2D", ] @classmethod def from_tree_transform(cls, node, ctx): coefficients = np.asarray(node["coefficients"]) n_dim = coefficients.ndim if n_dim == 1: domain = node.get("domain", None) window = node.get("window", None) model = modeling.models.Polynomial1D( coefficients.size - 1, domain=domain, window=window ) model.parameters = coefficients elif n_dim == 2: x_domain, y_domain = tuple(node.get("domain", (None, None))) x_window, y_window = tuple(node.get("window", (None, None))) shape = coefficients.shape degree = shape[0] - 1 if shape[0] != shape[1]: raise TypeError("Coefficients must be an (n+1, n+1) matrix") coeffs = {} for i in range(shape[0]): for j in range(shape[0]): if i + j < degree + 1: name = "c" + str(i) + "_" + str(j) coeffs[name] = coefficients[i, j] model = modeling.models.Polynomial2D( degree, x_domain=x_domain, y_domain=y_domain, x_window=x_window, y_window=y_window, **coeffs, ) else: raise NotImplementedError( "Asdf currently only supports 1D or 2D polynomial transform." ) return model @classmethod def to_tree_transform(cls, model, ctx): if isinstance(model, modeling.models.Polynomial1D): coefficients = np.array(model.parameters) elif isinstance(model, modeling.models.Polynomial2D): degree = model.degree coefficients = np.zeros((degree + 1, degree + 1)) for i in range(degree + 1): for j in range(degree + 1): if i + j < degree + 1: name = "c" + str(i) + "_" + str(j) coefficients[i, j] = getattr(model, name).value node = {"coefficients": coefficients} typeindex = cls.types.index(model.__class__) ndim = (typeindex % 2) + 1 if cls.version >= PolynomialTypeBase.DOMAIN_WINDOW_MIN_VERSION: # Schema versions prior to 1.2 included an unrelated "domain" # property. We can't serialize the new domain values with those # versions because they don't validate. if ndim == 1: if model.domain is not None: node["domain"] = model.domain if model.window is not None: node["window"] = model.window else: if model.x_domain or model.y_domain is not None: node["domain"] = (model.x_domain, model.y_domain) if model.x_window or model.y_window is not None: node["window"] = (model.x_window, model.y_window) return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance( a, (modeling.models.Polynomial1D, modeling.models.Polynomial2D) ) and isinstance( b, (modeling.models.Polynomial1D, modeling.models.Polynomial2D) ) assert_array_equal(a.parameters, b.parameters) if cls.version > PolynomialTypeBase.DOMAIN_WINDOW_MIN_VERSION: # Schema versions prior to 1.2 are known not to serialize # domain or window. if isinstance(a, modeling.models.Polynomial1D): assert a.domain == b.domain assert a.window == b.window else: assert a.x_domain == b.x_domain assert a.x_window == b.x_window assert a.y_domain == b.y_domain assert a.y_window == b.y_window class PolynomialType1_0(PolynomialTypeBase): version = "1.0.0" class PolynomialType1_1(PolynomialTypeBase): version = "1.1.0" class PolynomialType1_2(PolynomialTypeBase): version = "1.2.0" class OrthoPolynomialType(TransformType): name = "transform/ortho_polynomial" types = [ "astropy.modeling.models.Legendre1D", "astropy.modeling.models.Legendre2D", "astropy.modeling.models.Chebyshev1D", "astropy.modeling.models.Chebyshev2D", "astropy.modeling.models.Hermite1D", "astropy.modeling.models.Hermite2D", ] typemap = { "legendre": 0, "chebyshev": 2, "hermite": 4, } invtypemap = {v: k for k, v in typemap.items()} version = "1.0.0" @classmethod def from_tree_transform(cls, node, ctx): coefficients = np.asarray(node["coefficients"]) n_dim = coefficients.ndim poly_type = node["polynomial_type"] if n_dim == 1: domain = node.get("domain", None) window = node.get("window", None) model = cls.types[cls.typemap[poly_type]]( coefficients.size - 1, domain=domain, window=window ) model.parameters = coefficients elif n_dim == 2: x_domain, y_domain = tuple(node.get("domain", (None, None))) x_window, y_window = tuple(node.get("window", (None, None))) coeffs = {} shape = coefficients.shape x_degree = shape[0] - 1 y_degree = shape[1] - 1 for i in range(x_degree + 1): for j in range(y_degree + 1): name = f"c{i}_{j}" coeffs[name] = coefficients[i, j] model = cls.types[cls.typemap[poly_type] + 1]( x_degree, y_degree, x_domain=x_domain, y_domain=y_domain, x_window=x_window, y_window=y_window, **coeffs, ) else: raise NotImplementedError( "Asdf currently only supports 1D or 2D polynomial transforms." ) return model @classmethod def to_tree_transform(cls, model, ctx): typeindex = cls.types.index(model.__class__) poly_type = cls.invtypemap[int(typeindex / 2) * 2] ndim = (typeindex % 2) + 1 if ndim == 1: coefficients = np.array(model.parameters) else: coefficients = np.zeros((model.x_degree + 1, model.y_degree + 1)) for i in range(model.x_degree + 1): for j in range(model.y_degree + 1): name = f"c{i}_{j}" coefficients[i, j] = getattr(model, name).value node = {"polynomial_type": poly_type, "coefficients": coefficients} if ndim == 1: if model.domain is not None: node["domain"] = model.domain if model.window is not None: node["window"] = model.window else: if model.x_domain or model.y_domain is not None: node["domain"] = (model.x_domain, model.y_domain) if model.x_window or model.y_window is not None: node["window"] = (model.x_window, model.y_window) return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. # There should be a more elegant way of doing this TransformType.assert_equal(a, b) assert ( ( isinstance(a, (modeling.models.Legendre1D, modeling.models.Legendre2D)) and isinstance( b, (modeling.models.Legendre1D, modeling.models.Legendre2D) ) ) or ( isinstance( a, (modeling.models.Chebyshev1D, modeling.models.Chebyshev2D) ) and isinstance( b, (modeling.models.Chebyshev1D, modeling.models.Chebyshev2D) ) ) or ( isinstance(a, (modeling.models.Hermite1D, modeling.models.Hermite2D)) and isinstance( b, (modeling.models.Hermite1D, modeling.models.Hermite2D) ) ) ) assert_array_equal(a.parameters, b.parameters) class Linear1DType(TransformType): name = "transform/linear1d" version = "1.0.0" types = ["astropy.modeling.models.Linear1D"] @classmethod def from_tree_transform(cls, node, ctx): slope = node.get("slope", None) intercept = node.get("intercept", None) return modeling.models.Linear1D(slope=slope, intercept=intercept) @classmethod def to_tree_transform(cls, model, ctx): return { "slope": _parameter_to_value(model.slope), "intercept": _parameter_to_value(model.intercept), } @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, modeling.models.Linear1D) and isinstance( b, modeling.models.Linear1D ) assert_array_equal(a.slope, b.slope) assert_array_equal(a.intercept, b.intercept)
fe74c0698513181c441f3d0b0f62d531c3164bc427be97a1ed59ae515f3c9ba6
# Licensed under a 3-clause BSD style license - see LICENSE.rst import numpy as np from numpy.testing import assert_array_equal from astropy import modeling from astropy import units as u from astropy.io.misc.asdf.tags.transform.basic import TransformType from astropy.modeling.bounding_box import ModelBoundingBox __all__ = ["TabularType"] class TabularType(TransformType): name = "transform/tabular" version = "1.2.0" types = [modeling.models.Tabular2D, modeling.models.Tabular1D] @classmethod def from_tree_transform(cls, node, ctx): lookup_table = node.pop("lookup_table") dim = lookup_table.ndim fill_value = node.pop("fill_value", None) if dim == 1: # The copy is necessary because the array is memory mapped. points = (node["points"][0][:],) model = modeling.models.Tabular1D( points=points, lookup_table=lookup_table, method=node["method"], bounds_error=node["bounds_error"], fill_value=fill_value, ) elif dim == 2: points = tuple(p[:] for p in node["points"]) model = modeling.models.Tabular2D( points=points, lookup_table=lookup_table, method=node["method"], bounds_error=node["bounds_error"], fill_value=fill_value, ) else: tabular_class = modeling.models.tabular_model(dim) points = tuple(p[:] for p in node["points"]) model = tabular_class( points=points, lookup_table=lookup_table, method=node["method"], bounds_error=node["bounds_error"], fill_value=fill_value, ) return model @classmethod def to_tree_transform(cls, model, ctx): node = {} if model.fill_value is not None: node["fill_value"] = model.fill_value node["lookup_table"] = model.lookup_table node["points"] = [p for p in model.points] node["method"] = str(model.method) node["bounds_error"] = model.bounds_error return node @classmethod def assert_equal(cls, a, b): if isinstance(a.lookup_table, u.Quantity): assert u.allclose(a.lookup_table, b.lookup_table) assert u.allclose(a.points, b.points) a_box = a.bounding_box if isinstance(a_box, ModelBoundingBox): a_box = a_box.bounding_box() b_box = b.bounding_box if isinstance(b_box, ModelBoundingBox): b_box = b_box.bounding_box() for i in range(len(a_box)): assert u.allclose(a_box[i], b_box[i]) else: assert_array_equal(a.lookup_table, b.lookup_table) assert_array_equal(a.points, b.points) a_box = a.bounding_box if isinstance(a_box, ModelBoundingBox): a_box = a_box.bounding_box() b_box = b.bounding_box if isinstance(b_box, ModelBoundingBox): b_box = b_box.bounding_box() assert_array_equal(a_box, b_box) assert a.method == b.method if a.fill_value is None: assert b.fill_value is None elif np.isnan(a.fill_value): assert np.isnan(b.fill_value) else: assert a.fill_value == b.fill_value assert a.bounds_error == b.bounds_error
55471f4fc67b37aedb5a5c9dda51baa05585780cfa1539fbabf254a882b6b377
# Licensed under a 3-clause BSD style license - see LICENSE.rst from numpy.testing import assert_array_equal from astropy.io.misc.asdf.tags.transform.basic import TransformType from astropy.modeling import physical_models from . import _parameter_to_value __all__ = ["BlackBody", "Drude1DType", "Plummer1DType"] class BlackBody(TransformType): name = "transform/blackbody" version = "1.0.0" types = ["astropy.modeling.physical_models.BlackBody"] @classmethod def from_tree_transform(cls, node, ctx): return physical_models.BlackBody( scale=node["scale"], temperature=node["temperature"] ) @classmethod def to_tree_transform(cls, model, ctx): node = { "scale": _parameter_to_value(model.scale), "temperature": _parameter_to_value(model.temperature), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, physical_models.BlackBody) and isinstance( b, physical_models.BlackBody ) assert_array_equal(a.scale, b.scale) assert_array_equal(a.temperature, b.temperature) class Drude1DType(TransformType): name = "transform/drude1d" version = "1.0.0" types = ["astropy.modeling.physical_models.Drude1D"] @classmethod def from_tree_transform(cls, node, ctx): return physical_models.Drude1D( amplitude=node["amplitude"], x_0=node["x_0"], fwhm=node["fwhm"] ) @classmethod def to_tree_transform(cls, model, ctx): node = { "amplitude": _parameter_to_value(model.amplitude), "x_0": _parameter_to_value(model.x_0), "fwhm": _parameter_to_value(model.fwhm), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, physical_models.Drude1D) and isinstance( b, physical_models.Drude1D ) assert_array_equal(a.amplitude, b.amplitude) assert_array_equal(a.x_0, b.x_0) assert_array_equal(a.fwhm, b.fwhm) class Plummer1DType(TransformType): name = "transform/plummer1d" version = "1.0.0" types = ["astropy.modeling.physical_models.Plummer1D"] @classmethod def from_tree_transform(cls, node, ctx): return physical_models.Plummer1D(mass=node["mass"], r_plum=node["r_plum"]) @classmethod def to_tree_transform(cls, model, ctx): node = { "mass": _parameter_to_value(model.mass), "r_plum": _parameter_to_value(model.r_plum), } return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert isinstance(a, physical_models.Plummer1D) and isinstance( b, physical_models.Plummer1D ) assert_array_equal(a.mass, b.mass) assert_array_equal(a.r_plum, b.r_plum)