source: trunk/DataCheck/CheckRawData.sh@ 12972

Last change on this file since 12972 was 12972, checked in by Daniela Dorner, 13 years ago
introduced usage of global logfile and function sendquery
  • Property svn:executable set to *
File size: 16.6 KB
Line 
1#!/bin/bash
2
3# this script has been written to run on La Palma on the machine data
4# i.e. paths are only working on this machine
5# the script starts from the zipped files
6# this causes a delay until files are in the database
7# because they have to be rsynced and zipped first (see RsyncRawData.sh, ZipRawData.sh)
8
9# missing
10# entry in status table
11
12# options:
13skipmd5sum="no" # fill md5 sums in any case
14skipmd5sum="iffilled" # fill md5 sum only if they are not yet in db # default
15#skipmd5sum="yes" # do not fill md5 sums in any case
16
17doupdate="yes" # update all entries
18doupdate="no" # fill only entries which are not yet existing #default
19
20source `dirname $0`/Sourcefile.sh
21printprocesslog "INFO starting $0 with options doupdate="$doupdate" and skipmd5sum="$skipmd5sum
22
23# setup to use ftools
24source $HEADAS/headas-init.sh
25
26pwfile=`dirname $0`/.pw
27password=`cat $pwfile 2>/dev/null`
28if [ "$password" == "" ]
29then
30 echo "please insert password in $pwfile"
31 printprocesslog "ERROR password for DB access in $pwfile missing"
32 finish
33fi
34
35# check if software is available
36if ! ls $factpath/fitsdump >/dev/null 2>&1
37then
38 printprocesslog "ERROR "$factpath"/fitsdump is not available."
39 finish
40fi
41
42# check if paths are available
43if ! ls /daq/raw >/dev/null 2>&1
44then
45 printprocesslog "ERROR /daq/raw is not available."
46 finish
47fi
48if ! ls /loc_data/raw >/dev/null 2>&1
49then
50 printprocesslog "ERROR /loc_data/raw is not available."
51 finish
52fi
53if ! ls /loc_data/zipraw >/dev/null 2>&1
54then
55 printprocesslog "ERROR /loc_data/zipraw is not available."
56 finish
57fi
58
59# get last 3 nights
60dates=( `date +%Y/%m/%d --date="-12hour"` `date +%Y/%m/%d --date="-36hour"` )
61#dates=( `find /loc_data/zipraw/ -mindepth 3 -type d | sort | sed -e 's/\/loc_data\/zipraw\///g'` ) #all available dates in /loc_data/zipraw
62#dates=( "2012/02/19" )
63
64# do check for rawfiles of these dates
65for date in ${dates[@]}
66do
67 ziprawdir=/loc_data/zipraw/$date
68 # check if data are available from that night
69 if ! [ -d $ziprawdir ]
70 then
71 printprocesslog "INFO "$ziprawdir" does not exist."
72 continue
73 else
74 printprocesslog "INFO processing "$ziprawdir"..."
75 fi
76
77 # find all fits.gz files starting with the oldest file
78 printprocesslog "INFO finding files to be checked in $ziprawdir..."
79 fitsgzfiles=`find $ziprawdir -type f -name '*.fits.gz'| sort `
80
81 # get runnumber from date
82 runnumber=`echo $date | sed -e 's/\///g'`
83
84 # loop to check files
85 for file in $fitsgzfiles
86 do
87 printprocesslog "INFO checking file "$file
88 #echo "INFO checking file "$file
89
90 # raw and original file
91 rawfile=`echo $file | sed -e 's/zipraw/raw/' -e 's/fits[.]gz/fits/'`
92 origfile=`echo $rawfile | sed -e 's/loc_data/daq/'`
93
94 # check if raw file was changed in the last 30 minutes
95 isnew=`find $file -cmin -30`
96 if [ "$isnew" != "" ]
97 then
98 printprocesslog "INFO "$file" is not older than 30 min. -> continue"
99 continue
100 fi
101
102 # check if it is drs file
103 isnew=`ls $file | grep drs`
104 if [ "$isnew" != "" ]
105 then
106 printprocesslog "INFO "$file" is a drs file. -> continue"
107 continue
108 fi
109
110 # check if file is already finished
111 # original file on daq (if data was taken on daq
112 if [ -e $origfile ]
113 then
114 # get time of last modification as seconds since Epoch for both files
115 timeorig=`stat -c %Y $origfile`
116 timecopy=`stat -c %Y $rawfile`
117 # compare times
118 if ! [ $timeorig -eq $timecopy ]
119 then
120 # if times are not the same, the file is still open => no check
121 printprocesslog "INFO file "$rawfile" not yet closed. -> continue"
122 continue
123 fi
124 else
125 # if the origfile doesn't exist, the data was probably written not on daq but on data
126 printprocesslog "INFO file "$rawfile" was probably taken on data and not daq."
127 fi
128
129 # get run and file number form filename
130 runnumbererror="no"
131 numbererror="no"
132 numberfromname=`echo $file | grep -E -o '20[1-9][0-9][01][0-9][0-3][0-9]_[0-9]{3}'`
133 runnumberfromname=`echo $numberfromname | cut -d_ -f1`
134 filenumberfromname=`echo $numberfromname | cut -d_ -f2 | sed -e 's/^0//g' -e 's/^0//g'`
135 if [ "$runnumber" != "$runnumberfromname" ]
136 then
137 runnumbererror="yes"
138 printprocesslog "ERROR for file "$file": runnumber from date ("$runnumber") and filename ("$runnumberfromname") don't agree."
139 fi
140
141 # check if entry already exists
142 query="SELECT fNight FROM RunInfo WHERE Concat(fNight, '_', LPAD(fRunID, 3, 0))='"$numberfromname"'"
143 printprocesslog "DEBUG check if entry already exists in DB. QUERY: "$query
144 #result3=`/usr/bin/mysql -u operator --host=fact01.fact.local --password=$password FactData -e "$query3"`
145 result3=`sendquery`
146
147 # only proceed with file
148 # if information is not yet in database
149 # and no update is wished ($doupdate)
150 if [ "$result3" != "" ] && [ "$doupdate" == "no" ]
151 then
152 printprocesslog "INFO "$file" has been inserted already. -> continue "
153 continue
154 fi
155
156 runtype=
157 # check if fits file is corrupted
158 numfitserrors=0
159 #checkfitsfile=`fverify $rawfile 2>/dev/null | grep '0 error(s)'`
160 numfitserrors=`fverify $rawfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
161 #if [ "$checkfitsfile" == "" ]
162 if [ $numfitserrors -gt 0 ]
163 then
164 printprocesslog "WARN "$rawfile" has "$numfitserrors" fitserror(s). "
165 #numfitserrors=1
166 fi
167
168 if [ $numfitserrors -eq 0 ]
169 then
170 # get run and file number from file
171 runnumberfromfile=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep NIGHT | grep -E -o '20[1-9][0-9][01][0-9][0-3][0-9]'`
172 filenumberfromfileorig=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep RUNID | grep -E -o '[0-9]{1,3}'`
173 if [ "$runnumberfromfile" = "" ] || [ "$filenumberfromfileorig" = "" ]
174 then
175 printprocesslog "ERROR couldn't get run or file number from file name ("$file")."
176 fi
177 numberfromfile=$runnumberfromfile"_"`printf %03d $filenumberfromfileorig`
178 # compare numbers
179 if [ "$numberfromfile" != "$numberfromname" ]
180 then
181 numbererror="yes"
182 printprocesslog "ERROR for file "$file": number from filename ("$numberfromname") and file ("$numberfromfile") don't agree."
183 fi
184
185 # check if this run has drs file
186 # in case file is available, get STEP from header
187 # in the very beginning only drs-files were existing
188 # in the beginning the keywords DRSCALIB and STEP were not existing
189 numdrsfiles=
190 step=
191 drsfile=`echo $file | sed -e 's/fits/drs.fits/'`
192 numdrsfiles=`ls $drsfile 2>/dev/null | wc -l`
193 drscalib=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep DRSCALIB | grep -E -o "['][TF][']" | sed -e "s/'//g"`
194 if [ "$drscalib" == "T" ]
195 then
196 step=`$factpath/fitsdump -h -t Events $drsfile 2>/dev/null | grep STEP | grep -E -o "['][012][']" | sed -e "s/'//g"`
197 if ! [ $numdrsfiles -eq 1 ]
198 then
199 printprocesslog "ERROR for file "$file" number of drsfiles ("$numdrsfiles") and information from header ("$drscalib") don't agree."
200 fi
201 else
202 if ! [ "$drscalib" == "F" ]
203 then
204 printprocesslog "WARN for file "$file" DRSCALIB is neither T nor F."
205 fi
206 fi
207
208 # get other variables from header
209 runtype=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep RUNTYPE | grep -E -o "['][a-z0-9._-]+[']" | sed -e "s/'//g" -e "s/_/-/g" -e "s/[.]//g"`
210 #echo "runtype for file "$file": "$runtype
211 roi=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep NROI | grep -v NROITM | grep -E -o '[0-9]{1,4}'`
212 roitm=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep NROITM | grep -E -o '[0-9]{1,4}'`
213 numevents=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep Events | grep -E -o '[0-9]+'`
214 numphys=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRG ' | grep -E -o '[0-9]+'`
215 numext1=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGEXT1' | grep -E -o '[ ][0-9]+[ ]' | sed -e 's/\ //g'`
216 numext2=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGEXT2' | grep -E -o '[ ][0-9]+[ ]' | sed -e 's/\ //g'`
217 numelp=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGLPE' | grep -E -o '[0-9]+'`
218 numilp=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGLPI' | grep -E -o '[0-9]+'`
219 numoth=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGMISC' | grep -E -o '[0-9]+'`
220 numped=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGPED' | grep -E -o '[0-9]+'`
221 numtime=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGTIM' | grep -E -o '[0-9]+'`
222 compiled=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'COMPILED' | grep -E -o "['][a-zA-Z]+[ ][ 12][0-9][ ]20[0-9][0-9][ ][0-2][0-9]:[0-5][0-9]:[0-5][0-9][']" | sed -e "s/'//g"`
223 if ! [ "$compiled" == "" ]
224 then
225 compiletime=`date +'%F %H:%M:%S' --date="${compiled}" `
226 else
227 compiletime=
228 fi
229 revnum=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'REVISION' | grep -E -o "['][0-9]+[:]?[0-9]*[MSP]*[']" | sed -e "s/'//g"`
230 # in newest data start time is in DATE-OBS
231 # in older data start time is in TSTART
232 # in the beginning TSTART was empty
233 runstart=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep DATE-OBS | grep -E -o '20[1-9][0-9][\-][01][0-9][\-][0-3][0-9]T[0-2][0-9]:[0-6][0-9]:[0-6][0-9][.][0-9]{6}'`
234 runstart2=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep TSTART | grep -E -o '20[1-9][0-9][\-][01][0-9][\-][0-3][0-9]T[0-2][0-9]:[0-6][0-9]:[0-6][0-9][.][0-9]{6}'`
235 if [ "$runstart" == "" ]
236 then
237 if [ "$runstart2" == "" ]
238 then
239 runstart=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep DATE | grep -v 'DATE-' | grep -E -o '20[1-9][0-9][\-][01][0-9][\-][0-3][0-9]T[0-2][0-9]:[0-6][0-9]:[0-6][0-9][.][0-9]{6}'`
240 else
241 runstart=$runstart2
242 fi
243 fi
244 # in newest data start time is in DATE-END
245 # in older data start time is in TSTOP
246 # in the beginning TSTOP was empty
247 runstop=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep DATE-END | grep -E -o '20[1-9][0-9][\-][01][0-9][\-][0-3][0-9]T[0-2][0-9]:[0-6][0-9]:[0-6][0-9][.][0-9]{6}'`
248 runstop2=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep TSTOP | grep -E -o '20[1-9][0-9][\-][01][0-9][\-][0-3][0-9]T[0-2][0-9]:[0-6][0-9]:[0-6][0-9][.][0-9]{6}'`
249 if [ "$runstop" == "" ]
250 then
251 if [ "$runstop2" == "" ]
252 then
253 runstop=`stat $file 2>/dev/null | grep Modify | grep -E -o '20[1-9][0-9][\-][01][0-9][\-][0-3][0-9][ ][0-2][0-9]:[0-6][0-9]:[0-6][0-9][.][0-9]{9}'`
254 else
255 runstop=$runstop2
256 fi
257 fi
258 fi
259
260 # set runtype to 'unknown', if no runtype could be retrieved from file
261 if [ "$runtype" == "" ]
262 then
263 runtype="n/a"
264 fi
265 # on 15.11.2011 the runtypes had different names
266 if [ "$date" == "2011/11/15" ]
267 then
268 if [ "$runtype" == "drs-calib" ]
269 then
270 runtype="drs-gain"
271 fi
272 if [ "$runtype" == "drs-time-calib" ]
273 then
274 runtype="drs-time"
275 fi
276 if [ "$runtype" == "pedestal" ]
277 then
278 runtype="drs-pedestal"
279 fi
280 if [ "$runtype" == "light-pulser" ]
281 then
282 runtype="light-pulser-ext"
283 fi
284 if [ "$runtype" == "pedestal-on" ]
285 then
286 runtype="pedestal"
287 fi
288 fi
289 # get runtype
290 query="SELECT fRunTypeKEY FROM RunType WHERE fRunTypeName='"$runtype"'"
291 printprocesslog "DEBUG get run type from DB. QUERY:"$query
292 #result2=( `/usr/bin/mysql -u operator --host=fact01.fact.local --password=$password FactData -e "$query2"` )
293 result2=( `sendquery` )
294 if [ ${#result2} -eq 0 ]
295 then
296 printprocesslog "ERROR "$numberfromname": Could not query fRunTypeKey for runtype "$runtype" ."
297 continue
298 fi
299
300 # check if entry has already checksums
301 query="SELECT fNight FROM RunInfo WHERE Concat(fNight, '_', LPAD(fRunID, 3, 0))='"$numberfromname"'"
302 query=$query" AND NOT ISNULL(fMd5sumRaw) AND NOT ISNULL(fMd5sumRawZip)"
303 printprocesslog "DEBUG check if md5sums are alreay in DB. QUERY:"$query
304 #result5=`/usr/bin/mysql -u operator --host=fact01.fact.local --password=$password FactData -e "$query5"`
305 result5=`sendquery`
306
307 # get md5sums of raw and zip file
308 # to safe time for tests and update this can be skipped ($skipmd5sum)
309 md5sum=
310 md5sumzip=
311 if [ "$skipmd5sum" == "no" ] || [ "$skipmd5sum" == "iffilled" ]
312 then
313 #echo "skip: "$skipmd5sum >> $logfile 2>&1
314 #echo "res5: -"$result5"-" >> $logfile 2>&1
315 #if [ "$skipmd5sum" == "no" ] || [ "$result5$skipmd5sum" == "iffilled" ]
316 if [ "$skipmd5sum" == "no" ] || [ "$result5" == "" ]
317 then
318 #zipfile=`echo $file | sed -e 's/raw/zipraw/' -e 's/fits/fits.gz/'`
319 # only do the md5sum if the zipfile is already available
320 if ls $file >/dev/null 2>&1
321 then
322 printprocesslog "INFO calculation md5sum for rawfile "$rawfile
323 md5sum=`md5sum $rawfile | cut -d' ' -f1`
324 printprocesslog "INFO calculation md5sum for zipfile "$file
325 md5sumzip=`md5sum $file | cut -d' ' -f1`
326 fi
327 fi
328 fi
329
330 # insert or update depending on whether run exists
331 if [ "$result3" == "" ]
332 then
333 query="INSERT"
334 querymid=" fNight="$runnumber", fRunID="$filenumberfromname", "
335 querystop=
336 else
337 query="UPDATE"
338 querymid=
339 querystop=" WHERE fNight="$runnumber" AND fRunID="$filenumberfromname
340 fi
341 query=$query" RunInfo SET "$querymid" fRunTypeKey="${result2[0]}
342 if [ $numfitserrors -eq 0 ]
343 then
344 query=$query", fRunStart='"$runstart"', fRunStop='"$runstop"'"
345 if [ "$numevents" != "" ]
346 then
347 query=$query", fNumEvents="$numevents
348 fi
349 if [ "$roi" != "" ]
350 then
351 query=$query", fROI="$roi
352 fi
353 if [ "$roitm" != "" ]
354 then
355 query=$query", fROITimeMarker="$roitm
356 fi
357 if [ "$numphys" != "" ]
358 then
359 query=$query", fNumPhysicsTrigger="$numphys
360 fi
361 if [ "$numext1" != "" ]
362 then
363 query=$query", fNumExt1Trigger="$numext1
364 fi
365 if [ "$numext2" != "" ]
366 then
367 query=$query", fNumExt2Trigger="$numext2
368 fi
369 if [ "$numelp" != "" ]
370 then
371 query=$query", fNumELPTrigger="$numelp
372 fi
373 if [ "$numilp" != "" ]
374 then
375 query=$query", fNumILPTrigger="$numilp
376 fi
377 if [ "$numped" != "" ]
378 then
379 query=$query", fNumPedestalTrigger="$numped
380 fi
381 if [ "$numtime" != "" ]
382 then
383 query=$query", fNumTimeTrigger="$numtime
384 fi
385 if [ "$numoth" != "" ]
386 then
387 query=$query", fNumOtherTrigger="$numoth
388 fi
389 fi
390 if [ "$md5sum" != "" ]
391 then
392 query=$query", fMd5sumRaw='"$md5sum"', fMd5sumRawZip='"$md5sumzip"'"
393 fi
394 if [ "$numdrsfiles" != "" ]
395 then
396 query=$query", fHasDrsFile="$numdrsfiles
397 fi
398 if [ "$step" != "" ]
399 then
400 query=$query", fDrsStep="$step
401 fi
402 if [ "$compiletime" != "" ]
403 then
404 query=$query", fCompileTime='"$compiletime"'"
405 fi
406 if [ "$revnum" != "" ]
407 then
408 query=$query", fRevisionNumber='"$revnum"'"
409 fi
410 query=$query", fFitsFileErrors="$numfitserrors
411 query=$query" "$querystop
412 printprocesslog "INFO insert/update entry in DB. QUERY: "$query
413 # send query to DB
414 sendquery >/dev/null
415 #if ! /usr/bin/mysql -u operator --host=fact01.fact.local --password=$password FactData -e "$query4"
416 #then
417 # printprocesslog "ERROR insert/update of "$numfromfile" to mysql failed."
418 # finish
419 #fi
420 done
421done
422
423finish
424
Note: See TracBrowser for help on using the repository browser.