From 77b0c173a447058d0c348bf4627924e0d4b07407 Mon Sep 17 00:00:00 2001
From: Jonas Krimmer <jonas.krimmer@kit.edu>
Date: Fri, 28 Mar 2025 09:34:17 +0000
Subject: [PATCH] fix: behavior for non-existent xvals.csv

---
 examples/batch_analysis.jl | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/examples/batch_analysis.jl b/examples/batch_analysis.jl
index 4993e42..ab4fdd5 100644
--- a/examples/batch_analysis.jl
+++ b/examples/batch_analysis.jl
@@ -107,17 +107,16 @@ function batch_scintillation_index(dirname)
 
     # gather all hdf5 files in the given directory `dirname`
     files = readdir(dirname, join=true)
-    csvidx = findfirst(contains("xvals.csv"), files)
     h5files = filter(x -> splitext(x)[2] == ".hdf5", files)
 
     Nf = length(h5files)
 
-    # gather xvals from corresponding csv if possible - otherwise 1:N
-    df = if isnothing(csvidx)
-        DataFrame(:File => range(1, Nf))
-    else
-        CSV.read(files[csvidx], DataFrame)
+    # gather xvals from corresponding csv if possible - otherwise generate xvals.csv and fill with 1:N
+    xvals_fn = joinpath(dirname, "xvals.csv")
+    !isfile(xvals_fn) && open(xvals_fn, "w") do fid
+        CSV.write(fid, DataFrame(:file => range(1, Nf)))
     end
+    df = CSV.read(xvals_fn, DataFrame)
 
     SI_est = zeros(Nf)
     SI_pw = zeros(Nf)
@@ -133,7 +132,9 @@ function batch_scintillation_index(dirname)
 
         # make sure the order the files are read matches the xvals in the DataFrame
         xname = first(names(df))
-        @assert df[k, xname] == metadata[string(xname)]
+        if xname != "file"
+            @assert df[k, xname] == metadata[string(xname)]
+        end
 
         Cn² = metadata["Cn2"]
         λ = metadata["lambda"]
-- 
GitLab