In [1]:
%matplotlib inline
In [2]:
# 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 [3]:
fiberflat = '/Users/xavier/DESI/Wavelengths/pix-z0-00000001.fits'
flat_hdu = fits.open(fiberflat)
header = flat_hdu[0].header
flat = flat_hdu[0].data
ny = flat.shape[0]
In [4]:
xpk, ypos, cut = desiboot.find_fiber_peaks(flat)
In [5]:
tst = True
if tst:
xpk = xpk[0:5]
In [6]:
reload(desiboot)
desiboot.qa_fiber_peaks(xpk, cut)
In [7]:
# Crude first
xset, xerr = desiboot.trace_crude_init(flat,xpk,ypos)
# Polynomial fits
xfit, fdicts = desiboot.fit_traces(xset,xerr)
In [8]:
# QA
desiboot.qa_fiber_Dx(xfit, fdicts)
In [9]:
gauss = desiboot.fiber_gauss(flat,xfit,xerr)
In [10]:
reload(desiboot)
desiboot.qa_fiber_gauss(gauss)
In [11]:
import desimodel.io
In [12]:
desi_psf = desimodel.io.load_psf('z')
In [13]:
wave4 = desi_psf.wavelength(4,np.arange(desi_psf.npix_y))
In [14]:
np.min(wave4), np.max(wave4)
Out[14]:
In [15]:
for pix in [1606.8,1800.4,1958.76,2030.1,2063.3,3212.157]:
print("{:f}, {:f}".format(pix,wave4[int(pix)]))
In [16]:
np.abs(wave4[0]-wave4[-1])/desi_psf.npix_y
Out[16]:
In [17]:
arcfile = '/Users/xavier/DESI/Wavelengths/pix-z0-00000000.fits'
arc_hdu = fits.open(arcfile)
arc = arc_hdu[0].data
In [18]:
all_spec = desiboot.extract_sngfibers_gaussianpsf(arc,xfit,gauss,verbose=False)
In [19]:
reload(desiboot)
camera = header['CAMERA']
llist = desiboot.load_arcline_list(camera)
dlamb, wmark, gd_lines, line_guess = desiboot.load_gdarc_lines(camera)
In [20]:
gd_lines
Out[20]:
In [21]:
ii=4
In [22]:
spec = all_spec[:,ii]
# Find Lines
pixpk = desiboot.find_arc_lines(spec)
In [23]:
pixpk
Out[23]:
In [24]:
#xdb.xplot(spec)
In [25]:
reload(desiboot)
id_dict = desiboot.id_arc_lines(pixpk,gd_lines,dlamb,wmark,line_guess=line_guess)#line_guess)
In [26]:
id_dict
Out[26]:
In [28]:
# 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 [29]:
desiboot.add_gdarc_lines(id_dict, pixpk, gd_lines)
In [30]:
id_dict
Out[30]:
In [31]:
# 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 [32]:
desiboot.id_remainder(id_dict, pixpk, llist)
In [33]:
id_dict['rms']
Out[33]:
In [34]:
# 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 [35]:
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 [36]:
print("RMS = {:g}".format(rms))