1 | #!/bin/bash
|
---|
2 |
|
---|
3 | # Recursively finds all .fits.fz and .drs.fits files in DIR and extract
|
---|
4 | # NIGHT, RUNID and if the file is a DRS file and writes an SQL query with the
|
---|
5 | # columns: (Telescope, NIGHT, RUNID, ISDRSFILE, ISPRECESSED) to OUTPUT.
|
---|
6 | #
|
---|
7 | # Example:
|
---|
8 | # bash update-file-table2.sh 1 /data/HE01/raw
|
---|
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 | # define variables
|
---|
32 | OUTPUT="insert-OnDisk.sql"
|
---|
33 | INSERT="INSERT INTO"
|
---|
34 | readonly PROGRAM=$0
|
---|
35 |
|
---|
36 | usage()
|
---|
37 | {
|
---|
38 | echo "usage: $PROGRAM [-hir] [-o outfile] [Telescope Dir]"
|
---|
39 | echo " -h display help"
|
---|
40 | echo " -i query ignores already existing rows"
|
---|
41 | echo " -r query replaces already existing rows(overwrites -i)"
|
---|
42 | echo " -o outfile name of the SQL query file"
|
---|
43 | exit 1;
|
---|
44 | }
|
---|
45 |
|
---|
46 | # check for flags
|
---|
47 | while getopts 'hiro:' flag
|
---|
48 | do
|
---|
49 | case "${flag}" in
|
---|
50 | h) usage ;;
|
---|
51 | i) INSERT="INSERT IGNORE" ;;
|
---|
52 | r) INSERT="REPLACE INTO" ;;
|
---|
53 | o) OUTPUT="${OPTARG}" ;;
|
---|
54 | *) usage ;;
|
---|
55 | esac
|
---|
56 | done
|
---|
57 | shift $(($OPTIND-1))
|
---|
58 |
|
---|
59 | # Check if at least two arguments are provided
|
---|
60 | if [ -z "${1}" ] || [ -z "${2}" ]
|
---|
61 | then
|
---|
62 | echo "ERROR: Not enough arguments. Check -h for help!" >&2
|
---|
63 | exit 1
|
---|
64 | fi
|
---|
65 |
|
---|
66 | TEL="${1}"
|
---|
67 | DIR="${2}"
|
---|
68 |
|
---|
69 |
|
---|
70 | # Initialize the Table
|
---|
71 | echo \
|
---|
72 | "\
|
---|
73 | CREATE TABLE IF NOT EXISTS DataOnDisk
|
---|
74 | (
|
---|
75 | Telescope tinyint UNSIGNED NOT NULL,
|
---|
76 | NIGHT int UNSIGNED NOT NULL,
|
---|
77 | RUNID mediumint UNSIGNED NOT NULL,
|
---|
78 | ISDRSFILE tinyint(1) NOT NULL,
|
---|
79 | STATUS tinyint UNSIGNED NOT NULL,
|
---|
80 | Updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
---|
81 | ON UPDATE CURRENT_TIMESTAMP,
|
---|
82 | PRIMARY KEY (Telescope, NIGHT, RUNID, ISDRSFILE)
|
---|
83 | );
|
---|
84 |
|
---|
85 | CREATE TEMPORARY TABLE DataOnDisk_tmp
|
---|
86 | (
|
---|
87 | Telescope tinyint UNSIGNED NOT NULL,
|
---|
88 | NIGHT int UNSIGNED NOT NULL,
|
---|
89 | RUNID mediumint UNSIGNED NOT NULL,
|
---|
90 | ISDRSFILE tinyint(1) NOT NULL,
|
---|
91 | STATUS tinyint UNSIGNED NOT NULL,
|
---|
92 | Updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
---|
93 | ON UPDATE CURRENT_TIMESTAMP,
|
---|
94 | PRIMARY KEY (Telescope, NIGHT, RUNID, ISDRSFILE)
|
---|
95 | );
|
---|
96 |
|
---|
97 | INSERT INTO DataOnDisk_tmp (Telescope, NIGHT, RUNID, ISDRSFILE, STATUS)
|
---|
98 | VALUES
|
---|
99 | "\ > ${OUTPUT}
|
---|
100 |
|
---|
101 | # Find query for .fits.fz files
|
---|
102 | # Crop filenames from the form /nnnnnnnn_ddd.fits.fz$ to nnnnnnnn_ddd
|
---|
103 | NRID=`find "${DIR}" -type f \
|
---|
104 | | grep -o "/[0-9]\{8\}_[0-9]\{3\}.fits.fz$" \
|
---|
105 | | cut -f2 -d "/" \
|
---|
106 | | cut -f1 -d "."`
|
---|
107 |
|
---|
108 | # Format to (Telescope, NIGHT, RUNID, ISDRSFILE, ISPROCESSED),
|
---|
109 | FZQUERY=`paste <(for i in ${NRID}; do echo ${TEL}; done) \
|
---|
110 | <(echo "${NRID}" | cut -f1 -d "_") \
|
---|
111 | <(echo "${NRID}" | cut -f2 -d "_") \
|
---|
112 | <(for i in ${NRID}; do echo "false"; done) \
|
---|
113 | <(for i in ${NRID}; do echo "0"; done) \
|
---|
114 | --delimiters "," \
|
---|
115 | | sed 's/^/(/' \
|
---|
116 | | sed 's/$/),/'`
|
---|
117 |
|
---|
118 | # Find query for .drs.fits files
|
---|
119 | # Crop filenames from the form nnnnnnnn_ddd.drs.fits to nnnnnnnn_ddd
|
---|
120 | DNRID=`find "${DIR}" -type f \
|
---|
121 | | grep -o "/[0-9]\{8\}_[0-9]\{3\}.drs.fits$" \
|
---|
122 | | cut -f2 -d "/" \
|
---|
123 | | cut -f1 -d "."`
|
---|
124 |
|
---|
125 | # Format to (Telescope, NIGHT, RUNID, ISDRSFILE, ISPROCESSED),
|
---|
126 | DRSQUERY=`paste <(for i in ${DNRID}; do echo ${TEL}; done) \
|
---|
127 | <(echo "${DNRID}" | cut -f1 -d "_") \
|
---|
128 | <(echo "${DNRID}" | cut -f2 -d "_") \
|
---|
129 | <(for i in ${DNRID}; do echo "true"; done) \
|
---|
130 | <(for i in ${DNRID}; do echo "0"; done) \
|
---|
131 | --delimiters "," \
|
---|
132 | | sed 's/^/(/' \
|
---|
133 | | sed 's/$/),/'`
|
---|
134 |
|
---|
135 | # Write query to OUTPUT file
|
---|
136 | echo "${FZQUERY}" >> ${OUTPUT}
|
---|
137 | echo "${DRSQUERY}" >> ${OUTPUT}
|
---|
138 |
|
---|
139 | # Change last comma to semicolon
|
---|
140 | sed -i '$s/,$/;/' "${OUTPUT}"
|
---|
141 |
|
---|
142 | # 1) If there is data in the database that is not on disk then set status = 0
|
---|
143 | # for that night
|
---|
144 | # 2) Delete all entries from the database that are not on disk
|
---|
145 | # 3) ${INSERT} all entries into the database that are on disk
|
---|
146 | echo \
|
---|
147 | "\
|
---|
148 | CREATE TEMPORARY TABLE TableNotDisk
|
---|
149 | (
|
---|
150 | Telescope tinyint UNSIGNED NOT NULL,
|
---|
151 | NIGHT int UNSIGNED NOT NULL,
|
---|
152 | RUNID mediumint UNSIGNED NOT NULL,
|
---|
153 | ISDRSFILE tinyint(1) NOT NULL,
|
---|
154 | STATUS tinyint UNSIGNED NOT NULL,
|
---|
155 | Updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
---|
156 | ON UPDATE CURRENT_TIMESTAMP,
|
---|
157 | PRIMARY KEY (Telescope, NIGHT, RUNID, ISDRSFILE)
|
---|
158 | ) SELECT * FROM DataOnDisk
|
---|
159 | WHERE (Telescope, NIGHT, RUNID, ISDRSFILE) NOT IN
|
---|
160 | (SELECT Telescope, NIGHT, RUNID, ISDRSFILE FROM DataOnDisk_tmp);
|
---|
161 |
|
---|
162 | UPDATE DataOnDisk SET STATUS = 0 WHERE NIGHT IN
|
---|
163 | (SELECT NIGHT FROM TableNotDisk);
|
---|
164 |
|
---|
165 | DELETE FROM DataOnDisk
|
---|
166 | WHERE (Telescope, NIGHT, RUNID, ISDRSFILE) NOT IN
|
---|
167 | (SELECT Telescope, NIGHT, RUNID, ISDRSFILE FROM DataOnDisk_tmp);
|
---|
168 |
|
---|
169 | ${INSERT} DataOnDisk (SELECT * FROM DataOnDisk_tmp);
|
---|
170 | "\ >> ${OUTPUT}
|
---|