/
Input_Batch.jl
360 lines (261 loc) · 10.5 KB
/
Input_Batch.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#**************************************************************************************
# Input_Batch.jl
# =============== part of the GeoEfficiency.jl package.
#
# all the input from the csv files or for batch work to the package is handled by some function here.
#
#**************************************************************************************
#------------------ consts - globals - imports ----------------------------
using .MathConstants, DelimitedFiles
# parameters [dataDir - dataFolder] are assigned in `Config.jl`.
# include("Config.jl")
mkpath(dataDir)
const Detectors = "Detectors.csv";
const srcHeights = "srcHeights.csv";
const srcRhos = "srcRhos.csv";
const srcRadii = "srcRadii.csv";
const srcLengths = "srcLengths.csv";
@enum SrcType srcUnknown = -1 srcPoint = 0 srcLine = 1 srcDisk = 2 srcVolume = 3 srcNotPoint = 4
global srcType = srcUnknown
#------------------ typeofSrc --------------------------------------
"""
typeofSrc()::SrcType
return the current value of the global `GeoEfficiency.srcType`.
"""
typeofSrc()::SrcType = srcType # srcType != SrcType, but
"""
typeofSrc(x::Int)::SrcType
set and return the value of the global `GeoEfficiency.srcType` corresponding to `x`.
* srcUnknown = -1 also any negative integer treated as so,
* srcPoint = 0,
* srcLine = 1,
* srcDisk = 2,
* srcVolume = 3,
* srcNotPoint = 4 also any greater than 4 integer treated as so.
"""
function typeofSrc(x::Int)::SrcType
global srcType = if x < 0
SrcType(-1)
elseif x > 4
SrcType(4)
else
SrcType(x)
end
end #function
#------------------------ setSrcToPoint ---------------------------
"""
setSrcToPoint()::Bool
return whether the source type is a point or not.
"""
setSrcToPoint()::Bool = srcType === srcPoint
"""
setSrcToPoint(yes::Bool)::Bool
return whether the source type is a point or not after setting `srcType` to `srcPoint` if
`yes` = `true` else if `yes` = `false` setting it to `srcNotPoint` if it was not already
set to other non-point type (`srcDisk`, `srcLine`, `srcVolume`).
!!! note
* The user can use this function to change the source type any time.
* The source type is set the fist time asked for source.
**see also:** [`typeofSrc(::Int)`](@ref).
"""
function setSrcToPoint(yes::Bool)::Bool
global srcType = if yes
srcPoint
elseif srcType in [srcUnknown, srcPoint]
srcNotPoint
else
srcType
end
return srcType === srcPoint
end
"""
setSrcToPoint(prompt::AbstractString)::Bool
return whether the source type is a point or not. only prompt the user to set the source
type if it were not already set before.
**see also:** [`typeofSrc(::Int)`](@ref), [`setSrcToPoint(::Bool)`](@ref).
"""
setSrcToPoint(prompt::AbstractString)::Bool = srcType != srcUnknown ? setSrcToPoint() :
setSrcToPoint(input(prompt) |> lowercase != "n")
#---------------------------- detector_info_from_csvFile ------------------------------
"""# UnExported
detector_info_from_csvFile(detectors::AbstractString = Detectors,
datadir::AbstractString = dataDir)
return a vector{Detector} based on information in the file of name `detectors` found in the
directory `datadir`.
!!! note
* if no path is given the second argument `datadir` is default to `$(dataDir)` as set by
the constant `dataDir`.
* if no file name is specified the name of the predefined file `$Detectors` as set by
the constant `Detectors`.
* the no argument method is the most useful; other methods are mainly for `test propose`.
"""
function detector_info_from_csvFile(detectors::AbstractString = Detectors,
datadir::AbstractString = dataDir)
detector_info_array::Matrix{Float64} = Matrix{Float64}(undef, 0, 0)
@info("opening '$(detectors)'......", _file=nothing)
try
detector_info_array = readdlm(joinpath(datadir, detectors), ',', header = true)[1];
return getDetectors(detector_info_array)
catch err
if isa(err, SystemError)
@error("detector_info_from_csvFile: Some thing went wrong, may be the file '$(joinpath(datadir, detectors))' can't be found")
end
rethrow()
end #try
end #function
#---------------------------- read_from_csvFile --------------------------------
"""# UnExported
read_from_csvFile(csv_data::AbstractString,
datadir::AbstractString = dataDir)::Vector{Float64}
return Vector{Float64} based on data in csv file named `csv_data`. directory `datadir` point to
where the file is located default to ``$(dataDir)`` as set by the constant `dataDir`.
"""
function read_from_csvFile(csv_data::AbstractString, datadir::AbstractString = dataDir)::Vector{Float64}
@info("Opening `$(csv_data)`......", _file=nothing)
try
indata = readdlm(joinpath(datadir, csv_data), ',', header = true)[1][:,1]
return float(indata) |> sort;
catch err
if isa(err, SystemError)
@error("Some thing went wrong, may be `$(csv_data)` can't be found in `$(datadir)`", _file=nothing)
else
@error("Some thing went wrong, may be `$(csv_data)` in `$(datadir)` format is bad or empty", _file=nothing)
end
return Float64[0.0]
end #try
end #function
#--------------------------- read_batch_info ------------------------------------
"""# UnExported
read_batch_info()
read `detectors` and `sources` parameters from the predefined csv files.
Return a tuple
(detectors_array,
srcHeights_array,
srcRhos_array,
srcRadii_array,
srcLengths_array,
GeoEfficiency_isPoint)
"""
read_batch_info() = read_batch_info(dataDir,
Detectors,
srcHeights,
srcRhos,
srcRadii,
srcLengths)
"""# UnExported
read_batch_info(datadir::AbstractString,
detectors::AbstractString,
srcHeights::AbstractString,
srcRhos::AbstractString,
srcRadii::AbstractString,
srcLengths::AbstractString)
read `detectors` and `sources` parameters from the location given in the argument list.
Return a tuple
(detectors_array,
srcHeights_array,
srcRhos_array,
srcRadii_array,
srcLengths_array,
isPoint)
"""
function read_batch_info(datadir::AbstractString,
detectors::AbstractString,
srcHeights::AbstractString,
srcRhos::AbstractString,
srcRadii::AbstractString,
srcLengths::AbstractString)
@info("Starting the Batch Mode ....", _file=nothing)
isPoint = setSrcToPoint("\n Is it a point source {Y|n} ?")
@info("Reading data from `CSV files` at $datadir .....", _file=nothing)
detectors_array::Vector{Detector} = try
detector_info_from_csvFile(detectors, datadir)
catch err
getDetectors()
end
srcHeights_array::Vector{Float64} = read_from_csvFile(srcHeights, datadir)
srcRhos_array::Vector{Float64} = [0.0]
srcRadii_array::Vector{Float64} = [0.0]
srcLengths_array::Vector{Float64} = [0.0]
function batchfailure(err::AbstractString)
@warn(err, ", transfer to direct data input via the `console`......", _file=nothing)
sleep(3); src = source()
srcHeights_array, srcRhos_array, srcRadii_array, srcLengths_array =
[src[1].Height], [src[1].Rho], [src[2]], [src[3]]
return nothing
end #function
if srcHeights_array == [0.0]
batchfailure("`$(srcHeights)` is not found in `$(datadir)`)")
elseif isPoint
srcRhos_array = read_from_csvFile(srcRhos, datadir)
else
srcRadii_array = read_from_csvFile(srcRadii, datadir)
if srcRadii_array == [0.0]
batchfailure("`$(srcRadii)` is not found in `$(datadir)`)")
else
srcLengths_array = read_from_csvFile(srcLengths, datadir)
end #if
end #if
#println("\n Results log\n=============")
return (detectors_array,
srcHeights_array,
srcRhos_array,
srcRadii_array,
srcLengths_array,
isPoint,)
end #function
#------------------------- getDetectors -------------------------------------
"""
getDetectors(detectors_array::Vector{<:Detector} = Detector[])::Vector{Detector}
return the `detectors_array` as Vector{Detector} extended by the entered detectors and sorted according to the
detector volume.
prompt the user to input detector parameters from the `console`.
!!! note
If no array received in the input an empty array will be created to receive the converted detectors.
"""
function getDetectors(detectors_array::Vector{<:Detector} = Detector[])::Vector{Detector}
Vector{Detector}(detectors_array); @info("Please, input the detector information via the console", _file=nothing)
while(true)
try
push!(detectors_array, Detector());
catch err
println(err); @warn("Please: Enter a New Detector", _file=nothing)
continue
end #try
lowercase(input("""\n
- To add a new detector press return\n
- To quit press 'q'|'Q' then return\n
\n\t your Choice: """, :blue)) == "q" && break
end #while
return detectors_array |> sort
end #function
"""
getDetectors(detector_info_array::Matrix{<:Real},
detectors_array::Vector{<:Detector} = Detector[];
console_FB=true)::Vector{Detector}
return `detectors_array` as Vector{Detector}, after extending it with the successfully converted detectors. while,
attempt to convert detectors from the information in `detector_info_array`.
!!! note
if `console_FB` argument is set to true , the function will call `getDetectors()` to take input
from the `console` if the `detector_info_array` is empty or contain no numerical element.
"""
function getDetectors(detector_info_array::Matrix{<:Real},
detectors_array::Vector{<:Detector} = Detector[];
console_FB = true)::Vector{Detector}
if isempty(detector_info_array)
if console_FB
@info("The new detectors information may entered via the console", _file=nothing)
return getDetectors(detectors_array)
else
error("getDetectors: Empty `detector_info_array`")
end
else
Vector{Detector}(detectors_array)
for i_th_line = 1:size(detector_info_array)[1]
try
push!(detectors_array, Detector((detector_info_array[i_th_line,:])...))
catch err
end #try
end #for
return detectors_array |> sort
end #if
end #function