clm5/py_env_create
2024-05-09 15:14:01 +08:00

141 lines
3.9 KiB
Bash

#!/bin/bash
#
# py_env_create -- setup the python environment in order to use CTSM python tools
#
# Simple bash script to setup the python environment for the user so they can run the CTSM
# python tools using "conda".
#
dir=${0%/*}
if [ "$dir" = "$0" ];then
dir="."
fi
# Check if conda is in your path
conda --help >& condahelp.txt
error=$?
if [ $error != 0 ]; then
echo "conda is NOT in your path for the bash shell add it with modules or whatever is required on your system to get it in your path"
echo "on Derecho/capser/etc use -- module load conda"
echo "on izumi/CGD systems use -- module unload lang/python; module load lang/anaconda/23.11.0/base"
echo "For notes on installing on a user system see: https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html"
echo "Error code was $error"
cat condahelp.txt
rm condahelp.txt
exit -1
fi
rm condahelp.txt
ctsm_python=ctsm_pylib
condadir="$dir/python"
domain=`domainname`
condafile="conda_env_ctsm_py.txt"
#----------------------------------------------------------------------
# Usage subroutine
usage() {
echo ""
echo "***********************************************************************"
echo "usage:"
echo "./py_env_create"
echo ""
echo "valid arguments: "
echo "[-h|--help] "
echo " Displays this help message"
echo "[-v|--verbose] "
echo " Run with verbose mode for the install so you see the progress bar"
echo "[-f|--file <file>] "
echo " Conda environment requirements text file to use (text format) in addition to the others"
echo " Assumed to be under the directory: $condadir"
echo " Default is: $condafile"
echo "[--option <option>] "
echo " Option(s) to pass to 'conda install' step"
echo "***********************************************************************"
}
verbose="No"
option=""
while [ $# -gt 0 ]; do
case $1 in
-h|--help )
usage
exit 0
;;
-v|--verbose )
verbose="Yes"
;;
-f|--file )
condafile=$2
shift
;;
--option )
option=$2
shift
;;
* )
echo "ERROR:: invalid argument sent in: $2"
usage
exit 1
;;
esac
shift
done
#
# Error checking options and setup
#
if [ ! -f $condadir/$condafile ]; then
echo "$condadir/$condafile does NOT exist"
echo "Use the --file option with a valid filename"
exit -1
fi
echo "Use conda to install the python environment needed to run the CTSM python tools in the conda environment: $ctsm_python"
echo "Using the file: $condadir/$condafile"
#
# Check if the environment already exists, if it does continue, if not create it
#
conda list -n $ctsm_python >& /dev/null
if [ $? != 0 ]; then
echo "Create $ctsm_python"
cmd="conda create --force --name $ctsm_python --quiet"
echo "$cmd"
$cmd
if [ $? != 0 ]; then
echo "Error creating conda environment $ctsm_python"
exit -1
fi
else
echo "$ctsm_python environment already exists"
fi
#
# Install the environemnt
#
echo "Install $ctsm_python this can take a long time (12 to 20 minutes is expected), be patient...."
echo " ...."
echo " ...."
echo " ...."
verbosity="--quiet"
loglevel="ERROR"
if [ "$verbose" == "Yes" ]; then
verbosity="--verbose"
loglevel="INFO"
fi
cmd="conda install --yes $verbosity --channel conda-forge --name $ctsm_python $condadir/$condafile $option"
echo "$cmd"
$cmd
if [ $? != 0 ]; then
echo "Trouble installing the $ctsm_python python environment"
echo "There must be a problem in the $condadir/$condafile conda specification environment file"
echo "Change the file and try again"
exit -2
fi
#
# Report on success
#
echo "Successfully installed the $ctsm_python python environment"
echo
echo "activate the environment by doing the following..."
echo "conda activate $ctsm_python"