Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cwms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from importlib.metadata import PackageNotFoundError, version

from cwms.api import *
from cwms.catalog.blobs import *
from cwms.catalog.catalog import *
from cwms.catalog.clobs import *
from cwms.datafile_imports.shef_critfile_import import *
from cwms.forecast.forecast_instance import *
from cwms.forecast.forecast_spec import *
from cwms.levels.location_levels import *
from cwms.levels.specified_levels import *
from cwms.locations.gate_changes import *
from cwms.locations.location_groups import *
from cwms.locations.physical_locations import *
from cwms.outlets.outlets import *
from cwms.outlets.virtual_outlets import *
Expand All @@ -21,6 +24,7 @@
from cwms.timeseries.timerseries_identifier import *
from cwms.timeseries.timeseries import *
from cwms.timeseries.timeseries_bin import *
from cwms.timeseries.timeseries_group import *
from cwms.timeseries.timeseries_profile import *
from cwms.timeseries.timeseries_profile_instance import *
from cwms.timeseries.timeseries_profile_parser import *
Expand Down
2 changes: 1 addition & 1 deletion cwms/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Session management and REST functions for CWMS Data API.
"""Session management and REST functions for CWMS Data API.

This module provides functions for making REST calls to the CWMS Data API (CDA). These
functions should be used internally to interact with the API. The user should not have to
Expand Down
85 changes: 85 additions & 0 deletions cwms/catalog/blobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from typing import Optional

import cwms.api as api
from cwms.cwms_types import JSON, Data


def get_blob(blob_id: str, office_id: str) -> Data:
"""Get a single clob.

Parameters
----------
blob_id: string
Specifies the id of the blob
office_id: string
Specifies the office of the blob.


Returns
-------
cwms data type. data.json will return the JSON output and data.df will return a dataframe
"""

endpoint = f"blobs/{blob_id}"
params = {"office": office_id}
response = api.get(endpoint, params, api_version=1)
return Data(response)


def get_blobs(
office_id: Optional[str] = None,
page_size: Optional[int] = 100,
blob_id_like: Optional[str] = None,
) -> Data:
"""Get a subset of Blobs

Parameters
----------
office_id: Optional[string]
Specifies the office of the blob.
page_sie: Optional[Integer]
How many entries per page returned. Default 100.
blob_id_like: Optional[string]
Posix regular expression matching against the clob id

Returns
-------
cwms data type. data.json will return the JSON output and data.df will return a dataframe
"""

endpoint = "blobs"
params = {"office": office_id, "page-size": page_size, "like": blob_id_like}

response = api.get(endpoint, params, api_version=1)
return Data(response, selector="blobs")


def store_blobs(data: JSON, fail_if_exists: Optional[bool] = True) -> None:
"""Create New Blob

Parameters
----------
Data: JSON dictionary
JSON containing information of Blob to be updated
{
"office-id": "string",
"id": "string",
"description": "string",
"media-type-id": "string",
"value": "string"
}
fail_if_exists: Boolean
Create will fail if provided ID already exists. Default: true

Returns
-------
None
"""

if not isinstance(data, dict):
raise ValueError("Cannot store a Blob without a JSON data dictionary")

endpoint = "blobs"
params = {"fail-if-exists": fail_if_exists}

return api.post(endpoint, data, params, api_version=1)
158 changes: 158 additions & 0 deletions cwms/catalog/clobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
from typing import Optional

import cwms.api as api
from cwms.cwms_types import JSON, Data


def get_clob(clob_id: str, office_id: str, clob_id_query: Optional[str] = None) -> Data:
"""Get a single clob.

Parameters
----------
clob_id: string
Specifies the id of the clob
office_id: string
Specifies the office of the clob.
clob_id_query: string
If this query parameter is provided the id path parameter is ignored and the
value of the query parameter is used. Note: this query parameter is necessary
for id's that contain '/' or other special characters. Because of abuse even
properly escaped '/' in url paths are blocked. When using this query parameter
a valid path parameter must still be provided for the request to be properly
routed. If your clob id contains '/' you can't specify the clob-id query
parameter and also specify the id path parameter because firewall and/or server
rules will deny the request even though you are specifying this override. "ignored"
is suggested.


Returns
-------
cwms data type. data.json will return the JSON output and data.df will return a dataframe
"""

endpoint = f"clobs/{clob_id}"
params = {
"office": office_id,
"clob-id-query": clob_id_query,
}
response = api.get(endpoint, params)
return Data(response)


def get_clobs(
office_id: Optional[str] = None,
page_size: Optional[int] = 100,
include_values: Optional[bool] = False,
clob_id_like: Optional[str] = None,
) -> Data:
"""Get a subset of Clobs

Parameters
----------
office_id: Optional[string]
Specifies the office of the clob.
page_sie: Optional[Integer]
How many entries per page returned. Default 100.
include_values: Optional[Boolean]
Do you want the value associated with this particular clob (default: false)
clob_id_like: Optional[string]
Posix regular expression matching against the clob id

Returns
-------
cwms data type. data.json will return the JSON output and data.df will return a dataframe
"""

endpoint = "clobs"
params = {
"office": office_id,
"page-size": page_size,
"include-values": include_values,
"like": clob_id_like,
}

response = api.get(endpoint, params)
return Data(response, selector="clobs")


def delete_clob(clob_id: str, office_id: str) -> None:
"""Deletes requested clob

Parameters
----------
clob_id: string
Specifies the id of the clob to be deleted
office_id: string
Specifies the office of the clob.

Returns
-------
None
"""

endpoint = f"clobs/{clob_id}"
params = {"office": office_id}

return api.delete(endpoint, params=params, api_version=1)


def update_clob(data: JSON, clob_id: str, ignore_nulls: Optional[bool] = True) -> None:
"""Updates clob

Parameters
----------
Data: JSON dictionary
JSON containing information of Clob to be updated
{
"office-id": "string",
"id": "string",
"description": "string",
"value": "string"
}
clob_id: string
Specifies the id of the clob to be deleted
ignore_nulls: Boolean
If true, null and empty fields in the provided clob will be ignored and the existing value of those fields left in place. Default: true

Returns
-------
None
"""

if not isinstance(data, dict):
raise ValueError("Cannot store a Clob without a JSON data dictionary")

endpoint = f"clobs/{clob_id}"
params = {"ignore-nulls": ignore_nulls}

return api.patch(endpoint, data, params, api_version=1)


def store_clobs(data: JSON, fail_if_exists: Optional[bool] = True) -> None:
"""Create New Clob

Parameters
----------
Data: JSON dictionary
JSON containing information of Clob to be updated
{
"office-id": "string",
"id": "string",
"description": "string",
"value": "string"
}
fail_if_exists: Boolean
Create will fail if provided ID already exists. Default: true

Returns
-------
None
"""

if not isinstance(data, dict):
raise ValueError("Cannot store a Clob without a JSON data dictionary")

endpoint = "clobs"
params = {"fail-if-exists": fail_if_exists}

return api.post(endpoint, data, params, api_version=1)
16 changes: 10 additions & 6 deletions cwms/datafile_imports/shef_critfile_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

import pandas as pd

from cwms.timeseries.timeseries import (
timeseries_group_df_to_json,
update_timeseries_groups,
)
import cwms


def import_critfile_to_ts_group(
Expand All @@ -15,6 +12,7 @@ def import_critfile_to_ts_group(
group_id: str = "SHEF Data Acquisition",
category_id: str = "Data Acquisition",
group_office_id: str = "CWMS",
category_office_id: str = "CWMS",
replace_assigned_ts: bool = False,
) -> None:
"""
Expand Down Expand Up @@ -116,9 +114,15 @@ def append_df(
df = append_df(df, office_id, data["Timeseries ID"], data["Alias"])

# Generate JSON dictionary
json_dict = timeseries_group_df_to_json(df, group_id, group_office_id, category_id)
json_dict = cwms.timeseries_group_df_to_json(
data=df,
group_id=group_id,
group_office_id=group_office_id,
category_office_id=category_office_id,
category_id=category_id,
)

update_timeseries_groups(
cwms.update_timeseries_groups(
group_id=group_id,
office_id=office_id,
replace_assigned_ts=replace_assigned_ts,
Expand Down
Loading