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