From 9c51757b223021c24045e8b85717b081c2733824 Mon Sep 17 00:00:00 2001
From: Tamas Gal <himself@tamasgal.com>
Date: Thu, 7 Dec 2023 23:42:03 +0100
Subject: [PATCH] Add readout for mechanics

---
 docs/src/api.md            |  3 +++
 src/KM3io.jl               |  2 +-
 src/hardware.jl            | 53 ++++++++++++++++++++++++++++++++++++++
 test/hardware.jl           | 12 +++++++++
 test/samples/mechanics.txt |  3 +++
 5 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/docs/src/api.md b/docs/src/api.md
index 11b3ac01..a7c8e419 100644
--- a/docs/src/api.md
+++ b/docs/src/api.md
@@ -62,6 +62,9 @@ write(::AbstractString, ::Vector{Tripod})
 piezoenabled
 hydrophoneenabled
 center
+StringMechanics
+StringMechanicsParameters
+read(::AbstractString, ::Type{StringMechanics})
 ```
 
 ## Optical Data
diff --git a/src/KM3io.jl b/src/KM3io.jl
index f59d2e10..044a9d83 100644
--- a/src/KM3io.jl
+++ b/src/KM3io.jl
@@ -21,7 +21,7 @@ export ROOTFile
 export H5File, H5CompoundDataset, create_dataset, addmeta
 
 export Direction, Position, UTMPosition, Location, Quaternion, Track, AbstractCalibratedHit
-export Detector, DetectorModule, PMT, Tripod, Hydrophone, center, isbasemodule, getmodule, modules, getpmt, getpmts
+export Detector, DetectorModule, PMT, Tripod, Hydrophone, StringMechanics, center, isbasemodule, getmodule, modules, getpmt, getpmts
 
 # Acoustics
 export Waveform, AcousticSignal, AcousticsTriggerParameter, piezoenabled, hydrophoneenabled
diff --git a/src/hardware.jl b/src/hardware.jl
index ed469bf5..b0186111 100644
--- a/src/hardware.jl
+++ b/src/hardware.jl
@@ -548,3 +548,56 @@ function write(io::IO, d::Detector; version=:same)
         end
     end
 end
+
+
+"""
+
+Data structure for parameters of the mechanical model of strings. This data
+structure is used to calculate the effective height conform to the mechanical
+model of the string.
+
+"""
+struct StringMechanicsParameters
+    a::Float64
+    b::Float64
+end
+
+"""
+
+A container structure which holds the mechanical model parameters for multiple
+strings, including a default value for strings which have specific parameters.
+
+"""
+struct StringMechanics
+    default::StringMechanicsParameters
+    stringparams::Dict{Int, StringMechanicsParameters}
+end
+Base.getindex(s::StringMechanics, idx::Integer) = get(s.stringparams, idx, s.default)
+
+
+"""
+
+Reads the mechanical models from a text file.
+
+"""
+function read(filename::AbstractString, T::Type{StringMechanics})
+    stringparams = Dict{Int, StringMechanicsParameters}()
+    default_a = 0.0
+    default_b = 0.0
+    for line ∈ readlines(filename)
+        if startswith(line, "#")
+            continue
+        end
+        string, a, b = split(line)
+        s = parse(Int, string)
+        a = parse(Float64, a)
+        b = parse(Float64, b)
+        if s == -1  # wildcard for any string
+            default_a = a
+            default_b = b
+        else
+            stringparams[s] = StringMechanicsParameters(a, b)
+        end
+    end
+    T(StringMechanicsParameters(default_a, default_b), stringparams)
+end
diff --git a/test/hardware.jl b/test/hardware.jl
index 90d839b4..b62ef372 100644
--- a/test/hardware.jl
+++ b/test/hardware.jl
@@ -150,6 +150,18 @@ const SAMPLES_DIR = joinpath(@__DIR__, "samples")
         @test trigger.nmin == 90
     end
 
+    @testset "mechanics" begin
+        mechanics = read(joinpath(SAMPLES_DIR, "mechanics.txt"), StringMechanics)
+        @test 0.00094 ≈ mechanics[1].a
+        @test 294.291 ≈ mechanics[1].b
+        @test 0.00094 ≈ mechanics[-1].a
+        @test 294.291 ≈ mechanics[-1].b
+        @test 0.00094 ≈ mechanics[42].a
+        @test 294.291 ≈ mechanics[42].b
+        @test 5.6 ≈ mechanics[5].a
+        @test 7.8 ≈ mechanics[5].b
+    end
+
     @testset "utilities" begin
         mod = DetectorModule(1, UTMPosition(0, 0, 0), Location(0, 0), 0, PMT[], missing, 0, 0)
         @test hydrophoneenabled(mod)
diff --git a/test/samples/mechanics.txt b/test/samples/mechanics.txt
index 731e272d..a118877a 100644
--- a/test/samples/mechanics.txt
+++ b/test/samples/mechanics.txt
@@ -1 +1,4 @@
+# some comment
 -1 0.00094 294.291
+23 1.2 3.4
+5 5.6 7.8
-- 
GitLab