From 8a9d285019a85fed1dee7c803e1eec6af22284fd Mon Sep 17 00:00:00 2001 From: David Hoese <david.hoese@ssec.wisc.edu> Date: Wed, 15 Mar 2023 14:54:58 -0500 Subject: [PATCH] Fix influxdbv2 bad order when wind direction is present --- metobsapi/data_api.py | 8 ++++++-- metobsapi/tests/test_data_api.py | 8 ++++++++ metobsapi/util/query_influx.py | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/metobsapi/data_api.py b/metobsapi/data_api.py index 7f91c61..f2c7e2b 100644 --- a/metobsapi/data_api.py +++ b/metobsapi/data_api.py @@ -103,16 +103,20 @@ def _get_column_name_in_influxdb(site: str, inst: str, api_symbol: str) -> str: def handle_influxdb_result(data_frame, influx_to_requested_symbols): - valid_requested_symbols = [symbol for symbol in influx_to_requested_symbols.values() if symbol is not None] if data_frame is None: # invalid query + valid_requested_symbols = [symbol for symbol in influx_to_requested_symbols.values() if symbol is not None] return _create_empty_dataframe_for_columns(valid_requested_symbols) data_frame = _convert_wind_vectors_to_direction_if_needed(data_frame) data_frame = data_frame.round({s: ROUNDING.get(s, 1) for s in data_frame.columns}) + # reorder columns to match the order the user requested + new_column_order = [influx_name for influx_name in influx_to_requested_symbols if influx_name in data_frame.columns] + data_frame = data_frame[new_column_order] # rename columns to API names requested by the user # User could request "<site>.<inst>.name" or just "name" - data_frame.columns = valid_requested_symbols + renamed_columns = [influx_to_requested_symbols[symbol] for symbol in data_frame.columns] + data_frame.columns = renamed_columns return data_frame diff --git a/metobsapi/tests/test_data_api.py b/metobsapi/tests/test_data_api.py index d2bb3af..272f993 100644 --- a/metobsapi/tests/test_data_api.py +++ b/metobsapi/tests/test_data_api.py @@ -345,6 +345,14 @@ def test_shorthand_two_symbols_json_column_order_check(client): assert res1["results"]["data"]["rel_hum"] == res2["results"]["data"]["rel_hum"] +@pytest.mark.usefixtures("influxdb_wind_fields_9_values") +def test_shorthand_wind_symbols_json_column_order_check(client): + res1 = _query_with_symbols(client, "wind_speed:wind_direction") + res2 = _query_with_symbols(client, "wind_direction:wind_speed") + assert res1["results"]["data"]["wind_speed"] == res2["results"]["data"]["wind_speed"] + assert res1["results"]["data"]["wind_direction"] == res2["results"]["data"]["wind_direction"] + + def _query_with_symbols(client, symbols: str) -> dict: res = client.get(f"/api/data.json?site=aoss&inst=tower&symbols={symbols}&begin=-00:10:00&order=column") res = json.loads(res.data.decode()) diff --git a/metobsapi/util/query_influx.py b/metobsapi/util/query_influx.py index bbc2ee3..15d8f78 100644 --- a/metobsapi/util/query_influx.py +++ b/metobsapi/util/query_influx.py @@ -91,7 +91,7 @@ class QueryHandler: data_frame = data_frame.drop(columns=["site", "inst"]) # get dataframe into the same order that was requested data_frame = data_frame[symbol_names] - # "_time" should already be set as the index so we don't need to rename it + # "_time" should already be set as the index, so we don't need to rename it new_column_names = [f"{site}.{inst}.{col_name}" for col_name in data_frame.columns] data_frame.columns = new_column_names frames_to_concat.append(data_frame) -- GitLab