source: trunk/DataCheck/Processing/FillAuxData.sh@ 14834

Last change on this file since 14834 was 14828, checked in by Daniela Dorner, 12 years ago
removed -t for fitsdump as this option was removed
  • Property svn:executable set to *
File size: 15.0 KB
Line 
1#!/bin/bash
2
3# missing:
4# vferify of files
5# only treat file if it is there
6
7# mjd of 1970-01-01
8# needed in this script as for 1 day the mjd in the aux files are inconsistent
9# mjdref=40587
10
11# known:
12# 2011/11/22 MJDREF in DRIVE empty, Time > 55000
13# 2011/11/23 MJDREF in DRIVE not empty, Time > 55000
14# 2011/11/24 MJDREF in DRIVE not empty, Time > 15000
15# raw files
16# 2011/11/21 no MJDREF
17# 2011/11/22 MJDREF
18# further things: https://www.fact-project.org/logbook/showthread.php?tid=67
19
20# trigger rate has as first value -1, but with using the median it should be fine
21
22# option
23doupdate="yes" # update all entries (needed when new fields have been added)
24doupdate="no" # fill only entries which are not yet existing (default)
25
26source `dirname $0`/../Sourcefile.sh
27printprocesslog "INFO starting $0 with option doupdate="$doupdate
28
29logfile=$runlogpath"/FillAuxLP-"$datetime".log"
30date >> $logfile
31
32function getfitsstatistics()
33{
34 # $1 filename
35 # $2 colname
36 # $3 tstart
37 # $4 tstop
38 good=
39 min=
40 mean=
41 median=
42 max=
43 tmpfile=`dirname $0`/`basename $1`.tmp
44 echo "ftcopy $1'[col Time]' - | ftstat - | grep 'mean' | grep -E -o [0-9]+[.] | sed -e 's/[.]//g'" >> $logfile
45 timefromfile=`ftcopy $1'[col Time]' - 2>>$logfile | ftstat - 2>>$logfile | grep 'mean' | grep -E -o [0-9]+[.] | sed -e 's/[.]//g'`
46 if [ $timefromfile -gt 30000 ]
47 then
48 echo "ftcopy $1'[Time - 40587> '${3}' && Time - 40587< '${4}' ][col '${2}';Time]' - | ftcopy -'[col '${2}']' - | ftstat -" >> $logfile
49 #ftcopy $1'[Time - 40587> '${3}' && Time - 40587< '${4}' ][col '${2}';Time]' - | ftcopy -'[col '${2}']' - | ftstat -
50 ftcopy $1'[Time - 40587> '${3}' && Time - 40587< '${4}' ][col '${2}';Time]' - 2>>$logfile | ftcopy -'[col '${2}']' - 2>>$logfile | ftstat - 2>>$logfile > $tmpfile
51 else
52 echo "ftcopy $1'[Time> '${3}' && Time< '${4}' ][col '${2}';Time]' - | ftcopy -'[col '${2}']' - | ftstat -" >> $logfile
53 #ftcopy $1'[Time> '${3}' && Time< '${4}' ][col '${2}';Time]' - | ftcopy -'[col '${2}']' - | ftstat -
54 ftcopy $1'[Time> '${3}' && Time< '${4}' ][col '${2}';Time]' - 2>>$logfile | ftcopy -'[col '${2}']' - 2>>$logfile | ftstat - 2>>$logfile > $tmpfile
55 fi
56 good=`cat $tmpfile | grep 'good' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
57 min=`cat $tmpfile | grep 'min' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
58 mean=`cat $tmpfile | grep 'mean' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
59 median=`cat $tmpfile | grep 'median' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
60 max=`cat $tmpfile | grep 'max' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
61 if [ "$good" = "" ]
62 then
63 printprocesslog "WARN couldn't get statistics from file $1 for run $date $file"
64 rm $tmpfile
65 finish
66 fi
67 #echo "good: "$good
68 #echo "min: "$min
69 #echo "max: "$max
70 #echo "mean: "$max
71 #echo "median: "$max
72 rm $tmpfile
73}
74
75# setup to use ftools
76source $HEADAS/headas-init.sh
77
78# check if software is available
79if ! ls $factpath/fitsdump >/dev/null 2>&1
80then
81 printprocesslog "ERROR "$factpath"/fitsdump is not available."
82 finish
83fi
84
85
86# get last 3, 6 or 9 nights
87dates=( `date +%Y/%m/%d --date="-12hour"` `date +%Y/%m/%d --date="-36hour"` `date +%Y/%m/%d --date="-60hour"` \
88# `date +%Y/%m/%d --date="-84hour"` `date +%Y/%m/%d --date="-108hour"` `date +%Y/%m/%d --date="-132hour"` \
89# `date +%Y/%m/%d --date="-156hour"` `date +%Y/%m/%d --date="-180hour"` `date +%Y/%m/%d --date="-204hour"` \
90 )
91dates=( "2012/03/25" "2012/03/26" "2012/03/27" "2012/03/28" "2012/03/29" "2012/03/30" "2012/04/10" "2012/04/09" "2012/04/08" "2012/04/06" "2012/04/05" "2012/04/03" )
92#dates=( `find $ziprawdata -mindepth 3 -type d | sort | sed "s/\${ziprawdata_for_sed}//g" | sed -e 's/^\///'` ) #all available dates in /loc_data/zipraw
93printprocesslog "INFO processing the following night(s): "${dates[@]}
94echo `date`": processing the following night(s): "${dates[@]} >> $logfile 2>&1
95
96# do filling of aux data
97for date in ${dates[@]}
98do
99 auxdir=$auxdata/$date
100 runnumber=`echo $date | sed -e 's/\///g'`
101
102 # check if aux files are available from that night
103 if ! [ -d $auxdir ]
104 then
105 printprocesslog "INFO no data available in "$auxdir
106 continue
107 else
108 printprocesslog "INFO processing files in "$auxdir
109 fi
110
111 # get file numbers from DB
112 # but only for not-corrupted files
113 query="SELECT fRunID from RunInfo WHERE fNight="$runnumber" AND fFitsFileErrors=0 "
114 printprocesslog "DEBUG get filenumbers from DB: QUERY: "$query
115 filenumbers=( `sendquery $query` )
116 if [ ${#filenumbers} -eq 0 ]
117 then
118 printprocesslog "INFO No files found in the DB for night "$date
119 continue
120 fi
121
122 # get daily fits files
123 trackingfile=$auxdir/$runnumber.DRIVE_CONTROL_TRACKING_POSITION.fits
124 if ! [ -e $trackingfile ]
125 then
126 printprocesslog "WARN "$trackingfile" not found."
127 else
128 tracknumerrors=`fverify $trackingfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
129 if [ $tracknumerrors -gt 0 ]
130 then
131 printprocesslog "WARN for $trackingfile fverify returned "$tracknumerrors" error(s)."
132 fi
133 fi
134
135 triggerratefile=$auxdir/$runnumber.FTM_CONTROL_TRIGGER_RATES.fits
136 if ! [ -e $triggerratefile ]
137 then
138 printprocesslog "WARN "$triggerratefile" not found."
139 else
140 trignumerrors=`fverify $triggerratefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
141 if [ $trignumerrors -gt 0 ]
142 then
143 printprocesslog "WARN for $triggerratefile fverify returned "$trignumerrors" error(s)."
144 fi
145 fi
146
147 thresholdfile=$auxdir/$runnumber.FTM_CONTROL_STATIC_DATA.fits
148 if ! [ -e $thresholdfile ]
149 then
150 printprocesslog "WARN "$thresholdfile" not found."
151 else
152 treshnumerrors=`fverify $thresholdfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
153 if [ $treshnumerrors -gt 0 ]
154 then
155 printprocesslog "WARN for $thresholdfile fverify returned "$treshnumerrors" error(s)."
156 fi
157 fi
158
159 biasvoltagefile=$auxdir/$runnumber.BIAS_CONTROL_VOLTAGE.fits
160 if ! [ -e $biasvoltagefile ]
161 then
162 printprocesslog "WARN "$biasvoltagefile" not found."
163 else
164 biasnumerrors=`fverify $biasvoltagefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
165 if [ $biasnumerrors -gt 0 ]
166 then
167 printprocesslog "WARN for $biasvoltagefile fverify returned "$biasnumerrors" error(s)."
168 fi
169 fi
170
171 # fill auxiliary information for files
172 for filenum in ${filenumbers[@]}
173 do
174 printprocesslog "INFO processing file number "$runnumber"_"`printf %03d $filenum`
175 echo `date`": processing file number "$runnumber"_"`printf %03d $filenum` >> $logfile 2>&1
176 # get information from rawfile
177 rawfile=$ziprawdata/$date/$runnumber"_"`printf %03d $filenum`.fits.gz
178 if ! [ -e $rawfile ]
179 then
180 printprocesslog "ERROR: "$rawfile" not found."
181 continue
182 fi
183 #checkfitsfile=`fverify $rawfile 2>/dev/null | grep '0 error(s)'`
184 #if [ "$checkfitsfile" == "" ]
185 #then
186 # numfitserrors=1
187 # printprocesslog "WARN: "$rawfile" probably corrupted."
188 # continue
189 #fi
190 runtype=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep RUNTYPE | grep -E -o "['][a-z-]+[']" | sed -e "s/'//g"`
191 mjdrefraw=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'MJDREF' | grep -E -o '[0-9]{5}'`
192 tstarti=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTI' | grep -E -o '[0-9]{5}'`
193 tstartf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTF' | grep -E -o '0[.][0-9]+'`
194 tstopi=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPI' | grep -E -o '[0-9]{5}'`
195 tstopf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPF' | grep -E -o '0[.][0-9]+'`
196 if [ "$tstarti" == "" ] || [ "$tstopi" == "" ] || [ "$tstartf" == "" ] || [ "$tstopf" == "" ]
197 then
198 printprocesslog "WARN: "$rawfile": one of the following keywords is empty or 0: TSTARTI TSTARTF TSTOPI TSTOPF "
199 continue
200 fi
201 # assuming that at least TSTARTI and TSTOPI are consistent
202 #echo $rawfile
203 #echo $tstarti
204 #echo $tstopi
205 #echo $tstartf
206 #echo $tstopf
207 if [ $tstarti -gt 30000 ]
208 then
209 tstart=`echo " $tstarti + $tstartf - 40587 " | bc -l`
210 tstart2=`echo " $tstarti + $tstartf - 40587 - 0.00011574 " | bc -l` # 10 sec
211 #tstart2=`echo " $tstarti + $tstartf - 40587 - 0.000023148 " | bc -l` # 2 sec
212 tstop=`echo " $tstopi + $tstopf - 40587 " | bc -l`
213 else
214 tstart=`echo " $tstarti + $tstartf " | bc -l`
215 tstart2=`echo " $tstarti + $tstartf - 0.00011574 " | bc -l` # 10 sec
216 #tstart2=`echo " $tstarti + $tstartf - 0.000023148 " | bc -l` # 2 sec
217 tstop=`echo " $tstopi + $tstopf " | bc -l`
218 fi
219 #echo $tstart
220 #echo $tstop
221 #if [ $runnumber -eq 20111123 ]
222 #then
223 # # add mjdref for days were aux files were inconsistent
224 # tstart=`echo " $tstart + $mjdref " | bc -l`
225 # tstart2=`echo " $tstart2 + $mjdref " | bc -l`
226 # tstop=`echo " $tstop + $mjdref " | bc -l`
227 #fi
228
229 # build query to update runinfo in DB
230 query="UPDATE RunInfo SET "
231
232 # get information from tracking
233 if [ -e $trackingfile ] && [ $tracknumerrors -eq 0 ]
234 then
235 # RA
236 getfitsstatistics $trackingfile "Ra" $tstart $tstop
237 if [ "$min" == "$max" ] && [ $good -gt 0 ]
238 then
239 query=$query" fRightAscension="$mean
240 else
241 query=$query" fRightAscension=NULL"
242 if [ $good -gt 0 ]
243 then
244 printprocesslog "WARN for $rawfile RA changes within run (min: "$min", max: "$max")."
245 fi
246 fi
247 # Declination
248 getfitsstatistics $trackingfile "Dec" $tstart $tstop
249 if [ "$decmin" == "$decmax" ] && [ $good -gt 0 ]
250 then
251 query=$query", fDeclination="$mean
252 else
253 query=$query", fDeclination=NULL"
254 if [ $good -gt 0 ]
255 then
256 printprocesslog "WARN for $rawfile declination changes within run (min: "$min", max: "$max")."
257 fi
258 fi
259 # Zd
260 getfitsstatistics $trackingfile "Zd" $tstart $tstop
261 if [ $good -gt 0 ]
262 then
263 query=$query", fZenithDistanceMin="$min
264 query=$query", fZenithDistanceMean="$mean
265 query=$query", fZenithDistanceMax="$max
266 else
267 query=$query", fZenithDistanceMin=NULL"
268 query=$query", fZenithDistanceMean=NULL"
269 query=$query", fZenithDistanceMax=NULL"
270 fi
271 # Az
272 getfitsstatistics $trackingfile "Az" $tstart $tstop
273 if [ $good -gt 0 ]
274 then
275 query=$query", fAzimuthMin="$min
276 query=$query", fAzimuthMean="$mean
277 query=$query", fAzimuthMax="$max
278 else
279 query=$query", fAzimuthMin=NULL"
280 query=$query", fAzimuthMean=NULL"
281 query=$query", fAzimuthMax=NULL"
282 fi
283 else
284 query=$query" fRightAscension=NULL"
285 query=$query", fDeclination=NULL"
286 query=$query", fZenithDistanceMin=NULL"
287 query=$query", fZenithDistanceMean=NULL"
288 query=$query", fZenithDistanceMax=NULL"
289 query=$query", fAzimuthMin=NULL"
290 query=$query", fAzimuthMean=NULL"
291 query=$query", fAzimuthMax=NULL"
292 fi
293
294 # get information from trigger
295 if [ -e $triggerratefile ] && [ $trignumerrors -eq 0 ]
296 then
297 #echo " $triggerratefile TriggerRate $tstart $tstop"
298 getfitsstatistics $triggerratefile "TriggerRate" $tstart $tstop
299# if [ "$mjdreftrig" == "" ]
300# then
301# ratemin=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'min' | grep -E -o '[0-9]+[.]?[0-9]*'`
302# ratemax=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'max' | grep -E -o '[0-9]+[.]?[0-9]*'`
303# ratemean=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'mean' | grep -E -o '[0-9]+[.]?[0-9]*'`
304# ratemedian=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'median' | grep -E -o '[0-9]+[.]?[0-9]*'`
305# else
306# ratemin=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'min' | grep -E -o '[0-9]+[.]?[0-9]*'`
307# ratemax=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'max' | grep -E -o '[0-9]+[.]?[0-9]*'`
308# ratemean=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'mean' | grep -E -o '[0-9]+[.]?[0-9]*'`
309# ratemedian=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'median' | grep -E -o '[0-9]+[.]?[0-9]*'`
310# fi
311 if [ $good -gt 0 ]
312 then
313 query=$query", fTriggerRateMedian="$median
314 else
315 query=$query", fTriggerRateMedian=NULL"
316 fi
317 else
318 query=$query", fTriggerRateMedian=NULL"
319 fi
320
321 # get information from thresholds
322 if [ -e $thresholdfile ] && [ $treshnumerrors -eq 0 ]
323 then
324 getfitsstatistics $thresholdfile "PatchThresh" $tstart $tstop
325 if [ $good -eq 0 ]
326 then
327 getfitsstatistics $thresholdfile "PatchThresh" $tstart2 $tstop
328 fi
329 if [ $good -gt 0 ]
330 then
331 query=$query", fThresholdMedian="$median
332 else
333 query=$query", fThresholdMedian=NULL"
334 fi
335 else
336 query=$query", fThresholdMedian=NULL"
337 fi
338
339 # get information from bias: U
340 if [ -e $biasvoltagefile ] && [ $biasnumerrors -eq 0 ]
341 then
342 if [ $runnumber -gt 20120324 ]
343 then
344 value="Uout"
345 else
346 value="U"
347 fi
348 getfitsstatistics $biasvoltagefile $value $tstart $tstop
349 if [ $good -eq 0 ]
350 then
351 getfitsstatistics $biasvoltagefile $value $tstart2 $tstop
352 fi
353 if [ $good -gt 0 ]
354 then
355 query=$query", fBiasVoltageMedian="$median
356 else
357 query=$query", fBiasVoltageMedian=NULL"
358 fi
359 else
360 query=$query", fBiasVoltageMedian=NULL"
361 fi
362
363 # add where condition
364 query=$query" WHERE fNight="$runnumber" AND fRunID="$filenum
365
366 # send query to DB
367 sendquery >/dev/null
368 done
369done
370
371finish
372
373
Note: See TracBrowser for help on using the repository browser.