Skip to content

Commit

Permalink
start the implementation of the lvk class
Browse files Browse the repository at this point in the history
  • Loading branch information
FusRoman committed May 22, 2023
1 parent 4282e74 commit 2ca0a8f
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 99 deletions.
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ per-file-ignores =
../Fink_GRB/fink_grb/offline/spark_offline.py:W503,W605
../Fink_GRB/fink_grb/utils/fun_utils.py:F811
../Fink_GRB/fink_grb/distribution/distribution.py:W503
../Fink_GRB/fink_grb/observatory/__init__.py:E402
../Fink_GRB/fink_grb/observatory/__init__.py:E402
../Fink_GRB/setup.py:W503
107 changes: 66 additions & 41 deletions fink_grb/gcn_stream/gcn_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
from lxml.objectify import ObjectifiedElement
import json
from logging import Logger
from astropy.table import Table
import astropy_healpix as ah
from base64 import b64decode
from fink_grb.observatory.LVK import LVK
from fink_grb.observatory.LVK.LVK import LVK


def load_voevent_from_path(
Expand Down Expand Up @@ -91,13 +88,30 @@ def load_voevent_from_file(
def parse_gw_alert(
txt_file: str, logger: Logger, is_test: bool = False
) -> pd.DataFrame:
"""
Load the gw event and return it as a pandas dataframe
Parameters
----------
txt_file: str
the orginal gw event
logger: Logger
the logger object
is_test: (bool, optional)
if true, run this function in test mode
Parse gw event that are mock events
Defaults to False.
Returns
-------
gw_pdf: pd.DataFrame
the gw event as a dataframe
"""
logger.info("the alert is probably a new gw")
try:
record = json.loads(record)
record = json.loads(txt_file)
except Exception as e:
logger.error(
"failed to load the gw alert:\n\talert={}\n\tcause={}".format(txt_file, e)
)
logger.error("failed to load the gw alert:\n\tcause={}".format(e))

# Only respond to mock events. Real events have GraceDB IDs like
# S1234567, mock events have GraceDB IDs like M1234567.
Expand All @@ -109,42 +123,53 @@ def parse_gw_alert(
if record["superevent_id"][0] != event_kind:
return

print(type(record))
lvk_class = LVK(record)

print(lvk_class)

if record["alert_type"] == "RETRACTION":
print(record["superevent_id"], "was retracted")
return

# Respond only to 'CBC' events. Change 'CBC' to 'Burst' to respond to
# only unmodeled burst events.
if record["event"]["group"] != "CBC":
return

# Parse sky map
skymap_str = record.get("event", {}).pop("skymap")
if skymap_str:
# Decode, parse skymap, and print most probable sky location
skymap_bytes = b64decode(skymap_str)
skymap = Table.read(io.BytesIO(skymap_bytes))

# level, ipix = ah.uniq_to_level_ipix(
# skymap[np.argmax(skymap["PROBDENSITY"])]["UNIQ"]
# )

level, ipix = ah.uniq_to_level_ipix(skymap["UNIQ"])
print(level)
print(ipix)

# print(skymap)
ra, dec = ah.healpix_to_lonlat(ipix, ah.level_to_nside(level), order="nested")
print(ra)
print(dec)
# print(f"Most probable sky location (RA, Dec) = ({ra.deg}, {dec.deg})")

# Print some information from FITS header
# print(f'Distance = {skymap.meta["DISTMEAN"]} +/- {skymap.meta["DISTSTD"]}')
print(lvk_class.err_to_arcminute())
print(lvk_class.is_observation())
print(lvk_class.is_listened_packets_types())
print(lvk_class.detect_instruments())
print(lvk_class.get_trigger_id())
print(lvk_class.get_trigger_time())

print(lvk_class.voevent_to_df())

return lvk_class.voevent_to_df()

# if record["alert_type"] == "RETRACTION":
# print(record["superevent_id"], "was retracted")
# return

# # Respond only to 'CBC' events. Change 'CBC' to 'Burst' to respond to
# # only unmodeled burst events.
# if record["event"]["group"] != "CBC":
# return

# # Parse sky map
# skymap_str = record.get("event", {}).pop("skymap")
# if skymap_str:
# # Decode, parse skymap, and print most probable sky location
# skymap_bytes = b64decode(skymap_str)
# skymap = Table.read(io.BytesIO(skymap_bytes))

# # level, ipix = ah.uniq_to_level_ipix(
# # skymap[np.argmax(skymap["PROBDENSITY"])]["UNIQ"]
# # )

# level, ipix = ah.uniq_to_level_ipix(skymap["UNIQ"])
# print(level)
# print(ipix)

# # print(skymap)
# ra, dec = ah.healpix_to_lonlat(ipix, ah.level_to_nside(level), order="nested")
# print(ra)
# print(dec)
# print(f"Most probable sky location (RA, Dec) = ({ra.deg}, {dec.deg})")

# Print some information from FITS header
# print(f'Distance = {skymap.meta["DISTMEAN"]} +/- {skymap.meta["DISTSTD"]}')

# Print remaining fields
# print("Record:")
Expand Down
7 changes: 1 addition & 6 deletions fink_grb/observatory/Fermi/Fermi.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_trigger_id(self):
"""
toplevel_params = vp.get_toplevel_params(self.voevent)

return int(toplevel_params["TrigID"]["value"])
return toplevel_params["TrigID"]["value"]

def err_to_arcminute(self):
"""
Expand All @@ -65,8 +65,3 @@ def err_to_arcminute(self):
return err
else:
raise BadInstrument("{} is not a Fermi instrument".format(instrument))


if __name__ == "__main__":
fermi = Fermi()
print(fermi)
2 changes: 1 addition & 1 deletion fink_grb/observatory/IceCube/IceCube.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_trigger_id(self):
"""
toplevel_params = vp.get_toplevel_params(self.voevent)

return int(toplevel_params["AMON_ID"]["value"])
return toplevel_params["AMON_ID"]["value"]

def detect_instruments(self):
return self.voevent.attrib["ivorn"].split("#")[1].split("_")[1]
Expand Down
2 changes: 1 addition & 1 deletion fink_grb/observatory/Integral/Integral.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_trigger_id(self):
"""
toplevel_params = vp.get_toplevel_params(self.voevent)

return int(toplevel_params["TrigID"]["value"])
return toplevel_params["TrigID"]["value"]

def err_to_arcminute(self):
"""
Expand Down
Loading

0 comments on commit 2ca0a8f

Please sign in to comment.