-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #304 from bfhealy/convert-fritz-lcs
Add tool to convert skyportal lightcurves to nmma format
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |