source: trunk/Mars/hawc/processing/DiskToDB/write-calibration-query.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: 5.1 KB
Line 
1#!/bin/bash
2#
3# Write SQL query for calibration for a given telescope TEL and night NIGHT to
4# OUTPUT
5#
6# Example:
7# bash write-calibration-query.sh 1 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
30# define variables
31OUTPUT=find-calibration-file2.sql
32readonly PROGRAM=$0
33INSERT="INSERT INTO"
34
35usage()
36{
37 echo "usage: $PROGRAM [-hir] [-o outfile] [Telescope Night]"
38 echo " -h display help"
39 echo " -i query ignores already existing rows"
40 echo " -r query replaces already existing rows(overwrites -i)"
41 echo " -d delete all entries for a given Night and Telescope"
42 echo " -o outfile name of the SQL query file"
43 exit 1;
44}
45
46# Check for flags
47while getopts 'hirdo:' flag
48do
49 case "${flag}" in
50 h) usage ;;
51 i) INSERT="INSERT IGNORE" ;;
52 r) INSERT="REPLACE INTO" ;;
53 d) DELETE="true" ;;
54 o) OUTPUT="${OPTARG}" ;;
55 *) usage ;;
56 esac
57done
58shift $(($OPTIND-1))
59
60# Check if at least two arguments are provided
61if [ -z "${1}" ] || [ -z "${2}" ]
62then
63 echo "Not enough arguments. Check -h for help!"
64 exit 1
65fi
66
67TEL="${1}"
68NIGHT="${2}"
69
70
71echo \
72"\
73-- You might want to change the table name
74CREATE TABLE IF NOT EXISTS Calibration
75(
76 Telescope tinyint UNSIGNED NOT NULL,
77 NIGHT int UNSIGNED NOT NULL,
78 RUNID mediumint UNSIGNED NOT NULL,
79 DrsNight double NOT NULL,
80 DrsRunId double NOT NULL,
81 DeltaT double UNSIGNED NOT NULL,
82 PRIMARY KEY (Telescope, NIGHT, RUNID)
83);
84"\ > ${OUTPUT}
85
86if [ ${DELETE} = "true" ]
87then
88 echo "DELETE FROM Calibration" >> ${OUTPUT}
89 echo "WHERE Telescope = ${TEL}" >> ${OUTPUT}
90 echo "AND NIGHT = ${NIGHT};" >> ${OUTPUT}
91 echo "" >> ${OUTPUT}
92fi
93
94echo \
95"\
96-- Set Status to 6 if there was an error is the extraction of RawData
97UPDATE DataOnDisk
98SET DataOnDisk.calibration = 6
99WHERE DataOnDisk.header != 0;
100
101CREATE TEMPORARY TABLE Calibration_tmp
102(
103 WITH Table1 AS
104 (
105 SELECT
106 -- NIGHT and RUNID of the data file
107 R1.Telescope AS Telescope,
108 R1.NIGHT AS NIGHT,
109 R1.RUNID AS RUNID,
110 -- NIGHT and RUNID of the DRS file
111 R2.NIGHT AS DrsNight,
112 R2.RUNID AS DrsRunID,
113 -- Time difference between the two
114 LEAST(
115 ABS(UNIX_TIMESTAMP(R2.\`DATE-OBS\`)
116 - UNIX_TIMESTAMP(R1.\`DATE-OBS\`)),
117 ABS(UNIX_TIMESTAMP(R2.\`DATE-OBS\`)
118 - UNIX_TIMESTAMP(R1.\`DATE-END\`)),
119 ABS(UNIX_TIMESTAMP(R2.\`DATE-END\`)
120 - UNIX_TIMESTAMP(R1.\`DATE-OBS\`)),
121 ABS(UNIX_TIMESTAMP(R2.\`DATE-END\`)
122 - UNIX_TIMESTAMP(R1.\`DATE-END\`))
123 ) AS DeltaT,
124 -- Assign a row number to each combination of data-file and
125 -- drs-file after sorting them by time difference
126 ROW_NUMBER() OVER(PARTITION BY R1.NIGHT, R1.RUNID, R1.Telescope
127 ORDER BY
128 LEAST(
129 ABS(UNIX_TIMESTAMP(R2.\`DATE-OBS\`)
130 - UNIX_TIMESTAMP(R1.\`DATE-OBS\`)),
131 ABS(UNIX_TIMESTAMP(R2.\`DATE-OBS\`)
132 - UNIX_TIMESTAMP(R1.\`DATE-END\`)),
133 ABS(UNIX_TIMESTAMP(R2.\`DATE-END\`)
134 - UNIX_TIMESTAMP(R1.\`DATE-OBS\`)),
135 ABS(UNIX_TIMESTAMP(R2.\`DATE-END\`)
136 - UNIX_TIMESTAMP(R1.\`DATE-END\`))
137 )
138 ) AS RowNumber
139 FROM (
140 SELECT RawData.* FROM RawData
141 INNER JOIN DataOnDisk
142 ON DataOnDisk.Telescope = RawData.Telescope
143 AND DataOnDisk.NIGHT = RawData.NIGHT
144 AND DataOnDisk.RUNID = RawData.RUNID
145 AND DataOnDisk.ISDRSFILE = 0
146 WHERE DataOnDisk.header = 0
147 ) AS R1
148 -- Join the table with itself to get all possible combinations
149 -- of runs
150 CROSS JOIN (
151 SELECT RawData.* FROM RawData
152 INNER JOIN DataOnDisk
153 ON DataOnDisk.Telescope = RawData.Telescope
154 AND DataOnDisk.NIGHT = RawData.NIGHT
155 AND DataOnDisk.RUNID = RawData.RUNID
156 AND DataOnDisk.ISDRSFILE = 0
157 WHERE DataOnDisk.header = 0
158 ) AS R2
159 -- Join the DRS files with the table that tells you whether
160 -- they can be used if the table does not (yet) exists, this
161 -- can be omitted
162 WHERE
163 -- Of course claibration must be from the same telescope
164 R1.Telescope=R2.Telescope
165 AND
166 -- Select all combinations that have a data file in R1 and a
167 -- drs file (at step 1) in R2
168 ISNULL(R1.DRSSTEP) AND R2.DRSSTEP=1
169 AND
170 -- Only DRS files with 100 events and the correct ROI are
171 -- considered
172 R2.ZNAXIS2=1000 AND R1.NROI=R2.NROI
173 AND R1.NIGHT = ${NIGHT}
174 AND R2.NIGHT = ${NIGHT}
175 AND R1.Telescope = ${TEL}
176 -- AND
177 -- Check if they are valid
178 -- IFNULL(DoNotUse.WrongVoltage,false)!=true
179 -- This can be used to restrict the number of combinations
180 -- AND ABS(UNIX_TIMESTAMP(R2.\`DATE-OBS\`)-UNIX_TIMESTAMP(R1.\`DATE-OBS\`))<78*3600
181 ORDER BY
182 R1.Telescope,
183 R1.NIGHT,
184 R1.RUNID,
185 DeltaT
186 )
187 SELECT
188 Telescope,
189 NIGHT,
190 RUNID,
191 DrsNight,
192 DrsRunId,
193 DeltaT/3600e0 AS DeltaT
194 FROM
195 Table1
196 WHERE
197 -- Select only the rows with the smallest DeltaT
198 RowNumber = 1
199);
200
201-- Insert Calibration data into Database
202INSERT INTO Calibration SELECT * FROM Calibration_tmp;
203
204-- Set Status to 0 for all valid headers
205UPDATE DataOnDisk
206SET DataOnDisk.calibration = 0
207WHERE DataOnDisk.header = 0;
208
209-- Set Status to 2 if there was no drs file with step = 1 in the NIGHT
210UPDATE DataOnDisk
211SET DataOnDisk.calibration = 2
212WHERE EXISTS(SELECT * FROM Calibration WHERE NIGHT=${NIGHT}) = 0;
213"\ >> ${OUTPUT}
Note: See TracBrowser for help on using the repository browser.