Skip to content
Snippets Groups Projects
Commit f3c0a9f7 authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Handle deps with Miniconda

parent a6c394b9
Branches
No related tags found
1 merge request!1Cdo 2.4 miniconda
......@@ -4,3 +4,4 @@
.env
.venv
mapfile/*.map
last_run_timestamp
name: SEPTREFHUM
channels:
- conda-forge
dependencies:
- cdo=2.4.0
- Jinja2
- netCDF4
- pytz
- python-dotenv
\ No newline at end of file
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment