<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 GBM, LAT LLE and Swift Catalogs
Using 3ML's catalog and data downloading tools, it is easy to build an analysis for either a single or multiple GRBs from start to finish. 
Here, we demostrate how we can search the catalogs for trigger information and then use this to perform analysis. Currently, Swift analysis is performed with the Generic OGIPLike or XRTLike plugin only and no data dowloaders are provided. In the future, a more capable Swift plugin can be included.
Step1:  GBM catalog
The 3ML GBM catalog allows you to search via cones on the sky, trigger names, T90s, date ranges and model parameters.
Step2:  Searching cones on the sky
We can perform a cone search for a given Ra and Dec to see what GRBs are in this region of the sky.
Step3:  Or perhaps we want to know the GRBs around the Crab nebula
Step4:  Searching on T90 and time
Perhaps we want to analyze all short GRBs or GRBs in a specific date range.
First let's search on T90
Step5:  We can also search MJD or UTC. The GBM catalog uses MJD to index time. We can use astroy's time module to convert from UTC to MJD
Step6:  Searching on parameter space
We can also make selections on energy fluxes or model parameters.
GBM catalog fluxes depend on the model and the interval in which that model was fitted.
Step7:  Searching for triggers
We can also search for specific triggers
Step8:  Let's look at what a search can give us
Step9:  To automate or repeat an analysis, it is good to know the values of source/background selections that were used in the catalog. These are extracted into a dictionary.
Step10:  Let's examine what we have for GRB080916C.
We'll find the background selections and source (both peak and fluence) stored in a format readable by 3ML.
Additonally, we have the detectors used for catalog spectral analysis.
Step11:  Downloading data
We can now get the data now and have a look ourselves. Using the information from the search, we can get the TTE and RSP files needed via the GBM data downloader.
Step12:  We now have our data locally downloaded from the HEASARC servers. This gets the most current version of the files and prefers RSP2 to RSP. You can of course use other methods to obtain earlier versions or differnt DRM types.
We can see where they are stored
Step13:  3ML Analysis
Plugin setup
Let's build some plugins
Step14:  Model setup
We can use the model from the catalog and combine with the data into a JointLikelihood.
Step15:  LLE Catalog
Searching
Similar to the GBM catalog, we can search LAT LLE triggers with the LLE catalog
Step16:  We can perform cone, time, and trigger searches just like as with the GBM catalog. Currently, no T90 searches are available.
Step17:  Downloading LLE data
With the 3ML LLE plugin, we no longer need PHA files. Therefore, the 3ML LLE downloader will get the LLE event file and associated LLE RSP (if it exists) as well as the FT2 file.
Step18:  Analysis with GBM and LLE data
Analyze the LLE data you have just downloaded along with GBM data
Step19:  Swift Catalog
We can also examine the Swift GRB catalog. In the spirit of 3ML, it is linked to other instruments so that we can know which triggers were seen by multiple missions.
Step20:  Cone, time, T90, and trigger seraches
Just as before, we can search on cones, time ranges, and triggers. Thanks to the Swift team, we can immediately see if there were other detections
Step21:  Searching redshift (z) and other observations
Thanks to the detail in the Swift GRB catalog, we can easily find information out about other instruments.
Redshift
Let's first look at a reshift search. Want to know about really high-z GRBs?
Step22:  Great! Now lets get those redshifts and learn more
Step23:  We can even simply click on the links and pull up the reference when we call that column
Step24:  Other observations
There are several instruments observing GRBs. We can see all the instruments that co-observe with Swift
Step25:  Let's look at all those that are also seen by Integral and/or Chandra
Step26:  We can then obtain the informatrion from all the observing instruments from this (or any other) search
Step27:  In particular, for Fermi-GBM, we can view the trigger number. In the future, other instruments will have this feature
It is then very simple to use the GBM catalog and downloader to obtain the proper information and data to perform analysis with 3ML
Step28:  Other observations
It is also possible to investigate optical, radio, etc. other observations and their references
Step29:  Clean up
If you want to tidy up and remove the data you just downloaded, simply use the cleanup functions provided
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
%matplotlib notebook
from astropy.time import Time
from threeML import *
get_available_plugins()
Explanation: GBM, LAT LLE and Swift Catalogs
Using 3ML's catalog and data downloading tools, it is easy to build an analysis for either a single or multiple GRBs from start to finish. 
Here, we demostrate how we can search the catalogs for trigger information and then use this to perform analysis. Currently, Swift analysis is performed with the Generic OGIPLike or XRTLike plugin only and no data dowloaders are provided. In the future, a more capable Swift plugin can be included.
End of explanation
gbm_catalog = FermiGBMBurstCatalog()
Explanation: GBM catalog
The 3ML GBM catalog allows you to search via cones on the sky, trigger names, T90s, date ranges and model parameters.
End of explanation
gbm_catalog.cone_search(ra=0,dec=0,radius=10)
Explanation: Searching cones on the sky
We can perform a cone search for a given Ra and Dec to see what GRBs are in this region of the sky.
End of explanation
gbm_catalog.search_around_source('Crab',radius=5)
Explanation: Or perhaps we want to know the GRBs around the Crab nebula:
End of explanation
gbm_catalog.query("2<t90<10")
Explanation: Searching on T90 and time
Perhaps we want to analyze all short GRBs or GRBs in a specific date range.
First let's search on T90:
End of explanation
mjd = Time(['2010-01-01T00:00:00','2011-01-01T00:00:00']).mjd
gbm_catalog.query(' %f <trigger_time < %f' % tuple(mjd))
Explanation: We can also search MJD or UTC. The GBM catalog uses MJD to index time. We can use astroy's time module to convert from UTC to MJD:
End of explanation
gbm_catalog.query("pflx_band_alpha < -2./3. & pflx_band_ergflux > 1E-5")
Explanation: Searching on parameter space
We can also make selections on energy fluxes or model parameters.
GBM catalog fluxes depend on the model and the interval in which that model was fitted.
End of explanation
gbm_catalog.query_sources("GRB080916009",'GRB100204024')
Explanation: Searching for triggers
We can also search for specific triggers
End of explanation
gbm_catalog.result[['t90','t50']]
Explanation: Let's look at what a search can give us:
The matching criteria can be used to extract information about the analysis as well as for setting up your own analysis.
For example, we can look at the duration information from each GRB in our last search:
End of explanation
det_info = gbm_catalog.get_detector_information()
Explanation: To automate or repeat an analysis, it is good to know the values of source/background selections that were used in the catalog. These are extracted into a dictionary.
End of explanation
det_info['GRB080916009']
Explanation: Let's examine what we have for GRB080916C.
We'll find the background selections and source (both peak and fluence) stored in a format readable by 3ML.
Additonally, we have the detectors used for catalog spectral analysis.
End of explanation
gbm_data = download_GBM_trigger_data(trigger_name=det_info['GRB080916009']['trigger'],
                                      detectors=det_info['GRB080916009']['detectors'],
                                      destination_directory='gbm/bn080916009',
                                      compress_tte=True)
