Skip to content

Commit 283230c

Browse files
committed
feat: add data availability api, test case and documentation
1 parent 81d3945 commit 283230c

6 files changed

Lines changed: 165 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Discover Data Availability for Data Products
2+
3+
```python
4+
# Get the token from your Oceans 3.0 profile page
5+
from onc import ONC
6+
7+
onc = ONC("YOUR_TOKEN")
8+
```
9+
10+
## [/dataAvailability/dataproducts](https://data.oceannetworks.ca/OpenAPI#get-/dataAvailability/dataproducts)
11+
12+
### Get data availability from a specific location and a device category
13+
14+
Return which data products are available with _deviceCategoryCode_ "**BPR**" at location Barkley Upper Slope (
15+
_locationCode_:"**NCBC**").
16+
17+
```python
18+
19+
params = {
20+
"deviceCategoryCode": "BPR",
21+
"locationCode": "NCBC",
22+
}
23+
onc.getDataAvailability(params)
24+
```
25+
26+
### Get data availability from a specific device with a specific extension
27+
28+
Return which data products are available with _deviceCode_ "**BPR_BC**" and extension "**raw**"
29+
30+
```python
31+
32+
params = {
33+
"deviceCode": "BPR_BC",
34+
"extension": "raw",
35+
}
36+
onc.getDataAvailability(params)
37+
```

doc/source/Code_Examples/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Discover_Deployments.ipynb
1212
Discover_Device_Categories.ipynb
1313
Discover_Properties.ipynb
1414
Discover_Data_Products.ipynb
15+
Discover_Data_Availability.ipynb
1516
Download_Data_Products.ipynb
1617
Request_Real_Time_Data.ipynb
1718
Download_Archived_Files.ipynb

src/onc/modules/_OncDiscovery.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ def getProperties(self, filters: dict):
4646
def getDataProducts(self, filters: dict):
4747
filters = filters or {}
4848
return self._discoveryRequest(filters, service="dataProducts")
49+
50+
def getDataAvailability(self, filters: dict):
51+
filters = filters or {}
52+
return self._discoveryRequest(filters, service="dataAvailability/dataproducts")
4953

5054
def _sanitizeBooleans(self, data: list):
5155
"""

src/onc/modules/_OncService.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def _serviceUrl(self, service: str):
104104
"archivefile",
105105
"scalardata",
106106
"rawdata",
107+
"dataAvailability/dataproducts",
107108
]:
108109
return f"{self._config('baseUrl')}api/{service}"
109110

src/onc/onc.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,78 @@ def getDataProducts(self, filters: dict | None = None):
794794
]
795795
""" # noqa: E501
796796
return self.discovery.getDataProducts(filters)
797+
798+
def getDataAvailability(self, filters: dict | None = None):
799+
"""
800+
Return information about which data products are available at a given time.
801+
802+
The API endpoint is ``/dataAvailability/dataproducts``.
803+
804+
See https://data.oceannetworks.ca/OpenAPI#get-/dataAvailability/dataproducts
805+
for usage and available filters.
806+
807+
Parameters
808+
----------
809+
filters : dict, optional
810+
Query string parameters in the API request.
811+
812+
Supported parameters are:
813+
814+
- locationCode
815+
- deviceCategoryCode
816+
- deviceCode
817+
- propertyCode
818+
- dateFrom
819+
- dateTo
820+
- dataProductCode
821+
- extension
822+
- minimumCoverage
823+
- maximumCoverage
824+
- getLatest
825+
- groupBy
826+
- mergeGaps
827+
- includeEmptyDays
828+
- rowLimit
829+
830+
Returns
831+
-------
832+
list of dict
833+
API response.
834+
835+
Examples
836+
--------
837+
>>> params = {
838+
... "locationCode": "NCBC",
839+
... "deviceCategoryCode": "BPR",
840+
... "dateFrom": "2019-11-23",
841+
... "dateTo": "2019-11-30",
842+
... } # doctest: +SKIP
843+
>>> onc.getDataAvailability(params) # doctest: +SKIP
844+
{
845+
"availableDataProducts": [
846+
{
847+
"averageFileCoverage": 0.875,
848+
"dataProductCode": "LF",
849+
"dateFrom": "2019-11-23T00:00:00.000Z",
850+
"dateTo": "2019-12-01T00:00:00.000Z",
851+
"deviceCode": "BPR-Folger-59",
852+
"extension": "txt",
853+
"fileCount": 7,
854+
"maxFileCoverage": 1.0,
855+
"maxFileCoverageDate": "2019-11-23T00:00:00.000Z",
856+
"minFileCoverage": 0.0,
857+
"minFileCoverageDate": "2019-11-30T00:00:00.000Z",
858+
"totalFileSize": 8707618,
859+
"totalUncompressedFileSize": 33868912,
860+
}
861+
],
862+
"messages": [],
863+
"next": None,
864+
"queryUrl": "https://data.oceannetworks.ca/api/dataAvailability/dataproducts?locationCode=NCBC&deviceCategoryCode=BPR&dateFrom=2019-11-23&dateTo=2019-11-30&token=ONC_TOKEN",
865+
}
866+
867+
""" # noqa: E501
868+
return self.discovery.getDataAvailability(filters)
797869

798870
# Delivery methods
799871

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pytest
2+
import requests
3+
4+
5+
@pytest.fixture
6+
def params():
7+
return {
8+
"locationCode": "NCBC",
9+
"deviceCategoryCode": "BPR",
10+
"dateFrom": "2019-11-23",
11+
"dateTo": "2019-11-30",
12+
}
13+
14+
15+
def test_invalid_param_value(requester, params):
16+
params_invalid_param_value = params | {"locationCode": "INVALID"}
17+
with pytest.raises(requests.HTTPError, match=r"API Error 127"):
18+
requester.getDataAvailability(params_invalid_param_value)
19+
20+
21+
def test_valid_params(requester, params, util):
22+
data = requester.getDataAvailability(params)
23+
24+
expected_keys = {
25+
"availableDataProducts": list,
26+
"messages": list,
27+
"next": None,
28+
"queryUrl": str,
29+
}
30+
31+
expected_keys_available_data_products = {
32+
"averageFileCoverage": float,
33+
"dataProductCode": str,
34+
"dateFrom": str,
35+
"dateTo": str,
36+
"deviceCode": str,
37+
"extension": str,
38+
"fileCount": int,
39+
"maxFileCoverage": float,
40+
"maxFileCoverageDate": str,
41+
"minFileCoverage": float,
42+
"minFileCoverageDate": str,
43+
"totalFileSize": int,
44+
"totalUncompressedFileSize": int,
45+
}
46+
47+
util.assert_dict_key_types(data, expected_keys)
48+
util.assert_dict_key_types(
49+
data["availableDataProducts"][0], expected_keys_available_data_products
50+
)

0 commit comments

Comments
 (0)