@@ -23,10 +23,11 @@ function save_GMG(filename::String, data::Union{GeoData, CartData, UTMData}; dir
2323end
2424
2525"""
26- load_GMG(filename::String, dir=pwd())
26+ load_GMG(filename::String, dir=pwd(); maxattempts=5 )
2727
2828Loads 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
3132Example 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,22 @@ function load_GMG(filename::String, dir=pwd())
6970 # load data:
7071 data = load_object (file_ext)
7172
73+ # remove local temporary file
74+ if contains (filename," http" )
75+ rm (local_filename)
76+ end
77+
7278 return data
7379end
7480
7581
7682
7783
7884"""
79- download_data(url::String, local_filename="temp.dat"; dir=pwd() )
85+ download_data(url::String, local_filename="temp.dat"; dir=pwd(), maxattempts=5 )
8086
81- Downloads a remote dataset with name `url` from a remote location and saves it to the current directory
87+ Downloads a remote dataset with name `url` from a remote location and saves it to the current directory.
88+ If download fails, we make `maxattempts` attempts before giving up.
8289
8390Example
8491====
@@ -89,14 +96,28 @@ julia> download_data(url)
8996```
9097
9198"""
92- function download_data (url:: String , local_filename= " temp.dat" ; dir= pwd () )
99+ function download_data (url:: String , local_filename= " temp.dat" ; dir= pwd (), maxattempts = 5 )
93100
94101 if ! contains (url," http" )
95102 @warn " the url does not contain http; please double check that it worked"
96103 end
97104
98- # download remote file to a local temporary directory
99- file_ext = Downloads. download (url, joinpath (dir,local_filename))
105+ # download remote file to a local temporary directory
106+ file_ext = [];
107+ attempt = 0
108+ while attempt< maxattempts
109+ try
110+ file_ext = Downloads. download (url, joinpath (dir,local_filename))
111+ break
112+ catch
113+ @warn " Failed downloading data on attempt $attempt /$maxattempts "
114+ sleep (5 ) # wait a few sec
115+ end
116+ attempt += 1
117+ end
118+ if isempty (file_ext)
119+ error (" Could not download GMT topography data" )
120+ end
100121
101122 return file_ext
102123end
0 commit comments