Skip to content

Commit

Permalink
Refactor NDBC and fix error handelling
Browse files Browse the repository at this point in the history
  • Loading branch information
abdu558 committed Jul 28, 2024
1 parent 89efc16 commit 1a59b52
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
17 changes: 6 additions & 11 deletions searvey/_ndbc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import functools
import logging
from typing import List, Union
from typing import List
from typing import Union

import geopandas as gpd
import multifutures
Expand Down Expand Up @@ -96,7 +97,7 @@ def _fetch_ndbc(
:param mode: Data mode. One of ``'txt'``, ``'json'``, ``'spec'``.
:param start_dates: The starting date of the query. Defaults to 7 days ago.
:param end_dates: The finishing date of the query. Defaults to "now".
:param columns:
:param columns: Optional list of columns to retrieve.
:param multithreading_executor: A multithreading executor.
:return: A dictionary mapping station identifiers to their respective
TimeSeries.
Expand Down Expand Up @@ -131,13 +132,7 @@ def _fetch_ndbc(
executor=multithreading_executor,
)

# Check for errors and collect results
multifutures.check_results(results)

dataframes = {
result.kwargs["station_id"]: result.result for result in results if result.exception is None # type: ignore[index]

}
dataframes = {result.kwargs["station_id"]: result.result for result in results}
return dataframes


Expand All @@ -157,7 +152,7 @@ def fetch_ndbc_station(
:param mode: Data mode. Read the example ndbc file for more info.
:param start_date: The starting date of the query.
:param end_date: The finishing date of the query.
:param columns: List of columns to retrieve.
:param columns: Optional list of columns to retrieve.
:return: ``pandas.DataFrame`` with the station data.
"""
logger.info("NDBC-%s: Starting data retrieval: %s - %s", station_id, start_date, end_date)
Expand All @@ -180,4 +175,4 @@ def fetch_ndbc_station(

except Exception as e:
logger.error(f"Error fetching data for station {station_id}: {str(e)}")
return pd.DataFrame()
return pd.DataFrame()
16 changes: 7 additions & 9 deletions tests/ndbc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_fetch_ndbc_station_data():
mode="stdmet",
# test that both formats work
start_date=datetime.date(2023, 1, 1),
end_date="2023-01-10"
end_date="2023-01-10",
)

assert isinstance(df, pd.DataFrame)
Expand Down Expand Up @@ -76,8 +76,8 @@ def test_fetch_ndbc_data_multiple():
station_ids=["STDM4", "TPLM2"],
mode="stdmet",
# test that both formats work
start_dates=[datetime.date(2023, 1, 1),"2023-01-01"],
end_dates=["2023-01-10","2023-01-20"]
start_dates=[datetime.date(2023, 1, 1), "2023-01-01"],
end_dates=["2023-01-10", "2023-01-20"],
)

assert isinstance(dataframes, dict)
Expand Down Expand Up @@ -138,14 +138,12 @@ def test_fetch_ndbc_data_multiple_unavaliable_avaliable_data():
station_ids=["41001", "STDM4"],
mode="stdmet",
# test that both formats work
start_dates=[datetime.date(2023, 1, 1),"2023-01-01"],
end_dates=["2023-01-10","2023-01-10"]
start_dates=[datetime.datetime(2023, 1, 1, 10, 0, 0), datetime.datetime(2023, 1, 1, 10, 0, 0)],
end_dates=[datetime.date(2023, 1, 10), "2023-01-10"],
)

assert isinstance(dataframes, dict)
assert len(dataframes) == 2
assert "41001" in dataframes
assert dataframes["41001"].empty
assert "STDM4" in dataframes
df = dataframes["STDM4"]
assert isinstance(df, pd.DataFrame)
Expand All @@ -168,5 +166,5 @@ def test_fetch_ndbc_data_multiple_unavaliable_avaliable_data():
"TIDE",
]
)
assert df.index[0] == pd.to_datetime("2023-01-01 00:00:00")
assert df.index[-1] == pd.to_datetime("2023-01-10 00:00:00")
assert df.index[0] == pd.to_datetime("2023-01-01 10:00:00")
assert df.index[-1] == pd.to_datetime("2023-01-10 00:00:00")

0 comments on commit 1a59b52

Please sign in to comment.