#!/bin/bash # option whether to fill all row or only those where information is missing # $doupdate might be given as environment variable if [ "$doupdate" = "" ] then doupdate="yes" # update all entries (needed when new fields have been added) doupdate="no" # fill only entries which are not yet existing (default) fi source `dirname $0`/../Sourcefile.sh printprocesslog "INFO starting $0 with option doupdate="$doupdate logfile=$runlogpath"/FillAuxTemp-"$datetime".log" date >> $logfile # setup to use ftools source $HEADAS/headas-init.sh # check if software is available if ! ls $factpath/fitsdump >/dev/null 2>&1 then printprocesslog "ERROR "$factpath"/fitsdump is not available." finish fi # get dates if [ "$certaindate" != "" ] then getdates $certaindate echo `date`": processing the following night(s): "${dates[@]} else # get all night #getdates "all" # get last 6 nights getdates 6 fi printprocesslog "INFO processing the following night(s): "${dates[@]} echo `date`": processing the following night(s): "${dates[@]} >> $logfile 2>&1 cd $mars # do filling of aux data for date in ${dates[@]} do auxdir=$auxdata/$date runnumber=`echo $date | sed -e 's/\///g'` if [ $runnumber -lt 20120328 ] then continue fi # check if aux files are available from that night if ! [ -d $auxdir ] then printprocesslog "INFO no data available in "$auxdir continue else printprocesslog "INFO processing files in "$auxdir fi # get file numbers from DB # but only for not-corrupted files query="SELECT fRunID from RunInfo WHERE fNight="$runnumber" AND fFitsFileErrors=0 " if [ "$doupdate" = "no" ] then query=$query" AND ISNULL(fCameraTempMean) " fi printprocesslog "DEBUG get filenumbers from DB: QUERY: "$query filenumbers=( `sendquery $query` ) # proceed only if there are files available if [ ${#filenumbers} -eq 0 ] then printprocesslog "INFO No files found in the DB for night "$date continue fi magicweatherfile=$auxdir/$runnumber.MAGIC_WEATHER_DATA.fits if ! [ -e $magicweatherfile ] then printprocesslog "WARN "$magicweatherfile" not found." else weathernumerrors=`fverify $magicweatherfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'` if [ $weathernumerrors -gt 0 ] then printprocesslog "WARN for $magicweatherfile fverify returned "$weathernumerrors" error(s)." fi fi # fill auxiliary information for files for filenum in ${filenumbers[@]} do printprocesslog "INFO processing file number "$runnumber"_"`printf %03d $filenum` echo `date`": processing file number "$runnumber"_"`printf %03d $filenum` >> $logfile 2>&1 # get information from rawfile rawfile=$ziprawdata/$date/$runnumber"_"`printf %03d $filenum`.fits.fz if ! [ -e $rawfile ] then if [ $runnumber -lt $checknight ] then printprocesslog "WARN "$rawfile" not found." else printprocesslog "INFO "$rawfile" not found." fi printprocesslog "INFO getting information from DB for "$rawfile # get information from DB query="SELECT Mjd(fRunStart)-40587 FROM RunInfo WHERE fNight="$runnumber" AND fRunID="$filenum tstart=`sendquery` query="SELECT Mjd(fRunStop)-40587 FROM RunInfo WHERE fNight="$runnumber" AND fRunID="$filenum tstop=`sendquery` query="SELECT Mjd(fRunStart)-40587-0.00011574 FROM RunInfo WHERE fNight="$runnumber" AND fRunID="$filenum tstart2=`sendquery` query="SELECT Mjd(fRunStop)-40587-0.00011574 FROM RunInfo WHERE fNight="$runnumber" AND fRunID="$filenum tstop2=`sendquery` if [ "$tstart" = "" ] || [ "$tstop" = "" ] || [ "$runtype" = "n/a" ] then printprocesslog "WARN: "$rawfile" not available and one of the following information empty in DB: fRunStart("$tstart"), fRunStop("$tstop"), fRunTypeKey("$runtype") " continue fi else #checkfitsfile=`fverify $rawfile 2>/dev/null | grep '0 error(s)'` #if [ "$checkfitsfile" == "" ] #then # numfitserrors=1 # printprocesslog "WARN: "$rawfile" probably corrupted." # continue #fi mjdrefraw=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'MJDREF' | grep -E -o '[0-9]{5}'` tstarti=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTI' | grep -E -o '[0-9]{5}'` tstartf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTF' | grep -E -o '[0-9][.][0-9]+([E][\-][0-9][0-9])?' | sed -e 's/[E]+*/\\*10\\^/'` tstopi=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPI' | grep -E -o '[0-9]{5}'` tstopf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPF' | grep -E -o '[0-9][.][0-9]+([E][\-][0-9][0-9])?' | sed -e 's/[E]+*/\\*10\\^/'` if [ "$tstarti" == "" ] || [ "$tstopi" == "" ] || [ "$tstartf" == "" ] || [ "$tstopf" == "" ] then printprocesslog "WARN: "$rawfile": one of the following keywords is empty or 0: TSTARTI TSTARTF TSTOPI TSTOPF " continue fi # assuming that at least TSTARTI and TSTOPI are consistent if [ $tstarti -gt 30000 ] then tstart=`echo " $tstarti + $tstartf - 40587 " | bc -l` tstart2=`echo " $tstarti + $tstartf - 40587 - 0.00011574 " | bc -l` # 10 sec #tstart2=`echo " $tstarti + $tstartf - 40587 - 0.000023148 " | bc -l` # 2 sec tstop=`echo " $tstopi + $tstopf - 40587 " | bc -l` else tstart=`echo " $tstarti + $tstartf " | bc -l` tstart2=`echo " $tstarti + $tstartf - 0.00011574 " | bc -l` # 10 sec #tstart2=`echo " $tstarti + $tstartf - 0.000023148 " | bc -l` # 2 sec tstop=`echo " $tstopi + $tstopf " | bc -l` fi #echo `date`": processing file number "$runnumber"_"`printf %03d $filenum` fi # build query to update runinfo in DB query="UPDATE RunInfo SET " # get information from weather: T if [ -e $magicweatherfile ] && [ $weathernumerrors -eq 0 ] then #echo $magicweatherfile" "$tstart" "$tstop printprocesslog "DEBUG root -q -b -l fact/processing/magicweather.C\("\"$magicweatherfile\""\,"$tstart"\,"$tstop"\)" mtemps=( `root -q -b -l fact/processing/magicweather.C\("\"$magicweatherfile\""\,$tstart\,$tstop\) | grep "result" | grep -E -o '[-]?[0-9]+[.]?[0-9]*'` ) check1=$? case $check1 in 0) printprocesslog "INFO got info from "$magicweatherfile ;; *) printprocesslog "WARN getting info from "$magicweatherfile" failed (check1="$check1")." ;; esac #echo ${mtemps[@]} if [ "${mtemps[0]}" == "" ] then query=$query"fOutsideTempMean=NULL" else query=$query"fOutsideTempMean="${mtemps[0]} fi if [ "${mtemps[1]}" == "" ] then query=$query", fOutsideTempRms=NULL" else query=$query", fOutsideTempRms="${mtemps[1]} fi if [ "${mtemps[2]}" == "" ] then query=$query", fHumidityMean=NULL" else query=$query", fHumidityMean="${mtemps[2]} fi if [ "${mtemps[3]}" == "" ] then query=$query", fHumidityRms=NULL" else query=$query", fHumidityRms="${mtemps[3]} fi if [ "${mtemps[4]}" == "" ] then query=$query", fAirPressureMean=NULL" else query=$query", fAirPressureMean="${mtemps[4]} fi if [ "${mtemps[5]}" == "" ] then query=$query", fAirPressureRms=NULL" else query=$query", fAirPressureRms="${mtemps[5]} fi if [ "${mtemps[6]}" == "" ] then query=$query", fDewPointMean=NULL" else query=$query", fDewPointMean="${mtemps[6]} fi if [ "${mtemps[7]}" == "" ] then query=$query", fDewPointRms=NULL" else query=$query", fDewPointRms="${mtemps[7]} fi if [ "${mtemps[8]}" == "" ] then query=$query", fWindSpeedMean=NULL" else query=$query", fWindSpeedMean="${mtemps[8]} fi if [ "${mtemps[9]}" == "" ] then query=$query", fWindSpeedRms=NULL" else query=$query", fWindSpeedRms="${mtemps[9]} fi if [ "${mtemps[10]}" == "" ] then query=$query", fWindGustMean=NULL" else query=$query", fWindGustMean="${mtemps[10]} fi if [ "${mtemps[119]}" == "" ] then query=$query", fWindGustRms=NULL" else query=$query", fWindGustRms="${mtemps[11]} fi else query=$query"fOutsideTempMean=NULL" query=$query", fOutsideTempRms=NULL" query=$query", fHumidityMean=NULL" query=$query", fHumidityRms=NULL" query=$query", fAirPressureMean=NULL" query=$query", fAirPressureRms=NULL" query=$query", fDewPointMean=NULL" query=$query", fDewPointRms=NULL" query=$query", fWindSpeedMean=NULL" query=$query", fWindSpeedRms=NULL" query=$query", fWindGustMean=NULL" query=$query", fWindGustRms=NULL" fi # add where condition query=$query" WHERE fNight="$runnumber" AND fRunID="$filenum # send query to DB sendquery >/dev/null done done finish