Skip to content

Commit

Permalink
Remove some astropy usage
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeshingles committed Dec 23, 2024
1 parent 5e53269 commit 26fbca7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion artistools/inputmodel/slice1dfromconein3dmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def make_1d_model_files(args):
# cone = cone.loc[cone['rho_model'] > 0.0]


def make_plot(args):
def make_plot(args) -> None:
cone = make_cone(args)

cone = cone.loc[cone["rho_model"] > 0.0002] # cut low densities (empty cells?) from plot
Expand Down
4 changes: 2 additions & 2 deletions artistools/lightcurve/plotlightcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import numpy as np
import pandas as pd
import polars as pl
from astropy import constants as const

import artistools as at

Expand Down Expand Up @@ -440,7 +439,8 @@ def plot_artis_lightcurve(

if args.magnitude:
# convert to bol magnitude
lcdata["mag"] = 4.74 - (2.5 * np.log10(lcdata["lum"] / const.L_sun.to("erg/s").value))
Lsun_to_erg_s = 3.828e33
lcdata["mag"] = 4.74 - (2.5 * np.log10(lcdata["lum"] / Lsun_to_erg_s))
ycolumn = "mag"
else:
ycolumn = "lum"
Expand Down
6 changes: 2 additions & 4 deletions artistools/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,9 +1219,6 @@ def get_nprocs(modelpath: Path) -> int:
@lru_cache(maxsize=8)
def get_inputparams(modelpath: Path) -> dict[str, t.Any]:
"""Return parameters specified in input.txt."""
from astropy import constants as const
from astropy import units as u

params: dict[str, t.Any] = {}
with Path(modelpath, "input.txt").open("r", encoding="utf-8") as inputfile:
params["pre_zseed"] = int(readnoncommentline(inputfile).split("#")[0])
Expand All @@ -1234,8 +1231,9 @@ def get_inputparams(modelpath: Path) -> dict[str, t.Any]:

params["tmin"], params["tmax"] = (float(x) for x in readnoncommentline(inputfile).split("#")[0].split())

MeV_in_Hz = 2.417989242084918e20
params["nusyn_min"], params["nusyn_max"] = (
(float(x) * u.MeV / const.h).to("Hz") for x in readnoncommentline(inputfile).split("#")[0].split()
float(x) * MeV_in_Hz for x in readnoncommentline(inputfile).split("#")[0].split()
)

# number of times for synthesis
Expand Down
7 changes: 3 additions & 4 deletions artistools/nltepops/nltepops.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import pandas as pd
import polars as pl
from astropy import constants as const

import artistools as at

Expand Down Expand Up @@ -66,7 +65,7 @@ def add_lte_pops(dfpop, adata, columntemperature_tuples, noprint=False, maxlevel
columntemperature_tuples is a sequence of tuples of column name and temperature, e.g., ('mycolumn', 3000)
"""
k_b = const.k_B.to("eV / K").value
K_B = 8.617333262145179e-05 # eV / K

for _, row in dfpop.drop_duplicates(["modelgridindex", "timestep", "Z", "ion_stage"]).iterrows():
modelgridindex = int(row.modelgridindex)
Expand Down Expand Up @@ -104,7 +103,7 @@ def f_ltepop(x, T_exc: float, gsg: float, gse: float, ionlevels) -> float:
ltepop = (
ionlevels["g"].item(int(x.level))
/ gsg
* math.exp(-(ionlevels["energy_ev"].item(int(x.level)) - gse) / k_b / T_exc)
* math.exp(-(ionlevels["energy_ev"].item(int(x.level)) - gse) / K_B / T_exc)
)
assert isinstance(ltepop, float)
return ltepop
Expand Down Expand Up @@ -133,7 +132,7 @@ def f_ltepop(x, T_exc: float, gsg: float, gse: float, ionlevels) -> float:
for columnname, T_exc in columntemperature_tuples:
superlevelpop = (
ionlevels[levelnumber_sl:]
.select(pl.col("g") / gs_g * (-(pl.col("energy_ev") - gs_energy) / k_b / T_exc).exp())
.select(pl.col("g") / gs_g * (-(pl.col("energy_ev") - gs_energy) / K_B / T_exc).exp())
.sum()
.item()
)
Expand Down
12 changes: 6 additions & 6 deletions artistools/transitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class IonTuple(t.NamedTuple):


def get_kurucz_transitions() -> tuple[pd.DataFrame, list[IonTuple]]:
hc_evcm = (const.h * const.c).to("eV cm").value
hc_in_ev_cm = 0.0001239841984332003
transitiontuple = namedtuple(
"transitiontuple",
"Z ion_stage lambda_angstroms A lower_energy_ev upper_energy_ev lower_statweight upper_statweight",
Expand All @@ -40,7 +40,7 @@ def get_kurucz_transitions() -> tuple[pd.DataFrame, list[IonTuple]]:
continue
lambda_angstroms = float(line[:12]) * 10
loggf = float(line[11:18])
lower_energy_ev, upper_energy_ev = hc_evcm * float(line[24:36]), hc_evcm * float(line[52:64])
lower_energy_ev, upper_energy_ev = hc_in_ev_cm * float(line[24:36]), hc_in_ev_cm * float(line[52:64])
lower_statweight, upper_statweight = 2 * float(line[36:42]) + 1, 2 * float(line[64:70]) + 1
fij = (10**loggf) / lower_statweight
A = fij / (1.49919e-16 * upper_statweight / lower_statweight * lambda_angstroms**2)
Expand Down Expand Up @@ -103,7 +103,7 @@ def generate_ion_spectrum(
# contribute the Gaussian line profile to the discrete flux bins

centre_index = round((line["lambda_angstroms"] - args.xmin) / plot_resolution)
sigma_angstroms = line["lambda_angstroms"] * args.sigma_v / const.c.to("km / s").value
sigma_angstroms = line["lambda_angstroms"] * args.sigma_v / 299792.458
sigma_gridpoints = math.ceil(sigma_angstroms / plot_resolution)
window_left_index = max(int(centre_index - args.gaussian_window * sigma_gridpoints), 0)
window_right_index = min(int(centre_index + args.gaussian_window * sigma_gridpoints), len(xvalues))
Expand Down Expand Up @@ -205,7 +205,7 @@ def make_plot(
def add_upper_lte_pop(
dftransitions: pl.DataFrame, T_exc: float, ionpop: float, ltepartfunc: float, columnname: str | None = None
) -> pl.DataFrame:
K_B = const.k_B.to("eV / K").value
K_B = 8.617333262145179e-05 # eV / K
scalefactor = ionpop / ltepartfunc
if columnname is None:
columnname = f"upper_pop_lte_{T_exc:.0f}K"
Expand Down Expand Up @@ -445,7 +445,7 @@ def main(args: argparse.Namespace | None = None, argsraw: Sequence[str] | None =
print(f" {len(pldftransitions)} plottable transitions")

if args.atomicdatabase == "artis":
K_B = const.k_B.to("eV / K").value
K_B = 8.617333262145179e-05 # eV / K
T_exc = vardict["Te"]
ltepartfunc = (
ion["levels"].select(pl.col("g") * (-pl.col("energy_ev") / K_B / T_exc).exp()).sum().item()
Expand Down Expand Up @@ -500,7 +500,7 @@ def main(args: argparse.Namespace | None = None, argsraw: Sequence[str] | None =
T_exc = vardict[temperature]
popcolumnname = f"upper_pop_lte_{T_exc:.0f}K"
if args.atomicdatabase == "artis":
K_B = const.k_B.to("eV / K").value
K_B = 8.617333262145179e-05 # eV / K
ltepartfunc = (
ion["levels"].select(pl.col("g") * (-pl.col("energy_ev") / K_B / T_exc).exp()).sum().item()
)
Expand Down

0 comments on commit 26fbca7

Please sign in to comment.