<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Notebook to test the Fit of SEDs to an IGMF model
Inspired from https
Step1:  Imports
Step2:  load the catalog
Catalog file
Step3:  Remove identifier so in names so that join operation works
Step4:  join the tables -- DOES ONLY WORK WITH WORKAROUND ABOVE
Step5:  Load the TeV SEDs
Step6:  Load the IGMF model cube
Step7:  Loop over the sources and perform the likelihood fit
Spectra you want to include in the analysis
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
%load_ext autoreload
%autoreload 2
Explanation: Notebook to test the Fit of SEDs to an IGMF model
Inspired from https://github.com/woodmd/haloanalysis/blob/master/haloanalysis/scripts/fit_igmf.py#L65-L66
End of explanation
import os
import sys
from collections import OrderedDict
import yaml
import numpy as np
from astropy.io import fits
from astropy.table import Table, Column, join, hstack, vstack
import matplotlib.pyplot as plt
import matplotlib
import fermipy.utils as utils
from fermipy.spectrum import *
from fermipy.castro import CastroData
from haloanalysis.utils import create_mask, load_source_rows
from haloanalysis.sed import HaloSED
from haloanalysis.model import CascModel, CascLike
from haloanalysis.model import scan_igmf_likelihood
Explanation: Imports
End of explanation
cat = '../data/table_std_psf0123_joint2a_stdmodel_lnl_v14.fits'
tab_pars = Table.read(cat,hdu='SCAN_PARS')
tab_ebounds = Table.read(cat,hdu='EBOUNDS')
tables = [Table.read(cat,'CATALOG'),
            Table.read(cat,'LIKELIHOOD'),
            Table.read(cat,'SED')]
for i, t in enumerate(tables):
    if 'NAME' in t.columns:
        t['name'] = t['NAME']
print tables[0]['name'].data
print tables[1]['name'].data
print tables[2]['name'].data
Explanation: load the catalog
Catalog file:
End of explanation
for i, t in enumerate(tables):
    for j,n in enumerate(t['name']):
        tables[i]['name'][j] = n[5:-2] ### WORK AROUND
print tables[0]['name'].data
Explanation: Remove identifier so in names so that join operation works
End of explanation
tab_casc = join(tables[0],tables[1]) # table is empty since 'name' names are different
tab_casc = join(tab_casc,tables[2])
print tab_casc['name'].data.shape
Explanation: join the tables -- DOES ONLY WORK WITH WORKAROUND ABOVE
End of explanation
tab_sed_tev = Table.read('../data/CompiledTeVSources.fits')
print tab_sed_tev
Explanation: Load the TeV SEDs
End of explanation
casc_model = CascModel.create_from_fits(
    '/nfs/farm/g/glast/u/mmeyer/projects/FermiHalo/Output/EBLm6/th_jet6.00/gam-2.00/results_merged_z_th6d_t1e7.fits',
)
casc_model.set_eblmodel(eblmodel = 'dominguez')
Explanation: Load the IGMF model cube
End of explanation
src_names = ['1ES0229+200_HESS_2005-2006']
nstep = 5
casc_scale = 1.
casc_r68_scale = 1.
tab_igmf = []
for name in src_names:
    rows_sed_tev = load_source_rows(tab_sed_tev, [name], key='SOURCE_FULL')
    #cat_names = [ 'FHES %s'%row['3FGL_NAME'] for row in rows_sed_tev ]
    cat_names = [ '%s'%row['3FGL_NAME'][:-2] for row in rows_sed_tev ] # WORK AROUND
    
    cat_names = np.unique(np.array(cat_names))
    rows_sed_gev = load_source_rows(tab_casc, cat_names, key='name')
    rows_casc = load_source_rows(tab_casc, cat_names, key='name')
    
    print rows_sed_tev
    print rows_sed_gev
    print rows_casc
    
    tab = scan_igmf_likelihood(casc_model, rows_sed_tev, rows_sed_gev,
                                rows_casc, tab_pars, tab_ebounds, nstep,
                                casc_scale, casc_r68_scale)
    #tab_igmf += [tab]
Explanation: Loop over the sources and perform the likelihood fit
Spectra you want to include in the analysis
End of explanation
<END_TASK>