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

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