In [2]:
%matplotlib inline
In [3]:
# imports
try:
import seaborn as sns; sns.set(context="notebook",font_scale=2)
except:
pass
from desispec import bootcalib as desiboot
from desiutil import funcfits as dufits
from astropy.io import fits
from astropy.stats import sigma_clip
import numpy as np
from astropy.modeling import models, fitting
In [4]:
fiberflat = '/Users/xavier/DESI/Wavelengths/pix-r0-00000001.fits'
flat_hdu = fits.open(fiberflat)
header = flat_hdu[0].header
flat = flat_hdu[0].data
ny = flat.shape[0]
In [5]:
xpk, ypos, cut = desiboot.find_fiber_peaks(flat)
In [6]:
tst = True
if tst:
xpk = xpk[0:5]
In [7]:
reload(desiboot)
desiboot.qa_fiber_peaks(xpk, cut)
In [8]:
# Crude first
xset, xerr = desiboot.trace_crude_init(flat,xpk,ypos)
# Polynomial fits
xfit, fdicts = desiboot.fit_traces(xset,xerr)
In [9]:
# QA
desiboot.qa_fiber_Dx(xfit, fdicts)
In [10]:
gauss = desiboot.fiber_gauss(flat,xfit,xerr)
In [11]:
reload(desiboot)
desiboot.qa_fiber_gauss(gauss)
In [12]:
import desimodel.io
In [13]:
desi_psf = desimodel.io.load_psf('r')
In [14]:
wave4 = desi_psf.wavelength(4,np.arange(desi_psf.npix_y))
In [15]:
wave4[1692]
Out[15]:
In [16]:
wave4[2451]
Out[16]:
In [17]:
wave4[2493]
Out[17]:
In [18]:
wave4[2692]
Out[18]:
In [19]:
wave4[2969]
Out[19]:
In [22]:
np.min(wave4), np.max(wave4)
Out[22]:
In [24]:
np.abs(wave4[0]-wave4[-1])/desi_psf.npix_y
Out[24]:
In [25]:
arcfile = '/Users/xavier/DESI/Wavelengths/pix-r0-00000000.fits'
arc_hdu = fits.open(arcfile)
arc = arc_hdu[0].data
In [26]:
all_spec = desiboot.extract_sngfibers_gaussianpsf(arc,xfit,gauss,verbose=False)
In [27]:
reload(desiboot)
camera = header['CAMERA']
llist = desiboot.load_arcline_list(camera)
dlamb, wmark, gd_lines, line_guess = desiboot.load_gdarc_lines(camera)
In [28]:
gd_lines
Out[28]:
In [29]:
ii=4
In [30]:
spec = all_spec[:,ii]
# Find Lines
pixpk = desiboot.find_arc_lines(spec)
In [31]:
pixpk
Out[31]:
In [32]:
#xdb.xplot(spec)
In [33]:
reload(desiboot)
id_dict = desiboot.id_arc_lines(pixpk,gd_lines,dlamb,wmark,line_guess=line_guess)#line_guess)
In [34]:
id_dict
Out[34]:
In [35]:
# IDs
plt.clf()
plt.figure(figsize=(18, 11))
yspec = np.log10(np.maximum(spec,1))
xpk = id_dict['first_id_pix']
xplt = np.arange(spec.size)
plt.plot(xplt,yspec,'b-')
plt.scatter(xpk,yspec[np.round(xpk).astype(int)],color='red')
# Guesses
for jj,xpixpk in enumerate(id_dict['first_id_pix']):
plt.text(xpixpk, yspec[int(np.round(xpixpk))], '{:g}'.format(id_dict['first_id_wave'][jj]),
ha='center',color='red',size='x-large')
#
plt.ylim(0.,np.max(yspec)*1.05)
plt.xlim(0,spec.size)
#plt.xlim(2650., 2750)
plt.xlabel('pixel')
plt.ylabel('log Flux')
plt.show()
plt.close()
In [36]:
desiboot.add_gdarc_lines(id_dict, pixpk, gd_lines)
In [37]:
id_dict
Out[37]:
In [38]:
# IDs
plt.clf()
plt.figure(figsize=(18, 11))
yspec = np.log10(np.maximum(spec,1))
xpk = id_dict['id_pix']
xplt = np.arange(spec.size)
plt.plot(xplt,yspec,'b-')
plt.scatter(xpk,yspec[np.round(xpk).astype(int)],color='red')
# Guesses
for jj,xpixpk in enumerate(id_dict['id_pix']):
plt.text(xpixpk, yspec[int(np.round(xpixpk))], '{:g}'.format(id_dict['id_wave'][jj]),
ha='center',color='red')
#
plt.ylim(0.,np.max(yspec)*1.05)
plt.xlim(0,spec.size)
plt.xlabel('pixel')
plt.ylabel('log Flux')
plt.show()
plt.close()
In [39]:
desiboot.id_remainder(id_dict, pixpk, llist)
In [40]:
id_dict['rms']
Out[40]:
In [41]:
# IDs
plt.clf()
plt.figure(figsize=(18, 11))
yspec = np.log10(np.maximum(spec,1))
xpk = id_dict['id_pix']
xplt = np.arange(spec.size)
plt.plot(xplt,yspec,'b-')
plt.scatter(xpk,yspec[np.round(xpk).astype(int)],color='red')
# Guesses
for jj,xpixpk in enumerate(id_dict['id_pix']):
plt.text(xpixpk, yspec[int(np.round(xpixpk))], '{:g}'.format(id_dict['id_wave'][jj]),
ha='center',color='red')
#
plt.ylim(0.,np.max(yspec)*1.05)
plt.xlim(0,spec.size)
plt.xlabel('pixel')
plt.ylabel('log Flux')
plt.show()
plt.close()
In [42]:
final_fit, mask = dufits.iter_fit(np.array(id_dict['id_wave']), np.array(id_dict['id_pix']), 'polynomial', 3, xmin=0., xmax=1.)
rms = np.sqrt(np.mean((dufits.func_val(np.array(id_dict['id_wave'])[mask==0], final_fit)-np.array(id_dict['id_pix'])[mask==0])**2))
final_fit_pix,mask2 = dufits.iter_fit(np.array(id_dict['id_pix']), np.array(id_dict['id_wave']),'legendre',4, niter=5)
In [43]:
print("RMS = {:g}".format(rms))
In [ ]: