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
[ ${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
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 )