1 | #/bin/bash
|
---|
2 |
|
---|
3 | # This is the directory in which to serach for files
|
---|
4 | DIR="/data/raw"
|
---|
5 | # This is the path to the fitsdump executable
|
---|
6 | FITSDUMP=~/FACT++/build/fitsdump
|
---|
7 | # File to which the queries are written
|
---|
8 | OUTPUT="insert-queries.sql"
|
---|
9 |
|
---|
10 | # Remove an existing file with queries
|
---|
11 | rm -f ${OUTPUT}
|
---|
12 |
|
---|
13 | # Loop over all tokens returned by the given bash extension
|
---|
14 | for SUBDIR in ${DIR}/[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]
|
---|
15 | do
|
---|
16 |
|
---|
17 | # Extract year, month and day from directory name
|
---|
18 | YEAR=`echo ${SUBDIR} | cut -f6 -d\/`
|
---|
19 | MONTH=`echo ${SUBDIR} | cut -f7 -d\/`
|
---|
20 | DAY=`echo ${SUBDIR} | cut -f8 -d\/`
|
---|
21 |
|
---|
22 | # Loop over all files in this subdirectory
|
---|
23 | for ROOT in ${SUBDIR}/${YEAR}${MONTH}${DAY}*.fits.fz
|
---|
24 | do
|
---|
25 | FILE=`echo ${ROOT} | cut -f2 -d_`
|
---|
26 | echo ${YEAR} ${MONTH} ${DAY} ${FILE} ${ROOT}
|
---|
27 |
|
---|
28 | # Extact the header from the fits file with fitsdump, the following chain
|
---|
29 | # properly formats the output, replaces T/F with true/false, replaces
|
---|
30 | # run-ytpes by numeric types, adds enclosures for SQL variable names where
|
---|
31 | # necessary, removes enclosures around hex-numbers and adds commas
|
---|
32 | # after all lines except the last one
|
---|
33 | RESULT=`${FITSDUMP} -h ${ROOT} \
|
---|
34 | | 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' \
|
---|
35 | | grep -v CHECKSUM \
|
---|
36 | | cut -c4- \
|
---|
37 | | cut -f1 -d\/ \
|
---|
38 | | sed "s/'drs-pedestal'/1/g" \
|
---|
39 | | sed "s/'drs-gain'/2/g" \
|
---|
40 | | sed "s/'pedestal'/3/g" \
|
---|
41 | | sed "s/'data'/4/g" \
|
---|
42 | | sed "s/'custom'/0/g" \
|
---|
43 | | sed 's/\ T\ /true/g' \
|
---|
44 | | sed 's/\ F\ /false/g' \
|
---|
45 | | sed 's/^DATE-END/`DATE-END`/' \
|
---|
46 | | sed 's/^DATE-OBS/`DATE-OBS`/' \
|
---|
47 | | sed 's/^DATE[^-]/`DATE`/' \
|
---|
48 | | sed "s/^\(.*\)'\(0x[0-9a-f]*\)'\(.*\)$/\1\2\3/g" \
|
---|
49 | | sed '$!s/$/,/' >> query.txt`
|
---|
50 |
|
---|
51 | # count the number of lines in the result
|
---|
52 | CNT=`echo ${RESULT} | wc -l`
|
---|
53 |
|
---|
54 | echo "== $CNT =="
|
---|
55 |
|
---|
56 | # If the result is not empty write the corresponding insert query to the file
|
---|
57 | if [ $CNT -ne 0 ]
|
---|
58 | then
|
---|
59 | echo "INSERT INTO RawData SET" >> ${OUTPUT}
|
---|
60 | echo ${RESULT} >> ${OUTPUT}
|
---|
61 | echo ";" >> ${OUTPUT}
|
---|
62 | fi
|
---|
63 |
|
---|
64 | done
|
---|
65 |
|
---|
66 | done
|
---|