Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions morgoth/utils/result_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
from astropy.coordinates import SkyCoord
import astropy.units as unit

from astropy.table import Table
from astropy.coordinates import Angle
import pandas as pd
import os, ssl
import requests


base_dir = get_env_value("GBM_TRIGGER_DATA_DIR")


Expand Down Expand Up @@ -74,6 +81,12 @@ def __init__(
trigger_number=self._trigger_number, grb_name=self.grb_name
)

# Check catalog of bright gamma sources and get separation to GRB position
self._sep_bright_sources()

# Check catalog of SGRs and get separation to GRB position
self._sep_SGRs()

# Create a report containing all the results of the pipeline
self._build_report()

Expand Down Expand Up @@ -295,6 +308,109 @@ def _read_post_equal_weights_file(self, post_equal_weights_file):

if dec_err is not None:
self._dec_err = dec_err


def _sep_bright_sources(self):

#for the case of certification errror
if (not os.environ.get('PYTHONHTTPSVERIFY', '') and
getattr(ssl, '_create_unverified_context', None)):
ssl._create_default_https_context = ssl._create_unverified_context

#read in table from website
url = "https://swift.gsfc.nasa.gov/results/transients/BAT_current.html"
table_MN = pd.read_html(url)
df = table_MN[0]

#delete unwanted string
df.rename(columns={'Peak*': 'Peak'}, inplace=True)

#filter by peak value
df = df.drop(df[df.Peak=="-"].index)
df = df.astype({"Peak": int})
df_filtered = df[df['Peak']>400]

#for table of catalog
table = Table.from_pandas(df_filtered)
#table.show_in_browser(jsviewer=True)

#transform input in SkyCoord
position = SkyCoord(self._ra*unit.deg, self._dec*unit.deg, frame="icrs")

#transform table data in SkyCoord
coords = []
for i in range(len(df_filtered['RA J2000 Degs'])):
ra = table[i]['RA J2000 Degs']
dec = table[i]['Dec J2000 Degs']
coords.append(SkyCoord(ra*unit.deg, dec*unit.deg, frame="icrs"))

#get separation value
separations = []
for i in coords:
z = i.separation(position)
separations.append(z.to(unit.deg))

#for table of separations
table["Separation Degs"] = separations
table.round(3)
table.sort("Separation Degs")
#table.show_in_browser(jsviewer=True)

#create dictionary
dic = {}
for i in range(3):
dic[str(table[i]['Source Name'])]={"ra":float(table[i]['RA J2000 Degs']),
"dec":float(table[i]['Dec J2000 Degs']),
"separation":float(table[i]["Separation Degs"])}

self._dic_bright_sources = dic


def _sep_SGRs(self):

#get csv data from website
url = 'http://www.physics.mcgill.ca/~pulsar/magnetar/TabO1.csv'
r = requests.get(url, allow_redirects=True)
open('SGRList.csv', 'wb').write(r.content)
df = pd.read_csv('SGRList.csv')

#for table of catalog
table = Table.from_pandas(df)

#transform table data in SkyCoord
coords = []
for i in range(len(df['RA'])):
hour_ra = Angle(table[i]['RA']+" hours")
ra = hour_ra.to(unit.deg)
arc_dec = table[i]['Decl']
dec = Angle(tuple(map(float,arc_dec.split(' '))), unit=unit.deg)
coords.append(SkyCoord(ra, dec, frame="icrs"))

#transform input in SkyCoord
position=SkyCoord(self._ra*unit.deg, self._dec*unit.deg, frame="icrs")

#get separation value
separations = []
for i in coords:
z = i.separation(position)
separations.append(z.to(unit.deg))

#for table of separations
table["Separation Degs"] = separations
table["Coords Degs"] = coords
table.round(3)
table.sort("Separation Degs")
#table.show_in_browser(jsviewer=True)

#create dictionary
dic = {}
for i in range(3):
dic[str(table[i]['Name'])]={"ra":float(round(table[i]['Coords Degs'].ra.degree,3)),
"dec":float(round(table[i]['Coords Degs'].dec.degree,3)),
"separation":float(table[i]["Separation Degs"])}

self._dic_SGRs = dic


def _build_report(self):
self._report = {
Expand Down Expand Up @@ -349,8 +465,13 @@ def _build_report(self):
"active_time_stop": self._active_time_stop,
"used_detectors": self._used_detectors,
},
"separation_values": {
"bright_sources": self._dic_bright_sources,
"SGRs": self._dic_SGRs
}
}


def save_result_yml(self, file_path):
with open(file_path, "w") as f:
yaml.dump(self._report, f, default_flow_style=False)
Expand Down
4 changes: 3 additions & 1 deletion morgoth/utils/upload_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ def create_report_from_result(result):
"balrog_two_sig_err_circle": result["fit_result"][
"balrog_two_sig_err_circle"
],
"bright_sources": result["separation_values"]["bright_sources"],
"SGRs": result["separation_values"]["SGRs"]
}
],
]
}
return report

Expand Down