1 | #!/bin/bash
|
---|
2 |
|
---|
3 | # option whether to fill all row or only those where information is missing
|
---|
4 | # $doupdate might be given as environment variable
|
---|
5 | if [ "$doupdate" = "" ]
|
---|
6 | then
|
---|
7 | doupdate="yes" # update all entries (needed when new fields have been added)
|
---|
8 | doupdate="no" # fill only entries which are not yet existing (default)
|
---|
9 | fi
|
---|
10 |
|
---|
11 | source `dirname $0`/../Sourcefile.sh
|
---|
12 | printprocesslog "INFO starting $0 with option doupdate="$doupdate
|
---|
13 |
|
---|
14 | logfile=$runlogpath"/FillCtrlDev-"$datetime".log"
|
---|
15 | date >> $logfile
|
---|
16 |
|
---|
17 | # setup to use ftools
|
---|
18 | source $HEADAS/headas-init.sh
|
---|
19 |
|
---|
20 | # check if software is available
|
---|
21 | if ! ls $factpath/fitsdump >/dev/null 2>&1
|
---|
22 | then
|
---|
23 | printprocesslog "ERROR "$factpath"/fitsdump is not available."
|
---|
24 | finish
|
---|
25 | fi
|
---|
26 |
|
---|
27 | # get dates
|
---|
28 | if [ "$certaindate" != "" ]
|
---|
29 | then
|
---|
30 | getdates $certaindate
|
---|
31 | else
|
---|
32 | # get all night
|
---|
33 | #getdates "all"
|
---|
34 | # get last 6 nights
|
---|
35 | getdates 6
|
---|
36 | fi
|
---|
37 |
|
---|
38 | printprocesslog "INFO processing the following night(s): "${dates[@]}
|
---|
39 | echo `date`": processing the following night(s): "${dates[@]} >> $logfile 2>&1
|
---|
40 |
|
---|
41 | cd $mars
|
---|
42 |
|
---|
43 | # do filling of aux data
|
---|
44 | for date in ${dates[@]}
|
---|
45 | do
|
---|
46 | auxdir=$auxdata/$date
|
---|
47 | runnumber=`echo $date | sed -e 's/\///g'`
|
---|
48 | if [ $runnumber -lt 20111115 ]
|
---|
49 | then
|
---|
50 | continue
|
---|
51 | fi
|
---|
52 |
|
---|
53 | # check if aux files are available from that night
|
---|
54 | if ! [ -d $auxdir ]
|
---|
55 | then
|
---|
56 | printprocesslog "INFO no data available in "$auxdir
|
---|
57 | continue
|
---|
58 | else
|
---|
59 | printprocesslog "INFO processing files in "$auxdir
|
---|
60 | fi
|
---|
61 |
|
---|
62 | # get file numbers from DB
|
---|
63 | # but only for not-corrupted files
|
---|
64 | query="SELECT fRunID from RunInfo WHERE fNight="$runnumber" AND fFitsFileErrors=0 "
|
---|
65 | if [ "$doupdate" = "no" ]
|
---|
66 | then
|
---|
67 | query=$query" AND ISNULL(fCtrlDevMean) "
|
---|
68 | fi
|
---|
69 | printprocesslog "DEBUG get filenumbers from DB: QUERY: "$query
|
---|
70 | filenumbers=( `sendquery $query` )
|
---|
71 | # proceed only if there are files available
|
---|
72 | if [ ${#filenumbers} -eq 0 ]
|
---|
73 | then
|
---|
74 | printprocesslog "INFO No files found in the DB for night "$date
|
---|
75 | continue
|
---|
76 | fi
|
---|
77 |
|
---|
78 | # get daily fits files
|
---|
79 | trackingfile=$auxdir/$runnumber.DRIVE_CONTROL_TRACKING_POSITION.fits
|
---|
80 | if ! [ -e $trackingfile ]
|
---|
81 | then
|
---|
82 | printprocesslog "WARN "$trackingfile" not found."
|
---|
83 | echo "WARN "$trackingfile" not found."
|
---|
84 | continue
|
---|
85 | else
|
---|
86 | tracknumerrors=`fverify $trackingfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
|
---|
87 | if [ $tracknumerrors -gt 0 ]
|
---|
88 | then
|
---|
89 | printprocesslog "WARN for $trackingfile fverify returned "$tracknumerrors" error(s)."
|
---|
90 | fi
|
---|
91 | fi
|
---|
92 |
|
---|
93 | # fill auxiliary information for files
|
---|
94 | for filenum in ${filenumbers[@]}
|
---|
95 | do
|
---|
96 | printprocesslog "INFO processing file number "$runnumber"_"`printf %03d $filenum`
|
---|
97 | echo `date`": processing file number "$runnumber"_"`printf %03d $filenum` >> $logfile 2>&1
|
---|
98 | # get information from rawfile
|
---|
99 | rawfile=$ziprawdata/$date/$runnumber"_"`printf %03d $filenum`.fits.fz
|
---|
100 | if ! [ -e $rawfile ]
|
---|
101 | then
|
---|
102 | printprocesslog "ERROR: "$rawfile" not found."
|
---|
103 | continue
|
---|
104 | fi
|
---|
105 | runtype=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep RUNTYPE | grep -E -o "['][a-z-]+[']" | sed -e "s/'//g"`
|
---|
106 | mjdrefraw=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'MJDREF' | grep -E -o '[0-9]{5}'`
|
---|
107 | tstarti=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTI' | grep -E -o '[0-9]{5}'`
|
---|
108 | tstartf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTF' | grep -E -o '0[.][0-9]+'`
|
---|
109 | tstopi=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPI' | grep -E -o '[0-9]{5}'`
|
---|
110 | tstopf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPF' | grep -E -o '0[.][0-9]+'`
|
---|
111 | if [ "$tstarti" == "" ] || [ "$tstopi" == "" ] || [ "$tstartf" == "" ] || [ "$tstopf" == "" ]
|
---|
112 | then
|
---|
113 | printprocesslog "WARN: "$rawfile": one of the following keywords is empty or 0: TSTARTI TSTARTF TSTOPI TSTOPF "
|
---|
114 | continue
|
---|
115 | fi
|
---|
116 | # assuming that at least TSTARTI and TSTOPI are consistent
|
---|
117 | #echo $rawfile
|
---|
118 | #echo $tstarti
|
---|
119 | #echo $tstopi
|
---|
120 | #echo $tstartf
|
---|
121 | #echo $tstopf
|
---|
122 | if [ $tstarti -gt 30000 ]
|
---|
123 | then
|
---|
124 | tstart=`echo " $tstarti + $tstartf - 40587 " | bc -l`
|
---|
125 | tstart2=`echo " $tstarti + $tstartf - 40587 - 0.00011574 " | bc -l` # 10 sec
|
---|
126 | #tstart2=`echo " $tstarti + $tstartf - 40587 - 0.000023148 " | bc -l` # 2 sec
|
---|
127 | tstop=`echo " $tstopi + $tstopf - 40587 " | bc -l`
|
---|
128 | else
|
---|
129 | tstart=`echo " $tstarti + $tstartf " | bc -l`
|
---|
130 | tstart2=`echo " $tstarti + $tstartf - 0.00011574 " | bc -l` # 10 sec
|
---|
131 | #tstart2=`echo " $tstarti + $tstartf - 0.000023148 " | bc -l` # 2 sec
|
---|
132 | tstop=`echo " $tstopi + $tstopf " | bc -l`
|
---|
133 | fi
|
---|
134 |
|
---|
135 | # build query to update runinfo in DB
|
---|
136 | query="UPDATE RunInfo SET "
|
---|
137 |
|
---|
138 | # get ctrl dev
|
---|
139 | if [ -e $trackingfile ] && [ $tracknumerrors -eq 0 ]
|
---|
140 | then
|
---|
141 | ctrldevs=( `root -q -b -l fact/processing/ctrldev.C\("\"$trackingfile\""\,$tstart\,$tstop\) | grep "result" | grep -E -o '[0-9]+[.]?[0-9]*'` )
|
---|
142 | if [ "${ctrldevs[0]}" == "" ]
|
---|
143 | then
|
---|
144 | query=$query"fCtrlDevMean=NULL"
|
---|
145 | else
|
---|
146 | query=$query"fCtrlDevMean="${ctrldevs[0]}
|
---|
147 | fi
|
---|
148 | if [ "${ctrldevs[1]}" == "" ]
|
---|
149 | then
|
---|
150 | query=$query", fCtrlDevRms=NULL"
|
---|
151 | else
|
---|
152 | query=$query", fCtrlDevRms="${ctrldevs[1]}
|
---|
153 | fi
|
---|
154 | else
|
---|
155 | query=$query" fCtrlDevMean=NULL"
|
---|
156 | query=$query", fCtrlDevRms=NULL"
|
---|
157 | fi
|
---|
158 |
|
---|
159 | # add where condition
|
---|
160 | query=$query" WHERE fNight="$runnumber" AND fRunID="$filenum
|
---|
161 |
|
---|
162 | # send query to DB
|
---|
163 | sendquery >/dev/null
|
---|
164 | done
|
---|
165 | done
|
---|
166 |
|
---|
167 | finish
|
---|
168 |
|
---|
169 |
|
---|