Skip to content

Commit 20efb29

Browse files
Parsing time in EFIT header (#8)
* Try harder to parse time out of EFIT header and giveup with a warning message if that still fails. * Add set_time option to readg() as backup for failing to parse time in geqdsk * if set_time is provided, use it instead of parsing If set_time is provided, then it will be used and the code will not attempt to parse the description line to find time in it. This assumes that the user is setting time deliberately, so it should override any other logic. In case set_time is not provided, then parsing will be attempted but if it fails, instead of just printing a warning, the code should throw an error with the error message so that the code stops continuing further and alerts the user about the issue. * Broken (needs fix and cleanup): split header parsing into its own file and add test for header parsing * parse header testing works now The separated parse header function now works and is tested. To add more tests, the header list can be expanded with other cases and expected outcomes. --------- Co-authored-by: Anchal Gupta <[email protected]>
1 parent 66b945a commit 20efb29

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

src/io.jl

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,55 @@ function read_array2d(t,n,m)
6666
return data'
6767
end
6868

69-
function readg(gfile)
69+
function parse_gfile_header(headerline::String; set_time=nothing)
70+
s = split(headerline)
71+
72+
if !isnothing(set_time)
73+
time = set_time
74+
else
75+
time = Meta.parse(s[end-3])
76+
time_warning = "EFIT.jl could not convert time from ms (assumed units) to s"
77+
omfit_advice = "Options for proceeding include:\n" *
78+
"i) Editing the header in your gEQDSK file.\n" *
79+
"ii) Trying to use OMFIT: python: `OMFITgeqdsk(file).to_omas().save('ods.jl')`\n" *
80+
"iii) Set the time manually with the `set_time` keyword argument.\n"
81+
try
82+
time /= 1000.0 # Assume it was written in ms and convert to s
83+
catch e
84+
if time isa String
85+
rg2 = r"([[:digit:]]+|(?:[[:punct:]]|[[:blank:]])+)"
86+
try
87+
time = parse(Int, collect(eachmatch(rg2, time))[1][1]) / 1000.0
88+
catch e
89+
error(
90+
time_warning,
91+
", even after trying really hard with regex and everything.\nTime is ",
92+
time,
93+
". \n",
94+
omfit_advice
95+
)
96+
end
97+
else
98+
error(time_warning, ".\nTime is parsed as ", time," of type ", typeof(time), ". ", omfit_advice)
99+
end
100+
end
101+
end
102+
103+
idum = Meta.parse(s[end-2])
104+
nw = Meta.parse(s[end-1])
105+
nh = Meta.parse(s[end])
106+
107+
return idum, time, nw, nh
108+
end
109+
110+
function readg(gfile; set_time=nothing)
70111

71112
isfile(gfile) || error("$(gfile) does not exist")
72113

73114
f = open(gfile)
74115

75116
desc = readline(f)
76-
s = split(desc)
77-
78-
time = Meta.parse(s[end-3])
79-
time /= 1000.0 # Assume it was written in ms and convert to s
80-
idum = Meta.parse(s[end-2])
81-
nw = Meta.parse(s[end-1])
82-
nh = Meta.parse(s[end])
117+
idum, time, nw, nh = parse_gfile_header(desc, set_time=set_time)
83118

84119
token = file_numbers(f)
85120

test/runtests.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Test
22
using EFIT
3+
import EFIT
34

45
g = readg(EFIT.test_gfile)
56
g2 = readg(EFIT.test_gfile2)
@@ -58,3 +59,38 @@ lt, ut = triangularity(g)
5859
@test (xrs[2] > 1.2) & (xrs[2] < 1.33)
5960
@test (xzs[2] > 1.04) & (xzs[2] < 1.17)
6061
end
62+
63+
@testset "parse header" begin
64+
headers = [
65+
" EFITD 00/00/2008 #002296 0200ms Convert 0 257 513",
66+
" EFITD 11/23/2020 #184833 3600 3 65 65",
67+
]
68+
times = [
69+
0.2,
70+
3.6,
71+
]
72+
expect_exception = [
73+
true,
74+
false,
75+
]
76+
the_set_time = 0.2
77+
for set_time in [the_set_time, nothing]
78+
for i in eachindex(headers)
79+
try
80+
idum, time, nw, nh = EFIT.parse_gfile_header(headers[i]; set_time=set_time)
81+
if !isnothing(set_time)
82+
@test time == the_set_time
83+
else
84+
@test time == times[i]
85+
end
86+
catch
87+
if expect_exception[i]
88+
println("Got the expected exception")
89+
else
90+
println("Got the unexpected exception")
91+
end
92+
@test expect_exception[i]
93+
end
94+
end
95+
end
96+
end

0 commit comments

Comments
 (0)