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
41 changes: 41 additions & 0 deletions cwms/cwms_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,44 @@ def timeseries_type(orig_json: JSON, value_json: JSON) -> DataFrame:
df["date-time"] = to_datetime(df["date-time"], unit="ms", utc=True)
return df

def reorder_measurement_cols(df: DataFrame) -> DataFrame:
# reorders measurement columns for usability

# Define the columns to bring to the front
front_columns = [
"id.office-id",
"id.name",
"number",
"instant",
"streamflow-measurement.gage-height",
"streamflow-measurement.flow",
"streamflow-measurement.quality",
"used",
"agency",
"wm-comments",
]

# Identify columns containing 'unit' to be last
unit_columns = [col for col in df.columns if "unit" in col]

# Identify remaining columns (not in front_columns or unit_columns)
remaining_columns = [
col
for col in df.columns
if col not in front_columns and col not in unit_columns
]

# Construct the new column order
new_column_order = front_columns + remaining_columns + unit_columns

# Filter out columns that might not actually exist in the DataFrame.
existing_columns = [col for col in new_column_order if col in df.columns]

# Reorder the DataFrame
df = df[existing_columns]

return df

data = deepcopy(json)

if selector:
Expand All @@ -95,6 +133,9 @@ def timeseries_type(orig_json: JSON, value_json: JSON) -> DataFrame:
df = json_normalize(df_data) if df_data else DataFrame()
else:
df = json_normalize(data)
# if streamflow-measurement reorder columns
if "streamflow-measurement.flow" in df.columns:
df = reorder_measurement_cols(df)

return df

Expand Down
36 changes: 34 additions & 2 deletions cwms/measurements/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,15 @@ def store_measurements(
"fail-if-exists": fail_if_exists,
}

if not isinstance(data, dict):
raise ValueError("Cannot store a timeseries without a JSON data dictionary")
if not isinstance(data, list):
raise ValueError(
"Cannot store a measurement without a JSON list, object is not a list of dictionaries"
)
for item in data:
if not isinstance(item, dict):
raise ValueError(
"Cannot store a measurement without a JSON list: a non-dictionary object was found"
)

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

Expand Down Expand Up @@ -175,3 +182,28 @@ def delete_measurements(
}

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


def get_measurements_extents(
office_mask: Optional[str] = None,
) -> Data:
"""Get time extents of streamflow measurements

Parameters
----------
office_mask: string
Office Id used to filter the results.

Returns
-------
cwms data type. data.json will return the JSON output and data.df will return a dataframe. Dates returned are all in UTC.

"""
endpoint = "measurements/time-extents"

params = {
"office-mask": office_mask,
}

response = api.get(endpoint, params, api_version=1)
return Data(response) # , selector=selector)
Loading