<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Joint fitting XRT and GBM data with XSPEC models
Goals
3ML is designed to properly joint fit data from different instruments with thier instrument dependent likelihoods.
We demostrate this with joint fitting data from GBM and XRT while simultaneously showing hwo to use the XSPEC models form astromodels
Setup
You must have you HEASARC initiated so that astromodels can find the XSPEC libraries.
Step1:  Load XRT data
Make a likelihood for the XRT including all the appropriate files
Step2:  Load GBM data
Load all the GBM data you need and make appropriate background, source time, and energy selections. Make sure to check the light curves!
Step3:  View the light curve
Step4:  Make energy selections and check them out
Step5:  Setup the model
astromodels allows you to use XSPEC models if you have XSPEC installed.
Set all the normal parameters you would in XSPEC and build a model the normal 3ML/astromodel way!
Step6:  Setup the joint likelihood
Create a point source object and model. 
Load the data into a data list and create the joint likelihood
Step7:  Fitting
Maximum Likelihood style
Step8:  And then go Bayesian!
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
%matplotlib notebook
from threeML import *
import os
Explanation: Joint fitting XRT and GBM data with XSPEC models
Goals
3ML is designed to properly joint fit data from different instruments with thier instrument dependent likelihoods.
We demostrate this with joint fitting data from GBM and XRT while simultaneously showing hwo to use the XSPEC models form astromodels
Setup
You must have you HEASARC initiated so that astromodels can find the XSPEC libraries.
End of explanation
trigger="GRB110731A"
dec=-28.546
ra=280.52
xrt_dir='xrt'
xrt = SwiftXRTLike("XRT",pha_file=os.path.join(xrt_dir,"xrt_src.pha"),
                   bak_file=os.path.join(xrt_dir,"xrt_bkg.pha"),
                   rsp_file=os.path.join(xrt_dir,"xrt.rmf"),
                   arf_file=os.path.join(xrt_dir,"xrt.arf"))
xrt.view_count_spectrum()
Explanation: Load XRT data
Make a likelihood for the XRT including all the appropriate files
End of explanation
data_dir_gbm=os.path.join('gbm','bn110731A')
trigger_number = 'bn110731465'
gbm_data = download_GBM_trigger_data(trigger_number,detectors=['n3'],destination_directory=data_dir_gbm,compress_tte=True)
# Select the time interval
src_selection = "100.169342-150.169342"
nai3 = FermiGBMTTELike('NAI3',
                         os.path.join(data_dir_gbm,"glg_tte_n3_bn110731465_v00.fit.gz"),
                         "20-90,160-250", # background selection
                         src_selection,          # source interval
                         rsp_file=os.path.join(data_dir_gbm, "glg_cspec_n3_bn110731465_v00.rsp2"))
Explanation: Load GBM data
Load all the GBM data you need and make appropriate background, source time, and energy selections. Make sure to check the light curves!
End of explanation
nai3.view_lightcurve(20,250)
Explanation: View the light curve
End of explanation
nai3.set_active_measurements("8-900")
nai3.view_count_spectrum()
Explanation: Make energy selections and check them out
End of explanation
xspec_abund('angr')
spectral_model =  XS_phabs()* XS_zphabs() * XS_powerlaw()
spectral_model.nh_1=0.101
spectral_model.nh_1.fix = True
spectral_model.nh_2=0.1114424
spectral_model.nh_2.fix = True
spectral_model.redshift_2 = 0.618
spectral_model.redshift_2.fix =True
spectral_model.display()
Explanation: Setup the model
astromodels allows you to use XSPEC models if you have XSPEC installed.
Set all the normal parameters you would in XSPEC and build a model the normal 3ML/astromodel way!
End of explanation
ptsrc = PointSource(trigger,ra,dec,spectral_shape=spectral_model)
model = Model(ptsrc)
data = DataList(xrt,nai3)
jl = JointLikelihood(model, data, verbose=False)
model.display()
Explanation: Setup the joint likelihood
Create a point source object and model. 
Load the data into a data list and create the joint likelihood
End of explanation
res = jl.fit()
res = jl.get_errors()
res = jl.get_contours(spectral_model.phoindex_3,1.5,2.5,50)
res = jl.get_contours(spectral_model.norm_3,.1,.3,25,spectral_model.phoindex_3,1.5,2.5,50)
Explanation: Fitting
Maximum Likelihood style
End of explanation
spectral_model.phoindex_3.prior = Uniform_prior(lower_bound=-5.0, upper_bound=5.0)
spectral_model.norm_3.prior = Log_uniform_prior(lower_bound=1E-5, upper_bound=1)
bayes = BayesianAnalysis(model, data)
samples = bayes.sample(n_walkers=50,burn_in=100, n_samples=1000)
fig = bayes.corner_plot(plot_contours=True, plot_density=False)
bayes.get_highest_density_interval()
cleanup_downloaded_GBM_data(gbm_data)
Explanation: And then go Bayesian!
End of explanation
<END_TASK>