source: trunk/Mars/hawc/processing/DiskToDB/run-root2sql2.sh@ 20104

Last change on this file since 20104 was 20104, checked in by maslowski, 3 years ago
Added Telescope as command line parameter. And fixed some bugs.
File size: 4.0 KB
Line 
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
8set -o errexit
9set -o errtrace
10set -o nounset
11set -o pipefail
12
13function ErrExit()
14{
15 echo "ERROR: Line `caller`: ${BASH_COMMAND}" >&2
16 exit 1
17}
18
19function StrgCExit()
20{
21 echo " "
22 echo "$0 was forcefully terminated" >&2
23 exit 1
24}
25
26trap ErrExit ERR
27trap StrgCExit INT
28
29# Path to the executable
30readonly ROOT2SQL="/home/hawc/Desktop/FACT++/root2sql"
31readonly PROGRAM=${0}
32
33# Check if root2sql exists
34if [ ! -f "${ROOT2SQL}" ]
35then
36 echo "root2sql does not exists at ${ROOT2SQL}. Please change in script ${PROGRAM}."
37 exit 1
38fi
39
40usage()
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
50while getopts 'hIY' flag
51do
52 case "${flag}" in
53 h) usage ;;
54 I) SUFFIX=I;;
55 Y) SUFFIX=Y;;
56 *) usage ;;
57 esac
58done
59shift $(($OPTIND-1))
60
61# Check if at least two arguments are provided
62if [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] || [ -z "${4}" ] || [ -z "${5}" ]
63then
64 echo "Not enough arguments. Check -h for help!"
65 exit 1
66fi
67
68# File containing the access credentials for the database
69CRED="${1}"
70# Resource file to use (should match the input files)
71RESOURCE="${2}"
72# Specify the telescope for which to run the script.
73TEL="${3}"
74# Path where the data is stored... can be callisto or star
75BASEIN="${4}"
76# Night
77NIGHT="${5}"
78
79# The table to insert the data into (and create if not exists)
80if [ $SUFFIX = I ]
81then
82 TABLE=Star
83elif [ $SUFFIX = Y ]
84then
85 TABLE=Callisto
86else
87 echo "Wrong suffix."
88 exit 1
89fi
90
91function 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
116function 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
144echo \
145"\
146SELECT
147 Calibration.NIGHT,
148 Calibration.RUNID
149FROM
150 Calibration
151INNER JOIN
152 DataOnDisk
153ON
154 DataOnDisk.Telescope = Calibration.Telescope
155AND
156 DataOnDisk.NIGHT = Calibration.NIGHT
157AND
158 DataOnDisk.RUNID = Calibration.RUNID
159WHERE
160 Calibration.Telescope = ${TEL}
161AND
162 Calibration.NIGHT = ${NIGHT}
163AND
164 DataOnDisk.calibration = 0
165ORDER BY
166 Calibration.NIGHT, Calibration.RUNID;\
167"\
168 | mysql \
169 --defaults-file=${CRED} \
170 --skip-column-names \
171 --batch --raw \
172 --compress \
173 | \
174while read -r -a LINE
175do
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
209done
210
211PropagateStatus ${TEL} ${NIGHT} ${SUFFIX}
Note: See TracBrowser for help on using the repository browser.