Skip to content

Commit

Permalink
add from hdf method to spectrum class
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik11135 committed Feb 26, 2025
1 parent e0297c6 commit e439d41
Show file tree
Hide file tree
Showing 4 changed files with 649 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ Josh Shields <shields.joshua.v@gmail.com>
Karan Desai <karandesai_96@live.com>
Karan Desai <karandesai_96@live.com> karandesai-96 <karandesai_96@live.com>

Karthik Rishinarada <karthikrk11135@gmail.com>

Kaushik Varanasi <kaushik.varanasi1@gmail.com>
Kaushik Varanasi <kaushik.varanasi1@gmail.com> kaushik94 <kaushik.varanasi1@gmail.com>

Expand Down
662 changes: 624 additions & 38 deletions docs/physics/spectrum/basic.ipynb

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions tardis/spectrum/spectrum.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import warnings

import numpy as np
import pandas as pd
from astropy import units as u

from tardis.io.util import HDFWriterMixin
Expand Down Expand Up @@ -151,6 +152,20 @@ def plot(self, ax=None, mode="wavelength", **kwargs):
else:
warnings.warn(f"Did not find plotting mode {mode}, doing nothing.")

def from_hdf_to_class(self,hdf_path):
try:
with pd.HDFStore(hdf_path, mode="r") as store:
_freq = store['/tardis_spectrum/_frequency']
_f = np.array(_freq) * u.Hz

_lumi = store['/tardis_spectrum/luminosity']
_l = np.array(_lumi) * (u.erg/u.s)

return TARDISSpectrum(_f, _l)

except FileNotFoundError as e:
print(f"File not found: {e}")

def to_ascii(self, fname, mode="luminosity_density"):
if mode == "luminosity_density":
np.savetxt(
Expand Down
8 changes: 8 additions & 0 deletions tardis/spectrum/tests/test_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ def test_f_nu_to_f_lambda(spectrum):
spectrum.luminosity_density_lambda.value, expected.value
)


def test_from_hdf_to_class(spectrum):
actual = TARDISSpectrum(spectrum._frequency, spectrum.luminosity)
actual.to_hdf("test.hdf", overwrite=True)
expected = actual.from_hdf_to_class("test.hdf")

compare_spectra(actual, expected)


###
# Save and Load
Expand Down

0 comments on commit e439d41

Please sign in to comment.