obdeect-stream 0.8.0__tar.gz → 0.9.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/CMakeLists.txt +4 -4
  2. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/PKG-INFO +3 -3
  3. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/README.md +2 -2
  4. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/bench/bench_stream.cpp +1 -1
  5. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/bindings/core.cpp +38 -3
  6. obdeect_stream-0.9.2/cpp/include/obdeect/stream/atmprof.hpp +76 -0
  7. obdeect_stream-0.9.2/cpp/include/obdeect/stream/cli.hpp +116 -0
  8. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/include/obdeect/stream/constants.hpp +3 -0
  9. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/include/obdeect/stream/corsika.hpp +41 -22
  10. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/include/obdeect/stream/eventio.hpp +2 -0
  11. obdeect_stream-0.9.2/cpp/include/obdeect/stream/parse.hpp +57 -0
  12. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/include/obdeect/stream/simtel.hpp +76 -97
  13. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/include/obdeect/stream/stream.hpp +29 -2
  14. obdeect_stream-0.9.2/cpp/include/obdeect/stream/telpos.hpp +15 -0
  15. obdeect_stream-0.9.2/cpp/include/obdeect/stream/vec3.hpp +64 -0
  16. obdeect_stream-0.9.2/cpp/include/obdeect/stream/wavelength.hpp +15 -0
  17. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/data/conformance/expected.json +5 -5
  18. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/data/conformance/make_conformance.py +11 -11
  19. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/test_capi.c +69 -46
  20. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/test_capi_cpp.cpp +5 -5
  21. obdeect_stream-0.9.2/cpp/tests/test_cli.cpp +81 -0
  22. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/test_corsika.cpp +49 -10
  23. obdeect_stream-0.9.2/cpp/tests/test_parse.cpp +41 -0
  24. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/test_simtel.cpp +149 -43
  25. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/test_stream.cpp +66 -0
  26. obdeect_stream-0.9.2/cpp/tests/test_wavelength.cpp +49 -0
  27. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tools/corsika2ps.cpp +19 -30
  28. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tools/ps2grisu.cpp +32 -60
  29. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tools/ps2simtel.cpp +32 -49
  30. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tools/ps_cat.cpp +22 -28
  31. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tools/ps_tee.cpp +20 -23
  32. obdeect_stream-0.9.2/cpp/tools/tool_version.hpp +16 -0
  33. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/pyproject.toml +3 -1
  34. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/obdeect/stream/cli.py +17 -2
  35. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/obdeect/stream/convert.py +13 -5
  36. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/obdeect/stream/stage.py +10 -7
  37. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/obdeect/stream/stream.py +19 -2
  38. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/tests/test_cli.py +19 -1
  39. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/tests/test_convert.py +101 -30
  40. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/tests/test_grisu.py +5 -5
  41. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/tests/test_stage.py +61 -18
  42. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/tests/test_stream.py +48 -19
  43. obdeect_stream-0.8.0/cpp/tools/tool_version.hpp +0 -17
  44. obdeect_stream-0.8.0/python/tests/test_validation_script.py +0 -37
  45. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cmake/obdeect_stream-config.cmake.in +0 -0
  46. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cmake/version.hpp.in +0 -0
  47. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/bench/gen_corsika.cpp +0 -0
  48. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/capi/obdeect_stream_c.cpp +0 -0
  49. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/include/obdeect/stream/format.hpp +0 -0
  50. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/include/obdeect/stream/grisu.hpp +0 -0
  51. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/include/obdeect/stream/obdeect_stream.h +0 -0
  52. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/include/obdeect/stream/photons.hpp +0 -0
  53. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/include/obdeect/stream/random.hpp +0 -0
  54. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/include/obdeect/stream/stages.hpp +0 -0
  55. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/include/obdeect/stream/stdin_buffer.hpp +0 -0
  56. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/check.hpp +0 -0
  57. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/consumer/CMakeLists.txt +0 -0
  58. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/consumer/main.cpp +0 -0
  59. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/consumer/main_c.c +0 -0
  60. /obdeect_stream-0.8.0/cpp/tests/data/conformance/bad_magic.ps → /obdeect_stream-0.9.2/cpp/tests/data/conformance/bad_magic.pst +0 -0
  61. /obdeect_stream-0.8.0/cpp/tests/data/conformance/too_many_rows.ps → /obdeect_stream-0.9.2/cpp/tests/data/conformance/too_many_rows.pst +0 -0
  62. /obdeect_stream-0.8.0/cpp/tests/data/conformance/truncated_chunk.ps → /obdeect_stream-0.9.2/cpp/tests/data/conformance/truncated_chunk.pst +0 -0
  63. /obdeect_stream-0.8.0/cpp/tests/data/conformance/unknown_major.ps → /obdeect_stream-0.9.2/cpp/tests/data/conformance/unknown_major.pst +0 -0
  64. /obdeect_stream-0.8.0/cpp/tests/data/conformance/valid.ps → /obdeect_stream-0.9.2/cpp/tests/data/conformance/valid.pst +0 -0
  65. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/test_eventio.cpp +0 -0
  66. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/test_format.cpp +0 -0
  67. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/test_grisu.cpp +0 -0
  68. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/test_stages.cpp +0 -0
  69. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/cpp/tests/test_version.cpp +0 -0
  70. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/obdeect/stream/__init__.py +0 -0
  71. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/obdeect/stream/__main__.py +0 -0
  72. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/obdeect/stream/_package.py +0 -0
  73. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/tests/test_cmake_package.py +0 -0
  74. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/tests/test_constants.py +0 -0
  75. {obdeect_stream-0.8.0 → obdeect_stream-0.9.2}/python/tests/test_import.py +0 -0
@@ -161,7 +161,7 @@ if(OBDEECT_STREAM_BUILD_TESTING)
161
161
  enable_testing()
162
162
  if(OBDEECT_STREAM_BUILD_BENCH)
163
163
  add_test(NAME bench_stream_partial_chunk COMMAND bench_stream 65537
164
- ${CMAKE_CURRENT_BINARY_DIR}/bench_stream_test.ps)
164
+ ${CMAKE_CURRENT_BINARY_DIR}/bench_stream_test.pst)
165
165
  endif()
166
166
  if(OBDEECT_STREAM_BUILD_CAPI)
167
167
  enable_language(C)
@@ -172,17 +172,17 @@ if(OBDEECT_STREAM_BUILD_TESTING)
172
172
  target_link_libraries(test_capi PRIVATE obdeect_stream_c)
173
173
  target_compile_options(test_capi PRIVATE -Wall -Wextra -Wpedantic -Werror)
174
174
  set(OBDEECT_STREAM_CONFORMANCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cpp/tests/data/conformance)
175
- add_test(NAME capi COMMAND test_capi ${CMAKE_CURRENT_BINARY_DIR}/capi_roundtrip.ps
175
+ add_test(NAME capi COMMAND test_capi ${CMAKE_CURRENT_BINARY_DIR}/capi_roundtrip.pst
176
176
  ${OBDEECT_STREAM_CONFORMANCE_DIR})
177
177
  set_tests_properties(capi PROPERTIES FIXTURES_SETUP capi_stream)
178
178
  add_executable(test_capi_cpp cpp/tests/test_capi_cpp.cpp)
179
179
  target_link_libraries(test_capi_cpp PRIVATE obdeect_stream_c obdeect_stream_core)
180
180
  obdeect_stream_enable_warnings(test_capi_cpp)
181
- add_test(NAME capi_cpp COMMAND test_capi_cpp ${CMAKE_CURRENT_BINARY_DIR}/capi_roundtrip.ps
181
+ add_test(NAME capi_cpp COMMAND test_capi_cpp ${CMAKE_CURRENT_BINARY_DIR}/capi_roundtrip.pst
182
182
  ${OBDEECT_STREAM_CONFORMANCE_DIR})
183
183
  set_tests_properties(capi_cpp PROPERTIES FIXTURES_REQUIRED capi_stream)
184
184
  endif()
185
- foreach(test_name version format stream eventio corsika simtel grisu stages)
185
+ foreach(test_name version format parse cli stream eventio corsika simtel grisu stages wavelength)
186
186
  add_executable(test_${test_name} cpp/tests/test_${test_name}.cpp)
187
187
  target_link_libraries(test_${test_name} PRIVATE obdeect_stream_core)
188
188
  obdeect_stream_enable_warnings(test_${test_name})
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: obdeect-stream
3
- Version: 0.8.0
3
+ Version: 0.9.2
4
4
  Summary: Photon-stream format and I/O for Cherenkov-telescope simulation chains
5
5
  Author: obdeect contributors
6
6
  License-Expression: BSD-3-Clause
@@ -45,8 +45,8 @@ Requires Python 3.14 or higher.
45
45
  obdeect stream cat [FILE ...] # concatenate streams; --summary for record counts
46
46
  obdeect stream tee FILE # copy a stream passing through
47
47
  obdeect stream from-corsika < run.iact # CORSIKA IACT EventIO to photon stream
48
- obdeect stream to-simtel --camera-map cam.txt run.ps > sim.simtel # photon stream to sim_telarray input
49
- obdeect stream to-grisu --telescope-config TLLOC.dat run.ps > run.grisu # GrISU photon text
48
+ obdeect stream to-simtel --camera-map cam.txt run.pst > sim.simtel # photon stream to sim_telarray input
49
+ obdeect stream to-grisu --telescope-config TLLOC.dat run.pst > run.grisu # GrISU photon text
50
50
  ```
51
51
 
52
52
  Full documentation and the repository README: https://github.com/gammasim/obdeect
@@ -18,8 +18,8 @@ Requires Python 3.14 or higher.
18
18
  obdeect stream cat [FILE ...] # concatenate streams; --summary for record counts
19
19
  obdeect stream tee FILE # copy a stream passing through
20
20
  obdeect stream from-corsika < run.iact # CORSIKA IACT EventIO to photon stream
21
- obdeect stream to-simtel --camera-map cam.txt run.ps > sim.simtel # photon stream to sim_telarray input
22
- obdeect stream to-grisu --telescope-config TLLOC.dat run.ps > run.grisu # GrISU photon text
21
+ obdeect stream to-simtel --camera-map cam.txt run.pst > sim.simtel # photon stream to sim_telarray input
22
+ obdeect stream to-grisu --telescope-config TLLOC.dat run.pst > run.grisu # GrISU photon text
23
23
  ```
24
24
 
25
25
  Full documentation and the repository README: https://github.com/gammasim/obdeect
@@ -28,7 +28,7 @@ static int run(int argc, char **argv) {
28
28
  const std::uint64_t photons = argc > 1 ? std::strtoull(argv[1], nullptr, 10) : 100000000ULL;
29
29
  const std::filesystem::path path =
30
30
  argc > 2 ? std::filesystem::path(argv[2])
31
- : std::filesystem::temp_directory_path() / "obdeect_stream_bench.ps";
31
+ : std::filesystem::temp_directory_path() / "obdeect_stream_bench.pst";
32
32
  const std::uint64_t n_chunks = (photons + chunk_rows - 1) / chunk_rows;
33
33
 
34
34
  obdeect::stream::PhotonChunk chunk;
@@ -2,6 +2,7 @@
2
2
  // _POSIX_C_SOURCE), so the nanobind headers come first.
3
3
  #include <nanobind/nanobind.h>
4
4
  #include <nanobind/ndarray.h>
5
+ #include <nanobind/stl/optional.h>
5
6
  #include <nanobind/stl/string.h>
6
7
  #include <nanobind/stl/tuple.h>
7
8
  #include <nanobind/stl/vector.h>
@@ -103,7 +104,8 @@ obdeect::stream::RecordType metadata_type_from_name(const std::string &name) {
103
104
 
104
105
  class PyReader {
105
106
  public:
106
- explicit PyReader(const std::string &path) {
107
+ PyReader(const std::string &path,
108
+ const std::optional<std::vector<std::uint32_t>> &photon_telescopes) {
107
109
  if (path == "-") {
108
110
  input_ = &std::cin;
109
111
  } else {
@@ -114,6 +116,9 @@ public:
114
116
  input_ = file_.get();
115
117
  }
116
118
  reader_ = std::make_unique<obdeect::stream::Reader>(*input_);
119
+ if (photon_telescopes) {
120
+ reader_->keep_photons_of(*photon_telescopes);
121
+ }
117
122
  }
118
123
 
119
124
  nb::dict next() {
@@ -177,11 +182,13 @@ public:
177
182
  }
178
183
 
179
184
  void write(const std::string &record_type, const nb::dict &metadata) {
185
+ ensure_open();
180
186
  writer_->write_metadata(metadata_type_from_name(record_type), dict_to_metadata(metadata));
181
187
  }
182
188
 
183
189
  void write_photons(std::uint32_t telescope_id, std::uint32_t frame, std::uint64_t first_index,
184
190
  const nb::dict &columns) {
191
+ ensure_open();
185
192
  using Array = nb::ndarray<const float, nb::ndim<1>, nb::c_contig, nb::device::cpu>;
186
193
  obdeect::stream::PhotonChunk chunk;
187
194
  if (frame > static_cast<std::uint32_t>(obdeect::stream::Frame::camera)) {
@@ -221,12 +228,20 @@ public:
221
228
  }
222
229
 
223
230
  void write_blob(std::uint32_t eventio_type, std::uint32_t eventio_id, const nb::bytes &data) {
231
+ ensure_open();
224
232
  const auto *begin = reinterpret_cast<const std::uint8_t *>(data.c_str());
225
233
  writer_->write_blob(eventio_type, eventio_id,
226
234
  std::vector<std::uint8_t>(begin, begin + data.size()));
227
235
  }
228
236
 
229
237
  void close() {
238
+ if (aborted_) {
239
+ throw std::runtime_error("obdeect stream: writer is closed");
240
+ }
241
+ if (closed_) {
242
+ return;
243
+ }
244
+ ensure_open();
230
245
  writer_->finish();
231
246
  if (file_->is_open()) {
232
247
  file_->close();
@@ -235,9 +250,27 @@ public:
235
250
  "obdeect stream: writing the photon stream failed (e.g. disk full)");
236
251
  }
237
252
  }
253
+ closed_ = true;
254
+ }
255
+
256
+ // Closes the file without writing stream_end, so a failed run stays an invalid stream.
257
+ void abort() {
258
+ aborted_ = true;
259
+ closed_ = true;
260
+ if (file_->is_open()) {
261
+ file_->close();
262
+ }
238
263
  }
239
264
 
240
265
  private:
266
+ void ensure_open() const {
267
+ if (aborted_ || !file_->is_open()) {
268
+ throw std::runtime_error("obdeect stream: writer is closed");
269
+ }
270
+ }
271
+
272
+ bool aborted_{false};
273
+ bool closed_{false};
241
274
  std::unique_ptr<std::ofstream> file_;
242
275
  std::unique_ptr<obdeect::stream::Writer> writer_;
243
276
  };
@@ -393,7 +426,8 @@ NB_MODULE(_core, module) {
393
426
  nb::arg("text"));
394
427
 
395
428
  nb::class_<PyReader>(module, "Reader")
396
- .def(nb::init<const std::string &>(), nb::arg("path"))
429
+ .def(nb::init<const std::string &, const std::optional<std::vector<std::uint32_t>> &>(),
430
+ nb::arg("path"), nb::arg("photon_telescopes") = nb::none())
397
431
  .def("__iter__", [](nb::object self) { return self; })
398
432
  .def("__next__", &PyReader::next);
399
433
 
@@ -405,5 +439,6 @@ NB_MODULE(_core, module) {
405
439
  nb::arg("first_index"), nb::arg("columns"))
406
440
  .def("write_blob", &PyWriter::write_blob, nb::arg("eventio_type"), nb::arg("eventio_id"),
407
441
  nb::arg("data"))
408
- .def("close", &PyWriter::close);
442
+ .def("close", &PyWriter::close)
443
+ .def("abort", &PyWriter::abort);
409
444
  }
@@ -0,0 +1,76 @@
1
+ #pragma once
2
+
3
+ // Reader of CORSIKA/sim_telarray atmospheric tables (atmprof format), shared by the stream
4
+ // adapters and propagation. Header-only, standard library only.
5
+
6
+ #include "obdeect/stream/eventio.hpp"
7
+ #include "obdeect/stream/parse.hpp"
8
+
9
+ #include <array>
10
+ #include <cstddef>
11
+ #include <cstdint>
12
+ #include <istream>
13
+ #include <sstream>
14
+ #include <stdexcept>
15
+ #include <string>
16
+
17
+ namespace obdeect::stream {
18
+
19
+ // Profile number of the custom table (CORSIKA ATMOFILE).
20
+ inline constexpr std::uint32_t custom_profile_id = 99;
21
+
22
+ // Parses an atmospheric table as CORSIKA and sim_telarray read it:
23
+ // '#' starts a comment, blank lines are skipped, and the first four columns are altitude
24
+ // [km a.s.l.], density [g/cm^3], thickness [g/cm^2] and n-1. Further columns are ignored.
25
+ // Altitudes must ascend; 2 to 120 levels. The profile number is
26
+ // custom_profile_id; the observation level is set by the caller. Throws
27
+ // std::invalid_argument with the line number.
28
+ inline AtmosphereTable read_atmprof_text(std::istream &in, const std::string &file_name) {
29
+ AtmosphereTable table;
30
+ table.profile_id = custom_profile_id;
31
+ table.file_name = file_name;
32
+ std::string line;
33
+ std::size_t number = 0;
34
+ const auto fail = [&number](const std::string &what) {
35
+ return std::invalid_argument("atmospheric table line " + std::to_string(number) + ": " + what);
36
+ };
37
+ while (std::getline(in, line)) {
38
+ ++number;
39
+ const auto first = line.find_first_not_of(" \t\r");
40
+ if (first == std::string::npos || line[first] == '#') {
41
+ continue;
42
+ }
43
+ std::istringstream fields(line);
44
+ std::array<std::string, 4> tokens;
45
+ for (std::string &token : tokens) {
46
+ if (!(fields >> token)) {
47
+ throw fail("expected 4 columns: altitude_km density thickness n_minus_1");
48
+ }
49
+ }
50
+ std::array<double, 4> v{};
51
+ for (std::size_t k = 0; k < v.size(); ++k) {
52
+ if (!parse::finite_number(tokens[k], v[k])) {
53
+ throw fail("not a finite number: " + tokens[k]);
54
+ }
55
+ }
56
+ if (v[1] < 0.0 || v[2] < 0.0) {
57
+ throw fail("density and thickness must not be negative");
58
+ }
59
+ if (!table.alt_km.empty() && !(v[0] > table.alt_km.back())) {
60
+ throw fail("altitudes must ascend");
61
+ }
62
+ table.alt_km.push_back(v[0]);
63
+ table.rho.push_back(v[1]);
64
+ table.thick.push_back(v[2]);
65
+ table.refidx_m1.push_back(v[3]);
66
+ }
67
+ if (table.alt_km.size() < 2) {
68
+ throw std::invalid_argument("atmospheric table needs at least 2 levels");
69
+ }
70
+ if (table.alt_km.size() > 120) {
71
+ throw std::invalid_argument("atmospheric table has more than 120 levels");
72
+ }
73
+ return table;
74
+ }
75
+
76
+ } // namespace obdeect::stream
@@ -0,0 +1,116 @@
1
+ #pragma once
2
+
3
+ // Command-line scaffolding shared by every native tool: argument cursor, option parsing built on
4
+ // parse.hpp, `--version`, and one exit-status rule.
5
+ //
6
+ // 0 success (also --help and --version)
7
+ // 1 runtime failure (unreadable input, bad data, write error)
8
+ // 2 usage error (UsageError)
9
+ //
10
+ // Messages go to stderr as `<tool>: <message>`; a usage error is followed by the tool's usage text.
11
+
12
+ #include "obdeect/stream/format.hpp"
13
+ #include "obdeect/stream/parse.hpp"
14
+ #include "obdeect/stream/version.hpp"
15
+
16
+ #include <cstddef>
17
+ #include <cstdint>
18
+ #include <cstdio>
19
+ #include <exception>
20
+ #include <functional>
21
+ #include <iostream>
22
+ #include <limits>
23
+ #include <stdexcept>
24
+ #include <string>
25
+ #include <vector>
26
+
27
+ namespace obdeect::stream::cli {
28
+
29
+ // A bad command line. Library errors (std::invalid_argument and others) are runtime failures.
30
+ struct UsageError : std::invalid_argument {
31
+ using std::invalid_argument::invalid_argument;
32
+ };
33
+
34
+ // Walks argv; value() takes the argument after an option or fails with `<arg>: missing value`.
35
+ class Args {
36
+ public:
37
+ Args(int argc, char **argv) {
38
+ for (int i = 1; i < argc; ++i)
39
+ args_.emplace_back(argv[i]);
40
+ }
41
+ bool next(std::string &arg) {
42
+ if (i_ >= args_.size())
43
+ return false;
44
+ arg = args_[i_++];
45
+ return true;
46
+ }
47
+ std::string value(const std::string &arg) {
48
+ if (i_ >= args_.size())
49
+ throw UsageError(arg + ": missing value");
50
+ return args_[i_++];
51
+ }
52
+
53
+ private:
54
+ std::vector<std::string> args_;
55
+ std::size_t i_{0};
56
+ };
57
+
58
+ // A finite real number; the whole text must be a number.
59
+ inline double number(const std::string &text, const std::string &option) {
60
+ double value = 0.0;
61
+ if (!parse::finite_number(text, value))
62
+ throw UsageError(option + ": not a finite number: " + text);
63
+ return value;
64
+ }
65
+
66
+ // A decimal integer filling the whole text and lying in lo..hi.
67
+ inline long long integer(const std::string &text, const std::string &option,
68
+ long long lo = std::numeric_limits<long long>::min(),
69
+ long long hi = std::numeric_limits<long long>::max()) {
70
+ long long value = 0;
71
+ if (!parse::integer(text, value))
72
+ throw UsageError(option + ": not an integer: " + text);
73
+ if (value < lo || value > hi)
74
+ throw UsageError(option + ": integer out of range: " + text);
75
+ return value;
76
+ }
77
+
78
+ // The package version as "major.minor.patch" (sources and propagation have no version of their
79
+ // own).
80
+ inline std::string core_version() {
81
+ return std::to_string(version_major) + "." + std::to_string(version_minor) + "." +
82
+ std::to_string(version_patch);
83
+ }
84
+
85
+ // "<tool> <package version> (photon-stream format <major>.<minor>)".
86
+ inline std::string version_line(const std::string &tool, const std::string &version) {
87
+ return tool + " " + version + " (photon-stream format " + std::to_string(format_major) + "." +
88
+ std::to_string(format_minor) + ")\n";
89
+ }
90
+
91
+ // Runs a tool body. `--version` anywhere on the command line prints the version line and returns
92
+ // 0 before the body runs. A usage error prints `<tool>: <message>`, then `usage` (may be empty),
93
+ // and returns 2; any other exception prints `<tool>: <message>` and returns 1.
94
+ template <class Body>
95
+ int run_tool(const std::string &tool, const std::string &version, int argc, char **argv,
96
+ const std::function<void()> &usage, Body body) {
97
+ for (int i = 1; i < argc; ++i) {
98
+ if (std::string(argv[i]) == "--version") {
99
+ std::fputs(version_line(tool, version).c_str(), stdout);
100
+ return 0;
101
+ }
102
+ }
103
+ try {
104
+ return body();
105
+ } catch (const UsageError &error) {
106
+ std::cerr << tool << ": " << error.what() << '\n';
107
+ if (usage)
108
+ usage();
109
+ return 2;
110
+ } catch (const std::exception &error) {
111
+ std::cerr << tool << ": " << error.what() << '\n';
112
+ return 1;
113
+ }
114
+ }
115
+
116
+ } // namespace obdeect::stream::cli
@@ -12,6 +12,9 @@ inline constexpr double two_pi = 2.0 * std::numbers::pi;
12
12
  // Speed of light in vacuum [m/ns].
13
13
  inline constexpr double speed_of_light_m_per_ns = 0.299792458;
14
14
 
15
+ // Earth radius used by CORSIKA's CURVED option (6371.315 km) [m].
16
+ inline constexpr double earth_radius_m = 6371315.0;
17
+
15
18
  constexpr double deg_to_rad(double degrees) { return degrees * pi / 180.0; }
16
19
  constexpr double rad_to_deg(double radians) { return radians * 180.0 / pi; }
17
20
 
@@ -15,6 +15,7 @@
15
15
  #include <deque>
16
16
  #include <functional>
17
17
  #include <istream>
18
+ #include <map>
18
19
  #include <optional>
19
20
  #include <ostream>
20
21
  #include <stdexcept>
@@ -36,13 +37,15 @@ inline constexpr std::size_t runh_slope = 15; // word 16
36
37
  inline constexpr std::size_t runh_energy_min = 16; // word 17 [GeV]
37
38
  inline constexpr std::size_t runh_energy_max = 17; // word 18 [GeV]
38
39
  // EVTH
39
- inline constexpr std::size_t evth_event_number = 1; // word 2
40
- inline constexpr std::size_t evth_particle = 2; // word 3: particle code
41
- inline constexpr std::size_t evth_energy = 3; // word 4 [GeV]
42
- inline constexpr std::size_t evth_first_height = 6; // word 7 [cm]
43
- inline constexpr std::size_t evth_zenith = 10; // word 11 [rad]
44
- inline constexpr std::size_t evth_azimuth = 11; // word 12 [rad, CORSIKA convention]
45
- inline constexpr std::size_t evth_flags = 76; // word 77: integer flags (see below)
40
+ inline constexpr std::size_t evth_event_number = 1; // word 2
41
+ inline constexpr std::size_t evth_particle = 2; // word 3: particle code
42
+ inline constexpr std::size_t evth_energy = 3; // word 4 [GeV]
43
+ inline constexpr std::size_t evth_first_height = 6; // word 7 [cm]
44
+ inline constexpr std::size_t evth_zenith = 10; // word 11 [rad]
45
+ inline constexpr std::size_t evth_azimuth = 11; // word 12 [rad, CORSIKA convention]
46
+ inline constexpr std::size_t evth_flags = 76; // word 77: integer flags (see below)
47
+ // Word 79: CURVED flag (0 = standard, 1 = curved, 2 = curved with upgoing primaries).
48
+ inline constexpr std::size_t evth_curved_flag = 78;
46
49
  inline constexpr std::size_t evth_wavelength_min = 95; // word 96 [nm]
47
50
  inline constexpr std::size_t evth_wavelength_max = 96; // word 97 [nm]
48
51
  // Word 77 as an integer (corsika.tex:8624-8637, bit n = 1 << (n - 1)): bit 5 =
@@ -270,6 +273,7 @@ private:
270
273
  }
271
274
  emit(RecordType::event, std::move(metadata));
272
275
  event_open_ = true;
276
+ next_photon_index_.clear();
273
277
  blob(std::move(block));
274
278
  }
275
279
 
@@ -313,10 +317,10 @@ private:
313
317
  const TelPos &pos = *telpos_;
314
318
  std::vector<double> x, y, z, r;
315
319
  for (std::size_t i = 0; i < pos.x.size(); ++i) {
316
- x.push_back(pos.x[i] / 100.0);
317
- y.push_back(pos.y[i] / 100.0);
318
- z.push_back(pos.z[i] / 100.0);
319
- r.push_back(pos.r[i] / 100.0);
320
+ x.push_back(cm_to_m(pos.x[i]));
321
+ y.push_back(cm_to_m(pos.y[i]));
322
+ z.push_back(cm_to_m(pos.z[i]));
323
+ r.push_back(cm_to_m(pos.r[i]));
320
324
  }
321
325
  metadata["n_telescopes"] = static_cast<std::int64_t>(pos.x.size());
322
326
  metadata["telescope_x_m"] = std::move(x);
@@ -329,6 +333,10 @@ private:
329
333
  metadata["atmosphere_id"] = (flags >> corsika_word::evth_flag_atmosphere_shift) &
330
334
  corsika_word::evth_flag_atmosphere_mask;
331
335
  metadata["refraction_applied"] = (flags >> corsika_word::evth_flag_refraction_bit) & 1;
336
+ // CURVED: 1 = downgoing primaries, 2 = upgoing primaries also allowed.
337
+ const int curved = round_to_int(header_word(*evth, corsika_word::evth_curved_flag, "EVTH"));
338
+ metadata["curvature_applied"] =
339
+ (curved == 1 || curved == 2) ? std::int64_t{1} : std::int64_t{0};
332
340
  metadata["wavelength_min_nm"] =
333
341
  static_cast<double>(header_word(*evth, corsika_word::evth_wavelength_min, "EVTH"));
334
342
  metadata["wavelength_max_nm"] =
@@ -375,15 +383,16 @@ private:
375
383
  tel_meta["array_index"] = static_cast<std::int64_t>(p.array);
376
384
  if (telpos_ && p.tel >= 0 && static_cast<std::size_t>(p.tel) < telpos_->x.size()) {
377
385
  const auto i = static_cast<std::size_t>(p.tel);
378
- tel_meta["x_m"] = telpos_->x[i] / 100.0;
379
- tel_meta["y_m"] = telpos_->y[i] / 100.0;
380
- tel_meta["z_m"] = telpos_->z[i] / 100.0;
381
- tel_meta["r_m"] = telpos_->r[i] / 100.0;
386
+ tel_meta["x_m"] = cm_to_m(telpos_->x[i]);
387
+ tel_meta["y_m"] = cm_to_m(telpos_->y[i]);
388
+ tel_meta["z_m"] = cm_to_m(telpos_->z[i]);
389
+ tel_meta["r_m"] = cm_to_m(telpos_->r[i]);
382
390
  }
383
391
  emit(RecordType::telescope, std::move(tel_meta));
384
392
 
385
393
  const std::size_t total = p.bunches.size();
386
- std::uint64_t first_index = 0;
394
+ // Index among all photons of this telescope and event: continues across array reuses.
395
+ std::uint64_t &first_index = next_photon_index_[p.tel];
387
396
  constexpr std::uint32_t mask = column_bit(Column::x) | column_bit(Column::y) |
388
397
  column_bit(Column::cx) | column_bit(Column::cy) |
389
398
  column_bit(Column::t) | column_bit(Column::zem) |
@@ -426,7 +435,8 @@ private:
426
435
  std::optional<TelPos> telpos_;
427
436
  bool run_open_{};
428
437
  bool event_open_{};
429
- bool rune_seen_{}; // a RUNE block was read since the last RUNH
438
+ std::map<int, std::uint64_t> next_photon_index_; // per telescope, reset at each event
439
+ bool rune_seen_{}; // a RUNE block was read since the last RUNH
430
440
  };
