Skip to content

Commit baa8715

Browse files
committed
close files
1 parent 61a64f0 commit baa8715

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

src/MPISphericalHarmonics.jl

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,31 @@ MagneticFieldCoefficients(coeffs::Array{SphericalHarmonicCoefficients,2}, tDesig
5555

5656
# read coefficients from an HDF5-file
5757
function MagneticFieldCoefficients(path::String)
58-
file = h5open(path,"r")
5958

6059
# load spherical harmonic coefficients
6160
shcoeffs = SphericalHarmonicCoefficients(path)
62-
63-
if haskey(HDF5.root(file), "/radius")
64-
# file contains all relevant information
65-
radius = read(file, "/radius")
66-
center = read(file, "/center")
67-
if haskey(HDF5.root(file), "/ffp")
68-
ffp = read(file, "/ffp")
69-
return MagneticFieldCoefficients(shcoeffs, radius, center, ffp)
61+
62+
coeffsMF = h5open(path,"r") do file
63+
if haskey(HDF5.root(file), "/radius")
64+
# file contains all relevant information
65+
radius = read(file, "/radius")
66+
center = read(file, "/center")
67+
if haskey(HDF5.root(file), "/ffp")
68+
ffp = read(file, "/ffp")
69+
return MagneticFieldCoefficients(shcoeffs, radius, center, ffp)
70+
else
71+
# field has not FFP -> ffp = nothing
72+
return MagneticFieldCoefficients(shcoeffs, radius, center)
73+
end
7074
else
71-
# field has not FFP -> ffp = nothing
72-
return MagneticFieldCoefficients(shcoeffs, radius, center)
75+
# convert file of SphericalHarmonicCoefficients into MagneticFieldCoefficients
76+
# -> set all additional informations to 0 or nothing
77+
# use radius = 0.01 as default value
78+
return MagneticFieldCoefficients(shcoeffs, 0.01)
7379
end
74-
else
75-
# convert file of SphericalHarmonicCoefficients into MagneticFieldCoefficients
76-
# -> set all additional informations to 0 or nothing
77-
# use radius = 0.01 as default value
78-
return MagneticFieldCoefficients(shcoeffs, 0.01)
7980
end
81+
82+
return coeffsMF
8083
end
8184

8285
"""
@@ -200,15 +203,17 @@ end
200203

201204
function SphericalHarmonicsDefinedField(filename::String)
202205

203-
file = h5open(filename,"r")
206+
func = h5open(filename,"r") do file
207+
if haskey(file,"coeffs")
208+
# load coefficients
209+
coeffs_MF = MagneticFieldCoefficients(filename)
210+
func = fastfunc.(coeffs_MF.coeffs)
211+
else
212+
# load measured field
213+
coeffs_MF, expansion, func = loadTDesignCoefficients(filename)
214+
end
204215

205-
if haskey(file,"coeffs")
206-
# load coefficients
207-
coeffs_MF = MagneticFieldCoefficients(filename)
208-
func = fastfunc.(coeffs_MF.coeffs)
209-
else
210-
# load measured field
211-
coeffs_MF, expansion, func = loadTDesignCoefficients(filename)
216+
return func
212217
end
213218

214219
return SphericalHarmonicsDefinedField(func=func)

0 commit comments

Comments
 (0)