Code Examples
This page provides quick coding examples for searching for datasets on ESS-DIVE using Python, R, and Java.
Setup
The following code examples require installation of certain packages and authentication from your ESS-DIVE account. Follow the instructions for setting up the Dataset API for your preferred coding language before trying out the search code examples:
Setup and TroubleshootSearch for Datasets
Anyone can search for public datasets on ESS-DIVE using the Dataset API. If you are registered to submit data, you can also search for your private datasets. Query your dataset searches by defining parameters.
Limited dataset metadata are returned in the response of this call. Additionally, this call cannot be used to download data files. To look up all dataset metadata and download data files, use the API to Download Datasets.
👉 Review parameters and responses for this call in the Dataset API technical documentation.
The following lines of code will return the most recent 25 records. If the results contain more than 25 packages, use the rowStart and pageSize query parameters to page through the results.
For data users: Must pass isPublic=true to search for public datasets. This call searches for public datasets by default.
For data contributors: Must pass isPublic=false to search for private datasets.
Python Search Example
⭐ Make sure you have followed the Setup instructions before running this code
Search for datasets using any of the available query parameters. The code example below demonstrates how to format the search query with all parameters. It also demonstrates how to search for one parameter, in this case providerName or the publishing project.
# Enter parameters
creator= "creator/submitter of datasets"
providerName = ""\"Next-Generation Ecosystem Experiments (NGEE) Arctic"\" # Use exact match formatting to return datasets from only one project
text= "any text"
datePublished = "[YYYY-MM-DD]"
keywords = "Partial Match Keywords" # OR ""\"Exact Match Keywords"\"# Contruct query URL
get_packages_url = "{}/{}?creator=\"{}\"&providerName=\"{}\"&text=\"{}\"&datePublished=\"{}\"&keywords=\"{}\"&isPublic=true".format(base,endpoint,creator,providerName,text,datePublished,keywords)
## Not interested in using all the parameters? Just remove them from the query like so
## This example only uses the "providerName" parameter:
# get_packages_url = "{}/{}?providerName=\"{}\"&isPublic=true".format(base,endpoint,providerName)# Call GET /packages
get_packages_response = requests.get(get_packages_url,
headers={"Authorization":header_authorization})
if get_packages_response.status_code == 200:
#Success
print(get_packages_response.json())
else:
# There was an error
print(get_packages_response.text)R Search Example
⭐ Make sure you have followed the Setup instructions before running this code
Search for datasets using any of the available query parameters. At this time, this code example does not demonstrate how to format the query parameters. This example will return all public datasets on ESS-DIVE.
Java Search Example
⭐ Make sure you have followed the Setup instructions before running this code
Search for datasets using any of the available query parameters. At this time, this code example does not demonstrate how to format the query parameters. This example will return all public datasets on ESS-DIVE.
Download Dataset
Anyone can search for individual public datasets on ESS-DIVE using the Dataset API. If you are registered to submit data, you can also download your private dataset metadata.
This call will look up one dataset and return complete dataset metadata and file details. Metadata and data files can then be downloaded using standard request packages.
If you'd like to look up the dataset upload date, last modified date, or dataset access status, use the API to Search for Datasets.
👉 Review responses for this call in the Dataset API technical documentation.
R Download Example
⭐ Make sure you have followed the Setup instructions before running this code
Download Metadata
Download Data Files
The Dataset API search result will provide direct URLs that point to the data files. To download data files, you will need to grab the contentUrl and download it locally using a standard requests package.
and use a standard request or urlretrive package to download it locally. The data file URLs are stored in the JSON as such:
Java Download Example
⭐ Make sure you have followed the Setup instructions before running this code
Download Metadata
Download Data Files
The Dataset API search result will provide direct URLs that point to the data files. To download data files, you will need to grab the contentUrl and download it locally using a standard requests package.
and use a standard request or urlretrive package to download it locally. The data file URLs are stored in the JSON as such:
At the end of the document, don’t forget to close the class and main function blocks by adding two curly braces if you hadn’t done that already.
Last updated