In [1]:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

from PIL import Image
import math
import requests
from io import BytesIO

import numpy as np
import astropy.units as u
from linetools.spectralline import AbsLine
from linetools.spectra import io as lsio
from linetools.spectra.xspectrum1d import XSpectrum1D
from linetools.analysis import voigt as lav
# plots
from matplotlib import pyplot as plt
import pylab
pylab.rcParams['figure.figsize'] = (8.0, 6.0)
import matplotlib
matplotlib.rc('xtick', labelsize=20) 
matplotlib.rc('ytick', labelsize=20)

In [2]:
# Define a plotting function for the Voight Profile
def plt_line(spec):
    plt.clf()
    plt.figure(dpi=700)
    plt.plot(spec.wavelength.value, spec.flux.value, 'k-', drawstyle='steps-mid', lw=1.5)
    plt.xlim(3644., 3650.)
    plt.ylabel('Normalized Flux', fontsize=20.)
    plt.xlabel('Wavelength', fontsize=20.)
    ax = plt.gca()
    ax.xaxis.set_major_locator(plt.MultipleLocator(2.))
    ax.get_xaxis().get_major_formatter().set_useOffset(False)
    plt.ylim(0., 1.1)
    plt.show()
    plt.close()

Load and normalize spectra data


In [3]:
# Load in data
flux_data = np.load('../data/flux.npy')
wave_data = np.load('../data/wave.npy')

In [4]:
# Normalize flux data
flux_data *= 1/flux_data.max()

In [5]:
plt.clf()
plt.figure(dpi=700)
plt.plot(wave_data, flux_data, 'k-')
plt.ylabel('Normalized Flux', fontsize=20.)
plt.xlabel('Wavelength', fontsize=20.)
plt.show()


<Figure size 432x288 with 0 Axes>

Try plotting Voigt Profile from the test data


In [6]:
#Generate AbsLine
abslin = AbsLine(1215.670*u.AA)


read_sets: Using set file -- 
  /usr/local/lib/python3.6/dist-packages/linetools/lists/sets/llist_v1.2.ascii
Loading abundances from Asplund2009
Abundances are relative by number on a logarithmic scale with H=12

In [7]:
# Fill Attributes
abslin.attrib['N'] = 10**14./u.cm**2  # log N
abslin.attrib['b'] = 25.*u.km/u.s
abslin.setz(2.)

In [8]:
# Load
spectra1d = XSpectrum1D.from_tuple((wave_data, flux_data))
abslin.analy['spec'] = spectra1d


/usr/local/lib/python3.6/dist-packages/linetools/spectra/xspectrum1d.py:107: UserWarning: Assuming wavelength unit is Angstroms
  warnings.warn("Assuming wavelength unit is Angstroms")

In [9]:
#Generate
vmodel = abslin.generate_voigt()


/usr/local/lib/python3.6/dist-packages/linetools/analysis/voigt.py:187: UserWarning: Using a sub-grid wavelength array because the input array is too coarse.
  warnings.warn('Using a sub-grid wavelength array because the input array is too coarse.')
/usr/local/lib/python3.6/dist-packages/linetools/analysis/voigt.py:188: UserWarning: Will return values rebinned to the input array.
  warnings.warn('Will return values rebinned to the input array.')
/usr/local/lib/python3.6/dist-packages/linetools/analysis/voigt.py:244: UserWarning: Assuming infinite spectral resolution, i.e. no smoothing.
  warnings.warn('Assuming infinite spectral resolution, i.e. no smoothing.')
/usr/local/lib/python3.6/dist-packages/linetools/analysis/voigt.py:245: UserWarning: Set fwhm to smooth.
  warnings.warn('Set fwhm to smooth.')

In [10]:
# wv = vmodel.wavelength.value
# fx = vmodel.flux.value

In [11]:
# Plot
plt_line(vmodel)


<Figure size 432x288 with 0 Axes>

Try with self_generated wave_length


In [12]:
abslin.attrib['N'] = 10**17.5/u.cm**2
abslin.attrib['b'] = 20.*u.km/u.s

In [13]:
wave = np.linspace(3644, 3650,5500)*u.AA

In [14]:
vmodel2 = abslin.generate_voigt(wave=wave_data[0:100]*u.AA)

In [15]:
wv2 = vmodel2.wavelength.value
fx2 = vmodel2.flux.value

In [16]:
plt_line(vmodel2)


<Figure size 432x288 with 0 Axes>

Examine interesting feature visualizations


In [8]:
cvrl = np.load('../data/model_1d_vis/conv1_relu/conv1_relu.npy')
pool1 = np.load('../data/model_1d_vis/pool1/pool1.npy')

In [9]:
def vis_feature(input):
    plt.clf()
    plt.figure(dpi=40)
    plt.plot(input, 'k-')
    plt.show()
    plt.close

In [12]:
# Examine shapes with potential voigt profile match
t1 = cvrl[23]
t2 = cvrl[97]
t3 = pool1[0]
t4 = pool1[48]

In [13]:
vis_feature(t1)
vis_feature(t2)
vis_feature(t3)
vis_feature(t4)


<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>

In [16]:
con = np.concatenate((t1, t2, t3, t4))

In [19]:
np.save('../data/act_v.npy', con)

In [ ]:


In [23]:
test = np.load('../data/act_v.npy')
test.shape


Out[23]:
(1600,)

In [24]:
test1 = test[0:400]
test2 = test[400:800]
test3 = test[800:1200]
test4 = test[1200:]

In [25]:
vis_feature(test1)
vis_feature(test2)
vis_feature(test3)
vis_feature(test4)


<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>

Examine .fits data


In [29]:
from astropy.visualization import astropy_mpl_style
plt.style.use(astropy_mpl_style)

from astropy.io import fits

test_file = '../data/UM184_nF.fits'
test_data = fits.getdata(test_file, ext=0)
print(test_data.shape)


(16582,)

In [30]:
plt.figure()
plt.plot(test_data)


Out[30]:
[<matplotlib.lines.Line2D at 0x7f260c90aa20>]

In [ ]: