Skip to content

Commit

Permalink
Merge pull request #27 from egavazzi/safer_file_saving
Browse files Browse the repository at this point in the history
Safer file saving
  • Loading branch information
egavazzi authored Mar 21, 2024
2 parents 028ad42 + 0834fc8 commit bf038ad
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# News

- make saving simulation data safer [#27](https://github.com/egavazzi/AURORA.jl/pull/27)
- add scripts to plot I and Q in Julia [#26](https://github.com/egavazzi/AURORA.jl/pull/26)
- use a finer grid in altitude [#25](https://github.com/egavazzi/AURORA.jl/pull/25)
- add a steady state version of the transport code [#24](https://github.com/egavazzi/AURORA.jl/pull/24)
Expand Down
2 changes: 2 additions & 0 deletions src/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function calculate_e_transport(altitude_max, θ_lims, E_max, B_angle_to_zenith,
name_savedir = string(Dates.format(now(), "yyyymmdd-HHMM"))
end
savedir = pkgdir(AURORA, "data", root_savedir, name_savedir)
savedir = rename_if_exists(savedir)
if isdir(savedir) && (filter(startswith("IeFlickering-"), readdir(savedir)) |> length) > 0
# throw a warning if name_savedir exists and if it already contains results
print("\n", @bold @red "WARNING!")
Expand Down Expand Up @@ -178,6 +179,7 @@ function calculate_e_transport_steady_state(altitude_max, θ_lims, E_max, B_angl
name_savedir = string(Dates.format(now(), "yyyymmdd-HHMM"))
end
savedir = pkgdir(AURORA, "data", root_savedir, name_savedir)
savedir = rename_if_exists(savedir)
if isdir(savedir) && (filter(startswith("IeFlickering-"), readdir(savedir)) |> length) > 0
# throw a warning if name_savedir exists and if it already contains results
print("\n", @bold @red "WARNING!")
Expand Down
44 changes: 35 additions & 9 deletions src/utilitaries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,42 @@ function save_results(Ie, E, t, μ_lims, h_atm, I0, μ_scatterings, i, CFL_facto
end


function rename_if_exists(savefile, file_extension)
if !isfile(savefile)
return savefile
end
counter = 1
while isfile(savefile[1:end-4] * "($counter)" * "$file_extension")
counter += 1
"""
rename_if_exists(savefile)
This function takes a string as an input. If a file or folder with that name *does not* exist,
it returns the same string back. But if the folder or file already exists, it appends a
number between parenthesis to the name string.
For example, if the folder `foo/` already exist and `"foo"` is given as input, the
function will return a string `"foo(1)"` as an output. Similarly, if a file `foo.txt`
already exists and `"foo.txt"` is given as input, the function will return a string
`"foo(1).txt"`. If the file `foo(1).txt` also already exist, the function will return a string
`"foo(2).txt"`, etc...
The function should support all types of extensions.
# Calling
`newsavefile = rename_if_exists(savefile)`
"""
function rename_if_exists(savefile)
if isfile(savefile) # if a file already exists with that name
file_extension = split(savefile, ".")[end]
length_extension = length(file_extension) + 1
counter = 1
while isfile(savefile[1:end - length_extension] * "($counter)" * ".$file_extension")
counter += 1
end
newsavefile = savefile[1:end - length_extension] * "($counter)" * ".$file_extension"
elseif isdir(savefile) # if a folder already exists with that name
counter = 1
while isdir(savefile * "($counter)")
counter += 1
end
newsavefile = savefile * "($counter)"
end
savefile = savefile[1:end-4] * "($counter)" * "$file_extension"
return savefile

return newsavefile
end


Expand Down

0 comments on commit bf038ad

Please sign in to comment.