source: trunk/DataCheck/CheckRawData.sh@ 12761

Last change on this file since 12761 was 12761, checked in by Daniela Dorner, 13 years ago
path to FACT++ as variable
  • Property svn:executable set to *
File size: 14.7 KB
Line 
1#!/bin/bash
2
3today=`date +%F`
4logfile=/home/`whoami`/DataCheck/log/CheckRaw$today.log
5
6skipmd5sum="no" # fill md5 sums in any case
7skipmd5sum="iffilled" # fill md5 sum only if they are not yet in db
8#skipmd5sum="yes" # do not fill md5 sums in any case
9
10doupdate="yes" # update all entries
11doupdate="no" # fill only entries which are not yet existing
12
13fillonly="ifzipped" # fill only entries which are not yet existing + where zipped file is availabl
14
15echo "" >> $logfile 2>&1
16echo "" >> $logfile 2>&1
17echo "" >> $logfile 2>&1
18echo `date`"executing "$0"..." >> $logfile 2>&1
19echo "=====> doupdate: "$doupdate >> $logfile 2>&1
20echo "=====> skipmd5sum: "$skipmd5sum >> $logfile 2>&1
21echo "=====> fillonly: "$fillonly >> $logfile 2>&1
22
23password=`cat /home/fact/DataCheck/.pw`
24if [ "$password" == "" ]
25then
26 echo "please insert password in .pw file"
27fi
28
29# path to FACT++ version
30factpath=/home/fact/FACT++.2012.01.19
31
32# setup to use ftools
33export HEADAS=/opt/heasoft-6.11/x86_64-unknown-linux-gnu-libc2.13-0/
34source $HEADAS/headas-init.sh
35
36# get last 2 nights
37dates=( `date +%Y/%m/%d --date="-12hour"` `date +%Y/%m/%d --date="-36hour"` )
38
39# do rsync for rawfiles of these dates
40for date in ${dates[@]}
41do
42 echo "" >> $logfile 2>&1
43 echo "" >> $logfile 2>&1
44 echo "" >> $logfile 2>&1
45 rawdir=/loc_data/raw/$date
46 runnumber=`echo $date | sed -e 's/\///g'`
47 echo `date`": processing files in "$rawdir >> $logfile 2>&1
48 # check if data are available from that night
49 if ! [ -d $rawdir ]
50 then
51 echo `date`": no data available in "$rawdir >> $logfile 2>&1
52 continue
53 fi
54
55 # find all fits-files starting with the oldest file
56 echo `date`": finding files to be zipped in $rawdir..." >> $logfile 2>&1
57 fitsfiles=`find $rawdir -type f -name '*.fits'| sort `
58
59 # loop to zip files
60 echo `date`": checking files in $rawdir..." >> $logfile 2>&1
61 for file in $fitsfiles
62 do
63 echo "" >> $logfile 2>&1
64 echo "checking file "$file >> $logfile 2>&1
65 # check if raw file was changed in the last 30 minutes
66 isnew=`find $file -cmin -30`
67 if [ "$isnew" != "" ]
68 then
69 echo $file" is not older than 30 min => continue" >> $logfile 2>&1
70 continue
71 fi
72
73 # check if it is drs file
74 isnew=`ls $file | grep drs`
75 if [ "$isnew" != "" ]
76 then
77 echo $file" is a drs file => continue" >> $logfile 2>&1
78 continue
79 fi
80
81 # check if file is already finished
82 # original file on daq (if data was taken on daq
83 origfile=`echo $file | sed -e 's/loc_data/daq/'`
84 if [ -e $origfile ]
85 then
86 # get time of last modification as seconds since Epoch for both files
87 timeorig=`stat -c %Y $origfile`
88 timecopy=`stat -c %Y $file`
89 # compare times
90 if ! [ $timeorig -eq $timecopy ]
91 then
92 # if times are not the same, the file is still open => no zip
93 echo `date`": file "$file" not yet closed => continue" >> $logfile 2>&1
94 continue
95 fi
96 else
97 # if the origfile doesn't exist, the data was probably written not on daq but on data
98 echo `date`": file "$file" was probably taken on data and not daq " >> $logfile 2>&1
99 fi
100
101 # get run and file number form filename
102 runnumbererror="no"
103 numbererror="no"
104 numberfromname=`echo $file | grep -E -o '20[1-9][0-9][01][0-9][0-3][0-9]_[0-9]{3}'`
105 runnumberfromname=`echo $numberfromname | cut -d_ -f1`
106 filenumberfromname=`echo $numberfromname | cut -d_ -f2 | sed -e 's/^0//g' -e 's/^0//g'`
107 if [ "$runnumber" != "$runnumberfromname" ]
108 then
109 runnumbererror="yes"
110 echo "ERROR: for file "$file" runnumber from date and filename don't agree ("$runnumber" - "$runnumberfromname")"
111 echo "ERROR: for file "$file" runnumber from date and filename don't agree ("$runnumber" - "$runnumberfromname")" >> $logfile 2>&1
112 fi
113
114 # check if entry already exists
115 query3="SELECT fRunNumber FROM RunInfo WHERE Concat(fRunNumber, '_', LPAD(fFileNumber, 3, 0))='"$numberfromname"'"
116 echo "Q3:"$query3 >> $logfile 2>&1 2>&1
117 result3=`/usr/bin/mysql -u operator --host=fact01.fact.local --password=$password FactData -e "$query3" 2>> $logfile`
118
119 # only proceed with file
120 # if information is not yet in database
121 # and no update is wished ($doupdate)
122 if [ "$result3" != "" ] && [ "$doupdate" == "no" ]
123 then
124 echo $file" has been inserted already => continue " >> $logfile 2>&1
125 continue
126 fi
127
128 # check if entry has already checksums
129 query5="SELECT fRunNumber FROM RunInfo WHERE Concat(fRunNumber, '_', LPAD(fFileNumber, 3, 0))='"$numberfromname"'"
130 query5=$query5" AND NOT ISNULL(fMd5sumRaw) AND NOT ISNULL(fMd5sumRawZip)"
131 echo "Q5:"$query5 >> $logfile 2>&1
132 result5=`/usr/bin/mysql -u operator --host=fact01.fact.local --password=$password FactData -e "$query5" 2>> $logfile`
133
134 # get md5sums of raw and zip file
135 # to safe time for tests and update this can be skipped ($skipmd5sum)
136 md5sum=
137 md5sumzip=
138 if [ "$skipmd5sum" == "no" ] || [ "$skipmd5sum" == "iffilled" ]
139 then
140 #echo "skip: "$skipmd5sum >> $logfile 2>&1
141 #echo "res5: -"$result5"-" >> $logfile 2>&1
142 #if [ "$skipmd5sum" == "no" ] || [ "$result5$skipmd5sum" == "iffilled" ]
143 if [ "$skipmd5sum" == "no" ] || [ "$result5" == "" ]
144 then
145 zipfile=`echo $file | sed -e 's/raw/zipraw/' -e 's/fits/fits.gz/'`
146 # only do the md5sum if the zipfile is already available
147 if ls $zipfile >/dev/null >> $logfile 2>&1
148 then
149 echo "calculation md5sum for raw..." >> $logfile 2>&1
150 md5sum=`md5sum $file | cut -d' ' -f1`
151 echo "calculation md5sum for zipraw..." >> $logfile 2>&1
152 md5sumzip=`md5sum $zipfile | cut -d' ' -f1`
153 fi
154 fi
155 fi
156 if [ "$result3" == "" ] && [ "$md5sum" == "" ] && [ "$fillonly" == "ifzipped" ]
157 then
158 echo $file" is still missing zip => do not insert yet => continue " >> $logfile 2>&1
159 continue
160 fi
161
162 runtype=
163 # check if fits file is corrupted
164 numfitserrors=0
165 checkfitsfile=`fverify $file 2>> $logfile | grep '0 error(s)'`
166 if [ "$checkfitsfile" == "" ]
167 then
168 numfitserrors=1
169 fi
170
171 if [ $numfitserrors -eq 0 ]
172 then
173 # get run and file number from file
174 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]'`
175 filenumberfromfileorig=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep RUNID | grep -E -o '[0-9]{1,3}'`
176 numberfromfile=$runnumberfromfile"_"`printf %03d $filenumberfromfileorig`
177 # compare numbers
178 if [ "$numberfromfile" != "$numberfromname" ]
179 then
180 numbererror="yes"
181 echo "ERROR: for file "$file" number from filename and file don't agree ("$numberfromname" -"$numberfromfile")"
182 echo "ERROR: for file "$file" number from filename and file don't agree ("$numberfromname" -"$numberfromfile")" >> $logfile 2>&1
183 fi
184
185 # check if this run has drs file
186 drsfile=`echo $file | sed -e 's/fits/drs.fits/'`
187 numdrsfiles=`ls $drsfile 2>/dev/null | wc -l`
188
189 # get other variables from header
190 runtype=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep RUNTYPE | grep -E -o "['][a-z-]+[']" | sed -e "s/'//g"`
191 roi=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep NROI | grep -v NROITM | grep -E -o '[0-9]{1,4}'`
192 roitm=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep NROITM | grep -E -o '[0-9]{1,4}'`
193 numevents=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep Events | grep -E -o '[0-9]+'`
194 numphys=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRG ' | grep -E -o '[0-9]+'`
195 numext1=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGEXT1' | grep -E -o '[ ][0-9]+[ ]' | sed -e 's/\ //g'`
196 numext2=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGEXT2' | grep -E -o '[ ][0-9]+[ ]' | sed -e 's/\ //g'`
197 numelp=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGLPE' | grep -E -o '[0-9]+'`
198 numilp=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGLPI' | grep -E -o '[0-9]+'`
199 numoth=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGMISC' | grep -E -o '[0-9]+'`
200 numped=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGPED' | grep -E -o '[0-9]+'`
201 numtime=`$factpath/fitsdump -h -t Events $file 2>/dev/null | grep 'NTRGTIM' | grep -E -o '[0-9]+'`
202 # in newest data start time is in DATE-OBS
203 # in older data start time is in TSTART
204 # in the beginning TSTART was empty
205 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}'`
206 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}'`
207 if [ "$runstart" == "" ]
208 then
209 if [ "$runstart2" == "" ]
210 then
211 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}'`
212 else
213 runstart=$runstart2
214 fi
215 fi
216 # in newest data start time is in DATE-END
217 # in older data start time is in TSTOP
218 # in the beginning TSTOP was empty
219 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}'`
220 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}'`
221 if [ "$runstop" == "" ]
222 then
223 if [ "$runstop2" == "" ]
224 then
225 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}'`
226 else
227 runstop=$runstop2
228 fi
229 fi
230 fi
231
232 # set runtype to 'unknown' if no runtype could be queried
233 if [ "$runtype" == "" ]
234 then
235 runtype="n/a"
236 fi
237 # on 15.11.2011 the runtypes had different names
238 if [ "$date" == "2011/11/15" ]
239 then
240 if [ "$runtype" == "drs-calib" ]
241 then
242 runtype="drs-gain"
243 fi
244 if [ "$runtype" == "drs-time-calib" ]
245 then
246 runtype="drs-time"
247 fi
248 if [ "$runtype" == "pedestal" ]
249 then
250 runtype="drs-pedestal"
251 fi
252 if [ "$runtype" == "light-pulser" ]
253 then
254 runtype="light-pulser-ext"
255 fi
256 if [ "$runtype" == "pedestal-on" ]
257 then
258 runtype="pedestal"
259 fi
260 fi
261 # get runtype
262 query2="SELECT fRunTypeKEY FROM RunType WHERE fRunTypeName='"$runtype"'"
263 echo "Q2:"$query2 >> $logfile 2>&1
264 result2=( `/usr/bin/mysql -u operator --host=fact01.fact.local --password=$password FactData -e "$query2" 2>> $logfile` )
265 if [ ${#result2} -eq 0 ]
266 then
267 echo $numberfromname": Could not query fRunTypeKey for runtype "$runtype
268 echo $numberfromname": Could not query fRunTypeKey for runtype "$runtype >> $logfile 2>&1
269 continue
270 fi
271
272 # insert or update depending on whether run exists
273 if [ "$result3" == "" ]
274 then
275 query4="INSERT"
276 querymid=" fRunNumber="$runnumber", fFileNumber="$filenumberfromname", "
277 querystop=
278 else
279 query4="UPDATE"
280 querymid=
281 querystop=" WHERE fRunNumber="$runnumber" AND fFileNumber="$filenumberfromname
282 fi
283 query4=$query4" RunInfo SET "$querymid" fRunTypeKey="${result2[1]}
284 if [ $numfitserrors -eq 0 ]
285 then
286 query4=$query4", fNumEvents="$numevents", fROI="$roi", fROITimeMarker="$roitm
287 query4=$query4", fRunStart='"$runstart"', fRunStop='"$runstop"'"
288 if [ "$numphys" != "" ]
289 then
290 query4=$query4", fNumPhysicsTrigger="$numphys
291 fi
292 if [ "$numext1" != "" ]
293 then
294 query4=$query4", fNumExt1Trigger="$numext1
295 fi
296 if [ "$numext2" != "" ]
297 then
298 query4=$query4", fNumExt2Trigger="$numext2
299 fi
300 if [ "$numelp" != "" ]
301 then
302 query4=$query4", fNumELPTrigger="$numelp
303 fi
304 if [ "$numilp" != "" ]
305 then
306 query4=$query4", fNumILPTrigger="$numilp
307 fi
308 if [ "$numped" != "" ]
309 then
310 query4=$query4", fNumPedestalTrigger="$numped
311 fi
312 if [ "$numtime" != "" ]
313 then
314 query4=$query4", fNumTimeTrigger="$numtime
315 fi
316 if [ "$numoth" != "" ]
317 then
318 query4=$query4", fNumOtherTrigger="$numoth
319 fi
320 fi
321 if [ "$md5sum" != "" ]
322 then
323 query4=$query4", fMd5sumRaw='"$md5sum"', fMd5sumRawZip='"$md5sumzip"'"
324 query4=$query4", fHasDrsFile="$numdrsfiles
325 fi
326 query4=$query4", fFitsFileErrors="$numfitserrors
327 query4=$query4" "$querystop
328 echo "Q4:"$query4 >> $logfile 2>&1
329 if ! /usr/bin/mysql -u operator --host=fact01.fact.local --password=$password FactData -e "$query4" >> $logfile 2>&1
330 then
331 echo "insert/update of "$numfromfile" to mysql failed" >> $logfile 2>&1
332 fi
333
334 # print summary for debug
335 echo "*******" >> $logfile 2>&1
336 echo "summary for file "$file >> $logfile 2>&1
337 echo "*******" >> $logfile 2>&1
338 echo " errors: "$numfitserrors" (1 means errors exist)" >> $logfile 2>&1
339 echo " number of drs files: "$numdrsfiles >> $logfile 2>&1
340 echo " runnumber error: "$runnumbererror >> $logfile 2>&1
341 echo " number error: "$numbererror >> $logfile 2>&1
342 echo " roi: "$roi >> $logfile 2>&1
343 echo " roitm: "$roitm >> $logfile 2>&1
344 echo " runtype: "$runtype >> $logfile 2>&1
345 echo " numevents: "$numevents >> $logfile 2>&1
346 echo " md5sum: "$md5sum >> $logfile 2>&1
347 echo " md5sum(zip): "$md5sumzip >> $logfile 2>&1
348 echo " start: "$runstart >> $logfile 2>&1
349 echo " stop: "$runstop >> $logfile 2>&1
350
351 # missing
352 # check wether entry has been made (status table)
353 done
354done
355
356
Note: See TracBrowser for help on using the repository browser.