mangadap.util.fileio module

Provides a set of file I/O routines.

Revision history

27 May 2015: Original implementation by K. Westfall (KBW)
29 Jan 2016: (KBW) Added read_template_spectrum()
01 Feb 2016: (KBW) Moved wavelength calculation to a common function.
09 Feb 2016: (KBW) Added writefits_1dspec().
28 Mar 2016: (KBW) Added function init_record_array() and rec_to_fits_type()
19 May 2016: (KBW) In write_hdu(), removed verbose and added loggers and quiet.
25 Aug 2016: (KBW) Added channel_dictionary()
23 Feb 2017: (KBW) create_symlink now creates the directory if it doesn’t exist
23 Mar 2017: (KBW) Force type of header keywords in wavelength_vector().


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, clobber=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 clobber 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',, (2,) ),
          ('VALUE', numpy.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 or tuple) – Shape of the output array.
  • dtype (list of tuples) – 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.

Parameters:filename (str) – Name of the file to read.
Returns:Two numpy.float64 arrays with the wavelength and flux read for the spectrum.
Return type:numpy.ndarray
Raises: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, clobber=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 (array) – Vector of the flux values.
  • clobber (bool) – (Optional) Flag to overwrite any existing file of the same name.