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
7 changes: 4 additions & 3 deletions data_tools/query/_solcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,11 @@ def query(
return weather_df

# If we want to return datetimes, parse pandas.Timestamp to datetime, otherwise parse into POSIX timestamp
def time_parser(timestamp: pd.Timestamp) -> float | datetime:
return timestamp.timestamp() if not return_datetime else timestamp.to_pydatetime()
if return_datetime:
time_axis: NDArray = np.array([ts.to_pydatetime() for ts in weather_df.index])
else:
time_axis: NDArray = np.array([ts.timestamp() for ts in weather_df.index])

time_axis: NDArray = np.fromiter(map(time_parser, weather_df.index), dtype=float)
data_arrays: list[NDArray] = [
weather_df[str(output_parameter)].to_numpy() for output_parameter in output_parameters
]
Expand Down
26 changes: 26 additions & 0 deletions tests/test_solcast_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,29 @@ def test_solcast_query():
# Everything should be non-null
assert np.all(~np.isnan(ghi10[3:]))
assert np.all(~np.isnan(ghi))

def test_datetime_x_axis():
api_key = os.getenv("SOLCAST_API_KEY")
client = SolcastClient(api_key)

test_location = UNMETERED_LOCATIONS["Sydney Opera House"]

start_time = datetime.now(UTC) - timedelta(hours=3)
end_time = datetime.now(UTC) + timedelta(hours=3)

desired_outputs = [SolcastOutput(output) for output in ["ghi", "ghi10"]]

time, ghi, ghi10 = client.query(
latitude=test_location["latitude"],
longitude=test_location["longitude"],
period=SolcastPeriod.PT60M,
output_parameters=desired_outputs,
tilt=0,
azimuth=0,
start_time=start_time,
end_time=end_time,
return_datetime=True,
)

assert len(time) == len(ghi) == len(ghi10) == 1 + 3 + 3 # Number of forecasts = 1 + future hours + past hours
assert isinstance(time[0], datetime)