Skip to content

Commit cfb4517

Browse files
authored
Merge branch 'JuliaGeodynamics:main' into main
2 parents 964d20c + 59ec6d4 commit cfb4517

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
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
"""

src/IO.jl

Lines changed: 31 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,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
7379
end
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
8390
Example
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
102123
end

0 commit comments

Comments
 (0)