| 1 | #!/bin/bash
|
|---|
| 2 |
|
|---|
| 3 | today=`date +%F`
|
|---|
| 4 | logfile=/home/`whoami`/DataCheck/log/CheckRaw$today.log
|
|---|
| 5 |
|
|---|
| 6 | password=`cat /home/fact/DataCheck/.pw`
|
|---|
| 7 | if [ "$password" == "" ]
|
|---|
| 8 | then
|
|---|
| 9 | echo "please insert password in .pw file"
|
|---|
| 10 | fi
|
|---|
| 11 |
|
|---|
| 12 | # setup to use ftools
|
|---|
| 13 | export HEADAS=/opt/heasoft-6.11/x86_64-unknown-linux-gnu-libc2.13-0/
|
|---|
| 14 | source $HEADAS/headas-init.sh
|
|---|
| 15 |
|
|---|
| 16 | # get last 3 nights
|
|---|
| 17 | dates=( `date +%Y/%m/%d` `date +%Y/%m/%d --date="-1day"` `date +%Y/%m/%d --date="-2day"` `date +%Y/%m/%d --date="-3day"` )
|
|---|
| 18 | dates=( `date +%Y/%m/%d --date="-1day"` )
|
|---|
| 19 |
|
|---|
| 20 | # do rsync for rawfiles of these dates
|
|---|
| 21 | for date in ${dates[@]}
|
|---|
| 22 | do
|
|---|
| 23 | echo "" >> $logfile 2>&1
|
|---|
| 24 | echo "" >> $logfile 2>&1
|
|---|
| 25 | echo "" >> $logfile 2>&1
|
|---|
| 26 | rawdir=/loc_data/raw/$date
|
|---|
| 27 | runnumber=`echo $date | sed -e 's/\///g'`
|
|---|
| 28 | echo `date`": processing files in "$rawdir >> $logfile
|
|---|
| 29 | # check if data are available from that night
|
|---|
| 30 | if ! [ -d $rawdir ]
|
|---|
| 31 | then
|
|---|
| 32 | echo `date`": no data available in "$rawdir >> $logfile
|
|---|
| 33 | continue
|
|---|
| 34 | fi
|
|---|
| 35 |
|
|---|
| 36 | # find all fits-files starting with the oldest file
|
|---|
| 37 | echo `date`": finding files to be zipped in $rawdir..." >> $logfile
|
|---|
| 38 | fitsfiles=`find $rawdir -type f -name '*.fits'| sort `
|
|---|
| 39 |
|
|---|
| 40 | # loop to zip files
|
|---|
| 41 | echo `date`": zipping files in $rawdir..." >> $logfile
|
|---|
| 42 | for file in $fitsfiles
|
|---|
| 43 | do
|
|---|
| 44 | # check if raw file was accessed in the last 30 minutes
|
|---|
| 45 | isnew=`find $file -amin -30`
|
|---|
| 46 | if [ "$isnew" != "" ]
|
|---|
| 47 | then
|
|---|
| 48 | echo $file" is not older than 30 min => continue" >> $logfile
|
|---|
| 49 | continue
|
|---|
| 50 | fi
|
|---|
| 51 |
|
|---|
| 52 | # check if it is drs file
|
|---|
| 53 | isnew=`ls $file | grep drs`
|
|---|
| 54 | if [ "$isnew" != "" ]
|
|---|
| 55 | then
|
|---|
| 56 | echo $file" is a drs file => continue" >> $logfile
|
|---|
| 57 | continue
|
|---|
| 58 | fi
|
|---|
| 59 |
|
|---|
| 60 | # check if file is already finished
|
|---|
| 61 | # original file on daq (if data was taken on daq
|
|---|
| 62 | origfile=`echo $file | sed -e 's/loc_data/daq/'`
|
|---|
| 63 | if [ -e $origfile ]
|
|---|
| 64 | then
|
|---|
| 65 | # get time of last modification as seconds since Epoch for both files
|
|---|
| 66 | timeorig=`stat -c %Y $origfile`
|
|---|
| 67 | timecopy=`stat -c %Y $file`
|
|---|
| 68 | # compare times
|
|---|
| 69 | if ! [ $timeorig -eq $timecopy ]
|
|---|
| 70 | then
|
|---|
| 71 | # if times are not the same, the file is still open => no zip
|
|---|
| 72 | echo `date`": file "$file" not yet closed => continue" >> $logfile
|
|---|
| 73 | continue
|
|---|
| 74 | fi
|
|---|
| 75 | else
|
|---|
| 76 | # if the origfile doesn't exist, the data was probably written not on daq but on data
|
|---|
| 77 | echo `date`": file "$file" was probably taken on data and not daq " >> $logfile
|
|---|
| 78 | fi
|
|---|
| 79 |
|
|---|
| 80 | # get md5sum
|
|---|
| 81 | md5sum=`md5sum $file | cut -d' ' -f1`
|
|---|
| 82 | zipfile=`echo $file | sed -e 's/raw/zipraw/' -e 's/fits/fits.gz/'`
|
|---|
| 83 | md5sumzip=`md5sum $zipfile | cut -d' ' -f1`
|
|---|
| 84 |
|
|---|
| 85 | # get and check run and file number
|
|---|
| 86 | runnumbererror="no"
|
|---|
| 87 | numbererror="no"
|
|---|
| 88 | numberfromname=`echo $file | grep -E -o '20[1-9][0-9][01][0-9][0-3][0-9]_[0-9]{3}'`
|
|---|
| 89 | runnumberfromname=`echo $file | grep -E -o '20[1-9][0-9][01][0-9][0-3][0-9]'`
|
|---|
| 90 | 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]'`
|
|---|
| 91 | filenumberfromfileorig=`/home/fact/FACT++/fitsdump -h -t Events $file 2>/dev/null | grep RUNID | grep -E -o '[0-9]{1,3}'`
|
|---|
| 92 | filenumberfromfile=`printf %03d $filenumberfromfileorig`
|
|---|
| 93 | numberfromfile=$runnumberfromfile"_"$filenumberfromfile
|
|---|
| 94 | if [ "$runnumber" != "$runnumberfromname" ]
|
|---|
| 95 | then
|
|---|
| 96 | runnumbererror="yes"
|
|---|
| 97 | fi
|
|---|
| 98 | if [ "$numberfromfile" != "$numberfromname" ]
|
|---|
| 99 | then
|
|---|
| 100 | numbererror="yes"
|
|---|
| 101 | fi
|
|---|
| 102 |
|
|---|
| 103 | # get other variables from header
|
|---|
| 104 | runtype=`/home/fact/FACT++/fitsdump -h -t Events $file 2>/dev/null | grep RUNTYPE | grep -E -o "['][a-z-]+[']" | sed -e "s/'//g"`
|
|---|
| 105 | roi=`/home/fact/FACT++/fitsdump -h -t Events $file 2>/dev/null | grep NROI | grep -v NROITM | grep -E -o '[0-9]{1,4}'`
|
|---|
| 106 | numevents=`/home/fact/FACT++/fitsdump -h -t Events $file 2>/dev/null | grep Events | grep -E -o '[0-9]+'`
|
|---|
| 107 |
|
|---|
| 108 | # check if fits file is corrupted
|
|---|
| 109 | fitsfileerror="no"
|
|---|
| 110 | checkfitsfile=`fverify $file | grep '0 error(s)'`
|
|---|
| 111 | if [ "$checkfitsfile" == "" ]
|
|---|
| 112 | then
|
|---|
| 113 | fitsfileerror="yes"
|
|---|
| 114 | fi
|
|---|
| 115 | echo "summary for file"$file
|
|---|
| 116 | echo " errors: "$fitsfileerror
|
|---|
| 117 | echo " runnumber error: "$runnumbererror
|
|---|
| 118 | echo " number error: "$numbererror
|
|---|
| 119 | echo " roi: "$roi
|
|---|
| 120 | echo " runtype: "$runtype
|
|---|
| 121 | echo " numevents: "$numevents
|
|---|
| 122 | echo " md5sum: "$md5sum
|
|---|
| 123 | echo " md5sum(zip): "$md5sumzip
|
|---|
| 124 | # check wehter entry has been made (status table)
|
|---|
| 125 | # get runtype
|
|---|
| 126 | query2="SELECT fRunTypeKEY FROM RunType WHERE fRunTypeName='"$runtype"'"
|
|---|
| 127 | echo "Q2:"$query2
|
|---|
| 128 | result2=( `/usr/bin/mysql -u operator --host=fact01.fact.local --password=$password FactData -e "$query2"` )
|
|---|
| 129 | if [ ${#result2} -eq 0 ]
|
|---|
| 130 | then
|
|---|
| 131 | echo "Could not query fRunTypeKey for runtype "$runtype
|
|---|
| 132 | exit
|
|---|
| 133 | fi
|
|---|
| 134 | # check if entry already exists
|
|---|
| 135 | query3="SELECT fRunNumber FROM RunInfo WHERE fRunNumber="$runnumber" AND fFileNumber="$filenumberfromfileorig
|
|---|
| 136 | echo "Q3:"$query3
|
|---|
| 137 | resul3=`/usr/bin/mysql -u operator --host=fact01.fact.local --password=$password FactData -e "$query3"`
|
|---|
| 138 | if [ "$result3" == "" ]
|
|---|
| 139 | then
|
|---|
| 140 | query4="INSERT"
|
|---|
| 141 | querymid=" fRunNumber="$runnumber", fFileNumber="$filenumberfromfileorig", "
|
|---|
| 142 | else
|
|---|
| 143 | query4="UPDATE"
|
|---|
| 144 | querystop=" WHERE fRunNumber="$runnumber" AND fFileNumber="$filenumberfromfileorig
|
|---|
| 145 | fi
|
|---|
| 146 | query4=$query4" RunInfo SET "$querymid" fRunTypeKey="${result2[1]}", fNumEvents="$numevents", fROI="$roi
|
|---|
| 147 | query4=$query4", fMd5sumRaw='"$md5sum"', fMd5sumRawZip='"$md5sumzip"'"
|
|---|
| 148 | echo "Q4:"$query4
|
|---|
| 149 | if ! /usr/bin/mysql -u operator --host=fact01.fact.local --password=$password FactData -e "$query4"
|
|---|
| 150 | then
|
|---|
| 151 | echo "insert/update of "$numfromfile" to mysql"
|
|---|
| 152 | fi
|
|---|
| 153 | exit
|
|---|
| 154 | done
|
|---|
| 155 | done
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|