%matplotlib inline
import matplotlib
#matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
###Hill5 model in the pyspeckit package
import pyspeckit.spectrum.models.hill5infall as hill5infall
from pyspeckit.spectrum.classes import Spectrum
from pyspeckit.spectrum.units import SpectroscopicAxis
import numpy as np
from astropy import units as u
from astropy.io import fits
####an example of my own HNC line
hd = fits.getheader('myhnc.fits')
data = fits.getdata('myhnc.fits')
velo = hd['VELO']
dv = hd['deltav']
refc = hd['CRPIX1']
xaxis = np.divide(np.add(np.multiply(np.subtract(np.arange(1,hd['NAXIS1']+1), refc), dv), velo), 1000)
yaxis = data[0,0,0,:]
x =xaxis*u.km/u.s
xarr = SpectroscopicAxis(x, refX=hd['restfreq']*u.Hz, velocity_convention='radio')
sp = Spectrum(xarr=xarr, data=yaxis)
sp.plotter()
plt.xlim(5,12)
plt.ylabel('Main beam temperature (K)')
#plt.plot([8,8],[-0.5,2],'r')
##register hill5 model for fit
sp.Registry.add_fitter('hill5_fitter', hill5infall.hill5_fitter, 5)
##fitting, 5 pars correspond to tau, v_lsr, v_in, delta_v, T_ex
sp.specfit(fittype='hill5_fitter', guesses = [8, 8.2, 0.03, 0.95/2.35, 7])
##fitting results
sp.specfit.parinfo
sp.plotter()
sp.specfit.plot_fit()
plt.xlim(2,15)
plt.ylabel('Main beam temperature (K)')