source: trunk/Mars/hawc/processing/DiskToDB/write-calibration-query.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: 5.3 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
100AND NIGHT=${NIGHT}
101AND Telescope=${TEL};
102
103CREATE TEMPORARY TABLE Calibration_tmp
104(
105 WITH Table1 AS
106 (
107 SELECT
108 -- NIGHT and RUNID of the data file
109 R1.Telescope AS Telescope,
110 R1.NIGHT AS NIGHT,
111 R1.RUNID AS RUNID,
112 -- NIGHT and RUNID of the DRS file
113 R2.NIGHT AS DrsNight,
114 R2.RUNID AS DrsRunID,
115 -- Time difference between the two
116 LEAST(
117 ABS(UNIX_TIMESTAMP(R2.\`DATE-OBS\`)
118 - UNIX_TIMESTAMP(R1.\`DATE-OBS\`)),
119 ABS(UNIX_TIMESTAMP(R2.\`DATE-OBS\`)
120 - UNIX_TIMESTAMP(R1.\`DATE-END\`)),
121 ABS(UNIX_TIMESTAMP(R2.\`DATE-END\`)
122 - UNIX_TIMESTAMP(R1.\`DATE-OBS\`)),
123 ABS(UNIX_TIMESTAMP(R2.\`DATE-END\`)
124 - UNIX_TIMESTAMP(R1.\`DATE-END\`))
125 ) AS DeltaT,
126 -- Assign a row number to each combination of data-file and
127 -- drs-file after sorting them by time difference
128 ROW_NUMBER() OVER(PARTITION BY R1.NIGHT, R1.RUNID, R1.Telescope
129 ORDER BY
130 LEAST(
131 ABS(UNIX_TIMESTAMP(R2.\`DATE-OBS\`)
132 - UNIX_TIMESTAMP(R1.\`DATE-OBS\`)),
133 ABS(UNIX_TIMESTAMP(R2.\`DATE-OBS\`)
134 - UNIX_TIMESTAMP(R1.\`DATE-END\`)),
135 ABS(UNIX_TIMESTAMP(R2.\`DATE-END\`)
136 - UNIX_TIMESTAMP(R1.\`DATE-OBS\`)),
137 ABS(UNIX_TIMESTAMP(R2.\`DATE-END\`)
138 - UNIX_TIMESTAMP(R1.\`DATE-END\`))
139 )
140 ) AS RowNumber
141 FROM (
142 SELECT RawData.* FROM RawData
143 INNER JOIN DataOnDisk
144 ON DataOnDisk.Telescope = RawData.Telescope
145 AND DataOnDisk.NIGHT = RawData.NIGHT
146 AND DataOnDisk.RUNID = RawData.RUNID
147 AND DataOnDisk.ISDRSFILE = 0
148 WHERE DataOnDisk.header = 0
149 ) AS R1
150 -- Join the table with itself to get all possible combinations
151 -- of runs
152 CROSS JOIN (
153 SELECT RawData.* FROM RawData
154 INNER JOIN DataOnDisk
155 ON DataOnDisk.Telescope = RawData.Telescope
156 AND DataOnDisk.NIGHT = RawData.NIGHT
157 AND DataOnDisk.RUNID = RawData.RUNID
158 AND DataOnDisk.ISDRSFILE = 0
159 WHERE DataOnDisk.header = 0
160 ) AS R2
161 -- Join the DRS files with the table that tells you whether
162 -- they can be used if the table does not (yet) exists, this
163 -- can be omitted
164 WHERE
165 -- Of course claibration must be from the same telescope
166 R1.Telescope=R2.Telescope
167 AND
168 -- Select all combinations that have a data file in R1 and a
169 -- drs file (at step 1) in R2
170 ISNULL(R1.DRSSTEP) AND R2.DRSSTEP=1
171 AND
172 -- Only DRS files with 100 events and the correct ROI are
173 -- considered
174 R2.ZNAXIS2=1000 AND R1.NROI=R2.NROI
175 AND R1.NIGHT = ${NIGHT}
176 AND R2.NIGHT = ${NIGHT}
177 AND R1.Telescope = ${TEL}
178 -- AND
179 -- Check if they are valid
180 -- IFNULL(DoNotUse.WrongVoltage,false)!=true
181 -- This can be used to restrict the number of combinations
182 -- AND ABS(UNIX_TIMESTAMP(R2.\`DATE-OBS\`)-UNIX_TIMESTAMP(R1.\`DATE-OBS\`))<78*3600
183 ORDER BY
184 R1.Telescope,
185 R1.NIGHT,
186 R1.RUNID,
187 DeltaT
188 )
189 SELECT
190 Telescope,
191 NIGHT,
192 RUNID,
193 DrsNight,
194 DrsRunId,
195 DeltaT/3600e0 AS DeltaT
196 FROM
197 Table1
198 WHERE
199 -- Select only the rows with the smallest DeltaT
200 RowNumber = 1
201);
202
203-- Insert Calibration data into Database
204INSERT INTO Calibration SELECT * FROM Calibration_tmp;
205
206-- Set Status to 0 for all valid headers
207UPDATE DataOnDisk
208SET DataOnDisk.calibration = 0
209WHERE DataOnDisk.header = 0
210AND NIGHT=${NIGHT}
211AND Telescope=${TEL};
212
213-- Set Status to 2 if there was no drs file with step = 1 in the NIGHT
214UPDATE DataOnDisk
215SET DataOnDisk.calibration = 2
216WHERE EXISTS(
217 SELECT * FROM Calibration
218 WHERE NIGHT=${NIGHT}
219 AND Telescope=${TEL}
220 ) = 0;
221"\ >> ${OUTPUT}
Note: See TracBrowser for help on using the repository browser.