Skip to content

Commit 2c8b435

Browse files
committed
simplify code; make several download attempts; delete local temporary file after downloading
1 parent 41985ce commit 2c8b435

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/IO.jl

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ function save_GMG(filename::String, data::Union{GeoData, CartData, UTMData}; dir
2323
end
2424

2525
"""
26-
load_GMG(filename::String, dir=pwd())
26+
load_GMG(filename::String, dir=pwd(); maxattempts=5)
2727
2828
Loads a `GeoData`/`CartData`/`UTMData` data set from jld2 file `filename`
29-
Note: the `filename` can also be a remote `url`, in which case we first download that file to a temporary directory before opening it
29+
Note: the `filename` can also be a remote `url`, in which case we first download that file to a temporary directory before opening it.
30+
We make `maxattempts` attempts to download it before giving up.
3031
3132
Example 1 - Load local file
3233
====
@@ -56,11 +57,11 @@ GeoData
5657
```
5758
5859
"""
59-
function load_GMG(filename::String, dir=pwd())
60+
function load_GMG(filename::String, dir=pwd(); maxattempts=5)
6061

62+
local_filename = "download_GMG_temp.jld2"
6163
if contains(filename,"http")
62-
#download remote file to a local temporary directory
63-
file_ext = Downloads.download(filename, joinpath(pwd(),"download_GMG_temp.jld2"))
64+
file_ext = download_data(filename, local_filename, dir=dir, maxattempts=maxattempts)
6465
else
6566
# local file
6667
file_ext = joinpath(dir,filename*".jld2")
@@ -69,16 +70,20 @@ function load_GMG(filename::String, dir=pwd())
6970
# load data:
7071
data = load_object(file_ext)
7172

73+
# remove local temporary file
74+
rm(local_filename)
75+
7276
return data
7377
end
7478

7579

7680

7781

7882
"""
79-
download_data(url::String, local_filename="temp.dat"; dir=pwd() )
83+
download_data(url::String, local_filename="temp.dat"; dir=pwd(), maxattempts=5 )
8084
81-
Downloads a remote dataset with name `url` from a remote location and saves it to the current directory
85+
Downloads a remote dataset with name `url` from a remote location and saves it to the current directory.
86+
If download fails, we make `maxattempts` attempts before giving up.
8287
8388
Example
8489
====
@@ -89,14 +94,28 @@ julia> download_data(url)
8994
```
9095
9196
"""
92-
function download_data(url::String, local_filename="temp.dat"; dir=pwd() )
97+
function download_data(url::String, local_filename="temp.dat"; dir=pwd(), maxattempts=5)
9398

9499
if !contains(url,"http")
95100
@warn "the url does not contain http; please double check that it worked"
96101
end
97102

98-
# download remote file to a local temporary directory
99-
file_ext = Downloads.download(url, joinpath(dir,local_filename))
103+
#download remote file to a local temporary directory
104+
file_ext = [];
105+
attempt = 0
106+
while attempt<maxattempts
107+
try
108+
file_ext = Downloads.download(url, joinpath(dir,local_filename))
109+
break
110+
catch
111+
@warn "Failed downloading data on attempt $attempt/$maxattempts"
112+
sleep(5) # wait a few sec
113+
end
114+
attempt += 1
115+
end
116+
if isempty(file_ext)
117+
error("Could not download GMT topography data")
118+
end
100119

101120
return file_ext
102121
end

0 commit comments

Comments
 (0)