Skip to content

Commit ed3f211

Browse files
committed
Update CDS retreival to new API
1 parent 4aafbdb commit ed3f211

File tree

4 files changed

+32
-36
lines changed

4 files changed

+32
-36
lines changed

Project.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
name = "CDSAPI"
22
uuid = "8a7b9de3-9c00-473e-88b4-7eccd7ef2fea"
33
authors = ["Micky Yun Chan <michan@redhat.com> and contributors"]
4-
version = "1.0.1"
4+
version = "2.0.0"
55

66
[deps]
7-
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
87
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
98
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
109

src/CDSAPI.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module CDSAPI
22

33
using HTTP
44
using JSON
5-
using Base64
65

76
"""
87
retrieve(name, params, filename; max_sleep = 120.)
@@ -14,45 +13,46 @@ directory as `filename`.
1413
The client periodically requests the status of the retrieve request.
1514
`max_sleep` is the maximum time (in seconds) between the status updates.
1615
"""
17-
function retrieve(name, params, filename; max_sleep = 120.)
16+
function retrieve(name, params, filename; max_sleep=120.0)
1817
creds = Dict()
19-
open(joinpath(homedir(),".cdsapirc")) do f
18+
open(joinpath(homedir(), ".cdsapirc")) do f
2019
for line in readlines(f)
21-
key, val = strip.(split(line,':', limit=2))
20+
key, val = strip.(split(line, ':', limit=2))
2221
creds[key] = val
2322
end
2423
end
2524

26-
apikey = string("Basic ", base64encode(creds["key"]))
2725
response = HTTP.request(
2826
"POST",
29-
creds["url"] * "/resources/$name",
30-
["Authorization" => apikey],
31-
body=JSON.json(params),
27+
creds["url"] * "/retrieve/v1/processes/$name/execute/",
28+
["PRIVATE-TOKEN" => creds["key"]],
29+
body=JSON.json(Dict("inputs" => params)),
3230
verbose=1)
3331

3432
resp_dict = JSON.parse(String(response.body))
35-
data = Dict("state" => "queued")
36-
sleep_seconds = 1.
33+
data = Dict("status" => "queued")
34+
sleep_seconds = 1.0
3735

38-
while data["state"] != "completed"
39-
data = HTTP.request("GET", creds["url"] * "/tasks/" * string(resp_dict["request_id"]), ["Authorization" => apikey])
36+
while data["status"] != "successful"
37+
data = HTTP.request("GET", creds["url"] * "/retrieve/v1/jobs/" * string(resp_dict["jobID"]), ["PRIVATE-TOKEN" => creds["key"]])
4038
data = JSON.parse(String(data.body))
41-
println("request queue status ", data["state"])
39+
println("request queue status ", data["status"])
4240

43-
if data["state"] == "failed"
41+
if data["status"] == "failed"
4442
error("Request to dataset $name failed. Check " *
4543
"https://cds.climate.copernicus.eu/cdsapp#!/yourrequests " *
4644
"for more information (after login).")
4745
end
4846

49-
sleep_seconds = min(1.5 * sleep_seconds,max_sleep)
50-
if data["state"] != "completed"
47+
sleep_seconds = min(1.5 * sleep_seconds, max_sleep)
48+
if data["status"] != "successful"
5149
sleep(sleep_seconds)
5250
end
5351
end
5452

55-
HTTP.download(data["location"], filename)
53+
response = HTTP.request("GET", creds["url"] * "/retrieve/v1/jobs/" * string(resp_dict["jobID"]) * "/results/", ["PRIVATE-TOKEN" => creds["key"]])
54+
body = JSON.parse(String(response.body))
55+
HTTP.download(body["asset"]["value"]["href"], filename)
5656
return data
5757
end
5858

@@ -88,7 +88,7 @@ function py2ju(dictstr)
8888
# if there's no pair after the last comma
8989
if findnext(":", dictstr_cpy, lastcomma_pos) == nothing
9090
# remove the comma
91-
dictstr_cpy = dictstr_cpy[firstindex(dictstr_cpy):(lastcomma_pos - 1)] * dictstr_cpy[(lastcomma_pos + 1):lastindex(dictstr_cpy)]
91+
dictstr_cpy = dictstr_cpy[firstindex(dictstr_cpy):(lastcomma_pos-1)] * dictstr_cpy[(lastcomma_pos+1):lastindex(dictstr_cpy)]
9292
end
9393

9494
# removes trailing comma from a list

test/py2ju.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@testset "Py2Ju" begin
22
pydict_str = """{
3-
'format': 'grib',
3+
'data_format': 'grib',
44
'product_type': 'monthly_averaged_reanalysis',
55
'variable': 'divergence',
66
'pressure_level': '1',
@@ -12,14 +12,14 @@
1212
],
1313
'time': '00:00',
1414
}"""
15-
julia_dict = Dict("format"=> "grib",
16-
"month" => "06",
17-
"time" => "00:00",
18-
"year" => "2020",
19-
"pressure_level" => "1",
20-
"area" => Any[90, -180, -90, 180],
21-
"product_type" => "monthly_averaged_reanalysis",
22-
"variable" => "divergence")
15+
julia_dict = Dict("data_format" => "grib",
16+
"month" => "06",
17+
"time" => "00:00",
18+
"year" => "2020",
19+
"pressure_level" => "1",
20+
"area" => Any[90, -180, -90, 180],
21+
"product_type" => "monthly_averaged_reanalysis",
22+
"variable" => "divergence")
2323
py2ju_result = CDSAPI.py2ju(pydict_str)
2424

2525
@test typeof(py2ju_result) <: Dict

test/retrieve.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
@testset "Retrieve" begin
2-
datadir = joinpath(@__DIR__,"data")
2+
datadir = joinpath(@__DIR__, "data")
33

44
@testset "ERA5 monthly preasure data" begin
55
filepath = joinpath(datadir, "era5.grib")
66
response = CDSAPI.retrieve("reanalysis-era5-pressure-levels-monthly-means",
77
CDSAPI.py2ju("""{
8-
'format': 'grib',
8+
'data_format': 'grib',
99
'product_type': 'monthly_averaged_reanalysis',
1010
'variable': 'divergence',
1111
'pressure_level': '1',
@@ -20,7 +20,6 @@
2020
filepath)
2121

2222
@test typeof(response) <: Dict
23-
@test response["content_type"] == "application/x-grib"
2423
@test isfile(filepath)
2524

2625
GribFile(filepath) do datafile
@@ -43,12 +42,11 @@
4342
'emissions_scenario': 'rcp_2_6',
4443
'period': '2071_2100',
4544
'return_period': '100',
46-
'format': 'zip',
45+
'data_format': 'zip',
4746
}"""),
4847
filepath)
4948

5049
@test typeof(response) <: Dict
51-
@test response["content_type"] == "application/zip"
5250
@test isfile(filepath)
5351

5452
# extract contents
@@ -76,12 +74,11 @@
7674
'time_aggregation': '1_year_average',
7775
'vertical_level': '0_m',
7876
'bias_correction': 'bias_adjustment_based_on_gamma_distribution',
79-
'format': 'tgz',
77+
'data_format': 'tgz',
8078
}"""),
8179
filepath)
8280

8381
@test typeof(response) <: Dict
84-
@test response["content_type"] == "application/gzip"
8582
@test isfile(filepath)
8683

8784
# extract contents

0 commit comments

Comments
 (0)