431
441
 
432
442
  } // namespace detail
@@ -479,9 +489,12 @@ public:
479
489
  // The 1205 header (12 bytes) is filled in when the telescope ends.
480
490
  bunch_bytes_.assign(photons_header_size, 0);
481
491
  photon_sum_ = 0.0;
482
- next_index_ = 0;
483
492
  return;
484
493
  }
494
+ case RecordType::event:
495
+ begin_event();
496
+ flush_array();
497
+ break;
485
498
  case RecordType::photons:
486
499
  append(record.chunk);
487
500
  return;
@@ -514,6 +527,9 @@ public:
514
527
  // call this after the last record if the stream does not end with one.
515
528
  void finish() { flush_array(); }
516
529
 
530
+ // A new event: photon indices of each telescope start again at 0.
531
+ void begin_event() { next_index_.clear(); }
532
+
517
533
  // Photons of the open telescope record (ground frame), without a Record copy.
518
534
  void write_chunk(const PhotonChunk &chunk) { append(chunk); }
519
535
 
@@ -526,9 +542,10 @@ private:
526
542
  throw std::runtime_error("sim_telarray input needs ground-frame photons");
527
543
  }
528
544
  chunk.validate();
529
- if (chunk.first_index != next_index_) {
545
+ std::uint64_t &next_index = next_index_[telescope_id_];
546
+ if (chunk.first_index != next_index) {
530
547
  throw std::runtime_error("photon chunk first_index " + std::to_string(chunk.first_index) +
531
- " where " + std::to_string(next_index_) +
548
+ " where " + std::to_string(next_index) +
532
549
  " is expected (gap, overlap or reordering)");
533
550
  }
534
551
  const std::size_t n = chunk.rows();
@@ -559,7 +576,7 @@ private:
559
576
  detail::store_f32_le(dst + 28, lambda.empty() ? 0.0F : lambda[i]);
560
577
  photon_sum_ += w[i];
561
578
  }
