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

Add tool to convert skyportal lightcurves to nmma format #304

Merged
Merged
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
9 changes: 9 additions & 0 deletions doc/data_inj_obs.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@ Example: AT2017gfo data excerpt
2017-08-18T18:11:31.200 2massk 17.91000 0.05000

The first column represents the ISOT time, followed by the filter or spectral band in the second column, the measured AB magnitude and its respective error are given in the third and fourth column. This data structure detailed above is also applicable for other astrophysical sources (SNe, GRBs) or models implemented in NMMA.

Example: transient data downloaded from [SkyPortal](https://github.com/skyportal/skyportal)/fritz:

"id","mjd","mag","magerr","limiting_mag","filter","instrument_name","instrument_id","snr","magsys","origin","altdata","ra","dec","ra_unc","dec_unc","created_at","annotations","owner","Edit","Delete"
"518817455","60257.28104169993","","","19.30739974975586","ztfr","ZTF","1","","ab","None","","","","","","2023-12-07T05:49:42.369500","","[object Object]","",""
"518817456","60257.282500000205","","","19.347900390625","ztfr","ZTF","1","","ab","None","","","","","","2023-12-07T05:49:42.369555","","[object Object]","",""
"518817457","60257.36459490005","","","20.112499237060547","ztfg","ZTF","1","","ab","None","","","","","","2023-12-07T05:49:42.369574","","[object Object]","",""

Data downloaded from a source's photometry table on SkyPortal take the comma-separated format above. These files must be converted to NMMA format (see first example above) before running analyses. Run `tools/convert_skyportal_lcs.py --filepath <path/to/lcs.csv>` to perform the conversion.
57 changes: 57 additions & 0 deletions tools/convert_skyportal_lcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python
from astropy.table import Table
import numpy as np
from astropy.time import Time
import pathlib
import argparse

BASE_DIR = pathlib.Path(__file__).parent.parent.absolute()


def main(
filepath,
):
data_path = BASE_DIR / filepath
try:
data = Table.read(data_path, format="ascii.csv")
except Exception as e:
raise ValueError(f"input data is not in the expected format {e}")

try:
local_data_path = str(data_path.with_suffix(".dat"))
with open(local_data_path, "w") as f:
# output the data in the format desired by NMMA:
# remove rows where mag and magerr are missing, or not float, or negative
data = data[
np.isfinite(data["mag"])
& np.isfinite(data["magerr"])
& (data["mag"] > 0)
& (data["magerr"] > 0)
]
for row in data:
tt = Time(row["mjd"], format="mjd").isot
filt = row["filter"]
mag = row["mag"]
magerr = row["magerr"]
f.write(f"{tt} {filt} {mag} {magerr}\n")
print(f"Wrote reformatted lightcurve to {local_data_path}")
except Exception as e:
raise ValueError(f"failed to format data {e}")


def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
"--filepath",
type=str,
required=True,
help="path/name of lightcurve file (including extension) within base nmma directory",
)

return parser


if __name__ == "__main__":
parser = get_parser()
args = parser.parse_args()
main(**vars(args))
Loading