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 |
|
---|
9 | set -o errexit
|
---|
10 | set -o errtrace
|
---|
11 | set -o nounset
|
---|
12 | set -o pipefail
|
---|
13 |
|
---|
14 | function ErrExit()
|
---|
15 | {
|
---|
16 | echo "ERROR: Line `caller`: ${BASH_COMMAND}" >&2
|
---|
17 | exit 1
|
---|
18 | }
|
---|
19 |
|
---|
20 | function StrgCExit()
|
---|
21 | {
|
---|
22 | echo " "
|
---|
23 | echo "$0 was forcefully terminated" >&2
|
---|
24 | exit 1
|
---|
25 | }
|
---|
26 |
|
---|
27 | trap ErrExit ERR
|
---|
28 | trap StrgCExit INT
|
---|
29 |
|
---|
30 | readonly CALLISTO=callisto
|
---|
31 | # The .success file in /data/star will be resetted
|
---|
32 | readonly STAR=star
|
---|
33 | # Directory to Mars environment (usually Mars/build)
|
---|
34 | readonly MARS="/home/hawc/Desktop/Mars/"
|
---|
35 | # Absolut path to macros
|
---|
36 | readonly MACROS="/home/hawc/Desktop/Mars-src/hawc"
|
---|
37 | readonly PROGRAM=$0
|
---|
38 | REPLACE=false
|
---|
39 |
|
---|
40 | usage()
|
---|
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
|
---|
49 | while getopts 'hr' flag
|
---|
50 | do
|
---|
51 | case "${flag}" in
|
---|
52 | h) usage ;;
|
---|
53 | r) REPLACE=true ;;
|
---|
54 | *) usage ;;
|
---|
55 | esac
|
---|
56 | done
|
---|
57 | shift $(($OPTIND-1))
|
---|
58 |
|
---|
59 | # Check if Mars exists
|
---|
60 | if [ ! -d "${MARS}" ]
|
---|
61 | then
|
---|
62 | echo "Mars does not exists at ${MARS}. Please change in \
|
---|
63 | script ${PROGRAM}."
|
---|
64 | exit 1
|
---|
65 | fi
|
---|
66 |
|
---|
67 | # Check if MACROS exists
|
---|
68 | if [ ! -d "${MACROS}" ]
|
---|
69 | then
|
---|
70 | echo "Macros directorey does not exists at ${MACROS}. Please change in \
|
---|
71 | script ${PROGRAM}."
|
---|
72 | exit 1
|
---|
73 | fi
|
---|
74 |
|
---|
75 | # Check if at least two arguments are provided
|
---|
76 | if [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] || [ -z "${4}" ]
|
---|
77 | then
|
---|
78 | echo "Not enough arguments. Check -h for help!"
|
---|
79 | exit 1
|
---|
80 | fi
|
---|
81 |
|
---|
82 | # File containing the access credentials for the database
|
---|
83 | CRED="${1}"
|
---|
84 | # Specify the telescope for which to run the script.
|
---|
85 | TEL="${2}"
|
---|
86 | # The data is expected to be found in /data/raw and data/callisto
|
---|
87 | DATA="${3}"
|
---|
88 | NIGHT="${4}"
|
---|
89 |
|
---|
90 | function 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
|
---|
109 | echo \
|
---|
110 | "\
|
---|
111 | SELECT
|
---|
112 | Calibration.NIGHT,
|
---|
113 | Calibration.RUNID
|
---|
114 | FROM
|
---|
115 | Calibration
|
---|
116 | INNER JOIN
|
---|
117 | DataOnDisk
|
---|
118 | ON
|
---|
119 | DataOnDisk.Telescope = Calibration.Telescope
|
---|
120 | AND
|
---|
121 | DataOnDisk.NIGHT = Calibration.NIGHT
|
---|
122 | AND
|
---|
123 | DataOnDisk.RUNID = Calibration.RUNID
|
---|
124 | WHERE
|
---|
125 | Calibration.Telescope = ${TEL}
|
---|
126 | AND
|
---|
127 | Calibration.NIGHT = ${NIGHT}
|
---|
128 | AND
|
---|
129 | DataOnDisk.callisto = 0
|
---|
130 | ORDER 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
|
---|