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

Changes to generating band light curves - get spectrum between timemin and timemax #217

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
73 changes: 35 additions & 38 deletions artistools/lightcurve/lightcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,34 +188,20 @@ def generate_band_lightcurve_data(

if args.plotvspecpol and (modelpath / "vpkt.txt").is_file():
print("Found vpkt.txt, using virtual packets")
stokes_params = (
at.spectra.get_vspecpol_data(vspecindex=angle, modelpath=modelpath)
if angle >= 0
else at.spectra.get_specpol_data(angle=angle, modelpath=modelpath)
)
vspecdata = stokes_params["I"]
timearray = vspecdata.columns[1:]

elif args.plotviewingangle and at.anyexist(["specpol_res.out", "spec_res.out"], folder=modelpath, tryzipped=True):
specfilename = at.firstexisting(["specpol_res.out", "spec_res.out"], folder=modelpath, tryzipped=True)
specdataresdata = pd.read_csv(specfilename, sep=r"\s+")
timearray = [i for i in specdataresdata.columns.to_numpy()[1:] if i[-2] != "."]
# elif Path(modelpath, 'specpol.out').is_file():
# specfilename = os.path.join(modelpath, "specpol.out")
# specdata = pd.read_csv(specfilename, sep='\s+')
# timearray = [i for i in specdata.columns.values[1:] if i[-2] != '.']

else:
if args.plotviewingangle:
print("WARNING: no direction-resolved spectra available. Using angle-averaged spectra.")

specfilename = at.firstexisting(["spec.out", "specpol.out"], folder=modelpath, tryzipped=True)
specdata = pd.read_csv(specfilename, sep=r"\s+")

timearray = (
# Ignore Q and U values in pol file
[i for i in specdata.columns.to_numpy()[1:] if i[-2] != "."]
if "specpol.out" in str(specfilename)
else specdata.columns.to_numpy()[1:]
)
print(f"Using {specfilename}")

timearray = at.get_timestep_times(modelpath=modelpath, loc="start")
timeendarray = at.get_timestep_times(modelpath=modelpath, loc="end")

filters_dict = {}
if not args.filter:
Expand All @@ -228,6 +214,7 @@ def generate_band_lightcurve_data(
times, bol_magnitudes = bolometric_magnitude(
modelpath,
timearray,
timeendarray,
args,
angle=angle,
average_over_phi=args.average_over_phi_angle,
Expand All @@ -250,12 +237,17 @@ def generate_band_lightcurve_data(
filterdir, filter_name
)

for timestep, time in enumerate(float(time) for time in timearray):
if (args.timemin is None or args.timemin <= time) and (args.timemax is None or args.timemax >= time):
for timemin, timemax in zip(timearray, timeendarray, strict=False):
timemin = float(timemin)
timemax = float(timemax)
if (args.timemin is None or args.timemin <= timemin) and (
timemax < float(timearray[-1]) and (args.timemax is None or args.timemax >= timemax)
):
timeavg = (timemin + timemax) / 2.0
wavelength_from_spectrum, flux = get_spectrum_in_filter_range(
modelpath=modelpath,
timestep=timestep,
time=time,
timemin=timemin,
timemax=timemax,
wavefilter_min=wavefilter_min,
wavefilter_max=wavefilter_max,
angle=angle,
Expand All @@ -280,14 +272,15 @@ def generate_band_lightcurve_data(
# print(time, phot_filtobs_sn)
if phot_filtobs_sn != 0.0:
phot_filtobs_sn -= 25 # Absolute magnitude
filters_dict[filter_name].append((time, phot_filtobs_sn))
filters_dict[filter_name].append((timeavg, phot_filtobs_sn))

return filters_dict


def bolometric_magnitude(
modelpath: Path,
timearray: t.Collection[float | str],
timeendarray: t.Collection[float | str],
args: argparse.Namespace,
angle: int = -1,
average_over_phi: bool = False,
Expand All @@ -296,19 +289,24 @@ def bolometric_magnitude(
magnitudes = []
times = []

for timestep, time in enumerate(float(time) for time in timearray):
if (args.timemin is None or args.timemin <= time) and (args.timemax is None or args.timemax >= time):
for timemin, timemax in zip(timearray, timeendarray, strict=False):
if (args.timemin is None or args.timemin <= timemin) and (args.timemax is None or args.timemax >= timemax):
timestepmin = at.get_timestep_of_timedays(modelpath, timemin)
timestepmax = at.get_timestep_of_timedays(modelpath, timemax)
timeavg = (float(timemin) + float(timemax)) / 2.0
if angle == -1:
spectrum = at.spectra.get_spectrum(modelpath=modelpath, timestepmin=timestep, timestepmax=timestep)[-1]
spectrum = at.spectra.get_spectrum(
modelpath=modelpath, timestepmin=timestepmin, timestepmax=timestepmax
)[-1]

elif args.plotvspecpol:
spectrum = at.spectra.get_vspecpol_spectrum(modelpath, time, angle, args)
spectrum = at.spectra.get_vspecpol_spectrum(modelpath, timeavg, angle, args)
else:
spectrum = at.spectra.get_spectrum(
modelpath=modelpath,
directionbins=[angle],
timestepmin=timestep,
timestepmax=timestep,
timestepmin=timestepmin,
timestepmax=timestepmax,
average_over_phi=average_over_phi,
average_over_theta=average_over_theta,
)[angle]
Expand All @@ -318,7 +316,7 @@ def bolometric_magnitude(
with np.errstate(divide="ignore"):
magnitude = Mbol_sun - (2.5 * np.log10(integrated_luminosity / const.L_sun.to("erg/s").value))
magnitudes.append(magnitude)
times.append(time)
times.append(timeavg)
# print(const.L_sun.to('erg/s').value)
# sys.exit(1)

Expand Down Expand Up @@ -347,8 +345,8 @@ def get_filter_data(filterdir: Path | str, filter_name: str) -> tuple[float, np.

def get_spectrum_in_filter_range(
modelpath: Path | str,
timestep: int,
time: float,
timemin: float,
timemax: float,
wavefilter_min: float,
wavefilter_max: float,
angle: int = -1,
Expand All @@ -359,8 +357,8 @@ def get_spectrum_in_filter_range(
) -> tuple[np.ndarray, np.ndarray]:
spectrum = at.spectra.get_spectrum_at_time(
Path(modelpath),
timestep=timestep,
time=time,
timemin=timemin,
timemax=timemax,
args=args,
dirbin=angle,
average_over_phi=average_over_phi,
Expand Down Expand Up @@ -393,8 +391,7 @@ def get_band_lightcurve(
(time, brightness)
for time, brightness in band_lightcurve_data[band_name]
if ((args.timemin is None or args.timemin <= time) and (args.timemax is None or args.timemax >= time))
],
strict=False,
]
)

return times, np.array(brightness_in_mag)
Expand Down
11 changes: 6 additions & 5 deletions artistools/spectra/spectra.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,17 @@ def stackspectra(

def get_spectrum_at_time(
modelpath: Path,
timestep: int,
time: float,
timemin: float,
timemax: float,
args: argparse.Namespace | None,
dirbin: int = -1,
average_over_phi: bool | None = None,
average_over_theta: bool | None = None,
) -> pd.DataFrame:
if dirbin >= 0:
timeavg = (timemax + timemin) / 2
if args is not None and args.plotvspecpol and (modelpath / "vpkt.txt").is_file():
return get_vspecpol_spectrum(modelpath, time, dirbin, args).to_pandas(use_pyarrow_extension_array=True)
return get_vspecpol_spectrum(modelpath, timeavg, dirbin, args).to_pandas(use_pyarrow_extension_array=True)
assert average_over_phi is not None
assert average_over_theta is not None
else:
Expand All @@ -119,8 +120,8 @@ def get_spectrum_at_time(
return get_spectrum(
modelpath=modelpath,
directionbins=[dirbin],
timestepmin=timestep,
timestepmax=timestep,
timestepmin=at.get_timestep_of_timedays(modelpath, timemin),
timestepmax=at.get_timestep_of_timedays(modelpath, timemax),
average_over_phi=average_over_phi,
average_over_theta=average_over_theta,
)[dirbin].to_pandas(use_pyarrow_extension_array=True)
Expand Down
Loading