After installing the client library, the classes from the onc package will be available to your Python 3 scripts. An instance of the ONC class contains the methods you can use to consume our discovery, data product download, archive file download and near real-time data services.
All code examples in the documentation require you to replace 'YOUR_TOKEN_HERE' with a token of your own.
1. Obtaining a token
All requests require a unique Oceans 2.0 API token that authorizes you to access the data. If you don't have a token yet, generate it as follows:
- Register for an Oceans 2.0 account at https://data.oceannetworks.ca/Registration
- Log-in into your account at https://data.oceannetworks.ca by clicking the Login link
- Click the Profile link to access your account profile
- Access the Web Services API tab and click "Generate Token".
2. Searching with discovery methods
To download ONC data, first you need to specify the type of data you require, where it was obtained from, and in which date range.
In the Oceans 2.0 API, there's a unique code that identifies every location, device, property, data product type, etc. You include these codes in a set of filters that determine the data you're interested in.
Discovery methods allow you to explore the hierarchy of the ONC database to obtain the codes for your filters (they work like a "search" function). As an example, the code below uses the getLocations method to search for locations that include "Burrard" in their name (i.e. "Burrard Inlet"):
onc.print() method to print any result returned by the client library in a format easier to read.
You can find more information on this function at the Utility methods documentation.
The previous code prints a list with a location that matches the search filters provided; this location includes a "
locationCode" that can be used to continue searching "inside" it, as in the following examples:
3. Downloading data products
Once you determine the exact dictionary of filters that identifies the data you are interested in, there are multiple ways of downloading it. One of them is to request the ONC servers to generate a custom data product with the data; This is done through the data product download methods.
The following example downloads two PNG files with plots for 30 seconds of data from a CTD in Campbell River:
The filters above include codes for location, deviceCategory and dataProduct, as well as the file extension and a time interval (in UTC). They also include a couple of filters to configure this specific data product type (starting with the
"dpo_" prefix) which can be obtained from the Data Product Options documentation. You can download more than 120 different types of data products including audio & video.
4. Obtaining sensor readings in (near) real-time
Another method to obtain ONC data is by directly obtaining time-series of sensor readings (available as soon as they reach our database).
In the following example, we obtain 5 seconds of conductivity readings from the CTD at Burrard Inlet:
The result includes matching lists of "
values" and "
sampleTimes" (increases performance for long time ranges). We also use the property code "
conductivity" to limit results to a specific property available in this CTD.
5. Downloading archived files
ONC scripts auto-generate and archive data products of different types at set time intervals. You can directly download these data product files from our files archive, as long as you know their unique filename.
In the following example, we get the list of archived files available for a camera at Ridley Island (in a certain timespan), and download one of the files:
You can use the method "
getFile()" as above to download individual files or the method "
getDirectFiles()" to download all the files that match your filters.