Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SWIFT_BAT_GRB_POS_ACK conversion #36

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
68 changes: 68 additions & 0 deletions gcn_classic_to_json/notices/SWIFT_BAT_GRB_POS_ACK/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,78 @@
import numpy as np

from ... import utils

flag_descriptions = {
0: "A point source was found.",
1: "It is a GRB.",
2: "It is an interesting src.",
3: "It is in the flight catalog.",
5: "It is definitely not a GRB.",
6: "It is probably not a GRB or Transient(hi bkg level).",
7: "It is probably not a GRB or Transient(low image significance; < 7).",
8: "It is in the ground catalog.",
9: "It is probably not a GRB or Transient(negative bkg slop).",
10: "StraTracker not locked so trigger porbably bogus.",
11: "It is probably not a GRB or Transient(very low image significance; < 6.5).",
12: "It is the catalog of sources to be blocked.",
13: "There is a bright star nearby.",
14: "This was orginally a SubTresh, but it is now converted to a real BAT_POS.",
15: "This is a source that has purposefully been removed from on-board catalog.",
16: "This matched a Nearby_Galaxy in the on-board catalog.",
28: "There was a temporal coincidence with another event.",
29: "There was a spatial coincidence with another event.",
30: "This is a test submission",
}


def parse(bin):
bin[15] # Unused. According to docs: '4 bytes for the future'
bin[19] # Unused. Flags are either internal or equivalent to bin[18]
bin[26:36] # Unused. According to docs: '40 bytes for the future'
bin[36] # Unused. Flags Equivalent to bin[18]
bin[38] # Intentionally omitted. Sun/Moon parameters

integ_time = bin[14] * 16 / 1000

lat, lon = bin[16:17].view(">i2")

soln_status_bits = np.flip(np.unpackbits(bin[18:19].view(dtype="u1")))

comments = "\n".join(
[val for (key, val) in flag_descriptions.items() if (soln_status_bits[key])]
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please capture these flags in a machine-readable way, not as comments.

Copy link
Contributor Author

@athish-thiru athish-thiru Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use additional_info to capture the data stored in the COMMENTS: fields in the text notices. Some of these are redundant with the data in the notices and can be removed(I've mainly added them for completeness). However, some of the these comments I think may be too complex to easily translate into a field like "StarTracker not locked so trigger probably bogus" or "This was orginally a SubTresh, but it is now converted to a real BAT_POS.".


calalog_num = bin[25]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo on catalog


energy_ranges = [[15, 25], [15, 50], [25, 100], [50, 350]]
energy_range_idx = np.flip(bin[37:38].view(dtype="i1"))[0]
energy_range = energy_ranges[energy_range_idx]

return {
"mission": "SWIFT",
"instrument": "BAT",
"id": [bin[4]],
"trigger_time": utils.datetime_to_iso8601(bin[5], bin[6]),
"trigger_type": "image" if soln_status_bits[4] else "rate",
"image_duration": integ_time if soln_status_bits[4] else None,
"image_energy_range": energy_range if soln_status_bits[4] else None,
"rate_duration": integ_time if not soln_status_bits[4] else None,
"rate_energy_range": integ_time if not soln_status_bits[4] else None,
"ra": 1e-4 * bin[7],
"dec": 1e-4 * bin[8],
"ra_dec_error": 1e-4 * bin[11],
"instrument_phi": 1e-2 * bin[12],
"instrument_theta": 1e-2 * bin[13],
"latitude": lat * 1e-2,
"longitude": lon * 1e-2,
"rate_snr": bin[21] * 1e-2,
"image_snr": bin[20] * 1e-2,
"n_events": bin[9],
"image_peak": bin[10],
"background_events": bin[22],
"background_start_time": utils.datetime_to_iso8601(bin[5], bin[23]),
"backgroun_duration": bin[24] * 1e-2,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: should be "background_duration"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @bob-wiegand! Could you link the package that you used for spell-checking? I think it would be easier for me to run this on my end and correct it for each notice type instead of you having to manually list each error. Thanks!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I am not sure it would be simpler since it was still somewhat manual.
I ran https://github.com/crate-ci/typos
$ typos --words
on the files to find the words then
$ hunspell -d en_US
to check them. hunspell will build up a dictionary of excusable terms for gcn-*.

The silliest aspect is that I was unable to find a way to get typos to exclude times (e.g., 1987-01-02T03:04:05.6); although extend-ignore-re will exclude the times from the misspelling list, I could not figure out how to get it to exclude them from the word/identifier list.

"trigger_index": bin[17],
"catalog_number": calalog_num if soln_status_bits[3] else None,
"additional_info": comments if comments else None,
}
26 changes: 25 additions & 1 deletion gcn_classic_to_json/notices/SWIFT_BAT_GRB_POS_ACK/example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
{
"mission": "SWIFT",
"instrument": "BAT",
"id": [
1227767
],
"trigger_time": "2024-05-11T18:06:53.220Z",
"trigger_type": "rate",
"image_duration": null,
"image_energy_range": null,
"rate_duration": 8.192,
"rate_energy_range": 8.192,
"ra": 336.66450000000003,
"dec": 8.5135,
"ra_dec_error": 0.05
"ra_dec_error": 0.05,
"instrument_phi": -173.54,
"instrument_theta": 28.38,
"latitude": -11.14,
"longitude": 151.31,
"rate_snr": 14.89,
"image_snr": 8.52,
"n_events": 7538,
"image_peak": 281,
"background_events": 74677,
"background_start_time": "2024-05-11T18:06:22.570Z",
"backgroun_duration": 24.0,
"trigger_index": 262,
"catalog_number": null,
"additional_info": "A point source was found.\nIt is a GRB.\nThere was a spatial coincidence with another event."
}
11 changes: 1 addition & 10 deletions gcn_classic_to_json/test/test_notices.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
files = importlib.resources.files(notices)


def keys_passing_except_for(*failing):
return list(set(notices.keys) - set(failing)) + [
pytest.param(item, marks=pytest.mark.xfail) for item in failing
]


class NDArrayNanny(np.ndarray):
"""A ndarray subclass that tracks which elements have been accessed.

Expand Down Expand Up @@ -44,10 +38,7 @@ def __getitem__(self, i):
return super().__getitem__(i)


@pytest.mark.parametrize(
"key",
keys_passing_except_for("SWIFT_BAT_GRB_POS_ACK"),
)
@pytest.mark.parametrize("key", notices.keys)
def test_all_fields_used(key, monkeypatch):
"""Check that every field in the binary packet is used in the conversion."""

Expand Down
Loading