forked from ojalaquellueva/nsr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnsr_batch.php
More file actions
329 lines (272 loc) · 9.04 KB
/
nsr_batch.php
File metadata and controls
329 lines (272 loc) · 9.04 KB
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
<?php
///////////////////////////////////////////////////////////////////////////
/*
NSR batch processing service
For each combination of species+country+state_province+county_parish
returns an evaluation of native status
Usage:
php nsr_batch.php -f="PATH_TO_FILE/FILENAME" [-e=true*|false] [-i=true|false*] [-l=unix|mac*|win] [-t=csv*|tab] [-r=true|false*]
Ouput:
Tab-delimitted file, "FILENAME_nsr_results.txt", in same directory os input file
Input file columns:
family - optional
genus - optional, but required if species present
species - required; species or lower level taxon such as variety; NO AUTHORITY
country - required
state_province - optional, but required if county_parish present
county_parish - optional
user_id - optional; INTEGER UNSIGNED
*/
///////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////
// Parameters
//////////////////////////////////////////////////////
// Mode determines which database is used
$MODE = "batch";
// Generate unique code for this job
$job = date("Y-m-d-G:i:s").":".str_replace(".0","",strtok(microtime()," "));
#$job = '2018-02-03-8:50:42:0.69830700'; # Big BIEN job; save for now
// Get db connection parameters (in ALL CAPS)
include 'params.php';
include $CONFIG_DIR.'db_configw.php';
include $batch_includes_dir.'batch_params.php';
//////////////////////////////////////////////////////
// Options
//////////////////////////////////////////////////////
$shortopts = "";
$shortopts .= "e::"; // command line Echo (true/on;false/off [default])
$shortopts .= "i::"; // Interactive mode (true/on;false/off [default])
$shortopts .= "f::"; // File name, if provided will over-ride
// default name in params.php
$shortopts .= "d::"; // custom Data directory, if omitted defaults to
// value of $DATADIR set in params.php
$shortopts .= "t::"; // file Type (csv [default], tab)
$shortopts .= "l::"; // Line endings (mac [default], unix, win)
$shortopts .= "r::"; // Replace cache?
// false|keep [default]: keep cache
// replace: replace records for same taxon+locality
// delete: all records; clear entire cache
// get command line arguments, if any
$options = getopt($shortopts);
// Command line echo
$e=true;
$echo_on = true;
if (array_key_exists('e', $options)) {
$e = $options["e"];
if ($e === false || $e == 'false' || $e == 'off') {
$echo_on = false;
} else {
$echo_on = true;
}
}
// Interactive mode
$interactive = false;
if (array_key_exists('i', $options)) {
$i = $options["i"];
if ($i === true || $i == 'true' || $i == 'on') $interactive = true;
}
if ($interactive===true) $echo_on=true; // If interactive mode, echo MUST be on
// Data directory
$data_dir = $DATADIR; // Default as set in params.php
if (array_key_exists('d', $options)) {
// Over-ride default input file name from params file
$data_dir_custom = $options["d"];
if ($data_dir_custom<>false) {
if (file_exists($data_dir_custom)) {
$data_dir = $data_dir_custom;
} else {
// Bad directory
die("ERROR: Directory '$data_dir_custom' does not exist\r\n");
}
} else {
// No directory value supplied
die("ERROR: No value supplied for data directory option -d\r\n");
}
}
// Input file name
if (array_key_exists('f', $options)) {
// Over-ride default input file name from params file
$file = $options["f"];
if ($file<>false) $inputfilename = $file;
}
// Input file type
$filetype='csv';
if (array_key_exists('t', $options)) {
$t = $options["t"];
if($t == "tab") {
// tab-delimitted
// Otherwise assumes CSV, as set in params file
$fields_terminated_by = " FIELDS TERMINATED BY '\t' ";
$optionally_enclosed_by = " ";
$filetype='tab-delimited';
}
}
// Input file line endings
if (array_key_exists('l', $options)) {
// By default assumes unix line endings,
// as set in params file
$l = $options["l"];
switch ($l) {
case "mac":
// Mac
$lines_terminated_by = " LINES TERMINATED BY '\r' ";
break;
case "win":
// Windows
$lines_terminated_by = " LINES TERMINATED BY '\r\n' ";
break;
}
}
// parameters $inputfilename and $resultsfilename in
// batch_params.php, but can
$inputfile = $data_dir.$inputfilename;
// Set name of result file based on input file
$pizza = explode('.',$inputfilename);
if (count($pizza)>1) {
$lastpiece = array_pop($pizza);
$ext = '.'.$lastpiece;
} else {
$ext = '';
}
$resultsfilename = str_replace($ext,'',$inputfilename) . "_nsr_results.tsv";
$resultsfile = $data_dir.$resultsfilename;
// Replace cache?
// Default=false
// If true, will re-run all observations through the NSR,
// replacing any existing values in cache
if (array_key_exists('r', $options)) {
$r = $options["r"];
if ($r == 'replace' || $r == 't' || $r == 'true' ) {
$replace_cache = true;
} elseif ($r == 'f' || $r == 'false' || $r == 'keep') {
$replace_cache = false;
} else {
die("ERROR: Invalid cache option -r=".$r."\r\n");
}
} else {
$replace_cache = false;
}
$replace_cache_str=($replace_cache===false?'false':$replace_cache);
//////////////////////////////////////////////////////
// Confirm operation and connect to database
//////////////////////////////////////////////////////
if ($interactive) {
$msg = "
Run NSR batch application with following parameters:
NSR database: $DB
Data directory: $data_dir
Input file: $inputfilename
File type: $filetype
Results file: $resultsfilename
Replace cache: $replace_cache_str
Job id: $job
Enter Y to proceed, N to cancel: "
;
$proceed=responseYesNoDie($msg);
if ($proceed===false) die("\r\nOperation cancelled\r\n");
}
if ($echo_on) {
// Start timer and connect to mysql
echo "\r\n*********Begin operation*********\r\n\r\n";
include $timer_on;
}
//////////////////////////////////////////////////////
// Process the file
//////////////////////////////////////////////////////
if (!file_exists($inputfile)) die("\r\nError: file '$inputfile' not found!\r\n");
/* connect to the db */
include 'db_batch_connect.php';
// Import raw observations to temporary table
include_once $batch_includes_dir."create_observation_raw.php";
include_once $batch_includes_dir."import_raw_observations.php";
// insert raw observations
include_once $batch_includes_dir."insert_observations.php";
// perform any standardizations needed
if ($replace_cache===true) {
// Remove cached observations for these species+poldiv combinations
if ($echo_on) echo "Removing previous observations from cache...";
include_once "remove_observations_from_cache.php";
if ($echo_on) echo $done;
} else {
// Mark records already in cache
if ($echo_on) echo "Marking observations already in cache...";
include_once "prepare_observations.php";
include_once "mark_observations.php";
if ($echo_on) echo $done;
}
include 'db_batch_connect.php';
// Process observations not in cache, if any, then add to cache
// Do only if >=1 observations not in cache
$sql="
SELECT COUNT(*) AS `rows`
FROM observation
WHERE $JOB_WHERE_NA
AND is_in_cache=0
;
";
//$result = mysqli_query($dbh, $sql);
//$num_rows = mysqli_num_rows($result);
$num_rows = sql_get_first_result($dbh, $sql,'rows');
if ($num_rows>0) {
if ($echo_on) echo "Resolving $num_rows new observations in batches of $batch_size:\r\n";
// Calculate number of batches
$batches = $num_rows / $batch_size;
$whole = intval( $batches );
if ( $batches <= 1 ) {
$batches = 1;
} elseif ( $batches > $whole ) {
$batches = $whole + 1;
}
$batch = 1;
$msg1 = " Batch 1 of $batches...marking...";
$msg2 = " Batch 1 of $batches...marking...processing...";
if ($echo_on) echo $msg1;
while ( $batch <= $batches ) {
if ($echo_on) echo "\r" . $msg1;
// Mark the current batch
include "dbw2_open.php";
$sql="
UPDATE observation
SET batch=$batch
WHERE $JOB_WHERE_NA
AND is_in_cache=0
AND batch IS NULL
LIMIT $batch_size
";
sql_execute_multiple($dbw2, $sql);
mysqli_close($dbw2);
if ($echo_on) echo "\r" . $msg2;
// Turn off echo for NSR processes
$echo_on = false;
// Submit the current batch to NSR, reporting time if echo on
$start_batch = microtime(true);
include "nsr.php";
$end_batch = microtime(true);
$batch_sec = round( $end_batch - $start_batch, 2);
$batch_time = secondsToTime($batch_sec);
// Turn echo back on if applicable
if ($e === true || $e == 'true' || $e == 'on') $echo_on = true;
$msg2 = " Batch $batch of $batches...marking...processing...done ($batch_sec sec)";
if ($echo_on) echo "\r" . $msg2;
$batch++;
}
}
if ($echo_on) echo "\n";
// Update observation table from cache
include_once $batch_includes_dir."update_observations.php";
// dump nsr results to text file
include_once $batch_includes_dir."dump_nsr_results.php";
//exit ("Exiting...\r\n\r\n");
// Clear observation table
include_once "clear_observations.php";
//////////////////////////////////////////////////////
// Close connection and report time
//////////////////////////////////////////////////////
if ($echo_on) {
include $timer_off;
$msg = "\r\n********* Operation completed *********";
$msg = $msg . "\r\nCurrent time: ". $curr_time;
$msg = $msg . "\r\nTime elapsed: " . $tsecs . " seconds.\r\n";
echo $msg . "\r\n\r\n";
}
?>