Skip to content

Commit

Permalink
Update onc_json_to_dataset() to v3 API and add tests
Browse files Browse the repository at this point in the history
Update the onc_json_to_dataset() function to handle ONC data web service API v3
response JSON schema. Also add unit tests for the function.
  • Loading branch information
douglatornell committed Jan 19, 2024
1 parent 9e5fe54 commit 5525782
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 84 deletions.
19 changes: 10 additions & 9 deletions SalishSeaTools/salishsea_tools/data_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,12 @@ def onc_json_to_dataset(onc_json, teos=True):
:arg dict onc_json: Data structure returned from an ONC data web service
API request.
Typically produces by calling the :py:meth:`json`
Typically produced by calling the :py:meth:`json`
method on the :py:class:`~requests.Response` object
produced by calling :py:meth:`requests.get`.
:arg boolean teos: Convert salinity data from PSU
(Practical Salinity Units) to TEOS-10 reference
(Practical Salinity Units) to TEOS-10 reference
salinity in g/kg.
Defaults to :py:obj:`True`.
Expand All @@ -357,28 +357,29 @@ def onc_json_to_dataset(onc_json, teos=True):
data_vars = {}
for sensor in onc_json["sensorData"]:
if sensor["sensorName"] == "Practical Salinity" and teos:
data = teos_tools.psu_teos([d["value"] for d in sensor["data"]])
data = teos_tools.psu_teos([d for d in sensor["data"]["values"]])
sensor["sensorName"] = "Reference Salinity"
sensor["unitOfMeasure"] = "g/kg"
else:
data = [d["value"] for d in sensor["data"]]
data_vars[sensor["sensor"]] = xarray.DataArray(
name=sensor["sensor"],
data = [d for d in sensor["data"]["values"]]
sensor_code = sensor["sensorCode"].lower()
data_vars[sensor_code] = xarray.DataArray(
name=sensor_code,
data=data,
coords={
"sampleTime": [
arrow.get(d["sampleTime"]).naive for d in sensor["data"]
arrow.get(d).naive for d in sensor["data"]["sampleTimes"]
],
},
dims=("sampleTime",),
attrs={
"qaqcFlag": np.array([d["qaqcFlag"] for d in sensor["data"]]),
"qaqcFlag": np.array([d for d in sensor["data"]["qaqcFlags"]]),
"sensorName": sensor["sensorName"],
"unitOfMeasure": sensor["unitOfMeasure"],
"actualSamples": sensor["actualSamples"],
},
)
return xarray.Dataset(data_vars, attrs=onc_json["serviceMetadata"])
return xarray.Dataset(data_vars)


def get_chs_tides(
Expand Down
Loading

0 comments on commit 5525782

Please sign in to comment.