mangadap.util.fileio module

Provides a set of file I/O routines.


Copyright © 2019, SDSS-IV/MaNGA Pipeline Group

mangadap.util.fileio.channel_dictionary(hdu, ext, prefix='C')[source]

Construct a dictionary of the channels in a MAPS file.

mangadap.util.fileio.channel_units(hdu, ext, prefix='U')[source]

Construct an array with the channel units.

mangadap.util.fileio.compress_file(ifile, overwrite=False)[source]

Compress a file using gzip. The output file has the same name as the input file with ‘.gz’ appended.

Any existing file will be overwritten if overwrite is true.

An error is raised if the input file name already has ‘.gz’ appended to the end.

Create a symlink to the input file in the provided directory. If relative_symlink is True (default), the path to the file is relative to the directory with the symlink.

mangadap.util.fileio.init_record_array(shape, dtype)[source]

Utility function that initializes a record array using a provided input data type. For example:

dtype = [ ('INDX', int, (2,) ),
          ('VALUE', float) ]

Defines two columns, one named INDEX with two integers per row and the one named VALUE with a single float element per row. See numpy.recarray.

  • shape (int, tuple) – Shape of the output array.

  • dtype (list) – List of the tuples that define each element in the record array.


Zeroed record array

Return type


mangadap.util.fileio.read_template_spectrum(filename, data_ext=0, ivar_ext=None, sres_ext=None, log10=False)[source]

Read a template spectrum.

Template spectra are “raw format” files with template data and are, at minimum, expected to have the following components:


The latter has the flux data. If log10 is true, the wavelength solution above is expected to be in log wavelengths.

  • filename (str) – Name of the fits file to read.

  • data_ext (str, optional) – Name of the extension with the flux data.

  • ivar_ext (str, optional) – Name of the extension with the inverse variance data. If None, no inverse data are returned.

  • sres_ext (str, optional) – Name of the extension with the spectral resolution (:math:R=lambda/deltalambda`) measurements. If None, no spectral resolution data are returned.

  • log10 (bool, optional) – Flag the WCS wavelength coordinates as being in base-10 log wavelength, instead of linear. Default is to assume linear.


Up to four vectors with the wavelength, flux, inverse variance (if ivar_ext is provided), and spectral resolution (if sres_ext is provided) of the template spectrum.

Return type


  • ValueError – Raised if fits file is not one-dimensional.

  • KeyError – Raised if various header keywords or extension names are not available.

mangadap.util.fileio.readfits_1dspec(filename, log10=False)[source]

Read a 1D fits spectrum and return two vectors with the wavelength and flux.


filename (str) – Name of the file to read.


Two numpy.float64 arrays with the wavelength and flux read for the spectrum.

Return type



ValueError – Raised if the input fits file has more than one extension or its primary extension has more than two dimensions.


Return the string representation of the dimensions for the fits table column based on the provided record array element.

The shape is inverted because the first element is supposed to be the most rapidly varying; i.e. the shape is supposed to be written as row-major, as opposed to the native column-major order in python.


Return the string representation of a fits binary table data type based on the provided record array element.

mangadap.util.fileio.wavelength_vector(npix, header, log10=False)[source]

Return a vector with wavelength coordinates drawn from the WCS coordinates in the header. The function uses CRVAL1, CRPIX1, CDELT1.

  • npix (int) – Length of the vector in pixels.

  • header ( – Header with the WCS data

  • log10 (bool, optional) – Flag that the wavelengths are logarithmically sampled.


Wavelengths of a spectrum.

Return type


mangadap.util.fileio.writefits_1dspec(ofile, crval1, cdelt1, flux, hdr=None, overwrite=False)[source]

Write a simple one-dimensional spectrum.

  • ofile (str) – Name of the file to write.

  • crval1 (float) – (Log base 10 of the) Initial wavelength, which is included in the header with the keyword ‘CRVAL1’; ‘CRPIX1’ is always set to 1.

  • cdelt1 (float) – The change in (log base 10) wavelength per pixel, which is included in the header with the keywords ‘CDELT1’ and ‘CD1_1’; ‘CRPIX1’ is always set to 1.

  • flux (numpy.ndarray) – Vector of the flux values.

  • hdr (, optional) – Include sampling data in this header instead of beginning with an empty header.

  • overwrite (bool, optional) – Flag to overwrite any existing file of the same name.