Something went wrong on our end
-
Lene Wasskog authoredLene Wasskog authored
run_SEPTREFHUM.sh 3.08 KiB
#!/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>
# Defines HOME_DIR
source .env
validate_year() {
if [[ $1 =~ ^[0-9]{4}$ ]]; then
return 0
else
return 1
fi
}
# Check if the year parameter is passed and validate it
if [ -n "$1" ]; then
if validate_year "$1"; then
year=$1
else
echo "Invalid year: $1. Please provide a valid 4-digit year."
exit 1
fi
fi
# 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
LOG_FILE=${HOME_DIR}log/SEPTREFHUM.log
REQUIREMENTS=${HOME_DIR}requirements.txt
CDO_VERSION=2.4.0
# Test that we have GDAL and Miniconda installed
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
if ! command -v conda &> /dev/null
then
echo "ERROR: Miniconda is not installed. Pleare read: https://code.mpimet.mpg.de/projects/cdo/wiki/Anaconda"
exit
fi
cd $HOME_DIR
# Check if we need to create the virtual environment
eval "$(conda shell.bash hook)"
now=$(date +%s)
if ! conda env list | grep -q "SEPTREFHUM "; then
echo "Conda env not found, creating it"
conda env create -f conda_environment.yml
conda activate SEPTREFHUM
else
# Make sure we catch/update changes in dependencies
if [ -e last_run_timestamp ]; then
last_run_timestamp=$(<last_run_timestamp)
last_modified_env_file=$(stat -c "%Y" conda_environment.yml)
if [ $((last_run_timestamp - last_modified_env_file)) -lt 0 ]; then
echo "conda_environment.yml file has been updated. Updating the conda env. This may take a while"
conda env update --prune -f conda_environment.yml
fi
fi
conda activate SEPTREFHUM
fi
# Run the model
if [ -z "${year}" ]; then
echo "==== $(date): Running model for current year" >> "$LOG_FILE" 2>&1
python3 ${HOME_DIR}SEPTREFHUM.py >> "$LOG_FILE" 2>&1
else
echo "==== $(date): Running model for ${year}" >> "$LOG_FILE" 2>&1
python3 ${HOME_DIR}SEPTREFHUM.py "$year" >> "$LOG_FILE" 2>&1
fi
# Deactivate the virtual environment
conda deactivate
# Log this to compare with changes in conda_environment.yml
echo $now > last_run_timestamp