Explanation: Downloading data
We can now get the data now and have a look ourselves. Using the information from the search, we can get the TTE and RSP files needed via the GBM data downloader.
End of explanation
interval = 'fluence'
models = gbm_catalog.get_model(model=det_info['GRB080916009']['best fit model'][interval],interval=interval)
models['GRB080916009']
Explanation: We now have our data locally downloaded from the HEASARC servers. This gets the most current version of the files and prefers RSP2 to RSP. You can of course use other methods to obtain earlier versions or differnt DRM types.
We can see where they are stored:
If the data already exist in that location, the downloader will skip them.
Making an analysis
Now we have everything we need to make an analysis. We just need to create our plugins and set our models.
Getting a model from the selection
3ML's GBM catalog will build a model for you from the HEASARC values. We simply choose the model and interval we want, i.e., Band, Comt, PL, SBPL and either peak or fluence interval.
Here we will use the best fit model (SBPL) for the fluence interval.
End of explanation
det = 'n3'
nai3 = FermiGBMTTELike(name=det,
                       source_intervals=det_info['GRB080916009']['source']['fluence'],
                       background_selections=det_info['GRB080916009']['background']['full'],
                       tte_file=gbm_data[det]['tte'],
                       rsp_file=gbm_data[det]['rsp'])
det = 'n4'
nai4 = FermiGBMTTELike(name=det,
                       source_intervals=det_info['GRB080916009']['source']['fluence'],
                       background_selections=det_info['GRB080916009']['background']['full'],
                       tte_file=gbm_data[det]['tte'],
                       rsp_file=gbm_data[det]['rsp'])
det = 'b0'
bgo0 = FermiGBMTTELike(name=det,
                       source_intervals=det_info['GRB080916009']['source']['fluence'],
                       background_selections=det_info['GRB080916009']['background']['full'],
                       tte_file=gbm_data[det]['tte'],
                       rsp_file=gbm_data[det]['rsp'])
