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

Last change on this file since 15765 was 15398, checked in by Daniela Dorner, 12 years ago
adapted such that it treats only the current night during night, but the last six nights during day
  • Property svn:executable set to *
File size: 16.8 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 sigma=`cat $tmpfile | grep 'sigma' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
62 if [ "$good" = "" ]
63 then
64 printprocesslog "WARN couldn't get statistics from file $1 for run $date $file"
65 rm $tmpfile
66 finish
67 fi
68 #echo "good: "$good
69 #echo "min: "$min
70 #echo "max: "$max
71 #echo "mean: "$max
72 #echo "median: "$max
73 rm $tmpfile
74}
75
76# setup to use ftools
77source $HEADAS/headas-init.sh
78
79# check if software is available
80if ! ls $factpath/fitsdump >/dev/null 2>&1
81then
82 printprocesslog "ERROR "$factpath"/fitsdump is not available."
83 finish
84fi
85
86
87# get current hour
88hour=`date +%k`
89if [ $hour -le 7 ] || [ $hour -ge 19 ]
90then
91 dates=( `date +%Y/%m/%d --date="-12hour"` )
92else
93 # get last 3, 6 or 9 nights
94 dates=( `date +%Y/%m/%d --date="-12hour"` `date +%Y/%m/%d --date="-36hour"` `date +%Y/%m/%d --date="-60hour"` \
95 `date +%Y/%m/%d --date="-84hour"` `date +%Y/%m/%d --date="-108hour"` `date +%Y/%m/%d --date="-132hour"` \
96# `date +%Y/%m/%d --date="-156hour"` `date +%Y/%m/%d --date="-180hour"` `date +%Y/%m/%d --date="-204hour"` \
97 )
98fi
99#dates=( `find $ziprawdata -mindepth 3 -type d | sort | sed "s/\${ziprawdata_for_sed}//g" | sed -e 's/^\///'` ) #all available dates in /loc_data/zipraw
100
101
102printprocesslog "INFO processing the following night(s): "${dates[@]}
103echo `date`": processing the following night(s): "${dates[@]} >> $logfile 2>&1
104
105# do filling of aux data
106for date in ${dates[@]}
107do
108 auxdir=$auxdata/$date
109 runnumber=`echo $date | sed -e 's/\///g'`
110
111 # check if aux files are available from that night
112 if ! [ -d $auxdir ]
113 then
114 printprocesslog "INFO no data available in "$auxdir
115 continue
116 else
117 printprocesslog "INFO processing files in "$auxdir
118 fi
119
120 # get file numbers from DB
121 # but only for not-corrupted files
122 query="SELECT fRunID from RunInfo WHERE fNight="$runnumber" AND fFitsFileErrors=0 "
123 printprocesslog "DEBUG get filenumbers from DB: QUERY: "$query
124 filenumbers=( `sendquery $query` )
125 if [ ${#filenumbers} -eq 0 ]
126 then
127 printprocesslog "INFO No files found in the DB for night "$date
128 continue
129 fi
130
131 # get daily fits files
132 trackingfile=$auxdir/$runnumber.DRIVE_CONTROL_TRACKING_POSITION.fits
133 if ! [ -e $trackingfile ]
134 then
135 printprocesslog "WARN "$trackingfile" not found."
136 else
137 tracknumerrors=`fverify $trackingfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
138 if [ $tracknumerrors -gt 0 ]
139 then
140 printprocesslog "WARN for $trackingfile fverify returned "$tracknumerrors" error(s)."
141 fi
142 fi
143
144 sourceposfile=$auxdir/$runnumber.DRIVE_CONTROL_SOURCE_POSITION.fits
145 if ! [ -e $sourceposfile ]
146 then
147 printprocesslog "WARN "$sourceposfile" not found."
148 else
149 sourceposnumerrors=`fverify $sourceposfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
150 if [ $sourceposnumerrors -gt 0 ]
151 then
152 printprocesslog "WARN for $sourceposfile fverify returned "$sourceposnumerrors" error(s)."
153 fi
154 fi
155
156 triggerratefile=$auxdir/$runnumber.FTM_CONTROL_TRIGGER_RATES.fits
157 if ! [ -e $triggerratefile ]
158 then
159 printprocesslog "WARN "$triggerratefile" not found."
160 else
161 trignumerrors=`fverify $triggerratefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
162 if [ $trignumerrors -gt 0 ]
163 then
164 printprocesslog "WARN for $triggerratefile fverify returned "$trignumerrors" error(s)."
165 fi
166 fi
167
168 thresholdfile=$auxdir/$runnumber.FTM_CONTROL_STATIC_DATA.fits
169 if ! [ -e $thresholdfile ]
170 then
171 printprocesslog "WARN "$thresholdfile" not found."
172 else
173 treshnumerrors=`fverify $thresholdfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
174 if [ $treshnumerrors -gt 0 ]
175 then
176 printprocesslog "WARN for $thresholdfile fverify returned "$treshnumerrors" error(s)."
177 fi
178 fi
179
180 biasvoltagefile=$auxdir/$runnumber.BIAS_CONTROL_VOLTAGE.fits
181 if ! [ -e $biasvoltagefile ]
182 then
183 printprocesslog "WARN "$biasvoltagefile" not found."
184 else
185 biasnumerrors=`fverify $biasvoltagefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
186 if [ $biasnumerrors -gt 0 ]
187 then
188 printprocesslog "WARN for $biasvoltagefile fverify returned "$biasnumerrors" error(s)."
189 fi
190 fi
191
192 # fill auxiliary information for files
193 for filenum in ${filenumbers[@]}
194 do
195 printprocesslog "INFO processing file number "$runnumber"_"`printf %03d $filenum`
196 echo `date`": processing file number "$runnumber"_"`printf %03d $filenum` >> $logfile 2>&1
197 # get information from rawfile
198 rawfile=$ziprawdata/$date/$runnumber"_"`printf %03d $filenum`.fits.gz
199 if ! [ -e $rawfile ]
200 then
201 printprocesslog "ERROR: "$rawfile" not found."
202 continue
203 fi
204 #checkfitsfile=`fverify $rawfile 2>/dev/null | grep '0 error(s)'`
205 #if [ "$checkfitsfile" == "" ]
206 #then
207 # numfitserrors=1
208 # printprocesslog "WARN: "$rawfile" probably corrupted."
209 # continue
210 #fi
211 runtype=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep RUNTYPE | grep -E -o "['][a-z-]+[']" | sed -e "s/'//g"`
212 mjdrefraw=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'MJDREF' | grep -E -o '[0-9]{5}'`
213 tstarti=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTI' | grep -E -o '[0-9]{5}'`
214 tstartf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTF' | grep -E -o '0[.][0-9]+'`
215 tstopi=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPI' | grep -E -o '[0-9]{5}'`
216 tstopf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPF' | grep -E -o '0[.][0-9]+'`
217 if [ "$tstarti" == "" ] || [ "$tstopi" == "" ] || [ "$tstartf" == "" ] || [ "$tstopf" == "" ]
218 then
219 printprocesslog "WARN: "$rawfile": one of the following keywords is empty or 0: TSTARTI TSTARTF TSTOPI TSTOPF "
220 continue
221 fi
222 # assuming that at least TSTARTI and TSTOPI are consistent
223 #echo $rawfile
224 #echo $tstarti
225 #echo $tstopi
226 #echo $tstartf
227 #echo $tstopf
228 if [ $tstarti -gt 30000 ]
229 then
230 tstart=`echo " $tstarti + $tstartf - 40587 " | bc -l`
231 tstart2=`echo " $tstarti + $tstartf - 40587 - 0.00011574 " | bc -l` # 10 sec
232 #tstart2=`echo " $tstarti + $tstartf - 40587 - 0.000023148 " | bc -l` # 2 sec
233 tstop=`echo " $tstopi + $tstopf - 40587 " | bc -l`
234 else
235 tstart=`echo " $tstarti + $tstartf " | bc -l`
236 tstart2=`echo " $tstarti + $tstartf - 0.00011574 " | bc -l` # 10 sec
237 #tstart2=`echo " $tstarti + $tstartf - 0.000023148 " | bc -l` # 2 sec
238 tstop=`echo " $tstopi + $tstopf " | bc -l`
239 fi
240 #echo $tstart
241 #echo $tstop
242 #if [ $runnumber -eq 20111123 ]
243 #then
244 # # add mjdref for days were aux files were inconsistent
245 # tstart=`echo " $tstart + $mjdref " | bc -l`
246 # tstart2=`echo " $tstart2 + $mjdref " | bc -l`
247 # tstop=`echo " $tstop + $mjdref " | bc -l`
248 #fi
249
250 # get information from source_pos file
251 if [ -e $sourceposfile ] && [ $sourceposnumerrors -eq 0 ]
252 then
253 #sourcename=`${factpath}/fitsdump ${sourceposfile} -c Time -c Name --filter='[1]<'${tstop} 2>/dev/null | tail -1 2>&1 | grep -o -E "['][a-zA-Z0-9\ ]+[']" | sed -e "s/'//g"`
254 sourcename=`/home/fact/FACT++.dbg/fitsdump ${sourceposfile} -c Time -c Name --filter='[1]<'${tstop} 2>/dev/null | tail -1 2>&1 | grep -o -E "['][a-zA-Z0-9\ ]+[']" | sed -e "s/'//g"`
255 #echo "/home/fact/FACT++.dbg/fitsdump ${sourceposfile} -c Time -c Name --filter='[1]<'${tstop} 2>/dev/null | tail -1 2>&1 "
256 #echo $sourcename
257 if [ "$sourcename" == "" ]
258 then
259 printprocesslog "INFO couldn't get sourcename ("$sourcename") from "$sourceposfile" for "$runnumber"_"$filenum
260 else
261 query="SELECT fSourceKey FROM Source WHERE fSourceName='"$sourcename"'"
262 sourcekey=`sendquery`
263 if [ "$sourcename" == "" ]
264 then
265 printprocesslog "WARN couldn't get sourcekey for source "$sourcename" from DB for "$runnumber"_"$filenum
266 fi
267 #echo $sourcekey
268 fi
269 fi
270
271 # build query to update runinfo in DB
272 query="UPDATE RunInfo SET "
273 # fill source key only if available
274 if ! [ "$sourcekey" = "" ]
275 then
276 query=$query" fSourceKey="$sourcekey", "
277 fi
278
279 # get information from tracking
280 if [ -e $trackingfile ] && [ $tracknumerrors -eq 0 ]
281 then
282 # RA
283 getfitsstatistics $trackingfile "Ra" $tstart $tstop
284 if [ "$min" == "$max" ] && [ $good -gt 0 ]
285 then
286 query=$query" fRightAscension="$mean
287 else
288 query=$query" fRightAscension=NULL"
289 if [ $good -gt 0 ]
290 then
291 printprocesslog "WARN for $rawfile RA changes within run (min: "$min", max: "$max")."
292 fi
293 fi
294 # Declination
295 getfitsstatistics $trackingfile "Dec" $tstart $tstop
296 if [ "$decmin" == "$decmax" ] && [ $good -gt 0 ]
297 then
298 query=$query", fDeclination="$mean
299 else
300 query=$query", fDeclination=NULL"
301 if [ $good -gt 0 ]
302 then
303 printprocesslog "WARN for $rawfile declination changes within run (min: "$min", max: "$max")."
304 fi
305 fi
306 # Zd
307 getfitsstatistics $trackingfile "Zd" $tstart $tstop
308 if [ $good -gt 0 ]
309 then
310 query=$query", fZenithDistanceMin="$min
311 query=$query", fZenithDistanceMean="$mean
312 query=$query", fZenithDistanceMax="$max
313 else
314 query=$query", fZenithDistanceMin=NULL"
315 query=$query", fZenithDistanceMean=NULL"
316 query=$query", fZenithDistanceMax=NULL"
317 fi
318 # Az
319 getfitsstatistics $trackingfile "Az" $tstart $tstop
320 if [ $good -gt 0 ]
321 then
322 query=$query", fAzimuthMin="$min
323 query=$query", fAzimuthMean="$mean
324 query=$query", fAzimuthMax="$max
325 else
326 query=$query", fAzimuthMin=NULL"
327 query=$query", fAzimuthMean=NULL"
328 query=$query", fAzimuthMax=NULL"
329 fi
330 else
331 query=$query" fRightAscension=NULL"
332 query=$query", fDeclination=NULL"
333 query=$query", fZenithDistanceMin=NULL"
334 query=$query", fZenithDistanceMean=NULL"
335 query=$query", fZenithDistanceMax=NULL"
336 query=$query", fAzimuthMin=NULL"
337 query=$query", fAzimuthMean=NULL"
338 query=$query", fAzimuthMax=NULL"
339 fi
340
341 # get information from trigger
342 if [ -e $triggerratefile ] && [ $trignumerrors -eq 0 ]
343 then
344 #echo " $triggerratefile TriggerRate $tstart $tstop"
345 getfitsstatistics $triggerratefile "TriggerRate" $tstart $tstop
346# if [ "$mjdreftrig" == "" ]
347# then
348# 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]*'`
349# 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]*'`
350# 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]*'`
351# 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]*'`
352# else
353# 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]*'`
354# 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]*'`
355# 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]*'`
356# 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]*'`
357# fi
358 if [ $good -gt 0 ]
359 then
360 query=$query", fTriggerRateMedian="$median
361 else
362 query=$query", fTriggerRateMedian=NULL"
363 fi
364 else
365 query=$query", fTriggerRateMedian=NULL"
366 fi
367
368 # get information from thresholds
369 if [ -e $thresholdfile ] && [ $treshnumerrors -eq 0 ]
370 then
371 getfitsstatistics $thresholdfile "PatchThresh" $tstart $tstop
372 if [ $good -eq 0 ]
373 then
374 getfitsstatistics $thresholdfile "PatchThresh" $tstart2 $tstop
375 fi
376 if [ $good -gt 0 ]
377 then
378 query=$query", fThresholdMedian="$median
379 else
380 query=$query", fThresholdMedian=NULL"
381 fi
382 else
383 query=$query", fThresholdMedian=NULL"
384 fi
385
386 # get information from bias: U
387 if [ -e $biasvoltagefile ] && [ $biasnumerrors -eq 0 ]
388 then
389 if [ $runnumber -gt 20120324 ]
390 then
391 value="Uout"
392 else
393 value="U"
394 fi
395 getfitsstatistics $biasvoltagefile $value $tstart $tstop
396 if [ $good -eq 0 ]
397 then
398 getfitsstatistics $biasvoltagefile $value $tstart2 $tstop
399 fi
400 if [ $good -gt 0 ]
401 then
402 query=$query", fBiasVoltageMedian="$median
403 else
404 query=$query", fBiasVoltageMedian=NULL"
405 fi
406 else
407 query=$query", fBiasVoltageMedian=NULL"
408 fi
409
410 # add where condition
411 query=$query" WHERE fNight="$runnumber" AND fRunID="$filenum
412 #echo $query
413
414 # send query to DB
415 sendquery >/dev/null
416 done
417done
418
419finish
420
421
Note: See TracBrowser for help on using the repository browser.