Ignore:
Timestamp:
09/01/21 07:56:42 (3 years ago)
Author:
maslowski
Message:
Added better error handeling and tracking in the databas.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/hawc/processing/DiskToDB/run-scripts.sh

    r20093 r20103  
    7777readonly DATE=`date +%Y%m%d_%H%M`
    7878readonly LOG="./log/${SCRPTN}_${DATE}.log"
    79 exec 3>&1 1>>${LOG}
    80 exec 4>>${LOG}
    81 # exec 3>&1
    82 # exec 4>&1
     79# exec 3>&1 1>>${LOG}
     80# exec 4>>${LOG}
     81exec 3>&1
     82exec 4>&1
    8383
    8484echo "Start processing data"
     
    8888        # Delete entries form table ${3} for a given Telescope and Night
    8989        # Arguments: Credentials Telescope Table Night
    90         mysql --defaults-file="${1}" \
     90        local cred="${1}"
     91        local tel="${2}"
     92        local table="${3}"
     93        local night="${4}"
     94
     95        mysql --defaults-file="${cred}" \
    9196                --skip-column-names \
    9297                --batch \
    9398                --raw \
    9499                --compress \
    95                 -e "DELETE FROM ${3} WHERE Telescope = ${2} AND NIGHT = ${4};"
     100                -e "DELETE FROM ${table} WHERE Telescope = ${tel} \
     101                AND NIGHT = ${night};"
    96102}
    97103
    98104function SetStatus()
    99105{
    100         # Query with process update
    101         # Arguments: Credentials Telescope Status Night
    102         mysql --defaults-file="${1}" \
     106        # Status update for one Column, Night and Telescope
     107        # Arguments: Credentials Telescope Column Value Night
     108        local cred="${1}"
     109        local tel="${2}"
     110        local col="${3}"
     111        local val="${4}"
     112        local night="${5}"
     113
     114        # Check if a valid Column was selected
     115        if [ ${col} != "header" ] \
     116                && [ ${col} != "calibration" ] \
     117                && [ ${col} != "auxiliary" ] \
     118                && [ ${col} != "callisto" ] \
     119                && [ ${col} != "star" ]
     120        then
     121                echo "ERROR: ${col} is not a valid column."
     122                exit 1
     123        fi
     124
     125        mysql --defaults-file="${cred}" \
    103126                --skip-column-names \
    104127                --batch \
    105128                --raw \
    106129                --compress \
    107                 -e "UPDATE DataOnDisk SET STATUS = ${3} WHERE NIGHT = ${4} \
    108                 AND Telescope = ${2};"
     130                -e "UPDATE DataOnDisk SET ${col} = ${val}\
     131                WHERE NIGHT = ${night} AND Telescope = ${tel};"
    109132}
    110133
    111134function GetStatus()
    112135{
    113         # Return status of Night from database
    114         # Arguments: Credentials Telescope Night
    115         local STATUS=`mysql --defaults-file="${1}" \
     136        # Return status of Night for a given Column and Telescope
     137        # Arguments: Credentials Telescope Column Night
     138        local cred="${1}"
     139        local tel="${2}"
     140        local col="${3}"
     141        local night="${4}"
     142
     143        # Check if a valid Column was selected
     144        if [ ${col} != "header" ] \
     145                && [ ${col} != "calibration" ] \
     146                && [ ${col} != "auxiliary" ] \
     147                && [ ${col} != "callisto" ] \
     148                && [ ${col} != "star" ]
     149        then
     150                echo "ERROR: ${col} is not a valid column."
     151                exit 1
     152        fi
     153
     154        local STATUS=`mysql --defaults-file="${cred}" \
    116155                --skip-column-names \
    117156                --batch \
    118157                --raw \
    119158                --compress \
    120                 -e "SELECT DISTINCT STATUS FROM DataOnDisk WHERE NIGHT = ${3} \
    121                 AND Telescope = ${2};"`
    122 
    123         if [ -z ${STATUS} ]
    124         then
    125                 echo "ERROR: Status does not exist for telescope: ${2} and night: ${3}" >&2
     159                -e "SELECT DISTINCT ${col} FROM DataOnDisk \
     160                WHERE NIGHT = ${night} \
     161                AND Telescope = ${tel} \
     162                AND ${col} IS NULL;"`
     163
     164        echo "${STATUS}"
     165}
     166
     167
     168function DoesDirExists()
     169{
     170        local thing="${1}"
     171        # Check if thing exists
     172        if [ ! -d "${thing}" ]
     173        then
     174                echo "${thing} is not a valid Path. Please change variable in script ${SCRPTN}."
    126175                exit 1
    127176        fi
    128 
    129         if [ `echo "${STATUS}" | wc -l` -ne "1" ]
    130         then
    131                 echo "ERROR: Not all status values are the same for the night: ${3}!" >&2
     177}
     178
     179function DoesFileExists()
     180{
     181        local thing="${1}"
     182        # Check if thing exists
     183        if [ ! -f "${thing}" ]
     184        then
     185                echo "${thing} is not a valid File. Please change variable in script ${SCRPTN}."
    132186                exit 1
    133187        fi
    134 
    135         echo "${STATUS}"
    136 }
     188}
     189
    137190
    138191# Define variables
     
    147200# Credentials for root2sql for star
    148201readonly STARRC='../root2sql-star.rc'
     202
     203# Check if variable paths point to something sensible
     204DoesDirExists "${DIR}"
     205DoesFileExists "${CRED}"
     206DoesFileExists "${CALLISTORC}"
     207DoesFileExists "${STARRC}"
     208
    149209# SQL outputs
    150210mkdir -p ./queries
     
    156216# Create Database of existing files
    157217# Create SQL query with:
    158 # (Telescope, Night, RUNID, ISDRSFILE, ISPROCESSED).
     218# (Telescope, Night, RUNID, ISDRSFILE).
    159219echo "Updating file table"
    160220bash ./update-file-table2.sh -i -o "${ONDISK}" "${TEL}" "${DIR}/raw"
     
    170230        --raw \
    171231        --compress \
    172         -e "SELECT DISTINCT NIGHT FROM DataOnDisk WHERE STATUS != 5 \
     232        -e "SELECT DISTINCT NIGHT FROM DataOnDisk \
     233        WHERE header IS NULL \
     234        OR calibration IS NULL \
     235        OR auxiliary IS NULL \
     236        OR callisto IS NULL \
     237        OR star IS NULL \
    173238        AND Telescope = ${TEL};"`
    174239
     
    182247echo "Updating database"
    183248
     249NIGHTS=20201111
     250
    184251echo "Nights to process:"
    185252echo "${NIGHTS}"
     
    191258for NIGHT in ${NIGHTS}
    192259do
    193         STATUS=`GetStatus ${CRED} ${TEL} ${NIGHT}`
    194         echo "Status = ${STATUS} for night = ${NIGHT}"
    195         echo " "
    196 
    197         if [ ${STATUS} -lt "1" ]
     260        HEAD_S=`GetStatus ${CRED} ${TEL} "header" ${NIGHT}`
     261        CALI_S=`GetStatus ${CRED} ${TEL} "calibration" ${NIGHT}`
     262        AUXI_S=`GetStatus ${CRED} ${TEL} "auxiliary" ${NIGHT}`
     263        CALL_S=`GetStatus ${CRED} ${TEL} "callisto" ${NIGHT}`
     264        STAR_S=`GetStatus ${CRED} ${TEL} "star" ${NIGHT}`
     265
     266        echo "header = ${HEAD_S} for night = ${NIGHT}"
     267        echo " "
     268
     269        # If header is null
     270        if [ ! -z ${HEAD_S} ] && [ ${HEAD_S} = "NULL" ]
    198271        then
    199272                # Get raw data headers
     
    205278                echo "Uploading raw headers for night: ${NIGHT}"
    206279                mysql --defaults-file="${CRED}" < "${RAW}"
    207                 SetStatus ${CRED} ${TEL} "1" ${NIGHT}
    208280                echo "Finished uploading raw headers for night: ${NIGHT}"
    209281                echo " "
    210282        fi
    211283
    212         if [ ${STATUS} -lt "2" ]
     284        echo "calibration = ${CALI_S} for night = ${NIGHT}"
     285        echo " "
     286
     287        # If header is null and or calibration is null
     288        if ([ ! -z ${HEAD_S} ] && [ ${HEAD_S} = "NULL" ]) \
     289                || ([ ! -z ${CALI_S} ] && [ ${CALI_S} = "NULL" ])
    213290        then
    214291                # Get calibration files
     
    219296                echo "Uploading calibration for night: ${NIGHT}"
    220297                mysql --defaults-file="${CRED}" < "${CALIB}"
    221                 SetStatus ${CRED} ${TEL} "2" ${NIGHT}
    222298                echo "Finished uploading calibration for night: ${NIGHT}"
    223299                echo " "
    224300        fi
    225301
    226         if [ ${STATUS} -lt "3" ]
     302        echo "auxiliary = ${AUXI_S} for night = ${NIGHT}"
     303        echo " "
     304
     305        # If header is null and or calibration is null and or auxiliary is null
     306        if ([ ! -z ${HEAD_S} ] && [ ${HEAD_S} = "NULL" ]) \
     307                || ([ ! -z ${CALI_S} ] && [ ${CALI_S} = "NULL" ]) \
     308                || ([ ! -z ${AUXI_S} ] && [ ${AUXI_S} = "NULL" ])
    227309        then
    228310                # Get aux data
     
    235317                echo "Uploading aux data for night: ${NIGHT}"
    236318                mysql --defaults-file="${CRED}" < "${AUX}"
    237                 SetStatus ${CRED} ${TEL} "3" ${NIGHT}
    238319                echo "Finished uploading aux data for night: ${NIGHT}"
    239320                echo " "
    240321        fi
    241322
    242         if [ ${STATUS} -lt "4" ]
     323        echo "callisto = ${CALL_S} for night = ${NIGHT}"
     324        echo " "
     325
     326        # If header is null and or calibration is null and or callisto is null
     327        if ([ ! -z ${HEAD_S} ] && [ ${HEAD_S} = "NULL" ]) \
     328                || ([ ! -z ${CALI_S} ] && [ ${CALI_S} = "NULL" ]) \
     329                || ([ ! -z ${CALL_S} ] && [ ${CALL_S} = "NULL" ])
    243330        then
    244331                # Get callisto data
     
    260347                        "${DIR}"/callisto \
    261348                        ${NIGHT}
    262                 SetStatus ${CRED} ${TEL} "4" ${NIGHT}
    263349                echo "Finished uploading callisto entries for night: ${NIGHT}"
    264350                echo " "
    265351        fi
    266352
    267         if [ ${STATUS} -lt "5" ]
     353        echo "star = ${STAR_S} for night = ${NIGHT}"
     354        echo " "
     355
     356        # If header is null and or calibration is null and or callisto is null
     357        # and or star is null
     358        if ([ ! -z ${HEAD_S} ] && [ ${HEAD_S} = "NULL" ]) \
     359                || ([ ! -z ${CALI_S} ] && [ ${CALI_S} = "NULL" ]) \
     360                || ([ ! -z ${CALL_S} ] && [ ${CALL_S} = "NULL" ]) \
     361                || ([ ! -z ${STAR_S} ] && [ ${STAR_S} = "NULL" ])
    268362        then
    269363                # Get star data
     
    281375                        "${DIR}"/star \
    282376                        ${NIGHT}
    283                 SetStatus ${CRED} ${TEL} "5" ${NIGHT}
    284377                echo "Finished uploading star entries for night: ${NIGHT}"
    285378                echo " "
Note: See TracChangeset for help on using the changeset viewer.