1 | #/bin/bash
|
---|
2 | #
|
---|
3 | # Uploads star or callisto data to SQL database
|
---|
4 | #
|
---|
5 | # Example:
|
---|
6 | # bash run-root2sql2.sh -I cred-file starrc 1 /data/HE01/star 20201111
|
---|
7 |
|
---|
8 | set -o errexit
|
---|
9 | set -o errtrace
|
---|
10 | set -o nounset
|
---|
11 | set -o pipefail
|
---|
12 |
|
---|
13 | function ErrExit()
|
---|
14 | {
|
---|
15 | echo "ERROR: Line `caller`: ${BASH_COMMAND}" >&2
|
---|
16 | exit 1
|
---|
17 | }
|
---|
18 |
|
---|
19 | function StrgCExit()
|
---|
20 | {
|
---|
21 | echo " "
|
---|
22 | echo "$0 was forcefully terminated" >&2
|
---|
23 | exit 1
|
---|
24 | }
|
---|
25 |
|
---|
26 | trap ErrExit ERR
|
---|
27 | trap StrgCExit INT
|
---|
28 |
|
---|
29 | # Path to the executable
|
---|
30 | readonly ROOT2SQL="/home/hawc/Desktop/FACT++/root2sql"
|
---|
31 | readonly PROGRAM=${0}
|
---|
32 |
|
---|
33 | # Check if root2sql exists
|
---|
34 | if [ ! -f "${ROOT2SQL}" ]
|
---|
35 | then
|
---|
36 | echo "root2sql does not exists at ${ROOT2SQL}. Please change in script ${PROGRAM}."
|
---|
37 | exit 1
|
---|
38 | fi
|
---|
39 |
|
---|
40 | usage()
|
---|
41 | {
|
---|
42 | echo "usage: $PROGRAM [-hIY] [Credentials Resource Telescope Dir Night]"
|
---|
43 | echo " -h display help"
|
---|
44 | echo " -I is Star data"
|
---|
45 | echo " -Y is Callisto data"
|
---|
46 | exit 1;
|
---|
47 | }
|
---|
48 |
|
---|
49 | # Check for flags
|
---|
50 | while getopts 'hIY' flag
|
---|
51 | do
|
---|
52 | case "${flag}" in
|
---|
53 | h) usage ;;
|
---|
54 | I) SUFFIX=I;;
|
---|
55 | Y) SUFFIX=Y;;
|
---|
56 | *) usage ;;
|
---|
57 | esac
|
---|
58 | done
|
---|
59 | shift $(($OPTIND-1))
|
---|
60 |
|
---|
61 | # Check if at least two arguments are provided
|
---|
62 | if [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] || [ -z "${4}" ] || [ -z "${5}" ]
|
---|
63 | then
|
---|
64 | echo "Not enough arguments. Check -h for help!"
|
---|
65 | exit 1
|
---|
66 | fi
|
---|
67 |
|
---|
68 | # File containing the access credentials for the database
|
---|
69 | CRED="${1}"
|
---|
70 | # Resource file to use (should match the input files)
|
---|
71 | RESOURCE="${2}"
|
---|
72 | # Specify the telescope for which to run the script.
|
---|
73 | TEL="${3}"
|
---|
74 | # Path where the data is stored... can be callisto or star
|
---|
75 | BASEIN="${4}"
|
---|
76 | # Night
|
---|
77 | NIGHT="${5}"
|
---|
78 |
|
---|
79 | # The table to insert the data into (and create if not exists)
|
---|
80 | if [ $SUFFIX = I ]
|
---|
81 | then
|
---|
82 | TABLE=Star
|
---|
83 | elif [ $SUFFIX = Y ]
|
---|
84 | then
|
---|
85 | TABLE=Callisto
|
---|
86 | else
|
---|
87 | echo "Wrong suffix."
|
---|
88 | exit 1
|
---|
89 | fi
|
---|
90 |
|
---|
91 | function UpdateStatus()
|
---|
92 | {
|
---|
93 | local tel="${1}"
|
---|
94 | local night="${2}"
|
---|
95 | local runid="${3}"
|
---|
96 | local suf="${4}"
|
---|
97 | local stat="${5}"
|
---|
98 |
|
---|
99 | if [ $suf = I ]
|
---|
100 | then
|
---|
101 | local col=star
|
---|
102 | elif [ $suf = Y ]
|
---|
103 | then
|
---|
104 | local col=callisto
|
---|
105 | fi
|
---|
106 |
|
---|
107 | mysql --defaults-file=${CRED} \
|
---|
108 | --compress \
|
---|
109 | -e "UPDATE DataOnDisk \
|
---|
110 | SET DataOnDisk.${col} = ${stat} \
|
---|
111 | WHERE DataOnDisk.Telescope = ${tel} \
|
---|
112 | AND DataOnDisk.NIGHT = ${night} \
|
---|
113 | AND DataOnDisk.RUNID = ${runid};"
|
---|
114 | }
|
---|
115 |
|
---|
116 | function PropagateStatus()
|
---|
117 | {
|
---|
118 | local tel="${1}"
|
---|
119 | local night="${2}"
|
---|
120 | local suf="${3}"
|
---|
121 |
|
---|
122 | if [ $suf = I ]
|
---|
123 | then
|
---|
124 | local col=star
|
---|
125 | elif [ $suf = Y ]
|
---|
126 | then
|
---|
127 | local col=callisto
|
---|
128 | fi
|
---|
129 |
|
---|
130 | # Set col to 3 if the file is a DRS run
|
---|
131 | mysql --defaults-file=${CRED} \
|
---|
132 | --compress \
|
---|
133 | -e "UPDATE DataOnDisk \
|
---|
134 | SET DataOnDisk.${col} = 3 \
|
---|
135 | WHERE DataOnDisk.Telescope = ${tel} \
|
---|
136 | AND DataOnDisk.NIGHT = ${night} \
|
---|
137 | AND DataOnDisk.calibration = 0 \
|
---|
138 | AND DataOnDisk.${col} IS NULL;"
|
---|
139 | }
|
---|
140 |
|
---|
141 | # Get all runs that can (and should) be calibrated
|
---|
142 | # Pipe the output to mysql and read the
|
---|
143 | # mysql output line-by-libe
|
---|
144 | echo \
|
---|
145 | "\
|
---|
146 | SELECT
|
---|
147 | Calibration.NIGHT,
|
---|
148 | Calibration.RUNID
|
---|
149 | FROM
|
---|
150 | Calibration
|
---|
151 | INNER JOIN
|
---|
152 | DataOnDisk
|
---|
153 | ON
|
---|
154 | DataOnDisk.Telescope = Calibration.Telescope
|
---|
155 | AND
|
---|
156 | DataOnDisk.NIGHT = Calibration.NIGHT
|
---|
157 | AND
|
---|
158 | DataOnDisk.RUNID = Calibration.RUNID
|
---|
159 | WHERE
|
---|
160 | Calibration.Telescope = ${TEL}
|
---|
161 | AND
|
---|
162 | Calibration.NIGHT = ${NIGHT}
|
---|
163 | AND
|
---|
164 | DataOnDisk.calibration = 0
|
---|
165 | ORDER BY
|
---|
166 | Calibration.NIGHT, Calibration.RUNID;\
|
---|
167 | "\
|
---|
168 | | mysql \
|
---|
169 | --defaults-file=${CRED} \
|
---|
170 | --skip-column-names \
|
---|
171 | --batch --raw \
|
---|
172 | --compress \
|
---|
173 | | \
|
---|
174 | while read -r -a LINE
|
---|
175 | do
|
---|
176 | # Get NIGHT and RUNID of all files
|
---|
177 | NIGHT=${LINE[0]}
|
---|
178 | RUNID=${LINE[1]}
|
---|
179 |
|
---|
180 | # Format file and path names
|
---|
181 | DIR="${BASEIN}"/${NIGHT:0:4}/${NIGHT:4:2}/${NIGHT:6:2}
|
---|
182 |
|
---|
183 | PREFIX=`printf ${NIGHT}_%03d ${RUNID}`
|
---|
184 |
|
---|
185 | echo ""
|
---|
186 | echo "${DIR}"/${PREFIX}
|
---|
187 |
|
---|
188 | # Check if processing was successfull
|
---|
189 | if [ -f "${DIR}"/.${PREFIX}.success ]
|
---|
190 | then
|
---|
191 |
|
---|
192 | ${ROOT2SQL} "${DIR}"/${PREFIX}_${SUFFIX}.root \
|
---|
193 | -C ${RESOURCE} \
|
---|
194 | --table=${TABLE} \
|
---|
195 | --const.Telescope=${TEL} \
|
---|
196 | --const.NIGHT=${NIGHT} \
|
---|
197 | --const.RUNID=${RUNID} \
|
---|
198 | 2>&1 | tee "${DIR}"/${PREFIX}-root2sql.log
|
---|
199 |
|
---|
200 | echo RC=${PIPESTATUS[0]} >> "${DIR}"/${PREFIX}-root2sql.log
|
---|
201 |
|
---|
202 | UpdateStatus ${TEL} ${NIGHT} ${RUNID} ${SUFFIX} 0
|
---|
203 |
|
---|
204 | else
|
---|
205 | echo Skipped.
|
---|
206 | UpdateStatus ${TEL} ${NIGHT} ${RUNID} ${SUFFIX} 2
|
---|
207 | fi
|
---|
208 |
|
---|
209 | done
|
---|
210 |
|
---|
211 | PropagateStatus ${TEL} ${NIGHT} ${SUFFIX}
|
---|