Skip to content

Commit

Permalink
Adding check for duplicates in add_interferometer!
Browse files Browse the repository at this point in the history
This commit resolves #17
add_interferometer! function has been modified to read the json file
using JSON.parsefile directly and pass it to its method that takes as
input a dictionary. Note that JSON.parsefile returns a Dict{String, Any}
dictionary while OMAS.imas2dict constructs a Dict{Symbol, Any} dictionary.
Thus after reading the josn file it is converted into a Dict{Symbol, Any}.
Then a duplicacy check is run to ensure that new file has no channels that
have an overlapping name or identifier. If a duplicate is found, an error
is thrown and it recommends the user to use overwrite=true.

In case of overwrite=true, the duplicate channels in existing ids are
deletec and the new channels are added. This may change the indices of
the channel in ids.interferometer.channel array.

In case there is no duplicacy or overwrite=true with additional
channels, the new channels are appended at the end of the
ids.interferometer.channel array.

Test cases have been expanded to cover the new functionality.
  • Loading branch information
anchal-physics committed Feb 7, 2024
1 parent 15de965 commit b926763
Show file tree
Hide file tree
Showing 5 changed files with 366 additions and 57 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Anchal Gupta <guptaa@fusion.gat.com>"]
version = "0.2.0"

[deps]
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
GGDUtils = "b7b5e640-9b39-4803-84eb-376048795def"
OMAS = "91cfaa06-6526-4804-8666-b540b3feef2f"
Expand Down
35 changes: 35 additions & 0 deletions samples/test_interferometer_new_channels.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"interferometer": {
"channel": [
{
"name": "V1.5",
"identifier": "V1.5",
"line_of_sight": {
"first_point": {
"phi": 0.5,
"r": 5.5,
"z": -5.0
},
"second_point": {
"phi": 0.5,
"r": 5.5,
"z": 6.0
},
"third_point": {
"phi": 0.5,
"r": 5.5,
"z": -5.0
}
},
"wavelength": [
{
"value": 10.6e-6
},
{
"value": 6.33e-7
}
]
}
]
}
}
122 changes: 122 additions & 0 deletions samples/test_interferometer_same_names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"interferometer": {
"channel": [
{
"name": "V1",
"identifier": "V1",
"line_of_sight": {
"first_point": {
"phi": 0.2,
"r": 5.2,
"z": -5.0
},
"second_point": {
"phi": 0.2,
"r": 5.2,
"z": 6.0
},
"third_point": {
"phi": 0.2,
"r": 5.2,
"z": -5.0
}
},
"wavelength": [
{
"value": 10.6e-6
},
{
"value": 6.33e-7
}
]
},
{
"name": "V2",
"identifier": "V2",
"line_of_sight": {
"first_point": {
"phi": 0.2,
"r": 6.2,
"z": -5.0
},
"second_point": {
"phi": 0.2,
"r": 6.2,
"z": 6.0
},
"third_point": {
"phi": 0.2,
"r": 6.2,
"z": -5.0
}
},
"wavelength": [
{
"value": 10.6e-6
},
{
"value": 6.33e-7
}
]
},
{
"name": "V3",
"identifier": "V3",
"line_of_sight": {
"first_point": {
"phi": 0.2,
"r": 7.2,
"z": -5.0
},
"second_point": {
"phi": 0.2,
"r": 7.2,
"z": 6.0
},
"third_point": {
"phi": 0.2,
"r": 7.2,
"z": -5.0
}
},
"wavelength": [
{
"value": 10.6e-6
},
{
"value": 6.33e-7
}
]
},
{
"name": "H1",
"identifier": "H1",
"line_of_sight": {
"first_point": {
"phi": 0.2,
"r": 9.2,
"z": 0.0
},
"second_point": {
"phi": 0.2,
"r": 3.2,
"z": 0.0
},
"third_point": {
"phi": 0.2,
"r": 9.2,
"z": 0.0
}
},
"wavelength": [
{
"value": 10.6e-6
},
{
"value": 6.33e-7
}
]
}
]
}
}
85 changes: 81 additions & 4 deletions src/interferometer.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@

struct OverwriteAttemptError <: Exception
var::String
end

Base.showerror(io::IO, e::OverwriteAttemptError) = print(io, e.var)

function convert_strings_to_symbols(d::Dict{String, Any})
new_d = Dict{Symbol, Any}()
for (k, v) d
if isa(v, Dict{String, Any})
new_d[Symbol(k)] = convert_strings_to_symbols(v)
elseif isa(v, Vector{Any})
if length(v) > 0
if isa(v[1], Dict{String, Any})
new_d[Symbol(k)] = Vector{Dict{Symbol, Any}}(undef, length(v))
for ii eachindex(v)
new_d[Symbol(k)][ii] = convert_strings_to_symbols(v[ii])
end
else
new_d[Symbol(k)] = v
end
else
new_d[Symbol(k)] = v
end
else
new_d[Symbol(k)] = v
end
end
return new_d
end

"""
add_interferometer(
@nospecialize(ids::OMAS.dd)=OMAS.dd(),
Expand All @@ -10,10 +42,12 @@ line integrated electron density if not present
"""
function add_interferometer!(
config::String=default_ifo,
@nospecialize(ids::OMAS.dd)=OMAS.dd(),
@nospecialize(ids::OMAS.dd)=OMAS.dd();
overwrite::Bool=false, verbose::Bool=false,
)::OMAS.dd
if endswith(config, ".json")
OMAS.json2imas(config, ids)
config_dict = convert_strings_to_symbols(OMAS.JSON.parsefile(config))
add_interferometer!(config_dict, ids; overwrite=overwrite, verbose=verbose)
else
error("Only JSON files are supported.")
end
Expand All @@ -33,9 +67,52 @@ electron density if not present
"""
function add_interferometer!(
config::Dict{Symbol, Any},
@nospecialize(ids::OMAS.dd)=OMAS.dd(),
@nospecialize(ids::OMAS.dd)=OMAS.dd();
overwrite::Bool=false, verbose::Bool=false,
)::OMAS.dd
OMAS.dict2imas(config, ids)
# Check for duplicates
if length(ids.interferometer.channel) > 0
duplicate_indices = []
new_channels = Dict(
ch[:name] => ch[:identifier] for
ch config[:interferometer][:channel]
)
for (ii, ch) enumerate(ids.interferometer.channel)
if ch.name in keys(new_channels) ||
ch.identifier in values(new_channels)
append!(duplicate_indices, ii)
end
end
if overwrite
for ii reverse(duplicate_indices)
println(
"Overwriting interferometer channel ",
"$(ids.interferometer.channel[ii].name)...",
)
deleteat!(ids.interferometer.channel, ii)
end
else
if length(duplicate_indices) > 0
err_msg =
"Duplicate interferometer channels found with " *
"overlapping names or identifiers.\n" * "Identifier: Name\n"
for ii duplicate_indices
err_msg *=
"$(ids.interferometer.channel[ii].identifier): " *
"$(ids.interferometer.channel[ii].name)\n"
end
err_msg *= "Use overwrite=true to replace them."
throw(OverwriteAttemptError(err_msg))
end
end
config[:interferometer] =
mergewith(
append!,
OMAS.imas2dict(ids.interferometer),
config[:interferometer],
)
end
OMAS.dict2imas(config, ids; verbose=verbose)
compute_interferometer(ids)
return ids
end
Expand Down
Loading

0 comments on commit b926763

Please sign in to comment.