forked from CIROH-UA/NGIAB-CloudInfra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathguide.sh
executable file
·229 lines (193 loc) · 7.13 KB
/
guide.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/bash
# Color definitions
BBlack='\033[1;30m'
BRed='\033[1;31m'
BGreen='\033[1;32m'
BYellow='\033[1;33m'
BBlue='\033[1;34m'
BPurple='\033[1;35m'
BCyan='\033[1;36m'
BWhite='\033[1;37m'
UBlack='\033[4;30m'
URed='\033[4;31m'
UGreen='\033[4;32m'
UYellow='\033[4;33m'
UBlue='\033[4;34m'
UPurple='\033[4;35m'
UCyan='\033[4;36m'
UWhite='\033[4;37m'
Color_Off='\033[0m'
set -e
CONFIG_FILE="$HOME/.host_data_path.conf"
TETHYS_SCRIPT="./viewOnTethys.sh"
echo -e "\n========================================================="
echo -e "${UWhite} Welcome to CIROH-UA:NextGen National Water Model App! ${Color_Off}"
echo -e "=========================================================\n"
echo -e "Looking for input data (a directory containing the following directories: forcings, config and outputs):\n"
echo -e "${BBlue}forcings${Color_Off} is the hydrofabric input data for your model(s)."
echo -e "${BGreen}config${Color_Off} folder has all the configuration related files for the model."
echo -e "${BPurple}outputs${Color_Off} is where the output files are copied to when the model finish the run"
echo -e "\n"
# Check if the config file exists and read from it
if [ -f "$CONFIG_FILE" ]; then
LAST_PATH=$(cat "$CONFIG_FILE")
echo -e "Last used data directory path: ${BBlue}$LAST_PATH${Color_Off}"
read -erp "Do you want to use the same path? (Y/n): " use_last_path
if [[ "$use_last_path" != [Nn]* ]]; then
HOST_DATA_PATH=$LAST_PATH
else
read -erp "Enter your input data directory path (use absolute path): " HOST_DATA_PATH
fi
else
read -erp "Enter your input data directory path (use absolute path): " HOST_DATA_PATH
fi
# Check the directory exists
if [ ! -d "$HOST_DATA_PATH" ]; then
echo -e "${BRed}Directory does not exist. Exiting the program.${Color_Off}"
exit 0
fi
# Save the new path to the config file
echo "$HOST_DATA_PATH" > "$CONFIG_FILE"
echo -e "The Directory you've given is:\n$HOST_DATA_PATH\n"
# Function to validate directories
validate_directory() {
local dir=$1
local name=$2
local color=$3
if [ -d "$dir" ]; then
local count=$(find $dir -type f | uniq | wc -l)
echo -e "${color}${name}${Color_Off} exists. $count ${name} found."
else
echo -e "Error: Directory $dir does not exist."
fi
}
validate_directory "$HOST_DATA_PATH/forcings" "forcings" "$BBlue"
validate_directory "$HOST_DATA_PATH/config" "config" "$BGreen"
validate_directory "$HOST_DATA_PATH/outputs" "outputs" "$BPurple"
# Function to perform cleanup
cleanup_folder() {
local folder_path="$1"
local file_types="$2"
local folder_name="$3"
# Construct the find command
local find_cmd="find \"$folder_path\" -maxdepth 2 -type f \( $file_types \)"
# Execute the find command and count the results
local file_count=$(eval "$find_cmd" 2> /dev/null | wc -l)
echo "Files found: $file_count"
if [ "$file_count" -gt 0 ]; then
echo -e "${UYellow}Cleanup Process: matching files ($file_types) in $folder_name: $folder_path${Color_Off}"
echo -e "Select an option (type a number): "
choose_option
else
echo "$folder_name is ready for run. No matching files found."
fi
}
choose_option() {
options=("Delete files and run fresh" "Continue without cleaning" "Exit")
select option in "${options[@]}"; do
case $option in
"Delete files and run fresh")
echo "Cleaning folder for fresh run"
# Construct the find delete command
local find_delete_cmd="find \"$folder_path\" -maxdepth 2 -type f \( $file_types \) -delete"
# Execute the find delete command
eval "$find_delete_cmd"
break
;;
"Continue without cleaning")
echo "Continuing with existing files."
break
;;
"Exit")
echo "Exiting script. Have a nice day!"
exit 0
;;
*) echo "Invalid option $REPLY. Please select again."
;;
esac
done
}
# Cleanup Process for Outputs Folder
cleanup_folder "$HOST_DATA_PATH/outputs/" "-name '*' " "Outputs"
# Cleanup Process for restarts Folder
cleanup_folder "$HOST_DATA_PATH/restarts/" "-name '*' " "Restarts"
# File discovery
echo -e "\nLooking in the provided directory gives us:"
find_files() {
local path=$1
local name=$2
local regex=$3
local color=$4
local files=$(find "$path" -iname "$regex")
echo -e "${color}Found these $name files:${Color_Off}"
echo "$files" || echo "No $name files found."
}
find_files "$HOST_DATA_PATH" "hydrofabric" "*.gpkg" "$UGreen"
find_files "$HOST_DATA_PATH" "realization" "realization.json" "$UGreen"
# Detect Arch and Docker
echo -e "\nDetected ISA = $(uname -a)"
if docker --version ; then
echo "Docker found"
else
echo "Docker not found"
fi
if uname -a | grep arm64 || uname -a | grep aarch64 ; then
IMAGE_NAME=awiciroh/ciroh-ngen-image:latest
else
IMAGE_NAME=awiciroh/ciroh-ngen-image:latest-x86
fi
# ask the user if they want to run ngiab_dev ngiab_prod or public image
echo -e "${UYellow}Which image do you want to run? (type a number): ${Color_Off}"
options=("local dev image" "local production image" "latest public image")
select option in "${options[@]}"; do
case $option in
"local dev image")
IMAGE_NAME=ngiab_dev
break
;;
"local production image")
IMAGE_NAME=ngiab_prod
break
;;
"latest public image")
echo -e "${UYellow}Select an option (type a number): ${Color_Off}"
options=("use version saved locally" "download latest")
select option in "${options[@]}"; do
case $option in
"use version saved locally")
break
;;
"download latest")
docker pull $IMAGE_NAME
break
;;
*) echo "Invalid option $REPLY, 1 to continue with existing local image, 2 to update"
;;
esac
done
break
;;
*) echo "Invalid option $REPLY, 1 for local dev image, 2 for local production image, and 3 for latest public image"
;;
esac
done
# echo the current image name
echo -e "Current Docker image: ${BGreen}$IMAGE_NAME${Color_Off}\n"
echo -e "\nRunning NextGen docker container..."
echo -e "Mounting local host directory $HOST_DATA_PATH to /ngen/ngen/data within the container."
docker run --rm -it -v "$HOST_DATA_PATH:/ngen/ngen/data" "$IMAGE_NAME" /ngen/ngen/data/
# Final output count
Final_Outputs_Count=$(find "$HOST_DATA_PATH/outputs/" -type f | wc -l)
echo -e "$Final_Outputs_Count new outputs created."
echo -e "Any copied files can be found here: $HOST_DATA_PATH/outputs"
# visualize with Tethys
if [ $Final_Outputs_Count -gt 0 ]; then
ARG1="$HOST_DATA_PATH"
if ! "$TETHYS_SCRIPT" "$ARG1"; then
printf "Failed to visualize outputs in Tethys:"
fi
else
echo -e "No outputs to visualize."
fi
echo -e "Thank you for running NextGen In A Box: National Water Model! Have a nice day!"
exit 0