Skip to content

Commit

Permalink
Merge pull request #341 from tylerbarna/load-event-function-try-except
Browse files Browse the repository at this point in the history
More consistent loading of lightcurve files
  • Loading branch information
bfhealy committed Mar 19, 2024
2 parents 1c1603e + d8fac86 commit 9cf8dc3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
11 changes: 2 additions & 9 deletions nmma/em/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,15 +565,8 @@ def analysis(args):
lc.to_csv(args.injection_outfile)

else:
# load the kilonova afterglow data
try:
data = loadEvent(args.data)

except ValueError:
with open(args.data) as f:
data = json.load(f)
for key in data.keys():
data[key] = np.array(data[key])
# load the lightcurve data
data = loadEvent(args.data)

if args.trigger_time is None:
# load the minimum time as trigger time
Expand Down
60 changes: 38 additions & 22 deletions nmma/em/io.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import astropy
from astropy.time import Time
import h5py
Expand All @@ -9,28 +10,43 @@


def loadEvent(filename):
lines = [line.rstrip("\n") for line in open(filename)]
lines = filter(None, lines)

sncosmo_filts = [val["name"] for val in _BANDPASSES.get_loaders_metadata()]
sncosmo_maps = {name: name.replace(":", "_") for name in sncosmo_filts}

data = {}
for line in lines:
lineSplit = line.split(" ")
lineSplit = list(filter(None, lineSplit))
mjd = Time(lineSplit[0], format="isot").mjd
filt = lineSplit[1]

if filt in sncosmo_maps:
filt = sncosmo_maps[filt]

mag = float(lineSplit[2])
dmag = float(lineSplit[3])

if filt not in data:
data[filt] = np.empty((0, 3), float)
data[filt] = np.append(data[filt], np.array([[mjd, mag, dmag]]), axis=0)
"""
Reads in lightcurve data from a file and returns data in a dictionary format.
Args:
- filename (str): Path to lightcurve file
Returns:
- data (dict): Dictionary containing the lightcurve data from the file. The keys are generally 't' and each of the filters in the file as well as their accompanying error values.
"""
if filename.endswith(".json"):
with open(filename) as f:
data = json.load(f)
for key in data.keys():
data[key] = np.array(data[key])
else:
lines = [line.rstrip("\n") for line in open(filename)]
lines = filter(None, lines)

sncosmo_filts = [val["name"] for val in _BANDPASSES.get_loaders_metadata()]
sncosmo_maps = {name: name.replace(":", "_") for name in sncosmo_filts}

data = {}
for line in lines:
lineSplit = line.split(" ")
lineSplit = list(filter(None, lineSplit))
mjd = Time(lineSplit[0], format="isot").mjd
filt = lineSplit[1]

if filt in sncosmo_maps:
filt = sncosmo_maps[filt]

mag = float(lineSplit[2])
dmag = float(lineSplit[3])

if filt not in data:
data[filt] = np.empty((0, 3), float)
data[filt] = np.append(data[filt], np.array([[mjd, mag, dmag]]), axis=0)

return data

Expand Down

0 comments on commit 9cf8dc3

Please sign in to comment.