source: trunk/Mars/hawc/processing/DiskToDB/run-callisto2.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.7 KB
Line 
1#!/bin/bash
2#
3# Runs callisto macro on all files in directory DIR for a given telescope TEL
4# and night NIGHT and writes output to DIR/../callisto
5#
6# Example:
7# bash run-callisto2.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.callisto = ${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,
114 Calibration.DrsNight,
115 Calibration.DrsRunID
116FROM
117 Calibration
118INNER JOIN
119 DataOnDisk
120ON
121 DataOnDisk.Telescope = Calibration.Telescope
122AND
123 DataOnDisk.NIGHT = Calibration.NIGHT
124AND
125 DataOnDisk.RUNID = Calibration.RUNID
126WHERE
127 Calibration.Telescope = ${TEL}
128AND
129 Calibration.NIGHT = ${NIGHT}
130AND
131 DataOnDisk.calibration = 0
132ORDER BY
133 Calibration.NIGHT, Calibration.RUNID\
134"\
135| mysql \
136 --defaults-file=${CRED} \
137 --skip-column-names \
138 --batch --raw \
139 --compress \
140 | \
141 while read -r -a LINE
142 do
143 # Extract night/runid for data file and calibration file
144 DATNIGHT=${LINE[0]}
145 DATRUNID=${LINE[1]}
146 DRSNIGHT=${LINE[2]}
147 DRSRUNID=${LINE[3]}
148
149 # Formatting of the file paths and names
150 DATPATH=${DATNIGHT:0:4}/${DATNIGHT:4:2}/${DATNIGHT:6:2}
151 DRSPATH=${DRSNIGHT:0:4}/${DRSNIGHT:4:2}/${DRSNIGHT:6:2}
152
153 PREFIX=`printf ${DATNIGHT}_%03d ${DATRUNID}`
154
155 DATNAME=`printf ${DATNIGHT}_%03d.fits.fz ${DATRUNID}`
156 DRSNAME=`printf ${DRSNIGHT}_%03d.drs.fits ${DRSRUNID}`
157
158 OUT="${DATA}"/../${CALLISTO}/${DATPATH}
159
160 echo DAT=${DATNAME} [${DATPATH}]
161 echo DRS=${DRSNAME} [${DRSPATH}]
162
163
164 if [ ${REPLACE} = true ]
165 then
166 echo "deleting ${OUT}/.${PREFIX}.succsess !!!"
167 rm -f "${OUT}"/.${PREFIX}.success
168 fi
169
170 # Check if not yet successfully processed
171 # Removing this file can be used to trigger a re-processing
172 # the next time this script is executed
173 if [ ! -f "${OUT}/.${PREFIX}.success" ]
174 then
175
176 cd ${MARS}
177
178 mkdir -p "${OUT}"
179
180 # Trigger reprocessing of the process-fils in the star directory
181 rm -f "${DATA}/${STAR}/${DATPATH}/.${PREFIX}.*"
182
183 # Flag that a process is running
184 rm -f "${OUT}"/.${PREFIX}.done
185 rm -f "${OUT}"/.${PREFIX}.success
186
187 touch "${OUT}"/.${PREFIX}.running
188
189 echo ${DATA}
190
191 RC=0
192 # Execute the calibration and write output to log-file
193 root -b -q -l ${MACROS}/callisto.C'("'"${DATA}"/${DATPATH}/${DATNAME}'","'"${DATA}"/${DRSPATH}/${DRSNAME}'", 0 ,"'"${OUT}"'")' \
194 2>&1 | tee "${OUT}"/${PREFIX}.log || RC=1
195
196 # Remember exit status of callisto
197 echo RC=${RC} | tee -a "${OUT}"/${PREFIX}.log
198 echo ${RC} > "${OUT}"/.${PREFIX}.done
199 # Processing is finished
200 rm -f "${OUT}"/.${PREFIX}.running
201
202 # If processing was successfull write corresponding flag
203 if [ "${RC}" == "0" ]
204 then
205 touch "${OUT}"/.${PREFIX}.success
206 fi
207
208 cd -
209
210 echo "-----End of first iteration-----"
211 else
212 echo Skipped.
213 fi
214
215 done
Note: See TracBrowser for help on using the repository browser.