Skip to content

Commit 41985ce

Browse files
committed
make several attempts in case download fails
1 parent 4aaeeb8 commit 41985ce

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

ext/GMT_utils.jl

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ println("Loading GMT routines within GMG")
1818

1919

2020
"""
21-
Topo = ImportTopo(limits; file::String="@earth_relief_01m.grd")
21+
Topo = ImportTopo(limits; file::String="@earth_relief_01m.grd", maxattempts=5)
2222
23-
Uses `GMT` to download the topography of a certain region, specified with limits=[lon_min, lon_max, lat_min, lat_max]
23+
Uses `GMT` to download the topography of a certain region, specified with limits=[lon_min, lon_max, lat_min, lat_max].
24+
Sometimes download fails because of the internet connection. We do `maxattempts` to download it.
2425
2526
Note:
2627
====
@@ -64,7 +65,7 @@ julia> Write_Paraview(Topo,"Topo_Alps")
6465
"Topo_Alps.vts"
6566
```
6667
"""
67-
function ImportTopo(limits; file::String="@earth_relief_01m.grd")
68+
function ImportTopo(limits; file::String="@earth_relief_01m.grd", maxattempts=5)
6869

6970
# Correct if negative values are given (longitude coordinates that are west)
7071
ind = findall(limits[1:2] .< 0);
@@ -74,8 +75,22 @@ function ImportTopo(limits; file::String="@earth_relief_01m.grd")
7475
limits[1:2] = sort(limits[1:2])
7576
end
7677

77-
# Download topo file
78-
G = gmtread(file, limits=limits, grid=true);
78+
# Download topo file - add a few attempts to do so
79+
G = [];
80+
attempt = 0
81+
while attempt<maxattempts
82+
try
83+
G = gmtread(file, limits=limits, grid=true);
84+
break
85+
catch
86+
@warn "Failed downloading GMT topography on attempt $attempt/$maxattempts"
87+
sleep(5) # wait a few sec
88+
end
89+
attempt += 1
90+
end
91+
if isempty(G)
92+
error("Could not download GMT topography data")
93+
end
7994

8095
# Transfer to GeoData
8196
nx,ny = size(G.z,2), size(G.z,1)
@@ -88,7 +103,7 @@ function ImportTopo(limits; file::String="@earth_relief_01m.grd")
88103
end
89104

90105
"""
91-
ImportTopo(; lat::Vector{2}, lon::Vector{2}, file::String="@earth_relief_01m.grd")
106+
ImportTopo(; lat::Vector{2}, lon::Vector{2}, file::String="@earth_relief_01m.grd", maxattempts=5)
92107
93108
Imports topography (using GMT), by specifying keywords for latitude and longitude ranges
94109
@@ -103,7 +118,7 @@ julia> Topo = ImportTopo(lon=(-50, -40), lat=(-10,-5), file="@earth_relief_30s.g
103118
```
104119
105120
"""
106-
ImportTopo(; lat=[37,49], lon=[4,20], file::String="@earth_relief_01m.grd") = ImportTopo([lon[1],lon[2], lat[1], lat[2]], file=file)
121+
ImportTopo(; lat=[37,49], lon=[4,20], file::String="@earth_relief_01m.grd", maxattempts=5) = ImportTopo([lon[1],lon[2], lat[1], lat[2]], file=file, maxattempts=maxattempts)
107122

108123

109124
"""

0 commit comments

Comments
 (0)