This is tool is useful for those who want to extract large volumes of data from DataStream. This R package allows users to call to the DataStream Public API using built-in R functions and specific search queries. The package includes several functions which accept a selection of filtering queries and then return a dataframe with the desired data from DataStream.
You might use this tool, for example, if you:
- Want to download all available DataStream pH data in Ontario
- Want to count how many sites in New Brunswick have cesium data on DataStream
Note: DataStream's Custom Download tool is another option that allows users to download csv data from across datasets in a particular DataStream hub using basic filters. This tool has fewer filtering options than the API, but works well for basic searches. You can find it via 'Explore Data' in the header menu from any DataStream regional hub.
To have full API permissions, users must request an API token which is required to call to the API
To install the most recent version in R:
# install.packages("devtools")
devtools::install_github("datastreamapp/datastreamr")
Thank you ahead of time for using this data responsibly and providing the appropriate citations when necessary when presenting work to external parties. These dataset citations must be accompanied by a link to the DOI (https://doi.org/{value}). The dataset licence, citation, and DOI can be retrieved from the /Metadata
endpoint.
The API returns the URL for a dataset's licence, these should be mapped to the full licence name with a link to the full licence details.
Attribution Licence
:- EN: Attribution Licence (ODC-By) v1.0
- FR: Licence d'attribution (ODC-By) v1.0
- URL: https://opendatacommons.org/licenses/by/1-0/
Public Domain Dedication and Licence
:- EN: Public Domain Dedication and Licence (ODC-PDDL) v1.0
- FR: Dédicace et licence du domaine public (ODC-PDDL) v1.0
- URL: https://opendatacommons.org/licenses/pddl/1-0/
Open Government Licence
:- EN: Open Government Licence (OGL)
- FR: Licence du gouvernement ouvert (OGL)
- URL: Dataset-dependent, entered by data provider (eg. https://open.canada.ca/en/open-government-licence-canada)
The following functions are used to call to the DataStream API and pull desired information.
Description
Pulls only the dataset level metadata information including dataset name, citation, licence, abstract, etc.
Usage
ds_metadata(
api_token,
select = NULL,
filter = NULL,
top = NULL,
count = FALSE
)
Description
Pulls only the location data including Location ID, Location Name, Latitude, and Longitude.
Usage
ds_locations(
api_token,
select = NULL,
filter = NULL,
top = NULL,
count = FALSE
)
Description
Pulls data formatted the same as the downloaded DataStream CSV’s including all columns listed in the DataStream schema .
Usage
- This function will take longer than
ds_observations
, but provides all available columns in one request. - Use this function if you aim to pull all location and parameter data in one call
ds_records(
api_token,
select = NULL,
filter = NULL,
top = NULL,
count = FALSE
)
Description
Pulls data in a condensed format that must be joined with other endpoints to create a full dataset with all the DataStream columns. Specifically, location rows are not pulled, instead LocationId
is pulled for each observation and then can be used in combination with ds_locations()
.
Usage
- This function will be quicker than
ds_records
, but if location specifics are needed, needs to be paired withds_locations()
- Use this function either if you are uninterested in specific location coordinates, or in combination with
ds_locations()
when you plan to pull millions of rows of data
ds_observations(
api_token,
select = NULL,
filter = NULL,
top = NULL,
count = FALSE
)
All of the functions above accept query parameters. The ones supported are:
-
api_token: A character string containing your unique API key
- Click here to request an api token
- Click here to request an api token
-
select: A list of allowable columns to return
- Fields to be selected are entered as a list.
- Example:
select=c("DatasetName","Abstract")
- Default: All columns available.
-
filter: A list of conditions to filter by
- Available filters:
=, <, >, <=, >=, !=
- Grouping:
filter=c("CharacteristicName=Dissolved oxygen saturation", "DOI='10.25976/n02z-mm23'")
- Temporal (Dataset creation):
filter=c("CreateTimestamp>2020-03-23")
- Temporal (Data date-range):
filter=c("ActivityStartYear<1990")
- Spatial:
filter=c(RegionId=hub.atlantic)
RegionId
Values (these values are subject to change):- DataStream Hubs:
hub.{atlantic,lakewinnipeg,mackenzie,greatlakes,pacific }
- Countries:
admin.2.{ca}
- Provinces/Territories:
admin.4.ca.{ab,bc,mb,nb,nl,ns,nt,nu,on,pe,qc,sk,yt}
- Available filters:
-
top: Number of rows to return
- Maximum: 10000
- Example:
top=10
-
count: When TRUE, returns number of observations rather than the data itself
- Return only the count for the request. When the value is large enough it becomes an estimate (~0.0005% accurate)
- Example:
count=TRUE
- Default:
FALSE
- Use
select
to request only the parameters you need. This will decrease the amount of data needed to process and transfer.
The allowed select
and filter
options for each of the functions are listed HERE.
filter
field, a useful resource is the "allowed values" tab of our upload template . This will give you available strings for:
-
MonitoringLocationType
-
ActivityMediaName
-
CharacteristicName
Get the citation and licence for a dataset:
ds_metadata(api_token,filter=c("DOI='10.25976/1q5q-zy55'"), select=c("DOI","DatasetName","Licence","Citation","Version"))
Get all pH
observations in Alberta
:
ds_records(api_token,filter=c("CharacteristicName='pH'", "RegionId='admin.4.ca.ab'"))
More Examples:
# Pull all metadata for all datasets in the Atlantic DS Hub
Example01 = ds_metadata(api_token=key,
filter=c("RegionId='hub.atlantic'"))
# Pull all metadata for all datasets in BC
Example02 = ds_metadata(api_token=key,
filter=c("RegionId='admin.4.ca.bc'"))
# Pull only the DOI's and contact emails for all datasets in the Great Lakes Hub
Example03 = ds_metadata(api_token=key,
filter=c("RegionId='hub.greatlakes'"),
select=c('DOI','DataStewardEmail'))
# Pull all location information for sites in Ontario
Example04 = ds_locations(api_token=key,
filter=c("RegionId='admin.4.ca.on'"))
# Pull the site names and lat/lon coordinates for a particular dataset
Example05 = ds_locations(api_token=key,
filter=c("DOI='10.25976/1q5q-zy55'"),
select=c("Name","Latitude","Longitude"))
# Pull all ph data available in the Atlantic DS Hub (only pulling top 1000)
Example06 = ds_records(api_token=key,
filter = c("RegionId='hub.atlantic'","CharacteristicName='pH"),
top=1000)
# Now, only select desired columns
Example07 = ds_records(api_token=key,
filter = c("RegionId='hub.atlantic'","CharacteristicName='pH"),
select = c('DOI','DatasetName','MonitoringLocationName','MonitoringLocationLatitude',
'MonitoringLocationLongitude','ActivityStartDate','ResultValue','ResultUnit'),
top=1000)
# Now, only pull data before 2015
Example08 = ds_records(api_token=key,
filter = c("RegionId='hub.atlantic'","CharacteristicName='pH","ActivityStartYear<2015"),
select = c('DOI','DatasetName','MonitoringLocationName','MonitoringLocationLatitude',
'MonitoringLocationLongitude','ActivityStartDate','ResultValue','ResultUnit'),
top=1000)
# Try ds_observations()
Example09 = ds_observations(api_token=key,
select=c("ResultValue"),
filter=c("CharacteristicName = pH","ActivityStartYear>2019"),
top=1000)
# Use the count filter
Example10 = ds_observations(api_token=key,
select=c("ResultValue"),
filter=c("RegionId = 'hub.atlantic'","CharacteristicName = 'Ammonia'","ActivityStartYear>2019"),
count = TRUE)