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 new table structure.
20 dnl Karl O. Pinc <kop@meme.com>
23 include(`copyright.m4')
24 include(`constants.m4')
26 include(`macros.m4')dnl
28 dnl Reset our search path so we pick up the conversion schema.
29 define(`gmi_function_search_path', `prepared_data, gombemi, pg_temp')
31 dnl The animid used for baboons.
32 define(`_baboon_id', `NYA')
34 dnl The behavior code for eating.
35 define(`_ca_eating', `UE')
37 dnl Ugly hack to generate events.position
39 dnl syntax _gen_pos(behavior)
41 dnl behavior The behavior code.
44 define({_gen_pos},{(SELECT CASE WHEN position THEN '9' ELSE NULL END FROM behaviors WHERE behavior = $1)})dnl
48 CREATE OR REPLACE FUNCTION prepared_data.convert_access(fromdate DATE
53 gmi_function_set_search_path
55 -- Copy access data structures into new gombemi data structures.
57 -- GPL_notice(` --', `2013', `The Meme Factory, Inc. http://www.meme.com/')
59 -- Syntax: select convert_access(fromdate, todate);
62 -- fromdate Date to start copy (inclusive)
63 -- todate Date to stop copy (inclusive)
66 -- One-off program. Once it works we won't run it again.
68 -- Copies a day a a time.
70 -- Stops as soon as a transaction fails.
75 this_enterer people.person%TYPE;
76 this_collector people.person%TYPE;
80 this_sheetid sheets.sheetid%TYPE;
81 this_ffid fieldfollows.ffid%TYPE;
86 interval_comment TEXT;
88 this_uncertain BOOLEAN;
93 this_1_min_infant RECORD;
94 this_1_min_mom RECORD;
95 this_1_min_sib RECORD;
97 default_mom_uncertain BOOLEAN;
98 default_infant_uncertain BOOLEAN;
99 default_sib_uncertain BOOLEAN;
104 event_behavior TEXT[];
105 event_uncertain BOOLEAN[];
106 event_baboon BOOLEAN[];
107 event_comment TEXT[];
112 part1_participants TEXT[];
114 part2_participants TEXT[];
116 grooming_found BOOLEAN;
119 grooming_and_play BOOLEAN;
120 -- Did mom's grooming or play partner get changed to UNK?
122 grooms_other_converted BOOLEAN;
123 groomed_by_converted BOOLEAN;
124 plays_converted BOOLEAN;
131 this_voc_uncertain BOOLEAN;
135 this_participant TEXT;
137 this_part1 parts.part%TYPE;
138 this_part2 parts.part%TYPE;
141 -- Store duplicate entries.
142 define(`MAX_BEHAVIOR_LEN', `25')dnl
143 infant_mom_behaviors VARCHAR(MAX_BEHAVIOR_LEN)[];
144 infant_mom_uncertains BOOLEAN[];
145 infant_mom_events INT[];
146 infant_sib_behaviors VARCHAR(MAX_BEHAVIOR_LEN)[];
147 infant_sib_uncertains BOOLEAN[];
148 infant_sib_events INT[];
149 mom_sib_behaviors VARCHAR(MAX_BEHAVIOR_LEN)[];
150 mom_sib_uncertains BOOLEAN[];
151 mom_sib_events INT[];
153 sibling_found BOOLEAN;
159 IF current_database() = 'gombemi_raw' THEN
160 -- Make all the chimpids that don't already exist.
161 INSERT INTO chimpids (animid, chimpid)
162 SELECT biography_data.animid AS animid
163 , biography_data.animid AS chimpid
167 FROM chimpids AS this_id
168 WHERE this_id.animid = biography_data.animid
175 WHERE fromdate <= follow.follow_date
176 AND follow.follow_date <= todate
177 ORDER BY follow.follow_date, follow.follow_id
180 -- Check for already converted.
181 IF this_follow.done THEN
182 RAISE EXCEPTION 'Date %: Mom %: follow row lready converted'
183 , this_follow.follow_date
184 , this_follow.mother_id;
187 -- Create the follow.
188 INSERT INTO follows (date)
189 VALUES(this_follow.follow_date);
191 -- Create the followparts rows.
193 -- Create a followparts row for the mom.
194 INSERT INTO followparts (followid, chimpid, role)
195 VALUES(currval('follows_followid_seq'::regclass)
196 , this_follow.mother_id
199 -- Create a followparts row for the infant.
200 INSERT INTO followparts (followid, chimpid, role)
201 VALUES(currval('follows_followid_seq'::regclass)
202 , this_follow.infant_id
205 IF this_follow.sibling_id IS NOT NULL THEN
206 -- Create a followparts row for the sibling.
207 INSERT INTO followparts (followid, chimpid, role)
208 VALUES(currval('follows_followid_seq'::regclass)
209 , this_follow.sibling_id
213 -- The fieldfollow, access data has 1 per follow.
214 -- Administrative info: collector, enterer, etc.
220 WHERE people.descr = this_follow.entered_by;
223 RAISE EXCEPTION integrity_constraint_violation USING
224 MESSAGE = 'Cannot lookup enterer for follow on PEOPLE'
225 , DETAIL = 'Value (Follow_id) = ('
226 || this_follow.follow_id
227 || '): Value (Entered_by) = ('
228 || this_follow.entered_by
232 -- Find the type and entry date of the follow.
233 SELECT crfp.end_entry_date, TRIM(BOTH FROM crfp.temptype)
234 INTO this_entrydate, this_followtype
236 WHERE crfp.follow_date = this_follow.follow_date
237 AND crfp.focal_mom = this_follow.mother_id;
240 RAISE EXCEPTION integrity_constraint_violation USING
241 MESSAGE = 'Cannot lookup entrydate or template type for follow on cross_reference_forprogram'
242 , DETAIL = 'Value (Follow_id) = ('
243 || this_follow.follow_id
244 || '): Value (Entered_by) = ('
245 || this_follow.entered_by
249 -- Find the collector
253 WHERE people.descr = this_follow.conducted_by;
256 RAISE EXCEPTION integrity_constraint_violation USING
257 MESSAGE = 'Cannot lookup collecor for follow on PEOPLE'
258 , DETAIL = 'Value (Follow_id) = ('
259 || this_follow.follow_id
260 || '): Value (Conducted_by) = ('
261 || this_follow.conducted_by
265 -- Create the fieldfollow.
266 INSERT INTO fieldfollows (collector
270 VALUES(this_collector
275 this_ffid := CURRVAL('fieldfollows_ffid_seq'::REGCLASS);
277 -- The columns we've not converted:
279 -- Infant2_id | Sibling2_id | Sheet_id | Left_group_id | Right_group_id | Follow_status | Follow_id | Time_mode | AMPM
281 -- tableid | Follow_date | Mother_id | Infant_id | Infant2_id | Sibling_id | Sibling2_id | Conducted_by | Entered_by | Sheet_id | Left_group_id | Right_group_id | Follow_status | Follow_id | Time_mode | AMPM
282 -- 1 | 2010-11-03 | GA | GGL | | | | Juma Mazogo | Karen Anderson | 1 | 29 | 61 | Completed | 11/03/2010_GA | SWA | AM
285 -- Go through all the 1 minute intervals for the follow.
292 WHERE follow_1_minute.follow_id = this_follow.follow_id
293 ORDER BY follow_1_minute.interval_time
296 -- Check for already converted.
297 IF this_1_min.done THEN
298 RAISE EXCEPTION 'Date %: Mom %: Time %: follow_1_minute row already converted'
299 , this_follow.follow_date
300 , this_follow.mother_id
301 , this_1_min.interval_time;
304 IF this_1_min.interval_time::DATE != this_follow.follow_date THEN
305 RAISE EXCEPTION 'Follow_id %: Date %: Mom %: Time %: The date part of the follow_1_minute.interval_time value does not match the date of the follow'
306 , this_follow.follow_id
307 , this_follow.follow_date
308 , this_follow.mother_id
309 , this_1_min.interval_time;
313 SELECT follow_page.page_number, trim(both from follow_page.location)
314 INTO this_page, this_location
316 WHERE follow_page.page_id = this_1_min.page_id;
319 RAISE EXCEPTION 'Date %: Mom %: Time %: Page_id %: Cannot lookup page number from page_id'
320 , this_follow.follow_date
321 , this_follow.mother_id
322 , this_1_min.interval_time
323 , this_1_minute.page_id;
327 OR this_page != last_page THEN
328 last_page := this_page;
330 -- "Regular" modern follows (date/mom combos) can have multiple
331 -- researchers (aka collectors), but this does not happen in the
332 -- access data so we don't need to worry about having the
333 -- page number be unique per collector/breaking on collector.
335 -- Remember the page is converted.
338 WHERE follow_page.page_id = this_1_min.page_id;
340 -- Find the sheet on which the interval belongs.
341 SELECT sheets.sheetid
345 ON (followparts.followid = follows.followid)
346 JOIN intervals ON (intervals.followid = follows.followid)
347 JOIN sheets ON (sheets.sheetid = intervals.sheetid)
348 WHERE follows.date = this_follow.follow_date
349 AND followparts.chimpid = this_follow.mother_id
350 AND followparts.role = 'M'
351 AND sheets.page = this_page
355 -- Create a sheet, into which we will put this interval.
357 -- Get the followtype.
358 IF CHAR_LENGTH(this_followtype) > 1 then
359 -- The follow is of a mixed type.
360 -- Can't look at the cfrp for the follow type, look per page.
362 SELECT TRIM(BOTH FROM page_followtype.temptype)
363 INTO STRICT tmp_followtype
365 WHERE page_followtype.date = this_follow.follow_date
366 AND page_followtype.page = this_page::TEXT
367 AND page_followtype.mom = this_follow.mother_id;
370 WHEN TOO_MANY_ROWS THEN
371 RAISE EXCEPTION integrity_constraint_violation USING
372 MESSAGE = 'Template type on cross_reference_forprogram is longer than 1 character and there is more than one matching row on the page_followtype table'
373 , DETAIL = 'Value (Follow_id) = ('
374 || this_follow.follow_id
376 WHEN NO_DATA_FOUND THEN
377 RAISE EXCEPTION integrity_constraint_violation USING
378 MESSAGE = 'Template type on cross_reference_forprogram is longer than 1 character and there is not a matching row on the page_followtype table'
379 , DETAIL = 'Value (Follow_id) = ('
380 || this_follow.follow_id
384 -- Remember having looked at the followtype per page.
385 UPDATE page_followtype
387 WHERE page_followtype.date = this_follow.follow_date
388 AND page_followtype.page = this_page::TEXT
389 AND page_followtype.mom = this_follow.mother_id;
390 ELSE -- one char followtype
391 tmp_followtype := this_followtype;
394 -- Create the sheets row.
396 IF this_location = '' THEN
397 this_location = NULL;
400 INSERT INTO sheets (ffid, followtype, page, location)
406 this_sheetid = currval('sheets_sheetid_seq'::regclass);
412 -- tableid | Page_id | Interval_number | Interval_time | Bad_observation | Rain | Comment | CommentRTF | Interval_id
413 -- 1 | 11/28/2010_NUR_02 | 1 | 2010-11-28 07:15:00 | | | | | 11/28/2010_NUR_02_01
415 -- Clean up the comment
416 interval_comment := TRIM(BOTH FROM this_1_min.comment);
417 IF interval_comment = '' THEN
418 interval_comment := NULL;
421 -- Create the interval.
422 INSERT INTO intervals (followid
426 VALUES (currval('follows_followid_seq'::regclass)
427 , currval('sheets_sheetid_seq'::regclass)
428 , this_1_min.interval_time
431 -- Bad observeration.
432 default_mom_uncertain := FALSE;
433 default_infant_uncertain := FALSE;
434 default_sib_uncertain := FALSE;
435 IF this_1_min.bad_observation IS NOT NULL THEN
436 CASE this_1_min.bad_observation
438 default_mom_uncertain := TRUE;
439 default_infant_uncertain := TRUE;
440 default_sib_uncertain := TRUE;
443 default_mom_uncertain := TRUE;
446 default_infant_uncertain := TRUE;
449 default_sib_uncertain := TRUE;
452 default_mom_uncertain := TRUE;
453 default_infant_uncertain := TRUE;
456 default_mom_uncertain := TRUE;
457 default_sib_uncertain := TRUE;
460 default_infant_uncertain := TRUE;
461 default_sib_uncertain := TRUE;
464 RAISE EXCEPTION 'Interval_id %: Date %: Time %: Bad observation %: The follow_1_minute Bad_observation value is unrecognized'
465 , this_1_min.interval_id
466 , this_follow.follow_date
467 , this_1_min.interval_time
468 , this_1_min.bad_observation;
472 -- Save the data that's supposedly duplicated in the access db.
473 infant_mom_behaviors := ARRAY[]::VARCHAR(MAX_BEHAVIOR_LEN)[];
474 infant_mom_uncertains := ARRAY[]::BOOLEAN[];
475 infant_mom_events := ARRAY[]::INT[];
476 infant_sib_behaviors := ARRAY[]::VARCHAR(MAX_BEHAVIOR_LEN)[];
477 infant_sib_uncertains := ARRAY[]::BOOLEAN[];
478 infant_sib_events := ARRAY[]::INT[];
479 mom_sib_behaviors := ARRAY[]::VARCHAR(MAX_BEHAVIOR_LEN)[];
480 mom_sib_uncertains := ARRAY[]::BOOLEAN[];
481 mom_sib_events := ARRAY[]::INT[];
483 -- Get all the related 1 minute rows.
485 INTO this_1_min_infant
486 FROM follow_1_minute_infant
487 WHERE follow_1_minute_infant.interval_id = this_1_min.interval_id;
489 -- follow_1_minute_infant:
490 -- Grooms_other | Groomed_by | Plays | Cries | Vocalizations
492 -- tableid | Interval_id | Infant_id | Nursing | Eating | Walks | Playing | Resting | In_contact | Within_reach | Within_0_5_meters | Between_5_15_meters | More_than_15_meters | Rides_on_stomach | Rides_on_back | Dangles | Contacts_other | Grooms_other | Groomed_by | Plays | Displays | Refused | Cries | Vocalizations | Begs | Aggression | Infant_interval_id
493 -- 1 | 11/03/2010_GA_01_01 | GGL | | | | | | | | Y | | | | | | | | | GIZ | | | | | | | 11/03/2010_GA_01_01_GGL
496 -- Infant sanity check.
497 IF this_follow.infant_id != this_1_min_infant.infant_id THEN
498 RAISE EXCEPTION 'Follow Infant_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: The infant id in the follow does not match the infant id in the Follow_1_minute_infant'
499 , this_follow.infant_id
500 , this_1_min_infant.infant_id
501 , this_follow.follow_date
502 , this_1_min.interval_time;
505 -- Check for already converted.
506 IF this_1_min_infant.done THEN
507 RAISE EXCEPTION 'Date %: Mom %: Infant %: follow_1_minute_infant row lready converted'
508 , this_follow.follow_date
509 , this_follow.mother_id
510 , this_follow.infant_id;
514 IF this_1_min_infant.nursing IS NOT NULL THEN
515 CASE this_1_min_infant.nursing
517 this_uncertain := default_infant_uncertain;
519 this_uncertain := TRUE;
521 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: Nursing %: Nursing code unrecognized'
522 , this_follow.follow_id
523 , this_1_min_infant.infant_id
524 , this_follow.follow_date
525 , this_1_min.interval_time
526 , this_1_min_infant.nursing;
530 INSERT INTO events (intid
536 VALUES (currval('intervals_intid_seq'::regclass)
543 -- Nursing participants, mom.
544 INSERT INTO parts (eid, part, participant)
545 VALUES (CURRVAL('events_eid_seq'::regclass)
547 , this_follow.mother_id);
549 -- Nursing participants, infant.
550 INSERT INTO parts (eid, part, participant)
551 VALUES (CURRVAL('events_eid_seq'::regclass)
553 , this_follow.infant_id);
557 IF this_1_min_infant.eating IS NOT NULL THEN
558 CASE this_1_min_infant.eating
560 this_uncertain := default_infant_uncertain;
562 this_uncertain := TRUE;
564 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: Eating %: Eating code unrecognized'
565 , this_follow.follow_id
566 , this_1_min_infant.infant_id
567 , this_follow.follow_date
568 , this_1_min.interval_time
569 , this_1_min_infant.eating;
572 INSERT INTO events (intid
578 VALUES (currval('intervals_intid_seq'::regclass)
580 , _gen_pos('_ca_eating')
586 INSERT INTO parts (eid, part, participant)
587 VALUES (CURRVAL('events_eid_seq'::regclass)
589 , this_follow.infant_id);
593 IF this_1_min_infant.walks IS NOT NULL THEN
594 CASE this_1_min_infant.walks
596 this_uncertain := default_infant_uncertain;
598 this_uncertain := TRUE;
600 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: Walks %: Walks code unrecognized'
601 , this_follow.follow_id
602 , this_1_min_infant.infant_id
603 , this_follow.follow_date
604 , this_1_min.interval_time
605 , this_1_min_infant.walks;
608 INSERT INTO events (intid
614 VALUES (currval('intervals_intid_seq'::regclass)
622 INSERT INTO parts (eid, part, participant)
623 VALUES (CURRVAL('events_eid_seq'::regclass)
625 , this_follow.infant_id);
628 -- Playing (solitary)
629 IF this_1_min_infant.playing IS NOT NULL THEN
630 CASE this_1_min_infant.playing
633 this_uncertain := default_infant_uncertain;
636 this_uncertain := default_infant_uncertain;
639 this_uncertain := default_infant_uncertain;
642 this_uncertain := default_infant_uncertain;
645 this_uncertain := default_infant_uncertain;
648 this_uncertain := default_infant_uncertain;
651 this_uncertain := TRUE;
653 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: Playing %: Playing code unrecognized'
654 , this_follow.follow_id
655 , this_1_min_infant.infant_id
656 , this_follow.follow_date
657 , this_1_min.interval_time
658 , this_1_min_infant.playing;
661 INSERT INTO events (intid
667 VALUES (currval('intervals_intid_seq'::regclass)
674 INSERT INTO plays (eid, play)
675 VALUES (CURRVAL('events_eid_seq'::regclass)
679 INSERT INTO parts (eid, part, participant)
680 VALUES (CURRVAL('events_eid_seq'::regclass)
682 , this_follow.infant_id);
686 IF this_1_min_infant.resting IS NOT NULL THEN
687 CASE this_1_min_infant.resting
689 this_uncertain := default_infant_uncertain;
691 this_uncertain := TRUE;
693 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: Resting %: Resting code unrecognized'
694 , this_follow.follow_id
695 , this_1_min_infant.infant_id
696 , this_follow.follow_date
697 , this_1_min.interval_time
698 , this_1_min_infant.resting;
701 IF (this_1_min_infant.resting = '?'
702 AND (this_1_min_infant.nursing = '?'
703 OR this_1_min_infant.nursing IS NULL)
704 AND (this_1_min_infant.eating = '?'
705 OR this_1_min_infant.eating IS NULL)
706 AND (this_1_min_infant.walks = '?'
707 OR this_1_min_infant.walks IS NULL)
708 AND (this_1_min_infant.playing = '?'
709 OR this_1_min_infant.playing IS NULL)
710 AND (this_1_min_infant.rides_on_stomach = '?'
711 OR this_1_min_infant.rides_on_stomach IS NULL)
712 AND (this_1_min_infant.rides_on_back = '?'
713 OR this_1_min_infant.rides_on_back IS NULL)
714 AND (this_1_min_infant.grooms_other = '?'
715 OR this_1_min_infant.grooms_other IS NULL)
716 AND (this_1_min_infant.groomed_by = '?'
717 OR this_1_min_infant.groomed_by IS NULL)
718 AND (this_1_min_infant.plays = '?'
719 OR this_1_min_infant.plays IS NULL)
720 AND (this_1_min_infant.dangles = '?'
721 OR this_1_min_infant.dangles IS NULL))
722 OR (this_1_min_infant.resting = 'Y'
723 AND this_1_min_infant.nursing IS NULL
724 AND this_1_min_infant.eating IS NULL
725 AND this_1_min_infant.walks IS NULL
726 AND this_1_min_infant.playing IS NULL
727 AND this_1_min_infant.rides_on_stomach IS NULL
728 AND this_1_min_infant.rides_on_back IS NULL
729 AND this_1_min_infant.grooms_other IS NULL
730 AND this_1_min_infant.groomed_by IS NULL
731 AND this_1_min_infant.plays IS NULL
732 AND this_1_min_infant.dangles IS NULL) THEN
733 -- Can only be definatly resting if not doing anything else
734 -- at all. Are allowed to be probably resting so long as
735 -- everything else that might be happening is only probable.
736 INSERT INTO events (intid
742 VALUES (currval('intervals_intid_seq'::regclass)
750 INSERT INTO parts (eid, part, participant)
751 VALUES (CURRVAL('events_eid_seq'::regclass)
753 , this_follow.infant_id);
756 INSERT INTO rests (eid, rest)
757 VALUES (CURRVAL('events_eid_seq'::regclass)
763 IF this_1_min_infant.rides_on_stomach IS NOT NULL THEN
764 CASE this_1_min_infant.rides_on_stomach
766 this_uncertain := default_infant_uncertain;
767 this_participant := this_follow.mother_id;
769 this_uncertain := TRUE;
770 this_participant := 'UNK';
773 this_uncertain := default_infant_uncertain;
774 this_participant := convert_animid(this_1_min_infant.rides_on_stomach, this_follow.follow_date);
777 INSERT INTO events (intid
783 VALUES (currval('intervals_intid_seq'::regclass)
790 INSERT INTO rides (eid, ride)
791 VALUES (CURRVAL('events_eid_seq'::regclass)
794 -- Participant -- focal
795 INSERT INTO parts (eid, part, participant)
796 VALUES (CURRVAL('events_eid_seq'::regclass)
798 , this_follow.infant_id);
800 -- Participant -- ridee
801 INSERT INTO parts (eid, part, participant)
802 VALUES (CURRVAL('events_eid_seq'::regclass)
808 IF this_1_min_infant.rides_on_back IS NOT NULL THEN
809 CASE this_1_min_infant.rides_on_back
811 this_uncertain := default_infant_uncertain;
812 this_participant := this_follow.mother_id;
814 this_uncertain := TRUE;
815 this_participant := 'UNK';
818 this_uncertain := default_infant_uncertain;
819 this_participant := convert_animid(this_1_min_infant.rides_on_back, this_follow.follow_date);
822 INSERT INTO events (intid
828 VALUES (currval('intervals_intid_seq'::regclass)
835 INSERT INTO rides (eid, ride)
836 VALUES (CURRVAL('events_eid_seq'::regclass)
839 -- Participant -- focal
840 INSERT INTO parts (eid, part, participant)
841 VALUES (CURRVAL('events_eid_seq'::regclass)
843 , this_follow.infant_id);
845 -- Participant -- ridee
846 INSERT INTO parts (eid, part, participant)
847 VALUES (CURRVAL('events_eid_seq'::regclass)
854 IF this_1_min_infant.dangles IS NOT NULL THEN
855 IF (this_1_min_infant.dangles = '?'
856 AND (this_1_min_infant.nursing = '?'
857 OR this_1_min_infant.nursing IS NULL)
858 AND (this_1_min_infant.eating = '?'
859 OR this_1_min_infant.eating IS NULL)
860 AND (this_1_min_infant.walks = '?'
861 OR this_1_min_infant.walks IS NULL)
862 AND (this_1_min_infant.playing = '?'
863 OR this_1_min_infant.playing IS NULL)
864 AND (this_1_min_infant.rides_on_stomach = '?'
865 OR this_1_min_infant.rides_on_stomach IS NULL)
866 AND (this_1_min_infant.rides_on_back = '?'
867 OR this_1_min_infant.rides_on_back IS NULL)
868 AND (this_1_min_infant.grooms_other = '?'
869 OR this_1_min_infant.grooms_other IS NULL)
870 AND (this_1_min_infant.groomed_by = '?'
871 OR this_1_min_infant.groomed_by IS NULL)
872 AND (this_1_min_infant.plays = '?'
873 OR this_1_min_infant.plays IS NULL)
874 AND (this_1_min_infant.resting = '?'
875 OR this_1_min_infant.resting IS NULL))
876 OR (this_1_min_infant.dangles IS NOT NULL
877 AND this_1_min_infant.nursing IS NULL
878 AND this_1_min_infant.eating IS NULL
879 AND this_1_min_infant.walks IS NULL
880 AND this_1_min_infant.playing IS NULL
881 AND this_1_min_infant.rides_on_stomach IS NULL
882 AND this_1_min_infant.rides_on_back IS NULL
883 AND this_1_min_infant.grooms_other IS NULL
884 AND this_1_min_infant.groomed_by IS NULL
885 AND this_1_min_infant.plays IS NULL
886 AND this_1_min_infant.resting IS NULL) THEN
887 -- Can only be definatly dangling if not doing anything else
888 -- at all. Are allowed to be probably dangling so long as
889 -- everything else that might be happening is only probable.
891 CASE this_1_min_infant.dangles
893 this_uncertain := default_infant_uncertain;
894 this_participant := this_follow.mother_id;
896 this_uncertain := TRUE;
897 this_participant := 'UNK';
900 this_uncertain := default_infant_uncertain;
901 this_participant := convert_animid(this_1_min_infant.dangles
902 , this_follow.follow_date);
905 INSERT INTO events (intid
911 VALUES (currval('intervals_intid_seq'::regclass)
918 -- Participant -- focal
919 INSERT INTO parts (eid, part, participant)
920 VALUES (CURRVAL('events_eid_seq'::regclass)
922 , this_follow.infant_id);
924 -- Participant -- dangled from
925 INSERT INTO parts (eid, part, participant)
926 VALUES (CURRVAL('events_eid_seq'::regclass)
930 IF this_1_min_infant.resting IS NOT NULL
931 AND this_1_min_infant.nursing IS NULL
932 AND this_1_min_infant.eating IS NULL
933 AND this_1_min_infant.walks IS NULL
934 AND this_1_min_infant.playing IS NULL
935 AND this_1_min_infant.rides_on_stomach IS NULL
936 AND this_1_min_infant.rides_on_back IS NULL
937 AND this_1_min_infant.grooms_other IS NULL
938 AND this_1_min_infant.groomed_by IS NULL
939 AND this_1_min_infant.plays IS NULL THEN
940 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant infant_id %: Date %: Time %: Dangles %: There is both resting and dangling but no other on-the-minute activities'
941 , this_follow.follow_id
942 , this_1_min_infant.infant_id
943 , this_follow.follow_date
944 , this_1_min.interval_time
945 , this_1_min_infant.dangles;
953 IF this_1_min_infant.contacts_other IS NOT NULL THEN
954 -- It seems there can be more than one animid in contacts other.
955 animids := regexp_split_to_array(this_1_min_infant.contacts_other
957 FOREACH animid IN ARRAY animids LOOP
958 animid := convert_animid(TRIM(BOTH FROM animid)
959 , this_follow.follow_date);
960 this_baboon := FALSE;
963 this_participant := 'UNK';
965 this_uncertain := TRUE;
966 this_participant := 'UNK';
967 WHEN '_baboon_id' THEN
968 this_uncertain := default_infant_uncertain;
971 -- Another individual
972 this_uncertain := default_infant_uncertain;
973 this_participant := animid;
976 INSERT INTO events (intid
982 VALUES (currval('intervals_intid_seq'::regclass)
990 INSERT INTO parts (eid, part, participant)
991 VALUES (CURRVAL('events_eid_seq'::regclass)
993 , this_follow.infant_id);
995 IF NOT(this_baboon) THEN
996 -- Participant -- one touched
997 INSERT INTO parts (eid, part, participant)
998 VALUES (CURRVAL('events_eid_seq'::regclass)
1000 , this_participant);
1007 IF this_1_min_infant.grooms_other IS NOT NULL THEN
1009 animids := regexp_split_to_array(this_1_min_infant.grooms_other
1012 FOREACH animid IN ARRAY animids LOOP
1013 animid := convert_animid(TRIM(BOTH FROM animid)
1014 , this_follow.follow_date);
1015 this_uncertain := default_infant_uncertain;
1016 this_baboon := FALSE;
1017 this_participant := NULL;
1018 this_part := 'gmi_actor';
1022 this_participant := 'UNK';
1023 this_uncertain := TRUE;
1025 this_participant := 'UNK';
1026 WHEN this_follow.infant_id THEN
1028 this_part := 'gmi_self';
1029 WHEN '_baboon_id' THEN
1030 this_baboon := TRUE;
1032 this_participant := animid;
1035 -- Insert a row into events.
1036 INSERT INTO events (intid
1043 currval('intervals_intid_seq'::regclass)
1050 -- The infant participant.
1051 INSERT INTO parts (eid, part, participant)
1052 VALUES(currval('events_eid_seq'::regclass)
1053 , this_part::parts_part
1054 , this_follow.infant_id);
1056 IF this_participant IS NOT NULL THEN
1057 INSERT INTO parts (eid, part, participant)
1058 VALUES(currval('events_eid_seq'::regclass)
1060 , this_participant);
1062 -- Remember grooming of mom or sib.
1063 CASE this_participant
1064 WHEN this_follow.mother_id THEN
1065 infant_mom_behaviors := infant_mom_behaviors || '{grooms another}';
1066 infant_mom_uncertains
1067 := infant_mom_uncertains || this_uncertain;
1069 := infant_mom_events
1070 || currval('events_eid_seq'::regclass)::INT;
1071 WHEN this_follow.sibling_id THEN
1072 infant_sib_behaviors := infant_sib_behaviors || '{grooms another}';
1073 infant_sib_uncertains
1074 := infant_sib_uncertains || this_uncertain;
1076 := infant_sib_events
1077 || currval('events_eid_seq'::regclass)::INT;
1081 END IF; -- NULL participant
1082 END LOOP; -- Over grooms_other
1087 IF this_1_min_infant.groomed_by IS NOT NULL THEN
1089 animids := regexp_split_to_array(this_1_min_infant.groomed_by
1092 FOREACH animid IN ARRAY animids LOOP
1093 animid := convert_animid(TRIM(BOTH FROM animid)
1094 , this_follow.follow_date);
1095 this_uncertain := default_infant_uncertain;
1096 this_baboon := FALSE;
1100 this_participant := 'UNK';
1101 this_uncertain := TRUE;
1103 this_participant := 'UNK';
1104 WHEN this_follow.infant_id THEN
1105 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant infant_id %: Date %: Time %: Groomed_by %: The focal (the infant) appears in the Groomed_by column'
1106 , this_follow.follow_id
1107 , this_1_min_infant.infant_id
1108 , this_follow.follow_date
1109 , this_1_min.interval_time
1110 , this_1_min_infant.groomed_by;
1111 WHEN '_baboon_id' THEN
1112 this_baboon := TRUE;
1114 this_participant := animid;
1117 -- Insert a row into events.
1118 INSERT INTO events (intid
1125 currval('intervals_intid_seq'::regclass)
1132 -- The infant participant.
1133 INSERT INTO parts (eid, part, participant)
1134 VALUES(currval('events_eid_seq'::regclass)
1136 , this_follow.infant_id);
1138 IF NOT(this_baboon) THEN
1139 INSERT INTO parts (eid, part, participant)
1140 VALUES(currval('events_eid_seq'::regclass)
1142 , this_participant);
1144 -- Remember grooming by mom or sib.
1145 CASE this_participant
1146 WHEN this_follow.mother_id THEN
1147 infant_mom_behaviors := infant_mom_behaviors || '{groomed by another}';
1148 infant_mom_uncertains
1149 := infant_mom_uncertains || this_uncertain;
1151 := infant_mom_events
1152 || currval('events_eid_seq'::regclass)::INT;
1153 WHEN this_follow.sibling_id THEN
1154 infant_sib_behaviors := infant_sib_behaviors || '{groomed by another}';
1155 infant_sib_uncertains
1156 := infant_sib_uncertains || this_uncertain;
1158 := infant_sib_events
1159 || currval('events_eid_seq'::regclass)::INT;
1164 END LOOP; -- Over groomed_by
1168 IF this_1_min_infant.plays IS NOT NULL THEN
1170 animids := regexp_split_to_array(this_1_min_infant.plays
1173 FOREACH animid IN ARRAY animids LOOP
1174 animid := convert_animid(TRIM(BOTH FROM animid)
1175 , this_follow.follow_date);
1176 this_uncertain := default_infant_uncertain;
1177 this_baboon := FALSE;
1181 this_participant := 'UNK';
1182 this_uncertain := TRUE;
1184 this_participant := 'UNK';
1185 WHEN this_follow.infant_id THEN
1186 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant infant_id %: Date %: Time %: Plays %: The focal (the infant) appears in the Plays column'
1187 , this_follow.follow_id
1188 , this_1_min_infant.infant_id
1189 , this_follow.follow_date
1190 , this_1_min.interval_time
1191 , this_1_min_infant.plays;
1192 WHEN '_baboon_id' THEN
1193 this_baboon := TRUE;
1194 this_participant := NULL;
1196 this_participant := animid;
1199 -- Figure out whether play is directed or mutual.
1200 IF this_participant = this_follow.mother_id
1201 OR this_participant = this_follow.sibling_id THEN
1202 this_part1 := 'gmi_actor';
1203 this_part2 := 'gmi_actee';
1205 this_part1 := 'gmi_participant';
1206 this_part2 := 'gmi_participant';
1209 -- Insert a row into events.
1210 INSERT INTO events (intid
1217 currval('intervals_intid_seq'::regclass)
1225 INSERT INTO plays (eid, play)
1226 VALUES(currval('events_eid_seq'::regclass)
1229 -- The infant participant.
1230 INSERT INTO parts (eid, part, participant)
1231 VALUES(currval('events_eid_seq'::regclass)
1233 , this_follow.infant_id);
1235 IF NOT(this_baboon) THEN
1236 INSERT INTO parts (eid, part, participant)
1237 VALUES(currval('events_eid_seq'::regclass)
1239 , this_participant);
1242 -- Remember play with mom or sib.
1243 CASE this_participant
1244 WHEN this_follow.mother_id THEN
1245 infant_mom_behaviors := infant_mom_behaviors || '{play}';
1246 infant_mom_uncertains
1247 := infant_mom_uncertains || this_uncertain;
1249 := infant_mom_events
1250 || currval('events_eid_seq'::regclass)::INT;
1251 WHEN this_follow.sibling_id THEN
1252 infant_sib_behaviors := infant_sib_behaviors || '{play}';
1253 infant_sib_uncertains
1254 := infant_sib_uncertains || this_uncertain;
1256 := infant_sib_events
1257 || currval('events_eid_seq'::regclass)::INT;
1261 END LOOP; -- Over plays
1266 IF this_1_min_infant.displays IS NOT NULL THEN
1267 CASE this_1_min_infant.displays
1269 this_uncertain := default_infant_uncertain;
1271 this_uncertain := TRUE;
1273 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: Displays %: Displays code unrecognized'
1274 , this_follow.follow_id
1275 , this_1_min_infant.infant_id
1276 , this_follow.follow_date
1277 , this_1_min.interval_time
1278 , this_1_min_infant.displays;
1281 INSERT INTO events (intid
1287 VALUES (currval('intervals_intid_seq'::regclass)
1295 INSERT INTO parts (eid, part, participant)
1296 VALUES (CURRVAL('events_eid_seq'::regclass)
1298 , this_follow.infant_id);
1302 IF this_1_min_infant.refused IS NOT NULL THEN
1303 CASE this_1_min_infant.refused
1305 this_uncertain := default_infant_uncertain;
1307 this_uncertain := TRUE;
1309 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: Refused %: Refused code unrecognized'
1310 , this_follow.follow_id
1311 , this_1_min_infant.infant_id
1312 , this_follow.follow_date
1313 , this_1_min.interval_time
1314 , this_1_min_infant.refused;
1317 INSERT INTO events (intid
1323 VALUES (currval('intervals_intid_seq'::regclass)
1331 INSERT INTO parts (eid, part, participant)
1332 VALUES (CURRVAL('events_eid_seq'::regclass)
1334 , this_follow.infant_id);
1336 -- Other participant is unknown until it's pulled from the comments.
1337 INSERT INTO parts (eid, part, participant)
1338 VALUES (CURRVAL('events_eid_seq'::regclass)
1345 IF this_1_min_infant.begs IS NOT NULL THEN
1346 this_baboon := FALSE;
1347 CASE this_1_min_infant.begs
1349 this_uncertain := default_infant_uncertain;
1350 this_participant := 'UNK';
1352 this_uncertain := TRUE;
1353 this_participant := 'UNK';
1354 WHEN '_baboon_id' THEN
1355 this_baboon := TRUE;
1357 -- Another individual
1358 this_uncertain := default_infant_uncertain;
1359 this_participant := convert_animid(this_1_min_infant.begs
1360 , this_follow.follow_date);
1363 INSERT INTO events (intid
1369 VALUES (currval('intervals_intid_seq'::regclass)
1377 INSERT INTO parts (eid, part, participant)
1378 VALUES (CURRVAL('events_eid_seq'::regclass)
1380 , this_follow.infant_id);
1382 IF NOT(this_baboon) THEN
1383 -- Participant -- one begged from
1384 INSERT INTO parts (eid, part, participant)
1385 VALUES (CURRVAL('events_eid_seq'::regclass)
1387 , this_participant);
1393 IF this_1_min_infant.aggression IS NOT NULL THEN
1394 this_baboon := FALSE;
1395 CASE this_1_min_infant.aggression
1397 this_uncertain := default_infant_uncertain;
1398 this_participant := 'UNK';
1400 this_uncertain := TRUE;
1401 this_participant := 'UNK';
1402 WHEN '_baboon_id' THEN
1403 this_baboon := TRUE;
1405 -- Another individual
1406 this_uncertain := default_infant_uncertain;
1407 this_participant := convert_animid(this_1_min_infant.aggression
1408 , this_follow.follow_date);
1411 INSERT INTO events (intid
1417 VALUES (currval('intervals_intid_seq'::regclass)
1425 INSERT INTO parts (eid, part, participant)
1426 VALUES (CURRVAL('events_eid_seq'::regclass)
1428 , this_follow.infant_id);
1430 IF NOT(this_baboon) THEN
1431 -- Participant -- one agressed against
1432 INSERT INTO parts (eid, part, participant)
1433 VALUES (CURRVAL('events_eid_seq'::regclass)
1435 , this_participant);
1441 IF this_1_min.comment ~ 'DEVBEH!'
1443 -- happening at the minute?
1444 AND this_1_min_infant.nursing IS NULL
1445 AND this_1_min_infant.eating IS NULL
1446 AND this_1_min_infant.walks IS NULL
1447 AND this_1_min_infant.playing IS NULL
1448 AND this_1_min_infant.resting IS NULL
1449 AND this_1_min_infant.rides_on_stomach IS NULL
1450 AND this_1_min_infant.rides_on_back IS NULL
1451 AND this_1_min_infant.dangles IS NULL
1452 AND this_1_min_infant.contacts_other IS NULL
1453 AND (this_1_min_infant.grooms_other IS NULL
1454 OR TRIM(BOTH FROM this_1_min_infant.grooms_other) = '')
1455 AND (this_1_min_infant.groomed_by IS NULL
1456 OR TRIM(BOTH FROM this_1_min_infant.groomed_by) = '')
1457 AND this_1_min_infant.plays IS NULL THEN
1459 IF this_1_min.comment !~ 'DEVBEH!\[.+\]' THEN
1460 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: follow_1_minute.Comment %: Invalid DEVBEH! comment format'
1461 , this_follow.follow_id
1462 , this_1_min_infant.infant_id
1463 , this_follow.follow_date
1464 , this_1_min.interval_time
1465 , this_1_min.comment;
1468 -- Take the devebeh out of the interval comment and
1469 -- put it into the event comment.
1470 IF interval_comment IS NULL THEN
1471 this_devbeh := NULL;
1473 this_devbeh := SUBSTRING(interval_comment FROM 'DEVBEH!\[.+\]');
1474 IF this_devbeh IS NOT NULL THEN
1475 -- Remove the devbeh from the comment.
1476 interval_comment := split_part(interval_comment, this_devbeh, 1)
1477 || split_part(interval_comment
1480 IF TRIM(BOTH FROM interval_comment) = '' THEN
1481 interval_comment := NULL;
1484 -- Save the new interval comment.
1486 SET comment = interval_comment
1488 intervals.intid = CURRVAL('intervals_intid_seq'::regclass);
1493 INSERT INTO events (intid
1499 VALUES (currval('intervals_intid_seq'::regclass)
1503 , default_infant_uncertain
1506 -- Participant -- focal
1507 INSERT INTO parts (eid, part, participant)
1508 VALUES (CURRVAL('events_eid_seq'::regclass)
1510 , this_follow.infant_id);
1515 IF this_1_min_infant.in_contact IS NOT NULL
1516 OR this_1_min_infant.within_reach IS NOT NULL
1517 OR this_1_min_infant.within_0_5_meters IS NOT NULL
1518 OR this_1_min_infant.between_5_15_meters IS NOT NULL
1519 OR this_1_min_infant.more_than_15_meters IS NOT NULL THEN
1521 this_error := FALSE;
1522 this_distance := NULL;
1523 this_uncertain := default_infant_uncertain;
1525 IF this_1_min_infant.in_contact IS NOT NULL THEN
1526 this_distance := '1';
1527 CASE this_1_min_infant.in_contact
1530 this_uncertain := TRUE;
1532 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: In_contact %: In_contact code unrecognized'
1533 , this_follow.follow_id
1534 , this_1_min_infant.infant_id
1535 , this_follow.follow_date
1536 , this_1_min.interval_time
1537 , this_1_min_infant.in_contact;
1541 IF this_1_min_infant.within_reach IS NOT NULL THEN
1542 this_error := this_error OR this_distance IS NOT NULL;
1543 this_distance := '2';
1544 CASE this_1_min_infant.within_reach
1547 this_uncertain := TRUE;
1549 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: Within_reach %: Within_reach code unrecognized'
1550 , this_follow.follow_id
1551 , this_1_min_infant.infant_id
1552 , this_follow.follow_date
1553 , this_1_min.interval_time
1554 , this_1_min_infant.within_reach;
1558 IF this_1_min_infant.within_0_5_meters IS NOT NULL THEN
1559 this_error := this_error OR this_distance IS NOT NULL;
1560 this_distance := '3';
1561 CASE this_1_min_infant.within_0_5_meters
1564 this_uncertain := TRUE;
1566 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: Within_0_5_meters %: Within_0_5_meters code unrecognized'
1567 , this_follow.follow_id
1568 , this_1_min_infant.infant_id
1569 , this_follow.follow_date
1570 , this_1_min.interval_time
1571 , this_1_min_infant.within_0_5_meters;
1575 IF this_1_min_infant.between_5_15_meters IS NOT NULL THEN
1576 this_error := this_error OR this_distance IS NOT NULL;
1577 this_distance := '4';
1578 CASE this_1_min_infant.between_5_15_meters
1581 this_uncertain := TRUE;
1583 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: Between_5_15_meters %: Between_5_15_meters code unrecognized'
1584 , this_follow.follow_id
1585 , this_1_min_infant.infant_id
1586 , this_follow.follow_date
1587 , this_1_min.interval_time
1588 , this_1_min_infant.between_5_15_meters;
1592 IF this_1_min_infant.more_than_15_meters IS NOT NULL THEN
1593 this_error := this_error OR this_distance IS NOT NULL;
1594 this_distance := '5';
1595 CASE this_1_min_infant.more_than_15_meters
1598 this_uncertain := TRUE;
1600 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: More_than_15_meters %: More_than_15_meters code unrecognized'
1601 , this_follow.follow_id
1602 , this_1_min_infant.infant_id
1603 , this_follow.follow_date
1604 , this_1_min.interval_time
1605 , this_1_min_infant.more_than_15_meters;
1610 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: There is more than one infant distance column (In_contact, Within_reach, Within_0_5_meters, Between_5_15_meters, More_than_15_meters) non-null'
1611 , this_follow.follow_id
1612 , this_1_min_infant.infant_id
1613 , this_follow.follow_date
1614 , this_1_min.interval_time;
1617 INSERT INTO distances (intid, uncertain, distance)
1618 VALUES (currval('intervals_intid_seq'::regclass)
1623 INSERT INTO distanceparts (distid, role, participant)
1624 VALUES (CURRVAL('distances_distid_seq'::regclass)
1626 , this_follow.infant_id);
1629 INSERT INTO distanceparts (distid, role, participant)
1630 VALUES (CURRVAL('distances_distid_seq'::regclass)
1632 , this_follow.mother_id);
1635 -- Vocalizations and cries can't happen together.
1636 IF this_1_min_infant.cries IS NOT NULL
1637 AND this_1_min_infant.vocalizations IS NOT NULL THEN
1638 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: Cries %: Vocalizations %: Infant has both cries and vocalizations'
1639 , this_follow.follow_id
1640 , this_1_min_infant.infant_id
1641 , this_follow.follow_date
1642 , this_1_min.interval_time
1643 , this_1_min_infant.cries
1644 , this_1_min_infant.vocalizations;
1648 IF this_1_min_infant.cries IS NOT NULL THEN
1649 CASE this_1_min_infant.cries
1651 this_uncertain := default_infant_uncertain;
1653 this_uncertain := TRUE;
1655 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: Cries %: Cries code unrecognized'
1656 , this_follow.follow_id
1657 , this_1_min_infant.infant_id
1658 , this_follow.follow_date
1659 , this_1_min.interval_time
1660 , this_1_min_infant.cries;
1663 INSERT INTO events (intid
1669 VALUES (currval('intervals_intid_seq'::regclass)
1676 INSERT INTO vocs (eid, vocalization)
1677 VALUES (CURRVAL('events_eid_seq'::regclass)
1681 INSERT INTO parts (eid, part, participant)
1682 VALUES (CURRVAL('events_eid_seq'::regclass)
1684 , this_follow.infant_id);
1687 -- Infant vocalizations.
1688 IF this_1_min_infant.vocalizations IS NOT NULL THEN
1690 old_voc := TRIM(BOTH FROM this_1_min_infant.vocalizations);
1693 this_voc_uncertain := default_infant_uncertain;
1695 -- Convert the vocalization code.
1741 this_voc_uncertain := TRUE;
1744 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: Vocalizations %: The infant vocalization (%) is unknown'
1745 , this_follow.follow_id
1746 , this_1_min_infant.infant_id
1747 , this_follow.follow_date
1748 , this_1_min.interval_time
1749 , this_1_min_infant.vocalization
1754 -- Save vocalization event.
1755 INSERT INTO events (intid
1762 currval('intervals_intid_seq'::regclass)
1766 , this_voc_uncertain
1769 INSERT INTO vocs (eid, vocalization)
1770 VALUES (currval('events_eid_seq'::regclass)
1774 INSERT INTO parts (eid, part, participant)
1775 VALUES (CURRVAL('events_eid_seq'::regclass)
1777 , this_follow.infant_id);
1779 END IF; -- There is a infant vocalization.
1782 -- follow_1_minute_mother:
1787 FROM follow_1_minute_mother
1788 WHERE follow_1_minute_mother.interval_id = this_1_min.interval_id;
1790 -- | Behavior | Grooms_other | Groomed_by |
1792 -- tableid | Interval_id | Mother_id | Behavior | Grooms_other | Groomed_by | Vocalizations
1793 -- 1 | 11/08/2010_GM_01_01 | GM | Rest | | |
1795 -- Mom sanity check.
1796 IF this_follow.mother_id != this_1_min_mom.mother_id THEN
1797 RAISE EXCEPTION 'Follow Mother_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: The mother id in the follow does not match the mother id in the Follow_1_minute_mother'
1798 , this_follow.mother_id
1799 , this_1_min_mom.mother_id
1800 , this_follow.follow_date
1801 , this_1_min.interval_time;
1804 -- Check for already converted.
1805 IF this_1_min_mom.done THEN
1806 RAISE EXCEPTION 'Date %: Mom %: follow_1_minute_mother row lready converted'
1807 , this_follow.follow_date
1808 , this_follow.mother_id;
1815 grooms_other_converted := FALSE;
1816 groomed_by_converted := FALSE;
1817 multi_found := FALSE;
1818 grooming_and_play := FALSE;
1819 gp_unknown := FALSE;
1820 IF this_1_min_mom.behavior IS NOT NULL THEN
1821 -- Record mom's behavior(s)
1823 -- Split into separate codes.
1824 behaviors := regexp_split_to_array(this_1_min_mom.behavior, ';');
1826 -- Go through all the behaviors ahead of time so we know
1827 -- what we've got and what's implied.
1828 grooming_found := FALSE;
1829 play_found := FALSE;
1830 FOREACH old_behavior IN ARRAY behaviors LOOP
1831 old_behavior := TRIM(BOTH FROM old_behavior);
1834 WHEN 'Grooming' THEN
1835 grooming_found := TRUE;
1844 -- Test for grooming and play mixed.
1845 grooming_and_play := grooming_found AND play_found;
1847 -- Initialize storage
1848 event_behavior := ARRAY[]::TEXT[];
1849 event_uncertain := ARRAY[]::BOOLEAN[];
1850 event_baboon := ARRAY[]::BOOLEAN[];
1851 event_comment := ARRAY[]::TEXT[];
1852 part1_parts := ARRAY[]::TEXT[];
1853 part1_participants := ARRAY[]::TEXT[];
1854 part2_parts := ARRAY[]::TEXT[];
1855 part2_participants := ARRAY[]::TEXT[];
1858 FOREACH old_behavior IN ARRAY behaviors LOOP
1860 old_behavior := TRIM(BOTH FROM old_behavior);
1861 this_uncertain := default_mom_uncertain;
1866 new_behavior := 'UNK';
1867 this_uncertain := TRUE;
1870 CONTINUE; -- no event
1873 new_behavior := 'ASSR';
1876 new_behavior := 'BAD';
1877 this_uncertain := TRUE;
1879 WHEN 'Build nest' THEN
1880 new_behavior := 'BN';
1883 new_behavior := 'C';
1886 new_behavior := 'DR';
1889 new_behavior := '_ca_eating';
1892 new_behavior := 'G';
1894 WHEN 'Grooming' THEN
1895 new_behavior := 'G';
1898 new_behavior := 'MULTI';
1899 multi_found := TRUE;
1901 WHEN 'Multigroom' THEN
1902 new_behavior := 'G';
1905 new_behavior := 'O';
1908 new_behavior := 'P';
1911 new_behavior := 'R';
1913 WHEN 'Self-groom' THEN
1914 new_behavior := 'G';
1917 new_behavior := 'T';
1920 new_behavior := 'VUA';
1923 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Behavior %: The mother behavior (%) is unrecognized'
1924 , this_follow.follow_id
1925 , this_1_min_mom.mother_id
1926 , this_follow.follow_date
1927 , this_1_min.interval_time
1928 , this_1_min_mom.behavior
1933 eventcnt := eventcnt + 1;
1934 event_uncertain[eventcnt] := this_uncertain;
1935 event_baboon[eventcnt] := FALSE;
1936 event_comment[eventcnt] := NULL;
1937 part1_parts[eventcnt] := 'gmi_self';
1938 part1_participants[eventcnt] := this_follow.mother_id;
1939 part2_parts[eventcnt] := NULL;
1941 IF old_behavior = 'Grooming'
1942 AND TRIM(BOTH FROM this_1_min_mom.grooms_other)
1943 = this_follow.mother_id THEN
1944 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Behavior %: The mother has a Grooming behavior but Grooms_other contains only the mother''s id'
1945 , this_follow.follow_id
1946 , this_1_min_mom.mother_id
1947 , this_follow.follow_date
1948 , this_1_min.interval_time
1949 , this_1_min_mom.behavior;
1952 event_behavior[eventcnt] := new_behavior;
1954 IF old_behavior = 'Grooming'
1955 OR old_behavior = 'Multigroom'
1956 OR old_behavior = 'Multi'
1957 OR old_behavior = 'Play' THEN
1958 IF this_1_min_mom.grooms_other IS NOT NULL THEN
1959 -- Record the groomed (or play partners) individuals.
1960 grooms_other_converted := TRUE;
1961 animids := regexp_split_to_array(this_1_min_mom.grooms_other
1963 animid_cnt := ARRAY_LENGTH(animids, 1);
1965 IF old_behavior != 'Multi' THEN
1966 -- Leave the multi behavior on the mom but
1967 -- make grooming and play behaviors.
1968 eventcnt := eventcnt - 1;
1970 FOREACH animid IN ARRAY animids LOOP
1971 eventcnt := eventcnt + 1;
1972 animid := convert_animid(TRIM(BOTH FROM animid)
1973 , this_follow.follow_date);
1974 -- The animal got groomed by the mom.
1975 IF old_behavior = 'Multi' THEN
1976 -- Make both grooming and play behaviors.
1977 -- First the play behavior.
1978 event_behavior[eventcnt] := 'P';
1980 event_behavior[eventcnt] := new_behavior;
1982 event_uncertain[eventcnt] := default_mom_uncertain;
1983 event_baboon[eventcnt] := FALSE;
1984 event_comment[eventcnt] := NULL;
1985 IF (old_behavior = 'Play'
1986 OR old_behavior = 'Multi') -- doing play first.
1987 AND animid != this_follow.infant_id
1988 AND (animid != this_follow.sibling_id
1989 OR this_follow.sibling_id IS NULL) THEN
1990 part1_parts[eventcnt] := 'gmi_participant';
1991 part2_parts[eventcnt] := 'gmi_participant';
1993 part1_parts[eventcnt] := 'gmi_actor';
1994 part2_parts[eventcnt] := 'gmi_actee';
1996 part1_participants[eventcnt] := this_follow.mother_id;
1999 part2_participants[eventcnt] := 'UNK';
2000 event_uncertain[eventcnt] := TRUE;
2002 part2_participants[eventcnt] := 'UNK';
2003 WHEN this_follow.mother_id THEN
2004 -- Self grooming or self-play.
2005 part1_parts[eventcnt] := 'gmi_self';
2006 part2_parts[eventcnt] := NULL;
2007 IF grooming_and_play THEN
2008 event_comment[eventcnt] := 'convert_access(): Both self-grooming and solitary play because of grooming+play in access db.';
2011 IF (grooming_and_play
2013 AND (old_behavior = 'Grooming'
2014 OR old_behavior = 'Play')) THEN
2015 -- Can't tell the who played and who groomed.
2016 part2_participants[eventcnt] := 'UNK';
2017 IF old_behavior = 'Play' THEN
2018 -- Force interaction as participant as non focals
2019 -- (UNK) require this.
2020 part1_parts[eventcnt] = 'gmi_participant';
2021 part2_parts[eventcnt] = 'gmi_participant';
2023 event_comment[eventcnt] := 'convert_access(): Changed ('
2024 || this_1_min_mom.grooms_other
2025 || ') to (UNK) and ignored mutiple animids due to grooming occurring with play';
2027 EXIT; -- Do only 1 animid.
2028 ELSIF old_behavior = 'Multi' THEN
2029 part2_participants[eventcnt] := 'UNK';
2030 event_comment[eventcnt] := 'convert_access(): Changed ('
2031 || this_1_min_mom.grooms_other
2032 || ') to (UNK) due to Multi';
2033 -- Force interaction as participant as non focals
2034 -- (UNK) require this.
2035 part1_parts[eventcnt] = 'gmi_participant';
2036 part2_parts[eventcnt] = 'gmi_participant';
2039 -- That takes care of the play event for Multi
2040 -- Also make a grooming event.
2041 eventcnt := eventcnt + 1;
2042 event_behavior[eventcnt] := 'G';
2043 event_uncertain[eventcnt] := default_mom_uncertain;
2044 event_baboon[eventcnt] := FALSE;
2045 event_comment[eventcnt] := 'convert_access(): Changed ('
2046 || this_1_min_mom.grooms_other
2047 || ') to (UNK) due to Multi';
2048 part1_parts[eventcnt] := 'gmi_actor';
2049 part2_parts[eventcnt] := 'gmi_actee';
2050 part1_participants[eventcnt] := this_follow.mother_id;
2051 part2_participants[eventcnt] := 'UNK';
2052 ELSIF animid = '_baboon_id' THEN
2053 event_baboon[eventcnt] := TRUE;
2054 part2_parts[eventcnt] := NULL;
2056 part2_participants[eventcnt] := animid;
2060 END LOOP; -- Over grooms_other
2061 ELSE -- Grooms_other is NULL
2062 IF old_behavior != 'Multi' THEN
2063 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Behavior %: The Grooms_other column cannot be empty for play or grooming or multigroom'
2064 , this_follow.follow_id
2065 , this_1_min_mom.mother_id
2066 , this_follow.follow_date
2067 , this_1_min.interval_time
2068 , this_1_min_mom.behavior;
2073 IF old_behavior = 'Groomed'
2074 OR old_behavior = 'Multi'
2075 OR old_behavior = 'Multigroom' THEN
2076 IF this_1_min_mom.groomed_by IS NOT NULL THEN
2077 -- Record the individuals doing the grooming.
2078 groomed_by_converted := TRUE;
2079 animids := regexp_split_to_array(this_1_min_mom.groomed_by
2081 IF this_1_min_mom.grooms_other IS NULL
2082 AND old_behavior != 'Multi' THEN
2083 -- Have not yet seen multiple individuals.
2084 -- (The multi retains it's own event as a 'self' event.)
2085 eventcnt := eventcnt - 1;
2087 FOREACH animid IN ARRAY animids LOOP
2088 eventcnt := eventcnt + 1;
2089 animid := convert_animid(TRIM(BOTH FROM animid)
2090 , this_follow.follow_date);
2091 -- The animal groomed the mom.
2092 IF old_behavior = 'Multi' THEN
2093 -- We've already recorded the multi
2094 -- Now we want a grooming event for each grooming partner.
2095 event_behavior[eventcnt] := 'G';
2096 event_comment[eventcnt] := 'convert_access(): Mom groomed due to Multi';
2098 event_behavior[eventcnt] := new_behavior;
2099 event_comment[eventcnt] := NULL;
2101 event_uncertain[eventcnt] := default_mom_uncertain;
2102 event_baboon[eventcnt] := FALSE;
2103 part1_parts[eventcnt] := 'gmi_actee';
2104 part1_participants[eventcnt] := this_follow.mother_id;
2105 part2_parts[eventcnt] := 'gmi_actor';
2108 part2_participants[eventcnt] := 'UNK';
2109 event_uncertain[eventcnt] := TRUE;
2111 part2_participants[eventcnt] := 'UNK';
2112 WHEN this_follow.mother_id THEN
2113 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Behavior %: Groomed_by %: The focal (the mother) appears in the Groomed_by column'
2114 , this_follow.follow_id
2115 , this_1_min_mom.mother_id
2116 , this_follow.follow_date
2117 , this_1_min.interval_time
2118 , this_1_min_mom.behavior
2119 , this_1_min_mom.groomed_by;
2120 WHEN '_baboon_id' THEN
2121 event_baboon[eventcnt] := TRUE;
2122 part2_parts[eventcnt] := NULL;
2124 part2_participants[eventcnt] := animid;
2126 END LOOP; -- Over groomed_by
2127 ELSE -- Groomed_by is NULL
2128 IF old_behavior != 'Multi' THEN
2129 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Behavior %: The Groomed_by column cannot be empty for groomed or multigroom'
2130 , this_follow.follow_id
2131 , this_1_min_mom.mother_id
2132 , this_follow.follow_date
2133 , this_1_min.interval_time
2134 , this_1_min_mom.behavior;
2138 END LOOP; -- Over mom behaviors
2140 alen := ARRAY_LENGTH(part1_parts, 1);
2141 IF alen IS NOT NULL THEN
2142 -- Go thorough all the participants and make events.
2143 FOR i IN 1 .. alen LOOP
2144 --debug-- raise notice 'time %: behavior %: part1 %: role1 %: part2 %: role2 %', this_1_min.interval_time, event_behavior[i], part1_participants[i], part1_parts[i], part2_participants[i], part2_parts[i];
2147 IF event_behavior[i] = 'G'
2148 AND ((part1_participants[i] = this_follow.mother_id
2149 AND part2_participants[i] = this_follow.infant_id
2150 AND part1_parts[i] = 'gmi_actor'
2151 AND part2_parts[i] = 'gmi_actee')
2152 OR (part2_participants[i] = this_follow.mother_id
2153 AND part1_participants[i] = this_follow.infant_id
2154 AND part2_parts[i] = 'gmi_actor'
2155 AND part1_parts[i] = 'gmi_actee')) THEN
2156 -- The mom groomed the infant.
2159 behavior_cnt := ARRAY_LENGTH(infant_mom_behaviors, 1);
2160 IF behavior_cnt IS NOT NULL AND behavior_cnt != 0 THEN
2161 FOR j IN 1 .. behavior_cnt LOOP
2162 IF infant_mom_behaviors[j] = 'groomed by another' THEN
2163 -- We've already recorded this behavior.
2164 IF infant_mom_uncertains[j] != event_uncertain[i]
2165 AND NOT(infant_mom_uncertains[j]) THEN
2166 -- Must make the recorded behavior uncertain.
2168 SET uncertain = TRUE
2169 WHERE events.eid = infant_mom_events[j];
2172 -- Note that we've done with it by removing the array
2174 infant_mom_behaviors := infant_mom_behaviors[1:j - 1]
2175 || infant_mom_behaviors[j+1:behavior_cnt];
2176 infant_mom_uncertains := infant_mom_uncertains[1:j - 1]
2177 || infant_mom_uncertains[j+1:behavior_cnt];
2178 infant_mom_events := infant_mom_events[1:j - 1]
2179 || infant_mom_events[j+1:behavior_cnt];
2186 IF NOT(i_found) THEN
2187 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Behavior %: Grooms_other %: The follow_1_minute_mother says the mother groomed the infant but the follow_1_minute_infant has no record of this'
2188 , this_follow.follow_id
2189 , this_1_min_mom.mother_id
2190 , this_follow.follow_date
2191 , this_1_min.interval_time
2192 , this_1_min_mom.behavior
2193 , this_1_min_mom.grooms_other;
2195 ELSIF event_behavior[i] = 'G'
2196 AND ((part1_participants[i] = this_follow.mother_id
2197 AND part2_participants[i] = this_follow.infant_id
2198 AND part1_parts[i] = 'gmi_actee'
2199 AND part2_parts[i] = 'gmi_actor')
2200 OR (part2_participants[i] = this_follow.mother_id
2201 AND part1_participants[i] = this_follow.infant_id
2202 AND part2_parts[i] = 'gmi_actee'
2203 AND part1_parts[i] = 'gmi_actor')) THEN
2204 -- The infant groomed the mom.
2207 behavior_cnt := ARRAY_LENGTH(infant_mom_behaviors, 1);
2208 IF behavior_cnt IS NOT NULL AND behavior_cnt != 0 THEN
2209 FOR j IN 1 .. behavior_cnt LOOP
2210 IF infant_mom_behaviors[j] = 'grooms another' THEN
2211 -- We've already recorded this behavior.
2212 IF infant_mom_uncertains[j] != event_uncertain[i]
2213 AND NOT(infant_mom_uncertains[j]) THEN
2214 -- Must make the recorded behavior uncertain.
2216 SET uncertain = TRUE
2217 WHERE events.eid = infant_mom_events[j];
2220 -- Note that we've done with it by removing the array
2222 infant_mom_behaviors := infant_mom_behaviors[1:j - 1]
2223 || infant_mom_behaviors[j+1:behavior_cnt];
2224 infant_mom_uncertains := infant_mom_uncertains[1:j - 1]
2225 || infant_mom_uncertains[j+1:behavior_cnt];
2226 infant_mom_events := infant_mom_events[1:j - 1]
2227 || infant_mom_events[j+1:behavior_cnt];
2234 IF NOT(i_found) THEN
2235 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Behavior %: Groomed_by %: The follow_1_minute_mother says the infant groomed the mother but the follow_1_minute_infant has no record of this'
2236 , this_follow.follow_id
2237 , this_1_min_mom.mother_id
2238 , this_follow.follow_date
2239 , this_1_min.interval_time
2240 , this_1_min_mom.behavior
2241 , this_1_min_mom.groomed_by;
2244 ELSIF event_behavior[i] = 'P'
2245 AND ((part1_participants[i] = this_follow.mother_id
2246 AND part2_participants[i] = this_follow.infant_id
2247 AND part1_parts[i] = 'gmi_actor'
2248 AND part2_parts[i] = 'gmi_actee')
2249 OR (part2_participants[i] = this_follow.mother_id
2250 AND part1_participants[i] = this_follow.infant_id
2251 AND part2_parts[i] = 'gmi_actor'
2252 AND part1_parts[i] = 'gmi_actee')) THEN
2253 -- The mom played with the infant.
2255 behavior_cnt := ARRAY_LENGTH(infant_mom_behaviors, 1);
2256 IF behavior_cnt IS NOT NULL AND behavior_cnt != 0 THEN
2257 FOR j IN 1 .. behavior_cnt LOOP
2258 IF infant_mom_behaviors[j] = 'play' THEN
2259 -- The infant played with the mom.
2260 -- Make the play mutual.
2262 SET part = 'gmi_participant'
2263 WHERE parts.eid = infant_mom_events[j];
2265 IF infant_mom_uncertains[j] != event_uncertain[i]
2266 AND NOT(infant_mom_uncertains[j]) THEN
2267 -- Must make the recorded behavior uncertain.
2269 SET uncertain = TRUE
2270 WHERE events.eid = infant_mom_events[j];
2279 -- Insert a row into events.
2280 INSERT INTO events (intid
2287 currval('intervals_intid_seq'::regclass)
2289 , _gen_pos(event_behavior[i])
2291 , event_uncertain[i]
2292 , event_comment[i]);
2294 -- Now save the participant(s).
2295 INSERT INTO parts (eid, part, participant)
2296 VALUES(currval('events_eid_seq'::regclass)
2297 , part1_parts[i]::parts_part
2298 , part1_participants[i]);
2300 IF part2_parts[i] IS NOT NULL THEN
2301 INSERT INTO parts (eid, part, participant)
2302 VALUES(currval('events_eid_seq'::regclass)
2303 , part2_parts[i]::parts_part
2304 , part2_participants[i]);
2306 IF part2_participants[i] = this_follow.sibling_id THEN
2307 -- Remember sibling interactions.
2308 IF part1_parts[i] = 'gmi_actee'
2309 AND event_behavior[i] = 'G' THEN
2310 -- The mom was groomed by the sib.
2311 mom_sib_behaviors = mom_sib_behaviors
2312 || '{groomed by another}';
2313 mom_sib_uncertains = mom_sib_uncertains
2314 || event_uncertain[i];
2315 ELSIF part1_parts[i] = 'gmi_actor'
2316 AND event_behavior[i] = 'G' THEN
2317 -- The sib was groomed by the mom.
2318 --raise notice 'grooms sib: time %: gp_unknown %', this_1_min.interval_time, gp_unknown;
2319 mom_sib_behaviors = mom_sib_behaviors
2320 || '{grooms another}';
2321 mom_sib_uncertains = mom_sib_uncertains
2322 || event_uncertain[i];
2323 ELSIF part1_parts[i] = 'gmi_actor'
2324 AND event_behavior[i] = 'P' THEN
2325 -- The sib and the mom played.
2326 mom_sib_behaviors = mom_sib_behaviors
2328 mom_sib_uncertains = mom_sib_uncertains
2329 || event_uncertain[i];
2331 mom_sib_events = mom_sib_events
2332 || currval('events_eid_seq'::regclass)::INT;
2333 END IF; -- 2nd participant is sib
2334 END IF; -- There is a 2nd participant
2336 IF event_behavior[i] = 'R' THEN
2337 -- Resting, needs a row on RESTS.
2338 INSERT INTO rests (eid, rest)
2339 VALUES (CURRVAL('events_eid_seq'::regclass), '9');
2340 ELSIF event_behavior[i] = 'P' THEN
2341 -- Playing, needs a row on PLAYS.'
2342 INSERT INTO plays (eid, play)
2343 VALUES (CURRVAL('events_eid_seq'::regclass), 'Y');
2347 END LOOP; -- Over participants
2351 -- Check to be sure we got all the mom behavior data.
2352 IF NOT(grooms_other_converted)
2353 AND this_1_min_mom.grooms_other IS NOT NULL THEN
2354 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Behavior %: Grooms_other %: The mother has a Grooms_other value but no behaviors to go with grooming or play'
2355 , this_follow.follow_id
2356 , this_1_min_mom.mother_id
2357 , this_follow.follow_date
2358 , this_1_min.interval_time
2359 , this_1_min_mom.behavior
2360 , this_1_min_mom.grooms_other;
2363 IF NOT(groomed_by_converted)
2364 AND this_1_min_mom.groomed_by IS NOT NULL THEN
2365 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Behavior %: Groomed_by %: The mother has a Grooms_other value but no behaviors to go with grooming'
2366 , this_follow.follow_id
2367 , this_1_min_mom.mother_id
2368 , this_follow.follow_date
2369 , this_1_min.interval_time
2370 , this_1_min_mom.behavior
2371 , this_1_min_mom.groomed_by;
2374 -- And check that all non-play behaviors related to the infant
2375 -- are also in the mom's record.
2376 behavior_cnt := ARRAY_LENGTH(infant_mom_behaviors, 1);
2377 IF behavior_cnt IS NOT NULL AND behavior_cnt != 0 THEN
2378 FOR j IN 1 .. behavior_cnt LOOP
2379 IF infant_mom_behaviors[j] != 'play'
2380 AND NOT(infant_mom_behaviors[j] = 'groomed by another'
2381 AND gp_unknown) THEN
2382 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Behavior %: The infant has a behavior (of a kind indicating %) with the mom but the mom has no record of this behavior'
2383 , this_follow.follow_id
2384 , this_1_min_mom.mother_id
2385 , this_follow.follow_date
2386 , this_1_min.interval_time
2387 , this_1_min_mom.behavior
2388 , infant_mom_behaviors[j];
2393 -- Mother vocalizations.
2394 IF this_1_min_mom.vocalizations IS NOT NULL THEN
2396 old_voc := TRIM(BOTH FROM this_1_min_mom.vocalizations);
2399 this_voc_uncertain := default_mom_uncertain;
2401 -- Convert the vocalization code.
2447 this_voc_uncertain := TRUE;
2450 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Vocalizations %: The mother vocalization (%) is unknown'
2451 , this_follow.follow_id
2452 , this_1_min_mom.mother_id
2453 , this_follow.follow_date
2454 , this_1_min.interval_time
2455 , this_1_min_mom.vocalization
2460 -- Save vocalization event.
2461 INSERT INTO events (intid
2468 currval('intervals_intid_seq'::regclass)
2472 , this_voc_uncertain
2475 INSERT INTO vocs (eid, vocalization)
2476 VALUES (currval('events_eid_seq'::regclass)
2480 INSERT INTO parts (eid, part, participant)
2481 VALUES (CURRVAL('events_eid_seq'::regclass)
2483 , this_follow.mother_id);
2485 END IF; -- There is a mom vocalization.
2488 -- follow_1_minute_sibling:
2493 FROM follow_1_minute_sibling
2494 WHERE follow_1_minute_sibling.interval_id = this_1_min.interval_id;
2496 sibling_found := FOUND;
2497 IF sibling_found THEN
2498 -- | Behavior | Grooms_other | Groomed_by |
2500 -- tableid | Interval_id | Sibling_id | Bad_observation | Behavior | Distance_from_mother | Vocalizations | Plays | Grooms_other | Groomed_by | Distance_from_infant | Sibling_interval_id
2501 -- 1 | 11/05/2010_SA_01_01 | SAM | | Vua | 3 | | | | | 3 | 11/05/2010_SA_01_01_SAM
2504 -- Sib sanity check.
2505 IF NOT(this_follow.sibling_id IS NULL
2506 AND this_1_min_sib.sibling_id IS NULL)
2507 AND this_follow.sibling_id != this_1_min_sib.sibling_id THEN
2508 RAISE EXCEPTION 'Follow Sibling_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: The sibling id in the follow does not match the sibling id in the Follow_1_minute_sibling'
2509 , this_follow.sibling_id
2510 , this_1_min_sib.sibling_id
2511 , this_follow.follow_date
2512 , this_1_min.interval_time;
2515 -- Check for already converted.
2516 IF this_1_min_sib.done THEN
2517 RAISE EXCEPTION 'Date %: Mom %: Sibling %: follow_1_minute_sibling row lready converted'
2518 , this_follow.follow_date
2519 , this_follow.mother_id
2520 , this_follow.sibling_id;
2523 -- Sibling uncertainity
2524 IF this_1_min_sib.bad_observation IS NOT NULL THEN
2525 -- Fuss with sibling uncertainty here, need be.
2526 CASE this_1_min_sib.bad_observation
2528 default_sib_uncertain := TRUE;
2532 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: Bad_observation %: The sibling Bad_observation (%) is unrecognized'
2533 , this_follow.follow_id
2534 , this_1_min_sib.sibling_id
2535 , this_follow.follow_date
2536 , this_1_min.interval_time
2537 , this_1_min_sib.bad_observation
2538 , this_1_min_sib.bad_observation;
2542 -- Sibling behavior.
2543 grooms_other_converted := FALSE;
2544 groomed_by_converted := FALSE;
2545 plays_converted := FALSE;
2547 IF this_1_min_sib.behavior IS NOT NULL THEN
2548 -- Record sibling's behavior(s)
2549 -- Note that the prepare_data() function has created
2550 -- Grooming, Groomed, and Play behaviors when
2551 -- there is a Multi behavior, so we don't have to.
2553 -- Initialize storage
2554 event_behavior := ARRAY[]::TEXT[];
2555 event_uncertain := ARRAY[]::BOOLEAN[];
2556 event_baboon := ARRAY[]::BOOLEAN[];
2557 event_comment := ARRAY[]::TEXT[];
2558 part1_parts := ARRAY[]::TEXT[];
2559 part1_participants := ARRAY[]::TEXT[];
2560 part2_parts := ARRAY[]::TEXT[];
2561 part2_participants := ARRAY[]::TEXT[];
2563 -- Split into separate codes.
2564 behaviors := regexp_split_to_array(this_1_min_sib.behavior, ';');
2567 FOREACH old_behavior IN ARRAY behaviors LOOP
2569 old_behavior := TRIM(BOTH FROM old_behavior);
2570 this_uncertain := default_sib_uncertain;
2575 new_behavior := 'N';
2578 new_behavior := 'RI';
2581 new_behavior := 'UNK';
2582 this_uncertain := TRUE;
2585 CONTINUE; -- no event
2588 new_behavior := 'ASSR';
2591 new_behavior := 'BAD';
2592 this_uncertain := TRUE;
2594 WHEN 'Build nest' THEN
2595 new_behavior := 'BN';
2598 new_behavior := 'C';
2601 new_behavior := 'DR';
2604 new_behavior := '_ca_eating';
2607 new_behavior := 'G';
2609 WHEN 'Grooming' THEN
2610 new_behavior := 'G';
2613 new_behavior := 'MULTI';
2615 WHEN 'Multigroom' THEN
2616 new_behavior := 'G';
2619 new_behavior := 'O';
2622 new_behavior := 'P';
2625 new_behavior := 'R';
2627 WHEN 'Self-groom' THEN
2628 new_behavior := 'G';
2631 new_behavior := 'T';
2634 new_behavior := 'VUA';
2637 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: Behavior %: The sibling behavior (%) is unrecognized'
2638 , this_follow.follow_id
2639 , this_1_min_sib.sibling_id
2640 , this_follow.follow_date
2641 , this_1_min.interval_time
2642 , this_1_min_sib.behavior
2647 eventcnt := eventcnt + 1;
2648 event_uncertain[eventcnt] := this_uncertain;
2649 event_baboon[eventcnt] := FALSE;
2650 event_comment[eventcnt] := NULL;
2651 part1_parts[eventcnt] := 'gmi_self';
2652 part1_participants[eventcnt] := this_follow.sibling_id;
2653 part2_parts[eventcnt] := NULL;
2655 event_behavior[eventcnt] := new_behavior;
2657 IF old_behavior = 'Grooming'
2658 AND TRIM(BOTH FROM this_1_min_sib.grooms_other)
2659 = this_follow.sibling_id THEN
2660 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: Behavior %: The sibling has a Grooming behavior but Grooms_other contains only the sibling''s id'
2661 , this_follow.follow_id
2662 , this_1_min_sib.sibling_id
2663 , this_follow.follow_date
2664 , this_1_min.interval_time
2665 , this_1_min_sib.behavior;
2668 past_first := FALSE;
2670 IF old_behavior = 'Grooming'
2671 OR old_behavior = 'Multigroom'
2672 OR old_behavior = 'Play' THEN
2673 IF (new_behavior = 'G'
2674 AND this_1_min_sib.grooms_other IS NOT NULL)
2675 OR (new_behavior = 'P'
2676 AND this_1_min_sib.plays IS NOT NULL) THEN
2677 -- Record the groomed or played with individuals.
2678 IF new_behavior = 'G' THEN
2679 grooms_other_converted := TRUE;
2680 animids := regexp_split_to_array(this_1_min_sib.grooms_other
2683 plays_converted := TRUE;
2684 animids := regexp_split_to_array(this_1_min_sib.plays, ',');
2687 eventcnt := eventcnt - 1;
2688 FOREACH animid IN ARRAY animids LOOP
2689 eventcnt := eventcnt + 1;
2691 animid := convert_animid(TRIM(BOTH FROM animid)
2692 , this_follow.follow_date);
2693 -- The animal got groomed by or played with the sibling.
2694 event_behavior[eventcnt] := new_behavior;
2695 event_uncertain[eventcnt] := default_sib_uncertain;
2696 event_baboon[eventcnt] := FALSE;
2697 event_comment[eventcnt] := NULL;
2698 part1_participants[eventcnt] := this_follow.sibling_id;
2699 IF old_behavior = 'Play'
2700 AND animid != this_follow.mother_id
2701 AND animid != this_follow.infant_id THEN
2702 part1_parts[eventcnt] := 'gmi_participant';
2703 part2_parts[eventcnt] := 'gmi_participant';
2705 part1_parts[eventcnt] := 'gmi_actor';
2706 part2_parts[eventcnt] := 'gmi_actee';
2710 part2_participants[eventcnt] := 'UNK';
2711 event_uncertain[eventcnt] := TRUE;
2713 part2_participants[eventcnt] := 'UNK';
2714 WHEN this_follow.sibling_id THEN
2715 -- Self grooming or self-play.
2716 part1_parts[eventcnt] := 'gmi_self';
2717 part2_parts[eventcnt] := NULL;
2718 WHEN '_baboon_id' THEN
2719 event_baboon[eventcnt] := TRUE;
2720 part2_parts[eventcnt] := NULL;
2722 part2_participants[eventcnt] := animid;
2725 END LOOP; -- Over grooms_other/plays animids
2727 -- The grooms_other or plays column is null.
2728 IF new_behavior = 'G' THEN
2729 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: Behavior %: The Grooms_other column cannot be empty for multi or grooming or multigroom'
2730 , this_follow.follow_id
2731 , this_1_min_sib.sibling_id
2732 , this_follow.follow_date
2733 , this_1_min.interval_time
2734 , this_1_min_sib.behavior;
2735 ELSE -- new_behavior = 'P'
2736 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: Behavior %: The Plays column cannot be empty for multi or play'
2737 , this_follow.follow_id
2738 , this_1_min_sib.sibling_id
2739 , this_follow.follow_date
2740 , this_1_min.interval_time
2741 , this_1_min_sib.behavior;
2746 IF old_behavior = 'Groomed'
2747 OR old_behavior = 'Multigroom' THEN
2748 IF this_1_min_sib.groomed_by IS NOT NULL THEN
2749 -- Record the individuals doing the grooming.
2750 groomed_by_converted := TRUE;
2751 animids := regexp_split_to_array(this_1_min_sib.groomed_by
2753 IF NOT(past_first) THEN
2754 -- Have not yet filled the first participant.
2755 eventcnt := eventcnt - 1;
2757 FOREACH animid IN ARRAY animids LOOP
2758 eventcnt := eventcnt + 1;
2759 animid := convert_animid(TRIM(BOTH FROM animid)
2760 , this_follow.follow_date);
2761 -- The animal groomed the sibling.
2762 event_behavior[eventcnt] := new_behavior;
2763 event_uncertain[eventcnt] := default_sib_uncertain;
2764 event_baboon[eventcnt] := FALSE;
2765 part1_parts[eventcnt] := 'gmi_actee';
2766 part1_participants[eventcnt] := this_follow.sibling_id;
2767 part2_parts[eventcnt] := 'gmi_actor';
2770 part2_participants[eventcnt] := 'UNK';
2771 event_uncertain[eventcnt] := TRUE;
2773 part2_participants[eventcnt] := 'UNK';
2774 WHEN this_follow.sibling_id THEN
2775 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: Behavior %: Groomed_by %: The focal (the sibling) appears in the Groomed_by column'
2776 , this_follow.follow_id
2777 , this_1_min_sib.sibling_id
2778 , this_follow.follow_date
2779 , this_1_min.interval_time
2780 , this_1_min_sib.behavior
2781 , this_1_min_sib.groomed_by;
2782 WHEN '_baboon_id' THEN
2783 event_baboon[eventcnt] := TRUE;
2784 part2_parts[eventcnt] := NULL;
2786 part2_participants[eventcnt] := animid;
2789 END LOOP; -- Over groomed_by
2790 ELSE -- Groomed_by is NULL
2791 IF old_behavior != 'Multi' THEN
2792 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: Behavior %: The Groomed_by column cannot be empty for groomed or multigroom'
2793 , this_follow.follow_id
2794 , this_1_min_sib.sibling_id
2795 , this_follow.follow_date
2796 , this_1_min.interval_time
2797 , this_1_min_sib.behavior;
2802 IF old_behavior = 'Nurse' THEN
2803 -- Sibling is nursing.
2804 part1_parts[eventcnt] = 'gmi_actor';
2805 part2_parts[eventcnt] = 'gmi_actee';
2806 part2_participants[eventcnt] = this_follow.mother_id;
2807 ELSIF old_behavior = 'Ride' THEN
2808 -- Sibling is riding.
2809 part1_parts[eventcnt] = 'gmi_actor';
2810 part2_parts[eventcnt] = 'gmi_actee';
2811 part2_participants[eventcnt] = 'UNK';
2814 END LOOP; -- Over sibling behaviors
2816 alen := array_length(part1_parts, 1);
2817 IF alen IS NOT NULL THEN
2818 -- Go thorough all the participants and make events.
2819 FOR i IN 1 .. alen LOOP
2821 IF event_behavior[i] = 'G'
2822 AND ((part1_participants[i] = this_follow.sibling_id
2823 AND part2_participants[i] = this_follow.infant_id
2824 AND part1_parts[i] = 'gmi_actor'
2825 AND part2_parts[i] = 'gmi_actee')
2826 OR (part2_participants[i] = this_follow.sibling_id
2827 AND part1_participants[i] = this_follow.infant_id
2828 AND part2_parts[i] = 'gmi_actor'
2829 AND part1_parts[i] = 'gmi_actee')) THEN
2830 -- The sib groomed the infant.
2833 behavior_cnt := ARRAY_LENGTH(infant_sib_behaviors, 1);
2834 IF behavior_cnt IS NOT NULL AND behavior_cnt != 0 THEN
2835 FOR j IN 1 .. behavior_cnt LOOP
2836 IF infant_sib_behaviors[j] = 'groomed by another' THEN
2837 -- We've already recorded this behavior.
2838 IF infant_sib_uncertains[j] != event_uncertain[i]
2839 AND NOT(infant_sib_uncertains[j]) THEN
2840 -- Must make the recorded behavior uncertain.
2842 SET uncertain = TRUE
2843 WHERE events.eid = infant_sib_events[j];
2846 -- Note that we've done with it by removing the array
2848 infant_sib_behaviors := infant_sib_behaviors[1:j - 1]
2849 || infant_sib_behaviors[j+1:behavior_cnt];
2850 infant_sib_uncertains := infant_sib_uncertains[1:j - 1]
2851 || infant_sib_uncertains[j+1:behavior_cnt];
2852 infant_sib_events := infant_sib_events[1:j - 1]
2853 || infant_sib_events[j+1:behavior_cnt];
2860 IF NOT(i_found) THEN
2861 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: Behavior %: Grooms_other %: The follow_1_minute_sibling says the sib groomed the infant but the follow_1_minute_infant has no record of this'
2862 , this_follow.follow_id
2863 , this_1_min_sib.sibling_id
2864 , this_follow.follow_date
2865 , this_1_min.interval_time
2866 , this_1_min_sib.behavior
2867 , this_1_min_sib.grooms_other;
2869 ELSIF event_behavior[i] = 'G'
2870 AND ((part1_participants[i] = this_follow.sibling_id
2871 AND part2_participants[i] = this_follow.infant_id
2872 AND part1_parts[i] = 'gmi_actee'
2873 AND part2_parts[i] = 'gmi_actor')
2874 OR (part2_participants[i] = this_follow.sibling_id
2875 AND part1_participants[i] = this_follow.infant_id
2876 AND part2_parts[i] = 'gmi_actee'
2877 AND part1_parts[i] = 'gmi_actor')) THEN
2878 -- The infant groomed the sib.
2881 behavior_cnt := ARRAY_LENGTH(infant_sib_behaviors, 1);
2882 IF behavior_cnt IS NOT NULL AND behavior_cnt != 0 THEN
2883 FOR j IN 1 .. behavior_cnt LOOP
2884 IF infant_sib_behaviors[j] = 'grooms another' THEN
2885 -- We've already recorded this behavior.
2886 IF infant_sib_uncertains[j] != event_uncertain[i]
2887 AND NOT(infant_sib_uncertains[j]) THEN
2888 -- Must make the recorded behavior uncertain.
2890 SET uncertain = TRUE
2891 WHERE events.eid = infant_sib_events[j];
2894 -- Note that we've done with it by removing the array
2896 infant_sib_behaviors := infant_sib_behaviors[1:j - 1]
2897 || infant_sib_behaviors[j+1:behavior_cnt];
2898 infant_sib_uncertains := infant_sib_uncertains[1:j - 1]
2899 || infant_sib_uncertains[j+1:behavior_cnt];
2900 infant_sib_events := infant_sib_events[1:j - 1]
2901 || infant_sib_events[j+1:behavior_cnt];
2908 IF NOT(i_found) THEN
2909 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: Behavior %: Groomed_by %: The follow_1_minute_sibling says the infant groomed the sib but the follow_1_minute_infant has no record of this'
2910 , this_follow.follow_id
2911 , this_1_min_sib.sibling_id
2912 , this_follow.follow_date
2913 , this_1_min.interval_time
2914 , this_1_min_sib.behavior
2915 , this_1_min_sib.groomed_by;
2918 ELSIF event_behavior[i] = 'P'
2919 AND ((part1_participants[i] = this_follow.sibling_id
2920 AND part2_participants[i] = this_follow.infant_id
2921 AND part1_parts[i] = 'gmi_actor'
2922 AND part2_parts[i] = 'gmi_actee')
2923 OR (part2_participants[i] = this_follow.sibling_id
2924 AND part1_participants[i] = this_follow.infant_id
2925 AND part2_parts[i] = 'gmi_actor'
2926 AND part1_parts[i] = 'gmi_actee')) THEN
2927 -- The sibling played with the infant.
2930 behavior_cnt := ARRAY_LENGTH(infant_sib_behaviors, 1);
2931 IF behavior_cnt IS NOT NULL AND behavior_cnt != 0 THEN
2932 FOR j IN 1 .. behavior_cnt LOOP
2933 IF infant_sib_behaviors[j] = 'play' THEN
2934 -- We've already recorded the infant playing with
2936 -- Make the play mutual.
2938 SET part = 'gmi_participant'
2939 WHERE parts.eid = infant_sib_events[j];
2941 IF infant_sib_uncertains[j] != event_uncertain[i]
2942 AND NOT(infant_sib_uncertains[j]) THEN
2943 -- Must make the recorded behavior uncertain.
2945 SET uncertain = TRUE
2946 WHERE events.eid = infant_sib_events[j];
2954 ELSIF event_behavior[i] = 'G'
2955 AND ((part1_participants[i] = this_follow.mother_id
2956 AND part2_participants[i] = this_follow.sibling_id
2957 AND part1_parts[i] = 'gmi_actor'
2958 AND part2_parts[i] = 'gmi_actee')
2959 OR (part2_participants[i] = this_follow.mother_id
2960 AND part1_participants[i] = this_follow.sibling_id
2961 AND part2_parts[i] = 'gmi_actor'
2962 AND part1_parts[i] = 'gmi_actee')) THEN
2963 -- The mom grooms the sib.
2966 behavior_cnt := ARRAY_LENGTH(mom_sib_behaviors, 1);
2967 IF behavior_cnt IS NOT NULL AND behavior_cnt != 0 THEN
2968 FOR j IN 1 .. behavior_cnt LOOP
2969 IF mom_sib_behaviors[j] = 'grooms another' THEN
2970 -- We've already recorded this behavior.
2971 IF mom_sib_uncertains[j] != event_uncertain[i]
2972 AND NOT(mom_sib_uncertains[j]) THEN
2973 -- Must make the recorded behavior uncertain.
2975 SET uncertain = TRUE
2976 WHERE events.eid = mom_sib_events[j];
2979 -- Note that we've done with it by removing the array
2981 mom_sib_behaviors := mom_sib_behaviors[1:j - 1]
2982 || mom_sib_behaviors[j+1:behavior_cnt];
2983 mom_sib_uncertains := mom_sib_uncertains[1:j - 1]
2984 || mom_sib_uncertains[j+1:behavior_cnt];
2985 mom_sib_events := mom_sib_events[1:j - 1]
2986 || mom_sib_events[j+1:behavior_cnt];
2993 IF NOT(i_found) AND NOT(gp_unknown) THEN
2994 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Behavior %: Grooms_other %: The follow_1_minute_sibling says the mother groomed the sib but the follow_1_minute_mother has no record of this'
2995 , this_follow.follow_id
2996 , this_1_min_mom.mother_id
2997 , this_follow.follow_date
2998 , this_1_min.interval_time
2999 , this_1_min_mom.behavior
3000 , this_1_min_mom.grooms_other;
3002 ELSIF event_behavior[i] = 'G'
3003 AND ((part1_participants[i] = this_follow.mother_id
3004 AND part2_participants[i] = this_follow.sibling_id
3005 AND part1_parts[i] = 'gmi_actee'
3006 AND part2_parts[i] = 'gmi_actor')
3007 OR (part2_participants[i] = this_follow.mother_id
3008 AND part1_participants[i] = this_follow.sibling_id
3009 AND part2_parts[i] = 'gmi_actee'
3010 AND part1_parts[i] = 'gmi_actor')) THEN
3011 -- The mom was groomed by sib.
3014 behavior_cnt := ARRAY_LENGTH(mom_sib_behaviors, 1);
3015 IF behavior_cnt IS NOT NULL AND behavior_cnt != 0 THEN
3016 FOR j IN 1 .. behavior_cnt LOOP
3017 IF mom_sib_behaviors[j] = 'groomed by another' THEN
3018 -- We've already recorded this behavior.
3019 IF mom_sib_uncertains[j] != event_uncertain[i]
3020 AND NOT(mom_sib_uncertains[j]) THEN
3021 -- Must make the recorded behavior uncertain.
3023 SET uncertain = TRUE
3024 WHERE events.eid = mom_sib_events[j];
3027 -- Note that we've done with it by removing the array
3029 mom_sib_behaviors := mom_sib_behaviors[1:j - 1]
3030 || mom_sib_behaviors[j+1:behavior_cnt];
3031 mom_sib_uncertains := mom_sib_uncertains[1:j - 1]
3032 || mom_sib_uncertains[j+1:behavior_cnt];
3033 mom_sib_events := mom_sib_events[1:j - 1]
3034 || mom_sib_events[j+1:behavior_cnt];
3041 IF NOT(i_found) THEN
3042 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Behavior %: Groomed_by %: The follow_1_minute_sibling says the sib groomed the mother but the follow_1_minute_mother has no record of this'
3043 , this_follow.follow_id
3044 , this_1_min_mom.mother_id
3045 , this_follow.follow_date
3046 , this_1_min.interval_time
3047 , this_1_min_mom.behavior
3048 , this_1_min_mom.groomed_by;
3051 ELSIF event_behavior[i] = 'P'
3052 AND ((part1_participants[i] = this_follow.mother_id
3053 AND part2_participants[i] = this_follow.sibling_id
3054 AND part1_parts[i] = 'gmi_actee'
3055 AND part2_parts[i] = 'gmi_actor')
3056 OR (part2_participants[i] = this_follow.mother_id
3057 AND part1_participants[i] = this_follow.sibling_id
3058 AND part2_parts[i] = 'gmi_actee'
3059 AND part1_parts[i] = 'gmi_actor')) THEN
3060 -- The sibling played with the mom.
3062 behavior_cnt := ARRAY_LENGTH(mom_sib_behaviors, 1);
3063 IF behavior_cnt IS NOT NULL AND behavior_cnt != 0 THEN
3064 FOR j IN 1 .. behavior_cnt LOOP
3065 IF mom_sib_behaviors[j] = 'play' THEN
3066 -- We've already recorded the mom playing with
3067 -- the sibling. Make the play mutual.
3069 SET part = 'gmi_participant'
3070 WHERE parts.eid = mom_sib_events[j];
3072 IF mom_sib_uncertains[j] != event_uncertain[i]
3073 AND NOT(mom_sib_uncertains[j]) THEN
3074 -- Must make the recorded behavior uncertain.
3076 SET uncertain = TRUE
3077 WHERE events.eid = mom_sib_events[j];
3086 -- Insert a row into events.
3087 INSERT INTO events (intid
3094 currval('intervals_intid_seq'::regclass)
3096 , _gen_pos(event_behavior[i])
3098 , event_uncertain[i]
3101 -- Now save the participant(s).
3102 INSERT INTO parts (eid, part, participant)
3103 VALUES(currval('events_eid_seq'::regclass)
3104 , part1_parts[i]::parts_part
3105 , part1_participants[i]);
3107 IF part2_parts[i] IS NOT NULL THEN
3108 INSERT INTO parts (eid, part, participant)
3109 VALUES(currval('events_eid_seq'::regclass)
3110 , part2_parts[i]::parts_part
3111 , part2_participants[i]);
3114 IF event_behavior[i] = 'R' THEN
3115 -- Resting, needs a row on RESTS.
3116 INSERT INTO rests (eid, rest)
3117 VALUES (CURRVAL('events_eid_seq'::regclass), '9');
3118 ELSIF event_behavior[i] = 'P' THEN
3119 -- Playing, needs a row on PLAYS.
3120 INSERT INTO plays (eid, play)
3121 VALUES (CURRVAL('events_eid_seq'::regclass), 'Y');
3122 ELSIF event_behavior[i] = 'RI' THEN
3123 -- Riding, needs a row on RIDES.
3124 INSERT INTO rides (eid, ride)
3125 VALUES (CURRVAL('events_eid_seq'::regclass), 'U');
3127 END IF; -- Check for duplicates regards focals
3129 END LOOP; -- Over participants
3133 -- Check to be sure we got all the sibling behavior data.
3134 IF NOT(grooms_other_converted)
3135 AND this_1_min_sib.grooms_other IS NOT NULL THEN
3136 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: Behavior %: Grooms_other %: The sibling has a Grooms_other value but no behaviors to go with grooming'
3137 , this_follow.follow_id
3138 , this_1_min_sib.sibling_id
3139 , this_follow.follow_date
3140 , this_1_min.interval_time
3141 , this_1_min_sib.behavior
3142 , this_1_min_sib.grooms_other;
3145 IF NOT(groomed_by_converted)
3146 AND this_1_min_sib.groomed_by IS NOT NULL THEN
3147 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: Behavior %: Groomed_by %: The sibling has a Grooms_other value but no behaviors to go with grooming'
3148 , this_follow.follow_id
3149 , this_1_min_sib.sibling_id
3150 , this_follow.follow_date
3151 , this_1_min.interval_time
3152 , this_1_min_sib.behavior
3153 , this_1_min_sib.groomed_by;
3156 IF NOT(plays_converted)
3157 AND this_1_min_sib.plays IS NOT NULL THEN
3158 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: Behavior %: Plays %: The sibling has a Plays value but no behaviors to go with play'
3159 , this_follow.follow_id
3160 , this_1_min_sib.sibling_id
3161 , this_follow.follow_date
3162 , this_1_min.interval_time
3163 , this_1_min_sib.behavior
3164 , this_1_min_sib.plays;
3167 -- Distance_from_mother
3169 this_distance := TRIM(BOTH FROM this_1_min_sib.distance_from_mother);
3170 IF this_distance IS NOT NULL
3171 AND this_distance != '-' THEN
3173 IF this_1_min_sib.distance_from_mother = '?' THEN
3174 this_distance := '99';
3175 this_uncertain := TRUE;
3177 this_distance := this_1_min_sib.distance_from_mother;
3178 this_uncertain := default_sib_uncertain;
3181 -- Create the distances row.
3182 INSERT INTO distances (intid, uncertain, distance)
3183 VALUES (currval('intervals_intid_seq'::regclass)
3187 -- Create the distanceparts for the mom.
3188 INSERT INTO distanceparts (distid, role, participant)
3189 VALUES (currval('distances_distid_seq'::regclass)
3191 , this_follow.mother_id);
3193 -- Create the distanceparts for the sibling.
3194 INSERT INTO distanceparts (distid, role, participant)
3195 VALUES (currval('distances_distid_seq'::regclass)
3197 , this_follow.sibling_id);
3199 END IF; -- There is a sibling-mom distance.
3201 -- Distance_from_infant
3204 this_distance := TRIM(BOTH FROM this_1_min_sib.distance_from_infant);
3205 IF this_distance IS NOT NULL
3206 AND this_distance != '-' THEN
3208 IF this_1_min_sib.distance_from_infant = '?' THEN
3209 this_distance := '99';
3210 this_uncertain := TRUE;
3212 this_distance := this_1_min_sib.distance_from_infant;
3213 this_uncertain := default_sib_uncertain;
3216 -- Create the distances row.
3217 INSERT INTO distances (intid, uncertain, distance)
3218 VALUES (currval('intervals_intid_seq'::regclass)
3222 -- Create the distanceparts for the sibling.
3223 INSERT INTO distanceparts (distid, role, participant)
3224 VALUES (currval('distances_distid_seq'::regclass)
3226 , this_follow.sibling_id);
3228 -- Create the distanceparts for the infant.
3229 INSERT INTO distanceparts (distid, role, participant)
3230 VALUES (currval('distances_distid_seq'::regclass)
3232 , this_follow.infant_id);
3234 END IF; -- There is a sibling-infant distance.
3236 -- Sibling vocalizations.
3238 IF this_1_min_sib.vocalizations IS NOT NULL THEN
3240 old_voc := TRIM(BOTH FROM this_1_min_sib.vocalizations);
3243 this_voc_uncertain := default_sib_uncertain;
3245 -- Convert the vocalization code.
3291 this_voc_uncertain := TRUE;
3294 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_sibling Sibling_id %: Date %: Time %: Vocalizations %: The sibling vocalization (%) is unknown'
3295 , this_follow.follow_id
3296 , this_1_min_sib.sibling_id
3297 , this_follow.follow_date
3298 , this_1_min.interval_time
3299 , this_1_min_sib.vocalization
3304 -- Save vocalization event.
3305 INSERT INTO events (intid
3312 currval('intervals_intid_seq'::regclass)
3316 , this_voc_uncertain
3319 INSERT INTO vocs (eid, vocalization)
3320 VALUES (currval('events_eid_seq'::regclass)
3324 INSERT INTO parts (eid, part, participant)
3325 VALUES (CURRVAL('events_eid_seq'::regclass)
3327 , this_follow.sibling_id);
3329 END IF; -- There is a sib vocalization.
3330 END IF; -- There is a sibling.
3332 -- And check that all non-play behaviors related to the infant
3333 -- involving the sib are also in the sib's record.
3334 behavior_cnt := ARRAY_LENGTH(infant_sib_behaviors, 1);
3335 IF behavior_cnt IS NOT NULL AND behavior_cnt != 0 THEN
3336 FOR j IN 1 .. behavior_cnt LOOP
3337 IF infant_sib_behaviors[j] != 'play' THEN
3338 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_infant Infant_id %: Date %: Time %: The infant has a behavior (of a kind indicating %) with the sib but the sib has no record of this behavior'
3339 , this_follow.follow_id
3340 , this_1_min_infant.infant_id
3341 , this_follow.follow_date
3342 , this_1_min.interval_time
3343 , infant_sib_behaviors[j];
3348 -- And check that all non-play behaviors related to the mom
3349 -- involving the sib are also in the sib's record.
3350 behavior_cnt := ARRAY_LENGTH(mom_sib_behaviors, 1);
3351 IF behavior_cnt IS NOT NULL AND behavior_cnt != 0 THEN
3352 FOR j IN 1 .. behavior_cnt LOOP
3353 IF mom_sib_behaviors[j] != 'play'
3354 AND NOT(mom_sib_behaviors[j] = 'grooms another'
3355 AND gp_unknown) THEN
3356 RAISE EXCEPTION 'Follow Follow_id %: Follow_1_minute_mother Mother_id %: Date %: Time %: Behavior %: The mom has a behavior (of a kind indicating %) with the sib but the sib has no record of this behavior'
3357 , this_follow.follow_id
3358 , this_1_min_mom.mother_id
3359 , this_follow.follow_date
3360 , this_1_min.interval_time
3361 , this_1_min_mom.behavior
3362 , mom_sib_behaviors[j];
3367 -- Remember we converted this minute.
3368 UPDATE follow_1_minute
3370 WHERE tableid = this_1_min.tableid;
3372 -- Remember we converted this mother/minute
3373 UPDATE follow_1_minute_mother
3375 WHERE tableid = this_1_min_mom.tableid;
3377 -- Remember we converted this infant/minute.
3378 UPDATE follow_1_minute_infant
3380 WHERE tableid = this_1_min_infant.tableid;
3382 IF sibling_found THEN
3383 -- Remember we converted this sibling/minute.
3384 UPDATE follow_1_minute_sibling
3386 WHERE tableid = this_1_min_sib.tableid;
3389 END LOOP; -- Over 1 minutes
3391 -- Remember we converted this follow.
3394 WHERE tableid = this_follow.tableid;
3396 -- Keep track of how many follows we converted.
3397 converted := converted + 1;
3406 grant_func_priv(`prepared_data.convert_access(DATE, DATE)')