From ff87ac8edd609510ce02b79ca71852efa2f63e44 Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Wed, 26 Oct 2022 15:52:25 +0200
Subject: [PATCH] Fix: Handle wrong period

---
 app/models.py            | 1 -
 app/weatherdata_utils.py | 8 ++++----
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/app/models.py b/app/models.py
index 5320c98..0f21d86 100644
--- a/app/models.py
+++ b/app/models.py
@@ -16,7 +16,6 @@
 #    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 from datetime import datetime
-from _datetime import date
 
 from shapely import wkb
 from shapely.geometry import Point
diff --git a/app/weatherdata_utils.py b/app/weatherdata_utils.py
index 8ed9445..ef27e86 100644
--- a/app/weatherdata_utils.py
+++ b/app/weatherdata_utils.py
@@ -7,14 +7,14 @@ from models import WeatherData
 def filter_period(weather_data, time_start, time_end):
     # Get the start and end index
     #print(time_start)
-    start_index = weather_data.get_index_from_epoch_seconds(WeatherData.to_epoch_seconds(time_start))
+    start_index = max(0,weather_data.get_index_from_epoch_seconds(WeatherData.to_epoch_seconds(time_start)))
     #print(start_index)
-    end_index = weather_data.get_index_from_epoch_seconds(WeatherData.to_epoch_seconds(time_end))
+    end_index = min(weather_data.get_index_from_epoch_seconds(weather_data.timeEnd), weather_data.get_index_from_epoch_seconds(WeatherData.to_epoch_seconds(time_end)))
     for lwd in weather_data.locationWeatherData:
         lwd.data = lwd.data[start_index:end_index]
     # Adjust timeStart and timeEnd
-    weather_data.timeStart = WeatherData.to_epoch_seconds(time_start)
-    weather_data.timeEnd = WeatherData.to_epoch_seconds(time_end)
+    weather_data.timeStart = weather_data.timeStart + (start_index * weather_data.interval)
+    weather_data.timeEnd = weather_data.timeStart + (end_index * weather_data.interval)
     return weather_data
 
 def filter_params(weather_data, params):
-- 
GitLab