X-Git-Url: https://git.wikimedia.ca/?p=eccc_to_commons.git;a=blobdiff_plain;f=eccc_to_commons.sh;h=f6d399b67fe8028ca26475d128d15a9c474e90f6;hp=c580fbbc6518c99ba474e034e8887a7b9b7c1386;hb=refs%2Fheads%2Fmaster;hpb=86faa15d147318dbfdabdaf1d04efb8a59ff7051 diff --git a/eccc_to_commons.sh b/eccc_to_commons.sh index c580fbb..f6d399b 100755 --- a/eccc_to_commons.sh +++ b/eccc_to_commons.sh @@ -42,20 +42,20 @@ do declare -i MINUTE_COUNT=$(xmlstarlet sel -t -v 'count(//stationdata[@minute])' "${REPLY}") declare -i AL_MONTH_COUNT=$(xmlstarlet sel -t -v 'count(//month)' "${REPLY}") - # Detect station id - declare -i LAST_DIR=$(basename $(dirname "${REPLY}")) - declare -i FILENAME=$(basename "${REPLY%.*}") + # Detect climate id + declare LAST_DIR=$(basename $(dirname "${REPLY}")) + declare FILENAME=$(basename "${REPLY%.*}") - if [ "${LAST_DIR}" -gt 0 ] + if [[ "${LAST_DIR}" =~ ^[A-Z0-9]{7}$ ]] then - # Station id is in last directory name - STATION_ID="${LAST_DIR}" - elif [ "${FILENAME}" -gt 0 ] + # Climate id is in last directory name + CLIMATE_ID="${LAST_DIR}" + elif [[ "${FILENAME}" =~ ^[A-Z0-9]{7}$ ]] then - # Station id is in file name - STATION_ID="${FILENAME}" + # Climate id is in file name + CLIMATE_ID="${FILENAME}" else - echo "${REPLY}: Cannot detect station id" + echo "${REPLY}: Cannot detect climate id" exit 1 fi @@ -88,13 +88,20 @@ do [ ${MINUTE_COUNT} -eq 0 ] then STYLESHEET_PATH='monthly_to_commons.xslt' - DESTINATION_PATH="${DESTINATION}/weather.gc.ca/Monthly/${STATION_ID}.tab" + DESTINATION_PATH="${DESTINATION}/weather.gc.ca/Monthly/${CLIMATE_ID}.tab" elif [ ${STATIONDATA_COUNT} -eq 0 ]&& \ [ ${AL_MONTH_COUNT} -gt 0 ] then - echo "${REPLY}: Almanac data not compatible yet. Ignoring." >&2 - continue - DESTINATION_PATH="${DESTINATION}/weather.gc.ca/Almanac/${STATION_ID}.tab" + # Check file contains data + declare -i AL_DAY_COUNT=$(xmlstarlet sel -t -v 'count(//day)' "${REPLY}") + if [ ${AL_DAY_COUNT} -eq 0 ] + then + echo "${REPLY}: No day found. Ignoring." >&2 + continue + fi + + STYLESHEET_PATH='almanac_to_commons.xslt' + DESTINATION_PATH="${DESTINATION}/weather.gc.ca/Almanac/${CLIMATE_ID}.tab" else echo "${REPLY}: Cannot detect file type. Exiting." exit 1 @@ -102,5 +109,5 @@ do echo "Processing ${REPLY} to ${DESTINATION_PATH}…" >&2 mkdir -p "$(dirname "${DESTINATION_PATH}")" - xmlstarlet tr "${STYLESHEET_PATH}" "${REPLY}" > "${DESTINATION_PATH}" + xmlstarlet tr "${STYLESHEET_PATH}" "${REPLY}" | jq > "${DESTINATION_PATH}" done 9< <( find "${SOURCE}" -type f -name '*.xml' -print0 )