-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move some things into a utility file, in particular time convertion and dose computation. Of course, that means a huge amount of churn and deduplication, but yay tests.
- Loading branch information
Showing
12 changed files
with
132 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
Some stuff found to be useful in various scripts, and should thus be hoisted into a | ||
utility library, rather than being imported from and between scripts. | ||
""" | ||
|
||
from datetime import datetime | ||
from typing import Union, List, Any | ||
|
||
Number = Union[int, float] | ||
|
||
# The spectrogram format uses FileTime, the number of 100ns intervals since the | ||
# beginning of 1600 CE. On a linux/unix/bsd host, we get the number of (fractional) | ||
# seconds since the beginning of 1970 CE. Here are some conversions, which, If I use | ||
# them one more time are getting moved into a utility file... | ||
|
||
_filetime_quantum = 1e-7 | ||
_filetime_epoch_offset = 116444736000000000 | ||
|
||
|
||
def FileTime2UnixTime(x: Number) -> float: | ||
"Convert a FileTime to Unix timestamp" | ||
return (float(x) - _filetime_epoch_offset) * _filetime_quantum | ||
|
||
|
||
def FileTime2DateTime(x: Number) -> datetime: | ||
"Convert a FileTime to Python DateTime" | ||
return datetime.fromtimestamp(FileTime2UnixTime(x)) | ||
|
||
|
||
def UnixTime2FileTime(x: Number) -> int: | ||
"Convert a Unix timestamp to FileTime" | ||
return int(float(x) / _filetime_quantum + _filetime_epoch_offset) | ||
|
||
|
||
def DateTime2FileTime(dt: datetime) -> int: | ||
"Convert a Python DateTime to FileTime" | ||
return UnixTime2FileTime(dt.timestamp()) | ||
|
||
|
||
def stringify(a: List[Any], c: str = " ") -> str: | ||
"Make a string out of a list of things, nicer than str(list(...))" | ||
return c.join([f"{x}" for x in a]) | ||
|
||
|
||
def get_dose_from_spectrum( | ||
counts: List[int], | ||
a0: float = 0, | ||
a1: float = 3000 / 1024, | ||
a2: float = 0, | ||
d: float = 4.51, | ||
v: float = 1.0, | ||
) -> float: | ||
""" | ||
A somewhat generic function to estimate the energy represented by a spectrum. | ||
counts: a list of counts per channel | ||
a0-a2 are the calibration coefficients for the instrument | ||
d: density in g/cm^3 of the scintillator crystal, approximately 4.51 for CsI:Tl | ||
v: volume of the scintillator crystal, radiacode is 1cm^3 | ||
""" | ||
|
||
joules_per_keV = 1.60218e-16 | ||
mass = d * v * 1e-3 # kg | ||
|
||
def _chan2kev(c): | ||
return a0 + a1 * c + a2 * c**2 | ||
|
||
total_keV = sum([_chan2kev(ch) * n for ch, n in enumerate(counts)]) | ||
gray = total_keV * joules_per_keV / mass | ||
uSv = gray * 1e6 | ||
return uSv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env python3 | ||
# coding: utf-8 | ||
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 syn=python | ||
# SPDX-License-Identifier: MIT | ||
|
||
import datetime | ||
import unittest | ||
import rcutils | ||
|
||
|
||
class TestRadiaCodeUtils(unittest.TestCase): | ||
unix_time = datetime.datetime(2023, 12, 1, 0, 16, 11) | ||
file_time = 133458921710000000 | ||
|
||
def test_datetime_to_filetime(self): | ||
self.assertEqual(rcutils.DateTime2FileTime(self.unix_time), self.file_time) | ||
|
||
def test_filetime_to_datetime(self): | ||
self.assertEqual(rcutils.FileTime2DateTime(self.file_time), self.unix_time) | ||
|
||
def test_spectrum_dose(self): | ||
a = [-10, 3.0, 0] | ||
c = [100] * 1024 | ||
usv = rcutils.get_dose_from_spectrum(c, *a) | ||
self.assertAlmostEqual(usv, 5.5458, delta=1e-3) | ||
|
||
def test_stringify(self): | ||
testcases = [([], ""), ([0], "0"), ([0, 1, 2, 3], "0 1 2 3")] | ||
|
||
for t in testcases: | ||
self.assertEqual(rcutils.stringify(t[0]), t[1]) | ||
self.assertEqual(rcutils.stringify(t[0], ","), t[1].replace(" ", ",")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters