source: trunk/DataCheck/CheckRawData.sh@ 12659

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