562
- next_index_ += n;
579
+ next_index += n;
563
580
  }
564
581
 
565
582
  void finish_telescope(const Metadata &metadata) {
@@ -619,7 +636,9 @@ private:
619
636
  int array_{};
620
637
  int telescope_id_{};
621
638
  bool tel_open_{};
622
- std::uint64_t next_index_{}; // first_index the next chunk of the open telescope must have
639
+ // first_index the next chunk of each telescope must have in the current event; it
640
+ // continues across array reuses.
641
+ std::map<int, std::uint64_t> next_index_;
623
642
  std::vector<std::uint8_t> bunch_bytes_; // 1205 block of the open telescope (header first)
624
643
  double photon_sum_{}; // sum of the weights of the open telescope
625
644
  std::vector<EventioBlock> children_;
@@ -3,6 +3,8 @@
3
3
  // EventIO block container and the IACT payloads of CORSIKA output (types
4
4
  // 1200-1217). Own little-endian code; only the formats needed by obdeect.stream.
5
5
 
6
+ #include "obdeect/stream/telpos.hpp"
7
+
6
8
  #include <algorithm>
7
9
  #include <bit>
8
10
  #include <cmath>
@@ -0,0 +1,57 @@
1
+ #pragma once
2
+
3
+ // Locale-independent number parsing. The whole token must be the number; a leading '+' is
4
+ // accepted. Callers add their own context to the error message.
5
+
6
+ #include <cctype>
7
+ #include <charconv>
8
+ #include <cmath>
9
+ #include <locale>
10
+ #include <sstream>
11
+ #include <string>
12
+ #include <string_view>
13
+ #include <system_error>
14
+
15
+ namespace obdeect::stream::parse {
16
+
17
+ // A finite real number filling the whole token.
18
+ [[nodiscard]] inline bool finite_number(std::string_view token, double &value) {
19
+ if (token.size() > 1 && token.front() == '+' && token[1] != '-' && token[1] != '+')
20
+ token.remove_prefix(1);
21
+ if (token.empty())
22
+ return false;
23
+ #if defined(__cpp_lib_to_chars) && __cpp_lib_to_chars >= 201611L
24
+ double parsed = 0.0;
25
+ const auto result = std::from_chars(token.data(), token.data() + token.size(), parsed);
26
+ if (result.ec != std::errc{} || result.ptr != token.data() + token.size())
27
+ return false;
28
+ #else
29
+ if (std::isspace(static_cast<unsigned char>(token.front())))
30
+ return false;
31
+ std::istringstream stream{std::string(token)};
32
+ stream.imbue(std::locale::classic());
33
+ double parsed = 0.0;
34
+ if (!(stream >> parsed) || stream.peek() != std::char_traits<char>::eof())
35
+ return false;
36
+ #endif
37
+ if (!std::isfinite(parsed))
38
+ return false;
39
+ value = parsed;
40
+ return true;
41
+ }
42
+
43
+ // A decimal integer (optional sign) filling the whole token and fitting in T.
44
+ template <class T> [[nodiscard]] inline bool integer(std::string_view token, T &value) {
45
+ if (token.size() > 1 && token.front() == '+' && token[1] != '-' && token[1] != '+')
46
+ token.remove_prefix(1);
47
+ if (token.empty())
48
+ return false;
49
+ T parsed{};
50
+ const auto result = std::from_chars(token.data(), token.data() + token.size(), parsed, 10);
51
+ if (result.ec != std::errc{} || result.ptr != token.data() + token.size())
52
+ return false;
53
+ value = parsed;
54
+ return true;
55
+ }
56
+
57
+ } // namespace obdeect::stream::parse