# Do set up
import numpy as np
from astropy.io import fits
from matplotlib import pyplot
import matplotlib.pyplot as plt
# Open the FITS files for 1d and 2d spectra
# The files are assumed to be in same directory as the notebook
hdu1 = fits.open('spec1d.cos-m32.033.COSMOS-974310.fits')
hdu2 = fits.open('spec2d.cos-m32.033.COSMOS-974310.fits')
# read data and headers
data1d = hdu1[1].data
header1d = hdu1[1].header
data2d = hdu2[1].data
header2d = hdu2[1].header
# Set up variables to plot -- flux, wavelength, and inverse variance (1./sigma^2)
flux1d = data1d['flux'][0,:]
wave1d = data1d['lambda'][0,:]
ivar1d = data1d['ivar'][0,:]
flux2d = data2d['flux'][0,:,:]
wave2d = data2d['lambda'][0,:,:]
ivar2d = data2d['ivar'][0,:,:]
# Plot 1d spectrum
f=plt.figure(figsize=[14,10])
plt.plot(wave1d,flux1d)
plt.xlabel('Wavelength (A)')
plt.ylabel('Flux (arbitrary units)')
plt.show()
# Plot 2d spectrum
fig_2d = plt.figure(figsize=[20,1.5])
ax3 = fig_2d.add_axes([.0,.0,.99,.99])
pmap = ax3.imshow(flux2d*np.sqrt(ivar2d),origin='lower',interpolation='nearest',vmin=0.01,vmax=4,aspect="auto",cmap="Greys_r")
plt.show(fig_2d)