-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathrun_imi.sh
executable file
·224 lines (188 loc) · 7.62 KB
/
run_imi.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
#!/bin/bash
#PBS -l nodes=1,ncpus=1
#PBS -o "imi_output.log"
# This script will run the Integrated Methane Inversion (IMI) with GEOS-Chem.
# For documentation, see https://imi.readthedocs.io.
#
# Authors: Daniel Varon, Melissa Sulprizio, Lucas Estrada, Will Downs
##=======================================================================
## Import Shell functions
##=======================================================================
source src/utilities/common.sh
source src/components/setup_component/setup.sh
source src/components/template_component/template.sh
source src/components/statevector_component/statevector.sh
source src/components/preview_component/preview.sh
source src/components/spinup_component/spinup.sh
source src/components/jacobian_component/jacobian.sh
source src/components/inversion_component/inversion.sh
source src/components/posterior_component/posterior.sh
source src/components/kalman_component/kalman.sh
# trap and exit on errors
trap 'imi_failed $LINENO' ERR
start_time=$(date)
setup_start=$(date +%s)
##=======================================================================
## Parse config.yml file
##=======================================================================
printf "\n=== PARSING CONFIG FILE (run_imi.sh) ===\n"
# Check if user has specified a configuration file
if [[ $# == 1 ]] ; then
ConfigFile=$1
else
ConfigFile="config.yml"
fi
# Get configuration
source src/utilities/parse_yaml.sh
eval $(parse_yaml ${ConfigFile})
if ! "$isAWS"; then
# Load environment for compiling and running GEOS-Chem
if [ ! -f "${GEOSChemEnv}" ]; then
printf "\nGEOS-Chem environment file ${GEOSChemEnv} does not exist!"
printf "\nIMI $RunName Aborted\n"
exit 1
else
printf "\nLoading GEOS-Chem environment: ${GEOSChemEnv}\n"
source ${GEOSChemEnv}
fi
# Load the python environment
if [ ! -f "${PythonEnv}" ]; then
printf "\nPython environment file ${PythonEnv} does not exist!"
printf "\nIMI $RunName Aborted\n"
exit 1
else
printf "\nLoading Python environment: ${PythonEnv}\n"
source ${PythonEnv}
fi
# If scheduler is PBS, get the list of needed sites
if [[ "$SchedulerType" = "PBS" ]]; then
convert_sbatch_to_pbs
fi
fi
# Check all necessary config variables are present
python src/utilities/sanitize_input_yaml.py $ConfigFile || imi_failed
# Set path to IMI runs
RunDirs="${OutputPath}/${RunName}"
##=======================================================================
## Standard settings
##=======================================================================
# In safe mode check whether selected options will overwrite existing files
if "$SafeMode"; then
# Check if directories exist before creating them
if ([ -d "${RunDirs}/spinup_run" ] && "$SetupSpinupRun") || \
([ -d "${RunDirs}/jacobian_runs" ] && "$SetupJacobianRuns") || \
([ -d "${RunDirs}/inversion" ] && "$SetupInversion") || \
([ -d "${RunDirs}/posterior_run" ] && "$SetupPosteriorRun"); then
printf "\nERROR: Run directories in ${RunDirs}/"
printf "\n already exist. Please change RunName or change the"
printf "\n Setup* options to false in the IMI config file.\n"
printf "\n To proceed, and overwrite existing run directories, set"
printf "\n SafeMode in the config file to false.\n"
printf "\nIMI $RunName Aborted\n"
exit 1
fi
# Check if output from previous runs exists
if ([ -d "${RunDirs}/spinup_run" ] && "$DoSpinup") || \
([ -d "${RunDirs}/jacobian_runs" ] && "$DoJacobian") || \
([ -d "${RunDirs}/inversion" ] && "$DoInversion") || \
([ -d "${RunDirs}/posterior_run/OutputDir/" ] && "$DoPosterior"); then
printf "\nWARNING: Output files in ${RunDirs}/"
printf "\n may be overwritten. Please change RunName in the IMI"
printf "\n config file to avoid overwriting files.\n"
printf "\n To proceed, and overwrite existing output files, set"
printf "\n SafeMode in the config file to false.\n"
printf "\nIMI $RunName Aborted\n"
exit 1
fi
fi
# Path to inversion setup
InversionPath=$(pwd -P)
# add inversion path to python path
export PYTHONPATH=${PYTHONPATH}:${InversionPath}
##=======================================================================
## Download the TROPOMI data
##=======================================================================
# Download TROPOMI data from AWS. You will be charged if your ec2 instance is not in the eu-central-1 region.
mkdir -p -v ${RunDirs}
satelliteCache=${RunDirs}/data_satellite
if ("$isAWS" && [[ "$Species" == "CH4" ]]); then
{ # test if instance has access to TROPOMI bucket
stdout=`aws s3 ls s3://meeo-s5p`
} || { # catch
printf "\nError: Unable to connect to TROPOMI bucket. This is likely caused by misconfiguration of the ec2 instance iam role s3 permissions.\n"
printf "IMI $RunName Aborted.\n"
exit 1
}
mkdir -p -v $satelliteCache
printf "Downloading TROPOMI data from S3\n"
python src/utilities/download_TROPOMI.py $StartDate $EndDate $satelliteCache
printf "\nFinished TROPOMI download\n"
elif ("$isAWS" && [[ "$Species" != "CO2" ]]); then
printf "Non methane species are not currently supported on AWS."
else
# use existing tropomi data and create a symlink to it
if [[ ! -L $satelliteCache ]]; then
ln -s $DataPathObs $satelliteCache
fi
fi
# Check to make sure there are no duplicate TROPOMI files (e.g., two files with the same orbit number but a different processor version)
python src/utilities/test_TROPOMI_dir.py $satelliteCache
##=======================================================================
## Run the setup script
##=======================================================================
if "$RunSetup"; then
setup_imi
fi
setup_end=$(date +%s)
##=======================================================================
## Submit spinup simulation
##=======================================================================
if "$DoSpinup"; then
run_spinup
fi
##=======================================================================
## Run Kalman Filter Mode
##=======================================================================
if "$KalmanMode"; then
setup_kf
run_kf
fi
##=======================================================================
## Submit Jacobian simulation
##=======================================================================
if ("$DoJacobian" && ! "$KalmanMode"); then
run_jacobian
fi
##=======================================================================
## Process data and run inversion
##=======================================================================
if ("$DoInversion" && ! "$KalmanMode"); then
run_inversion
fi
##=======================================================================
## Submit posterior simulation and process the output
##=======================================================================
if ("$DoPosterior" && ! "$KalmanMode"); then
run_posterior
fi
printf "\n=== DONE RUNNING THE IMI ===\n"
# Run time
end_time=$(date)
printf "\nIMI started : %s" "$start_time"
printf "\nIMI ended : %s" "$end_time"
printf "\n"
if ! "$KalmanMode"; then
print_stats
fi
# Copy output log to run directory for storage
if [[ -f ${InversionPath}/imi_output.log ]]; then
cp "${InversionPath}/imi_output.log" "${RunDirs}/imi_output.log"
fi
# copy config file to run directory
cd $InversionPath
cp $ConfigFile "${RunDirs}/config_${RunName}.yml"
# Upload output to S3 if specified
if "$S3Upload"; then
python src/utilities/s3_upload.py $ConfigFile
fi
exit 0