source: trunk/Mars/hawc/processing/extract-raw-headers.sh@ 20084

Last change on this file since 20084 was 20034, checked in by tbretz, 4 years ago
Using the power of bash to do the splitting.
File size: 3.6 KB
Line 
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
5TELESCOPE=1
6
7# This is the directory in which to serach for files
8DIR="/data/raw"
9# This is the path to the fitsdump executable
10FITSDUMP=~/FACT++/build/fitsdump
11# File to which the queries are written
12OUTPUT="insert-raw.sql"
13
14echo \
15"\
16CREATE 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
62for SUBDIR in ${DIR}/[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]
63do
64
65 # Loop over all files in this subdirectory
66 for ROOT in ${SUBDIR}/${NIGHT}*.fits.fz
67 do
68 FILE=`echo ${ROOT} | cut -f2 -d_`
69 echo ${NIGHT} ${FILE} ${ROOT}
70
71 # Extact the header from the fits file with fitsdump, the following chain
72 # properly formats the output, replaces T/F with true/false, replaces
73 # run-ytpes by numeric types, adds enclosures for SQL variable names where
74 # necessary, removes enclosures around hex-numbers and adds commas
75 # after all lines except the last one
76 RESULT=`${FITSDUMP} -h ${ROOT} \
77 | 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' \
78 | grep -v CHECKSUM \
79 | cut -c4- \
80 | cut -f1 -d\/ \
81 | sed "s/'drs-pedestal'/1/g" \
82 | sed "s/'drs-gain'/2/g" \
83 | sed "s/'pedestal'/3/g" \
84 | sed "s/'data'/4/g" \
85 | sed "s/'custom'/0/g" \
86 | sed 's/\ T\ /true/g' \
87 | sed 's/\ F\ /false/g' \
88 | sed 's/^DATE-END/`DATE-END`/' \
89 | sed 's/^DATE-OBS/`DATE-OBS`/' \
90 | sed 's/^DATE[^-]/`DATE`/' \
91 | sed "s/^\(.*\)'\(0x[0-9a-f]*\)'\(.*\)$/\1\2\3/g" \
92 | sed '$!s/$/,/' >> query.txt`
93
94 # count the number of lines in the result
95 CNT=`echo ${RESULT} | wc -l`
96
97 echo "== $CNT =="
98
99 # If the result is not empty write the corresponding insert query to the file
100 if [ $CNT -ne 0 ]
101 then
102 echo "INSERT INTO RawData SET" >> ${OUTPUT}
103 echo Telescope=${TELESCOPE}, >> ${OUTPUT}
104 echo ${RESULT} >> ${OUTPUT}
105 echo ";" >> ${OUTPUT}
106 fi
107
108 done
109
110done
Note: See TracBrowser for help on using the repository browser.