nai3.set_active_measurements('8-900')
nai4.set_active_measurements('8-900')
bgo0.set_active_measurements('250-42000')
threeML_config['gbm']['selection color'] = 'r'
nai3.view_lightcurve(stop=100)
Explanation: 3ML Analysis
Plugin setup
Let's build some plugins
End of explanation
data_list = DataList(nai3,nai4,bgo0)
jl = JointLikelihood(models['GRB080916009'], data_list=data_list)
# Now we can fit
res = jl.fit()
_ = display_ogip_model_counts(jl,min_rate=5,step=False)
Explanation: Model setup
We can use the model from the catalog and combine with the data into a JointLikelihood.
End of explanation
lle_catalog = FermiLLEBurstCatalog()
Explanation: LLE Catalog
Searching
Similar to the GBM catalog, we can search LAT LLE triggers with the LLE catalog:
End of explanation
lle_catalog.cone_search(0,0,30)
lle_catalog.query_sources('GRB080916009')
lle_catalog.result.loc['GRB080916009']['trigger_name']
Explanation: We can perform cone, time, and trigger searches just like as with the GBM catalog. Currently, no T90 searches are available.
End of explanation
lle_data_info = download_LLE_trigger_data(lle_catalog.result.loc['GRB080916009']['trigger_name'],destination_directory='lat')
lle_data_info
Explanation: Downloading LLE data
With the 3ML LLE plugin, we no longer need PHA files. Therefore, the 3ML LLE downloader will get the LLE event file and associated LLE RSP (if it exists) as well as the FT2 file.
End of explanation
lle = FermiLATLLELike('LLE',
                      lle_file=lle_data_info['lle'],
                      rsp_file=lle_data_info['rsp'],
                      ft2_file=lle_data_info['ft2'],
                      background_selections=det_info['GRB080916009']['background']['full'],
                      source_intervals=det_info['GRB080916009']['source']['fluence']
                     )
lle.view_lightcurve(stop=100)
lle.set_active_measurements('50000-100000')
data_list = DataList(nai3,nai4,bgo0,lle)
jl = JointLikelihood(models['GRB080916009'], data_list=data_list)
# Now we can fit
res = jl.fit()
_ = display_ogip_model_counts(jl,min_rate=5,step=False)
Explanation: Analysis with GBM and LLE data
Analyze the LLE data you have just downloaded along with GBM data
End of explanation
swift_catalog = SwiftGRBCatalog()
Explanation: Swift Catalog
We can also examine the Swift GRB catalog. In the spirit of 3ML, it is linked to other instruments so that we can know which triggers were seen by multiple missions.
End of explanation
swift_catalog.cone_search(0,0,10)
swift_catalog.query('"2005-09-22T15:02:00.257060" < trigger_time < "2005-10-22T15:02:00.257060"')
Explanation: Cone, time, T90, and trigger seraches
Just as before, we can search on cones, time ranges, and triggers. Thanks to the Swift team, we can immediately see if there were other detections:
End of explanation
swift_catalog.query('redshift > 7')
Explanation: Searching redshift (z) and other observations
Thanks to the detail in the Swift GRB catalog, we can easily find information out about other instruments.
Redshift
Let's first look at a reshift search. Want to know about really high-z GRBs?
End of explanation
swift_catalog.get_redshift()
Explanation: Great! Now lets get those redshifts and learn more:
End of explanation
swift_catalog.get_redshift()['reference']
Explanation: We can even simply click on the links and pull up the reference when we call that column:
End of explanation
swift_catalog.other_observing_instruments
Explanation: Other observations
There are several instruments observing GRBs. We can see all the instruments that co-observe with Swift:
End of explanation
swift_catalog.query_other_observing_instruments('Integral','Chandra')
Explanation: Let's look at all those that are also seen by Integral and/or Chandra:
End of explanation
other_instruments = swift_catalog.get_other_instrument_information()
other_instruments
Explanation: We can then obtain the informatrion from all the observing instruments from this (or any other) search:
End of explanation
other_instruments['Fermi-GBM']
Explanation: In particular, for Fermi-GBM, we can view the trigger number. In the future, other instruments will have this feature
It is then very simple to use the GBM catalog and downloader to obtain the proper information and data to perform analysis with 3ML
End of explanation
swift_catalog.get_other_observation_information()
Explanation: Other observations
It is also possible to investigate optical, radio, etc. other observations and their references:
End of explanation
cleanup_downloaded_GBM_data(gbm_data)
cleanup_downloaded_LLE_data(lle_data_info)
Explanation: Clean up
If you want to tidy up and remove the data you just downloaded, simply use the cleanup functions provided:
End of explanation
<END_TASK>