Skip to content

Commit 130ffbd

Browse files
committed
feat: add data availability api, test case and documentation
1 parent a422c7b commit 130ffbd

File tree

6 files changed

+165
-0
lines changed

6 files changed

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

788860
# Delivery methods
789861

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)