source: trunk/Mars/hawc/processing/DiskToDB/update-file-table2.sh@ 20106

Last change on this file since 20106 was 20106, checked in by maslowski, 3 years ago
Fixed some bugs.
File size: 4.6 KB
Line 
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,
6# header, calibration, auxiliary, callisto, star) to OUTPUT.
7#
8# Example:
9# bash update-file-table2.sh 1 /data/HE01/raw
10
11set -o errexit
12set -o errtrace
13set -o nounset
14set -o pipefail
15
16function ErrExit()
17{
18 echo "ERROR: Line `caller`: ${BASH_COMMAND}" >&2
19 exit 1
20}
21
22function StrgCExit()
23{
24 echo " "
25 echo "$0 was forcefully terminated" >&2
26 exit 1
27}
28
29trap ErrExit ERR
30trap StrgCExit INT
31
32# define variables
33OUTPUT="insert-OnDisk.sql"
34INSERT="INSERT INTO"
35readonly PROGRAM=$0
36
37usage()
38{
39 echo "usage: $PROGRAM [-hir] [-o outfile] [Telescope Dir]"
40 echo " -h display help"
41 echo " -i query ignores already existing rows"
42 echo " -r query replaces already existing rows(overwrites -i)"
43 echo " -o outfile name of the SQL query file"
44 exit 1;
45}
46
47# check for flags
48while getopts 'hiro:' flag
49do
50 case "${flag}" in
51 h) usage ;;
52 i) INSERT="INSERT IGNORE" ;;
53 r) INSERT="REPLACE INTO" ;;
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 "ERROR: Not enough arguments. Check -h for help!" >&2
64 exit 1
65fi
66
67TEL="${1}"
68DIR="${2}"
69
70
71# Initialize the Table
72echo \
73"\
74CREATE TABLE IF NOT EXISTS DataOnDisk
75(
76 Telescope tinyint UNSIGNED NOT NULL,
77 NIGHT int UNSIGNED NOT NULL,
78 RUNID mediumint UNSIGNED NOT NULL,
79 ISDRSFILE tinyint(1) NOT NULL,
80 header tinyint UNSIGNED,
81 calibration tinyint UNSIGNED,
82 auxiliary smallint UNSIGNED,
83 callisto tinyint UNSIGNED,
84 star tinyint UNSIGNED,
85 Updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
86 ON UPDATE CURRENT_TIMESTAMP,
87 PRIMARY KEY (Telescope, NIGHT, RUNID, ISDRSFILE)
88);
89
90CREATE TEMPORARY TABLE DataOnDisk_tmp
91(
92 Telescope tinyint UNSIGNED NOT NULL,
93 NIGHT int UNSIGNED NOT NULL,
94 RUNID mediumint UNSIGNED NOT NULL,
95 ISDRSFILE tinyint(1) NOT NULL,
96 header tinyint UNSIGNED,
97 calibration tinyint UNSIGNED,
98 auxiliary smallint UNSIGNED,
99 callisto tinyint UNSIGNED,
100 star tinyint UNSIGNED,
101 Updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
102 ON UPDATE CURRENT_TIMESTAMP,
103 PRIMARY KEY (Telescope, NIGHT, RUNID, ISDRSFILE)
104);
105
106INSERT INTO DataOnDisk_tmp (Telescope, NIGHT, RUNID, ISDRSFILE)
107VALUES
108"\ > ${OUTPUT}
109
110# Find query for .fits.fz files
111# Crop filenames from the form /nnnnnnnn_ddd.fits.fz$ to nnnnnnnn_ddd
112NRID=`find "${DIR}" -type f \
113 | grep -o "/[0-9]\{8\}_[0-9]\{3\}.fits.fz$" \
114 | cut -f2 -d "/" \
115 | cut -f1 -d "."`
116
117# Format to (Telescope, NIGHT, RUNID, ISDRSFILE),
118FZQUERY=`paste <(for i in ${NRID}; do echo ${TEL}; done) \
119 <(echo "${NRID}" | cut -f1 -d "_") \
120 <(echo "${NRID}" | cut -f2 -d "_") \
121 <(for i in ${NRID}; do echo "false"; done) \
122 --delimiters "," \
123 | sed 's/^/(/' \
124 | sed 's/$/),/'`
125
126# Find query for .drs.fits files
127# Crop filenames from the form nnnnnnnn_ddd.drs.fits to nnnnnnnn_ddd
128DNRID=`find "${DIR}" -type f \
129 | grep -o "/[0-9]\{8\}_[0-9]\{3\}.drs.fits$" \
130 | cut -f2 -d "/" \
131 | cut -f1 -d "."`
132
133# Format to (Telescope, NIGHT, RUNID, ISDRSFILE),
134DRSQUERY=`paste <(for i in ${DNRID}; do echo ${TEL}; done) \
135 <(echo "${DNRID}" | cut -f1 -d "_") \
136 <(echo "${DNRID}" | cut -f2 -d "_") \
137 <(for i in ${DNRID}; do echo "true"; done) \
138 --delimiters "," \
139 | sed 's/^/(/' \
140 | sed 's/$/),/'`
141
142# Write query to OUTPUT file
143echo "${FZQUERY}" >> ${OUTPUT}
144echo "${DRSQUERY}" >> ${OUTPUT}
145
146# Change last comma to semicolon
147sed -i '$s/,$/;/' "${OUTPUT}"
148
149# 1) If there is data in the database that is not on disk then set
150# header = NULL for that night
151# 2) Delete all entries from the database that are not on disk
152# 3) ${INSERT} all entries into the database that are on disk
153echo \
154"\
155CREATE TEMPORARY TABLE TableNotDisk
156(
157 Telescope tinyint UNSIGNED NOT NULL,
158 NIGHT int UNSIGNED NOT NULL,
159 RUNID mediumint UNSIGNED NOT NULL,
160 ISDRSFILE tinyint(1) NOT NULL,
161 header tinyint UNSIGNED,
162 calibration tinyint UNSIGNED,
163 auxiliary smallint UNSIGNED,
164 callisto tinyint UNSIGNED,
165 star tinyint UNSIGNED,
166 Updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
167 ON UPDATE CURRENT_TIMESTAMP,
168 PRIMARY KEY (Telescope, NIGHT, RUNID, ISDRSFILE)
169) SELECT * FROM DataOnDisk
170 WHERE (Telescope, NIGHT, RUNID, ISDRSFILE) NOT IN
171 (SELECT Telescope, NIGHT, RUNID, ISDRSFILE FROM DataOnDisk_tmp)
172 AND Telescope=${TEL};
173
174UPDATE DataOnDisk SET header = NULL WHERE NIGHT IN
175 (SELECT NIGHT FROM TableNotDisk)
176 AND Telescope=${TEL};
177
178DELETE FROM DataOnDisk
179 WHERE (Telescope, NIGHT, RUNID, ISDRSFILE) NOT IN
180 (SELECT Telescope, NIGHT, RUNID, ISDRSFILE FROM DataOnDisk_tmp)
181 AND Telescope=${TEL};
182
183${INSERT} DataOnDisk (SELECT * FROM DataOnDisk_tmp);
184"\ >> ${OUTPUT}
Note: See TracBrowser for help on using the repository browser.