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

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