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

Last change on this file since 20093 was 20091, checked in by maslowski, 4 years ago
First commit of disk to database scripts.
File size: 4.3 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, ISPRECESSED) to OUTPUT.
6#
7# Example:
8# bash update-file-table2.sh 1 /data/HE01/raw
9
10set -o errexit
11set -o errtrace
12set -o nounset
13set -o pipefail
14
15function ErrExit()
16{
17 echo "ERROR: Line `caller`: ${BASH_COMMAND}" >&2
18 exit 1
19}
20
21function StrgCExit()
22{
23 echo " "
24 echo "$0 was forcefully terminated" >&2
25 exit 1
26}
27
28trap ErrExit ERR
29trap StrgCExit INT
30
31# define variables
32OUTPUT="insert-OnDisk.sql"
33INSERT="INSERT INTO"
34readonly PROGRAM=$0
35
36usage()
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
47while getopts 'hiro:' flag
48do
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
56done
57shift $(($OPTIND-1))
58
59# Check if at least two arguments are provided
60if [ -z "${1}" ] || [ -z "${2}" ]
61then
62 echo "ERROR: Not enough arguments. Check -h for help!" >&2
63 exit 1
64fi
65
66TEL="${1}"
67DIR="${2}"
68
69
70# Initialize the Table
71echo \
72"\
73CREATE 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
85CREATE 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
97INSERT INTO DataOnDisk_tmp (Telescope, NIGHT, RUNID, ISDRSFILE, STATUS)
98VALUES
99"\ > ${OUTPUT}
100
101# Find query for .fits.fz files
102# Crop filenames from the form /nnnnnnnn_ddd.fits.fz$ to nnnnnnnn_ddd
103NRID=`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),
109FZQUERY=`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
120DNRID=`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),
126DRSQUERY=`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
136echo "${FZQUERY}" >> ${OUTPUT}
137echo "${DRSQUERY}" >> ${OUTPUT}
138
139# Change last comma to semicolon
140sed -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
146echo \
147"\
148CREATE 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
162UPDATE DataOnDisk SET STATUS = 0 WHERE NIGHT IN
163 (SELECT NIGHT FROM TableNotDisk);
164
165DELETE 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}
Note: See TracBrowser for help on using the repository browser.