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

Last change on this file since 14838 was 14837, checked in by Daniela Dorner, 12 years ago
added sigma to statistic in getstatistics function
  • 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 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 )
92dates=( "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" )
93#dates=( `find $ziprawdata -mindepth 3 -type d | sort | sed "s/\${ziprawdata_for_sed}//g" | sed -e 's/^\///'` ) #all available dates in /loc_data/zipraw
94printprocesslog "INFO processing the following night(s): "${dates[@]}
95echo `date`": processing the following night(s): "${dates[@]} >> $logfile 2>&1
96
97# do filling of aux data
98for date in ${dates[@]}
99do
100 auxdir=$auxdata/$date
101 runnumber=`echo $date | sed -e 's/\///g'`
102
103 # check if aux files are available from that night
104 if ! [ -d $auxdir ]
105 then
106 printprocesslog "INFO no data available in "$auxdir
107 continue
108 else
109 printprocesslog "INFO processing files in "$auxdir
110 fi
111
112 # get file numbers from DB
113 # but only for not-corrupted files
114 query="SELECT fRunID from RunInfo WHERE fNight="$runnumber" AND fFitsFileErrors=0 "
115 printprocesslog "DEBUG get filenumbers from DB: QUERY: "$query
116 filenumbers=( `sendquery $query` )
117 if [ ${#filenumbers} -eq 0 ]
118 then
119 printprocesslog "INFO No files found in the DB for night "$date
120 continue
121 fi
122
123 # get daily fits files
124 trackingfile=$auxdir/$runnumber.DRIVE_CONTROL_TRACKING_POSITION.fits
125 if ! [ -e $trackingfile ]
126 then
127 printprocesslog "WARN "$trackingfile" not found."
128 else
129 tracknumerrors=`fverify $trackingfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
130 if [ $tracknumerrors -gt 0 ]
131 then
132 printprocesslog "WARN for $trackingfile fverify returned "$tracknumerrors" error(s)."
133 fi
134 fi
135
136 triggerratefile=$auxdir/$runnumber.FTM_CONTROL_TRIGGER_RATES.fits
137 if ! [ -e $triggerratefile ]
138 then
139 printprocesslog "WARN "$triggerratefile" not found."
140 else
141 trignumerrors=`fverify $triggerratefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
142 if [ $trignumerrors -gt 0 ]
143 then
144 printprocesslog "WARN for $triggerratefile fverify returned "$trignumerrors" error(s)."
145 fi
146 fi
147
148 thresholdfile=$auxdir/$runnumber.FTM_CONTROL_STATIC_DATA.fits
149 if ! [ -e $thresholdfile ]
150 then
151 printprocesslog "WARN "$thresholdfile" not found."
152 else
153 treshnumerrors=`fverify $thresholdfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
154 if [ $treshnumerrors -gt 0 ]
155 then
156 printprocesslog "WARN for $thresholdfile fverify returned "$treshnumerrors" error(s)."
157 fi
158 fi
159
160 biasvoltagefile=$auxdir/$runnumber.BIAS_CONTROL_VOLTAGE.fits
161 if ! [ -e $biasvoltagefile ]
162 then
163 printprocesslog "WARN "$biasvoltagefile" not found."
164 else
165 biasnumerrors=`fverify $biasvoltagefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
166 if [ $biasnumerrors -gt 0 ]
167 then
168 printprocesslog "WARN for $biasvoltagefile fverify returned "$biasnumerrors" error(s)."
169 fi
170 fi
171
172 # fill auxiliary information for files
173 for filenum in ${filenumbers[@]}
174 do
175 printprocesslog "INFO processing file number "$runnumber"_"`printf %03d $filenum`
176 echo `date`": processing file number "$runnumber"_"`printf %03d $filenum` >> $logfile 2>&1
177 # get information from rawfile
178 rawfile=$ziprawdata/$date/$runnumber"_"`printf %03d $filenum`.fits.gz
179 if ! [ -e $rawfile ]
180 then
181 printprocesslog "ERROR: "$rawfile" not found."
182 continue
183 fi
184 #checkfitsfile=`fverify $rawfile 2>/dev/null | grep '0 error(s)'`
185 #if [ "$checkfitsfile" == "" ]
186 #then
187 # numfitserrors=1
188 # printprocesslog "WARN: "$rawfile" probably corrupted."
189 # continue
190 #fi
191 runtype=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep RUNTYPE | grep -E -o "['][a-z-]+[']" | sed -e "s/'//g"`
192 mjdrefraw=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'MJDREF' | grep -E -o '[0-9]{5}'`
193 tstarti=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTI' | grep -E -o '[0-9]{5}'`
194 tstartf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTF' | grep -E -o '0[.][0-9]+'`
195 tstopi=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPI' | grep -E -o '[0-9]{5}'`
196 tstopf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPF' | grep -E -o '0[.][0-9]+'`
197 if [ "$tstarti" == "" ] || [ "$tstopi" == "" ] || [ "$tstartf" == "" ] || [ "$tstopf" == "" ]
198 then
199 printprocesslog "WARN: "$rawfile": one of the following keywords is empty or 0: TSTARTI TSTARTF TSTOPI TSTOPF "
200 continue
201 fi
202 # assuming that at least TSTARTI and TSTOPI are consistent
203 #echo $rawfile
204 #echo $tstarti
205 #echo $tstopi
206 #echo $tstartf
207 #echo $tstopf
208 if [ $tstarti -gt 30000 ]
209 then
210 tstart=`echo " $tstarti + $tstartf - 40587 " | bc -l`
211 tstart2=`echo " $tstarti + $tstartf - 40587 - 0.00011574 " | bc -l` # 10 sec
212 #tstart2=`echo " $tstarti + $tstartf - 40587 - 0.000023148 " | bc -l` # 2 sec
213 tstop=`echo " $tstopi + $tstopf - 40587 " | bc -l`
214 else
215 tstart=`echo " $tstarti + $tstartf " | bc -l`
216 tstart2=`echo " $tstarti + $tstartf - 0.00011574 " | bc -l` # 10 sec
217 #tstart2=`echo " $tstarti + $tstartf - 0.000023148 " | bc -l` # 2 sec
218 tstop=`echo " $tstopi + $tstopf " | bc -l`
219 fi
220 #echo $tstart
221 #echo $tstop
222 #if [ $runnumber -eq 20111123 ]
223 #then
224 # # add mjdref for days were aux files were inconsistent
225 # tstart=`echo " $tstart + $mjdref " | bc -l`
226 # tstart2=`echo " $tstart2 + $mjdref " | bc -l`
227 # tstop=`echo " $tstop + $mjdref " | bc -l`
228 #fi
229
230 # build query to update runinfo in DB
231 query="UPDATE RunInfo SET "
232
233 # get information from tracking
234 if [ -e $trackingfile ] && [ $tracknumerrors -eq 0 ]
235 then
236 # RA
237 getfitsstatistics $trackingfile "Ra" $tstart $tstop
238 if [ "$min" == "$max" ] && [ $good -gt 0 ]
239 then
240 query=$query" fRightAscension="$mean
241 else
242 query=$query" fRightAscension=NULL"
243 if [ $good -gt 0 ]
244 then
245 printprocesslog "WARN for $rawfile RA changes within run (min: "$min", max: "$max")."
246 fi
247 fi
248 # Declination
249 getfitsstatistics $trackingfile "Dec" $tstart $tstop
250 if [ "$decmin" == "$decmax" ] && [ $good -gt 0 ]
251 then
252 query=$query", fDeclination="$mean
253 else
254 query=$query", fDeclination=NULL"
255 if [ $good -gt 0 ]
256 then
257 printprocesslog "WARN for $rawfile declination changes within run (min: "$min", max: "$max")."
258 fi
259 fi
260 # Zd
261 getfitsstatistics $trackingfile "Zd" $tstart $tstop
262 if [ $good -gt 0 ]
263 then
264 query=$query", fZenithDistanceMin="$min
265 query=$query", fZenithDistanceMean="$mean
266 query=$query", fZenithDistanceMax="$max
267 else
268 query=$query", fZenithDistanceMin=NULL"
269 query=$query", fZenithDistanceMean=NULL"
270 query=$query", fZenithDistanceMax=NULL"
271 fi
272 # Az
273 getfitsstatistics $trackingfile "Az" $tstart $tstop
274 if [ $good -gt 0 ]
275 then
276 query=$query", fAzimuthMin="$min
277 query=$query", fAzimuthMean="$mean
278 query=$query", fAzimuthMax="$max
279 else
280 query=$query", fAzimuthMin=NULL"
281 query=$query", fAzimuthMean=NULL"
282 query=$query", fAzimuthMax=NULL"
283 fi
284 else
285 query=$query" fRightAscension=NULL"
286 query=$query", fDeclination=NULL"
287 query=$query", fZenithDistanceMin=NULL"
288 query=$query", fZenithDistanceMean=NULL"
289 query=$query", fZenithDistanceMax=NULL"
290 query=$query", fAzimuthMin=NULL"
291 query=$query", fAzimuthMean=NULL"
292 query=$query", fAzimuthMax=NULL"
293 fi
294
295 # get information from trigger
296 if [ -e $triggerratefile ] && [ $trignumerrors -eq 0 ]
297 then
298 #echo " $triggerratefile TriggerRate $tstart $tstop"
299 getfitsstatistics $triggerratefile "TriggerRate" $tstart $tstop
300# if [ "$mjdreftrig" == "" ]
301# then
302# 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]*'`
303# 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]*'`
304# 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]*'`
305# 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]*'`
306# else
307# 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]*'`
308# 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]*'`
309# 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]*'`
310# 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]*'`
311# fi
312 if [ $good -gt 0 ]
313 then
314 query=$query", fTriggerRateMedian="$median
315 else
316 query=$query", fTriggerRateMedian=NULL"
317 fi
318 else
319 query=$query", fTriggerRateMedian=NULL"
320 fi
321
322 # get information from thresholds
323 if [ -e $thresholdfile ] && [ $treshnumerrors -eq 0 ]
324 then
325 getfitsstatistics $thresholdfile "PatchThresh" $tstart $tstop
326 if [ $good -eq 0 ]
327 then
328 getfitsstatistics $thresholdfile "PatchThresh" $tstart2 $tstop
329 fi
330 if [ $good -gt 0 ]
331 then
332 query=$query", fThresholdMedian="$median
333 else
334 query=$query", fThresholdMedian=NULL"
335 fi
336 else
337 query=$query", fThresholdMedian=NULL"
338 fi
339
340 # get information from bias: U
341 if [ -e $biasvoltagefile ] && [ $biasnumerrors -eq 0 ]
342 then
343 if [ $runnumber -gt 20120324 ]
344 then
345 value="Uout"
346 else
347 value="U"
348 fi
349 getfitsstatistics $biasvoltagefile $value $tstart $tstop
350 if [ $good -eq 0 ]
351 then
352 getfitsstatistics $biasvoltagefile $value $tstart2 $tstop
353 fi
354 if [ $good -gt 0 ]
355 then
356 query=$query", fBiasVoltageMedian="$median
357 else
358 query=$query", fBiasVoltageMedian=NULL"
359 fi
360 else
361 query=$query", fBiasVoltageMedian=NULL"
362 fi
363
364 # add where condition
365 query=$query" WHERE fNight="$runnumber" AND fRunID="$filenum
366
367 # send query to DB
368 sendquery >/dev/null
369 done
370done
371
372finish
373
374
Note: See TracBrowser for help on using the repository browser.