source: trunk/Mars/hawc/processing/DiskToDB/run-star2.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.3 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/hawc/Desktop/Mars/"
35# Absolut path to macros
36readonly MACROS="/home/hawc/Desktop/Mars-src/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 Calibration.NIGHT,
113 Calibration.RUNID
114FROM
115 Calibration
116INNER JOIN
117 DataOnDisk
118ON
119 DataOnDisk.Telescope = Calibration.Telescope
120AND
121 DataOnDisk.NIGHT = Calibration.NIGHT
122AND
123 DataOnDisk.RUNID = Calibration.RUNID
124WHERE
125 Calibration.Telescope = ${TEL}
126AND
127 Calibration.NIGHT = ${NIGHT}
128AND
129 DataOnDisk.callisto = 0
130ORDER BY
131 Calibration.NIGHT, Calibration.RUNID\
132"\
133 | mysql \
134 --defaults-file=${CRED} \
135 --skip-column-names \
136 --batch --raw \
137 --compress \
138 | \
139 while read -r -a LINE
140 do
141 # Get NIGHT and RUNID of all files
142 NIGHT=${LINE[0]}
143 RUNID=${LINE[1]}
144
145 # Formatting of the file paths and names
146 DATPATH=${NIGHT:0:4}/${NIGHT:4:2}/${NIGHT:6:2}
147
148 PREFIX=`printf ${NIGHT}_%03d ${RUNID}`
149
150 DATNAME=`printf ${PREFIX}_Y.root ${RUNID}`
151
152 OUT="${DATA}"/../${STAR}/${DATPATH}
153 IN="${DATA}"/../${CALLISTO}/${DATPATH}
154
155 echo ${DATNAME}
156 echo ${IN} " => " ${OUT}
157
158 if [ ${REPLACE} = true ]
159 then
160 echo "deleting ${OUT}/.${PREFIX}.succsess !!!"
161 rm -f "${OUT}"/.${PREFIX}.success
162 fi
163
164 # Check if not yet successfully processed but sucessfully calibrated
165 if [[ -f "${IN}/.${PREFIX}.success" && ! -f "${IN}/.${PREFIX}.running" && ! -f "${OUT}/.${PREFIX}.success" ]]
166 then
167
168 cd ${MARS}
169
170 mkdir -p "${OUT}"
171
172 # Flag that a process is running
173 rm -f "${OUT}"/.${PREFIX}.done
174 rm -f "${OUT}"/.${PREFIX}.success
175
176 touch "${OUT}"/.${PREFIX}.running
177
178 RC=0
179 # set +o pipefail
180 # Execute image parameter calculation and write log-file
181 root -b -q -l ${MACROS}/star.C'("'"${IN}"/${DATNAME}'","'"${OUT}"'")' \
182 2>&1 | tee "${OUT}"/${PREFIX}.log || RC=1
183
184 # RC=$?
185 # set -o pipefail
186
187 # Remember exit status of star
188 # RC=${PIPESTATUS[0]}
189
190 echo RC=${RC} | tee -a "${OUT}"/${PREFIX}.log
191 echo ${RC} > "${OUT}"/.${PREFIX}.done
192
193 # Flag that processing is finished
194 rm -f "${OUT}"/.${PREFIX}.running
195
196 # If processing was successfull write coresponding flag
197 if [ "${RC}" == "0" ]
198 then
199 touch "${OUT}"/.${PREFIX}.success
200 fi
201
202 cd -
203
204 else
205 echo Skipped.
206 fi
207
208 done
Note: See TracBrowser for help on using the repository browser.