Skip to content

Commit

Permalink
remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
ckuethe committed Dec 22, 2023
1 parent 60134e4 commit bdc7c3d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 26 deletions.
25 changes: 5 additions & 20 deletions src/spectrogram_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import datetime
from collections import namedtuple
from binascii import unhexlify
from rcutils import get_dose_from_spectrum, FileTime2DateTime
import struct
import sys
import os
Expand All @@ -29,14 +30,6 @@ def get_args() -> Namespace:
return rv


def FileTime2DateTime(x):
"""Convert a Microsoft FileTime into a datetime.datetime"""
epoch_offset_jiffies = 116444736000000000
timescale = 10000000

return datetime.datetime.fromtimestamp((float(x) - epoch_offset_jiffies) / timescale)


def parse_header(s: str) -> SGHeader:
"""Parse the header line into some useful properties"""
rv = dict([w.split(": ") for w in s.strip("\n").split("\t")])
Expand Down Expand Up @@ -64,14 +57,6 @@ def extract_calibration_from_spectrum(s: str) -> EnergyCalibration:
return EnergyCalibration(*tmp[1:])


def keV_to_uSv(keV: float) -> float:
"""handwavy conversion of kiloelectronvolts to microsieverts, assuming full dose absorption and Gray ~= Sievert"""
kev2j = 1.60218e-16
mass = 4.51e-3 # kg, CsI:Tl density is 4.51g/cm^3, crystal is 1cm^3
gray = keV * kev2j / mass
return gray * 1e6


def load_spectrogram(fn: str) -> SpecEnergy:
"""Open a spectrogram"""
cal: EnergyCalibration = None
Expand All @@ -91,11 +76,11 @@ def load_spectrogram(fn: str) -> SpecEnergy:
counts = [int(c) for c in counts]
if len(counts) < header.channels: # pad to the right number of channels
counts.extend([0] * (header.channels - len(counts)))
keVz = sum([n * (cal.a0 + c * cal.a1 + c * c * cal.a2) for c, n in enumerate(counts)])
peak_dose_rate = max(peak_dose_rate, keVz / acc_time)
total_energy += keVz
dose = get_dose_from_spectrum(counts, *cal)
peak_dose_rate = max(peak_dose_rate, dose / acc_time)
total_energy += dose

return SpecEnergy(keV_to_uSv(total_energy), header.duration, keV_to_uSv(peak_dose_rate))
return SpecEnergy(total_energy, header.duration, peak_dose_rate)


def main() -> None:
Expand Down
6 changes: 0 additions & 6 deletions tests/test_spectrogram_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ def test_extract_calibration(self):
self.assertAlmostEqual(ec.a1, 2.3659, delta=0.001)
self.assertAlmostEqual(ec.a2, 4.4e-5, delta=0.001)

def test_kev_to_gray(self):
e = spectrogram_energy.keV_to_uSv(0)
self.assertEqual(e, 0)
e = spectrogram_energy.keV_to_uSv(1e12)
self.assertAlmostEqual(e, 35525, delta=0.1)

def test_parse_header(self):
parsed = spectrogram_energy.parse_header("\t".join(self.header_fields))
self.assertEqual(parsed.name, "unittest")
Expand Down

0 comments on commit bdc7c3d

Please sign in to comment.