In [49]:
% matplotlib inline
from __future__ import (division,
print_function)
import os
import sys
import copy
import fnmatch
import warnings
import collections
import numpy as np
import scipy
try:
from scipy.stats import scoreatpercentile
except:
scoreatpercentile = False
from scipy.interpolate import interp1d
import cPickle as pickle
# Astropy
from astropy.io import fits
from astropy import units as u
from astropy.stats import sigma_clip
from astropy.table import Table, Column
from astropy.utils.console import ProgressBar
from astropy.convolution import convolve, Box1DKernel
from astropy.coordinates import SkyCoord
# Matplotlib related
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.patches import Ellipse
from matplotlib.ticker import NullFormatter, MaxNLocator, FormatStrFormatter
from matplotlib.collections import PatchCollection
tickFormat = FormatStrFormatter('$\mathbf{%g}$')
# Extinction
import mwdust
from specutils import extinction
In [7]:
ls
In [39]:
for name in ['NGC5286_a_1', 'NGC6522_a_1', 'NGC6528_c_1']:
# Name of the file
spec = name + '.fits'
aux = name + '.aux.fits'
# Read in data
flux = fits.open(spec)[0].data
head = fits.open(spec)[0].header
saux = fits.open(aux)[0].data
# Read in aux data
band1 = saux[0,:]
band2 = saux[1,:]
band3 = saux[2,:]
band4 = saux[3,:]
# Reconstruct wavelength
naxis1 = head['NAXIS1']
crval1 = head['CRVAL1']
crpix1 = head['CRPIX1']
cdelt1 = head['CDELT1']
wave = np.arange(naxis1) * cdelt1 + crval1
waveNew = np.arange(3200) * 0.8 + 3500.0
# Galactic extinction
coord = SkyCoord(head['RA'], head['DEC'], frame='icrs', unit=(u.hourangle, u.deg))
gall, galb = coord.galactic.l.degree, coord.galactic.b.degree
ext = mwdust.SFD()
r_v = ext(gall, galb, 1.0)[0]
redden = 10.0 ** (0.4 * extinction.extinction(wave * u.Angstrom, r_v * 3.1, r_v=3.1, model='d03'))
flux *= redden
# Error spectra
svar = (band1 / band4)
serr = (flux / (band4 / 10.0))
# Interpolate the spectra to new wavelength array
intrp = interp1d(wave, flux, kind='slinear', bounds_error=False)
fluxNew = intrp(waveNew)
intrp = interp1d(wave, serr, kind='slinear', bounds_error=False)
serrNew = intrp(waveNew)
intrp = interp1d(wave, band1, kind='slinear', bounds_error=False)
band1New = intrp(waveNew)
intrp = interp1d(wave, band3, kind='slinear', bounds_error=False)
band3New = intrp(waveNew)
# Build a mask
mask = np.asarray([((band3 / band1) >= 0.5) & (serr <= 0.0)], dtype=int)
maskNew = np.asarray([((band3New / band1New) >= 0.49) & (serrNew <= 0.02)], dtype=int)[0]
sl = Table()
sl.add_column(Column(waveNew, name='wave'))
sl.add_column(Column(fluxNew * 1.0E15, name='flux'))
sl.add_column(Column(serrNew * 1.0E15, name='err'))
sl.add_column(Column(maskNew, name='mask'))
sl.write(name + '_sl.txt', format='ascii')
In [40]:
In [41]:
In [42]:
In [43]:
Out[43]:
In [45]:
In [48]:
plt.plot(wave, redden * flux)
plt.plot(wave, flux)
Out[48]:
In [107]:
plt.plot(wave, flux * 1.0E15)
plt.plot(waveNew, fluxNew * 1.0E15)
Out[107]:
In [105]:
plt.plot(wave, serr * 1.0E15)
plt.plot(waveNew, serrNew * 1.0E15)
Out[105]:
In [108]:
plt.plot(wave, band3 / band1)
Out[108]: