Grizli spectral products and fitting tools


In [1]:
%matplotlib inline

In [2]:
import glob
import time
import os

import numpy as np
import matplotlib.pyplot as plt

import astropy.io.fits as pyfits
import drizzlepac

import grizli
from grizli import utils, multifit
print(grizli.__version__)


The following task in the stsci.skypac package can be run with TEAL:
                                    skymatch                                    
The following tasks in the drizzlepac package can be run with TEAL:
    astrodrizzle       imagefindpars           mapreg              photeq       
     pixreplace           pixtopix            pixtosky        refimagefindpars  
     resetbits          runastrodriz          skytopix           tweakback      
      tweakreg           updatenpol
1.0.dev1427

NB: The example below uses the "ERS" exposures pre-processed as described in the WFC3IR_Reduction notebook.


In [3]:
os.getcwd()
root = 'j033216m2743'
os.chdir('{0}/Extractions'.format(root)) 

grp = multifit.GroupFLT(grism_files=glob.glob('*GrismFLT.fits'), 
                        catalog='{0}-ir.cat.fits'.format(root), 
                        cpu_count=-1, sci_extn=1, pad=256)


Load ib6o23rsq.01.GrismFLT.fits!
Load ib6o21qmq.01.GrismFLT.fits!
Load ib6o23ruq.01.GrismFLT.fits!
Load ib6o21r6q.01.GrismFLT.fits!
Load ib6o21qoq.01.GrismFLT.fits!
Load ib6o23ryq.01.GrismFLT.fits!
Load ib6o21r8q.01.GrismFLT.fits!
Load ib6o23s0q.01.GrismFLT.fits!
Files loaded - 2.79 sec.

Now provide two ways of saving the extracted 2D spectra of a given object: "beams" cutouts from the FLT files and drizzled combinations of them, combined for the available grisms separately and for combinations of the available PAs.


In [4]:
# Fitting templates

# First is set with combined emission line complexes for the redshift fit 
# (don't allow infinite freedom) of the line ratios / fluxes
templ0 = grizli.utils.load_templates(fwhm=1200, line_complexes=True, stars=False, 
                                     full_line_list=None,  continuum_list=None, 
                                     fsps_templates=True)

# Second set has individual line templates for fitting the line fluxes
templ1 = grizli.utils.load_templates(fwhm=1200, line_complexes=False, stars=False, 
                                     full_line_list=None, continuum_list=None, 
                                     fsps_templates=True)

# Show the template names / dictionary keys
fmt = '{0:<36s} {1:<36s}'
print(fmt.format('templ0', 'templ1'))
print(fmt.format('------', '------'))

for i in range(len(templ1)):
    if i > len(templ0)-1:
        print(fmt.format('', list(templ1.keys())[i]))
    else:
        print(fmt.format(list(templ0.keys())[i], list(templ1.keys())[i]))

# Parameters for drizzled line maps
pline = {'kernel': 'point', 'pixfrac': 0.2, 'pixscale': 0.1, 'size': 8, 'wcs': None}


templ0                               templ1                              
------                               ------                              
fsps/fsps_QSF_12_v3_nolines_001.dat  fsps/fsps_QSF_12_v3_nolines_001.dat 
fsps/fsps_QSF_12_v3_nolines_002.dat  fsps/fsps_QSF_12_v3_nolines_002.dat 
fsps/fsps_QSF_12_v3_nolines_003.dat  fsps/fsps_QSF_12_v3_nolines_003.dat 
fsps/fsps_QSF_12_v3_nolines_004.dat  fsps/fsps_QSF_12_v3_nolines_004.dat 
fsps/fsps_QSF_12_v3_nolines_005.dat  fsps/fsps_QSF_12_v3_nolines_005.dat 
fsps/fsps_QSF_12_v3_nolines_006.dat  fsps/fsps_QSF_12_v3_nolines_006.dat 
fsps/fsps_QSF_12_v3_nolines_007.dat  fsps/fsps_QSF_12_v3_nolines_007.dat 
fsps/fsps_QSF_12_v3_nolines_008.dat  fsps/fsps_QSF_12_v3_nolines_008.dat 
fsps/fsps_QSF_12_v3_nolines_009.dat  fsps/fsps_QSF_12_v3_nolines_009.dat 
fsps/fsps_QSF_12_v3_nolines_010.dat  fsps/fsps_QSF_12_v3_nolines_010.dat 
fsps/fsps_QSF_12_v3_nolines_011.dat  fsps/fsps_QSF_12_v3_nolines_011.dat 
fsps/fsps_QSF_12_v3_nolines_012.dat  fsps/fsps_QSF_12_v3_nolines_012.dat 
line Ha+NII+SII+SIII+He+PaB          line PaB                            
line OIII+Hb+Hg+Hd                   line HeI-1083                       
line OII+Ne                          line SIII                           
line Gal-UV-lines                    line OII-7325                       
                                     line ArIII-7138                     
                                     line SII                            
                                     line Ha                             
                                     line OI-6302                        
                                     line HeI-5877                       
                                     line OIII                           
                                     line Hb                             
                                     line OIII-4363                      
                                     line Hg                             
                                     line Hd                             
                                     line H7                             
                                     line H8                             
                                     line H9                             
                                     line H10                            
                                     line NeIII-3867                     
                                     line OII                            
                                     line NeVI-3426                      
                                     line NeV-3346                       
                                     line MgII                           
                                     line CIV-1549                       
                                     line CIII-1906                      
                                     line CIII-1908                      
                                     line OIII-1663                      
                                     line HeII-1640                      
                                     line NIII-1750                      
                                     line NIV-1487                       
                                     line NV-1240                        
                                     line Lya                            

In [5]:
### Find IDs of specific objects to extract (2 galaxies and a star)
import astropy.units as u
tab = utils.GTable()
tab['ra'] = [53.0657456, 53.0624459, 53.077048]
tab['dec'] = [-27.720518, -27.707018, -27.705928]

idx, dr = grp.catalog.match_to_catalog_sky(tab)
source_ids = grp.catalog['NUMBER'][idx]
tab['id'] = source_ids
tab['dr'] = dr.to(u.mas)
tab['dr'].format='.1f'

In [6]:
id = source_ids[0]

# Pull out the 2D cutouts
beams = grp.get_beams(id, size=80)
mb = multifit.MultiBeam(beams, fcontam=0.2, group_name=root, psf=False)

# Save a FITS file with the 2D cutouts (beams) from the individual exposures
mb.write_master_fits()

# Fit polynomial model for initial continuum subtraction
wave = np.linspace(2000,2.5e4,100)
poly_templates = grizli.utils.polynomial_templates(wave, order=7)
pfit = mb.template_at_z(z=0, templates=poly_templates, fit_background=True, 
                        fitter='lstsq', get_uncertainties=2)

# Drizzle grisms / PAs and make a figure
hdu, fig = mb.drizzle_grisms_and_PAs(fcontam=0.2, flambda=False, kernel='point', 
                                     size=32, zfit=pfit, diff=True)

# Save drizzle figure FITS file
fig.savefig('{0}_{1:05d}.stack.png'.format(root, id))
hdu.writeto('{0}_{1:05d}.stack.fits'.format(root, id), clobber=True)


j033216m2743_00152.beams.fits

In [7]:
# High-level wrapper script for doing everything (redshift fits, line fluxes, drizzled line 
# maps).  More explanation of the details of individual steps TBD.
#
# Needs to be able to find {root}_{id:05d}.beams.fits and {root}_{id:05d}.stack.fits 
# generated above
from grizli import fitting
out = fitting.run_all_parallel(id, get_output_data=True, 
                               fit_beams=True, fit_only_beams=True, 
                               verbose=False,
                               argsfile='fit_args.npy')


Run id=152 with fit_args.npy
j033216m2743_00152.full.fits

In [8]:
# Products
mb, st, fit, tfit, line_hdu = out

# Objects that are used for the fitting.  They are 
# both subclasses of grizli.fitting.GroupFitter, 
# which provides the fitting methods.
print('mb: ', mb.__class__) # From "beams.fits"
print('st: ', st.__class__) # Drizzled spectra from "stack.fits" -- DEPRECATED


mb:  <class 'grizli.multifit.MultiBeam'>
st:  <class 'NoneType'>

In [9]:
# Properties of the fit on the redshift grid
# stored in an astropy.table.Table
print('Fit table:', fit.colnames)
for k in fit.meta:
    print(k, fit.meta[k])


Fit table: ['zgrid', 'chi2', 'coeffs', 'covar', 'pdf', 'risk']
N (8, 'Number of spectrum extensions')
polyord (3, 'Order polynomial fit')
chi2poly (8883.249505790865, 'Chi^2 of polynomial fit')
chi2spl (8724.683022125279, 'Chi^2 of spline fit')
Rspline (30, 'R=lam/dlam of spline fit')
kspl (36, 'Parameters, k, of spline fit')
huberdel (4, 'Huber delta parameter, see scipy.special.huber')
splf01 (0.0, 'Spline flux at 0.61 um')
sple01 (0.0, 'Spline flux err at 0.61 um')
splf02 (1.0433726849084645e-18, 'Spline flux at 0.81 um')
sple02 (8.321498196858621e-19, 'Spline flux err at 0.81 um')
splf03 (3.762355800395128e-19, 'Spline flux at 0.90 um')
sple03 (1.9310076965357014e-19, 'Spline flux err at 0.90 um')
splf04 (3.8550273122679896e-19, 'Spline flux at 1.27 um')
sple04 (6.685032741181579e-20, 'Spline flux err at 1.27 um')
splf05 (3.488747228218265e-19, 'Spline flux at 1.40 um')
sple05 (5.39503162116942e-20, 'Spline flux err at 1.40 um')
NTEMP (17, 'Number of fitting templates')
DoF (9411, 'Degrees of freedom (number of pixels)')
ktempl (14, 'Parameters, k, of template fit')
chimin (8607.022750360617, 'Minimum chi2 of template fit')
chimax (9092.65564904033, 'Maximum chi2 of template fit')
fitter ('nnls', 'Minimization algorithm')
as_epsf (0, 'Object fit with effective PSF morphology')
bic_poly (386.0223693925543, 'BIC of polynomial fit')
bic_spl (447.04711365157834, 'BIC of spline fit')
bic_temp (128.094882956023, 'BIC of template fit')
T001NAME ('fsps/fsps_QSF_12_v3_nolines_001.dat', 'Template name')
T002NAME ('fsps/fsps_QSF_12_v3_nolines_002.dat', 'Template name')
T003NAME ('fsps/fsps_QSF_12_v3_nolines_003.dat', 'Template name')
T004NAME ('fsps/fsps_QSF_12_v3_nolines_004.dat', 'Template name')
T005NAME ('fsps/fsps_QSF_12_v3_nolines_005.dat', 'Template name')
T006NAME ('fsps/fsps_QSF_12_v3_nolines_006.dat', 'Template name')
T007NAME ('fsps/fsps_QSF_12_v3_nolines_007.dat', 'Template name')
T008NAME ('fsps/fsps_QSF_12_v3_nolines_008.dat', 'Template name')
T009NAME ('fsps/fsps_QSF_12_v3_nolines_009.dat', 'Template name')
T010NAME ('fsps/fsps_QSF_12_v3_nolines_010.dat', 'Template name')
T011NAME ('fsps/fsps_QSF_12_v3_nolines_011.dat', 'Template name')
T012NAME ('fsps/fsps_QSF_12_v3_nolines_012.dat', 'Template name')
T013NAME ('alf_SSP.dat', 'Template name')
T014NAME ('line Ha+NII+SII+SIII+He+PaB', 'Template name')
T014FWHM (1000, 'FWHM, if emission line')
T015NAME ('line OIII+Hb+Hg+Hd', 'Template name')
T015FWHM (1000, 'FWHM, if emission line')
T016NAME ('line OII+Ne', 'Template name')
T016FWHM (1000, 'FWHM, if emission line')
T017NAME ('line Gal-UV-lines', 'Template name')
T017FWHM (1000, 'FWHM, if emission line')
st_df (23.66457491822874, 'Student-t df of spline fit')
st_loc (0.008196425102240147, 'Student-t loc of spline fit')
st_scl (0.9156047248534518, 'Student-t scale of spline fit')
hasprior (False, 'Prior applied to PDF')
Z02 (1.740431502251477, 'Integrated p(z) = 0.025')
Z16 (1.741222525688178, 'Integrated p(z) = 0.16')
Z50 (1.7420219529780239, 'Integrated p(z) = 0.5')
Z84 (1.7428227224789197, 'Integrated p(z) = 0.84')
Z97 (1.7436115008452804, 'Integrated p(z) = 0.975')
ZWIDTH1 (0.001600196790741748, 'Width between the 16th and 84th p(z) percentiles')
ZWIDTH2 (0.003179998593803468, 'Width between the 2.5th and 97.5th p(z) percentiles')
z_map (1.7420162338643563, 'Redshift at MAX(PDF)')
zrmin (0.050000000000000044, 'z grid start')
zrmax (3.396418200943862, 'z grid end')
z_risk (1.74202494630871, 'Redshift at minimum risk')
min_risk (3.71620562761906e-06, 'Minimum risk')
gam_loss (0.15, 'Gamma factor of the risk/loss function')

In [10]:
plt.plot(fit['zgrid'], fit['chi2'])
plt.xlabel('z'); plt.ylabel(r'$\chi^2$')


Out[10]:
Text(0, 0.5, '$\\chi^2$')

In [11]:
# Results of the fit at the best determined redshift
print(tfit.keys())
print('z = {0}'.format(tfit['z']))

print('Continuum template, cont1d: ', tfit['cont1d'].__class__)
plt.plot(tfit['cont1d'].wave/1.e4, tfit['cont1d'].flux, label='continuum')
plt.plot(tfit['line1d'].wave/1.e4, tfit['line1d'].flux, label='total')
plt.xlim(0.8, 1.7); plt.ylim(0,1.e-18); plt.grid()
plt.xlabel(r'$\lambda$, microns'); plt.ylabel(r'$f_\lambda$, erg/s/cm2/A'); plt.legend()

# cfit, coeffs, covar are coefficients of the template fit
# and their covariance


odict_keys(['cont1d', 'line1d', 'cfit', 'coeffs', 'chi2', 'covar', 'z', 'templates'])
z = 1.742015699170632
Continuum template, cont1d:  <class 'grizli.utils.SpectrumTemplate'>
Out[11]:
<matplotlib.legend.Legend at 0x7fd429e2cef0>

Retrieving the fit output

All of the information above is also stored in the *full.fits file, so you don't have to retrieve the outputs directly from the fitting script.


In [12]:
fit_hdu = pyfits.open('{0}_{1:05d}.full.fits'.format(root, id)) 
fit_hdu.info()


Filename: j033216m2743_00152.full.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU      97   ()      
  1  ZFIT_STACK    1 BinTableHDU     92   400R x 6C   [D, D, 25D, 625D, D, D]   
  2  ZFIT_BEAM     1 BinTableHDU     87   62R x 6C   [D, D, 25D, 625D, D, D]   
  3  TEMPL         1 BinTableHDU    177   9830R x 3C   [D, D, D]   
  4  COVAR         1 ImageHDU       203   (53, 53)   float64   
  5  SEG           1 ImageHDU         8   (80, 80)   int32   
  6  DSCI        F098M ImageHDU        28   (80, 80)   float32   
  7  DWHT        F098M ImageHDU        28   (80, 80)   float32   
  8  DSCI        F140W ImageHDU        28   (80, 80)   float32   
  9  DWHT        F140W ImageHDU        28   (80, 80)   float32   
 10  LINE        OI-6302 ImageHDU        31   (80, 80)   float32   
 11  CONTINUUM   OI-6302 ImageHDU        31   (80, 80)   float32   
 12  CONTAM      OI-6302 ImageHDU        31   (80, 80)   float32   
 13  LINEWHT     OI-6302 ImageHDU        31   (80, 80)   float32   
 14  LINE        HeI-5877 ImageHDU        31   (80, 80)   float32   
 15  CONTINUUM   HeI-5877 ImageHDU        31   (80, 80)   float32   
 16  CONTAM      HeI-5877 ImageHDU        31   (80, 80)   float32   
 17  LINEWHT     HeI-5877 ImageHDU        31   (80, 80)   float32   
 18  LINE        OIII ImageHDU        31   (80, 80)   float32   
 19  CONTINUUM   OIII ImageHDU        31   (80, 80)   float32   
 20  CONTAM      OIII ImageHDU        31   (80, 80)   float32   
 21  LINEWHT     OIII ImageHDU        31   (80, 80)   float32   
 22  LINE        Hb  ImageHDU        31   (80, 80)   float32   
 23  CONTINUUM   Hb  ImageHDU        31   (80, 80)   float32   
 24  CONTAM      Hb  ImageHDU        31   (80, 80)   float32   
 25  LINEWHT     Hb  ImageHDU        31   (80, 80)   float32   
 26  LINE        OIII-4363 ImageHDU        31   (80, 80)   float32   
 27  CONTINUUM   OIII-4363 ImageHDU        31   (80, 80)   float32   
 28  CONTAM      OIII-4363 ImageHDU        31   (80, 80)   float32   
 29  LINEWHT     OIII-4363 ImageHDU        31   (80, 80)   float32   
 30  LINE        Hg  ImageHDU        31   (80, 80)   float32   
 31  CONTINUUM   Hg  ImageHDU        31   (80, 80)   float32   
 32  CONTAM      Hg  ImageHDU        31   (80, 80)   float32   
 33  LINEWHT     Hg  ImageHDU        31   (80, 80)   float32   
 34  LINE        Hd  ImageHDU        31   (80, 80)   float32   
 35  CONTINUUM   Hd  ImageHDU        31   (80, 80)   float32   
 36  CONTAM      Hd  ImageHDU        31   (80, 80)   float32   
 37  LINEWHT     Hd  ImageHDU        31   (80, 80)   float32   
 38  LINE        H7  ImageHDU        31   (80, 80)   float32   
 39  CONTINUUM   H7  ImageHDU        31   (80, 80)   float32   
 40  CONTAM      H7  ImageHDU        31   (80, 80)   float32   
 41  LINEWHT     H7  ImageHDU        31   (80, 80)   float32   
 42  LINE        H8  ImageHDU        31   (80, 80)   float32   
 43  CONTINUUM   H8  ImageHDU        31   (80, 80)   float32   
 44  CONTAM      H8  ImageHDU        31   (80, 80)   float32   
 45  LINEWHT     H8  ImageHDU        31   (80, 80)   float32   
 46  LINE        H9  ImageHDU        31   (80, 80)   float32   
 47  CONTINUUM   H9  ImageHDU        31   (80, 80)   float32   
 48  CONTAM      H9  ImageHDU        31   (80, 80)   float32   
 49  LINEWHT     H9  ImageHDU        31   (80, 80)   float32   
 50  LINE        H10 ImageHDU        31   (80, 80)   float32   
 51  CONTINUUM   H10 ImageHDU        31   (80, 80)   float32   
 52  CONTAM      H10 ImageHDU        31   (80, 80)   float32   
 53  LINEWHT     H10 ImageHDU        31   (80, 80)   float32   
 54  LINE        NeIII-3867 ImageHDU        31   (80, 80)   float32   
 55  CONTINUUM   NeIII-3867 ImageHDU        31   (80, 80)   float32   
 56  CONTAM      NeIII-3867 ImageHDU        31   (80, 80)   float32   
 57  LINEWHT     NeIII-3867 ImageHDU        31   (80, 80)   float32   
 58  LINE        OII ImageHDU        31   (80, 80)   float32   
 59  CONTINUUM   OII ImageHDU        31   (80, 80)   float32   
 60  CONTAM      OII ImageHDU        31   (80, 80)   float32   
 61  LINEWHT     OII ImageHDU        31   (80, 80)   float32   
 62  LINE        NeVI-3426 ImageHDU        31   (80, 80)   float32   
 63  CONTINUUM   NeVI-3426 ImageHDU        31   (80, 80)   float32   
 64  CONTAM      NeVI-3426 ImageHDU        31   (80, 80)   float32   
 65  LINEWHT     NeVI-3426 ImageHDU        31   (80, 80)   float32   
 66  LINE        NeV-3346 ImageHDU        31   (80, 80)   float32   
 67  CONTINUUM   NeV-3346 ImageHDU        31   (80, 80)   float32   
 68  CONTAM      NeV-3346 ImageHDU        31   (80, 80)   float32   
 69  LINEWHT     NeV-3346 ImageHDU        31   (80, 80)   float32   
 70  LINE        MgII ImageHDU        31   (80, 80)   float32   
 71  CONTINUUM   MgII ImageHDU        31   (80, 80)   float32   
 72  CONTAM      MgII ImageHDU        31   (80, 80)   float32   
 73  LINEWHT     MgII ImageHDU        31   (80, 80)   float32   
 74  DPSF        F105W ImageHDU        25   (80, 80)   float32   
 75  DPSF        F125W ImageHDU        25   (80, 80)   float32   
 76  DPSF        F140W ImageHDU        25   (80, 80)   float32   
 77  DPSF        F160W ImageHDU        25   (80, 80)   float32   

In [13]:
# Basic properties of the input data
fit_hdu[0].header


Out[13]:
SIMPLE  =                    T / conforms to FITS standard                      
BITPIX  =                    8 / array data type                                
NAXIS   =                    0 / number of array dimensions                     
EXTEND  =                    T                                                  
ID      =                  152 / Object ID                                      
RA      =    53.06575002554333 / Central R.A.                                   
DEC     =   -27.72050775519466 / Central Decl.                                  
PIXFRAC =                  0.2 / Drizzle PIXFRAC                                
DRIZKRNL= 'point   '           / Drizzle kernel                                 
NINPUT  =                    8 / Number of drizzled beams                       
FILE0001= 'ib6o23rsq_flt.fits' / Parent filename                                
GRIS0001= 'G141    '           / Beam grism element                             
PA0001  =                 74.0 / PA of dispersion axis                          
FILE0002= 'ib6o23ruq_flt.fits' / Parent filename                                
GRIS0002= 'G141    '           / Beam grism element                             
PA0002  =                 74.0 / PA of dispersion axis                          
FILE0003= 'ib6o23ryq_flt.fits' / Parent filename                                
GRIS0003= 'G141    '           / Beam grism element                             
PA0003  =                 74.0 / PA of dispersion axis                          
FILE0004= 'ib6o23s0q_flt.fits' / Parent filename                                
GRIS0004= 'G141    '           / Beam grism element                             
PA0004  =                 74.0 / PA of dispersion axis                          
FILE0005= 'ib6o21qmq_flt.fits' / Parent filename                                
GRIS0005= 'G102    '           / Beam grism element                             
PA0005  =                 75.0 / PA of dispersion axis                          
FILE0006= 'ib6o21r6q_flt.fits' / Parent filename                                
GRIS0006= 'G102    '           / Beam grism element                             
PA0006  =                 75.0 / PA of dispersion axis                          
FILE0007= 'ib6o21qoq_flt.fits' / Parent filename                                
GRIS0007= 'G102    '           / Beam grism element                             
PA0007  =                 75.0 / PA of dispersion axis                          
FILE0008= 'ib6o21r8q_flt.fits' / Parent filename                                
GRIS0008= 'G102    '           / Beam grism element                             
PA0008  =                 75.0 / PA of dispersion axis                          
NDFILT  =                    2 / Number of direct image filters                 
DFILT01 = 'F098M   '                                                            
NFILT01 =                    4 / Number of beams with this direct filter        
DFILT02 = 'F140W   '                                                            
NFILT02 =                    4 / Number of beams with this direct filter        
REDSHIFT=    1.742015699170632 / Redshift used                                  
NUMLINES=                   16 / Number of lines in this file                   
LINE001 = 'OI-6302 '                                                            
FLUX001 =               -1E-16 / Line flux, 1e-17 erg/s/cm2                     
ERR001  = 4.40775277140216E-15 / Line flux err, 1e-17 erg/s/cm2                 
LINE002 = 'HeI-5877'                                                            
FLUX002 = 7.11600971764735E-18 / Line flux, 1e-17 erg/s/cm2                     
ERR002  = 6.52840902563790E-18 / Line flux err, 1e-17 erg/s/cm2                 
LINE003 = 'OIII    '                                                            
FLUX003 = 1.42472809492159E-16 / Line flux, 1e-17 erg/s/cm2                     
ERR003  = 9.60473336036594E-18 / Line flux err, 1e-17 erg/s/cm2                 
LINE004 = 'Hb      '                                                            
FLUX004 = 3.19281863586945E-17 / Line flux, 1e-17 erg/s/cm2                     
ERR004  = 7.52312366635734E-18 / Line flux err, 1e-17 erg/s/cm2                 
LINE005 = 'OIII-4363'                                                           
FLUX005 = -1.1167948660632E-19 / Line flux, 1e-17 erg/s/cm2                     
ERR005  = 1.32834406380169E-17 / Line flux err, 1e-17 erg/s/cm2                 
LINE006 = 'Hg      '                                                            
FLUX006 = 1.21765988123416E-17 / Line flux, 1e-17 erg/s/cm2                     
ERR006  = 1.36896287943030E-17 / Line flux err, 1e-17 erg/s/cm2                 
LINE007 = 'Hd      '                                                            
FLUX007 = 4.15691669608074E-18 / Line flux, 1e-17 erg/s/cm2                     
ERR007  = 6.76107713671605E-18 / Line flux err, 1e-17 erg/s/cm2                 
LINE008 = 'H7      '                                                            
FLUX008 = -3.1556404753972E-18 / Line flux, 1e-17 erg/s/cm2                     
ERR008  = 7.55469703501321E-18 / Line flux err, 1e-17 erg/s/cm2                 
LINE009 = 'H8      '                                                            
FLUX009 = -8.0025337376360E-18 / Line flux, 1e-17 erg/s/cm2                     
ERR009  = 8.96106873931849E-18 / Line flux err, 1e-17 erg/s/cm2                 
LINE010 = 'H9      '                                                            
FLUX010 = -6.2568719794693E-18 / Line flux, 1e-17 erg/s/cm2                     
ERR010  = 8.80056945591498E-18 / Line flux err, 1e-17 erg/s/cm2                 
LINE011 = 'H10     '                                                            
FLUX011 = -3.3355377853195E-18 / Line flux, 1e-17 erg/s/cm2                     
ERR011  = 8.97584388217958E-18 / Line flux err, 1e-17 erg/s/cm2                 
LINE012 = 'NeIII-3867'                                                          
FLUX012 = 1.55419633924873E-17 / Line flux, 1e-17 erg/s/cm2                     
ERR012  = 9.31010281879448E-18 / Line flux err, 1e-17 erg/s/cm2                 
LINE013 = 'OII     '                                                            
FLUX013 =  7.0439939356129E-17 / Line flux, 1e-17 erg/s/cm2                     
ERR013  = 9.52398567322051E-18 / Line flux err, 1e-17 erg/s/cm2                 
LINE014 = 'NeVI-3426'                                                           
FLUX014 = 7.30085680327346E-18 / Line flux, 1e-17 erg/s/cm2                     
ERR014  = 1.20706714278154E-17 / Line flux err, 1e-17 erg/s/cm2                 
LINE015 = 'NeV-3346'                                                            
FLUX015 = -1.8395147748249E-18 / Line flux, 1e-17 erg/s/cm2                     
ERR015  = 1.32803098359621E-17 / Line flux err, 1e-17 erg/s/cm2                 
LINE016 = 'MgII    '                                                            
FLUX016 =                1E-13 / Line flux, 1e-17 erg/s/cm2                     
ERR016  = 1.13420546831274E-12 / Line flux err, 1e-17 erg/s/cm2                 
HASLINES= 'OI-6302 HeI-5877 OIII Hb OIII-4363 Hg Hd H7 H8 H9 H10 NeIII-3867 &'  
CONTINUE  'OII NeVI-3426 NeV-3346 MgII&'                                        
CONTINUE  '' / Lines in this file                                               
GRIZLIV = '1.0.dev1427'        / Grizli version                                 
T_G141  =          4211.742066 / Total exposure time [s]                        
N_G141  =                    4 / Number of individual exposures                 
T_G102  =          4211.742066 / Total exposure time [s]                        
N_G102  =                    4 / Number of individual exposures                 
P_G141  =                    1 / Number of PAs                                  
P_G102  =                    1 / Number of PAs                                  

In [14]:
from astropy.table import Table

# same as the fit table above, redshift fit to the stacked spectra
fit_stack = Table(fit_hdu['ZFIT_STACK'].data) 
plt.plot(fit_stack['zgrid'], fit_stack['pdf'], label='Stacked')

# zoom in around the initial best-guess with the individual "beam" spectra
if 'ZFIT_BEAM' in fit_hdu:
    fit_beam = Table(fit_hdu['ZFIT_BEAM'].data)   
    plt.plot(fit_beam['zgrid'], fit_beam['pdf'], label='Zoom, beams')

plt.xlim(0.6, 1.9); plt.semilogy(); plt.grid()
plt.ylim(1.e-80, 1e4)
plt.xlabel('z'); plt.ylabel('PDF(z)'); plt.legend()


Out[14]:
<matplotlib.legend.Legend at 0x7fd3b03b4320>

In [15]:
# Best-fit templates, like `tfit` above
templ = Table(fit_hdu['TEMPL'].data)
print(templ.colnames)


['wave', 'continuum', 'full']

In [16]:
# Extensions 'DSCI', 'LINE', 'LINEWHT'... contain the drizzled line maps
print('{0} has lines [{1}]'.format(fit_hdu.filename(), fit_hdu[0].header['HASLINES']))

# Helper script for plotting them, not generated automatically
fig = grizli.fitting.show_drizzled_lines(fit_hdu, size_arcsec=1.6, cmap='plasma_r')
fig.savefig('{0}_{1:05d}.line.png'.format(root, id))


j033216m2743_00152.full.fits has lines [OI-6302 HeI-5877 OIII Hb OIII-4363 Hg Hd H7 H8 H9 H10 NeIII-3867 OII NeVI-3426 NeV-3346 MgII]

Fit stellar templates to a point source


In [17]:
# Red star
id=source_ids[2]

In [18]:
from grizli.pipeline import auto_script
auto_script.extract(field_root=root, ids=[id], MW_EBV=0, 
                    pline=pline, run_fit=False, grp=grp, diff=True)

from IPython.display import Image
Image(filename='{0}_{1:05d}.R30.png'.format(root, id))


Out[18]:

