forked from MaskedBit/aviationCharts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunzip_and_normalize.sh
executable file
·120 lines (95 loc) · 3.87 KB
/
unzip_and_normalize.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
# set -o xtrace
IFS=$(printf '\n\t') # IFS is newline or tab
main() {
# Get the number of function parameters
local -r NUMARGS=$#
# Get the base directory of where source charts are
local -r chartsRoot=$(readlink -f "$1")
# Set our destination directories
local -r UNZIP_DESTINATION_ABSOLUTE_PATH=$(readlink -f "${chartsRoot}/1_all_tifs/")
local -r NORMALIZED_FILE_DESTINATION_ABSOLUTE_PATH=$(readlink -f "${chartsRoot}/2_normalized/")
# Unzip every .tif in every .zip, overwriting older files when needed
unzip_freshen "${chartsRoot}" "${UNZIP_DESTINATION_ABSOLUTE_PATH}"
# copy/move/link georeferenced tifs another directory with normalized names
normalize "${UNZIP_DESTINATION_ABSOLUTE_PATH}" "${NORMALIZED_FILE_DESTINATION_ABSOLUTE_PATH}"
}
unzip_freshen() {
# Get the number of function parameters
local -r NUMARGS=$#
# Validate number of function parameters
if [ "$NUMARGS" -ne 2 ] ; then
echo "Bad unzip parameters"
fi
local -r chartsRoot="$1"
local -r UNZIP_DESTINATION_ABSOLUTE_PATH="$2"
mkdir --parents "${UNZIP_DESTINATION_ABSOLUTE_PATH}"
# Unzip any .tif file in any .zip file in the supplied directory
echo "Unzipping all .zip files under ${chartsRoot} to ${UNZIP_DESTINATION_ABSOLUTE_PATH}"
find "${chartsRoot}" \
-type f \
-iname "*.zip" \
-exec unzip -uo -j -d "${UNZIP_DESTINATION_ABSOLUTE_PATH}" "{}" "*.tif" \;
}
normalize() {
# Get the number of function parameters
local -r NUMARGS=$#
# Validate number of function parameters
if [ "$NUMARGS" -ne 2 ] ; then
echo "Bad number of normalize parameters"
fi
local -r UNZIP_DESTINATION_ABSOLUTE_PATH="$1"
local -r NORMALIZED_FILE_DESTINATION_ABSOLUTE_PATH="$2"
# Where we'll put normalized files
mkdir --parents "${NORMALIZED_FILE_DESTINATION_ABSOLUTE_PATH}"
# All of the .tif files in the source directory
local -r CHART_ARRAY=("${UNZIP_DESTINATION_ABSOLUTE_PATH}/*.tif")
echo "Normalize and copy"
for SOURCE_CHART_ABSOLUTE_NAME in ${CHART_ARRAY[@]}
do
# Does this file have georeference info?
if gdalinfo "$SOURCE_CHART_ABSOLUTE_NAME" -noct | grep -q -P 'PROJCS'
then
# Replace non-alpha characters with _ and
# then strip off the series number and add .tif back on
local SANITIZED_CHART_NAME_WITHOUT_VERSION=($(basename $SOURCE_CHART_ABSOLUTE_NAME |
sed --regexp-extended 's/\W+/_/g' |
sed --regexp-extended 's/_[0-9]+_tif$/\.tif/ig' |
sed --regexp-extended 's/_tif$/\.tif/ig'))
# echo "Latest $SOURCE_CHART_ABSOLUTE_NAME, calling it $SANITIZED_CHART_NAME_WITHOUT_VERSION"
# Copy/move/link this file if it's newer than what is already there
mv \
--update \
--verbose \
"$SOURCE_CHART_ABSOLUTE_NAME" \
"${NORMALIZED_FILE_DESTINATION_ABSOLUTE_PATH}/${SANITIZED_CHART_NAME_WITHOUT_VERSION}"
fi
done
echo "Finished Normalize and copy"
}
USAGE() {
echo "Unzip and normalize chart names"
echo "Usage: $PROGNAME <charts_root_directory> <normalized_directory>" >&2
exit 1
}
# The script begins here
# Set some basic variables
declare -r PROGNAME=$(basename "$0")
declare -r PROGDIR=$(readlink -m $(dirname "$0"))
declare -r ARGS="$@"
# Set fonts for Help.
declare -r NORM=$(tput sgr0)
declare -r BOLD=$(tput bold)
declare -r REV=$(tput smso)
#Get the number of remaining command line arguments
NUMARGS=$#
#Validate number of command line parameters
if [ "$NUMARGS" -ne 1 ] ; then
USAGE
fi
# Call the main routine
main "$@"
exit 0