Skip to content

Commit

Permalink
fixing the response when there is no data found getting the station data
Browse files Browse the repository at this point in the history
  • Loading branch information
PhillipsOwen committed Sep 6, 2023
1 parent b3014d8 commit 5758040
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions src/common/pg_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,45 +137,49 @@ def get_station_data(self, **kwargs) -> str:
# get forecast data
forecast_data = self.get_forecast_station_data(kwargs['station_name'], kwargs['time_mark'], kwargs['data_source'])

# derive start date from the time mark
start_date = (datetime.fromisoformat(kwargs['time_mark']) - timedelta(4)).isoformat()
# check for an error
if forecast_data.empty:
return ''
else:
# derive start date from the time mark
start_date = (datetime.fromisoformat(kwargs['time_mark']) - timedelta(4)).isoformat()

# get end_date from last datetime in forecast data
end_date = forecast_data['time_stamp'].iloc[-1]
# get end_date from last datetime in forecast data
end_date = forecast_data['time_stamp'].iloc[-1]

# get nowcast data_source from forecast data_source
nowcast_source = 'NOWCAST_' + "_".join(kwargs['data_source'].split('_')[1:])
# get nowcast data_source from forecast data_source
nowcast_source = 'NOWCAST_' + "_".join(kwargs['data_source'].split('_')[1:])

# get obs and nowcast data
obs_data = self.get_obs_station_data(kwargs['station_name'], start_date, end_date, nowcast_source)
# get obs and nowcast data
obs_data = self.get_obs_station_data(kwargs['station_name'], start_date, end_date, nowcast_source)

# drop empty columns
empty_cols = [col for col in obs_data.columns if obs_data[col].isnull().all()]
obs_data.drop(empty_cols, axis=1, inplace=True)
# drop empty columns
empty_cols = [col for col in obs_data.columns if obs_data[col].isnull().all()]
obs_data.drop(empty_cols, axis=1, inplace=True)

# replace any None values with np.nan, in both DataFrames
forecast_data.fillna(value=np.nan)
obs_data.fillna(value=np.nan)
# replace any None values with np.nan, in both DataFrames
forecast_data.fillna(value=np.nan)
obs_data.fillna(value=np.nan)

# convert all values after the time mark to nan, in obs data, except in the time_stamp and tidal_predictions columns
for col in obs_data.columns:
if col not in ('time_stamp', 'tidal_predictions'):
obs_data.loc[obs_data.time_stamp >= kwargs['time_mark'], col] = np.nan
else:
continue
# convert all values after the time mark to nan, in obs data, except in the time_stamp and tidal_predictions columns
for col in obs_data.columns:
if col not in ('time_stamp', 'tidal_predictions'):
obs_data.loc[obs_data.time_stamp >= kwargs['time_mark'], col] = np.nan
else:
continue

# merge the obs DataFrame with the forecast Dataframe
station_df = obs_data.merge(forecast_data, on='time_stamp', how='outer')
# merge the obs DataFrame with the forecast Dataframe
station_df = obs_data.merge(forecast_data, on='time_stamp', how='outer')

# get the forecast and nowcast column names
forecast_column_name = "".join(kwargs['data_source'].split('.')).lower()
nowcast_column_name = "".join(nowcast_source.split('.')).lower()
# get the forecast and nowcast column names
forecast_column_name = "".join(kwargs['data_source'].split('.')).lower()
nowcast_column_name = "".join(nowcast_source.split('.')).lower()

# rename the columns
station_df.rename(columns={forecast_column_name: 'forecast_water_level', nowcast_column_name: 'nowcast_water_level'}, inplace=True)
# rename the columns
station_df.rename(columns={forecast_column_name: 'forecast_water_level', nowcast_column_name: 'nowcast_water_level'}, inplace=True)

# return the data to the caller
return station_df.to_csv(index=False)
# return the data to the caller
return station_df.to_csv(index=False)

def get_forecast_station_data(self, station_name, time_mark, data_source) -> pd.DataFrame:
"""
Expand All @@ -200,6 +204,8 @@ def get_forecast_station_data(self, station_name, time_mark, data_source) -> pd.
if station_data != -1:
# convert query output to Pandas dataframe
ret_val = pd.DataFrame.from_dict(station_data, orient='columns')
else:
ret_val = pd.DataFrame(None)

# Return Pandas dataframe
return ret_val
Expand Down

0 comments on commit 5758040

Please sign in to comment.