From 4865904b040a86ef7f4e396845ca56c8b67aa76c Mon Sep 17 00:00:00 2001 From: Etienne Date: Thu, 21 Mar 2024 18:00:07 +0100 Subject: [PATCH 1/3] make `rename_if_exists` work with folder + all types of extension + add docstring --- src/utilitaries.jl | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/utilitaries.jl b/src/utilitaries.jl index 8488f52..98e7bb5 100644 --- a/src/utilitaries.jl +++ b/src/utilitaries.jl @@ -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 From 3c5bf1c902fcdff34ddfeb45991b23813acc3511 Mon Sep 17 00:00:00 2001 From: Etienne Date: Thu, 21 Mar 2024 18:02:09 +0100 Subject: [PATCH 2/3] use the new `rename_if_exists` function when creating simulation results folders --- src/main.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.jl b/src/main.jl index 9506f7d..58db4ca 100644 --- a/src/main.jl +++ b/src/main.jl @@ -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!") @@ -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!") From 0834fc8a0be220f3f7147a88525e9b5f97d05b3d Mon Sep 17 00:00:00 2001 From: Etienne Date: Thu, 21 Mar 2024 18:05:50 +0100 Subject: [PATCH 3/3] update NEWS.md --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index e7fc05d..f13cca6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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)