diff --git a/scripts/0.run_all.bash b/scripts/0.run_all.bash index 4fa50e4..09c7e29 100755 --- a/scripts/0.run_all.bash +++ b/scripts/0.run_all.bash @@ -1,64 +1,364 @@ #!/bin/bash -#if [ $# -ne 5 ] -#then -# echo "" -# echo "Instructions: execute the command below" -# echo "" -# echo "${0} GitHubUserRepo EXP_NAME RESOLUTION LABELI FCST" -# echo "" -# echo "GitHubUserRepo :: GitHub link for your personal fork, eg: https://github.com/MYUSER/MONAN-Model.git" -# echo "EXP_NAME :: Forcing: GFS" -# echo "RESOLUTION :: number of points in resolution model grid, e.g: 1024002 (24 km)" -# echo "LABELI :: Initial date YYYYMMDDHH, e.g.: 2024010100" -# echo "FCST :: Forecast hours, e.g.: 24 or 36, etc." -# echo "" -# echo "24 hour forcast example:" -# echo "${0} https://github.com/MYUSER/MONAN-Model.git GFS 1024002 2024010100 24" -# echo "" -# exit -#fi - -# Set environment variables exports: -echo "" -echo -e "\033[1;32m==>\033[0m Moduling environment for MONAN model...\n" -. setenv.bash - - -# Standart directories variables:--------------------------------------- -DIRHOMES=${DIR_SCRIPTS}/scripts_CD-CT; mkdir -p ${DIRHOMES} -DIRHOMED=${DIR_DADOS}/scripts_CD-CT; mkdir -p ${DIRHOMED} -SCRIPTS=${DIRHOMES}/scripts; mkdir -p ${SCRIPTS} -DATAIN=${DIRHOMED}/datain; mkdir -p ${DATAIN} -DATAOUT=${DIRHOMED}/dataout; mkdir -p ${DATAOUT} -SOURCES=${DIRHOMES}/sources; mkdir -p ${SOURCES} -EXECS=${DIRHOMED}/execs; mkdir -p ${EXECS} -#---------------------------------------------------------------------- - - -# Input variables:----------------------------------------------------- -github_link="https://github.com/monanadmin/MONAN-Model.git" -monan_branch=1.4.3-rc -convertmpas_branch=1.2.0 -EXP=GFS -RES=1024002 #Options: 40962=120km;163842=60km;655362=30Km;1024002=24km;2621442=15Km;5898242=10Km + + +#--- Function that shows usage. +function show_usage() { + echo " Usage: " + echo "" + echo " ${0} [-h] [-m12] [-o] [-z] [-bc TAG_CONVERT_MPAS] [ -bm TAG_MONAN ] \\" + echo " [-d OUTPUT_DIAG_INT] [-e EXP ] [-f FCST] [-gc GIT_CONVERT_MPAS] \\" + echo " [-gm GIT_MONAN ] [-i INPUT_PATH] [-l NLEV] [-r RES] [-s STEP] \\" + echo " [-t YYYYMMDDHH] [-v VARTABLE]" + echo "" + echo " List of optional flags: " + echo "" + echo " -bc TAG_CONVERT -- branch or tag name of the MONAN repository. For example:" + echo " \"develop\". This is used only by step 1." + echo " -bm TAG_MONAN -- branch or tag name of the MONAN repository. For example:" + echo " \"develop\". This is used only by step 1." + echo " -d OUTPUT_DIAG_INT -- Output interval for diagnostic. The format must be" + echo " \"HH:MM:SS\". This is used by steps 3 and 4." + echo " -e EXP -- Meteorological drivers. For example, GFS" + echo " -f FCST -- Simulation length in hours, e.g., 24 or 48." + echo " -gc GIT_CONVERT -- GitHub handle for MONAN. For example:" + echo " https://github.com/monanadmin/MONAN-Model.git" + echo " This is used only by step 1." + echo " -gm GIT_MONAN -- GitHub handle for MONAN. For example:" + echo " https://github.com/monanadmin/MONAN-Model.git" + echo " This is used only by step 1." + echo " -h -- Shows this message." + echo " -i INPUT_PATH -- Path containing input data for MONAN. If left empty, the" + echo " default path defined in setenv.bash will be used" + echo " -l NLEV -- Number of vertical levels for the output. This is used" + echo " by steps 3 and 4." + echo " -m12 -- Is this a MONAN run based on 1.2.0-rc and branches" + echo " derived from this version (e.g., feature/monan-757-NF)?" + echo " This is a temporary flag that will be removed once" + echo " the versions containing Noah-MP are merged into the new" + echo " release. This allows the script to manage older code and" + echo " still run on jaci." + echo " -o -- Overwrite static files. This is used only by step 2." + echo " -r RES -- grid resolution. Supported options are:" + echo " 65536002 (~ 3 km)" + echo " 5898242 (~ 10 km)" + echo " 2621442 (~ 15 km)" + echo " 1024002 (~ 24 km)" + echo " 655362 (~ 30 km)" + echo " 163842 (~ 60 km)" + echo " 40962 (~ 120 km)" + echo " -s STEP -- Step to run. Options are:" + echo " 1 - Compile MONAN executables." + echo " 2 - Run pre-processing." + echo " 3 - Run atmospheric model." + echo " 4 - Run post-processing." + echo " 0 - Run all steps 1-4." + echo " -t YYYYMMDDHH -- Initial time. For example if 22 Sept 2025 00 UTC, the" + echo " argument should be 2025092200. This is used by steps" + echo " 2, 3 and 4." + echo " -v VARTABLE -- Suffix for defining which version of the" + echo " stream_list_atmosphere.diagnostics template to use." + echo " The default is to not use any suffix. This is used only" + echo " by steps 3 and 4." + echo " -z -- This will remove all files generated by all scripts and" + echo " reset the directory to the original state, except for" + echo " directory \"scripts\"." + echo "" + echo " All settings can be defined directly in the script." + echo "" +} +#---~--- + + + + +#--- Default input variables: +STEP=1 +OVERWRITE="" +MONAN_ONETWO="" +github_link_MONAN="https://github.com/monanadmin/MONAN-Model.git" +tag_or_branch_name_MONAN="release/1.4.3-rc" +github_link_CONVERT_MPAS="https://github.com/monanadmin/convert_mpas.git" +tag_or_branch_name_CONVERT_MPAS="release/1.2.0" +EXP="GFS" +INPUT_PATH="" +RES=1024002 YYYYMMDDHHi=2026012000 FCST=24 -#---------------------------------------------------------------------- +NLEV=55 +OUTPUT_DIAG_INTERVAL="03:00:00" +VARTABLE="" +ZERO_STATE=false +#---~--- + + +#--- Parse arguments. +while [[ ${#} > 0 ]] +do + key="${1}" + case ${key} in + -bc) + tag_or_branch_name_CONVERT_MPAS="${2}" + shift 2 # past flag and argument + ;; + -bm) + tag_or_branch_name_MONAN="${2}" + shift 2 # past flag and argument + ;; + -d) + OUTPUT_DIAG_INTERVAL="${2}" + shift 2 # Past flag and argument + ;; + -e) + EXP="${2}" + shift 2 # past flag and argument + ;; + -f) + FCST="${2}" + shift 2 # past flag and argument + ;; + -gc) + github_link_CONVERT_MPAS="${2}" + shift 2 # past flag and argument + ;; + -gm) + github_link_MONAN="${2}" + shift 2 # past flag and argument + ;; + -h) + show_usage + exit 0 + ;; + -i) + INPUT_PATH="${2}" + shift 2 # Past flag and argument + ;; + -l) + NLEV="${2}" + shift 2 # Past flag and argument + ;; + -m12) + MONAN_ONETWO="${key}" + shift 1 # past flag + ;; + -o) + OVERWRITE="${key}" + shift 1 # past flag + ;; + -r) + RES="${2}" + shift 2 # past flag and argument + ;; + -s) + STEP="${2}" + shift 2 # past flag and argument + ;; + -t) + YYYYMMDDHHi="${2}" + shift 2 # past flag and argument + ;; + -v) + VFIRST=$(echo ${2} | cut -c 1-1) + case "${VFIRST}" in + .) VARTABLE="${2}" ;; + *) VARTABLE=".${2}" ;; + esac + shift 2 # past flag and argument + ;; + -z) + ZERO_STATE=true + shift 1 # past flag + ;; + *) + echo "" + echo " Option \"${key}\" is not valid." + echo "" + show_usage + echo "" + echo " *** FATAL ERROR! ***" + echo " Unknown key or key-value argument pair." + echo "" + exit 2 + ;; + esac +done +#---~--- + + +#--- Set environment variables exports: +. setenv.bash ${MONAN_ONETWO} +#---~--- + + + + +#---~--- +# If the user has set -z, we ask for confirmation before all work is deleted... +#---~--- +if ${ZERO_STATE} +then + echo -e "" + echo -e "${ORANGE}!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!${NC}" + echo -e "${ORANGE}!!! WARNING !!! WARNING !!! WARNING !!! WARNING !!!${NC}" + echo -e "${ORANGE}!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!${NC}" + echo -e "${ORANGE}==>${NC} This script is about to reset this working case." + read -p " Are you sure about this? [y|N] " proceed + proceed=$(echo ${proceed:-N} | tr [:upper:] [:lower:]) + case "${proceed}" in + y|yes) + #---~--- + # Give one last chance to avoid accidental file deletion. + #---~--- + echo -e "" + echo -e "" + echo -e "${ORANGE}==>${NC} Alright, but mind that this cannot be undone." + echo -e "${ORANGE}==>${NC} I am giving you a few seconds to cancel this script" + echo -e " in case you change your mind. Hit Ctrl+C before the countdown ends." + echo -e "" + echo -e "" + count=11 + while [[ ${count} -gt 1 ]] + do + let count=${count}-1 + echo -e "${ORANGE}==>${NC} File deletion will begin in ${count} seconds." + sleep 1 + done + /bin/rm -frv ${DATAIN} + /bin/rm -frv ${DATAOUT} + /bin/rm -frv ${SOURCES} + /bin/rm -frv ${EXECS} + /bin/rm -frv ${DIR_SCRIPTS}/run.?????????? + ;; + *) + #--- Keep files. + echo -e "" + echo -e "${GREEN}==>${NC} Keeping all files. To run MONAN, call this script again" + echo -e " without the \"-z\" option." + #---~--- + ;; + esac + + #---~--- + # We do not run the scripts when -z is set. + #---~--- + exit 0 + #---~--- +fi +#---~--- + + +#--- If INPUT_PATH is provided, replace the path in setenv.bash +if [[ "${INPUT_PATH}" != "" ]] && [[ -d "${INPUT_PATH}" ]] +then + SYSTEM_SETENV="${DIR_SCRIPTS}/scripts_CD-CT/scripts/stools/setenv_${SYSTEM_KEYC}.bash" + sed -i.bck "s,^export DIRDADOS=.*,export DIRDADOS=${INPUT_PATH},g" ${SYSTEM_SETENV} + /bin/rm -f ${SYSTEM_SETENV}.bck +fi +#---~--- + + +#---~--- +# Make sure the grid resolution settings are valid. +#---~--- +case "${RES}" in +65536002) + echo -e "${GREEN}==>${NC} Grid resolution ${RES} (~ 3 km).\n" + ;; +5898242) + echo -e "${GREEN}==>${NC} Grid resolution ${RES} (~ 10 km).\n" + ;; +2621442) + echo -e "${GREEN}==>${NC} Grid resolution ${RES} (~ 15 km).\n" + ;; +1024002) + echo -e "${GREEN}==>${NC} Grid resolution ${RES} (~ 24 km).\n" + ;; +655362) + echo -e "${GREEN}==>${NC} Grid resolution ${RES} (~ 30 km).\n" + ;; +163842) + echo -e "${GREEN}==>${NC} Grid resolution ${RES} (~ 60 km).\n" + ;; +40962) + echo -e "${GREEN}==>${NC} Grid resolution ${RES} (~ 120 km).\n" + ;; +*) + echo -e "${ORANGE}****** WARNING ******${NC} \n" + echo -e "${ORANGE}==>${NC} Provided grid resolution (${RES}) is not recognised.\n" + echo -e "${ORANGE}==>${NC} We cannot guarantee that MONAN will run fine.\n" + ;; +esac +#---~--- + + +#--- Make sure VARTABLE has the leading "-v" if not empty. +if [[ "${VARTABLE}" == "" ]] +then + dv_VARTABLE="" +else + dv_VARTABLE="-v ${VARTABLE}" +fi +#---~--- -# STEP 1: Installing and compiling the A-MONAN model and utility programs: -time ${SCRIPTS}/1.install_monan.bash ${github_link} ${monan_branch} ${convertmpas_branch} -#exit -# STEP 2: Executing the pre-processing fase. Preparing all CI/CC files needed: -time ${SCRIPTS}/2.pre_processing.bash ${EXP} ${RES} ${YYYYMMDDHHi} ${FCST} -#exit +#---~--- +# Select step. +#---~--- +case ${STEP} in +0) + #---~--- + # Call the script itself four times, passing all the configuration. + #---~--- + step_now=0 + while [[ ${step_now} -lt 4 ]] + do + #--- Update step + let step_now=${step_now}+1 + #---~--- -# STEP 3: Executing the Model run: -time ${SCRIPTS}/3.run_model.bash ${EXP} ${RES} ${YYYYMMDDHHi} ${FCST} -#exit -# STEP 4: Executing the Post of Model run: -time ${SCRIPTS}/4.run_post.bash ${EXP} ${RES} ${YYYYMMDDHHi} ${FCST} -#exit + #--- Run step + time ${0} ${OVERWRITE} ${dv_VARTABLE} ${MONAN_ONETWO} \ + -bc ${tag_or_branch_name_CONVERT_MPAS} -bm ${tag_or_branch_name_MONAN} \ + -d ${OUTPUT_DIAG_INTERVAL} -e ${EXP} -f ${FCST} -gc ${github_link_CONVERT_MPAS} \ + -gm ${github_link_MONAN} -l ${NLEV} -r ${RES} -s ${step_now} -t ${YYYYMMDDHHi} + #---~--- + done + #---~--- + ;; +1) + #---~--- + # STEP 1: Install and compile MONAN and its utility programs. + #---~--- + time 1.install_monan.bash ${MONAN_ONETWO} -bc ${tag_or_branch_name_CONVERT_MPAS} \ + -bm ${tag_or_branch_name_MONAN} -gc ${github_link_CONVERT_MPAS} \ + -gm ${github_link_MONAN} + #---~--- + ;; +2) + #---~--- + # STEP 2: Run the pre-processing step, and make initial/boundary conditions if needed. + #---~--- + time 2.pre_processing.bash ${MONAN_ONETWO} ${OVERWRITE} -e ${EXP} -f ${FCST} -r ${RES} \ + -t ${YYYYMMDDHHi} + #---~--- + ;; +3) + #---~--- + # STEP 3: Run the model. + #---~--- + time 3.run_model.bash ${MONAN_ONETWO} ${dv_VARTABLE} -d ${OUTPUT_DIAG_INTERVAL} \ + -e ${EXP} -f ${FCST} -l ${NLEV} -r ${RES} -t ${YYYYMMDDHHi} + #---~--- + ;; +4) + #---~--- + # STEP 4: Run the post-processing step. + #---~--- + time 4.run_post.bash ${MONAN_ONETWO} ${dv_VARTABLE} -d ${OUTPUT_DIAG_INTERVAL} \ + -e ${EXP} -f ${FCST} -l ${NLEV} -r ${RES} -t ${YYYYMMDDHHi} + #---~--- + ;; +esac +#---~--- diff --git a/scripts/1.install_monan.bash b/scripts/1.install_monan.bash index a797ca1..2c95507 100755 --- a/scripts/1.install_monan.bash +++ b/scripts/1.install_monan.bash @@ -17,21 +17,51 @@ umask 022 # #-----------------------------------------------------------------------------# -#Fixed parameters ------------------------------------------------------------# -github_link_CONVERT_MPAS="https://github.com/monanadmin/convert_mpas.git" -#-----------------------------------------------------------------------------# + +#--- Function that shows usage. +function show_usage() { + echo " Usage: " + echo "" + echo " ${0} [-h] [-m12] [-bc TAG_CONVERT_MPAS] [-bm TAG_MONAN] [-gm GIT_MONAN] \\" + echo " [-gc GIT_CONVERT_MPAS]" + echo "" + echo " List of optional flags: " + echo "" + echo " -h -- Shows this message." + echo " -m12 -- Is this a MONAN run based on 1.2.0-rc and branches derived" + echo " from this version (e.g., feature/monan-757-NF)? This is a" + echo " temporary flag that will be removed once the versions" + echo " containing Noah-MP are merged into the new release. This" + echo " allows the script to manage older code and still run on jaci." + echo "" + echo " List of **required** flags: " + echo "" + echo " -bc TAG_CONVERT -- branch or tag name of the MONAN repository. For example:" + echo " \"develop\"." + echo " -bm TAG_MONAN -- branch or tag name of the MONAN repository. For example:" + echo " \"develop\"." + echo " -gc GIT_CONVERT -- GitHub handle for MONAN. For example:" + echo " https://github.com/monanadmin/MONAN-Model.git" + echo " -gm GIT_MONAN -- GitHub handle for MONAN. For example:" + echo " https://github.com/monanadmin/MONAN-Model.git" + echo "" +} +#---~--- + + + #Functions -------------------------------------------------------------------# function checkout_system() { local source_dir=$1 local github_link=$2 local tag_or_branch_name=$3 - if [ -d "${source_dir}" ]; then + if [[ -d "${source_dir}" ]]; then echo -e "${GREEN}==>${NC} Source dir already exists, updating it ...\n" else echo -e "${GREEN}==>${NC} Cloning your fork repository...\n" git clone ${github_link} ${source_dir} - if [ ! -d "${source_dir}" ]; then + if [[ ! -d "${source_dir}" ]]; then echo -e "${RED}==>${NC} An error occurred while cloning your fork. Possible causes: wrong URL, user or password.\n" exit -1 fi @@ -51,48 +81,91 @@ function checkout_system() { #-----------------------------------------------------------------------------# -if [ $# -lt 1 ] +#---~--- +# Retrieve configuration. +#---~--- +#--- Default settings (all empty) +MONAN_ONETWO="" +github_link_MONAN="" +tag_or_branch_name_MONAN="" +github_link_CONVERT_MPAS="" +tag_or_branch_name_CONVERT_MPAS="" +#---~--- + + +#---~--- +# Parse arguments +#---~--- +while [[ ${#} > 0 ]] +do + key="${1}" + case ${key} in + -bc) + tag_or_branch_name_CONVERT_MPAS="${2}" + shift 2 # past flag and argument + ;; + -bm) + tag_or_branch_name_MONAN="${2}" + shift 2 # past flag and argument + ;; + -gc) + github_link_CONVERT_MPAS="${2}" + shift 2 # past flag and argument + ;; + -gm) + github_link_MONAN="${2}" + shift 2 # past flag and argument + ;; + -h) + show_usage + exit 0 + ;; + -m12) + MONAN_ONETWO="${key}" + shift 1 # past flag + ;; + *) + echo "" + echo " Option \"${key}\" is not valid." + echo "" + show_usage + echo "" + echo " *** FATAL ERROR! ***" + echo " Unknown key or key-value argument pair." + echo "" + exit 2 + ;; + esac +done +#---~--- + +#---~--- +# Make sure all required settings were provided. +#---~--- +if [[ "${tag_or_branch_name_CONVERT_MPAS}" == "" ]] || + [[ "${tag_or_branch_name_MONAN}" == "" ]] || + [[ "${github_link_CONVERT_MPAS}" == "" ]] || + [[ "${github_link_MONAN}" == "" ]] then - echo "" - echo "Instructions: execute the command below" - echo "" - echo "${0} [G] [M] [C]" - echo "" - echo "G :: MONAN GitHub link of your personal fork, eg: https://github.com/MYUSER/MONAN-Model.git" - echo "M :: MONAN tag or branch name of your personal fork. (will be used 'develop' if not informed)" - echo "C :: Convert_MPAS tag from ${github_link_CONVERT_MPAS} (will be used 'develop' if not informed)" - echo "" - exit + echo " This script requires arguments to be set through flags." + show_usage + exit 2 fi +#---~--- -# Set environment variables exports: -echo "" -echo -e "\033[1;32m==>\033[0m Moduling environment for MONAN model...\n" -. setenv.bash +#--- Set environment variables exports: +. setenv.bash ${MONAN_ONETWO} +#---~--- + echo "" echo "---- Installing the Model ----" echo "" -# Standart directories variables:--------------------------------------- -DIRHOMES=${DIR_SCRIPTS}/scripts_CD-CT; mkdir -p ${DIRHOMES} -DIRHOMED=${DIR_DADOS}/scripts_CD-CT; mkdir -p ${DIRHOMED} -SCRIPTS=${DIRHOMES}/scripts; mkdir -p ${SCRIPTS} -DATAIN=${DIRHOMED}/datain; mkdir -p ${DATAIN} -DATAOUT=${DIRHOMED}/dataout; mkdir -p ${DATAOUT} -SOURCES=${DIRHOMES}/sources; mkdir -p ${SOURCES} -EXECS=${DIRHOMED}/execs; mkdir -p ${EXECS} -#---------------------------------------------------------------------- - # Input variables:----------------------------------------------------- -github_link_MONAN=${1}; #github_link=https://github.com/monanadmin/MONAN-Model.git -tag_or_branch_name_MONAN=${2} -tag_or_branch_name_MONAN=${tag_or_branch_name_MONAN:="1.4.3-rc"} -echo "MONAN branch name in use: ${tag_or_branch_name_MONAN}" - -tag_or_branch_name_CONVERT_MPAS=${3} -tag_or_branch_name_CONVERT_MPAS=${tag_or_branch_name_CONVERT_MPAS:="1.2.0"} +tag_or_branch_name_MONAN=${tag_or_branch_name_MONAN:="release/1.4.3-rc"} +tag_or_branch_name_CONVERT_MPAS=${tag_or_branch_name_CONVERT_MPAS:="release/1.2.0"} echo "convert_mpas branch name in use: ${tag_or_branch_name_CONVERT_MPAS}" #---------------------------------------------------------------------- @@ -105,7 +178,7 @@ CONVERT_MPAS_DIR=${SOURCES}/convert_mpas_${tag_or_branch_name_CONVERT_MPAS} #$(sed -i "s;DIR_DADOS=.*$;DIR_DADOS=$(dirname $(dirname $(pwd)));" setenv.bash) $(sed -i "s;MONANDIR=.*$;MONANDIR=$MONANDIR;" setenv.bash) chmod 755 ${SCRIPTS}/setenv.bash -. ${SCRIPTS}/setenv.bash +. ${SCRIPTS}/setenv.bash ${MONAN_ONETWO} #---------------------------------------------------------------------- @@ -134,22 +207,23 @@ echo "*" echo "********************************************************************************" echo "" +echo -e "${GREEN}==>${NC} github_link_MONAN = ${github_link_MONAN}" echo -e "${GREEN}==>${NC} tag_or_branch_name_MONAN = ${tag_or_branch_name_MONAN}" echo "" -read -p "Are you sure you are installing the right versions scripts x MONAN-Model ? [Y/n]" confirma -confirma=${confirma:-Y} - -if [[ "${confirma}" =~ ^[Yy]$ ]] -then +read -p "Are you sure you are installing the right versions scripts x MONAN-Model ? [Y/n] " confirma +confirma=$(echo ${confirma:-Y} | tr [:upper:] [:lower:]) +case "${confirma}" in +y|yes) echo "" - echo -e "${GREEN}==>${NC} OK, so keep going." + echo -e "${GREEN}==>${NC} OK, proceeding to installation." echo "" -else + ;; +*) echo "" echo -e " ${RED}==>${NC} Please, make the right versions and try again." exit echo "" -fi +esac checkout_system ${MONANDIR} ${github_link_MONAN} ${tag_or_branch_name_MONAN} @@ -200,7 +274,7 @@ cat << EOF > make-all.sh # SHAREDLIB=true - generate position-independent code suitable for use in a shared library. Default is false. cd ${SCRIPTS} -. ${SCRIPTS}/setenv.bash +. ${SCRIPTS}/setenv.bash ${MONAN_ONETWO} cd $MONANDIR rm -rf $MONANDIR/default_inputs/ $MONANDIR/src/core_atmosphere/physics/physics_wrf/files @@ -244,7 +318,7 @@ mv ${MONANDIR}/init_atmosphere_model ${EXECS} make clean CORE=init_atmosphere -if [ -s "${EXECS}/init_atmosphere_model" ] && [ -e "${EXECS}/atmosphere_model" ]; then +if [[ -s "${EXECS}/init_atmosphere_model" ]] && [[ -e "${EXECS}/atmosphere_model" ]]; then echo "" echo -e "${GREEN}==>${NC} Files init_atmosphere_model and atmosphere_model generated Successfully in ${EXECS} !" echo @@ -261,14 +335,14 @@ echo -e "${GREEN}==>${NC} Installing init_atmosphere_model and atmosphere_model echo "" . ${MONANDIR}/make-all.sh -if [ "$HOSTNAME" == "ian" ]; then +if [[ "$HOSTNAME" == "ian" ]]; then # echo "hostname=$HOSTNAME" export PATH=$NETCDF/bin:$PATH fi # install convert_mpas echo "" -echo -e "${GREEN}==>${NC} Moduling environment for convert_mpas...\n" +echo -e "${GREEN}==>${NC} Loading modules needed by convert_mpas...\n" cd ${CONVERT_MPAS_DIR} make clean @@ -279,7 +353,7 @@ mv ${CONVERT_MPAS_DIR}/convert_mpas ${EXECS}/ cp ${CONVERT_MPAS_DIR}/VERSION.txt ${EXECS}/CONVMPAS-VERSION.txt -if [ -s "${EXECS}/convert_mpas" ] ; then +if [[ -s "${EXECS}/convert_mpas" ]] ; then echo "" echo -e "${GREEN}==>${NC} File convert_mpas generated Sucessfully in ${CONVERT_MPAS_DIR} and copied to ${EXECS} !" echo diff --git a/scripts/2.pre_processing.bash b/scripts/2.pre_processing.bash index baba5e6..aed2e5b 100755 --- a/scripts/2.pre_processing.bash +++ b/scripts/2.pre_processing.bash @@ -18,33 +18,121 @@ umask 022 # #-----------------------------------------------------------------------------# -if [ $# -ne 4 -a $# -ne 1 ] -then + + +#--- Function that shows usage. +function show_usage() { + echo " Usage: " echo "" - echo "Instructions: execute the command below" + echo " ${0} [-h] [-m12] [-o] [-e EXP ] [-f FCST] [-r RES] [-t YYYYMMDDHH]" echo "" - echo "${0} EXP_NAME/OP RESOLUTION LABELI FCST" + echo " List of optional flags: " echo "" - echo "EXP_NAME :: Forcing: GFS" - echo " :: Others options to be added later..." - echo "RESOLUTION :: number of points in resolution model grid, e.g: 1024002 (24 km)" - echo " 40962 (120 km)" - echo "LABELI :: Initial date YYYYMMDDHH, e.g.: 2024010100" - echo "FCST :: Forecast hours, e.g.: 24 or 36, etc." + echo " -h -- Shows this message." + echo " -m12 -- Is this a MONAN run based on 1.2.0-rc and branches derived" + echo " from this version (e.g., feature/monan-757-NF)? This is a" + echo " temporary flag that will be removed once the versions" + echo " containing Noah-MP are merged into the new release. This" + echo " allows the script to manage older code and still run on jaci." + echo " -o -- Overwrite static files." echo "" - echo "24 hour forecast example for 24km:" - echo "${0} GFS 1024002 2024010100 24" - echo "48 hour forecast example for 120km:" - echo "${0} GFS 40962 2024010100 48" + echo " List of **required** flags when -c is not set: " echo "" - - exit + echo " -e EXP -- meteorological drivers. For example, GFS" + echo " -f FCST -- Simulation length in hours, e.g., 24 or 48." + echo " -r RES -- grid resolution. Supported options are:" + echo " 65536002 (~ 3 km)" + echo " 5898242 (~ 10 km)" + echo " 2621442 (~ 15 km)" + echo " 1024002 (~ 24 km)" + echo " 655362 (~ 30 km)" + echo " 163842 (~ 60 km)" + echo " 40962 (~ 120 km)" + echo " -t YYYYMMDDHH -- Initial time. For example if 22 Sept 2025 00 UTC, set it to:" + echo " 2025092200" + echo "" +} +#---~--- + + + + +#--- Default input variables: +OVERWRITE=false +MONAN_ONETWO="" +EXP="" +RES="" +YYYYMMDDHHi="" +FCST="" +#---~--- + + +#--- Parse arguments. +while [[ ${#} > 0 ]] +do + key="${1}" + case ${key} in + -e) + EXP="${2}" + shift 2 # past flag and argument + ;; + -f) + FCST="${2}" + shift 2 # past flag and argument + ;; + -h) + show_usage + exit 0 + ;; + -m12) + MONAN_ONETWO="${key}" + shift 1 # past flag + ;; + -o) + OVERWRITE=true + shift 1 # past flag + ;; + -r) + RES="${2}" + shift 2 # past flag and argument + ;; + -t) + YYYYMMDDHHi="${2}" + shift 2 # past flag and argument + ;; + *) + echo "" + echo " Option \"${key}\" is not valid." + echo "" + show_usage + echo "" + echo " *** FATAL ERROR! ***" + echo " Unknown key or key-value argument pair." + echo "" + exit 2 + ;; + esac +done +#---~--- + + + +#---~--- +# Make sure all required settings were provided. +#---~--- +if [[ "${EXP}" == "" ]] || [[ "${RES}" == "" ]] || + [[ "${YYYYMMDDHHi}" == "" ]] || [[ "${FCST}" == "" ]] +then + echo " This script requires some arguments to be set through flags." + show_usage + exit 2 fi +#---~--- -# Set environment variables exports: -echo "" -echo -e "\033[1;32m==>\033[0m Moduling environment for MONAN model...\n" -. setenv.bash + +#--- Set environment variables exports: +. setenv.bash ${MONAN_ONETWO} +#---~--- echo "" @@ -52,25 +140,6 @@ echo "---- Pre Processing ----" echo "" -# Standart directories variables:--------------------------------------- -DIRHOMES=${DIR_SCRIPTS}/scripts_CD-CT; mkdir -p ${DIRHOMES} -DIRHOMED=${DIR_DADOS}/scripts_CD-CT; mkdir -p ${DIRHOMED} -SCRIPTS=${DIRHOMES}/scripts; mkdir -p ${SCRIPTS} -DATAIN=${DIRHOMED}/datain; mkdir -p ${DATAIN} -DATAOUT=${DIRHOMED}/dataout; mkdir -p ${DATAOUT} -SOURCES=${DIRHOMES}/sources; mkdir -p ${SOURCES} -EXECS=${DIRHOMED}/execs; mkdir -p ${EXECS} -#---------------------------------------------------------------------- - - -# Input variables:-------------------------------------- -EXP=${1}; #EXP=GFS -RES=${2}; #RES=1024002 -YYYYMMDDHHi=${3}; #YYYYMMDDHHi=2024012000 -FCST=${4}; #FCST=24 -#------------------------------------------------------- - - # Local variables-------------------------------------- # Calculating CIs and final forecast dates in model namelist format: yyyymmddi=${YYYYMMDDHHi:0:8} @@ -86,7 +155,7 @@ echo -e "${GREEN}==>${NC} Scripts_CD-CT last commit: \n" git log | head -1 -if [ ! -d ${DATAIN}/fixed ] +if [[ ! -d ${DATAIN}/fixed ]] then echo -e "${GREEN}==>${NC} copying and linking fixed input data ${SYSTEM_KEYC}... \n" mkdir -p ${DATAIN} @@ -96,29 +165,31 @@ then fi -# Creating the x1.${RES}.static.nc file once, if does not exist yet:--------------- -if [ ! -s ${DATAIN}/fixed/x1.${RES}.static.nc ] +#---~--- +# Create the x1.${RES}.static.nc file. This is normally needed only once, when the file +# does not exist. However, if the files must be recreated for whichever reason, option +# OVERWRITE forces creation. +#---~--- +if ${OVERWRITE} || [[ ! -s ${DATAIN}/fixed/x1.${RES}.static.nc ]] then - echo -e "${GREEN}==>${NC} Creating static.bash for submiting init_atmosphere to create x1.${RES}.static.nc...\n" - time ./make_static.bash ${EXP} ${RES} ${YYYYMMDDHHi} ${FCST} + echo -e "${GREEN}==>${NC} Creating static.bash for submitting init_atmosphere to create x1.${RES}.static.nc...\n" + time ./make_static.bash ${MONAN_ONETWO} -e ${EXP} -f ${FCST} -r ${RES} -t ${YYYYMMDDHHi} else echo -e "${GREEN}==>${NC} File x1.${RES}.static.nc already exist in ${DATAIN}/fixed.\n" fi -#---------------------------------------------------------------------------------- - - -# Degrib phase:--------------------------------------------------------------------- -echo -e "${GREEN}==>${NC} Running Degrib:\n" -time ./make_degrib.bash ${EXP} ${RES} ${YYYYMMDDHHi} ${FCST} -#---------------------------------------------------------------------------------- +#---~--- +#--- Run the degrib step. +echo -e "${GREEN}==>${NC} Submitting Degrib...\n" +time ./make_degrib.bash ${MONAN_ONETWO} -e ${EXP} -f ${FCST} -r ${RES} -t ${YYYYMMDDHHi} +#---~--- -# Init Atmosphere phase:------------------------------------------------------------ -echo -e "${GREEN}==>${NC} Running Init Atmosphere...\n" -time ./make_initatmos.bash ${EXP} ${RES} ${YYYYMMDDHHi} ${FCST} -#---------------------------------------------------------------------------------- +#--- Run the atmosphere initialisation step. +echo -e "${GREEN}==>${NC} Submitting Init Atmosphere...\n" +time ./make_initatmos.bash ${MONAN_ONETWO} -e ${EXP} -f ${FCST} -r ${RES} -t ${YYYYMMDDHHi} +#---~--- diff --git a/scripts/3.run_model.bash b/scripts/3.run_model.bash index 0165d95..9db902d 100755 --- a/scripts/3.run_model.bash +++ b/scripts/3.run_model.bash @@ -16,71 +16,165 @@ umask 022 # #-----------------------------------------------------------------------------# -if [ $# -ne 4 -a $# -ne 1 ] -then + +#--- Function that shows usage. +function show_usage() { + echo " Usage: " echo "" - echo "Instructions: execute the command below" + echo " ${0} [-h] [-m12] [-v VARTABLE] [-d OUTPUT_DIAG_INT] [-e EXP ] [-f FCST] \\" + echo " [-l NLEV] [-r RES] [-t YYYYMMDDHH]" echo "" - echo "${0} [EXP_NAME/OP] RESOLUTION LABELI FCST" + echo " List of optional flags: " echo "" - echo "EXP_NAME :: Forcing: GFS" - echo "RESOLUTION :: number of points in resolution model grid, e.g: 1024002 (24 km)" - echo "LABELI :: Initial date YYYYMMDDHH, e.g.: 2024010100" - echo "FCST :: Forecast hours, e.g.: 24 or 36, etc." + echo " -h -- Shows this message." + echo " -m12 -- Is this a MONAN run based on 1.2.0-rc and branches" + echo " derived from this version (e.g., feature/monan-757-NF)?" + echo " This is a temporary flag that will be removed once the" + echo " versions containing Noah-MP are merged into the new" + echo " release. This allows the script to manage older code and" + echo " still run on jaci." + echo " -v VARTABLE -- Suffix for defining which version of the" + echo " stream_list_atmosphere.diagnostics template to use." + echo " The default is to not use any suffix." echo "" - echo "24 hour forecast example for 24km:" - echo "${0} GFS 1024002 2024010100 24" - echo "48 hour forecast example for 120km:" - echo "${0} GFS 40962 2024010100 48" + echo " List of **required** flags when -c is not set: " echo "" + echo " -d OUTPUT_DIAG_INT -- Output interval for diagnostic. The format must be" + echo " \"HH:MM:SS\"" + echo " -e EXP -- meteorological drivers. For example, GFS" + echo " -f FCST -- Simulation length in hours, e.g., 24 or 48." + echo " -l NLEV -- Number of vertical levels for the output." + echo " -r RES -- grid resolution. Supported options are:" + echo " 65536002 (~ 3 km)" + echo " 5898242 (~ 10 km)" + echo " 2621442 (~ 15 km)" + echo " 1024002 (~ 24 km)" + echo " 655362 (~ 30 km)" + echo " 163842 (~ 60 km)" + echo " 40962 (~ 120 km)" + echo " -t YYYYMMDDHH -- Initial time. For example if 22 Sept 2025 00 UTC," + echo " set it to: 2025092200" + echo "" +} +#---~--- + + + + +#--- Default input variables: +MONAN_ONETWO="" +EXP="" +RES="" +YYYYMMDDHHi="" +FCST="" +NLEV="" +OUTPUT_DIAG_INTERVAL="" +VARTABLE="" +#---~--- + - exit +#--- Parse arguments. +while [[ ${#} > 0 ]] +do + key="${1}" + case ${key} in + -d) + OUTPUT_DIAG_INTERVAL="${2}" + shift 2 # Past flag and argument + ;; + -e) + EXP="${2}" + shift 2 # past flag and argument + ;; + -f) + FCST="${2}" + shift 2 # past flag and argument + ;; + -h) + show_usage + exit 0 + ;; + -l) + NLEV="${2}" + shift 2 # Past flag and argument + ;; + -m12) + MONAN_ONETWO="${key}" + shift 1 # past flag + ;; + -r) + RES="${2}" + shift 2 # past flag and argument + ;; + -t) + YYYYMMDDHHi="${2}" + shift 2 # past flag and argument + ;; + -v) + VFIRST=$(echo ${2} | cut -c 1-1) + case "${VFIRST}" in + .) VARTABLE="${2}" ;; + *) VARTABLE=".${2}" ;; + esac + shift 2 # past flag and argument + ;; + *) + echo "" + echo " Option \"${key}\" is not valid." + echo "" + show_usage + echo "" + echo " *** FATAL ERROR! ***" + echo " Unknown key or key-value argument pair." + echo "" + exit 2 + ;; + esac +done +#---~--- + + + +#---~--- +# Make sure all required settings were provided. +#---~--- +if [[ "${EXP}" == "" ]] || [[ "${RES}" == "" ]] || + [[ "${YYYYMMDDHHi}" == "" ]] || [[ "${FCST}" == "" ]] || + [[ "${NLEV}" == "" ]] || [[ "${OUTPUT_DIAG_INTERVAL}" == "" ]] +then + echo " This script requires some arguments to be set through flags." + show_usage + exit 2 fi +#---~--- -# Set environment variables exports: -echo "" -echo -e "\033[1;32m==>\033[0m Moduling environment for MONAN model...\n" -. setenv.bash + +#--- Set environment variables exports: +. setenv.bash ${MONAN_ONETWO} +#---~--- echo "" echo "---- Run Model ----" echo "" -# Standart directories variables:--------------------------------------- -DIRHOMES=${DIR_SCRIPTS}/scripts_CD-CT; mkdir -p ${DIRHOMES} -DIRHOMED=${DIR_DADOS}/scripts_CD-CT; mkdir -p ${DIRHOMED} -SCRIPTS=${DIRHOMES}/scripts; mkdir -p ${SCRIPTS} -DATAIN=${DIRHOMED}/datain; mkdir -p ${DATAIN} -DATAOUT=${DIRHOMED}/dataout; mkdir -p ${DATAOUT} -SOURCES=${DIRHOMES}/sources; mkdir -p ${SOURCES} -EXECS=${DIRHOMED}/execs; mkdir -p ${EXECS} -#---------------------------------------------------------------------- - - -# Input variables:-------------------------------------- -EXP=${1}; #EXP=GFS -RES=${2}; #RES=1024002 -YYYYMMDDHHi=${3}; #YYYYMMDDHHi=2024012000 -FCST=${4}; #FCST=6 -#------------------------------------------------------- +#--- Set and create standard directories mkdir -p ${DATAOUT}/${YYYYMMDDHHi}/Model/logs +export DIRRUN=${DIRHOMED}/run.${YYYYMMDDHHi}; rm -fr ${DIRRUN}; mkdir -p ${DIRRUN} +#---~--- # Local variables-------------------------------------- start_date=${YYYYMMDDHHi:0:4}-${YYYYMMDDHHi:4:2}-${YYYYMMDDHHi:6:2}_${YYYYMMDDHHi:8:2}:00:00 cores=${MODEL_ncores} hhi=${YYYYMMDDHHi:8:2} -NLEV=55 CONFIG_CONV_INTERVAL="00:30:00" -VARTABLE=".OPER" -export DIRRUN=${DIRHOMED}/run.${YYYYMMDDHHi}; rm -fr ${DIRRUN}; mkdir -p ${DIRRUN} #------------------------------------------------------------------------------------ -# Variables for flex outpout interval from streams.atmosphere------------------------ -t_strout=$(cat ${SCRIPTS}/namelists/streams.atmosphere.TEMPLATE | sed -n '//s/.*output_interval="\([^"]*\)".*/\1/p') +# Variables for flex outpout interval ------------------------ +t_strout=${OUTPUT_DIAG_INTERVAL} t_stroutsec=$(echo ${t_strout} | awk -F: '{print ($1 * 3600) + ($2 * 60) + $3}') -t_strouthor=$(echo "scale=4; (${t_stroutsec}/60)/60" | bc) +t_strouthor=`echo "scale=4; (${t_stroutsec}/60)/60" | bc` #------------------------------------------------------------------------------------ # Format to HH:MM:SS t_strout (output_interval) @@ -88,42 +182,64 @@ IFS=":" read -r h m s <<< "${t_strout}" printf -v t_strout "%02d:%02d:%02d" "$h" "$m" "$s" # From now on, CONFI_LEN_DISP becames cte = 0.0, pickin up this value from static file. -# Calculating default parameters for different resolutions -if [ $RES -eq 1024002 ]; then #24Km - CONFIG_DT=150.0 +#---~--- +# Set default parameters according to the requested resolution. +#---~--- +case ${RES} in +65536002) #3km + CONFIG_DT=18.0 + CONFIG_LEN_DISP=3000.0 CONFIG_CONV_INTERVAL="00:15:00" -elif [ $RES -eq 2621442 ]; then #15Km - CONFIG_DT=90.0 + ;; +5898242) #10km + CONFIG_DT=60.0 + CONFIG_LEN_DISP=10000.0 CONFIG_CONV_INTERVAL="00:15:00" -elif [ $RES -eq 40962 ]; then #120Km - CONFIG_DT=600.0 + ;; +2621442) #15Km + CONFIG_DT=90.0 + CONFIG_LEN_DISP=15000.0 CONFIG_CONV_INTERVAL="00:15:00" -elif [ $RES -eq 163842 ]; then #60Km - CONFIG_DT=300.0 + ;; +1024002) #24Km + CONFIG_DT=150.0 + CONFIG_LEN_DISP=24000.0 CONFIG_CONV_INTERVAL="00:15:00" -elif [ $RES -eq 655362 ]; then #30Km + ;; +655362) #30Km CONFIG_DT=150.0 + CONFIG_LEN_DISP=30000.0 CONFIG_CONV_INTERVAL="00:15:00" -elif [ $RES -eq 5898242 ]; then #10Km - CONFIG_DT=60.0 + ;; +163842) #60Km + CONFIG_DT=300.0 + CONFIG_LEN_DISP=60000.0 CONFIG_CONV_INTERVAL="00:15:00" -elif [ $RES -eq 65536002 ]; then #3Km - CONFIG_DT=18.0 + ;; +40962) #120Km + CONFIG_DT=600.0 + CONFIG_LEN_DISP=120000.0 CONFIG_CONV_INTERVAL="00:15:00" -fi -#------------------------------------------------------- + ;; +*) + echo -e "${ORANGE}****** WARNING ******${NC} \n" + echo -e "${ORANGE}==>${NC} Provided grid resolution (${RES}) is not recognised.\n" + echo -e "${ORANGE}==>${NC} We cannot guarantee that MONAN will run fine.\n" + ;; +esac +#---~--- # Calculating final forecast dates in model namelist format: DD_HH:MM:SS # using: start_date(yyyymmdd) + FCST(hh) : -ind=$(printf "%02d\n" $(echo "${FCST}/24" | bc)) -inh=$(printf "%02.0f\n" $(echo "((${FCST}/24)-${ind})*24" | bc -l)) +ind=`printf "%02d\n" $(echo "${FCST}/24" | bc)` +inh=`printf "%02.0f\n" $(echo "((${FCST}/24)-${ind})*24" | bc -l)` DD_HHMMSS_forecast=$(echo "${ind}_${inh}:00:00") -if [ ! -s ${DATAIN}/fixed/x1.${RES}.graph.info.part.${cores} ] +if [[ ! -s ${DATAIN}/fixed/x1.${RES}.graph.info.part.${cores} ]] then - if [ ! -s ${DATAIN}/fixed/x1.${RES}.graph.info ] + if [[ ! -s ${DATAIN}/fixed/x1.${RES}.graph.info ]] then cd ${DATAIN}/fixed echo -e "${GREEN}==>${NC} downloading meshes tgz files ... \n" @@ -142,9 +258,9 @@ fi files_needed=("${SCRIPTS}/namelists/stream_list.atmosphere.output" ""${SCRIPTS}/namelists/stream_list.atmosphere.diagnostics${VARTABLE} "${SCRIPTS}/namelists/stream_list.atmosphere.surface" "${EXECS}/atmosphere_model" "${DATAIN}/fixed/x1.${RES}.static.nc" "${DATAIN}/fixed/x1.${RES}.graph.info.part.${cores}" "${DATAOUT}/${YYYYMMDDHHi}/Pre/x1.${RES}.init.nc" "${DATAIN}/fixed/Vtable.GFS") for file in "${files_needed[@]}" do - if [ ! -s "${file}" ] + if [[ ! -s "${file}" ]] then - echo -e "\n${RED}==>${NC} ***** ATTENTION *****\n" + echo -e "\n${RED}==>${NC} ***** FATAL ERROR *****\n" echo -e "${RED}==>${NC} [${0}] At least the file ${file} was not generated. \n" exit -1 fi @@ -160,15 +276,22 @@ cp -f ${DATAOUT}/${YYYYMMDDHHi}/Pre/x1.${RES}.init.nc ${DIRRUN} cp -f ${DATAIN}/fixed/Vtable.GFS ${DIRRUN} -if [ ${EXP} = "GFS" ] -then +# ML: I replaced the if construction with a case one, as this is a case selection, but I +# wonder if the sed commands should be applied in all cases instead of only when EXP +# is set to GFS... +case "${EXP}" in +GFS) sed -e "s,#LABELI#,${start_date},g;s,#FCSTS#,${DD_HHMMSS_forecast},g;s,#RES#,${RES},g; s,#CONFIG_DT#,${CONFIG_DT},g;s,#CONFIG_LEN_DISP#,${CONFIG_LEN_DISP},g;s,#CONFIG_CONV_INTERVAL#,${CONFIG_CONV_INTERVAL},g" \ ${SCRIPTS}/namelists/namelist.atmosphere.TEMPLATE > ${DIRRUN}/namelist.atmosphere - - sed -e "s,#RES#,${RES},g;s,#CIORIG#,${EXP},g;s,#LABELI#,${YYYYMMDDHHi},g;s,#NLEV#,${NLEV},g" \ + + sed -e "s,#RES#,${RES},g;s,#CIORIG#,${EXP},g;s,#LABELI#,${YYYYMMDDHHi},g;s,#NLEV#,${NLEV},g; +s,#OUTPUT_DIAG_INTERVAL#,${OUTPUT_DIAG_INTERVAL},g" \ ${SCRIPTS}/namelists/streams.atmosphere.TEMPLATE > ${DIRRUN}/streams.atmosphere -fi + ;; +esac + + cp -f ${SCRIPTS}/namelists/stream_list.atmosphere.output ${DIRRUN} cp -f ${SCRIPTS}/namelists/stream_list.atmosphere.diagnostics${VARTABLE} ${DIRRUN}/stream_list.atmosphere.diagnostics cp -f ${SCRIPTS}/namelists/stream_list.atmosphere.surface ${DIRRUN} @@ -179,7 +302,7 @@ chmod 755 ${DIRRUN} rm -f ${DIRRUN}/model.bash -if [ ${SCHEDULER_SYSTEM} != "GENERIC" ] +if [[ ${SCHEDULER_SYSTEM} != "GENERIC" ]] then sed -e "s,#JOBNAME#,${MODEL_jobname},g; s,#NNODES#,${MODEL_nnodes},g; @@ -205,18 +328,26 @@ ulimit -v unlimited ulimit -s unlimited cd ${DIRRUN} -. ${SCRIPTS}/setenv.bash +. ${SCRIPTS}/setenv.bash ${MONAN_ONETWO} date beg_secs=\`date +"%s"\` -if [ "$HOSTNAME" = "egeon" ]; then +#ML: Replace HOSTNAME with SCHEDULER_SYSTEM so this can be expanded to other HPC environments more easily. +case "${SCHEDULER_SYSTEM}" in +SLURM) echo "-- SLURM_JOB_ID: \$SLURM_JOB_ID" time mpirun -np ${MODEL_ncores} ./\${executable} -else + ;; +PBS) echo "-- PBS_JOBID: \$PBS_JOBID" time mpirun --ppn ${MODEL_ncpn} -np ${MODEL_ncores} --depth=${MODEL_nthreads} --cpu-bind depth ./\${executable} -fi + ;; +*) + echo "-- GENERIC:" + time mpirun -np ${MODEL_ncores} ./\${executable} + ;; +esac date end_secs=\`date +"%s"\` @@ -240,32 +371,35 @@ chmod a+x ${DIRRUN}/model.bash case "${SCHEDULER_SYSTEM}" in - SLURM) - echo -e "${GREEN}==>${NC} Submitting MONAN atmosphere model and waiting for finish before exit... \n" - echo -e "${GREEN}==>${NC} Logs being generated at ${DATAOUT}/logs... \n" - echo -e "sbatch ${SCRIPTS}/model.bash" - cd ${DIRRUN} - sbatch --wait ${DIRRUN}/model.bash - ;; - PBS) - echo -e "${GREEN}==>${NC} Submitting MONAN atmosphere model and waiting for finish before exit... \n" - echo -e "${GREEN}==>${NC} Logs being generated at ${DATAOUT}/logs... \n" - echo -e "${GREEN}==>${NC} qsub model.bash...\n" - cd ${DIRRUN} - qsub -W block=true ${DIRRUN}/model.bash - ;; -# GENERIC) -# echo "Nenhum gerenciador detectado" -# cd ${DIRRUN} -# ${DIRRUN}/model.bash -# ;; +SLURM) + echo -e "${GREEN}==>${NC} Submitting MONAN atmosphere model and waiting for finish before exit... \n" + echo -e "${GREEN}==>${NC} Logs being generated at ${DATAOUT}/logs... \n" + echo -e "sbatch ${SCRIPTS}/model.bash" + cd ${DIRRUN} + sbatch --wait ${DIRRUN}/model.bash + ;; +PBS) + echo -e "${GREEN}==>${NC} Submitting MONAN atmosphere model and waiting for finish before exit... \n" + echo -e "${GREEN}==>${NC} Logs being generated at ${DATAOUT}/logs... \n" + echo -e "${GREEN}==>${NC} qsub model.bash...\n" + cd ${DIRRUN} + qsub -W block=true ${DIRRUN}/model.bash + ;; +#GENERIC) +# echo "Nenhum gerenciador detectado" +# cd ${DIRRUN} +# ${DIRRUN}/model.bash +# ;; esac mv ${DIRRUN}/model.bash ${DATAOUT}/${YYYYMMDDHHi}/Model/logs -#-----Loop que verifica se os arquivos foram gerados corretamente (>0)----- +#---~--- +# Loop that checks whether the files were properly produced or not. +#---~--- output_interval=${t_strouthor} nfiles=$(echo "$FCST/$output_interval + 1" | bc) +missed=false for ii in $(seq 1 ${nfiles}) do i=$(printf "%04d" ${ii}) @@ -273,13 +407,20 @@ do currentdate=$(date -d "${YYYYMMDDHHi:0:8} ${hh}:00:00 $(echo "(${i}-1)*${t_strout:0:2}" | bc) hours $(echo "(${i}-1)*${t_strout:3:2}" | bc) minutes $(echo "(${i}-1)*${t_strout:6:2}" | bc) seconds" +"%Y%m%d%H.%M.%S") file=MONAN_DIAG_G_MOD_${EXP}_${YYYYMMDDHHi}_${currentdate}.x${RES}L${NLEV}.nc - if [ ! -s ${DATAOUT}/${YYYYMMDDHHi}/Model/${file} ] + if [[ ! -s ${DATAOUT}/${YYYYMMDDHHi}/Model/${file} ]] then - echo -e "\n${RED}==>${NC} ***** ATTENTION *****\n" - echo -e "${RED}==>${NC} [${0}] At least the file ${DATAOUT}/${YYYYMMDDHHi}/Model/${file} was not generated. \n" - exit -1 - fi + if ! ${missed} + then + echo -e "\n${RED}==>${NC} ***** FATAL ERROR *****\n" + missed=true + fi + echo -e "${RED}==>${NC} [${0}] File ${DATAOUT}/${YYYYMMDDHHi}/Model/${file} was not generated. \n" + fi done - -rm -fr ${DIRRUN} +if ${missed} +then + exit -1 +else + rm -fr ${DIRRUN} +fi diff --git a/scripts/4.run_post.bash b/scripts/4.run_post.bash index a65efb8..b64731d 100755 --- a/scripts/4.run_post.bash +++ b/scripts/4.run_post.bash @@ -16,155 +16,327 @@ umask 022 # #-----------------------------------------------------------------------------# -if [ $# -ne 4 -a $# -ne 1 ] -then +#--- Function that shows usage. +function show_usage() { + echo " Usage: " + echo "" + echo " ${0} [-h] [-m12] [-v VARTABLE] [-d OUTPUT_DIAG_INT] [-e EXP ] [-f FCST] \\" + echo " [-l N_MODEL_LEV] [-r RES] [-t YYYYMMDDHH]" echo "" - echo "Instructions: execute the command below" + echo " List of optional flags: " echo "" - echo "${0} ]EXP_NAME/OP] RESOLUTION LABELI FCST" + echo " -h -- Shows this message." + echo " -m12 -- Is this a MONAN run based on 1.2.0-rc and branches" + echo " derived from this version (e.g., feature/monan-757-NF)?" + echo " This is a temporary flag that will be removed once the" + echo " versions containing Noah-MP are merged into the new" + echo " release. This allows the script to manage older code and" + echo " still run on jaci." + echo " -v VARTABLE -- Suffix for defining which version of the" + echo " stream_list_atmosphere.diagnostics template to use." + echo " The default is to not use any suffix." echo "" - echo "EXP_NAME :: Forcing: GFS" - echo "RESOLUTION :: number of points in resolution model grid, e.g: 1024002 (24 km)" - echo "LABELI :: Initial date YYYYMMDDHH, e.g.: 2024010100" - echo "FCST :: Forecast hours, e.g.: 24 or 36, etc." + echo " List of **required** flags when -c is not set: " echo "" - echo "24 hour forcast example:" - echo "${0} GFS 1024002 2024010100 24" - echo "${0} GFS 40962 2024010100 48" + echo " -d OUTPUT_DIAG_INT -- Output interval for diagnostic. The format must be" + echo " \"HH:MM:SS\"" + echo " -e EXP -- meteorological drivers. For example, GFS" + echo " -f FCST -- Simulation length in hours, e.g., 24 or 48." + echo " -l N_MODEL_LEV -- Number of vertical levels for the output." + echo " -r RES -- grid resolution. Supported options are:" + echo " 65536002 (~ 3 km)" + echo " 5898242 (~ 10 km)" + echo " 2621442 (~ 15 km)" + echo " 1024002 (~ 24 km)" + echo " 655362 (~ 30 km)" + echo " 163842 (~ 60 km)" + echo " 40962 (~ 120 km)" + echo " -t YYYYMMDDHH -- Initial time. For example if 22 Sept 2025 00 UTC," + echo " set it to: 2025092200" echo "" +} +#---~--- - exit -fi -# Set environment variables exports: -echo "" -echo -e "\033[1;32m==>\033[0m Moduling environment for MONAN model...\n" -. setenv.bash + + +#--- Default input variables: +MONAN_ONETWO="" +EXP="" +RES="" +YYYYMMDDHHi="" +FCST="" +N_MODEL_LEV="" +OUTPUT_DIAG_INTERVAL="" +VARTABLE="" +#---~--- + + +#--- Parse arguments. +while [[ ${#} > 0 ]] +do + key="${1}" + case ${key} in + -d) + OUTPUT_DIAG_INTERVAL="${2}" + shift 2 # Past flag and argument + ;; + -e) + EXP="${2}" + shift 2 # past flag and argument + ;; + -f) + FCST="${2}" + shift 2 # past flag and argument + ;; + -h) + show_usage + exit 0 + ;; + -l) + N_MODEL_LEV="${2}" + shift 2 # Past flag and argument + ;; + -m12) + MONAN_ONETWO="${key}" + shift 1 # past flag + ;; + -r) + RES="${2}" + shift 2 # past flag and argument + ;; + -t) + YYYYMMDDHHi="${2}" + shift 2 # past flag and argument + ;; + -v) + VFIRST=$(echo ${2} | cut -c 1-1) + case "${VFIRST}" in + .) VARTABLE="${2}" ;; + *) VARTABLE=".${2}" ;; + esac + shift 2 # past flag and argument + ;; + *) + echo "" + echo " Option \"${key}\" is not valid." + echo "" + show_usage + echo "" + echo " *** FATAL ERROR! ***" + echo " Unknown key or key-value argument pair." + echo "" + exit 2 + ;; + esac +done +#---~--- + + +#--- Set environment variables exports: +. setenv.bash ${MONAN_ONETWO} +#---~--- + + + +#---~--- +# Make sure all required settings were provided. +#---~--- +if [[ "${EXP}" == "" ]] || [[ "${RES}" == "" ]] || + [[ "${YYYYMMDDHHi}" == "" ]] || [[ "${FCST}" == "" ]] || + [[ "${N_MODEL_LEV}" == "" ]] || [[ "${OUTPUT_DIAG_INTERVAL}" == "" ]] +then + echo " This script requires some arguments to be set through flags." + show_usage + exit 2 +fi +#---~--- echo "" echo "---- Run Post ----" echo "" -# Standart directories variables:--------------------------------------- -DIRHOMES=$(dirname "$(pwd)"); mkdir -p ${DIRHOMES} -DIRHOMED=${DIR_DADOS}/scripts_CD-CT; mkdir -p ${DIRHOMED} -export SCRIPTS=${DIRHOMES}/scripts; mkdir -p ${SCRIPTS} -DATAIN=${DIRHOMED}/datain; mkdir -p ${DATAIN} -DATAOUT=${DIRHOMED}/dataout; mkdir -p ${DATAOUT} -SOURCES=${DIRHOMES}/sources; mkdir -p ${SOURCES} -EXECS=${DIRHOMED}/execs; mkdir -p ${EXECS} -#---------------------------------------------------------------------- - - -# Input variables:-------------------------------------- -EXP=${1}; #EXP=GFS -RES=${2}; #RES=1024002 -YYYYMMDDHHi=${3}; #YYYYMMDDHHi=2024042000 -FCST=${4}; #FCST=40 -#------------------------------------------------------- +#--- Set and create standard directories mkdir -p ${DATAOUT}/${YYYYMMDDHHi}/Post/logs +export DIRRUN=${DIRHOMED}/run.${YYYYMMDDHHi}; rm -fr ${DIRRUN}; mkdir -p ${DIRRUN} +#---~--- # Local variables-------------------------------------- START_DATE_YYYYMMDD="${YYYYMMDDHHi:0:4}-${YYYYMMDDHHi:4:2}-${YYYYMMDDHHi:6:2}" START_HH="${YYYYMMDDHHi:8:2}" -maxpostpernode=30 # <------ qtde max de convert_mpas por no! -VARTABLE=".OPER" -export DIRRUN=${DIRHOMED}/run.${YYYYMMDDHHi}; rm -fr ${DIRRUN}; mkdir -p ${DIRRUN} -N_MODEL_LEV=55 -NLEV=18 +maxpostpernode=30 # <------ maximum number of convert_mpas processes per node! #------------------------------------------------------- -# Variables for flex outpout interval from streams.atmosphere------------------------ -t_strout=$(cat ${SCRIPTS}/namelists/streams.atmosphere.TEMPLATE | sed -n '//s/.*output_interval="\([^"]*\)".*/\1/p') -t_stroutsec=$(echo ${t_strout} | awk -F: '{print ($1 * 3600) + ($2 * 60) + $3}') -t_strouthor=$(echo "scale=4; (${t_stroutsec}/60)/60" | bc) +# Variables for flex output interval ------------------------ +t_strout=${OUTPUT_DIAG_INTERVAL} +t_stroutsec=`echo ${t_strout} | awk -F: '{print ($1 * 3600) + ($2 * 60) + $3}'` +t_strouthor=`echo "scale=4; (${t_stroutsec}/60)/60" | bc` #------------------------------------------------------------------------------------ # Format to HH:MM:SS t_strout (output_interval) IFS=":" read -r h m s <<< "${t_strout}" printf -v t_strout "%02d:%02d:%02d" "$h" "$m" "$s" -# Calculating default parameters for different resolutions -if [ $RES -eq 1024002 ]; then #24Km - NLAT=721 #180/0.25 - NLON=1441 #360/0.25 + +#---~--- +# Calculate default parameters for different resolutions. +# ML: The original numbers assumed 1 degree ~ 100 km. Across latitude and near the +# Equator, 1 degree ~ 111.2 km, so the regridded data ended up being slightly coarser +# than it needed to be. To ensure an average grid mesh in regular lon/lat that is +# close to the original resolution, we determine the average resolution based on +# the number of points in a full sphere (4*pi steradians), and pick the nearest +# integer number of points per degree. +# delta_xy = sqrt( 4*pi * (180/pi)^2 / NumberOfPoints) +# PointsPerDegree = round(1/delta_xy) +# NLON = 360 * PointsPerDegree + 1 +# NLAT = 180 * PointsPerDegree + 1 +#---~--- +case ${RES} in +65536002) + #---~--- + # 3 km, use 40 points per degree + #---~--- + NLAT=7201 + NLON=14401 STARTLAT=-90.0 STARTLON=0.0 ENDLAT=90.0 ENDLON=360.0 -elif [ $RES -eq 2621442 ]; then #15Km - NLAT=1201 #180/0.15 - NLON=2401 #360/0.15 + #---~--- + ;; +5898242) + #---~--- + # 10 km, use 12 points per degree + #---~--- + NLAT=2161 + NLON=4321 STARTLAT=-90.0 STARTLON=0.0 ENDLAT=90.0 ENDLON=360.0 -elif [ $RES -eq 40962 ]; then #120Km - NLAT=150 #180/1.2 - NLON=300 #360/1.2 + #---~--- + ;; +2621442) + #---~--- + # 15 km, use 8 points per degree + #---~--- + NLAT=1441 + NLON=2881 STARTLAT=-90.0 STARTLON=0.0 ENDLAT=90.0 ENDLON=360.0 -elif [ $RES -eq 163842 ]; then #60Km - NLAT=301 #180/0.6 - NLON=601 #360/0.6 + #---~--- + ;; +1024002) + #---~--- + # 24 km, use 5 points per degree + #---~--- + NLAT=901 + NLON=1801 STARTLAT=-90.0 STARTLON=0.0 ENDLAT=90.0 ENDLON=360.0 -elif [ $RES -eq 655362 ]; then #30Km - NLAT=601 #180/0.3 - NLON=1201 #360/0.3 + #---~--- + ;; +655362) + #---~--- + # 30 km, use 4 points per degree + #---~--- + NLAT=721 + NLON=1441 STARTLAT=-90.0 STARTLON=0.0 ENDLAT=90.0 ENDLON=360.0 -elif [ $RES -eq 5898242 ]; then #10Km - NLAT=1801 #180/0.10 (+1) - NLON=3601 #360/0.10 (+1) + #---~--- + ;; +163842) + #---~--- + # 60 km, use 2 points per degree + #---~--- + NLAT=361 + NLON=721 STARTLAT=-90.0 STARTLON=0.0 ENDLAT=90.0 ENDLON=360.0 -elif [ $RES -eq 65536002 ]; then #3Km - NLAT=6001 #180/0.03 - NLON=12001 #360/0.03 + #---~--- + ;; +40962) + #---~--- + # 120 km, use 1 points per degree + #---~--- + NLAT=181 + NLON=361 STARTLAT=-90.0 STARTLON=0.0 ENDLAT=90.0 ENDLON=360.0 + #---~--- + ;; +*) + #---~--- + # Unrecognised resolution + #---~--- + echo -e "${RED}****** FATAL ERROR ******${NC} \n" + echo -e "${RED}==>${NC} Provided grid resolution (${RES}) is not recognised.\n" + echo -e "${RED}==>${NC} ${0} cannot post-process this MONAN simulation.\n" + exit -1 + #---~--- + ;; +esac +#---~--- + +# Retrieve N_ISOBARIC_LEV from t_iso_levels in Registry_isobaric.xml: +if [[ -s ${MONANDIR}/src/core_atmosphere/diagnostics/Registry_isobaric.xml ]] +then + N_ISOBARIC_LEV=$(grep "t_iso_levels" ${MONANDIR}/src/core_atmosphere/diagnostics/Registry_isobaric.xml | grep definition | cut -d\" -f4) +else + N_ISOBARIC_LEV=18 fi -#------------------------------------------------------- files_needed=("${SCRIPTS}/namelists/include_fields.diag${VARTABLE}" "${SCRIPTS}/namelists/convert_mpas.nml" "${SCRIPTS}/namelists/target_domain.TEMPLATE" "${EXECS}/convert_mpas" "${DATAOUT}/${YYYYMMDDHHi}/Pre/x1.${RES}.init.nc") for file in "${files_needed[@]}" do - if [ ! -s "${file}" ] + if [[ ! -s "${file}" ]] then - echo -e "\n${RED}==>${NC} ***** ATTENTION *****\n" + echo -e "\n${RED}==>${NC} ***** FATAL ERROR *****\n" echo -e "${RED}==>${NC} [${0}] At least the file ${file} was not generated. \n" exit -1 fi done - -# Captura quantos arquivos do modelo tiverem para serem pos-processados e -# quando nos serao necessarios para executar ${maxpostpernode} convert_mpas por no: +#---~--- +# Tally the number of model output files to be postprocessed, and find out how +# many nodes are needed to process ${maxpostpernode} convert_mpas runs per node: #nfiles=$(ls -l ${DATAOUT}/${YYYYMMDDHHi}/Model/MONAN*nc | wc -l) # from streams.atmosphere.TEMPLATE in diagnostics the output_interval is flexible +#---~--- output_interval=${t_strouthor} #nfiles=FCST/output_interval + 1(time zero file) -nfiles=$(echo "$FCST/$output_interval + 1" | bc) +nfiles=$(echo "${FCST}/${output_interval} + 1" | bc) echo "${nfiles} post to submit." echo "Max ${maxpostpernode} submits per nodes." how_many_nodes ${nfiles} ${maxpostpernode} +#---~--- -# Cria os diretorios e arquivos/links para cada saida do convert_mpas: +#--- Set the python environment +case "${MONAN_ONETWO}" in +-m12) + . ${SCRIPTS}/setenv_python.bash + ;; +esac + + +#---~--- +# Make paths and create files/links for each convert_mpas output: +#---~--- cd ${DIRRUN} for ii in $(seq 1 ${nfiles}) @@ -174,30 +346,37 @@ do cp -f ${SCRIPTS}/setenv.bash ${DIRRUN}/dir.${i} cp -f ${SCRIPTS}/namelists/include_fields.diag${VARTABLE} ${DIRRUN}/dir.${i}/include_fields.diag${VARTABLE} cp -f ${DIRRUN}/dir.${i}/include_fields.diag${VARTABLE} ${DIRRUN}/dir.${i}/include_fields - sed -e "s,#NISOLEV#,${NLEV},g;s,#NMODELLEV#,${N_MODEL_LEV},g" \ + sed -e "s,#NISOLEV#,${N_ISOBARIC_LEV},g;s,#NMODELLEV#,${N_MODEL_LEV},g" \ ${SCRIPTS}/namelists/convert_mpas.nml > ${DIRRUN}/dir.${i}/convert_mpas.nml sed -e "s,#NLAT#,${NLAT},g;s,#NLON#,${NLON},g;s,#STARTLAT#,${STARTLAT},g;s,#ENDLAT#,${ENDLAT},g;s,#STARTLON#,${STARTLON},g;s,#ENDLON#,${ENDLON},g;" \ ${SCRIPTS}/namelists/target_domain.TEMPLATE > ${DIRRUN}/dir.${i}/target_domain done +#---~--- + cd ${DIRRUN} chmod -R 755 ${DIRRUN}/* -# Laco para criar os arquivos de submissao com os blocos de convertmpas para cada node: echo "scheduler system = " ${SCHEDULER_SYSTEM} echo "system key = " ${SYSTEM_KEY} echo "" -# Laco para criar os arquivos de submissao com os blocos de convertmpas para cada node: +#---~--- +# Loop that generates submission files that distributes chunks of convertmpas runs to +# each node: +#---~--- node=1 inicio=1 fim=$((maxpostpernode <= nfiles ? maxpostpernode : nfiles)) -while [ ${inicio} -le ${nfiles} ] +while [[ ${inicio} -le ${nfiles} ]] do rm -f ${DIRRUN}/PostAtmos_node.${node}.sh - if [ ${SCHEDULER_SYSTEM} != "GENERIC" ] - then + case "${SCHEDULER_SYSTEM}" in + GENERIC) + echo "#!/bin/bash " > ${DIRRUN}/PostAtmos_node.${node}.sh + ;; + *) sed -e "s,#JOBNAME#,MO.Pos${node},g; s,#NNODES#,${POST_nnodes},g; s,#NCPUS#,${POST_ncpus},g; @@ -210,24 +389,27 @@ do s,#ERRORJOB#,${DATAOUT}/${YYYYMMDDHHi}/Post/logs/PostAtmos_node.${node}.e,g" \ ${SCRIPTS}/stools/submit_${SYSTEM_KEY}.bash_TEMPLATE > \ ${DIRRUN}/PostAtmos_node.${node}.sh - else - echo "#!/bin/bash " > ${DIRRUN}/PostAtmos_node.${node}.sh - fi + ;; + esac cat << EOSH >> ${DIRRUN}/PostAtmos_node.${node}.sh -cd ${DIRRUN} -. ${SCRIPTS}/setenv.bash -chmod 755 ${DIRRUN}/* +MONAN_ONETWO="${MONAN_ONETWO}" +DIRRUN="${DIRRUN}" -echo "Executing posts ${inicio} to ${fim} in node Node ${node}." +. ${SCRIPTS}/setenv.bash ${MONAN_ONETWO} +chmod 755 \${DIRRUN}/* + +cd \${DIRRUN} + +echo "Submitting posts ${inicio} to ${fim} to node Node ${node}." for ii in \$(seq ${inicio} ${fim}) do i=\$(printf "%04d" \${ii}) echo "Preparing post files \${i}" - cp -f ${DATAOUT}/${YYYYMMDDHHi}/Pre/x1.${RES}.init.nc ${DIRRUN}/dir.\${i} & - cp -f ${EXECS}/convert_mpas ${DIRRUN}/dir.\${i} & + cp -f \${DATAOUT}/${YYYYMMDDHHi}/Pre/x1.${RES}.init.nc \${DIRRUN}/dir.\${i} & + cp -f \${EXECS}/convert_mpas \${DIRRUN}/dir.\${i} & done wait @@ -242,22 +424,81 @@ do currentdate=\$(date -d "${YYYYMMDDHHi:0:8} \${hh}:00:00 \$(echo "(\${i}-1)*${t_strout:0:2}" | bc) hours \$(echo "(\${i}-1)*${t_strout:3:2}" | bc) minutes \$(echo "(\${i}-1)*${t_strout:6:2}" | bc) seconds" +"%Y%m%d%H.%M.%S") diag_name=MONAN_DIAG_G_MOD_${EXP}_${YYYYMMDDHHi}_\${currentdate}.x${RES}L${N_MODEL_LEV}.nc echo "" - echo "executando convert mpas" - chmod 755 ${DATAOUT}/${YYYYMMDDHHi}/Model/* + echo " = Running convert_mpas" + chmod 755 \${DATAOUT}/${YYYYMMDDHHi}/Model/* time ./convert_mpas x1.${RES}.init.nc ${DATAOUT}/${YYYYMMDDHHi}/Model/\${diag_name} > convert_mpas.output & echo "./convert_mpas x1.${RES}.init.nc ${DATAOUT}/${YYYYMMDDHHi}/Model/\${diag_name} > convert_mpas.output" done -# necessario aguardar as rodadas em background +# Make sure that the job remains active whilst convert_mpas runs in the background wait +#---~--- +# For the older MONAN versions, we must group variables by levels. This code is being +# temporarily added back here until Noah-MP is fully integrated to a stable MONAN release. +#---~--- +case "\${MONAN_ONETWO}" in +-m12) + + # Load python configuration on node + . \${SCRIPTS}/setenv_python.bash + + + . \${PYTHON_ENV_PATH}/bin/activate + + #---~--- + # Group vertical levels. + #---~--- + for ii in \$(seq ${inicio} ${fim}) + do + i=\$(printf "%04d" \${ii}) + cd \${DIRRUN}/dir.\${i} + ${PYTHON_EXEC} \${SCRIPTS}/group_levels.py \${DIRRUN}/dir.\${i} latlon.nc latlon_\${i}.nc \ + 1> \${DATAOUT}/${YYYYMMDDHHi}/Post/logs/out_group_levels_${node}.log 2>&1 & + echo "${PYTHON_EXEC} \${SCRIPTS}/group_levels.py \${DIRRUN}/dir.\${i} latlon.nc latlon_\${i}.nc" + done + #---~--- + + #--- Make sure that the job remains active whilst convert_mpas runs in the background. + wait + #---~--- + + #---~--- + # Rename files so the subsequent steps are not dependent upon the MONAN/convertmpas version. + #---~--- + for ii in \$(seq ${inicio} ${fim}) + do + i=\$(printf "%04d" \${ii}) + cd \${DIRRUN}/dir.\${i} + #--- Move file to the default latlon.nc + if [[ -s latlon_\${i}.nc ]] + then + /bin/rm latlon.nc + /bin/mv latlon_\${i}.nc latlon.nc + else + echo -e "\${RED}==>\${NC} *** FATAL ERROR ***" + echo -e "\${RED}==>\${NC} Script group_levels.py failed" + exit -1 + fi + #---~--- + + done + #---~--- + + #--- Unload python. + deactivate + #---~--- + ;; +esac +#---~--- + for ii in \$(seq ${inicio} ${fim}) do i=\$(printf "%04d" \${ii}) hh=${YYYYMMDDHHi:8:2} currentdate=\$(date -d "${YYYYMMDDHHi:0:8} \${hh}:00:00 \$(echo "(\${i}-1)*${t_strout:0:2}" | bc) hours \$(echo "(\${i}-1)*${t_strout:3:2}" | bc) minutes \$(echo "(\${i}-1)*${t_strout:6:2}" | bc) seconds" +"%Y%m%d%H.%M.%S") - diag_name_post=MONAN_DIAG_G_POS_${EXP}_${YYYYMMDDHHi}_\${currentdate}.x${RES}L${N_MODEL_LEV}.nc - + diag_name_post=MONAN_DIAG_G_POS_${EXP}_${YYYYMMDDHHi}_\${currentdate}.x${RES}L${N_ISOBARIC_LEV}.nc + cd ${DIRRUN}/dir.\${i} chmod 755 * cp latlon.nc ${DATAOUT}/${YYYYMMDDHHi}/Post/\${diag_name_post} >> convert_mpas.output & @@ -275,22 +516,22 @@ EOSH cp -f ${DIRRUN}/PostAtmos_node.${node}.sh ${DATAOUT}/${YYYYMMDDHHi}/Post/logs chmod 755 ${DATAOUT}/${YYYYMMDDHHi}/Post/* case "${SCHEDULER_SYSTEM}" in - SLURM) - echo "Sbatch PostAtmos_node.${node}.sh" - jobid[${node}]=$(sbatch --parsable ${DIRRUN}/PostAtmos_node.${node}.sh) - echo "JobId node ${node} = ${jobid[${node}]} , convert_mpas ${inicio} to ${fim}" - echo "" - ;; - PBS) - echo "Rodando em PBS" - echo -e "${GREEN}==>${NC} qsub PostAtmos_node.${node}.sh...\n" - cd ${DIRRUN} - jobid[${node}]=$(qsub ${DIRRUN}/PostAtmos_node.${node}.sh | cut -d '.' -f1) - ;; -# GENERIC) -# echo "Nenhum gerenciador detectado" -# ${DIRRUN}/PostAtmos_node.${node}.sh -# ;; + SLURM) + echo "sbatch PostAtmos_node.${node}.sh" + jobid[${node}]=$(sbatch --parsable ${DIRRUN}/PostAtmos_node.${node}.sh) + echo "JobId node ${node} = ${jobid[${node}]} , convert_mpas ${inicio} to ${fim}" + echo "" + ;; + PBS) + echo "qsub PostAtmos_node.${node}.sh" + echo -e "${GREEN}==>${NC} qsub PostAtmos_node.${node}.sh...\n" + cd ${DIRRUN} + jobid[${node}]=$(qsub ${DIRRUN}/PostAtmos_node.${node}.sh | cut -d '.' -f1) + ;; +# GENERIC) +# echo "Nenhum gerenciador detectado" +# ${DIRRUN}/PostAtmos_node.${node}.sh +# ;; esac @@ -303,20 +544,26 @@ done -# Dependencias JobId: +#--- Set JobId dependencies: dependency="afterok" for job_id in "${jobid[@]}" do dependency="${dependency}:${job_id}" done +#---~--- - -# Script final , para conferir todos os arquivos, criar o template final e apagar o diretorio DIRRUN +#---~--- +# Final script, which will check every file, make the final template and remove +# directory ${DIRRUN} +#---~--- node=0 rm -f ${DIRRUN}/PostAtmos_node.${node}.sh -if [ ${SCHEDULER_SYSTEM} != "GENERIC" ] -then +case "${SCHEDULER_SYSTEM}" in +GENERIC) + echo "#!/bin/bash " > ${DIRRUN}/PostAtmos_node.${node}.sh + ;; +*) sed -e "s,#JOBNAME#,MO.Pos${node},g; s,#NNODES#,${POST_nnodes},g; s,#NCPUS#,${POST_ncpus},g; @@ -329,29 +576,69 @@ then s,#ERRORJOB#,${DATAOUT}/${YYYYMMDDHHi}/Post/logs/PostAtmos_node.${node}.e,g" \ ${SCRIPTS}/stools/submit_${SYSTEM_KEY}.bash_TEMPLATE > \ ${DIRRUN}/PostAtmos_node.${node}.sh -else - echo "#!/bin/bash " > ${DIRRUN}/PostAtmos_node.${node}.sh -fi + ;; +esac cat << EOSH >> ${DIRRUN}/PostAtmos_node.${node}.sh -cd ${DIRRUN} -. ${SCRIPTS}/setenv.bash +MONAN_ONETWO="${MONAN_ONETWO}" +DIRRUN="${DIRRUN}" + +. ${SCRIPTS}/setenv.bash \${MONAN_ONETWO} + +cd \${DIRRUN} + +#---~--- +# For older versions, we group data into a single file, fix the time units and shift the +# bounding box so it goes from 180W to 180E (as opposed to 0-360). +#---~--- +case "\${MONAN_ONETWO}" in +-m12) + #--- Delete temporary files. + echo " - Remove existing files" + /bin/rm -f \${DATAOUT}/${YYYYMMDDHHi}/Post/mergetime.nc + /bin/rm -f \${DATAOUT}/${YYYYMMDDHHi}/Post/timeunits.nc + /bin/rm -f \${DATAOUT}/${YYYYMMDDHHi}/Post/MONAN_DIAG_G_POS_${EXP}_${YYYYMMDDHHi}_AllTimes.x${RES}L${N_ISOBARIC_LEV}.nc + #---~--- + + #--- Merge all files. + echo " - Merge single-time files:" + cdo mergetime \${DATAOUT}/${YYYYMMDDHHi}/Post/MONAN_DIAG_G_POS_${EXP}_${YYYYMMDDHHi}_??????????.??.??.x${RES}L${N_ISOBARIC_LEV}.nc \${DATAOUT}/${YYYYMMDDHHi}/Post/mergetime.nc + sleep 5 + #---~--- + + #--- Fix time increment so it is consistent with the output. + echo " - Fix time interval so it matches the actual output:" + cdo settaxis,${START_DATE_YYYYMMDD},${START_HH}:00,${t_stroutsec}seconds \${DATAOUT}/${YYYYMMDDHHi}/Post/mergetime.nc \${DATAOUT}/${YYYYMMDDHHi}/Post/timeunits.nc + sleep 5 + #---~--- + + #--- Shift the bounding box to 180W:180E (as opposed to 0:360). + echo " - Set the bounding box to 180W:180E:" + cdo sellonlatbox,-180,180,-90,90 \${DATAOUT}/${YYYYMMDDHHi}/Post/timeunits.nc \${DATAOUT}/${YYYYMMDDHHi}/Post/MONAN_DIAG_G_POS_${EXP}_${YYYYMMDDHHi}_AllTimes.x${RES}L${N_ISOBARIC_LEV}.nc + sleep 5 + #---~--- + + #--- Delete temporary files. + echo " - Delete temporary files" + /bin/rm -f \${DATAOUT}/${YYYYMMDDHHi}/Post/mergetime.nc + /bin/rm -f \${DATAOUT}/${YYYYMMDDHHi}/Post/timeunits.nc + #---~--- + ;; +esac # Saving important files to the logs directory: -cp -f ${EXECS}/CONVMPAS-VERSION.txt ${DATAOUT}/${YYYYMMDDHHi}/Post -cp -f ${EXECS}/CONVMPAS-VERSION.txt ${DATAOUT}/${YYYYMMDDHHi}/Post/logs -cp -f ${DIRRUN}/dir.0001/target_domain ${DATAOUT}/${YYYYMMDDHHi}/Post/logs -cp -f ${DIRRUN}/dir.0001/convert_mpas.nml ${DATAOUT}/${YYYYMMDDHHi}/Post/logs -cp -f ${DIRRUN}/dir.0001/include_fields ${DATAOUT}/${YYYYMMDDHHi}/Post/logs -cp -f ${DIRRUN}/dir.0001/convert_mpas.output ${DATAOUT}/${YYYYMMDDHHi}/Post/logs -cp -f ${DIRRUN}/PostAtmos_node.*.sh ${DATAOUT}/${YYYYMMDDHHi}/Post/logs -cp -f ${DATAOUT}/${YYYYMMDDHHi}/Model/logs/* ${DATAOUT}/${YYYYMMDDHHi}/Post/logs -cp -f ${DATAOUT}/${YYYYMMDDHHi}/Model/MONAN-VERSION.txt ${DATAOUT}/${YYYYMMDDHHi}/Post/logs - - -cd ${DIRRUN}/.. -rm -fr ${DIRRUN} +echo " - Save relevant files to permanent locations:" +cp -f \${EXECS}/CONVMPAS-VERSION.txt \${DATAOUT}/${YYYYMMDDHHi}/Post +cp -f \${EXECS}/CONVMPAS-VERSION.txt \${DATAOUT}/${YYYYMMDDHHi}/Post/logs +cp -f \${DIRRUN}/dir.0001/target_domain \${DATAOUT}/${YYYYMMDDHHi}/Post/logs +cp -f \${DIRRUN}/dir.0001/convert_mpas.nml \${DATAOUT}/${YYYYMMDDHHi}/Post/logs +cp -f \${DIRRUN}/dir.0001/include_fields \${DATAOUT}/${YYYYMMDDHHi}/Post/logs +cp -f \${DIRRUN}/dir.0001/convert_mpas.output \${DATAOUT}/${YYYYMMDDHHi}/Post/logs +cp -f \${DIRRUN}/PostAtmos_node.*.sh \${DATAOUT}/${YYYYMMDDHHi}/Post/logs +cp -f \${DATAOUT}/${YYYYMMDDHHi}/Model/logs/* \${DATAOUT}/${YYYYMMDDHHi}/Post/logs +cp -f \${DATAOUT}/${YYYYMMDDHHi}/Model/MONAN-VERSION.txt \${DATAOUT}/${YYYYMMDDHHi}/Post/logs + EOSH @@ -359,24 +646,37 @@ chmod a+x ${DIRRUN}/PostAtmos_node.${node}.sh case "${SCHEDULER_SYSTEM}" in - SLURM) - echo "Sbatch PostAtmos_node.${node}.sh" - sbatch --wait --dependency=${dependency} ${DIRRUN}/PostAtmos_node.${node}.sh - ;; - PBS) - echo "Rodando em PBS" - echo -e "${GREEN}==>${NC} qsub PostAtmos_node.${node}.sh...\n" - cd ${DIRRUN} - qsub -W depend=${dependency} -W block=true ${DIRRUN}/PostAtmos_node.${node}.sh - ;; -# GENERIC) -# echo "Nenhum gerenciador detectado" -# ${DIRRUN}/PostAtmos_node.${node}.sh -# ;; +SLURM) + echo "Sbatch PostAtmos_node.${node}.sh" + sbatch --wait --dependency=${dependency} ${DIRRUN}/PostAtmos_node.${node}.sh + ;; +PBS) + echo "Qsub PostAtmos_node.${node}.sh" + echo -e "${GREEN}==>${NC} qsub PostAtmos_node.${node}.sh...\n" + cd ${DIRRUN} + qsub -W depend=${dependency} -W block=true ${DIRRUN}/PostAtmos_node.${node}.sh + ;; +#GENERIC) +# echo "No job manager detected." +# ${DIRRUN}/PostAtmos_node.${node}.sh +# ;; esac -#CR: passar este scriptpara dentro do script PostAtmos_node.0.sh, submetido. +#--- Make sure VARTABLE has the leading "-v" if not empty. +if [[ "${VARTABLE}" == "" ]] +then + dv_VARTABLE="" +else + dv_VARTABLE="-v ${VARTABLE}" +fi +#---~--- + +#CR: Append this script to script PostAtmos_node.0.sh, which has been submitted. cd ${SCRIPTS} chmod 755 ${DATAOUT}/${YYYYMMDDHHi}/Post/* -time ${SCRIPTS}/make_template.bash ${EXP} ${RES} ${YYYYMMDDHHi} ${FCST} +time ${SCRIPTS}/make_template.bash ${MONAN_ONETWO} ${dv_VARTABLE} \ + -d ${OUTPUT_DIAG_INTERVAL} -e ${EXP} -f ${FCST} -r ${RES} -t ${YYYYMMDDHHi} + +cd ${DIRHOMES} +rm -fr ${DIRRUN} diff --git a/scripts/group_levels.py b/scripts/group_levels.py new file mode 100644 index 0000000..1d7d66a --- /dev/null +++ b/scripts/group_levels.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python3 + +import netCDF4 as nc +import numpy as np +import re +import sys + + +def main(data_dir, file_in, file_out, levels=[1000, 975, 950, 925, 900, 875, 850, 825, 700, 600, 500, 400, \ + 300, 250, 200, 150, 100, 70, 50, 30, 20, 15]): + """ + This function takes in the data directory, input file name, and output file name. + It reads the input NetCDF file, groups variables based on their names, + creates a new NetCDF file with dimensions and variables copied from the input file, + and saves the concatenated variables to the new NetCDF file. + + Parameters: + data_dir (str): The directory where the input and output NetCDF files are located. + file_in (str): The name of the input NetCDF file. + file_out (str): The name of the output NetCDF file. + + Returns: + None + """ + + nc_file_in = nc.Dataset(f'{data_dir}/{file_in}', 'r') + # Save the concatenated variables to a new NetCDF file + nc_file_out = nc.Dataset(f'{data_dir}/{file_out}', 'w') + + dimensions_4D = ('time', 'level', 'latitude', 'longitude') + # Group variables based on their names + variable_groups = {} + first_hpa_variable = '' + for name, variable in reversed(list(nc_file_in.variables.items())): # reverse to be consistent to levels + # if 'hPa' in name or name in dimensions_4D: + if 'hPa' in name: + variable_type = re.split(r'_.\d*hPa', name)[0] # Extract the variable type (e.g., '15hPa', '20hPa') + first_hpa_variable = variable_type if first_hpa_variable == '' else first_hpa_variable + else: + variable_type = name.lower() + if variable_type not in variable_groups: + variable_groups[variable_type] = [variable] + else: + variable_groups[variable_type].append(variable) + + # if there isn't any hPa variable - set default 22 levels + if not first_hpa_variable: + print("No isobaric variable ('hPa') found in file. Using a dummy level count.") + level_dimension_size = 22 + else: + level_dimension_size = len(variable_groups[first_hpa_variable]) + + # Copy dimensions Time, latitude, longitude + for dim_name, dim_type in nc_file_in.dimensions.items(): + if dim_name.lower() in ['time', 'latitude', 'longitude']: + nc_file_out.createDimension(dim_name.lower(), dim_type.size) + nc_file_out.createDimension('level', level_dimension_size) + + + # Copy variables + for variable_type, variables in variable_groups.items(): + print(f'Creating var {variable_type}') + if variable_type == 'level': + new_variable = nc_file_out.createVariable(variable_type, variables[0].dtype, (variable_type) ) + new_variable.setncatts({k: variables[0].getncattr(k) for k in variables[0].ncattrs()}) + new_variable[:] = range(level_dimension_size, 0, -1) if levels is None else levels + new_variable.setncattr('positive', "down") + new_variable.setncattr('units', "hPa") + # if its variables with on isobaric level per variable + elif len(variables) == level_dimension_size: + new_variable = nc_file_out.createVariable(variable_type, variables[0].dtype, dimensions_4D) + new_variable.setncatts({k: variables[0].getncattr(k) for k in variables[0].ncattrs()}) + if 'vertically interpolated' in new_variable.getncattr('long_name'): + new_long_name = new_variable.getncattr('long_name').split('vertically interpolated')[0].strip() + elif 'interpolated' in new_variable.getncattr('long_name'): + new_long_name = new_variable.getncattr('long_name').split('interpolated')[0].strip() + elif '1000 hPa' in new_variable.getncattr('long_name'): + new_long_name = new_variable.getncattr('long_name').split('1000 hPa')[0].strip() + else: + new_long_name = new_variable.getncattr('long_name') + new_long_name += ' interpolated' + new_variable.setncattr('long_name', new_long_name) + + for i in range(level_dimension_size): + new_variable[:,i,:,:] = variables[i][:] + else: + # Create an empty list to store lowercase dimensions + lowercase_dimensions = [] + + # Iterate through each object in the tuple + for obj in variables[0].dimensions: + # Access the 'dimensions' attribute and convert to lowercase + lowercase_dimensions.append(obj.lower()) + # Convert the list of lowercase dimensions to a tuple + + lowercase_dimensions_tuple = tuple(lowercase_dimensions) + # variable_type in ('latitude', 'longitude', 'Time'): + new_variable = nc_file_out.createVariable(variable_type, variables[0].dtype, lowercase_dimensions_tuple) + new_variable.setncatts({k: variables[0].getncattr(k) for k in variables[0].ncattrs()}) + new_variable[:] = variables[0][:] + + nc_file_out.close() + nc_file_in.close() + + +if __name__ == "__main__": + import sys + + if len(sys.argv) != 4: + print(f"Usage: python3 {sys.argv[0]} ") + sys.exit(1) + + main(sys.argv[1], sys.argv[2], sys.argv[3]) diff --git a/scripts/make_degrib.bash b/scripts/make_degrib.bash index 8d7bd11..5fd44b3 100755 --- a/scripts/make_degrib.bash +++ b/scripts/make_degrib.bash @@ -2,54 +2,117 @@ umask 022 -if [ $# -ne 4 ] -then +#--- Function that shows usage. +function show_usage() { + echo " Usage: " echo "" - echo "Instructions: execute the command below" + echo " ${0} [-h] [-m12] [-e EXP ] [-f FCST] [-r RES] [-t YYYYMMDDHH]" echo "" - echo "${0} EXP_NAME RESOLUTION LABELI FCST" + echo " List of optional flags: " + echo " -h -- Shows this message." + echo " -m12 -- Is this a MONAN run based on 1.2.0-rc and branches derived" + echo " from this version (e.g., feature/monan-757-NF)? This is a" + echo " temporary flag that will be removed once the versions" + echo " containing Noah-MP are merged into the new release. This" + echo " allows the script to manage older code and still run on jaci." echo "" - echo "EXP_NAME :: Forcing: GFS" - echo " :: Others options to be added later..." - echo "RESOLUTION :: number of points in resolution model grid, e.g: 1024002 (24 km)" - echo "LABELI :: Initial date YYYYMMDDHH, e.g.: 2024010100" - echo "FCST :: Forecast hours, e.g.: 24 or 36, etc." + echo " List of **required** flags: " echo "" - echo "24 hour forcast example:" - echo "${0} GFS 1024002 2024010100 24" - echo "${0} GFS 40962 2024010100 48" + echo " -e EXP -- meteorological drivers. For example, GFS" + echo " -f FCST -- Simulation length in hours, e.g., 24 or 48." + echo " -r RES -- grid resolution. Supported options are:" + echo " 65536002 (~ 3 km)" + echo " 5898242 (~ 10 km)" + echo " 2621442 (~ 15 km)" + echo " 1024002 (~ 24 km)" + echo " 655362 (~ 30 km)" + echo " 163842 (~ 60 km)" + echo " 40962 (~ 120 km)" + echo " -t YYYYMMDDHH -- Initial time. For example if 22 Sept 2025 00 UTC, set it to:" + echo " 2025092200" echo "" +} +#---~--- + + + + +#--- Default input variables: +MONAN_ONETWO="" +EXP="" +RES="" +YYYYMMDDHHi="" +FCST="" +#---~--- + + +#--- Parse arguments. +while [[ ${#} > 0 ]] +do + key="${1}" + case ${key} in + -e) + EXP="${2}" + shift 2 # past flag and argument + ;; + -f) + FCST="${2}" + shift 2 # past flag and argument + ;; + -h) + show_usage + exit 0 + ;; + -m12) + MONAN_ONETWO="${key}" + shift 1 # past flag + ;; + -r) + RES="${2}" + shift 2 # past flag and argument + ;; + -t) + YYYYMMDDHHi="${2}" + shift 2 # past flag and argument + ;; + *) + echo "" + echo " Option \"${key}\" is not valid." + echo "" + show_usage + echo "" + echo " *** FATAL ERROR! ***" + echo " Unknown key or key-value argument pair." + echo "" + exit 2 + ;; + esac +done +#---~--- + - exit + +#---~--- +# Make sure all required settings were provided. +#---~--- +if [[ "${EXP}" == "" ]] || [[ "${RES}" == "" ]] || + [[ "${YYYYMMDDHHi}" == "" ]] || [[ "${FCST}" == "" ]] +then + echo " This script requires some arguments to be set through flags." + show_usage + exit 2 fi +#---~--- -# Set environment variables exports: -echo "" -echo -e "\033[1;32m==>\033[0m Moduling environment for MONAN model...\n" -. setenv.bash + +#--- Set environment variables exports: +. setenv.bash ${MONAN_ONETWO} +#---~--- echo "" echo "---- Make Degrib ----" echo "" -# Standart directories variables:--------------------------------------- -DIRHOMES=${DIR_SCRIPTS}/scripts_CD-CT; mkdir -p ${DIRHOMES} -DIRHOMED=${DIR_DADOS}/scripts_CD-CT; mkdir -p ${DIRHOMED} -SCRIPTS=${DIRHOMES}/scripts; mkdir -p ${SCRIPTS} -DATAIN=${DIRHOMED}/datain; mkdir -p ${DATAIN} -DATAOUT=${DIRHOMED}/dataout; mkdir -p ${DATAOUT} -SOURCES=${DIRHOMES}/sources; mkdir -p ${SOURCES} -EXECS=${DIRHOMED}/execs; mkdir -p ${EXECS} -#---------------------------------------------------------------------- - - -# Input variables:-------------------------------------- -EXP=${1}; #EXP=GFS -RES=${2}; #RES=1024002 -YYYYMMDDHHi=${3}; #YYYYMMDDHHi=2024012000 -FCST=${4}; #FCST=24 -#------------------------------------------------------- - @@ -60,30 +123,35 @@ export DIRRUN=${DIRHOMED}/run.${YYYYMMDDHHi}; rm -fr ${DIRRUN}; mkdir -p ${DIRRU mkdir -p ${DATAIN}/${YYYYMMDDHHi} mkdir -p ${DATAOUT}/${YYYYMMDDHHi}/Pre/logs -if [ "$HOSTNAME" = "egeon" ]; then - mkdir -p ${HOME}/local/lib64 - cp -f /usr/lib64/libjasper.so* ${HOME}/local/lib64 - cp -f /usr/lib64/libjpeg.so* ${HOME}/local/lib64 -fi - -#Se nao existir CI no diretorio do IO, -# busca no nosso dir /beegfs/monan/CIs, se nao existir tbm, aborta! -#CR: BNDDIR should be setted just for EGEON machine -#CR: some local variables were mobed into the SLURM section, particularly for egeon +#---~--- +# Machine-specific configurations. +#---~--- +case "${HOSTNAME}" in +egeon) + mkdir -p ${HOME}/local/lib64 + cp -f /usr/lib64/libjasper.so* ${HOME}/local/lib64 + cp -f /usr/lib64/libjpeg.so* ${HOME}/local/lib64 + ;; +esac +#---~--- +#--- Set path for potential location of boundary conditions. OPERDIREXP=${OPERDIR}/${EXP} BNDDIR=${OPERDIREXP}/0p25/brutos/${YYYYMMDDHHi:0:4}/${YYYYMMDDHHi:4:2}/${YYYYMMDDHHi:6:2}/${YYYYMMDDHHi:8:2} +#---~--- -# Se nao existir CI no diretorio do IO, -# busca no nosso dir /beegfs/monan/CIs (Egeon) , /p/monan/CIs (xd2000) se nao existir tbm, aborta! -#CR: maybe this if should belong to the SLURM kind of running... -if [ ! -s ${BNDDIR}/gfs.t${YYYYMMDDHHi:8:2}z.pgrb2.0p25.f000.${YYYYMMDDHHi}.grib2 ] +#---~--- +# Retrieve boundary conditions, starting with the I/O path. If not found, look for them +# at default paths, /beegfs/monan/CIs (Egeon), /p/monan/CIs (Jaci). +# If files are not found at these locations, abort the run. +#---~--- +if [[ ! -s ${BNDDIR}/gfs.t${YYYYMMDDHHi:8:2}z.pgrb2.0p25.f000.${YYYYMMDDHHi}.grib2 ]] then - if [ ! -s ${GCCCIS}/${EXP}/${YYYYMMDDHHi:0:4}/${YYYYMMDDHHi}/gfs.t${YYYYMMDDHHi:8:2}z.pgrb2.0p25.f000.${YYYYMMDDHHi}.grib2 ] + if [[ ! -s ${GCCCIS}/${EXP}/${YYYYMMDDHHi:0:4}/${YYYYMMDDHHi}/gfs.t${YYYYMMDDHHi:8:2}z.pgrb2.0p25.f000.${YYYYMMDDHHi}.grib2 ]] then - echo -e "${RED}==>${NC}Condicao de contorno inexistente !" - echo -e "${RED}==>${NC}Check ${BNDDIR} or." + echo -e "${RED}==>${NC}Failed to find boundary conditions!" + echo -e "${RED}==>${NC}Check ${BNDDIR} or" echo -e "${RED}==>${NC}Check ${GCCCIS}/${EXP}" exit 1 else @@ -98,9 +166,9 @@ files_needed=("${DATAIN}/fixed/x1.${RES}.static.nc" "${DATAIN}/fixed/Vtable.${EX for file in "${files_needed[@]}" do - if [ ! -s "${file}" ] + if [[ ! -s "${file}" ]] then - echo -e "\n${RED}==>${NC} ***** ATTENTION *****\n" + echo -e "\n${RED}==>${NC} ***** FATAL ERROR *****\n" echo -e "${RED}==>${NC} [${0}] At least the file ${file} was not generated. \n" exit -1 fi @@ -117,7 +185,7 @@ cp -f ${SCRIPTS}/link_grib.csh ${DIRRUN} rm -f ${DIRRUN}/degrib.bash -if [ ${SCHEDULER_SYSTEM} != "GENERIC" ] +if [[ ${SCHEDULER_SYSTEM} != "GENERIC" ]] then sed -e "s,#JOBNAME#,${DEGRIB_jobname},g; s,#NNODES#,${DEGRIB_nnodes},g; @@ -146,7 +214,7 @@ export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:${HOME}/local/lib64 cd ${DIRRUN} -. ${SCRIPTS}/setenv.bash +. ${SCRIPTS}/setenv.bash ${MONAN_ONETWO} ldd ungrib.exe @@ -172,7 +240,7 @@ date grep "Successful completion of program ungrib.exe" ${DIRRUN}/ungrib.log >& /dev/null -if [ \$? -ne 0 ]; then +if [[ \$? -ne 0 ]]; then echo " BUMMER: Ungrib generation failed for some yet unknown reason." echo " " tail -10 ${DIRRUN}/ungrib.log @@ -217,9 +285,9 @@ esac files_ungrib=("${EXP}:${YYYYMMDDHHi:0:4}-${YYYYMMDDHHi:4:2}-${YYYYMMDDHHi:6:2}_${YYYYMMDDHHi:8:2}") for file in "${files_ungrib[@]}" do - if [ ! -s ${DATAOUT}/${YYYYMMDDHHi}/Pre/${file} ] + if [[ ! -s ${DATAOUT}/${YYYYMMDDHHi}/Pre/${file} ]] then - echo -e "\n${RED}==>${NC} ***** ATTENTION *****\n" + echo -e "\n${RED}==>${NC} ***** FATAL ERROR *****\n" echo -e "${RED}==>${NC} Degrib fails! At least the file ${file} was not generated at ${DATAIN}/${YYYYMMDDHHi}. \n" echo -e "${RED}==>${NC} Check logs at ${DATAOUT}/logs/degrib.* .\n" echo -e "${RED}==>${NC} Exiting script. \n" diff --git a/scripts/make_initatmos.bash b/scripts/make_initatmos.bash index 822c183..937a1a6 100755 --- a/scripts/make_initatmos.bash +++ b/scripts/make_initatmos.bash @@ -2,52 +2,113 @@ umask 022 -if [ $# -ne 4 ] -then + + +#--- Function that shows usage. +function show_usage() { + echo " Usage: " echo "" - echo "Instructions: execute the command below" + echo " ${0} [-h] [-m12] [-e EXP ] [-f FCST] [-r RES] [-t YYYYMMDDHH]" echo "" - echo "${0} EXP_NAME RESOLUTION LABELI FCST" + echo " List of optional flags: " + echo " -h -- Shows this message." + echo " -m12 -- Is this a MONAN run based on 1.2.0-rc and branches derived" + echo " from this version (e.g., feature/monan-757-NF)? This is a" + echo " temporary flag that will be removed once the versions" + echo " containing Noah-MP are merged into the new release. This" + echo " allows the script to manage older code and still run on jaci." echo "" - echo "EXP_NAME :: Forcing: GFS" - echo " :: Others options to be added later..." - echo "RESOLUTION :: number of points in resolution model grid, e.g: 1024002 (24 km)" - echo "LABELI :: Initial date YYYYMMDDHH, e.g.: 2024010100" - echo "FCST :: Forecast hours, e.g.: 24 or 36, etc." + echo " List of **required** flags: " echo "" - echo "24 hour forcast example:" - echo "${0} GFS 1024002 2024010100 24" + echo " -e EXP -- meteorological drivers. For example, GFS" + echo " -f FCST -- Simulation length in hours, e.g., 24 or 48." + echo " -r RES -- grid resolution. Supported options are:" + echo " 65536002 (~ 3 km)" + echo " 5898242 (~ 10 km)" + echo " 2621442 (~ 15 km)" + echo " 1024002 (~ 24 km)" + echo " 655362 (~ 30 km)" + echo " 163842 (~ 60 km)" + echo " 40962 (~ 120 km)" + echo " -t YYYYMMDDHH -- Initial time. For example if 22 Sept 2025 00 UTC, set it to:" + echo " 2025092200" echo "" +} +#---~--- + + + +#--- Default input variables: +MONAN_ONETWO="" +EXP="" +RES="" +YYYYMMDDHHi="" +FCST="" +#---~--- + + +#--- Parse arguments. +while [[ ${#} > 0 ]] +do + key="${1}" + case ${key} in + -e) + EXP="${2}" + shift 2 # past flag and argument + ;; + -f) + FCST="${2}" + shift 2 # past flag and argument + ;; + -h) + show_usage + exit 0 + ;; + -m12) + MONAN_ONETWO="${key}" + shift 1 # past flag + ;; + -r) + RES="${2}" + shift 2 # past flag and argument + ;; + -t) + YYYYMMDDHHi="${2}" + shift 2 # past flag and argument + ;; + *) + echo "" + echo " Option \"${key}\" is not valid." + echo "" + show_usage + echo "" + echo " *** FATAL ERROR! ***" + echo " Unknown key or key-value argument pair." + echo "" + exit 2 + ;; + esac +done +#---~--- + - exit + +#---~--- +# Make sure all required settings were provided. +#---~--- +if [[ "${EXP}" == "" ]] || [[ "${RES}" == "" ]] || + [[ "${YYYYMMDDHHi}" == "" ]] || [[ "${FCST}" == "" ]] +then + echo " This script requires some arguments to be set through flags." + show_usage + exit 2 fi +#---~--- -# Set environment variables exports: -echo "" -echo -e "\033[1;32m==>\033[0m Moduling environment for MONAN model...\n" -. setenv.bash - -echo "" -echo "---- Make Init Atmosphere ----" -echo "" - -# Standart directories variables:--------------------------------------- -DIRHOMES=${DIR_SCRIPTS}/scripts_CD-CT; mkdir -p ${DIRHOMES} -DIRHOMED=${DIR_DADOS}/scripts_CD-CT; mkdir -p ${DIRHOMED} -SCRIPTS=${DIRHOMES}/scripts; mkdir -p ${SCRIPTS} -DATAIN=${DIRHOMED}/datain; mkdir -p ${DATAIN} -DATAOUT=${DIRHOMED}/dataout; mkdir -p ${DATAOUT} -SOURCES=${DIRHOMES}/sources; mkdir -p ${SOURCES} -EXECS=${DIRHOMED}/execs; mkdir -p ${EXECS} -#---------------------------------------------------------------------- - - -# Input variables:-------------------------------------- -EXP=${1}; #EXP=GFS -RES=${2}; #RES=1024002 -YYYYMMDDHHi=${3}; #YYYYMMDDHHi=2024012000 -FCST=${4}; #FCST=24 -#------------------------------------------------------- + +#--- Set environment variables exports: +. setenv.bash ${MONAN_ONETWO} +#---~--- # Local variables-------------------------------------- @@ -61,9 +122,9 @@ mkdir -p ${DATAOUT}/${YYYYMMDDHHi}/Pre/logs -if [ ! -s ${DATAIN}/fixed/x1.${RES}.graph.info.part.${cores} ] +if [[ ! -s ${DATAIN}/fixed/x1.${RES}.graph.info.part.${cores} ]] then - if [ ! -s ${DATAIN}/fixed/x1.${RES}.graph.info ] + if [[ ! -s ${DATAIN}/fixed/x1.${RES}.graph.info ]] then cd ${DATAIN}/fixed echo -e "${GREEN}==>${NC} downloading meshes tgz files ... \n" @@ -83,9 +144,9 @@ fi files_needed=("${SCRIPTS}/namelists/namelist.init_atmosphere.TEMPLATE" "${SCRIPTS}/namelists/streams.init_atmosphere.TEMPLATE" "${DATAIN}/fixed/x1.${RES}.graph.info.part.${cores}" "${DATAIN}/fixed/x1.${RES}.static.nc" "${DATAOUT}/${YYYYMMDDHHi}/Pre/${EXP}:${start_date:0:13}" "${EXECS}/init_atmosphere_model") for file in "${files_needed[@]}" do - if [ ! -s "${file}" ] + if [[ ! -s "${file}" ]] then - echo -e "\n${RED}==>${NC} ***** ATTENTION *****\n" + echo -e "\n${RED}==>${NC} ***** FATAL ERROR *****\n" echo -e "${RED}==>${NC} [${0}] At least the file ${file} was not generated. \n" exit -1 fi @@ -111,7 +172,7 @@ chmod 755 ${DATAOUT}/${YYYYMMDDHHi}/Pre/* rm -f ${DIRRUN}/initatmos.bash -if [ ${SCHEDULER_SYSTEM} != "GENERIC" ] +if [[ ${SCHEDULER_SYSTEM} != "GENERIC" ]] then sed -e "s,#JOBNAME#,${INITATMOS_jobname},g; s,#NNODES#,${INITATMOS_nnodes},g; @@ -138,19 +199,27 @@ ulimit -s unlimited cd ${DIRRUN} -. ${SCRIPTS}/setenv.bash +. ${SCRIPTS}/setenv.bash ${MONAN_ONETWO} date beg_secs=\`date +"%s"\` -if [ "$HOSTNAME" = "egeon" ]; then +#ML: Replace HOSTNAME with SCHEDULER_SYSTEM so this can be expanded to other HPC environments more easily. +case "${SCHEDULER_SYSTEM}" in +SLURM) echo "-- SLURM_JOB_ID: \$SLURM_JOB_ID" time mpirun -np ${INITATMOS_ncores} ./\${executable} -else + ;; +PBS) echo "-- PBS_JOBID: \$PBS_JOBID" time mpirun --ppn ${INITATMOS_ncpn} -np ${INITATMOS_ncores} --depth=${INITATMOS_nthreads} --cpu-bind depth ./\${executable} -fi + ;; +#*) +# echo "-- GENERIC:" +# time mpirun -np ${INITATMOS_ncores} ./\${executable} +# ;; +esac date end_secs=\`date +"%s"\` @@ -168,29 +237,29 @@ EOF0 chmod a+x ${DIRRUN}/initatmos.bash case "${SCHEDULER_SYSTEM}" in - SLURM) - echo -e "${GREEN}==>${NC} Sbatch initatmos.bash...\n" - cd ${DIRRUN} - sbatch --wait ${DIRRUN}/initatmos.bash - ;; - PBS) - echo -e "${GREEN}==>${NC} qsub initatmos.bash...\n" - cd ${DIRRUN} - qsub -W block=true ${DIRRUN}/initatmos.bash - ;; -# GENERIC) -# echo "Nenhum gerenciador detectado" -# cd ${DIRRUN} -# ${DIRRUN}/initatmos.bash -# ;; +SLURM) + echo -e "${GREEN}==>${NC} Sbatch initatmos.bash...\n" + cd ${DIRRUN} + sbatch --wait ${DIRRUN}/initatmos.bash + ;; +PBS) + echo -e "${GREEN}==>${NC} qsub initatmos.bash...\n" + cd ${DIRRUN} + qsub -W block=true ${DIRRUN}/initatmos.bash + ;; +#GENERIC) +# echo -e "${ORANGE}==>${NC} No scheduler system detected...\n" +# cd ${DIRRUN} +# ${DIRRUN}/initatmos.bash +# ;; esac mv ${DIRRUN}/initatmos.bash ${DATAOUT}/${YYYYMMDDHHi}/Pre/logs -if [ ! -s ${DATAOUT}/${YYYYMMDDHHi}/Pre/x1.${RES}.init.nc ] +if [[ ! -s ${DATAOUT}/${YYYYMMDDHHi}/Pre/x1.${RES}.init.nc ]] then - echo -e "\n${RED}==>${NC} ***** ATTENTION *****\n" + echo -e "\n${RED}==>${NC} ***** FATAL ERROR *****\n" echo -e "${RED}==>${NC} Init Atmosphere phase fails! Check logs at ${DATAOUT}/logs/initatmos.* .\n" echo -e "${RED}==>${NC} Exiting script. \n" exit -1 diff --git a/scripts/make_static.bash b/scripts/make_static.bash index 82f079f..0099d78 100755 --- a/scripts/make_static.bash +++ b/scripts/make_static.bash @@ -2,53 +2,117 @@ umask 022 -if [ $# -ne 4 ] -then +#--- Function that shows usage. +function show_usage() { + echo " Usage: " echo "" - echo "Instructions: execute the command below" + echo " ${0} [-h] [-m12] [-e EXP ] [-f FCST] [-r RES] [-t YYYYMMDDHH]" echo "" - echo "${0} EXP_NAME RESOLUTION LABELI FCST" + echo " List of optional flags: " + echo " -h -- Shows this message." + echo " -m12 -- Is this a MONAN run based on 1.2.0-rc and branches derived" + echo " from this version (e.g., feature/monan-757-NF)? This is a" + echo " temporary flag that will be removed once the versions" + echo " containing Noah-MP are merged into the new release. This" + echo " allows the script to manage older code and still run on jaci." echo "" - echo "EXP_NAME :: Forcing: GFS" - echo " :: Others options to be added later..." - echo "RESOLUTION :: number of points in resolution model grid, e.g: 1024002 (24 km)" - echo "LABELI :: Initial date YYYYMMDDHH, e.g.: 2024010100" - echo "FCST :: Forecast hours, e.g.: 24 or 36, etc." + echo " List of **required** flags: " echo "" - echo "24 hour forcast example:" - echo "${0} GFS 1024002 2024010100 24" + echo " -e EXP -- meteorological drivers. For example, GFS" + echo " -f FCST -- Simulation length in hours, e.g., 24 or 48." + echo " -r RES -- grid resolution. Supported options are:" + echo " 65536002 (~ 3 km)" + echo " 5898242 (~ 10 km)" + echo " 2621442 (~ 15 km)" + echo " 1024002 (~ 24 km)" + echo " 655362 (~ 30 km)" + echo " 163842 (~ 60 km)" + echo " 40962 (~ 120 km)" + echo " -t YYYYMMDDHH -- Initial time. For example if 22 Sept 2025 00 UTC, set it to:" + echo " 2025092200" echo "" +} +#---~--- + + + +#--- Default input variables: +MONAN_ONETWO="" +EXP="" +RES="" +YYYYMMDDHHi="" +FCST="" +#---~--- + + +#--- Parse arguments. +while [[ ${#} > 0 ]] +do + key="${1}" + case ${key} in + -e) + EXP="${2}" + shift 2 # past flag and argument + ;; + -f) + FCST="${2}" + shift 2 # past flag and argument + ;; + -h) + show_usage + exit 0 + ;; + -m12) + MONAN_ONETWO="${key}" + shift 1 # past flag + ;; + -r) + RES="${2}" + shift 2 # past flag and argument + ;; + -t) + YYYYMMDDHHi="${2}" + shift 2 # past flag and argument + ;; + *) + echo "" + echo " Option \"${key}\" is not valid." + echo "" + show_usage + echo "" + echo " *** FATAL ERROR! ***" + echo " Unknown key or key-value argument pair." + echo "" + exit 2 + ;; + esac +done +#---~--- - exit + + +#---~--- +# Make sure all required settings were provided. +#---~--- +if [[ "${EXP}" == "" ]] || [[ "${RES}" == "" ]] || + [[ "${YYYYMMDDHHi}" == "" ]] || [[ "${FCST}" == "" ]] +then + echo " This script requires some arguments to be set through flags." + show_usage + exit 2 fi +#---~--- -# Set environment variables exports: -echo "" -echo -e "\033[1;32m==>\033[0m Moduling environment for MONAN model...\n" -. setenv.bash + + +#--- Set environment variables exports: +. setenv.bash ${MONAN_ONETWO} +#---~--- echo "" echo "---- Make Static ----" echo "" -# Standart directories variables:--------------------------------------- -DIRHOMES=${DIR_SCRIPTS}/scripts_CD-CT; mkdir -p ${DIRHOMES} -DIRHOMED=${DIR_DADOS}/scripts_CD-CT; mkdir -p ${DIRHOMED} -SCRIPTS=${DIRHOMES}/scripts; mkdir -p ${SCRIPTS} -DATAIN=${DIRHOMED}/datain; mkdir -p ${DATAIN} -DATAOUT=${DIRHOMED}/dataout; mkdir -p ${DATAOUT} -SOURCES=${DIRHOMES}/sources; mkdir -p ${SOURCES} -EXECS=${DIRHOMED}/execs; mkdir -p ${EXECS} -#---------------------------------------------------------------------- - - -# Input variables:-------------------------------------- -EXP=${1}; #EXP=GFS -RES=${2}; #RES=1024002 -YYYYMMDDHHi=${3}; #YYYYMMDDHHi=2024012000 -FCST=${4}; #FCST=24 -#------------------------------------------------------- - # Local variables-------------------------------------- GEODATA=${DATAIN}/WPS_GEOG @@ -59,9 +123,9 @@ export DIRRUN=${DIRHOMED}/run.${YYYYMMDDHHi}; rm -fr ${DIRRUN}; mkdir -p ${DIRRU -if [ ! -s ${DATAIN}/fixed/x1.${RES}.graph.info.part.${cores} ] +if [[ ! -s ${DATAIN}/fixed/x1.${RES}.graph.info.part.${cores} ]] then - if [ ! -s ${DATAIN}/fixed/x1.${RES}.graph.info ] + if [[ ! -s ${DATAIN}/fixed/x1.${RES}.graph.info ]] then mkdir -p ${DATAIN}/fixed cd ${DATAIN}/fixed @@ -82,9 +146,9 @@ fi files_needed=("${EXECS}/init_atmosphere_model" "${DATAIN}/fixed/x1.${RES}.graph.info.part.${cores}" "${DATAIN}/fixed/x1.${RES}.grid.nc" "${SCRIPTS}/namelists/namelist.init_atmosphere.STATIC" "${SCRIPTS}/namelists/streams.init_atmosphere.STATIC") for file in "${files_needed[@]}" do - if [ ! -s "${file}" ] + if [[ ! -s "${file}" ]] then - echo -e "\n${RED}==>${NC} ***** ATTENTION *****\n" + echo -e "\n${RED}==>${NC} ***** FATAL ERROR *****\n" echo -e "${RED}==>${NC} [${0}] At least the file ${file} was not generated. \n" exit -1 fi @@ -109,7 +173,7 @@ cp -f ${SCRIPTS}/setenv.bash ${DIRRUN} mkdir -p ${DATAOUT}/logs rm -f ${DIRRUN}/static.bash -if [ ${SCHEDULER_SYSTEM} != "GENERIC" ] +if [[ ${SCHEDULER_SYSTEM} != "GENERIC" ]] then sed -e "s,#JOBNAME#,${STATIC_jobname},g; s,#NNODES#,${STATIC_nnodes},g; @@ -137,7 +201,7 @@ ulimit -c unlimited ulimit -v unlimited -. ${SCRIPTS}/setenv.bash +. ${SCRIPTS}/setenv.bash ${MONAN_ONETWO} cd ${DIRRUN} chmod 755 * @@ -145,13 +209,21 @@ chmod 755 * date beg_secs=\`date +"%s"\` -if [ "$HOSTNAME" = "egeon" ]; then +#ML: Replace HOSTNAME with SCHEDULER_SYSTEM so this can be expanded to other HPC environments more easily. +case "${SCHEDULER_SYSTEM}" in +SLURM) echo "-- SLURM_JOB_ID: \$SLURM_JOB_ID" time mpirun -np ${STATIC_ncores} ./\${executable} -else + ;; +PBS) echo "-- PBS_JOBID: \$PBS_JOBID" time mpirun --ppn ${STATIC_ncpn} -np ${STATIC_ncores} --depth=${STATIC_nthreads} --cpu-bind depth ./\${executable} -fi + ;; +#*) +# echo "-- GENERIC: " +# time mpirun -np ${STATIC_ncores} ./\${executable} +# ;; +esac date end_secs=\`date +"%s"\` @@ -161,7 +233,7 @@ echo "STATIC time taken by run in seconds is " \$wallsecs grep "Finished running" log.init_atmosphere.0000.out >& /dev/null -if [ \$? -ne 0 ]; then +if [[ \$? -ne 0 ]]; then echo " BUMMER: Static generation failed for some yet unknown reason." echo " " tail -10 ${STATICPATH}/log.init_atmosphere.0000.out @@ -181,21 +253,21 @@ rm -fr ${DATAIN}/fixed/x1.${RES}.static.nc case "${SCHEDULER_SYSTEM}" in - SLURM) - echo -e "${GREEN}==>${NC} Sbatch static.bash...\n" - cd ${DIRRUN} - sbatch --wait ${DIRRUN}/static.bash - ;; - PBS) - echo -e "${GREEN}==>${NC} qsub static.bash...\n" - cd ${DIRRUN} - qsub -W block=true ${DIRRUN}/static.bash - ;; -# GENERIC) -# echo "Nenhum gerenciador detectado" -# cd ${DIRRUN} -# ${DIRRUN}/model.bash -# ;; +SLURM) + echo -e "${GREEN}==>${NC} sbatch static.bash...\n" + cd ${DIRRUN} + sbatch --wait ${DIRRUN}/static.bash + ;; +PBS) + echo -e "${GREEN}==>${NC} qsub static.bash...\n" + cd ${DIRRUN} + qsub -W block=true ${DIRRUN}/static.bash + ;; +#GENERIC) +# echo "No job manager found" +# cd ${DIRRUN} +# ${DIRRUN}/model.bash +# ;; esac mv ${DIRRUN}/static.bash ${DATAOUT}/logs/ @@ -204,11 +276,12 @@ mv ${DIRRUN}/namelist.init_atmosphere ${DATAOUT}/logs/ mv log.init_atmosphere.* ${DATAOUT}/logs/ -if [ -s ${DIRRUN}/x1.${RES}.static.nc ] +if [[ -s ${DIRRUN}/x1.${RES}.static.nc ]] then mv ${DIRRUN}/x1.${RES}.static.nc ${DATAIN}/fixed chmod 755 ${DATAIN}/fixed/* else + echo -e "${RED}==>${NC} ****** FATAL ERROR ******\n" echo -e "${RED}==>${NC} File ${DIRRUN}/x1.${RES}.static.nc was not created. \n" exit -1 fi diff --git a/scripts/make_template.bash b/scripts/make_template.bash index 2d6abf4..02b8b77 100755 --- a/scripts/make_template.bash +++ b/scripts/make_template.bash @@ -16,70 +16,157 @@ umask 022 # #-----------------------------------------------------------------------------# -if [ $# -ne 4 -a $# -ne 1 ] -then +#--- Function that shows usage. +function show_usage() { + echo " Usage: " echo "" - echo "Instructions: execute the command below" + echo " ${0} [-h] [-m12] [-v VARTABLE] [-d OUTPUT_DIAG_INT] [-e EXP ] [-f FCST] \\" + echo " [-r RES] [-t YYYYMMDDHH]" echo "" - echo "${0} ]EXP_NAME/OP] RESOLUTION LABELI FCST" + echo " List of optional flags: " echo "" - echo "EXP_NAME :: Forcing: GFS" - echo "RESOLUTION :: number of points in resolution model grid, e.g: 1024002 (24 km)" - echo "LABELI :: Initial date YYYYMMDDHH, e.g.: 2024010100" - echo "FCST :: Forecast hours, e.g.: 24 or 36, etc." + echo " -h -- Shows this message." + echo " -m12 -- Is this a MONAN run based on 1.2.0-rc and branches" + echo " derived from this version (e.g., feature/monan-757-NF)?" + echo " This is a temporary flag that will be removed once the" + echo " versions containing Noah-MP are merged into the new" + echo " release. This allows the script to manage older code and" + echo " still run on jaci." + echo " -v VARTABLE -- Suffix for defining which version of the" + echo " stream_list_atmosphere.diagnostics template to use." + echo " The default is to not use any suffix." echo "" - echo "24 hour forcast example:" - echo "${0} GFS 1024002 2024010100 24" - echo "${0} GFS 40962 2024010100 48" + echo " List of **required** flags: " echo "" - - exit + echo " -d OUTPUT_DIAG_INT -- Output interval for diagnostic. The format must be" + echo " \"HH:MM:SS\"" + echo " -e EXP -- meteorological drivers. For example, GFS" + echo " -f FCST -- Simulation length in hours, e.g., 24 or 48." + echo " -r RES -- grid resolution. Supported options are:" + echo " 65536002 (~ 3 km)" + echo " 5898242 (~ 10 km)" + echo " 2621442 (~ 15 km)" + echo " 1024002 (~ 24 km)" + echo " 655362 (~ 30 km)" + echo " 163842 (~ 60 km)" + echo " 40962 (~ 120 km)" + echo " -t YYYYMMDDHH -- Initial time. For example if 22 Sept 2025 00 UTC," + echo " set it to: 2025092200" + echo "" +} +#---~--- + + + + +#--- Default input variables: +MONAN_ONETWO="" +EXP="" +RES="" +YYYYMMDDHHi="" +FCST="" +OUTPUT_DIAG_INTERVAL="" +VARTABLE="" +#---~--- + + +#--- Parse arguments. +while [[ ${#} > 0 ]] +do + key="${1}" + case ${key} in + -d) + OUTPUT_DIAG_INTERVAL="${2}" + shift 2 # Past flag and argument + ;; + -e) + EXP="${2}" + shift 2 # past flag and argument + ;; + -f) + FCST="${2}" + shift 2 # past flag and argument + ;; + -h) + show_usage + exit 0 + ;; + -m12) + MONAN_ONETWO="${key}" + shift 1 # past flag + ;; + -r) + RES="${2}" + shift 2 # past flag and argument + ;; + -t) + YYYYMMDDHHi="${2}" + shift 2 # past flag and argument + ;; + -v) + VFIRST=$(echo ${2} | cut -c 1-1) + case "${VFIRST}" in + .) VARTABLE="${2}" ;; + *) VARTABLE=".${2}" ;; + esac + shift 2 # past flag and argument + ;; + *) + echo "" + echo " Option \"${key}\" is not valid." + echo "" + show_usage + echo "" + echo " *** FATAL ERROR! ***" + echo " Unknown key or key-value argument pair." + echo "" + exit 2 + ;; + esac +done +#---~--- + + + +#---~--- +# Make sure all required settings were provided. +#---~--- +if [[ "${EXP}" == "" ]] || [[ "${RES}" == "" ]] || + [[ "${YYYYMMDDHHi}" == "" ]] || [[ "${FCST}" == "" ]] || + [[ "${OUTPUT_DIAG_INTERVAL}" == "" ]] +then + echo " This script requires some arguments to be set through flags." + show_usage + exit 2 fi +#---~--- -# Set environment variables exports: -echo "" -echo -e "\033[1;32m==>\033[0m Moduling environment for MONAN model...\n" -. setenv.bash + +#--- Set environment variables exports: +. setenv.bash ${MONAN_ONETWO} +#---~--- echo "" echo "---- Make Template ----" echo "" -# Standart directories variables:--------------------------------------- -DIRHOMES=$(dirname "$(pwd)"); mkdir -p ${DIRHOMES} -DIRHOMED=${DIR_DADOS}/scripts_CD-CT; mkdir -p ${DIRHOMED} -export SCRIPTS=${DIRHOMES}/scripts; mkdir -p ${SCRIPTS} -DATAIN=${DIRHOMED}/datain; mkdir -p ${DATAIN} -DATAOUT=${DIRHOMED}/dataout; mkdir -p ${DATAOUT} -SOURCES=${DIRHOMES}/sources; mkdir -p ${SOURCES} -EXECS=${DIRHOMED}/execs; mkdir -p ${EXECS} -#---------------------------------------------------------------------- - - -# Input variables:-------------------------------------- -EXP=${1}; #EXP=GFS -RES=${2}; #RES=1024002 -YYYYMMDDHHi=${3}; #YYYYMMDDHHi=2024042000 -FCST=${4}; #FCST=40 -#------------------------------------------------------- +#--- Set and create standard directories mkdir -p ${DATAOUT}/${YYYYMMDDHHi}/Post/logs +#---~--- # Local variables-------------------------------------- START_DATE_YYYYMMDD="${YYYYMMDDHHi:0:4}-${YYYYMMDDHHi:4:2}-${YYYYMMDDHHi:6:2}" START_HH="${YYYYMMDDHHi:8:2}" -maxpostpernode=20 # <------ qtde max de convert_mpas por no! -VARTABLE=".OPER" -export DIRRUN=${DIRHOMED}/run.${YYYYMMDDHHi}; rm -fr ${DIRRUN}; mkdir -p ${DIRRUN} -N_MODEL_LEV=55 +export DIRRUN=${DIRHOMED}/run.${YYYYMMDDHHi}; mkdir -p ${DIRRUN} #------------------------------------------------------- -# Variables for flex outpout interval from streams.atmosphere------------------------ -t_strout=$(cat ${SCRIPTS}/namelists/streams.atmosphere.TEMPLATE | sed -n '//s/.*output_interval="\([^"]*\)".*/\1/p') -t_stroutsec=$(echo ${t_strout} | awk -F: '{print ($1 * 3600) + ($2 * 60) + $3}') -t_strouthor=$(echo "scale=4; (${t_stroutsec}/60)/60" | bc) -t_stroutmin=$(echo "${t_stroutsec}/60" | bc) +#--- Variables for flexible output intervals. +t_strout=${OUTPUT_DIAG_INTERVAL} +t_stroutsec=`echo ${t_strout} | awk -F: '{print ($1 * 3600) + ($2 * 60) + $3}'` +t_strouthor=`echo "scale=4; (${t_stroutsec}/60)/60" | bc` +t_stroutmin=`echo "${t_stroutsec}/60" | bc` #------------------------------------------------------------------------------------ cd ${DIRRUN} @@ -89,44 +176,94 @@ cd ${DIRRUN} IFS=":" read -r h m s <<< "${t_strout}" printf -v t_strout "%02d:%02d:%02d" "$h" "$m" "$s" -# Calculating default parameters for different resolutions -if [ $RES -eq 1024002 ]; then #24Km - NLAT=721 #180/0.25 - NLON=1441 #360/0.25 +#---~--- +# Calculate default parameters for different resolutions. +# ML: The original numbers assumed 1 degree ~ 100 km. Across latitude and near the +# Equator, 1 degree ~ 111.2 km, so the regridded data ended up being slightly coarser +# than it needed to be. To ensure an average grid mesh in regular lon/lat that is +# close to the original resolution, we determine the average resolution based on +# the number of points in a full sphere (4*pi steradians), and pick the nearest +# integer number of points per degree. +# delta_xy = sqrt( 4*pi * (180/pi)^2 / NumberOfPoints) +# PointsPerDegree = round(1/delta_xy) +# NLON = 360 * PointsPerDegree + 1 +# NLAT = 180 * PointsPerDegree + 1 +#---~--- +case ${RES} in +5898242) + #---~--- + # 10 km, use 12 points per degree + #---~--- + NLAT=2161 + NLON=4321 STARTLAT=-90.0 STARTLON=0.0 ENDLAT=90.0 ENDLON=360.0 -elif [ $RES -eq 2621442 ]; then #15Km - NLAT=1201 #180/0.15 - NLON=2401 #360/0.15 + #---~--- + ;; +2621442) + #---~--- + # 15 km, use 8 points per degree + #---~--- + NLAT=1441 + NLON=2881 STARTLAT=-90.0 STARTLON=0.0 ENDLAT=90.0 ENDLON=360.0 -elif [ $RES -eq 40962 ]; then #120Km - NLAT=150 #180/1.2 - NLON=300 #360/1.2 + #---~--- + ;; +1024002) + #---~--- + # 24 km, use 5 points per degree + #---~--- + NLAT=901 + NLON=1801 STARTLAT=-90.0 STARTLON=0.0 ENDLAT=90.0 ENDLON=360.0 -fi -#------------------------------------------------------- - -# NLEVS get from t_iso_levels in Registry_isobaric.xml: + #---~--- + ;; +40962) + #---~--- + # 120 km, use 1 points per degree + #---~--- + NLAT=181 + NLON=361 + STARTLAT=-90.0 + STARTLON=0.0 + ENDLAT=90.0 + ENDLON=360.0 + #---~--- + ;; +*) + #---~--- + # Unrecognised resolution. + #---~--- + echo -e "${RED}****** FATAL ERROR ******${NC} \n" + echo -e "${RED}==>${NC} Provided grid resolution (${RES}) is not recognised.\n" + echo -e "${RED}==>${NC} ${0} cannot post-process this MONAN simulation.\n" + exit -1 + #---~--- + ;; +esac +#---~--- + +# Retrieve N_ISOBARIC_LEV from t_iso_levels in Registry_isobaric.xml: if [ -s ${MONANDIR}/src/core_atmosphere/diagnostics/Registry_isobaric.xml ] then - NLEV=$(grep "t_iso_levels" ${MONANDIR}/src/core_atmosphere/diagnostics/Registry_isobaric.xml | grep definition | cut -d\" -f4) + N_ISOBARIC_LEV=$(grep "t_iso_levels" ${MONANDIR}/src/core_atmosphere/diagnostics/Registry_isobaric.xml | grep definition | cut -d\" -f4) else - NLEV=18 + N_ISOBARIC_LEV=18 fi output_interval=${t_strouthor} nfiles=$(echo "$FCST/$output_interval + 1" | bc) -diag_name_post=MONAN_DIAG_G_POS_${EXP}_${YYYYMMDDHHi}_${YYYYMMDDHHi}.00.00.x${RES}L${N_MODEL_LEV}.nc -diag_name_templ=MONAN_DIAG_G_POS_${EXP}_${YYYYMMDDHHi}_%y4%m2%d2%h2.%n2.00.x${RES}L${N_MODEL_LEV}.nc +diag_name_post=MONAN_DIAG_G_POS_${EXP}_${YYYYMMDDHHi}_${YYYYMMDDHHi}.00.00.x${RES}L${N_ISOBARIC_LEV}.nc +diag_name_templ=MONAN_DIAG_G_POS_${EXP}_${YYYYMMDDHHi}_%y4%m2%d2%h2.%n2.00.x${RES}L${N_ISOBARIC_LEV}.nc @@ -147,7 +284,7 @@ EOGS cd ${DIRRUN} -. ${SCRIPTS}/setenv.bash +. ${SCRIPTS}/setenv.bash ${MONAN_ONETWO} chmod 755 * @@ -162,4 +299,3 @@ sed -i "/dset/c\dset ^${diag_name_templ}" ${DIRRUN}/qctlinfo.ctl chmod 755 ${DIRRUN}/* mv ${DIRRUN}/qctlinfo.ctl ${DATAOUT}/${YYYYMMDDHHi}/Post/${diag_name_post}.template.ctl -rm -fr ${DIRRUN} diff --git a/scripts/namelists/namelist.atmosphere.TEMPLATE b/scripts/namelists/namelist.atmosphere.TEMPLATE index 72952b9..11d4b70 100755 --- a/scripts/namelists/namelist.atmosphere.TEMPLATE +++ b/scripts/namelists/namelist.atmosphere.TEMPLATE @@ -63,6 +63,7 @@ config_conv_interval = '#CONFIG_CONV_INTERVAL#' config_bucket_update = 'none' config_physics_suite = 'mesoscale_reference_monan' + config_lsm_scheme = 'sf_noah' config_mynn_edmf = 0 / &gf_monan diff --git a/scripts/namelists/streams.atmosphere.TEMPLATE b/scripts/namelists/streams.atmosphere.TEMPLATE index 04def1c..5432356 100755 --- a/scripts/namelists/streams.atmosphere.TEMPLATE +++ b/scripts/namelists/streams.atmosphere.TEMPLATE @@ -24,7 +24,7 @@ type="output" io_type="pnetcdf,cdf5" filename_template="MONAN_DIAG_G_MOD_#CIORIG#_#LABELI#_$Y$M$D$h.$m.$s.x#RES#L#NLEV#.nc" - output_interval="3:00:00" > + output_interval="#OUTPUT_DIAG_INTERVAL#" > diff --git a/scripts/requirements.txt b/scripts/requirements.txt new file mode 100644 index 0000000..cdd49f5 --- /dev/null +++ b/scripts/requirements.txt @@ -0,0 +1,2 @@ +netCDF4>=1.6.5 +numpy>=1.26.4 diff --git a/scripts/run_past2now.bash b/scripts/run_past2now.bash index 8b93e86..91879e1 100755 --- a/scripts/run_past2now.bash +++ b/scripts/run_past2now.bash @@ -1,54 +1,212 @@ #!/bin/bash -yyyymmddi=20241001 -yyyymmddf=20241005 -yyyymmdd=${yyyymmddi} +#--- Function that shows usage. +function show_usage() { + echo " Usage: " + echo "" + echo " ${0} [-o] [-d OUTPUT_DIAG_INT] [-e EXP ] [-f00 FCST_ZERO] [-f12 FCST_TWELVE] \\" + echo " [-i INPUT_PATH] [-l NLEV] [-r RES] [-ti YYYYMMDDi] [-tf YYYYMMDDf] \\" + echo " [-v VARTABLE]" + echo "" + echo " List of optional flags: " + echo " -d OUTPUT_DIAG_INT -- Output interval for diagnostic. The format must be" + echo " \"HH:MM:SS\"." + echo " -e EXP -- Meteorological drivers. For example, GFS" + echo " -f00 FCST_ZERO -- Simulation length in hours for runs starting at 00 UTC" + echo " e.g., 24 or 48." + echo " -f12 FCST_TWELVE -- Simulation length in hours for runs starting at 12 UTC" + echo " e.g., 24 or 48." + echo " -i INPUT_PATH -- Path containing input data for MONAN. If left empty, the" + echo " default path defined in setenv.bash will be used" + echo " -l NLEV -- Number of vertical levels for the output." + echo " -o -- Overwrite static files. If set, this will be done only" + echo " once." + echo " -r RES -- grid resolution. Supported options are:" + echo " 5898242 (~ 10 km)" + echo " 2621442 (~ 15 km)" + echo " 1024002 (~ 24 km)" + echo " 40962 (~ 120 km)" + echo " -ti YYYYMMDDi -- First initial date to run the script. For example if the" + echo " first run's initial time is on 22 Sept 2025, the" + echo " argument should be 20250922." + echo " -tf YYYYMMDDf -- Last initial date to run the script. For example if the" + echo " last run's initial time is on 31 Dec 2025, the" + echo " argument should be 20251231." + echo " -v VARTABLE -- Suffix for defining which version of the" + echo " stream_list_atmosphere.diagnostics template to use." + echo " The default is to not use any suffix." + echo "" + echo " All settings can be defined directly in the script." + echo "" +} +#---~--- + + +#--- Set environment variables exports: +. setenv.bash +#---~--- + + + + +#--- Default input variables: +STEP=1 +CLEAN="" +OVERWRITE="" +github_link_MONAN="https://github.com/monanadmin/MONAN-Model.git" +tag_or_branch_name_MONAN="release/1.4.1-rc" +github_link_CONVERT_MPAS="https://github.com/monanadmin/convert_mpas.git" +tag_or_branch_name_CONVERT_MPAS="release/1.2.0" +EXP="GFS" +INPUT_PATH="" +RES=1024002 +YYYYMMDDi=2024100100 +YYYYMMDDf=2024100500 +FCST_ZERO=240 +FCST_TWELVE=120 +NLEV=55 +OUTPUT_DIAG_INTERVAL="03:00:00" +VARTABLE="" +#---~--- + + +#--- Parse arguments. +while [[ ${#} > 0 ]] +do + key="${1}" + case ${key} in + -c) + CLEAN="-c" + shift 1 # Past flag + ;; + -d) + OUTPUT_DIAG_INTERVAL="${2}" + shift 2 # Past flag and argument + ;; + -e) + EXP="${2}" + shift 2 # past flag and argument + ;; + -f00) + FCST_ZERO="${2}" + shift 2 # past flag and argument + ;; + -f12) + FCST_TWELVE="${2}" + shift 2 # past flag and argument + ;; + -i) + INPUT_PATH="${2}" + shift 2 # Past flag and argument + ;; + -l) + NLEV="${2}" + shift 2 # Past flag and argument + ;; + -o) + OVERWRITE="-o" + shift 1 # past flag + ;; + -r) + RES="${2}" + shift 2 # past flag and argument + ;; + -s) + STEP="${2}" + shift 2 # past flag and argument + ;; + -ti) + YYYYMMDDHHi="${2}" + shift 2 # past flag and argument + ;; + -tf) + YYYYMMDDHHf="${2}" + shift 2 # past flag and argument + ;; + -v) + VFIRST=$(echo ${2} | cut -c 1-1) + case "${VFIRST}" in + .) VARTABLE="${2}" ;; + *) VARTABLE=".${2}" ;; + esac + shift 2 # past flag and argument + ;; + *) + echo "Unknown key-value argument pair." + show_usage + exit 2 + ;; + esac +done +#---~--- + + + +#--- If INPUT_PATH is provided, replace the path in setenv.bash +if [[ "${INPUT_PATH}" != "" ]] && [[ -d "${INPUT_PATH}" ]] +then + sed -i.bck "s,^export DIRDADOS=.*,export DIRDADOS=${INPUT_PATH},g" ${SCRIPTS}/setenv.bash + /bin/rm -f ${SCRIPTS}/setenv.bash.bck +fi +#---~--- # Input variables:----------------------------------------------------- DIR_DADOS=/mnt/beegfs/monan/users/renato/issues/ecflow-PREOPER/SCRATCHOUT; mkdir -p ${DIR_DADOS} DIRFLUSHOUT=/mnt/beegfs/monan/users/renato/issues/trashout; mkdir -p ${DIRFLUSHOUT} -EXP=GFS -RES=1024002 -FCST=240 #---------------------------------------------------------------------- -while [ ${yyyymmdd} -le ${yyyymmddf} ] +#--- Make sure VARTABLE has the leading "-v" if not empty. +if [[ "${VARTABLE}" == "" ]] +then + dv_VARTABLE="" +else + dv_VARTABLE="-v ${VARTABLE}" +fi +#---~--- + + +YYYYMMDD=${YYYYMMDDi} +while [ ${YYYYMMDD} -le ${YYYYMMDDf} ] do + HH_LIST="00 12" + for HH in ${HH_LIST} + do + YYYYMMDDHHi="${YYYYMMDD}${HH}" + case ${HH} in + 00) FCST=${FCST_ZERO} ;; + 12) FCST=${FCST_TWELVE} ;; + esac - echo "${SCRIPTS}/2.pre_processing.bash ${EXP} ${RES} ${yyyymmdd}00 240" - ${SCRIPTS}/2.pre_processing.bash ${EXP} ${RES} ${yyyymmdd}00 240 - echo "${SCRIPTS}/3.run_model.bash ${EXP} ${RES} ${yyyymmdd}00 240" - ${SCRIPTS}/3.run_model.bash ${EXP} ${RES} ${yyyymmdd}00 240 - echo "${SCRIPTS}/4.run_post.bash ${EXP} ${RES} ${yyyymmdd}00 240" - ${SCRIPTS}/4.run_post.bash ${EXP} ${RES} ${yyyymmdd}00 240 - - # Final data output directory: - mkdir -p ${DIRFLUSHOUT}/${yyyymmdd}00/ - # Copy post: - cp -fr ${DIRSCRIPTDADOS}/dataout/${yyyymmdd}00/Post/* ${DIRFLUSHOUT}/${yyyymmdd}00/ - # Remove all output files from the original output diretory dataout: - rm -fr ${DIRSCRIPTDADOS}/dataout/${yyyymmdd}00 - rm -fr ${DIRSCRIPTDADOS}/datain/${yyyymmdd}00 - + # Set arguments. + ARGS_TWO="${OVERWRITE} -e ${EXP} -f ${FCST} -r ${RES} -t ${YYYYMMDDHHi}" + ARGS_THREE="${dv_VARTABLE} -d ${OUTPUT_DIAG_INTERVAL} -e ${EXP}" + ARGS_THREE="${ARGS_THREE} -f ${FCST} -l ${NLEV} -r ${RES} -t ${YYYYMMDDHHi}" + ARGS_FOUR="${dv_VARTABLE} -d ${OUTPUT_DIAG_INTERVAL} -e ${EXP} -f ${FCST}" + ARGS_FOUR="${ARGS_FOUR} -l ${NLEV} -r ${RES} -t ${YYYYMMDDHHi}" - - echo "${SCRIPTS}/2.pre_processing.bash ${EXP} ${RES} ${yyyymmdd}12 120" - ${SCRIPTS}/2.pre_processing.bash ${EXP} ${RES} ${yyyymmdd}12 120 - echo "${SCRIPTS}/3.run_model.bash ${EXP} ${RES} ${yyyymmdd}12 120" - ${SCRIPTS}/3.run_model.bash ${EXP} ${RES} ${yyyymmdd}12 120 - echo "${SCRIPTS}/4.run_post.bash ${EXP} ${RES} ${yyyymmdd}12 120" - ${SCRIPTS}/4.run_post.bash ${EXP} ${RES} ${yyyymmdd}12 120 - - # Final data output directory: - mkdir -p ${DIRFLUSHOUT}/${yyyymmdd}12/ - # Copy post: - cp -fr ${DIRSCRIPTDADOS}/dataout/${yyyymmdd}12/Post/* ${DIRFLUSHOUT}/${yyyymmdd}12/ - # Remove all output files from the original output diretory dataout: - rm -fr ${DIRSCRIPTDADOS}/dataout/${yyyymmdd}12 - rm -fr ${DIRSCRIPTDADOS}/datain/${yyyymmdd}12 - - yyyymmdd=$(date -u +%Y%m%d -d "${yyyymmdd} 1 day") + # Run scripts + echo "${SCRIPTS}/2.pre_processing.bash ${ARGS_TWO}" + ${SCRIPTS}/2.pre_processing.bash ${ARGS_TWO} + echo "${SCRIPTS}/3.run_model.bash ${ARGS_THREE}" + ${SCRIPTS}/3.run_model.bash ${ARGS_THREE} + echo "${SCRIPTS}/4.run_post.bash ${ARGS_FOUR}" + ${SCRIPTS}/4.run_post.bash ${ARGS_FOUR} + + # Final data output directory: + mkdir -p ${DIRFLUSHOUT}/${YYYYMMDDHHi}/ + # Copy post: + cp -fr ${DIRSCRIPTDADOS}/dataout/${YYYYMMDDHHi}/Post/* ${DIRFLUSHOUT}/${YYYYMMDDHHi}/ + # Remove all output files from the original output diretory dataout: + rm -fr ${DIRSCRIPTDADOS}/dataout/${YYYYMMDDHHi} + rm -fr ${DIRSCRIPTDADOS}/datain/${YYYYMMDDHHi} + + # No need to overwrite static fields after the first iteration + OVERWRITE="" + done + + # Update time + YYYYMMDD=$(date -u +%Y%m%d -d "${YYYYMMDD} 1 day") done diff --git a/scripts/setenv.bash b/scripts/setenv.bash index 3452832..7d85034 100755 --- a/scripts/setenv.bash +++ b/scripts/setenv.bash @@ -1,6 +1,59 @@ #!/bin/bash umask 022 +#---~---- +# Colors. Please refrain from changing these variables. +#---~---- +export GREEN='\033[1;32m' # Green +export RED='\033[1;31m' # Red +export NC='\033[0m' # No Color +export BLUE='\033[01;34m' # Blue +export ORANGE='\033[01;91m' # Orange +#---~---- + + +#---~---- +# Notify users that this script is being called. +# Please refrain from changing these commands. +#---~---- +echo "" +echo -e "${GREEN}==>${NC} Load MONAN settings (setenv.bash).\n" +#---~---- + + +#--- Parse arguments. +USE_ONETWO=false +while [[ ${#} > 0 ]] +do + key="${1}" + case ${key} in + -m12) + USE_ONETWO=true + shift 1 # past flag + ;; + *) + echo "" + echo " Unknown key-value argument pair." + echo " Usage: " + echo "" + echo " . ${BASH_SOURCE[0]} [-m12]" + echo "" + echo " List of optional flags: " + echo "" + echo " -m12 -- Is this a MONAN run based on 1.2.0-rc and branches" + echo " derived from this version (e.g., feature/monan-757-NF)?" + echo " This is a temporary flag that will be removed once the" + echo " versions containing Noah-MP are merged into the new" + echo " release. This allows the script to manage older code and" + echo " still run on jaci." + echo "" + return + ;; + esac +done +#---~--- + + # Choose your compiler here (only on Jaci; on Egeon the compiler is fixed to ‘gnu’): export COMPILER=intel #export COMPILER=gnu @@ -24,27 +77,49 @@ fi # Detect hostname THOSTNAME=$(hostname -s) -# Identifying several names of the egeon: +#---~--- +# Identify which machine is being used based on the host name. Most HPC systems have +# multiple login nodes, but they share a common configuration system. +#---~--- case ${THOSTNAME} in - egeon-login|headnode|n[0-9]|n[1-2][0-9]|n3[0-3]) - export HOSTNAME="egeon" - export MAKE_TARG=gfortran - export MAKE_TARG2=gfortran - COMPILER=gnu +egeon-login|headnode|n[0-9]|n[1-2][0-9]|n3[0-3]) + #---~--- + # Egeon. Use gnu + #---~--- + export HOSTNAME="egeon" + export MAKE_TARG=gfortran + export MAKE_TARG2=gfortran + COMPILER=gnu + #---~--- + ;; +ian[0-9]*|cn-0[0-9][0-9][0-9]) + #---~--- + # Jaci. Decide which compiler to use based on variable COMPILER and the MONAN + # version we are running. + #---~--- + export HOSTNAME="ian" + case "${COMPILER}" in + intel) + export MAKE_TARG=intel-xd2000 + export MAKE_TARG2=intel2-xd2000 ;; - ian[0-9]*|cn-0[0-9][0-9][0-9]) - export HOSTNAME="ian" - if [ "$COMPILER" == "intel" ]; then - export MAKE_TARG=intel-xd2000 - export MAKE_TARG2=intel2-xd2000 - elif [ "$COMPILER" == "gnu" ]; then - export MAKE_TARG=gfortran-xd2000 - export MAKE_TARG2=gfortran-xd2000 - fi + gnu) + export MAKE_TARG=gfortran-xd2000 + export MAKE_TARG2=gfortran-xd2000 ;; + esac + #---~--- + ;; +*) + #---~--- + # Generic variable, use the hostname and hope for the best. + #---~--- + export HOSTNAME="${THOSTNAME}" + ;; + #---~--- esac # Make the same for other machines/systems... -echo "Host detected: $HOSTNAME" +echo "Host detected: ${HOSTNAME}" echo "Compiler to be used: ${COMPILER}" # Set unique key: scheduler + host: @@ -57,7 +132,9 @@ export SYSTEM_KEYC="${SCHEDULER_SYSTEM}_${HOSTNAME}_${COMPILER}" # MONAN-suite install root directories: # Put your directories: -export DIR_SCRIPTS=$(dirname $(dirname $(pwd))) +#ML slightly edited the logic in here, somehow using pwd was leading to errors on Jaci. +export THIS_PATH=`(cd "$(dirname ${BASH_SOURCE[0]})" && pwd)` +export DIR_SCRIPTS=$(dirname $(dirname ${THIS_PATH})) export DIR_DADOS=${DIR_SCRIPTS} export MONANDIR=$MONANDIR @@ -77,12 +154,17 @@ export MONANDIR=$MONANDIR # Others variables: -# Colors: -# -export GREEN='\033[1;32m' # Green -export RED='\033[1;31m' # Red -export NC='\033[0m' # No Color -export BLUE='\033[01;34m' # Blue +#--- Set and create standard directories +export DIRHOMES=${DIR_SCRIPTS}/scripts_CD-CT; mkdir -p ${DIRHOMES} +export DIRHOMED=${DIR_DADOS}/scripts_CD-CT; mkdir -p ${DIRHOMED} +export SCRIPTS=${DIRHOMES}/scripts; mkdir -p ${SCRIPTS} +export DATAIN=${DIRHOMED}/datain; mkdir -p ${DATAIN} +export DATAOUT=${DIRHOMED}/dataout; mkdir -p ${DATAOUT} +export SOURCES=${DIRHOMES}/sources; mkdir -p ${SOURCES} +export EXECS=${DIRHOMED}/execs; mkdir -p ${EXECS} +#---~--- + + # Functions: ====================================================================================================== @@ -92,11 +174,11 @@ how_many_nodes () { deno=${2} num=$(echo "${nume}/${deno}" | bc -l) how_many_nodes_int=$(echo "${num}/1" | bc) - dif=$(echo "scale=0; (${num}-${how_many_nodes_int})*100/1" | bc) - rest=$(echo "scale=0; (((${num}-${how_many_nodes_int})*${deno})+0.5)/1" | bc -l) - if [ ${dif} -eq 0 ]; then how_many_nodes_left=0; else how_many_nodes_left=1; fi - if [ ${how_many_nodes_int} -eq 0 ]; then how_many_nodes_int=1; how_many_nodes_left=0; rest=0; fi - how_many_nodes=$(echo "${how_many_nodes_int}+${how_many_nodes_left}" | bc ) + dif=`echo "scale=0; (${num}-${how_many_nodes_int})*100/1" | bc` + rest=`echo "scale=0; (((${num}-${how_many_nodes_int})*${deno})+0.5)/1" | bc -l` + if [[ ${dif} -eq 0 ]]; then how_many_nodes_left=0; else how_many_nodes_left=1; fi + if [[ ${how_many_nodes_int} -eq 0 ]]; then how_many_nodes_int=1; how_many_nodes_left=0; rest=0; fi + how_many_nodes=`echo "${how_many_nodes_int}+${how_many_nodes_left}" | bc ` #echo "INT number of nodes needed: \${how_many_nodes_int} = ${how_many_nodes_int}" #echo "number of nodes left: \${how_many_nodes_left} = ${how_many_nodes_left}" echo "The number of nodes needed: \${how_many_nodes} = ${how_many_nodes}" diff --git a/scripts/setenv_python.bash b/scripts/setenv_python.bash new file mode 100644 index 0000000..1c9ca21 --- /dev/null +++ b/scripts/setenv_python.bash @@ -0,0 +1,42 @@ +#!/bin/bash + +case "${HOSTNAME}" in +egeon) + echo "Load python ..." + module load python-3.9.13-gcc-9.4.0-moxjnc6 + export PYTHON_EXEC=python3 + export PIP_EXEC=pip3 + ;; +ian) + echo "Using the system's python ..." + export PYTHON_EXEC=python3.12 + export PIP_EXEC=pip3.12 + ;; +esac + + +echo "Define path for python environment" +export PYTHON_ENV_PATH="${DIRHOMES}/.venv" + +echo "Define cache for PIP" +export PIP_CACHE_DIR="${DIRHOMES}/.pip-cache" +export PIP_CACHE_CONFIG="${DIRHOMES}/.pip-config" + +if [[ ! -s ${PYTHON_ENV_PATH} ]] +then + echo "Create python environment at ${PYTHON_ENV_PATH}" + ${PYTHON_EXEC} -m venv ${PYTHON_ENV_PATH} + + echo "Activate python environment" + source ${PYTHON_ENV_PATH}/bin/activate + + echo "Install python libraries" + ${PIP_EXEC} install --upgrade pip + ${PIP_EXEC} install -r ${SCRIPTS}/requirements.txt +else + echo "Activate python environment" + source ${PYTHON_ENV_PATH}/bin/activate +fi + +export PYTHONPATH=${PYTHONPATH}:${SCRIPTS} +echo "export PYTHONPATH=${PYTHONPATH}" diff --git a/scripts/stools/setenv_PBS_ian_gnu.bash b/scripts/stools/setenv_PBS_ian_gnu.bash index 196318b..4827a46 100755 --- a/scripts/stools/setenv_PBS_ian_gnu.bash +++ b/scripts/stools/setenv_PBS_ian_gnu.bash @@ -9,7 +9,7 @@ module load cray-netcdf/4.9.0.15 module load cray-parallel-netcdf/1.12.3.15 module load xpmem/0.2.119-1.3_gef379be13330 module load grads/2.2.1.oga.1 -module load cdo/2.4.2 +module load cdo/2.5.4 module load METIS/5.1.0 module load cray-pals module list diff --git a/scripts/stools/setenv_PBS_ian_intel.bash b/scripts/stools/setenv_PBS_ian_intel.bash index 63211d8..aaffd3e 100755 --- a/scripts/stools/setenv_PBS_ian_intel.bash +++ b/scripts/stools/setenv_PBS_ian_intel.bash @@ -9,7 +9,7 @@ module load cray-netcdf/4.9.0.15 module load cray-parallel-netcdf/1.12.3.15 module load xpmem/0.2.119-1.3_gef379be13330 module load grads/2.2.1.oga.1 -module load cdo/2.4.2 +module load cdo/2.5.4 module load METIS/5.1.0 module load cray-pals module list