-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnrs.sh
executable file
·354 lines (293 loc) · 13.3 KB
/
gnrs.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/bin/bash
#########################################################################
# GNRS core application
#
# Purpose: Standardizes spellings of political division names to
# geonames.org
#
# Usage: ./gnrs.sh
#
# Requires:
# 1. GNRS database (create separately with module gnrs_db.sh)
# 2. Input data already present in GNRS database. Reads input
# from table observation and updates results to this same table.
#
# Authors: Brad Boyle ([email protected])
#########################################################################
# Comment-block tags - Use for all temporary comment blocks
#### TEMP ####
# echo "WARNING: portions of script `basename "$BASH_SOURCE"` commented out!"
## Other temporary code to be executed before comment block
## Start comment block
# : <<'COMMENT_BLOCK_xxx'
## End comment block
# COMMENT_BLOCK_xxx
## Temporary code to be executed after comment block
#### TEMP ####
## Exit all scripts
# echo "EXITING script `basename "$BASH_SOURCE"`"; exit 0
######################################################
# Set basic parameters, functions and options
######################################################
# Get working directory
DIR="$(dirname ${BASH_SOURCE[0]})"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
# Start logfile
export glogfile="$DIR/log/logfile_"$master".txt"
mkdir -p "$DIR/log"
touch $glogfile
# Set includes directory path, relative to $DIR
includes_dir=$DIR"/includes"
# Load parameters file
source "$DIR/params.sh"
# Load db configuration params
source "$db_config_path/db_config.sh"
# Load functions
source "$includes_dir/functions.sh"
# Set local directories to same as main
data_dir_local=$data_base_dir
data_dir=$data_base_dir
DIR_LOCAL=$DIR
# Set current script as master if not already source by another file
# master = name of this file.
# Tells sourced scripts not to reload general parameters and command line
# options as they are being called by another script. Allows component
# scripts to be called individually if needed
if [ -z ${master+x} ]; then
master=`basename "$0"`
fi
###########################################################
# Get custom options
# -a api call=true
###########################################################
# back up existing value of $e
if [ ! -z ${e+x} ]; then
e_bak=$e
fi
# Set defaults
api="false" # Assume not an api call
pgpassword="" # Not needed if not an api call
silent="false" # Assume echo & interactive are on
threshold=$DEF_MATCH_THRESHOLD # Use default (from params.sh)
threshold_type="default"
while [ "$1" != "" ]; do
case $1 in
-a | --api ) api="true"
;;
-t | --threshold ) shift
threshold=$1
threshold_type="custom"
;;
-s | --silent ) silent="true"
;;
-j | --job ) shift
job=$1
;;
* ) echo "invalid option: $1 ($local)"; exit 1
esac
shift
done
# Check fuzzy match threshold set correctly
if [[ $threshold =~ ^[+-]?[0-9]+$ ]] || [[ $threshold =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]; then
# Value is integer or float; check in range [0:1]
if [ $(echo "($threshold < 0) || ($threshold > 1)" | bc -l) -eq 1 ]; then
echo "ERROR: Match threshold not in range [0:1]"; exit 1
fi
else
echo "ERROR: Match threshold not a number"; exit 1
fi
# Set PGPASSWORD for api access
# Parameter $pgpwd set in config file
if [ "$api" == "true" ]; then
pgpassword="PGPASSWORD=$pgpwd"
fi
# Set echo parameter, if applicable
if [ "$silent" == "true" ]; then
e="false"
else
e="true"
fi
# Start timer if previous time not sent by calling script
if [ master=`basename "$0"` ]; then
source "$includes_dir/start_time.sh"
else
prev=`date +%s%N`
fi
#########################################################################
# Main
#########################################################################
# Detect political division submitted
# Flag bad records to omit from further processing
echoi $e -n "- Flagging political divisions submitted..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/detect_poldiv_submitted.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
# Populate alternate verbatim state and county columns
# Values are stripped of admin category identifiers such as "State",
# "Department", "Municipio de", etc.
echoi $e -n "- Populating alt state_province_verbatim column..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/populate_sp_verbatim_alt.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
# Populate alternate verbatim state and county columns
# Values are stripped of admin category identifiers such as "State",
# "Department", "Municipio de", etc.
echoi $e -n "- Populating alt county_parish_verbatim_verbatim column..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/populate_cp_verbatim_alt.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
############################################
# Check against existing results in cache
############################################
# Start by assuming all records not in cache
not_cached="t"
if [ "$threshold_type" == "default" ]; then
# Only check cache if using default match threshold
echoi $e -n "Checking for existing results in cache..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/check_cache.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
# If all records already in cache, skip resolution
sql_not_cached="SELECT EXISTS ( SELECT id FROM user_data WHERE job='$job' AND match_status IS NULL) AS a"
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs -qt -c \"$sql_not_cached\" | tr -d '[[:space:]]'"
not_cached=$(eval "$cmd")
fi
######## For testing only ########
if [ "$debug_mode" == "t" ]; then
curruser=$(whoami)
# if [ "$curruser" == "www-data" ]; then
# zz_dir="/tmp/gnrs"
# else
# zz_dir="../data/user"
# fi
zz_dir=$data_dir_local_abs
echo "Current user: $curruser ($local)" > $zz_dir/zz_gnrs_options.txt
echo "e: $e" >> $zz_dir/zz_gnrs_options.txt
echo "silent: $silent" >> $zz_dir/zz_gnrs_options.txt
echo "api: $api" >> $zz_dir/zz_gnrs_options.txt
echo "DB: $db_gnrs" >> $zz_dir/zz_gnrs_options.txt
echo "DB user: $user" >> $zz_dir/zz_gnrs_options.txt
echo "pgpassword: $pgpassword" >> $zz_dir/zz_gnrs_options.txt
echo "sql_not_cached: $sql_not_cached" >> $zz_dir/zz_gnrs_options.txt
echo "cmd: $cmd" >> $zz_dir/zz_gnrs_options.txt
echo "not_cached: $not_cached" >> $zz_dir/zz_gnrs_options.txt
echo "job: $job" >> $zz_dir/zz_gnrs_options.txt
fi
######## END: For testing only ########
if [ "$not_cached" == "t" ]; then
# Process non-cached records
############################################
# Resolve political divisions & summarize
############################################
echoi $e "Resolving political divisions:"
echoi $e "- Country:"
echoi $e -n "-- exact..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/resolve_country_exact.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e -n "-- fuzzy..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v match_threshold=$threshold -v job=$job -f $DIR_LOCAL/sql/resolve_country_fuzzy.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e "- Country-as-state:"
echoi $e -n "-- exact..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/resolve_countryasstate_exact.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e -n "-- fuzzy..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -v match_threshold=$threshold -f $DIR_LOCAL/sql/resolve_countryasstate_fuzzy.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e "- State/province:"
echoi $e -n "-- exact..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/resolve_sp_exact.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e -n "-- fuzzy..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v match_threshold=$threshold -v job=$job -f $DIR_LOCAL/sql/resolve_sp_fuzzy.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e "- State-as-country:"
echoi $e -n "-- exact..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/resolve_stateascountry_exact.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e -n "-- fuzzy..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -v match_threshold=$threshold -f $DIR_LOCAL/sql/resolve_stateascountry_fuzzy.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e "- County/parish:"
echoi $e -n "-- exact..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/resolve_cp_exact.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e -n "-- fuzzy..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v match_threshold=$threshold -v job=$job -f $DIR_LOCAL/sql/resolve_cp_fuzzy.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e "- State-as-county:"
echoi $e -n "-- exact..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/resolve_stateascounty_exact.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e -n "-- fuzzy..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -v match_threshold=$threshold -f $DIR_LOCAL/sql/resolve_stateascounty_fuzzy.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e "- County-as-state:"
echoi $e -n "-- exact..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/resolve_countyasstate_exact.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e -n "-- fuzzy..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -v match_threshold=$threshold -f $DIR_LOCAL/sql/resolve_countyasstate_fuzzy.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e -n "- Summarizing results..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/summarize.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
else
# Nothing to process, all submitted values already in cache
echoi $e "- All submitted political divisions already in cache!"
fi
############################################
# Populate supplementary fields
############################################
echoi $e -n "Populating ISO codes..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/iso_codes.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e -n "Populating gadm IDs..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/gadm_ids.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e -n "Populating geonames IDs..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/geonames_ids.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
echoi $e -n "Updating match scores..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/update_match_scores.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
############################################
# Updating cache
############################################
# Add new results to cache
# Only do if use default match threshold
if [ "$threshold_type" == "default" ]; then
echoi $e -n "Updating cache..."
cmd="$pgpassword PGOPTIONS='--client-min-messages=warning' psql -U $user -d $db_gnrs --set ON_ERROR_STOP=1 -q -v job=$job -f $DIR_LOCAL/sql/update_cache.sql"
eval $cmd
source "$DIR/includes/check_status.sh"
fi
######################################################
# Report total elapsed time and exit if running solo
######################################################
# Restore previous value of $e, if applicable
if [ ! -z ${e_bak+x} ]; then
e=$e_bak
fi
######################################################
# End script
######################################################