From dc9072d38703f51a84ddc9acb42bd900a4ef43e3 Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Mon, 13 Nov 2023 16:02:27 +0100
Subject: [PATCH] Add runnable script and virtualenv

---
 PSILARTEMP.py     |  6 ++---
 requirements.txt  |  4 +++
 run_PSILARTEMP.sh | 65 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 3 deletions(-)
 create mode 100644 requirements.txt
 create mode 100644 run_PSILARTEMP.sh

diff --git a/PSILARTEMP.py b/PSILARTEMP.py
index 278e696..8b15c06 100755
--- a/PSILARTEMP.py
+++ b/PSILARTEMP.py
@@ -25,9 +25,9 @@ from jinja2 import Environment, FileSystemLoader
 
 load_dotenv()
 
-weatherdata_path="in/"
-tmp_path="tmp/"
-out_path="out/"
+weatherdata_path = os.getenv("WEATHER_DATA_DIR")
+tmp_path = "tmp/"
+out_path = os.getenv("DATA_DIR")
 
 # TODO: Make this truly independent of time zones/Norwegian conditions?
 utc_offset = "+02:00"
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..222acb6
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,4 @@
+Jinja2
+netCDF4
+pytz
+python-dotenv
\ No newline at end of file
diff --git a/run_PSILARTEMP.sh b/run_PSILARTEMP.sh
new file mode 100644
index 0000000..b1e17f2
--- /dev/null
+++ b/run_PSILARTEMP.sh
@@ -0,0 +1,65 @@
+#!/bin/bash
+
+#    Copyright (C) 2023 NIBIO <http://www.nibio.no/>. 
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
+#
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+# Configures environment and logging before running the model
+# @author: Tor-Einar Skog <tor-einar.skog@nibio.no>
+
+
+
+# First: Test that we have CDO and GDAL installed
+if ! command -v cdo &> /dev/null
+then
+    echo "ERROR: CDO could not be found. Exiting."
+    exit
+fi
+
+if ! command -v gdal_merge.py &> /dev/null
+then
+    echo "ERROR: GDAL is either not installed, or not built with Python support. Exiting."
+    exit
+fi
+
+# Defines HOME_DIR
+source .env
+
+# Check for HOME_DIR
+if [ -z "${HOME_DIR}" ]
+then
+    echo "ERROR: \$HOME_DIR is not set. Please declare it in an .env file"
+    exit
+fi
+
+# Paths to scripts and requirements
+APP_PATH=${HOME_DIR}PSILARTEMP/
+LOG_FILE=${APP_PATH}log/PSILARTEMP.log
+REQUIREMENTS=${APP_PATH}requirements.txt
+
+cd $APP_PATH
+
+# Create and activate the virtual environment
+python3 -m venv .venv
+. .venv/bin/activate
+python3 -m pip install --upgrade pip
+pip install -r $REQUIREMENTS
+
+# Run the model
+echo "==== `date`: Running model" &>> "$LOG_FILE"
+python3 $APP_PATH/PSILARTEMP.py &>> "$LOG_FILE"
+echo "==== `date`: DONE running model" &>> "$LOG_FILE"
+
+# Deactivate the virtual environment
+deactivate
-- 
GitLab