1 dnl Copyright (C) 2013 The Meme Factory, Inc. http://www.meme.com/
3 dnl This file is part of Gombe-MI.
5 dnl Gombe-MI is free software; you can redistribute it and/or modify
6 dnl it under the terms of the GNU General Public License as published by
7 dnl the Free Software Foundation; either version 3 of the License, or
8 dnl (at your option) any later version.
10 dnl This program is distributed in the hope that it will be useful,
11 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
12 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 dnl GNU General Public License for more details.
15 dnl You should have received a copy of the GNU General Public License
16 dnl along with Gombe-MI. If not, see <http://www.gnu.org/licenses/>.
18 dnl Function to convert from access table structure to a temporary
19 dnl table structure that's easier to work with.
21 dnl Karl O. Pinc <kop@meme.com>
24 include(`copyright.m4')
25 include(`constants.m4')
26 include(`macros.m4')dnl
29 dnl Reset our search path so we pick up the conversion schema.
30 define(`gmi_function_search_path',
31 `prepared_data, conversion, gombemi, pg_temp')
33 CREATE OR REPLACE FUNCTION prepared_data.prepare_data()
37 gmi_function_set_search_path
39 -- Copy access data structures from the conversion schema
40 -- into a better format in the prepared_data schema.
42 -- GPL_notice(` --', `2013', `The Meme Factory, Inc. http://www.meme.com/')
44 -- Syntax: select prepare_data();
47 -- fromdate Date to start copy (inclusive)
48 -- todate Date to stop copy (inclusive)
51 -- One-off program. Once it works we won't run it again.
53 -- Destroy's what's in the prepared_data schema.
55 -- Stops as soon as a transaction fails.
61 raise notice 'deleting';
63 delete from prepared_data.follow_5_minute_group;
64 delete from prepared_data.follow_5_minute;
65 delete from prepared_data.follow_1_minute_sibling;
66 delete from prepared_data.follow_1_minute_mother;
67 delete from prepared_data.follow_1_minute_infant;
68 delete from prepared_data.follow_1_minute;
69 delete from prepared_data.follow_page;
70 delete from prepared_data.follow;
71 delete from prepared_data.crfp;
72 delete from prepared_data.page_followtype;
75 vacuum full prepared_data.follow_5_minute_group;
76 vacuum full prepared_data.follow_5_minute;
77 vacuum full prepared_data.follow_1_minute_sibling;
78 vacuum full prepared_data.follow_1_minute_mother;
79 vacuum full prepared_data.follow_1_minute_infant;
80 vacuum full prepared_data.follow_1_minute;
81 vacuum full prepared_data.follow;
87 -- Convert focal_mom from a name into an animid.
88 insert into prepared_data.crfp (
102 ,"group_comp_problem"
106 ,"last_checked_by_date"
107 ,"absent_mother_infant"
113 ,"paper_follow_location"
121 ,biography_data.animid
131 ,"Group comp problem?"
135 ,"Last Checked By Date"
136 ,"Absent Mother/Infant"
142 ,"Paper Follow Location"
145 from conversion.cross_reference_forprogram
147 ON (upper(trim(biography_data.animname))
148 = upper(trim(conversion.cross_reference_forprogram."Focal mom")));
151 from conversion.cross_reference_forprogram)
153 from prepared_data.crfp) THEN
154 RAISE EXCEPTION integrity_constraint_violation USING
155 MESSAGE = 'Error looking up cross_reference_forprogram."Focal mom" on biography_data'
156 , DETAIL = 'Missing (Focal mom) = ('
157 || (SELECT conversion.cross_reference_forprogram."Focal mom"
158 FROM conversion.cross_reference_forprogram
161 FROM gombemi.biography_data
162 WHERE upper(trim(gombemi.biography_data.animname))
163 = upper(trim(conversion.cross_reference_forprogram."Focal mom"))
171 raise notice 'page_followtype';
173 insert into prepared_data.page_followtype (
187 from conversion.page_followtype);
191 raise notice 'follow';
193 insert into prepared_data.follow (
229 from conversion.follow);
232 raise notice 'follow_page';
234 insert into prepared_data.follow_page (
251 from conversion.follow_page;
255 raise notice 'follow_1_minute';
257 insert into prepared_data.follow_1_minute (
280 ,split_part("Interval_id", '_', 1)
282 || split_part("Interval_id", '_', 2)
284 from conversion.follow_1_minute);
287 -- follow_1_minute_infant
288 raise notice 'follow_1_minute_infant';
290 insert into prepared_data.follow_1_minute_infant (
302 ,"between_5_15_meters"
303 ,"more_than_15_meters"
317 ,"infant_interval_id"
333 ,"Between_5_15_meters"
334 ,"More_than_15_meters"
348 ,"Infant_interval_id"
349 ,split_part("Interval_id", '_', 1)
351 || split_part("Interval_id", '_', 2)
353 from conversion.follow_1_minute_infant);
357 -- follow_1_minute_mother
358 raise notice 'follow_1_minute_mother';
360 insert into prepared_data.follow_1_minute_mother (
379 ,split_part("Interval_id", '_', 1)
381 || split_part("Interval_id", '_', 2)
383 from conversion.follow_1_minute_mother;
386 -- follow_1_minute_sibling
387 raise notice 'follow_1_minute_sibling';
389 insert into prepared_data.follow_1_minute_sibling (
395 ,"distance_from_mother"
400 ,"distance_from_infant"
401 ,"sibling_interval_id"
411 ,"Distance_from_mother"
416 ,"Distance_from_infant"
417 ,"Sibling_interval_id"
418 ,split_part("Interval_id", '_', 1)
420 || split_part("Interval_id", '_', 2)
422 from conversion.follow_1_minute_sibling;
424 -- Go through the follow_1_minute_sibling and where
425 -- the 'Multi' behavior code is found add grooms other,
426 -- plays, and groomed by as is found in the data.
428 -- follow_1_minute_sibling Multi -> Grooming (based on grooms_other)
429 UPDATE prepared_data.follow_1_minute_sibling
431 WHEN TRIM(TRAILING FROM behavior) ~ ';$'
432 THEN TRIM(TRAILING FROM behavior) || 'Grooming'
434 TRIM(TRAILING FROM behavior) || ';Grooming'
436 WHERE behavior ~ '(?:^|;)[[:blank:]]*Multi[[:blank:]]*(?:$|;)'
437 AND TRIM(BOTH FROM grooms_other) != ''
438 AND grooms_other IS NOT NULL
439 AND behavior !~ '(?:^|;)[[:blank:]]*Grooming[[:blank:]]*(?:$|;)';
441 -- follow_1_minute_sibling Multi -> Groomed (based on groomed_by)
442 UPDATE prepared_data.follow_1_minute_sibling
444 WHEN TRIM(TRAILING FROM behavior) ~ ';$'
445 THEN TRIM(TRAILING FROM behavior) || 'Groomed'
447 TRIM(TRAILING FROM behavior) || ';Groomed'
449 WHERE behavior ~ '(?:^|;)[[:blank:]]*Multi[[:blank:]]*(?:$|;)'
450 AND TRIM(BOTH FROM groomed_by) != ''
451 AND groomed_by IS NOT NULL
452 AND behavior !~ '(?:^|;)[[:blank:]]*Groomed[[:blank:]]*(?:$|;)';
454 -- follow_1_minute_sibling Multi -> Play (based on plays)
455 UPDATE prepared_data.follow_1_minute_sibling
457 WHEN TRIM(TRAILING FROM behavior) ~ ';$'
458 THEN TRIM(TRAILING FROM behavior) || 'Play'
460 TRIM(TRAILING FROM behavior) || ';Play'
462 WHERE behavior ~ '(?:^|;)[[:blank:]]*Multi[[:blank:]]*(?:$|;)'
463 AND TRIM(BOTH FROM plays) != ''
464 AND plays IS NOT NULL
465 AND behavior !~ '(?:^|;)[[:blank:]]*Play[[:blank:]]*(?:$|;)';
469 raise notice 'follow_5_minute';
471 insert into prepared_data.follow_5_minute(
477 ,"food_part_initials"
492 ,"Food_part_initials"
498 ,split_part("Interval_id", '_', 1)
500 || split_part("Interval_id", '_', 2)
502 from conversion.follow_5_minute;
505 -- follow_5_minute_group
506 raise notice 'follow_5_minute_group';
508 insert into prepared_data.follow_5_minute_group (
525 ,split_part("Interval_id", '_', 1)
527 || split_part("Interval_id", '_', 2)
529 from conversion.follow_5_minute_left_group
538 ,split_part("Interval_id", '_', 1)
540 || split_part("Interval_id", '_', 2)
542 from conversion.follow_5_minute_right_group;
545 vacuum analyze prepared_data.follow_5_minute_group;
546 vacuum analyze prepared_data.follow_5_minute;
547 vacuum analyze prepared_data.follow_1_minute_sibling;
548 vacuum analyze prepared_data.follow_1_minute_mother;
549 vacuum analyze prepared_data.follow_1_minute_infant;
550 vacuum analyze prepared_data.follow_1_minute;
551 vacuum analyze prepared_data.follow;
559 grant_func_priv(`prepared_data.prepare_data()')