1 | #!/bin/bash
|
---|
2 |
|
---|
3 | # Read fits files from DIR for a given telescope TEL and night NIGHT and write
|
---|
4 | # an SQL query to OUTPUT for uploading the raw header information.
|
---|
5 | #
|
---|
6 | # Example:
|
---|
7 | # bash extract-raw-header2.sh 1 /data/HE01/raw 20201111
|
---|
8 | #
|
---|
9 |
|
---|
10 | set -o errexit
|
---|
11 | set -o errtrace
|
---|
12 | set -o nounset
|
---|
13 | set -o pipefail
|
---|
14 |
|
---|
15 | function ErrExit()
|
---|
16 | {
|
---|
17 | echo "ERROR: Line `caller`: ${BASH_COMMAND}" >&2
|
---|
18 | exit 1
|
---|
19 | }
|
---|
20 |
|
---|
21 | function StrgCExit()
|
---|
22 | {
|
---|
23 | echo " "
|
---|
24 | echo "$0 was forcefully terminated" >&2
|
---|
25 | exit 1
|
---|
26 | }
|
---|
27 |
|
---|
28 | trap ErrExit ERR
|
---|
29 | trap StrgCExit INT
|
---|
30 |
|
---|
31 |
|
---|
32 | # define variables
|
---|
33 | OUTPUT=insert-raw.sql
|
---|
34 | readonly FITSDUMP="/home/frankm/Dateien/FACT++/build/fitsdump"
|
---|
35 | readonly PROGRAM=$0
|
---|
36 | INSERT="INSERT INTO"
|
---|
37 |
|
---|
38 | # Check if fitsdump exists
|
---|
39 | if [ ! -f "${FITSDUMP}" ]
|
---|
40 | then
|
---|
41 | echo "ERROR: fitsdump does not exists at ${FITSDUMP}. Please change in script ${PROGRAM}." >&2
|
---|
42 | exit 1
|
---|
43 | fi
|
---|
44 |
|
---|
45 | usage()
|
---|
46 | {
|
---|
47 | echo "usage: $PROGRAM [-hird] [-o outfile] [Telescope Directory Night]"
|
---|
48 | echo " -h display help"
|
---|
49 | echo " -i query ignores already existing rows"
|
---|
50 | echo " -r query replaces already existing rows (overwrites -i)"
|
---|
51 | echo " -d delete all entries for a given Night and Telescope"
|
---|
52 | echo " -o outfile name of the SQL query file"
|
---|
53 | exit 1;
|
---|
54 | }
|
---|
55 |
|
---|
56 | # Check for flags
|
---|
57 | while getopts 'hirdo:' flag
|
---|
58 | do
|
---|
59 | case "${flag}" in
|
---|
60 | h) usage ;;
|
---|
61 | i) INSERT="INSERT IGNORE" ;;
|
---|
62 | r) INSERT="REPLACE INTO" ;;
|
---|
63 | d) DELETE="true" ;;
|
---|
64 | o) OUTPUT="${OPTARG}" ;;
|
---|
65 | *) usage ;;
|
---|
66 | esac
|
---|
67 | done
|
---|
68 | shift $(($OPTIND-1))
|
---|
69 |
|
---|
70 | # Check if at least two arguments are provided
|
---|
71 | if [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ]
|
---|
72 | then
|
---|
73 | echo "ERROR: Not enough arguments. Check -h for help!" >&2
|
---|
74 | exit 1
|
---|
75 | fi
|
---|
76 |
|
---|
77 | TEL="${1}"
|
---|
78 | DIR="${2}"
|
---|
79 | NIGHT="${3}"
|
---|
80 |
|
---|
81 | readonly SUBDIR="${DIR}"/${NIGHT:0:4}/${NIGHT:4:2}/${NIGHT:6:2}
|
---|
82 |
|
---|
83 | # Keywords to grep from fitsdump
|
---|
84 | readonly TOGREP='NIGHT\|RUNID\|TSTART\|TSTOP\|STEP\|DATE\|NROI[^T]\|DAC[0-7]\|DNA[01][^0-9]\|FWVER[01][^0-9]\|DRSCALIB\|NTRG\|NTRG\|ZNAXIS2\|REFCLK\|ZRATIO\|RUNTYPE'
|
---|
85 | # Refference declarations for checking the header formatting
|
---|
86 | readonly SEARCHSHORT='DAC0\|DAC1\|DAC2\|DAC3\|DAC4\|DAC5\|DAC6\|DAC7\|`DATE`\|`DATE-END`\|`DATE-OBS`\|DNA0\|DNA1\|DRSCALIB\|FWVER0\|FWVER1\|NIGHT\|NROI\|NTRG\|NTRGEXT1\|NTRGEXT2\|NTRGLPE\|NTRGLPI\|NTRGMISC\|NTRGPED\|NTRGTIM\|REFCLK\|RUNID\|RUNTYPE\|TSTARTF\|TSTARTI\|TSTOPF\|TSTOPI\|ZNAXIS2\|ZRATIO'
|
---|
87 | readonly SEARCHLONG='DAC0\|DAC1\|DAC2\|DAC3\|DAC4\|DAC5\|DAC6\|DAC7\|`DATE`\|`DATE-END`\|`DATE-OBS`\|DNA0\|DNA1\|DRSCALIB\|DRSSTEP\|FWVER0\|FWVER1\|NIGHT\|NROI\|NTRG\|NTRGEXT1\|NTRGEXT2\|NTRGLPE\|NTRGLPI\|NTRGMISC\|NTRGPED\|NTRGTIM\|REFCLK\|RUNID\|RUNTYPE\|TSTARTF\|TSTARTI\|TSTOPF\|TSTOPI\|ZNAXIS2\|ZRATIO'
|
---|
88 |
|
---|
89 | readonly REFSHORT=`echo "${SEARCHSHORT}" | grep -o "${SEARCHSHORT}"`
|
---|
90 | readonly REFLONG=`echo "${SEARCHLONG}" | grep -o "${SEARCHLONG}"`
|
---|
91 | readonly CNTSHORT=34
|
---|
92 | readonly CNTLONG=35
|
---|
93 |
|
---|
94 | function InsertRaw()
|
---|
95 | {
|
---|
96 | local res="${1}"
|
---|
97 |
|
---|
98 | echo "${INSERT} RawData SET" >> ${OUTPUT}
|
---|
99 | echo Telescope=${TEL}, >> ${OUTPUT}
|
---|
100 | echo "${res}" >> ${OUTPUT}
|
---|
101 | echo ";" >> ${OUTPUT}
|
---|
102 | }
|
---|
103 |
|
---|
104 | function SetRawStatus()
|
---|
105 | {
|
---|
106 | local root="${1}"
|
---|
107 | local runid="${2}"
|
---|
108 | local stat="${3}"
|
---|
109 |
|
---|
110 | echo "${root} header not valid!"
|
---|
111 | echo "UPDATE DataOnDisk SET" >> ${OUTPUT}
|
---|
112 | echo "header=${stat} " >> ${OUTPUT}
|
---|
113 | echo "WHERE NIGHT = ${NIGHT}" >> ${OUTPUT}
|
---|
114 | echo "AND Telescope = ${TEL}" >> ${OUTPUT}
|
---|
115 | echo "AND RUNID = ${runid}" >> ${OUTPUT}
|
---|
116 | echo ";" >> ${OUTPUT}
|
---|
117 | }
|
---|
118 |
|
---|
119 | echo \
|
---|
120 | "\
|
---|
121 | CREATE TABLE IF NOT EXISTS RawData
|
---|
122 | (
|
---|
123 | Telescope tinyint UNSIGNED NOT NULL,
|
---|
124 | NIGHT int UNSIGNED NOT NULL,
|
---|
125 | RUNID mediumint UNSIGNED NOT NULL,
|
---|
126 | \`DATE-OBS\` datetime(6) NOT NULL,
|
---|
127 | \`DATE-END\` datetime(6) NOT NULL,
|
---|
128 | RUNTYPE tinyint UNSIGNED NOT NULL,
|
---|
129 | DRSSTEP tinyint UNSIGNED DEFAULT NULL,
|
---|
130 | NROI mediumint UNSIGNED NOT NULL,
|
---|
131 | ZNAXIS2 bigint UNSIGNED NOT NULL,
|
---|
132 | NTRG int UNSIGNED NOT NULL,
|
---|
133 | NTRGMISC int UNSIGNED NOT NULL,
|
---|
134 | NTRGPED int UNSIGNED NOT NULL,
|
---|
135 | REFCLK float NOT NULL,
|
---|
136 | ZRATIO float NOT NULL,
|
---|
137 | DAC0 mediumint UNSIGNED NOT NULL,
|
---|
138 | DAC1 mediumint UNSIGNED NOT NULL,
|
---|
139 | DAC2 mediumint UNSIGNED NOT NULL,
|
---|
140 | DAC3 mediumint UNSIGNED NOT NULL,
|
---|
141 | DAC4 mediumint UNSIGNED NOT NULL,
|
---|
142 | DAC5 mediumint UNSIGNED NOT NULL,
|
---|
143 | DAC6 mediumint UNSIGNED NOT NULL,
|
---|
144 | DAC7 mediumint UNSIGNED NOT NULL,
|
---|
145 | \`DATE\` timestamp NOT NULL,
|
---|
146 | DRSCALIB tinyint(1) NOT NULL,
|
---|
147 | NTRGEXT1 int UNSIGNED NOT NULL,
|
---|
148 | NTRGEXT2 int UNSIGNED NOT NULL,
|
---|
149 | NTRGLPE int UNSIGNED NOT NULL,
|
---|
150 | NTRGLPI int UNSIGNED NOT NULL,
|
---|
151 | NTRGTIM int UNSIGNED NOT NULL,
|
---|
152 | TSTARTF double NOT NULL,
|
---|
153 | TSTARTI mediumint UNSIGNED NOT NULL,
|
---|
154 | TSTOPF double NOT NULL,
|
---|
155 | TSTOPI mediumint UNSIGNED NOT NULL,
|
---|
156 | DNA0 bigint UNSIGNED NOT NULL,
|
---|
157 | DNA1 bigint UNSIGNED NOT NULL,
|
---|
158 | FWVER0 mediumint UNSIGNED NOT NULL,
|
---|
159 | FWVER1 mediumint UNSIGNED NOT NULL,
|
---|
160 | PRIMARY KEY (Telescope, NIGHT, RUNID),
|
---|
161 | KEY (RUNTYPE)
|
---|
162 | );
|
---|
163 | "\ > ${OUTPUT}
|
---|
164 |
|
---|
165 | if [ ${DELETE} = "true" ]
|
---|
166 | then
|
---|
167 | echo "DELETE FROM RawData" >> ${OUTPUT}
|
---|
168 | echo "WHERE Telescope = ${TEL}" >> ${OUTPUT}
|
---|
169 | echo "AND NIGHT = ${NIGHT};" >> ${OUTPUT}
|
---|
170 | echo "" >> ${OUTPUT}
|
---|
171 | fi
|
---|
172 |
|
---|
173 |
|
---|
174 | # Loop over all input arguments
|
---|
175 | for ROOT in "${SUBDIR}"/*
|
---|
176 | do
|
---|
177 |
|
---|
178 | # check if file extension is correct
|
---|
179 | if [ "${ROOT: -8}" == ".fits.fz" ]
|
---|
180 | then
|
---|
181 | echo "${ROOT}"
|
---|
182 | RUNID=`basename "${ROOT}" .fits.fz | cut -f2 -d "_"`
|
---|
183 | else
|
---|
184 | echo "${ROOT} has wrong file extension and will not be processed."
|
---|
185 | continue
|
---|
186 | fi
|
---|
187 | EXT=0
|
---|
188 |
|
---|
189 | # Extract the header from the fits file with fitsdump,
|
---|
190 | # the following chain properly formats the output,
|
---|
191 | # replaces T/F with true/false, replaces run-ytpes by numeric
|
---|
192 | # types, adds enclosures for SQL variable names where
|
---|
193 | # necessary, removes enclosures around hex-numbers and adds
|
---|
194 | # commas after all lines except the last one
|
---|
195 | RESULT=`${FITSDUMP} -h "${ROOT}" 2>&4 \
|
---|
196 | | grep "${TOGREP}" \
|
---|
197 | | grep -v CHECKSUM \
|
---|
198 | | cut -c4- \
|
---|
199 | | cut -f1 -d\/ \
|
---|
200 | | sed "s/'drs-pedestal'/1/g" \
|
---|
201 | | sed "s/'drs-gain'/2/g" \
|
---|
202 | | sed "s/'pedestal'/3/g" \
|
---|
203 | | sed "s/'data'/4/g" \
|
---|
204 | | sed "s/'custom'/0/g" \
|
---|
205 | | sed 's/\ T\ /true/g' \
|
---|
206 | | sed 's/\ F\ /false/g' \
|
---|
207 | | sed 's/^DATE-END/\`DATE-END\`/' \
|
---|
208 | | sed 's/^DATE-OBS/\`DATE-OBS\`/' \
|
---|
209 | | sed 's/^DATE[^-]/\`DATE\`/' \
|
---|
210 | | sed "s/^\(.*\)'\(0x[0-9a-f]*\)'\(.*\)$/\1\2\3/g" \
|
---|
211 | | sed '$!s/$/,/'` || EXT=1
|
---|
212 |
|
---|
213 | if [ $EXT -ne 0 ]
|
---|
214 | then
|
---|
215 | SetRawStatus "${ROOT}" ${RUNID} "1"
|
---|
216 | continue
|
---|
217 | fi
|
---|
218 |
|
---|
219 | # compare to refference header and count commas
|
---|
220 | CHECKSHORT=`echo ${RESULT} | grep -o "${SEARCHSHORT}"`
|
---|
221 | CHECKLONG=`echo ${RESULT} | grep -o "${SEARCHLONG}"`
|
---|
222 | CNTCHECK=`echo "${RESULT}" | grep -o ',' | wc -l`
|
---|
223 |
|
---|
224 | # If long or short CHECK are matching, write result to file
|
---|
225 | if [ "${CHECKLONG}" == "${REFLONG}" ] &&
|
---|
226 | [ "${CNTCHECK}" == "${CNTLONG}" ]
|
---|
227 | then
|
---|
228 | InsertRaw "${RESULT}"
|
---|
229 | SetRawStatus "${ROOT}" ${RUNID} "0"
|
---|
230 | elif [ "${CHECKSHORT}" == "${REFSHORT}" ] &&
|
---|
231 | [ "${CNTCHECK}" == "${CNTSHORT}" ]
|
---|
232 | then
|
---|
233 | InsertRaw "${RESULT}"
|
---|
234 | SetRawStatus "${ROOT}" ${RUNID} "0"
|
---|
235 | else
|
---|
236 | SetRawStatus "${ROOT}" ${RUNID} "2"
|
---|
237 | fi
|
---|
238 |
|
---|
239 | done
|
---|