Skip to content

Commit b4e352e

Browse files
authored
Use gmtread instead of Downloads to fix the download slowness/hanging problem. (#1887)
1 parent 795863d commit b4e352e

File tree

1 file changed

+16
-38
lines changed

1 file changed

+16
-38
lines changed

src/analemma.jl

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -233,46 +233,24 @@ enso() # Plot ENSO index
233233
"""
234234
function enso(; data::Bool=false, data0::Bool=false, kwargs...)
235235

236-
# Fetch data
237-
try
238-
resp = Downloads.download("https://www.cpc.ncep.noaa.gov/data/indices/oni.ascii.txt")
239-
catch e
240-
@warn("Failed to download ENSO data from NOAA: $(e)")
241-
return nothing
242-
end
243-
lines = readlines(resp)
244-
245-
year, vals = Float64[], Float64[]
246-
247-
# ONI format: SEAS YR TOTAL ANOM
248-
# e.g.: DJF 1950 24.72 -1.53
249-
month_map = Dict("DJF"=>1, "JFM"=>2, "FMA"=>3, "MAM"=>4, "AMJ"=>5, "MJJ"=>6,
250-
"JJA"=>7, "JAS"=>8, "ASO"=>9, "SON"=>10, "OND"=>11, "NDJ"=>12)
251-
252-
for line in lines
253-
contains(line, "SEAS") && continue # Skip header
254-
isempty(strip(line)) && continue
255-
parts = split(line)
256-
length(parts) < 4 && continue
257-
258-
try
259-
season = String(parts[1])
260-
yr = parse(Float64, parts[2])
261-
anom = parse(Float64, parts[4])
262-
263-
# Convert to decimal year
264-
mon = get(month_map, season, 1)
265-
dec_year = yr + (mon - 0.5) / 12
266-
267-
push!(year, dec_year)
268-
push!(vals, anom)
269-
catch
270-
continue
271-
end
236+
# Incredibly, Downloads.download("https://www.cpc.ncep.noaa.gov/data/indices/oni.ascii.txt")
237+
# hangs most of times or is very slow, but gmtread that also uses curl is fast and works fine!
238+
# The file, however, has this struct: SEAS YR TOTAL ANOM, where SEAS is a 3-letter code for
239+
# overlapping 3-month seasons (DJF, JFM, FMA, etc). Being a text we must resort to use -fa but
240+
# that looses the first column (SEAS), so we read only YR and ANOM (cols 1 and 3 in -fa mode).
241+
# We then reconstruct the decimal year from the row number.
242+
opt_i = data ? "1,3" : "0,1,3"
243+
D = gmtread("https://www.cpc.ncep.noaa.gov/data/indices/oni.ascii.txt", h=1, f=:a, i=opt_i)
244+
if (data)
245+
for k = 1:size(D, 1) D[k, 1] += (rem(k,12) - 0.5) / 12 end
246+
else
247+
for k = 1:size(D, 1) D[k, 1] = D[k, 2] + (rem(k,12) - 0.5) / 12; D[k, 2] = 0.0 end
272248
end
273-
rm(resp)
249+
250+
for k = 12:12:size(D, 1) D[k, 1] = D[k-1, 1] + 1.0/12 end # Because rem(k,12) = 0 for decembers and the -0.5 we receeded 1 year
251+
D.ds_bbox = D.bbox = data ? [extrema(view(D,:,1))..., D.ds_bbox[end-1:end]...] : # Must update the bbox's
252+
[extrema(view(D,:,1))..., 0.0, 0.0, D.ds_bbox[end-1:end]...]
274253

275-
D = data ? mat2ds([year vals]) : mat2ds([year zeros(length(year)) vals])
276254
setdecyear_time!(D) # First column is decimal year, make a Time column
277255
D.colnames[data ? 2 : 3] = "ONI" # Will be wrong for plotting but in that case we don't care
278256

0 commit comments

Comments
 (0)