1 | #!/bin/bash
|
---|
2 | #
|
---|
3 | # Synchronizes the HAWC's Eye data on disk with the SQL database.
|
---|
4 | #
|
---|
5 | # The following scripts must be in the same directory as this script and
|
---|
6 | # the named variables must be eddited in those files.
|
---|
7 | # run-scripts.sh (this script)
|
---|
8 | # TEL
|
---|
9 | # DIR
|
---|
10 | # CRED
|
---|
11 | # CALLISTORC
|
---|
12 | # STARRC
|
---|
13 | # update-file-table2.sh
|
---|
14 | # extract-raw-header2.sh
|
---|
15 | # FITSDUMP
|
---|
16 | # write-calibration-query.sh
|
---|
17 | # extract-aux-data2.sh
|
---|
18 | # MARS
|
---|
19 | # MACROS
|
---|
20 | # run-callisto2.sh
|
---|
21 | # MARS
|
---|
22 | # MACROS
|
---|
23 | # run-star2.sh
|
---|
24 | # MARS
|
---|
25 | # MACROS
|
---|
26 | # run-root2sql2.sh
|
---|
27 | # ROOT2SQL
|
---|
28 | #
|
---|
29 | # Example:
|
---|
30 | # bash run-scripts.sh
|
---|
31 |
|
---|
32 | set -o errexit
|
---|
33 | set -o errtrace
|
---|
34 | set -o nounset
|
---|
35 | set -o pipefail
|
---|
36 |
|
---|
37 |
|
---|
38 | # Filename
|
---|
39 | readonly SCRPTN=`basename $0 .sh`
|
---|
40 |
|
---|
41 | # For lockfile
|
---|
42 | readonly LOCK="/tmp/${SCRPTN}.lock"
|
---|
43 |
|
---|
44 | function LockExit()
|
---|
45 | {
|
---|
46 | echo "ERROR: One instance of ${SCRPTN} already running" >&2
|
---|
47 | exit 1
|
---|
48 | }
|
---|
49 |
|
---|
50 | function DescExit()
|
---|
51 | {
|
---|
52 | echo "ERROR: Filedescriptor 200 already taken" >&2
|
---|
53 | exit 1
|
---|
54 | }
|
---|
55 |
|
---|
56 | function ErrExit()
|
---|
57 | {
|
---|
58 | echo "ERROR: Line `caller`: ${BASH_COMMAND}" >&2
|
---|
59 | exit 1
|
---|
60 | }
|
---|
61 |
|
---|
62 | function StrgCExit()
|
---|
63 | {
|
---|
64 | echo " "
|
---|
65 | echo "$0 was forcefully terminated" >&2
|
---|
66 | exit 1
|
---|
67 | }
|
---|
68 |
|
---|
69 | trap ErrExit ERR
|
---|
70 | trap StrgCExit INT
|
---|
71 |
|
---|
72 | exec 200>${LOCK} || DescExit
|
---|
73 | flock -n 200 || LockExit
|
---|
74 |
|
---|
75 | # For logfile
|
---|
76 | mkdir -p log
|
---|
77 | readonly DATE=`date +%Y%m%d_%H%M`
|
---|
78 | readonly LOG="./log/${SCRPTN}_${DATE}.log"
|
---|
79 | exec 3>&1 1>>${LOG}
|
---|
80 | exec 4>>${LOG}
|
---|
81 | # exec 3>&1
|
---|
82 | # exec 4>&1
|
---|
83 |
|
---|
84 | echo "Start processing data"
|
---|
85 |
|
---|
86 | function DeleteEntries()
|
---|
87 | {
|
---|
88 | # Delete entries form table ${3} for a given Telescope and Night
|
---|
89 | # Arguments: Credentials Telescope Table Night
|
---|
90 | mysql --defaults-file="${1}" \
|
---|
91 | --skip-column-names \
|
---|
92 | --batch \
|
---|
93 | --raw \
|
---|
94 | --compress \
|
---|
95 | -e "DELETE FROM ${3} WHERE Telescope = ${2} AND NIGHT = ${4};"
|
---|
96 | }
|
---|
97 |
|
---|
98 | function SetStatus()
|
---|
99 | {
|
---|
100 | # Query with process update
|
---|
101 | # Arguments: Credentials Telescope Status Night
|
---|
102 | mysql --defaults-file="${1}" \
|
---|
103 | --skip-column-names \
|
---|
104 | --batch \
|
---|
105 | --raw \
|
---|
106 | --compress \
|
---|
107 | -e "UPDATE DataOnDisk SET STATUS = ${3} WHERE NIGHT = ${4} \
|
---|
108 | AND Telescope = ${2};"
|
---|
109 | }
|
---|
110 |
|
---|
111 | function GetStatus()
|
---|
112 | {
|
---|
113 | # Return status of Night from database
|
---|
114 | # Arguments: Credentials Telescope Night
|
---|
115 | local STATUS=`mysql --defaults-file="${1}" \
|
---|
116 | --skip-column-names \
|
---|
117 | --batch \
|
---|
118 | --raw \
|
---|
119 | --compress \
|
---|
120 | -e "SELECT DISTINCT STATUS FROM DataOnDisk WHERE NIGHT = ${3} \
|
---|
121 | AND Telescope = ${2};"`
|
---|
122 |
|
---|
123 | if [ -z ${STATUS} ]
|
---|
124 | then
|
---|
125 | echo "ERROR: Status does not exist for telescope: ${2} and night: ${3}" >&2
|
---|
126 | exit 1
|
---|
127 | fi
|
---|
128 |
|
---|
129 | if [ `echo "${STATUS}" | wc -l` -ne "1" ]
|
---|
130 | then
|
---|
131 | echo "ERROR: Not all status values are the same for the night: ${3}!" >&2
|
---|
132 | exit 1
|
---|
133 | fi
|
---|
134 |
|
---|
135 | echo "${STATUS}"
|
---|
136 | }
|
---|
137 |
|
---|
138 | # Define variables
|
---|
139 | # Telescope for which to process
|
---|
140 | readonly TEL=1
|
---|
141 | # Directory in which the raw data is located
|
---|
142 | readonly DIR='/run/media/frankm/Neuer Datenträger/data/DATA/HE0'${TEL}
|
---|
143 | # Credentials for the SQL database
|
---|
144 | readonly CRED='../credentials-read-only.cnf'
|
---|
145 | # Credentials for root2sql for callisto
|
---|
146 | readonly CALLISTORC='../root2sql-callisto.rc'
|
---|
147 | # Credentials for root2sql for star
|
---|
148 | readonly STARRC='../root2sql-star.rc'
|
---|
149 | # SQL outputs
|
---|
150 | mkdir -p ./queries
|
---|
151 | readonly ONDISK='./queries/insert-OnDisk.sql'
|
---|
152 | readonly RAW='./queries/insert-raw.sql'
|
---|
153 | readonly CALIB='./queries/find-calibration2.sql'
|
---|
154 | readonly AUX='./queries/insert-aux.sql'
|
---|
155 |
|
---|
156 | # Create Database of existing files
|
---|
157 | # Create SQL query with:
|
---|
158 | # (Telescope, Night, RUNID, ISDRSFILE, ISPROCESSED).
|
---|
159 | echo "Updating file table"
|
---|
160 | bash ./update-file-table2.sh -i -o "${ONDISK}" "${TEL}" "${DIR}/raw"
|
---|
161 | # Update DataOnDisk tabel in database
|
---|
162 | echo "Uploading updated table"
|
---|
163 | mysql --defaults-file="${CRED}" < "${ONDISK}"
|
---|
164 |
|
---|
165 | # get list of NIGHTS with unprocessed files
|
---|
166 | echo "Getting nights to be processed"
|
---|
167 | NIGHTS=`mysql --defaults-file="${CRED}" \
|
---|
168 | --skip-column-names \
|
---|
169 | --batch \
|
---|
170 | --raw \
|
---|
171 | --compress \
|
---|
172 | -e "SELECT DISTINCT NIGHT FROM DataOnDisk WHERE STATUS != 5 \
|
---|
173 | AND Telescope = ${TEL};"`
|
---|
174 |
|
---|
175 | # Check if NIGHTS is empty and dont do anythin if so.
|
---|
176 | if [ -z "${NIGHTS}" ]
|
---|
177 | then
|
---|
178 | echo "Database is up to date. Nothing to do here."
|
---|
179 | exit 0
|
---|
180 | fi
|
---|
181 |
|
---|
182 | echo "Updating database"
|
---|
183 |
|
---|
184 | echo "Nights to process:"
|
---|
185 | echo "${NIGHTS}"
|
---|
186 | echo " "
|
---|
187 |
|
---|
188 |
|
---|
189 | # Execute scripts on all RunId for given Telescope and Night that has
|
---|
190 | # Loop over nights and upload to database for every loop
|
---|
191 | for NIGHT in ${NIGHTS}
|
---|
192 | do
|
---|
193 | STATUS=`GetStatus ${CRED} ${TEL} ${NIGHT}`
|
---|
194 | echo "Status = ${STATUS} for night = ${NIGHT}"
|
---|
195 | echo " "
|
---|
196 |
|
---|
197 | if [ ${STATUS} -lt "1" ]
|
---|
198 | then
|
---|
199 | # Get raw data headers
|
---|
200 | echo "== In Status 1 =="
|
---|
201 | bash ./extract-raw-header2.sh -d -o "${RAW}" \
|
---|
202 | ${TEL} \
|
---|
203 | "${DIR}"/raw \
|
---|
204 | ${NIGHT}
|
---|
205 | echo "Uploading raw headers for night: ${NIGHT}"
|
---|
206 | mysql --defaults-file="${CRED}" < "${RAW}"
|
---|
207 | SetStatus ${CRED} ${TEL} "1" ${NIGHT}
|
---|
208 | echo "Finished uploading raw headers for night: ${NIGHT}"
|
---|
209 | echo " "
|
---|
210 | fi
|
---|
211 |
|
---|
212 | if [ ${STATUS} -lt "2" ]
|
---|
213 | then
|
---|
214 | # Get calibration files
|
---|
215 | echo "== In Status 2 =="
|
---|
216 | bash ./write-calibration-query.sh -d -o "${CALIB}" \
|
---|
217 | ${TEL} \
|
---|
218 | ${NIGHT}
|
---|
219 | echo "Uploading calibration for night: ${NIGHT}"
|
---|
220 | mysql --defaults-file="${CRED}" < "${CALIB}"
|
---|
221 | SetStatus ${CRED} ${TEL} "2" ${NIGHT}
|
---|
222 | echo "Finished uploading calibration for night: ${NIGHT}"
|
---|
223 | echo " "
|
---|
224 | fi
|
---|
225 |
|
---|
226 | if [ ${STATUS} -lt "3" ]
|
---|
227 | then
|
---|
228 | # Get aux data
|
---|
229 | echo "== In Status 3 =="
|
---|
230 | bash ./extract-aux-data2.sh -d -o "${AUX}" \
|
---|
231 | ${CRED} \
|
---|
232 | ${TEL} \
|
---|
233 | "${DIR}"/auxil \
|
---|
234 | ${NIGHT}
|
---|
235 | echo "Uploading aux data for night: ${NIGHT}"
|
---|
236 | mysql --defaults-file="${CRED}" < "${AUX}"
|
---|
237 | SetStatus ${CRED} ${TEL} "3" ${NIGHT}
|
---|
238 | echo "Finished uploading aux data for night: ${NIGHT}"
|
---|
239 | echo " "
|
---|
240 | fi
|
---|
241 |
|
---|
242 | if [ ${STATUS} -lt "4" ]
|
---|
243 | then
|
---|
244 | # Get callisto data
|
---|
245 | echo "== In Status 4 =="
|
---|
246 | bash ./run-callisto2.sh -r \
|
---|
247 | "${CRED}" \
|
---|
248 | ${TEL} \
|
---|
249 | "${DIR}"/raw \
|
---|
250 | ${NIGHT}
|
---|
251 |
|
---|
252 | echo "Deleting callisto entries for night: ${NIGHT}"
|
---|
253 | DeleteEntries ${CRED} ${TEL} "Callisto" ${NIGHT}
|
---|
254 |
|
---|
255 | echo "Uploading callisto entries for night: ${NIGHT}"
|
---|
256 | bash ./run-root2sql2.sh -Y \
|
---|
257 | "${CRED}" \
|
---|
258 | "${CALLISTORC}" \
|
---|
259 | ${TEL} \
|
---|
260 | "${DIR}"/callisto \
|
---|
261 | ${NIGHT}
|
---|
262 | SetStatus ${CRED} ${TEL} "4" ${NIGHT}
|
---|
263 | echo "Finished uploading callisto entries for night: ${NIGHT}"
|
---|
264 | echo " "
|
---|
265 | fi
|
---|
266 |
|
---|
267 | if [ ${STATUS} -lt "5" ]
|
---|
268 | then
|
---|
269 | # Get star data
|
---|
270 | echo "== In Status 5 =="
|
---|
271 | bash ./run-star2.sh -r "${CRED}" ${TEL} "${DIR}"/raw ${NIGHT}
|
---|
272 |
|
---|
273 | echo "Deleting star entries for night: ${NIGHT}"
|
---|
274 | DeleteEntries ${CRED} ${TEL} "Star" ${NIGHT}
|
---|
275 |
|
---|
276 | echo "Uploading star entries for night: ${NIGHT}"
|
---|
277 | bash ./run-root2sql2.sh -I \
|
---|
278 | "${CRED}" \
|
---|
279 | "${STARRC}" \
|
---|
280 | ${TEL} \
|
---|
281 | "${DIR}"/star \
|
---|
282 | ${NIGHT}
|
---|
283 | SetStatus ${CRED} ${TEL} "5" ${NIGHT}
|
---|
284 | echo "Finished uploading star entries for night: ${NIGHT}"
|
---|
285 | echo " "
|
---|
286 | fi
|
---|
287 |
|
---|
288 | done
|
---|
289 |
|
---|
290 | echo " "
|
---|
291 | echo "Finished processing data"
|
---|