source: trunk/Mars/hawc/processing/DiskToDB/run-star2.sh@ 20103

Last change on this file since 20103 was 20103, checked in by maslowski, 3 years ago
Added better error handeling and tracking in the databas.
File size: 4.0 KB
Line 
1#!/bin/bash
2#
3# Run star macro on directory DIR for a given telescope TEL and night NIGHT
4# and write output to DIR/../star
5#
6# Example:
7# bash run-star2.sh cred-file 1 /data/HE01/raw 20201111
8
9set -o errexit
10set -o errtrace
11set -o nounset
12set -o pipefail
13
14function ErrExit()
15{
16 echo "ERROR: Line `caller`: ${BASH_COMMAND}" >&2
17 exit 1
18}
19
20function StrgCExit()
21{
22 echo " "
23 echo "$0 was forcefully terminated" >&2
24 exit 1
25}
26
27trap ErrExit ERR
28trap StrgCExit INT
29
30readonly CALLISTO=callisto
31# The .success file in /data/star will be resetted
32readonly STAR=star
33# Directory to Mars environment (usually Mars/build)
34readonly MARS="/home/frankm/Dateien/Mars-6.24.00/build4/"
35# Absolut path to macros
36readonly MACROS="/home/frankm/Dateien/Mars-6.24.00/hawc"
37readonly PROGRAM=$0
38REPLACE=false
39
40usage()
41{
42 echo "usage: $PROGRAM [-hr] [Credentials Telescope Dir Night]"
43 echo " -h display help"
44 echo " -r replace data"
45 exit 1;
46}
47
48# Check for flags
49while getopts 'hr' flag
50do
51 case "${flag}" in
52 h) usage ;;
53 r) REPLACE=true ;;
54 *) usage ;;
55 esac
56done
57shift $(($OPTIND-1))
58
59# Check if Mars exists
60if [ ! -d "${MARS}" ]
61then
62 echo "Mars does not exists at ${MARS}. Please change in \
63 script ${PROGRAM}."
64 exit 1
65fi
66
67# Check if MACROS exists
68if [ ! -d "${MACROS}" ]
69then
70 echo "Macros directorey does not exists at ${MACROS}. Please change in \
71 script ${PROGRAM}."
72 exit 1
73fi
74
75# Check if at least two arguments are provided
76if [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] || [ -z "${4}" ]
77then
78 echo "Not enough arguments. Check -h for help!"
79 exit 1
80fi
81
82# File containing the access credentials for the database
83CRED="${1}"
84# Specify the telescope for which to run the script.
85TEL="${2}"
86# The data is expected to be found in /data/raw and data/callisto
87DATA="${3}"
88NIGHT="${4}"
89
90function UpdateStatus()
91{
92 local tel="${1}"
93 local night="${2}"
94 local runid="${3}"
95 local stat="${4}"
96
97 mysql --defaults-file=${CRED} \
98 --compress \
99 -e "UPDATE DataOnDisk \
100 SET DataOnDisk.star = ${stat} \
101 WHERE DataOnDisk.Telescope = ${tel} \
102 AND DataOnDisk.NIGHT = ${night} \
103 AND DataOnDisk.RUNID = ${runid};"
104}
105
106# Get all runs that can (and should) be calibrated
107# Pipe the output to mysql and read the
108# mysql output line-by-libe
109echo \
110"\
111SELECT
112 NIGHT, RUNID
113FROM
114 Calibration
115WHERE
116 Telescope=${TEL}
117AND
118 NIGHT=${NIGHT}
119ORDER BY
120 NIGHT, RUNID\
121"\
122 | mysql \
123 --defaults-file=${CRED} \
124 --skip-column-names \
125 --batch --raw \
126 --compress \
127 | \
128 while read -r -a LINE
129 do
130 # Get NIGHT and RUNID of all files
131 NIGHT=${LINE[0]}
132 RUNID=${LINE[1]}
133
134 # Formatting of the file paths and names
135 DATPATH=${NIGHT:0:4}/${NIGHT:4:2}/${NIGHT:6:2}
136
137 PREFIX=`printf ${NIGHT}_%03d ${RUNID}`
138
139 DATNAME=`printf ${PREFIX}_Y.root ${RUNID}`
140
141 OUT="${DATA}"/../${STAR}/${DATPATH}
142 IN="${DATA}"/../${CALLISTO}/${DATPATH}
143
144 echo ${DATNAME}
145 echo ${IN} " => " ${OUT}
146
147 if [ ${REPLACE} = true ]
148 then
149 echo "deleting ${OUT}/.${PREFIX}.succsess !!!"
150 rm -f "${OUT}"/.${PREFIX}.success
151 fi
152
153 # Check if not yet successfully processed but sucessfully calibrated
154 if [[ -f "${IN}/.${PREFIX}.success" && ! -f "${IN}/.${PREFIX}.running" && ! -f "${OUT}/.${PREFIX}.success" ]]
155 then
156
157 cd ${MARS}
158
159 mkdir -p "${OUT}"
160
161 # Flag that a process is running
162 rm -f "${OUT}"/.${PREFIX}.done
163 rm -f "${OUT}"/.${PREFIX}.success
164
165 touch "${OUT}"/.${PREFIX}.running
166
167 # Execute image parameter calculation and write log-file
168 root -b -q -l ${MACROS}/star.C'("'"${IN}"/${DATNAME}'","'"${OUT}"'")' \
169 2>&1 | tee "${OUT}"/${PREFIX}.log
170
171 # Remember exit status of callisto
172 RC=${PIPESTATUS[0]}
173
174 echo RC=${RC} | tee -a "${OUT}"/${PREFIX}.log
175 echo ${RC} > "${OUT}"/.${PREFIX}.done
176
177 # Flag that processing is finished
178 rm -f "${OUT}"/.${PREFIX}.running
179
180 # If processing was successfull write coresponding flag
181 if [ "${RC}" == "0" ]
182 then
183 touch "${OUT}"/.${PREFIX}.success
184 else
185 UpdateStatus ${TEL} ${NIGHT} ${DATRUNID} 1
186 fi
187
188 cd -
189
190 else
191 echo Skipped.
192 fi
193
194 done
Note: See TracBrowser for help on using the repository browser.