source: trunk/Mars/hawc/processing/DiskToDB/run-scripts.sh@ 20095

Last change on this file since 20095 was 20093, checked in by maslowski, 4 years ago
Fixed error handeling regarding fitsdump, star and callisto. Also Fixed some typos.
File size: 6.4 KB
Line 
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
32set -o errexit
33set -o errtrace
34set -o nounset
35set -o pipefail
36
37
38# Filename
39readonly SCRPTN=`basename $0 .sh`
40
41# For lockfile
42readonly LOCK="/tmp/${SCRPTN}.lock"
43
44function LockExit()
45{
46 echo "ERROR: One instance of ${SCRPTN} already running" >&2
47 exit 1
48}
49
50function DescExit()
51{
52 echo "ERROR: Filedescriptor 200 already taken" >&2
53 exit 1
54}
55
56function ErrExit()
57{
58 echo "ERROR: Line `caller`: ${BASH_COMMAND}" >&2
59 exit 1
60}
61
62function StrgCExit()
63{
64 echo " "
65 echo "$0 was forcefully terminated" >&2
66 exit 1
67}
68
69trap ErrExit ERR
70trap StrgCExit INT
71
72exec 200>${LOCK} || DescExit
73flock -n 200 || LockExit
74
75# For logfile
76mkdir -p log
77readonly DATE=`date +%Y%m%d_%H%M`
78readonly LOG="./log/${SCRPTN}_${DATE}.log"
79exec 3>&1 1>>${LOG}
80exec 4>>${LOG}
81# exec 3>&1
82# exec 4>&1
83
84echo "Start processing data"
85
86function 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
98function 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
111function 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
140readonly TEL=1
141# Directory in which the raw data is located
142readonly DIR='/run/media/frankm/Neuer Datenträger/data/DATA/HE0'${TEL}
143# Credentials for the SQL database
144readonly CRED='../credentials-read-only.cnf'
145# Credentials for root2sql for callisto
146readonly CALLISTORC='../root2sql-callisto.rc'
147# Credentials for root2sql for star
148readonly STARRC='../root2sql-star.rc'
149# SQL outputs
150mkdir -p ./queries
151readonly ONDISK='./queries/insert-OnDisk.sql'
152readonly RAW='./queries/insert-raw.sql'
153readonly CALIB='./queries/find-calibration2.sql'
154readonly AUX='./queries/insert-aux.sql'
155
156# Create Database of existing files
157# Create SQL query with:
158# (Telescope, Night, RUNID, ISDRSFILE, ISPROCESSED).
159echo "Updating file table"
160bash ./update-file-table2.sh -i -o "${ONDISK}" "${TEL}" "${DIR}/raw"
161# Update DataOnDisk tabel in database
162echo "Uploading updated table"
163mysql --defaults-file="${CRED}" < "${ONDISK}"
164
165# get list of NIGHTS with unprocessed files
166echo "Getting nights to be processed"
167NIGHTS=`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.
176if [ -z "${NIGHTS}" ]
177then
178 echo "Database is up to date. Nothing to do here."
179 exit 0
180fi
181
182echo "Updating database"
183
184echo "Nights to process:"
185echo "${NIGHTS}"
186echo " "
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
191for NIGHT in ${NIGHTS}
192do
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
288done
289
290echo " "
291echo "Finished processing data"
Note: See TracBrowser for help on using the repository browser.