1 | #/bin/bash
|
---|
2 |
|
---|
3 | # Specify the telescope for which to run the script. Replace '1'
|
---|
4 | # by $1 if the telesocpe number is supplied as an option to the script
|
---|
5 | TELESCOPE=1
|
---|
6 |
|
---|
7 | # This is the directory in which to serach for files
|
---|
8 | DIR="/data/raw"
|
---|
9 | # This is the path to the fitsdump executable
|
---|
10 | FITSDUMP=~/FACT++/build/fitsdump
|
---|
11 | # File to which the queries are written
|
---|
12 | OUTPUT="insert-raw.sql"
|
---|
13 |
|
---|
14 | echo \
|
---|
15 | "\
|
---|
16 | CREATE TABLE IF NOT EXISTS RawData
|
---|
17 | (
|
---|
18 | Telescope tinyint UNSIGNED NOT NULL,
|
---|
19 | NIGHT int UNSIGNED NOT NULL,
|
---|
20 | RUNID mediumint UNSIGNED NOT NULL,
|
---|
21 | DATE-OBS datetime(6) NOT NULL,
|
---|
22 | DATE-END datetime(6) NOT NULL,
|
---|
23 | RUNTYPE tinyint UNSIGNED NOT NULL,
|
---|
24 | DRSSTEP tinyint UNSIGNED DEFAULT NULL,
|
---|
25 | NROI mediumint UNSIGNED NOT NULL,
|
---|
26 | ZNAXIS2 bigint UNSIGNED NOT NULL,
|
---|
27 | NTRG int UNSIGNED NOT NULL,
|
---|
28 | NTRGMISC int UNSIGNED NOT NULL,
|
---|
29 | NTRGPED int UNSIGNED NOT NULL,
|
---|
30 | REFCLK float NOT NULL,
|
---|
31 | ZRATIO float NOT NULL,
|
---|
32 | DAC0 mediumint UNSIGNED NOT NULL,
|
---|
33 | DAC1 mediumint UNSIGNED NOT NULL,
|
---|
34 | DAC2 mediumint UNSIGNED NOT NULL,
|
---|
35 | DAC3 mediumint UNSIGNED NOT NULL,
|
---|
36 | DAC4 mediumint UNSIGNED NOT NULL,
|
---|
37 | DAC5 mediumint UNSIGNED NOT NULL,
|
---|
38 | DAC6 mediumint UNSIGNED NOT NULL,
|
---|
39 | DAC7 mediumint UNSIGNED NOT NULL,
|
---|
40 | DATE timestamp NOT NULL,
|
---|
41 | DRSCALIB tinyint(1) NOT NULL,
|
---|
42 | NTRGEXT1 int UNSIGNED NOT NULL,
|
---|
43 | NTRGEXT2 int UNSIGNED NOT NULL,
|
---|
44 | NTRGLPE int UNSIGNED NOT NULL,
|
---|
45 | NTRGLPI int UNSIGNED NOT NULL,
|
---|
46 | NTRGTIM int UNSIGNED NOT NULL,
|
---|
47 | TSTARTF double NOT NULL,
|
---|
48 | TSTARTI mediumint UNSIGNED NOT NULL,
|
---|
49 | TSTOPF double NOT NULL,
|
---|
50 | TSTOPI mediumint UNSIGNED NOT NULL,
|
---|
51 | DNA0 bigint UNSIGNED NOT NULL,
|
---|
52 | DNA1 bigint UNSIGNED NOT NULL,
|
---|
53 | FWVER0 mediumint UNSIGNED NOT NULL,
|
---|
54 | FWVER1 mediumint UNSIGNED NOT NULL,
|
---|
55 | PRIMARY KEY (Telescope, NIGHT, RUNID),
|
---|
56 | KEY (RUNTYPE)
|
---|
57 | );
|
---|
58 | "\ > ${OUTPUT}
|
---|
59 |
|
---|
60 |
|
---|
61 | # Loop over all tokens returned by the given bash extension
|
---|
62 | for SUBDIR in ${DIR}/[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]
|
---|
63 | do
|
---|
64 |
|
---|
65 | # Extract year, month and day from directory name
|
---|
66 | YEAR=`echo ${SUBDIR} | cut -f6 -d\/`
|
---|
67 | MONTH=`echo ${SUBDIR} | cut -f7 -d\/`
|
---|
68 | DAY=`echo ${SUBDIR} | cut -f8 -d\/`
|
---|
69 |
|
---|
70 | # Loop over all files in this subdirectory
|
---|
71 | for ROOT in ${SUBDIR}/${YEAR}${MONTH}${DAY}*.fits.fz
|
---|
72 | do
|
---|
73 | FILE=`echo ${ROOT} | cut -f2 -d_`
|
---|
74 | echo ${YEAR} ${MONTH} ${DAY} ${FILE} ${ROOT}
|
---|
75 |
|
---|
76 | # Extact the header from the fits file with fitsdump, the following chain
|
---|
77 | # properly formats the output, replaces T/F with true/false, replaces
|
---|
78 | # run-ytpes by numeric types, adds enclosures for SQL variable names where
|
---|
79 | # necessary, removes enclosures around hex-numbers and adds commas
|
---|
80 | # after all lines except the last one
|
---|
81 | RESULT=`${FITSDUMP} -h ${ROOT} \
|
---|
82 | | grep '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' \
|
---|
83 | | grep -v CHECKSUM \
|
---|
84 | | cut -c4- \
|
---|
85 | | cut -f1 -d\/ \
|
---|
86 | | sed "s/'drs-pedestal'/1/g" \
|
---|
87 | | sed "s/'drs-gain'/2/g" \
|
---|
88 | | sed "s/'pedestal'/3/g" \
|
---|
89 | | sed "s/'data'/4/g" \
|
---|
90 | | sed "s/'custom'/0/g" \
|
---|
91 | | sed 's/\ T\ /true/g' \
|
---|
92 | | sed 's/\ F\ /false/g' \
|
---|
93 | | sed 's/^DATE-END/`DATE-END`/' \
|
---|
94 | | sed 's/^DATE-OBS/`DATE-OBS`/' \
|
---|
95 | | sed 's/^DATE[^-]/`DATE`/' \
|
---|
96 | | sed "s/^\(.*\)'\(0x[0-9a-f]*\)'\(.*\)$/\1\2\3/g" \
|
---|
97 | | sed '$!s/$/,/' >> query.txt`
|
---|
98 |
|
---|
99 | # count the number of lines in the result
|
---|
100 | CNT=`echo ${RESULT} | wc -l`
|
---|
101 |
|
---|
102 | echo "== $CNT =="
|
---|
103 |
|
---|
104 | # If the result is not empty write the corresponding insert query to the file
|
---|
105 | if [ $CNT -ne 0 ]
|
---|
106 | then
|
---|
107 | echo "INSERT INTO RawData SET" >> ${OUTPUT}
|
---|
108 | echo Telescope=${TELESCOPE}, >> ${OUTPUT}
|
---|
109 | echo ${RESULT} >> ${OUTPUT}
|
---|
110 | echo ";" >> ${OUTPUT}
|
---|
111 | fi
|
---|
112 |
|
---|
113 | done
|
---|
114 |
|
---|
115 | done
|
---|