Skip to content

Commit

Permalink
test_calibration.py passes w/o failure
Browse files Browse the repository at this point in the history
  • Loading branch information
rstrub committed Nov 29, 2023
1 parent 784d0b1 commit f318497
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 68 deletions.
10 changes: 4 additions & 6 deletions hermes_eea/SkymapFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from hermes_core import log
from hermes_eea.io import EEA
from hermes_eea.util.time import ccsds_to_cdf_time

from hermes_eea import energies as voltages

# This may eventually be put inside a multiprocessor:
def SkymapFactory(l0_cdf,energies,deflections,myEEA):
Expand All @@ -21,12 +21,10 @@ def SkymapFactory(l0_cdf,energies,deflections,myEEA):
return_package = {}
beginning_packets = np.where((l0_cdf['STEP'][stepper_table_packets[0]:]) == 0 )[0] + stepper_table_packets[0]
package = []
log.info("n_sweeps:" + str(len(beginning_packets)))

epochs = ccsds_to_cdf_time.helpConvertEEA(l0_cdf)
try:
#for ptr in range(0,len(beginning_packets)):
for ptr in range(87,200):
#skymap = np.zeros((beginning_packets[ptr+1]-beginning_packets[ptr],32))
for ptr in range(0,len(beginning_packets)):
package.append((
l0_cdf['STEP'][beginning_packets[ptr]:beginning_packets[ptr+1]],
l0_cdf['ACCUM'][beginning_packets[ptr]:beginning_packets[ptr+1]],
Expand Down Expand Up @@ -94,7 +92,7 @@ def do_eea_packet( stepperTableCounter,
return_package['µEpoch'] = µepoch
return_package['Epoch'] = epoch[0]
return_package['stats'] = np.sum(skymap)
return_package['energies'] = energies
return_package['energies'] = voltages
return_package['sun_angles'] = deflections
return_package['counter1'] = counter1
return_package['counter2'] = counter2
Expand Down
13 changes: 13 additions & 0 deletions hermes_eea/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,16 @@

skeleton = str( os.path.join(_data_directory, "masterSkeletons", "hermes_eea_l0_00000000000000_v0.0.0.cdf") )
stepper_table = "flight_stepper.txt"


energies = [2.18000000e+00, 2.63177330e+00, 3.17717004e+00, 3.83559233e+00,
4.63046306e+00, 5.59005918e+00, 6.74851766e+00, 8.14704980e+00,
9.83540739e+00, 1.18736525e+01, 1.43342944e+01, 1.73048684e+01,
2.08910507e+01, 2.52204172e+01, 3.04469818e+01, 3.67566761e+01,
4.43739626e+01, 5.35698211e+01, 6.46713874e+01, 7.80735920e+01,
9.42532085e+01, 1.13785815e+02, 1.37366271e+02, 1.65833433e+02,
2.00200000e+02, 2.39800000e+02, 3.17794829e+02, 4.21157437e+02,
5.58138682e+02, 7.39673007e+02, 9.80251281e+02, 1.29907752e+03,
1.72160182e+03, 2.28155195e+03, 3.02362557e+03, 4.00705827e+03,
5.31035195e+03, 7.03754125e+03, 9.32649800e+03, 1.23599368e+04,
1.63800000e+04]
57 changes: 57 additions & 0 deletions hermes_eea/calibration/build_spectra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

from hermes_core.timedata import HermesData
import astropy.units as astropy_units
from astropy.timeseries import TimeSeries
from astropy.time import Time
from hermes_core.timedata import HermesData
from astropy.nddata import NDData
from ndcube import NDCube, NDCollection
from hermes_eea.util.time.iso_epoch import epoch_to_iso_obj, epoch_to_eea_iso, epoch_to_iso
import numpy as np
from astropy.wcs import WCS

class Build_Hermes_EEA_Data:

def __init__(self, myEEA):
self.EEA = myEEA
self.raw_counts = astropy_units.def_unit("raw instrument counts")
def build_HermesData(self):
iso_times = Time(epoch_to_iso(self.EEA.Epoch[:]), scale='utc')
ts_1d_uQ = TimeSeries(
time=iso_times,
data={"stats": astropy_units.Quantity(self.EEA.stats, "gauss", dtype=np.uint16)}
) # this works
self._hermes_eea_spectra()
bare_attrs = HermesData.global_attribute_template("eea", "l1", "1.0.0")
ts_justTime = TimeSeries(time=iso_times)

self.hermes_eea_data = HermesData(timeseries=ts_1d_uQ, spectra=self.multiple_spectra, meta=bare_attrs)
self.hermes_eea_data.timeseries['stats'].meta.update({"CATDESC": "Sum of skymap for each sweep"})

def _hermes_eea_spectra(self):
self.multiple_spectra = NDCollection(
[
("hermes_eea_settle_step_times",
NDCube(data=np.array(self.EEA.µEpoch), wcs=WCS(naxis=2), meta={"CATDESC": "Settle for Each Step"},
unit="s", )),
("hermes_eea_energy_profile",
NDCube(data=np.array(self.EEA.EnergyLabels), wcs=WCS(naxis=2), meta={"CATDESC": "Energy Profile"},
unit="eV", )),
("hermes_eea_accum",
NDCube(data=np.array(self.EEA.ACCUM), wcs=WCS(naxis=3), meta={"CATDESC": "EEA raw skymap"},
unit="count" )),

("hermes_eea_counter1",
NDCube(data=np.array(self.EEA.Counter1), wcs=WCS(naxis=2),
meta={"CATDESC": "Estimate 1 of the number of counts in this accumulation"},
unit=astropy_units.dimensionless_unscaled, )),

("hermes_eea_counter1",
NDCube(data=np.array(self.EEA.Counter1), wcs=WCS(naxis=2),
meta={"CATDESC": "Estimate 1 of the number of counts in this accumulation"},
unit=astropy_units.dimensionless_unscaled, )),
("hermes_eea_counter2",
NDCube(data=np.array(self.EEA.Counter1), wcs=WCS(naxis=2),
meta={"CATDESC": "Estimate 1 of the number of counts in this accumulation"},
unit=astropy_units.dimensionless_unscaled, ))
])
72 changes: 10 additions & 62 deletions hermes_eea/calibration/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@
from hermes_eea.SkymapFactory import SkymapFactory
from hermes_eea.Global_Attrs_Cnts import Global_Attrs
from hermes_eea.util.time.iso_epoch import epoch_to_iso_obj, epoch_to_eea_iso, epoch_to_iso
import astropy.units as astropy_units
from astropy.timeseries import TimeSeries, BinnedTimeSeries
from astropy.time import Time
from hermes_core.timedata import HermesData
from astropy.nddata import NDData
from ndcube import NDCube, NDCollection
from astropy.wcs import WCS


from hermes_eea.calibration.build_spectra import Build_Hermes_EEA_Data

__all__ = [
"process_file",
Expand Down Expand Up @@ -245,67 +241,19 @@ def l0_sci_data_to_cdf(data: dict, original_filename: Path) -> Path:
SkymapFactory(data, calib.energies, calib.deflections, myEEA)
most_active = np.where(np.array(myEEA.stats) > 150)
example_start_times = epoch_to_iso_obj(myEEA.Epoch[0:10])
iso_times = Time(epoch_to_iso(myEEA.Epoch[:]), scale='utc')
myrange = [myEEA.Epoch[0], myEEA.Epoch[-1]]
range_of_packet = epoch_to_iso(myrange)
log.info("Range of file:" + range_of_packet[0] + " to " + range_of_packet[1])

n_packets = len(myEEA.Epoch)
#outputFile = eea_cdf.writeCDF(glblattr, myEEA, range, n_packets, srvy='normal')
#ts = TimeSeries(
# time=times,
# data={'diff_e_flux': u.Quantity(myEAA.stats,
# '1/(cm**2 * s * eV * steradian)',
# dtype=np.uint16)}
# )
energyLabels = NDData( data=np.array(myEEA.EnergyLabels))
counterA = NDData( data=np.array(myEEA.Counter1))
counterB = NDData( data=np.array(myEEA.Counter2))
accumSkymaps = NDData( data=np.array(myEEA.ACCUM))
epochs = NDData( data=np.array(myEEA.Epoch))
stats = NDData( data=np.array(myEEA.stats))

hard_coded_attrs= retrieve_canned_attributes()
bare_attrs = HermesData.global_attribute_template("eea", "l1", "1.0.0")
ts_justTime = TimeSeries( time=iso_times )
#'stats': u.Quantity(np.array(myEEA.stats), 'cm', dtype=np.uint16),
support_data = {
'energies' : energyLabels,
'pulse_a' : counterA,
'pulse_b' : counterB,
'ACCUM' : accumSkymaps
}
multiple_spectra = NDCollection(
[
( "example_2d_spectra",
NDCube( data = np.array(myEEA.EnergyLabels), wcs = WCS(naxis=2), meta = {"CATDESC": "Example 2D Spectra Variable"},
unit = "eV", )),
("example_3d_spectra",
NDCube(data=np.array(myEEA.ACCUM), wcs=WCS(naxis=3), meta={"CATDESC": "Example 3D Spectra Variable"},
unit="eV", ))
])

ts_1d_uQ = TimeSeries(
time=iso_times,
data= {"Bx": astropy_units.Quantity(myEEA.stats, "gauss", dtype=np.uint16) }
) # this works
ts_2d_uQ = TimeSeries(
time=iso_times,
data= {"Bx": astropy_units.Quantity(myEEA.EnergyLabels, "gauss", dtype=np.uint16) }
) # this works
hermes_eea_data = HermesData(timeseries=ts_1d_uQ, spectra=multiple_spectra, meta=bare_attrs)


# This seems to work to add it after the facthhhhhhhhh
hermes_eea_data.timeseries['Bx'].meta.update({"CATDESC": "X component of the Magnetic field measured by HERMES"})

hermes_eea_factory = Build_Hermes_EEA_Data(myEEA)
hermes_eea_factory.build_HermesData()

try:
# my all-in-one-rec-version
cdf_path = hermes_eea_data.save( str(cdf_filename.parent) , True)
cdf_path = hermes_eea_factory.hermes_eea_data.save( str(cdf_filename.parent) , True)
except Exception as e:
log.error(e)
sys.exit(2)


return cdf_filename
return cdf_path


def get_calibration_file(data_filename: Path, time=None) -> Path:
Expand Down

0 comments on commit f318497

Please sign in to comment.