-
Notifications
You must be signed in to change notification settings - Fork 7
allow for passing directly the python dict string as response body #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
b23000a
25e438a
8c405da
1179149
5aa8218
008458b
99f7e06
1d78857
8ce9d56
493afa7
c1ac9e0
644c110
cd72432
20adf69
297c4b8
9e09ba2
ab44171
a57e82a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,36 +26,85 @@ Suppose that the `Show API request` button generated the following Python code: | |
| ```python | ||
| #!/usr/bin/env python | ||
| import cdsapi | ||
| c = cdsapi.Client() | ||
| c.retrieve("insitu-glaciers-elevation-mass", | ||
| { | ||
| "variable": "all", | ||
| "product_type": "elevation_change", | ||
| "file_version": "20170405", | ||
| "format": "tgz" | ||
| }, | ||
| "download.tar.gz") | ||
|
|
||
| dataset = "reanalysis-era5-single-levels" | ||
| request = { | ||
| "product_type": ["reanalysis"], | ||
| "variable": [ | ||
| "10m_u_component_of_wind", | ||
| "10m_v_component_of_wind" | ||
| ], | ||
| "year": ["2024"], | ||
| "month": ["12"], | ||
| "day": ["06"], | ||
| "time": ["16:00"], | ||
| "data_format": "netcdf", | ||
| "download_format": "unarchived", | ||
| "area": [58, 6, 55, 9] | ||
| } | ||
|
|
||
| client = cdsapi.Client() | ||
| client.retrieve(dataset, request).download() | ||
| ``` | ||
|
|
||
| You can obtain the same results in Julia with the following code: | ||
| You can obtain the same results in Julia by simply copying the request python dict and wrap it into a multiline string: | ||
|
|
||
| ```julia | ||
| using CDSAPI | ||
|
|
||
| CDSAPI.retrieve("insitu-glaciers-elevation-mass", | ||
| CDSAPI.py2ju(""" | ||
| { | ||
| "variable": "all", | ||
| "product_type": "elevation_change", | ||
| "file_version": "20170405", | ||
| "format": "tgz" | ||
| } | ||
| """), | ||
| "download.tar.gz") | ||
| dataset = "reanalysis-era5-single-levels" | ||
| request = """{ | ||
| "product_type": ["reanalysis"], | ||
| "variable": [ | ||
| "10m_u_component_of_wind", | ||
| "10m_v_component_of_wind" | ||
| ], | ||
| "year": ["2024"], | ||
| "month": ["12"], | ||
| "day": ["06"], | ||
| "time": ["16:00"], | ||
| "data_format": "netcdf", | ||
| "download_format": "unarchived", | ||
| "area": [58, 6, 55, 9] | ||
| }""" #<- notice the multiline string. | ||
|
|
||
| CDSAPI.retrieve(dataset, request, "download.nc") | ||
| ``` | ||
| We've copied/pasted the python request dictionary and simply wrapped it in a multiline string (the triple quote `"""`) and the `retrieve` function | ||
| will do the rest. | ||
ghyatzo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Python dictionaries can be valid JSON strings, and the CDS requests builder is kind enough to make it so, (make sure the request string does not contain single quotes, but only double quotes) | ||
juliohm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Therefore simply calling `JSON.parse(request)` will return a valid Julia dictionary. This is done for you automatically as the call above will be translated into `CDSAPI.retrieve(dataset, JSON.parse(request), "download.nc")`. | ||
|
||
|
|
||
| The previous method of calling `py2ju` is deprecated, but still available. | ||
|
|
||
| The `retrieve` function also accepts a `Dict` directly in case one needs to manipulate a base request: | ||
| ```julia | ||
| using CDSAPI | ||
|
|
||
| dataset = "reanalysis-era5-single-levels" | ||
| request = """{ | ||
| "product_type": ["reanalysis"], | ||
| "variable": [ | ||
| "10m_u_component_of_wind", | ||
| "10m_v_component_of_wind" | ||
| ], | ||
| "year": ["2024"], | ||
| "month": ["12"], | ||
| "day": ["06"], | ||
| "time": ["16:00"], | ||
| "data_format": "netcdf", | ||
| "download_format": "unarchived", | ||
| "area": [58, 6, 55, 9] | ||
| }""" | ||
|
|
||
| request_dict = JSON.parse(request) | ||
|
|
||
| We've copied/pasted the code and called the `py2ju` function on the second argument of the `retrieve` function. | ||
| The `py2ju` function simply converts the string containing a Python dictionary to an actual Julia dictionary. | ||
| #= ... Some operations with request_dict ... =# | ||
|
|
||
| CDSAPI.retrieve(dataset, request_dict, "download.nc") | ||
|
|
||
| ``` | ||
|
|
||
| Besides the downloaded file, the `retrieve` function also returns a dictionary with metadata: | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.