Read and plot spectra with SpectraContainer#

Note

This tutorial is not final. It will be finished once all the core functionalities of SpectraContainer are implemented.

Introduction to using madcubapy to read and plot MADCUBA spectra.

We start by importing the necessary libraries for this tutorial.

from madcubapy.io import SpectraContainer
import matplotlib.pyplot as plt

Reading the spectra file#

Spectra are exported from MADCUBA in a single FITS file containing a bintable with each one of the spectra inside. This FITS file is then packaged alongside the history file inside a .spec archive. With the SpectraContainer class we can open MADCUBA’s .spec files alongside their history tables.

We can read the .spec file with the SpectraContainer.read() method, and the corresponding history file will be loaded as well if present.

spectra_container = SpectraContainer.read(
    "../examples/data/IRAS16293_position_8_TM2_spectra.spec")

Note

Due to how MADCUBA saves some FITS header cards, several astropy warnings can pop up when reading spectra. Usually these warnings are incorrectly written units, or units using non-standard conventions.

The spectra are stored in the bintable attribute, which is an Astropy table contaning the data and header information for every spectra.

# Show the first two spectra inside as an example
spectra_container.bintable[0:2]
Table length=2
PROJIDOBSERVERVERSIONCRPIX1CRPIX2SCANOBS-NUMDATE-OBSTIME-OBSLSTDATEEXPOSUREOBJECTAZIMUTHELEVATIOHISTORYORIGINCTYPE3CRPIX3CRVAL3CDELT3CTYPE1CRVAL1CDELT1CTYPE2CRVAL2CDELT2EQUINOXRADESYSRESTFRQIMAGFREQSPECRESMOLECULETRANSITIONVELDEFVELREFALTRPIXALTRVALZSOURCESTOKESCHANNELSDATAWEIGTHSWAVETELESCOPBMAJBMINBPAFRONTENDBACKENDTSYSTSYSIMAGEOBSMODEQUALITYBUNITAPEREFFBEAMEFFETAFFSANTGAINGAINIMAGRESTWAVHIST-OLDTEMPSCALXAXIS
sssdegdegdegdegdegdegHzHzHzm / sm / sJym or HzdegdegdegKKK / JymHz
bytes32bytes16bytes24int32int32int32int32bytes24float64float64bytes24float32bytes32float32float32bytes32bytes24bytes8float64float64float64bytes8float64float64bytes8float64float64bytes8bytes8float64float64float64bytes16bytes16bytes8int32float64float64float64bytes2int32objectobjectobjectbytes8float32float32float32bytes16bytes16float32float32bytes32int32bytes32float32float32float32float32float32float64bytes32bytes16object
MASSArivilla1226232112018-12-16T13:42:49.72801665751050553.01000.02023-02-17T15:00:39.849Z1.0IRAS_16293-2422180.090.0history file nameMADCUBAFREQ-LSR1.093782290089.0122068.8545532RA---GLS248.094179931279680.0DEC--GLS-24.4752036114647420.02000.0ICRS93900000000.00.0122068.8545532UnknownUnknownVRAD-LSR257958.87635359156872500.08.339171921162217e-06I1918[-0.00553309 -0.00566683 -0.00572642 ... -0.00119151 -0.00168416\n 0.0022412 ][60835.836][-1.]ALMA-0.0005619182-0.00056191820.0PLOT MAD_CUB_member.uid___A001_X100.0100.0MADCUBA1Jy1.01.01.033.8388520.00.00.0TMB[9.37822901e+10 9.37824122e+10 9.37825342e+10 ... 9.40160519e+10\n 9.40161740e+10 9.40162961e+10] Hz
MASSArivilla1226232112018-12-16T13:42:49.72801665751097826.01000.02023-02-17T15:00:39.449Z1.0IRAS_16293-2422180.090.0history file nameMADCUBAFREQ-LSR1.0108880112783.0122069.8044281RA---GLS248.094179931279680.0DEC--GLS-24.4752036114647420.02000.0ICRS108998000000.00.0122069.8044281UnknownUnknownVRAD-LSR257959.28998880460142500.08.339171921162217e-06I1918[-0.00065054 -0.00024617 -0.00393423 ... 0.0009739 -0.0042386\n 0.00021092][37884.938][-1.]ALMA-0.00053032895-0.000530328950.0PLOT MAD_CUB_member.uid___A001_X100.0100.0MADCUBA1Jy1.01.01.028.1945610.00.00.0TMB[1.08880113e+11 1.08880235e+11 1.08880357e+11 ... 1.09113876e+11\n 1.09113999e+11 1.09114121e+11] Hz


The spectrum data is contained in the DATA and XAXIS columns alongside their units if correctly parsed from the header.

We can quickly plot a spectrum by accesing these values

index = 6

fig, ax = plt.subplots(1, 1, figsize=(7,5))

x = spectra_container.bintable[index]['XAXIS']
x_unit = spectra_container.bintable[index].table['XAXIS'].unit
y = spectra_container.bintable[index]['DATA']
y_unit = spectra_container.bintable[index].table['DATA'].unit

ax.plot(x, y, drawstyle='steps-mid')

ax.set_xlabel(f"Freq [{x_unit}]")
ax.set_ylabel(f"S [{y_unit}]")

plt.show()
read and plot spectra

Total running time of the script: (0 minutes 0.111 seconds)

Gallery generated by Sphinx-Gallery