desidatamodel API
desidatamodel
This package provides support for the DESI Data Model.
- exception desidatamodel.DataModelError[source]
Errors related to missing or malformed data model files, etc.
- exception desidatamodel.DataModelWarning[source]
Warnings related to missing or malformed data model files, etc.
desidatamodel.check
Check actual files against the data model for validity.
- class desidatamodel.check.DataModel(filename, section)[source]
Simple object to store data model data and metadata.
- Parameters:
filename (
str
orpathlib.Path
) – The full path of the data model file.section (
str
orpathlib.Path
) – The full path to the section of the data model containing the file.
- Raises:
TypeError – If filename or section have an unexpected type.
- _extract_columns(row, columns)[source]
Given column sizes, extract the data in each column.
Assumes a reStructuredText-compatible table.
- extract_metadata(error=False)[source]
Extract metadata from a data model file.
- Parameters:
error (
bool
, optional) – IfTrue
, failure to extract certain required metadata raises an exception.- Returns:
Metadata in a form similar to
Stub
metadata. The keys are theEXTNAME
header values.- Return type:
- Raises:
DataModelError – If error is set and the HDU has no
EXTNAME
keyword.
- get_regexp(root, error=False)[source]
Obtain the regular expression used to match files on disk.
Also internally updates the file type, if detected.
- Parameters:
- Returns:
The regular expression found, or
None
if not found. The regular expression is also stored internally.- Return type:
regular expression
- Raises:
DataModelError – If error is set and problems with the data model file are detected.
- desidatamodel.check._options()[source]
Parse command-line options.
- Returns:
The parsed options.
- Return type:
- desidatamodel.check.collect_files(root, files, n_prototypes=5)[source]
Scan a directory tree for files that correspond to data model files.
- Parameters:
Notes
Files are analyzed using this algorithm:
The first n_prototypes files that matches a regexp become the ‘prototype candidates’ for that data model file. The first candidate that can be opened cleanly is the ‘prototype’.
If no files match a data model file, then files of that type are ‘missing’.
If a file does not match any regular expression, it is ‘extraneous’.
If a file matches a regular expression that already has a prototype, it is ‘ignored’.
- desidatamodel.check.files_to_regexp(root, files, error=False)[source]
Convert a list of data model files into a list of regular expressions.
- Parameters:
- Raises:
DataModelError – If error is set and data model files with malformed regular expressions are detected.
- desidatamodel.check.main()[source]
Entry point for the check_model script.
- Returns:
An integer suitable for passing to
sys.exit()
.- Return type:
- desidatamodel.check.scan_model(section)[source]
Find all data model files in a top-level directory.
- desidatamodel.check.validate_prototypes(files, error=False, skip_keywords=False)[source]
Compares a set of prototype data files to their data models.
- Parameters:
Notes
Use set theory to compare the data headers to model headers. This should automatically find missing headers, extraneous headers, etc.
desidatamodel.columns
Render the standard column descriptions file.
- desidatamodel.columns.format_columns(rows)[source]
Does something.
- Parameters:
rows (iterable) – An iterable containing rows with any number of columns.
- Returns:
A tuple containing a format string, and an RST-style table separator.
- Return type:
- desidatamodel.columns.main()[source]
Entry point for command-line scripts.
- Returns:
An integer suitable for passing to
sys.exit()
.- Return type:
desidatamodel.scan
Deep scan available files to obtain a comprehensive set of metadata.
- class desidatamodel.scan.UnionStub(model, count, error=False)[source]
Container for unified metadata for both existing models and data files.
Initialize the metadata with a
DataModel
object, then add additionalStub
metadata.- Parameters:
- mark_optional()[source]
Mark the keywords and columns that do not appear in every file as optional.
- desidatamodel.scan._options()[source]
Parse command-line options.
- Returns:
The parsed options.
- Return type:
- desidatamodel.scan.collect_files(root, model)[source]
Scan a directory tree for all files that correspond to a data model files.
- desidatamodel.scan.main()[source]
Entry point for the deep_scan_metadata script.
- Returns:
An integer suitable for passing to
sys.exit()
.- Return type:
- desidatamodel.scan.union_metadata(model, stubs, error=False)[source]
Combine all HDU metadata from model and stubs.
desidatamodel.stub
Generate data model files from FITS files.
- class desidatamodel.stub.Stub(filename, description_file=None, error=False)[source]
This object contains metadata about a file and methods to print that metadata.
- Parameters:
- columns_header
The header of a table summarizing the columns of a BINTABLE HDU.
- Type:
tuple()
- contents_header
The header of a table summarizing the HDUs.
- Type:
tuple()
- keywords_header
The header of a table listing interesting FITS keywords.
- Type:
tuple()
- property basef
Base name of the file.
- columns(hdu, error=False)[source]
Describe the columns of a BINTABLE HDU.
- Parameters:
- Returns:
The rows of the table.
- Return type:
- Raises:
DataModelError – If the BINTABLE is actually a compressed image.
ValueError – If error and a
TUNIT
value does not have FITS-standard units.
- property contents
A table summarizing the HDUs.
- property filesize
Size of the file in human-readable format.
- property filetype
Type of file. Assumes FITS (for now) unless overridden in a subclass.
- format_table(table, indent=False)[source]
Convert tabular data into reStructuredText-compatible string.
This function assumes that table already has a header as the first row.
- property hdumeta
Metadata associated with each HDU.
- property hduname
Format of HDU names.
- image_format(hdr)[source]
Obtain format of an image HDU.
- Parameters:
hdr (
Header
) – The header to parse.- Returns:
A string describing the image format.
- Return type:
- Raises:
DataModelError – If
self.error
is set a BUNIT header with units that do not follow the FITS standard is detected.
- property modelname
Name to use for the data model file.
- desidatamodel.stub.extrakey(key)[source]
Return True if key is not a boring standard FITS keyword.
To make the data model more human readable, we don’t overwhelm the output with required keywords which are required by the FITS standard anyway, or cases where the number of headers might change over time.
This list isn’t exhaustive.
- Parameters:
key (
str
) – A FITS keyword.- Returns:
True
if the keyword is not boring.- Return type:
Examples
>>> extrakey('SIMPLE') False >>> extrakey('DEPNAM01') False >>> extrakey('BZERO') True
- desidatamodel.stub.file_size(filename)[source]
Determine file size and return string with human readable size format.
Adapted from stackoverflow answers for human readable size formatting.
- Parameters:
filename (
str
) – A string containing a filename.- Returns:
A human-readable file size.
- Return type:
Examples
>>> file_size('one-gb-file.dat') '1 GB'
- desidatamodel.stub.fits_column_format(format)[source]
Convert a FITS column format to a human-readable form.
- Parameters:
format (
str
) – A FITS-style format string.- Returns:
A human-readable version of the format string.
- Return type:
Examples
>>> fits_column_format('A') 'char[1]' >>> fits_column_format('J') 'int32' >>> fits_column_format('12E') 'float32[12]'
- desidatamodel.stub.main()[source]
Entry point for the generate_model script.
- Returns:
An integer suitable for passing to
sys.exit()
.- Return type:
- desidatamodel.stub.read_column_descriptions(filename)[source]
Read column descriptions csv file and return dictionary
- Parameters:
filename (str) – csv filename with columns NAME,TYPE,UNITS,DESCRIPTION,FULLDESCRIPTION
- Returns:
coldesc_dict[NAME] = dict with keys TYPE, UNITS, DESCRIPTION
desidatamodel.unit
Shared code for dealing with units in files and data models.
- desidatamodel.unit._validate_unit(unit, error=False)[source]
Check units for consistency with FITS standard, while allowing some special exceptions.
- Parameters:
- Returns:
If a special exception is detected, the name of the unit is returned. Otherwise,
None
.- Return type:
- Raises:
ValueError – If error is set and the unit can’t be parsed.
desidatamodel.update
Tools to update column units and descriptions in a pre-existing datamodel file.
- desidatamodel.update.format_rst_table(table)[source]
Format an astropy Table in left-aligned RST format
- Parameters:
table (astropy.table.Table)
- Returns:
list of strings to print/write for the RST-format table
Note: this doesn’t use astropy.io.ascii.rst because that generates right-aligned columns.
- desidatamodel.update.main()[source]
Updates a datamodel file with standard units and descriptions
- Returns:
An integer suitable for passing to
sys.exit()
.- Return type:
- desidatamodel.update.read_table_rows(lines, i)[source]
Read an RST-format table from a set of lines
Return: None or list of dict(Name, Type, Units, Description)
Looks for data table description of the form:
==== ==== ===== =========== Name Type Units Description ==== ==== ===== =========== blat int s biz bat bar foo int bing bang boom ==== ==== ===== ===========
while allowing the columns to have arbitrary widths or possibly be blank.
Returns None if table starting at line i doesn’t match that form.
- desidatamodel.update.update(lines, force=False)[source]
Update units and descriptions for data tables in datamodel lines
- Options:
force (bool): if True, update non-blank input entries too
Returns: list of str lines with updates units and descriptions
This function is separated from main primarily to facilitate testing of updating input lines into output lines without having to actually read and write files every time.