source: trunk/DataCheck/Processing/CheckRawData.sh@ 13394

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