From f3c0a9f76a9dbedf5289b283ba1a2e9aff1ca7cf Mon Sep 17 00:00:00 2001 From: Tor-Einar Skog <tor-einar.skog@nibio.no> Date: Thu, 25 Apr 2024 14:06:16 +0200 Subject: [PATCH] Handle deps with Miniconda --- .gitignore | 1 + conda_environment.yml | 9 +++++++ run_SEPTREFHUM.sh | 56 +++++++++++++++++++++++++++---------------- 3 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 conda_environment.yml diff --git a/.gitignore b/.gitignore index 0221f8c..a6ee8d1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .env .venv mapfile/*.map +last_run_timestamp diff --git a/conda_environment.yml b/conda_environment.yml new file mode 100644 index 0000000..7f5511f --- /dev/null +++ b/conda_environment.yml @@ -0,0 +1,9 @@ +name: SEPTREFHUM +channels: + - conda-forge +dependencies: + - cdo=2.4.0 + - Jinja2 + - netCDF4 + - pytz + - python-dotenv \ No newline at end of file diff --git a/run_SEPTREFHUM.sh b/run_SEPTREFHUM.sh index b77fc5a..fe0e4db 100755 --- a/run_SEPTREFHUM.sh +++ b/run_SEPTREFHUM.sh @@ -18,42 +18,55 @@ # Configures environment and logging before running the model # @author: Tor-Einar Skog <tor-einar.skog@nibio.no> +# Defines HOME_DIR +source .env - -# First: Test that we have CDO and GDAL installed -if ! command -v cdo &> /dev/null +# Check for HOME_DIR +if [ -z "${HOME_DIR}" ] then - echo "ERROR: CDO could not be found. Exiting." + 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 -# Defines HOME_DIR -source .env - -# Check for HOME_DIR -if [ -z "${HOME_DIR}" ] +if ! command -v conda &> /dev/null then - echo "ERROR: \$HOME_DIR is not set. Please declare it in an .env file" + echo "ERROR: Miniconda is not installed. Pleare read: https://code.mpimet.mpg.de/projects/cdo/wiki/Anaconda" exit fi -# Paths to scripts and requirements -LOG_FILE=${HOME_DIR}log/SEPTREFHUM.log -REQUIREMENTS=${HOME_DIR}requirements.txt - cd $HOME_DIR -# Create and activate the virtual environment -python3 -m venv .venv -. .venv/bin/activate -python3 -m pip install -q --upgrade pip -pip install -q -r $REQUIREMENTS +# 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 echo "==== `date`: Running model" &>> "$LOG_FILE" @@ -61,4 +74,7 @@ python3 ${HOME_DIR}SEPTREFHUM.py &>> "$LOG_FILE" echo "==== `date`: DONE running model" &>> "$LOG_FILE" # Deactivate the virtual environment -deactivate +conda deactivate + +# Log this to compare with changes in conda_environment.yml +echo $now > last_run_timestamp -- GitLab