1 | #!/bin/bash
|
---|
2 |
|
---|
3 | # Recursively finds all FAD_CONTROL_TEMPERATURE, BIAS_CONTROL_DYNAMIC_DATA,
|
---|
4 | # FTM_CONTROL_DATA, FTU_CONTROL_DATA files in DIR and their NIGHT and writes
|
---|
5 | # an SQL query with the columns:
|
---|
6 | # (Telescope,
|
---|
7 | # NIGHT,
|
---|
8 | # FAD_CONTROL_TEMPERATURE,
|
---|
9 | # BIAS_CONTROL_DYNAMIC_DATA,
|
---|
10 | # FTM_CONTROL_DATA,
|
---|
11 | # FTU_CONTROL_DATA)
|
---|
12 | # to OUTPUT.
|
---|
13 | #
|
---|
14 | # Example:
|
---|
15 | # bash update-aux-table.sh 1 /data/HE01/auxil/
|
---|
16 |
|
---|
17 | set -o errexit
|
---|
18 | set -o errtrace
|
---|
19 | set -o nounset
|
---|
20 | set -o pipefail
|
---|
21 |
|
---|
22 | function ErrExit()
|
---|
23 | {
|
---|
24 | echo "ERROR: Line `caller`: ${BASH_COMMAND}" >&2
|
---|
25 | exit 1
|
---|
26 | }
|
---|
27 |
|
---|
28 | function StrgCExit()
|
---|
29 | {
|
---|
30 | echo " "
|
---|
31 | echo "$0 was forcefully terminated" >&2
|
---|
32 | exit 1
|
---|
33 | }
|
---|
34 |
|
---|
35 | trap ErrExit ERR
|
---|
36 | trap StrgCExit INT
|
---|
37 |
|
---|
38 | # define variables
|
---|
39 | OUTPUT="insert-AuxOnDisk.sql"
|
---|
40 | INSERT="INSERT INTO"
|
---|
41 | readonly PROGRAM=$0
|
---|
42 |
|
---|
43 | usage()
|
---|
44 | {
|
---|
45 | echo "usage: $PROGRAM [-hir] [-o outfile] [Telescope Dir]"
|
---|
46 | echo " -h display help"
|
---|
47 | echo " -i query ignores already existing rows"
|
---|
48 | echo " -r query replaces already existing rows(overwrites -i)"
|
---|
49 | echo " -o outfile name of the SQL query file"
|
---|
50 | exit 1;
|
---|
51 | }
|
---|
52 |
|
---|
53 | # check for flags
|
---|
54 | while getopts 'hiro:' flag
|
---|
55 | do
|
---|
56 | case "${flag}" in
|
---|
57 | h) usage ;;
|
---|
58 | i) INSERT="INSERT IGNORE" ;;
|
---|
59 | r) INSERT="REPLACE INTO" ;;
|
---|
60 | o) OUTPUT="${OPTARG}" ;;
|
---|
61 | *) usage ;;
|
---|
62 | esac
|
---|
63 | done
|
---|
64 | shift $(($OPTIND-1))
|
---|
65 |
|
---|
66 | # Check if at least two arguments are provided
|
---|
67 | if [ -z "${1}" ] || [ -z "${2}" ]
|
---|
68 | then
|
---|
69 | echo "ERROR: Not enough arguments. Check -h for help!" >&2
|
---|
70 | exit 1
|
---|
71 | fi
|
---|
72 |
|
---|
73 | TEL="${1}"
|
---|
74 | DIR="${2}"
|
---|
75 |
|
---|
76 |
|
---|
77 | # Initialize the Table
|
---|
78 | echo \
|
---|
79 | "\
|
---|
80 | CREATE TABLE IF NOT EXISTS AuxOnDisk
|
---|
81 | (
|
---|
82 | Telescope tinyint UNSIGNED NOT NULL,
|
---|
83 | NIGHT int UNSIGNED NOT NULL,
|
---|
84 | FAD_CONTROL_TEMPERATURE tinyint UNSIGNED,
|
---|
85 | BIAS_CONTROL_DYNAMIC_DATA tinyint UNSIGNED,
|
---|
86 | FTM_CONTROL_DATA tinyint UNSIGNED,
|
---|
87 | FTU_CONTROL_DATA tinyint UNSIGNED,
|
---|
88 | Updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
---|
89 | ON UPDATE CURRENT_TIMESTAMP,
|
---|
90 | PRIMARY KEY (Telescope, NIGHT)
|
---|
91 | );
|
---|
92 | "\ > ${OUTPUT}
|
---|
93 |
|
---|
94 |
|
---|
95 | echo \
|
---|
96 | "\
|
---|
97 | CREATE TEMPORARY TABLE AuxOnDisk_tmp
|
---|
98 | (
|
---|
99 | Telescope tinyint UNSIGNED NOT NULL,
|
---|
100 | NIGHT int UNSIGNED NOT NULL,
|
---|
101 | FAD_CONTROL_TEMPERATURE tinyint UNSIGNED,
|
---|
102 | BIAS_CONTROL_DYNAMIC_DATA tinyint UNSIGNED,
|
---|
103 | FTM_CONTROL_DATA tinyint UNSIGNED,
|
---|
104 | FTU_CONTROL_DATA tinyint UNSIGNED,
|
---|
105 | Updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
---|
106 | ON UPDATE CURRENT_TIMESTAMP,
|
---|
107 | PRIMARY KEY (Telescope, NIGHT)
|
---|
108 | );
|
---|
109 | "\ >> ${OUTPUT}
|
---|
110 |
|
---|
111 | FAD="FAD_CONTROL_TEMPERATURE"
|
---|
112 | BIAS="BIAS_CONTROL_DYNAMIC_DATA"
|
---|
113 | FTM="FTM_CONTROL_DATA"
|
---|
114 | FTU="FTU_CONTROL_DATA"
|
---|
115 |
|
---|
116 | AUXIS=($FAD $BIAS $FTM $FTU)
|
---|
117 |
|
---|
118 | for AUX in ${AUXIS[@]}
|
---|
119 | do
|
---|
120 | echo ${AUX}
|
---|
121 |
|
---|
122 |
|
---|
123 | EXT=0
|
---|
124 | NIGHT=`find "${DIR}"/[0-9][0-9][0-9][0-9]/ -type f \
|
---|
125 | | grep -o "/[0-9]\{8\}.${AUX}.fits" \
|
---|
126 | | cut -f2 -d "/" \
|
---|
127 | | cut -f1 -d "."`|| EXT=1
|
---|
128 |
|
---|
129 | if [ ${EXT} = "0" ]
|
---|
130 | then
|
---|
131 |
|
---|
132 | echo \
|
---|
133 | "\
|
---|
134 | INSERT INTO AuxOnDisk_tmp (Telescope, NIGHT, ${AUX})
|
---|
135 | VALUES
|
---|
136 | "\ >> ${OUTPUT}
|
---|
137 |
|
---|
138 |
|
---|
139 | # Format to (Telescope, NIGHT, FAD_CONTROL_TEMPERATURE),
|
---|
140 | QUERY=`paste <(for i in ${NIGHT}; do echo ${TEL}; done) \
|
---|
141 | <(echo "${NIGHT}") \
|
---|
142 | <(for i in ${NIGHT}; do echo "0"; done) \
|
---|
143 | --delimiters "," \
|
---|
144 | | sed 's/^/(/' \
|
---|
145 | | sed 's/$/),/'`
|
---|
146 |
|
---|
147 | # Write query to OUTPUT file
|
---|
148 | echo "${QUERY}" >> ${OUTPUT}
|
---|
149 | # Change last comma to semicolon
|
---|
150 | sed -i '$s/,$//' "${OUTPUT}"
|
---|
151 |
|
---|
152 | echo "ON DUPLICATE KEY UPDATE ${AUX} = 0;" >> ${OUTPUT}
|
---|
153 |
|
---|
154 | # 1) If there is data in the database that is not on disk then set
|
---|
155 | # ${AUX} = NULL for that night
|
---|
156 | # 2) Delete all entries from the database that are not on disk
|
---|
157 | # 3) ${INSERT} all entries into the database that are on disk
|
---|
158 | echo \
|
---|
159 | "\
|
---|
160 | CREATE TEMPORARY TABLE TableNotDisk_${AUX}
|
---|
161 | (
|
---|
162 | Telescope tinyint UNSIGNED NOT NULL,
|
---|
163 | NIGHT int UNSIGNED NOT NULL,
|
---|
164 | FAD_CONTROL_TEMPERATURE tinyint UNSIGNED,
|
---|
165 | BIAS_CONTROL_DYNAMIC_DATA tinyint UNSIGNED,
|
---|
166 | FTM_CONTROL_DATA tinyint UNSIGNED,
|
---|
167 | FTU_CONTROL_DATA tinyint UNSIGNED,
|
---|
168 | Updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
---|
169 | ON UPDATE CURRENT_TIMESTAMP,
|
---|
170 | PRIMARY KEY (Telescope, NIGHT)
|
---|
171 | ) SELECT * FROM AuxOnDisk
|
---|
172 | WHERE (Telescope, NIGHT) NOT IN
|
---|
173 | (SELECT Telescope, NIGHT FROM AuxOnDisk_tmp)
|
---|
174 | AND Telescope=${TEL};
|
---|
175 |
|
---|
176 | UPDATE DataOnDisk SET auxiliary = NULL
|
---|
177 | WHERE DataOnDisk.NIGHT IN
|
---|
178 | (SELECT NIGHT FROM AuxOnDisk_tmp)
|
---|
179 | AND Telescope=${TEL};
|
---|
180 |
|
---|
181 | DELETE FROM AuxOnDisk
|
---|
182 | WHERE (Telescope, NIGHT) NOT IN
|
---|
183 | (SELECT Telescope, NIGHT FROM AuxOnDisk_tmp)
|
---|
184 | AND Telescope=${TEL};
|
---|
185 |
|
---|
186 | ${INSERT} AuxOnDisk (SELECT * FROM AuxOnDisk_tmp);
|
---|
187 | "\ >> ${OUTPUT}
|
---|
188 |
|
---|
189 | else
|
---|
190 | # 1) If there is data in the database that is not on disk then set
|
---|
191 | # ${AUX} = NULL for that night
|
---|
192 | # 2) Delete all entries from the database that are not on disk
|
---|
193 | # 3) ${INSERT} all entries into the database that are on disk
|
---|
194 | echo \
|
---|
195 | "\
|
---|
196 | CREATE TEMPORARY TABLE TableNotDisk_${AUX}
|
---|
197 | (
|
---|
198 | Telescope tinyint UNSIGNED NOT NULL,
|
---|
199 | NIGHT int UNSIGNED NOT NULL,
|
---|
200 | FAD_CONTROL_TEMPERATURE tinyint UNSIGNED,
|
---|
201 | BIAS_CONTROL_DYNAMIC_DATA tinyint UNSIGNED,
|
---|
202 | FTM_CONTROL_DATA tinyint UNSIGNED,
|
---|
203 | FTU_CONTROL_DATA tinyint UNSIGNED,
|
---|
204 | Updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
---|
205 | ON UPDATE CURRENT_TIMESTAMP,
|
---|
206 | PRIMARY KEY (Telescope, NIGHT)
|
---|
207 | ) SELECT * FROM AuxOnDisk
|
---|
208 | WHERE (Telescope, NIGHT) NOT IN
|
---|
209 | (SELECT Telescope, NIGHT FROM AuxOnDisk_tmp)
|
---|
210 | AND Telescope=${TEL};
|
---|
211 |
|
---|
212 | UPDATE DataOnDisk SET auxiliary = NULL
|
---|
213 | WHERE DataOnDisk.NIGHT IN
|
---|
214 | (SELECT NIGHT FROM AuxOnDisk_tmp)
|
---|
215 | AND Telescope=${TEL};
|
---|
216 |
|
---|
217 | DELETE FROM AuxOnDisk
|
---|
218 | WHERE (Telescope, NIGHT) NOT IN
|
---|
219 | (SELECT Telescope, NIGHT FROM AuxOnDisk_tmp)
|
---|
220 | AND Telescope=${TEL};
|
---|
221 |
|
---|
222 | ${INSERT} AuxOnDisk (SELECT * FROM AuxOnDisk_tmp);
|
---|
223 | "\ >> ${OUTPUT}
|
---|
224 |
|
---|
225 |
|
---|
226 | echo "No Auxiliary files."
|
---|
227 | continue;
|
---|
228 | fi
|
---|
229 |
|
---|
230 |
|
---|
231 |
|
---|
232 | done
|
---|