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/frankm/Dateien/Mars-6.24.00/build4/"
|
---|
35 | # Absolut path to macros
|
---|
36 | readonly MACROS="/home/frankm/Dateien/Mars-6.24.00/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 | NIGHT, RUNID
|
---|
113 | FROM
|
---|
114 | Calibration
|
---|
115 | WHERE
|
---|
116 | Telescope=${TEL}
|
---|
117 | AND
|
---|
118 | NIGHT=${NIGHT}
|
---|
119 | ORDER 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
|
---|