Revision 1631
Added by Aaron Marcuse-Kubitza over 12 years ago
inputs/REMIB/src/nodes.all.specimens.csv.make | ||
---|---|---|
1 |
#!/usr/bin/env python |
|
2 |
# Downloads REMIB data for all nodes |
|
3 |
# Usage: self |
|
4 |
|
|
5 |
import os.path |
|
6 |
import StringIO |
|
7 |
import sys |
|
8 |
import urllib2 |
|
9 |
|
|
10 |
sys.path.append(os.path.dirname(__file__)+"/../../../lib") |
|
11 |
|
|
12 |
import csv |
|
13 |
import profiling |
|
14 |
import streams |
|
15 |
import strings |
|
16 |
import term |
|
17 |
import util |
|
18 |
|
|
19 |
def is_ignore(line): |
|
20 |
line = strings.remove_line_ending(line) |
|
21 |
return line == '' or line.startswith('\t') or line.find(',') < 0 |
|
22 |
|
|
23 |
def main(): |
|
24 |
def clear_line(): sys.stderr.write(term.clear_line) |
|
25 |
log_indent = 0 |
|
26 |
def log(msg, line_ending='\n'): |
|
27 |
sys.stderr.write((' '*log_indent)+msg+line_ending) |
|
28 |
|
|
29 |
stdout = streams.LineCountOutputStream(sys.stdout) |
|
30 |
|
|
31 |
# Get by family ('familia') because that is the most general level at which |
|
32 |
# an identification can be made. This assumes all records have a family. |
|
33 |
url_template = ('http://www.conabio.gob.mx/remib/cgi-bin/' |
|
34 |
'remib_distribucion.cgi?lengua=EN&niveltax=familia&taxon=[prefix]%25&' |
|
35 |
'pais=Todos&pais_otro=&estado=100&formato=csv&mapa=no&mapabase=estados' |
|
36 |
'&coleccion=id%3D[node_id]') |
|
37 |
for node_id in xrange(1, 3): |
|
38 |
log('Processing node #'+str(node_id)+'...') |
|
39 |
log_indent += 1 |
|
40 |
profiler = profiling.ItersProfiler(start_now=True, iter_text='row') |
|
41 |
row_ct = 0 |
|
42 |
def print_status(line_ending='\n'): |
|
43 |
log('Processed '+str(row_ct)+' row(s)', line_ending) |
|
44 |
|
|
45 |
node_url_template = url_template.replace('[node_id]', str(node_id)) |
|
46 |
for prefix in ['AC']: |
|
47 |
log('Processing prefix '+prefix+'...') |
|
48 |
log_indent += 1 |
|
49 |
start_line_num = stdout.line_num |
|
50 |
|
|
51 |
url = node_url_template.replace('[prefix]', prefix) |
|
52 |
stream = streams.StreamIter(urllib2.urlopen(url)) |
|
53 |
|
|
54 |
util.skip(stream, is_ignore) # skip header |
|
55 |
metadata_row = csv.reader(stream).next() |
|
56 |
assert metadata_row[0] == 'COLLECTION' |
|
57 |
|
|
58 |
# Copy lines |
|
59 |
for line in stream: |
|
60 |
if is_ignore(line): |
|
61 |
error = strings.remove_prefix('\t\t', line) |
|
62 |
if len(error) != len(line): |
|
63 |
clear_line() |
|
64 |
log('! Line '+str(stdout.line_num)+': ' +error.rstrip()) |
|
65 |
break |
|
66 |
if row_ct % 100 == 0: print_status('\r') |
|
67 |
# CR at end so next print overwrites msg |
|
68 |
stdout.write(line) |
|
69 |
row_ct += 1 |
|
70 |
stream.close() |
|
71 |
|
|
72 |
clear_line() |
|
73 |
log_indent -= 1 |
|
74 |
|
|
75 |
profiler.stop(row_ct) |
|
76 |
print_status() |
|
77 |
log(profiler.msg()) |
|
78 |
|
|
79 |
if stdout.line_num == start_line_num: line_range = '<none>' |
|
80 |
else: line_range = str(start_line_num)+'-'+str(stdout.line_num-1) |
|
81 |
log('Used output lines '+line_range) |
|
82 |
|
|
83 |
log_indent -= 1 |
|
84 |
|
|
85 |
main() |
|
0 | 86 |
Also available in: Unified diff
Added inputs/REMIB/src/nodes.all.specimens.csv.make to download REMIB data for all nodes