1
|
SELECT util.search_path_append('util');
|
2
|
|
3
|
SELECT create_if_not_exists($$CREATE INDEX "Specimen.specimenHolderInstitutions" ON $$||:table_str||$$ ("specimenHolderInstitutions")$$);
|
4
|
|
5
|
-- remove frameshifted rows
|
6
|
DELETE FROM :table WHERE "specimenHolderInstitutions" IS NULL;
|
7
|
DELETE FROM :table WHERE "yearCollected" !~ '^(?:1[7-9]|20)\d{2}$';
|
8
|
DELETE FROM :table WHERE country_verbatim ~ '\d';
|
9
|
DELETE FROM :table WHERE longitude_deg_verbatim ~ '[[:alpha:]]' AND longitude_deg_verbatim NOT IN ('RESTRINGIDO');
|
10
|
DELETE FROM :table WHERE longitude_min_verbatim !~ '^\d*$';
|
11
|
DELETE FROM :table WHERE longitude_sec_verbatim !~ '^\d*$';
|
12
|
DELETE FROM :table WHERE latitude_min_verbatim !~ '^\d*$';
|
13
|
DELETE FROM :table WHERE latitude_sec_verbatim !~ '^\d*$';
|
14
|
|
15
|
-- Remove institutions that we have direct data for
|
16
|
DELETE FROM :table
|
17
|
WHERE "specimenHolderInstitutions" IN (
|
18
|
-- Comments are from e-mail from Brad Boyle on 2013-1-16
|
19
|
'MO' -- "all MO records in REMIB are also available from MO's own website"
|
20
|
--, 'ARIZ' -- Some REMIB ARIZ specimens not yet in ARIZ itself
|
21
|
--, 'NY' -- Some REMIB NY specimens not yet in NY itself
|
22
|
, 'TEX'
|
23
|
)
|
24
|
/* list obtained using the following on r9459:
|
25
|
SELECT DISTINCT dataprovider
|
26
|
FROM sourcelist
|
27
|
JOIN provider_count ON provider_count.dataprovider = sourcelist.name
|
28
|
WHERE source_id = (SELECT source_by_shortname('REMIB'))
|
29
|
ORDER BY dataprovider
|
30
|
*/
|
31
|
;
|
32
|
|
33
|
|
34
|
-- map_nulls() derived cols
|
35
|
-- runtime: 7.5 min ("real 7m27.083s") @vegbiendev
|
36
|
|
37
|
CREATE OR REPLACE FUNCTION map_nulls__text(value text)
|
38
|
RETURNS text AS
|
39
|
$BODY$
|
40
|
-- "ND = no disponible = not available" (https://projects.nceas.ucsb.edu/nceas/projects/bien/wiki/Spot-checking#REMIB)
|
41
|
SELECT util.map_nulls('{ND,NA}', $1)
|
42
|
$BODY$
|
43
|
LANGUAGE sql IMMUTABLE
|
44
|
COST 100;
|
45
|
|
46
|
SELECT mk_derived_col((:table_str, 'country' ), $$map_nulls__text("country_verbatim" )$$); -- runtime: 35 s ("Time: 35690.797 ms") @vegbiendev
|
47
|
SELECT mk_derived_col((:table_str, 'stateProvince'), $$map_nulls__text("stateProvince_verbatim")$$); -- runtime: 35 s ("Time: 36074.430 ms") @vegbiendev
|
48
|
SELECT mk_derived_col((:table_str, 'county' ), $$map_nulls__text("county_verbatim" )$$); -- runtime: 35 s ("Time: 36096.911 ms") @vegbiendev
|
49
|
SELECT mk_derived_col((:table_str, 'locality' ), $$map_nulls__text("locality_verbatim" )$$); -- runtime: 35 s ("Time: 36076.364 ms") @vegbiendev
|
50
|
SELECT mk_derived_col((:table_str, 'habitat' ), $$map_nulls__text("habitat_verbatim" )$$); -- runtime: 35 s ("Time: 35481.828 ms") @vegbiendev
|
51
|
|
52
|
|
53
|
CREATE OR REPLACE FUNCTION map_nulls__coord__longitude(value text)
|
54
|
RETURNS double precision AS
|
55
|
$BODY$
|
56
|
-- TODO: sometimes also +-99, but not when min/sec are valid
|
57
|
SELECT util.map_nulls('{-999,999,1000}',
|
58
|
util.map_nulls('{RESTRINGIDO}', $1)::double precision)
|
59
|
$BODY$
|
60
|
LANGUAGE sql IMMUTABLE
|
61
|
COST 100;
|
62
|
|
63
|
CREATE OR REPLACE FUNCTION map_nulls__coord__other(value text)
|
64
|
RETURNS double precision AS
|
65
|
$BODY$
|
66
|
-- also map nulls that are valid longitudes
|
67
|
SELECT util.map_nulls('{-99,99}', map_nulls__coord__longitude($1))
|
68
|
$BODY$
|
69
|
LANGUAGE sql IMMUTABLE
|
70
|
COST 100;
|
71
|
|
72
|
SELECT mk_derived_col((:table_str, 'longitude_deg'), $$map_nulls__coord__longitude("longitude_deg_verbatim")$$); -- runtime: 40 s ("Time: 39417.099 ms") @vegbiendev
|
73
|
SELECT mk_derived_col((:table_str, 'longitude_min'), $$map_nulls__coord__other ("longitude_min_verbatim")$$); -- runtime: 40 s ("Time: 41929.772 ms") @vegbiendev
|
74
|
SELECT mk_derived_col((:table_str, 'longitude_sec'), $$map_nulls__coord__other ("longitude_sec_verbatim")$$); -- runtime: 40 s ("Time: 42106.205 ms") @vegbiendev
|
75
|
SELECT mk_derived_col((:table_str, 'latitude_deg' ), $$map_nulls__coord__other ("latitude_deg_verbatim" )$$); -- runtime: 40 s ("Time: 42187.294 ms") @vegbiendev
|
76
|
SELECT mk_derived_col((:table_str, 'latitude_min' ), $$map_nulls__coord__other ("latitude_min_verbatim" )$$); -- runtime: 40 s ("Time: 43220.851 ms") @vegbiendev
|
77
|
SELECT mk_derived_col((:table_str, 'latitude_sec' ), $$map_nulls__coord__other ("latitude_sec_verbatim" )$$); -- runtime: 40 s ("Time: 42267.566 ms") @vegbiendev
|