diff --git a/metobsapi/data_api.py b/metobsapi/data_api.py index 7f91c6195b8dca38609e32b61a1d93092b1dfc9b..f2c7e2bf0aaf8f83bc4da81bdb5b74af56625df7 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 d2bb3af17e22ee98e50ce2129fc84ee331dacaa1..272f993e88b99b1f9c6d69772db018e3f64b1dc3 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 bbc2ee3ecc6c85f83d14efef0448a986011726a6..15d8f78bc7a6b94fd08f023e18ba699af7368495 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)