In [19]:
mb = multifit.MultiBeam('{0}_{1:05d}.beams.fits'.format(root, id), 
                    fcontam=0.2, group_name=root, psf=False, min_sens=0.05)

# Limited set of red stellar templates
tstar = grizli.utils.load_templates(fwhm=1200, line_complexes=True, 
                                    fsps_templates=True, stars=True)

# Fit spectral types.  Just makes the plot now, outputs not saved 
fig, result, tfit = mb.xfit_star(tstar=tstar, fit_background=False,
                                 spline_correction=True, spline_args={'Rspline':5},
                                 oned_args={'bin':2})


load_master_fits: j033216m2743_00431.beams.fits
1 ib6o23rsq_flt.fits G141
2 ib6o21qmq_flt.fits G102
3 ib6o23ruq_flt.fits G141
4 ib6o21r6q_flt.fits G102
5 ib6o21qoq_flt.fits G102
6 ib6o23ryq_flt.fits G141
7 ib6o21r8q_flt.fits G102
8 ib6o23s0q_flt.fits G141
stars/M6.5.txt
stars/M8.0.txt
stars/L1.0.txt
stars/L3.5.txt
stars/L6.0.txt
stars/T2.0.txt
stars/T6.0.txt
stars/T7.5.txt

# root id ra dec chi2 chi2_flat dof nk best_template as_epsf
j033216m2743       431 53.077096 -27.706010   5506.706   5556.666       4638          7 stars/M8.0.txt       0


In [20]:
hdu, fig = mb.drizzle_grisms_and_PAs(fcontam=0.2, flambda=False, kernel='point', 
                                     size=32, zfit=tfit, diff=True)



In [21]:
# Fit for small shifts of the trace perpendicular to the dispersion
# All exposures in a given grism have the same offset
tr = mb.fit_trace_shift(verbose=False)
print('Trace offset: ', tr[0])


Trace offset:  [-0.11033768 -0.11175376]

In [22]:
# Refit.  Chi-squared is a bit improved
fig, result, tfit = mb.xfit_star(tstar=tstar, fit_background=False,
                                 spline_correction=True, spline_args={'Rspline':6}, 
                                 oned_args={'bin':2})


stars/M6.5.txt
stars/M8.0.txt
stars/L1.0.txt
stars/L3.5.txt
stars/L6.0.txt
stars/T2.0.txt
stars/T6.0.txt
stars/T7.5.txt

# root id ra dec chi2 chi2_flat dof nk best_template as_epsf
j033216m2743       431 53.077096 -27.706010   5279.396   5319.501       4608          7 stars/M8.0.txt       0


In [23]:
hdu, fig = mb.drizzle_grisms_and_PAs(fcontam=0.2, flambda=False, kernel='point', 
                                     size=32, zfit=tfit, diff=True)


Use ePSF morphological model

Can sometimes get an even better morphological model for point sources using effective PSF models in place of the observed drizzled direct image.


In [24]:
USE_EPSF = True

mb = multifit.MultiBeam('{0}_{1:05d}.beams.fits'.format(root, id), 
                    fcontam=0.2, group_name=root, psf=USE_EPSF, min_sens=0.05)

# Trace offset
tr = mb.fit_trace_shift(verbose=False)

# Fit spectral types.  Just makes the plot now, outputs not saved 
fig, result, tfit = mb.xfit_star(tstar=tstar, fit_background=False,
                                 spline_correction=True, spline_args={'Rspline':5},
                                 oned_args={'bin':2})


load_master_fits: j033216m2743_00431.beams.fits
1 ib6o23rsq_flt.fits G141
2 ib6o21qmq_flt.fits G102
3 ib6o23ruq_flt.fits G141
4 ib6o21r6q_flt.fits G102
5 ib6o21qoq_flt.fits G102
6 ib6o23ryq_flt.fits G141
7 ib6o21r8q_flt.fits G102
8 ib6o23s0q_flt.fits G141
stars/M6.5.txt
stars/M8.0.txt
stars/L1.0.txt
stars/L3.5.txt
stars/L6.0.txt
stars/T2.0.txt
stars/T6.0.txt
stars/T7.5.txt

# root id ra dec chi2 chi2_flat dof nk best_template as_epsf
j033216m2743       431 53.077096 -27.706010   4064.487   4118.262       3835          7 stars/M8.0.txt       1


In [25]:
hdu, fig = mb.drizzle_grisms_and_PAs(fcontam=0.2, flambda=False, kernel='point', 
                                     size=32, zfit=tfit, diff=True)