/ conversion / access / functions /
/conversion/access/functions/convert_access.m4
1 dnl Copyright (C) 2013 The Meme Factory, Inc. http://www.meme.com/
2 dnl
3 dnl This file is part of Gombe-MI.
4 dnl
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.
9 dnl
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.
14 dnl
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/>.
17 dnl
18 dnl Function to convert from access table structure to new table structure.
19 dnl
20 dnl Karl O. Pinc <kop@meme.com>
21
22 dnl m4 includes
23 include(`copyright.m4')
24 include(`constants.m4')
25 include(`types.m4')
26 include(`macros.m4')dnl
27
28 dnl Reset our search path so we pick up the conversion schema.
29 define(`gmi_function_search_path', `prepared_data, gombemi, pg_temp')
30
31 dnl The animid used for baboons.
32 define(`_baboon_id', `NYA')
33
34 dnl The behavior code for eating.
35 define(`_ca_eating', `UE')
36
37 dnl Ugly hack to generate events.position
38 dnl
39 dnl syntax _gen_pos(behavior)
40 dnl
41 dnl behavior The behavior code.
42 dnl
43 changequote({,})
44 define({_gen_pos},{(SELECT CASE WHEN position THEN '9' ELSE NULL END FROM behaviors WHERE behavior = $1)})dnl
45 changequote(`,')dnl
46
47
48 CREATE OR REPLACE FUNCTION prepared_data.convert_access(fromdate DATE
49 , todate DATE)
50 RETURNS INT
51 LANGUAGE plpgsql
52 SECURITY DEFINER
53 gmi_function_set_search_path
54 AS $$
55 -- Copy access data structures into new gombemi data structures.
56 --
57 -- GPL_notice(` --', `2013', `The Meme Factory, Inc. http://www.meme.com/')
58 --
59 -- Syntax: select convert_access(fromdate, todate);
60 --
61 -- Arguments:
62 -- fromdate Date to start copy (inclusive)
63 -- todate Date to stop copy (inclusive)
64 --
65 -- Remarks:
66 -- One-off program. Once it works we won't run it again.
67 --
68 -- Copies a day a a time.
69 --
70 -- Stops as soon as a transaction fails.
71
72 DECLARE
73 converted INT := 0;
74
75 this_enterer people.person%TYPE;
76 this_collector people.person%TYPE;
77 this_followtype TEXT;
78 tmp_followtype TEXT;
79 this_entrydate DATE;
80 this_sheetid sheets.sheetid%TYPE;
81 this_ffid fieldfollows.ffid%TYPE;
82 this_page INT;
83 this_location TEXT;
84 last_page INT;
85
86 interval_comment TEXT;
87 this_devbeh TEXT;
88 this_uncertain BOOLEAN;
89 this_baboon BOOLEAN;
90
91 this_follow RECORD;
92 this_1_min RECORD;
93 this_1_min_infant RECORD;
94 this_1_min_mom RECORD;
95 this_1_min_sib RECORD;
96
97 default_mom_uncertain BOOLEAN;
98 default_infant_uncertain BOOLEAN;
99 default_sib_uncertain BOOLEAN;
100
101 behaviors TEXT[];
102 old_behavior TEXT;
103 new_behavior TEXT;
104 event_behavior TEXT[];
105 event_uncertain BOOLEAN[];
106 event_baboon BOOLEAN[];
107 event_comment TEXT[];
108 animids TEXT[];
109 animid_cnt INT;
110 animid TEXT;
111 part1_parts TEXT[];
112 part1_participants TEXT[];
113 part2_parts TEXT[];
114 part2_participants TEXT[];
115
116 grooming_found BOOLEAN;
117 play_found BOOLEAN;
118 multi_found BOOLEAN;
119 grooming_and_play BOOLEAN;
120 -- Did mom's grooming or play partner get changed to UNK?
121 gp_unknown BOOLEAN;
122 grooms_other_converted BOOLEAN;
123 groomed_by_converted BOOLEAN;
124 plays_converted BOOLEAN;
125
126 eventcnt INT;
127 alen INT;
128
129 old_voc TEXT;
130 new_voc TEXT;
131 this_voc_uncertain BOOLEAN;
132
133 this_distance TEXT;
134 this_play TEXT;
135 this_participant TEXT;
136 this_part TEXT;
137 this_part1 parts.part%TYPE;
138 this_part2 parts.part%TYPE;
139 this_error BOOLEAN;
140
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[];
152
153 sibling_found BOOLEAN;
154 i_found BOOLEAN;
155 behavior_cnt INT;
156 past_first BOOLEAN;
157 BEGIN
158
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
164 FROM biography_data
165 WHERE NOT EXISTS (
166 SELECT 1
167 FROM chimpids AS this_id
168 WHERE this_id.animid = biography_data.animid
169 LIMIT 1);
170 END IF;
171
172 FOR this_follow
173 IN SELECT *
174 FROM follow
175 WHERE fromdate <= follow.follow_date
176 AND follow.follow_date <= todate
177 ORDER BY follow.follow_date, follow.follow_id
178 LOOP
179
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;
185 END IF;
186
187 -- Create the follow.
188 INSERT INTO follows (date)
189 VALUES(this_follow.follow_date);
190
191 -- Create the followparts rows.
192
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
197 , 'M');
198
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
203 , 'I');
204
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
210 , 'S');
211 END IF;
212
213 -- The fieldfollow, access data has 1 per follow.
214 -- Administrative info: collector, enterer, etc.
215
216 -- Find the enterer
217 SELECT people.person
218 INTO this_enterer
219 FROM people
220 WHERE people.descr = this_follow.entered_by;
221
222 IF NOT FOUND THEN
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
229 || ')';
230 END IF;
231
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
235 FROM crfp
236 WHERE crfp.follow_date = this_follow.follow_date
237 AND crfp.focal_mom = this_follow.mother_id;
238
239 IF NOT FOUND THEN
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
246 || ')';
247 END IF;
248
249 -- Find the collector
250 SELECT people.person
251 INTO this_collector
252 FROM people
253 WHERE people.descr = this_follow.conducted_by;
254
255 IF NOT FOUND THEN
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
262 || ')';
263 END IF;
264
265 -- Create the fieldfollow.
266 INSERT INTO fieldfollows (collector
267 , entrydate
268 , enterer
269 , uploaddate)
270 VALUES(this_collector
271 , this_entrydate
272 , this_enterer
273 , current_date);
274
275 this_ffid := CURRVAL('fieldfollows_ffid_seq'::REGCLASS);
276
277 -- The columns we've not converted:
278 -- follow:
279 -- Infant2_id | Sibling2_id | Sheet_id | Left_group_id | Right_group_id | Follow_status | Follow_id | Time_mode | AMPM
280
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
283
284 --
285 -- Go through all the 1 minute intervals for the follow.
286 --
287 last_page := NULL;
288
289 FOR this_1_min
290 IN SELECT *
291 FROM follow_1_minute
292 WHERE follow_1_minute.follow_id = this_follow.follow_id
293 ORDER BY follow_1_minute.interval_time
294 LOOP
295
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;
302 END IF;
303
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;
310 END IF;
311
312 -- Get the page.
313 SELECT follow_page.page_number, trim(both from follow_page.location)
314 INTO this_page, this_location
315 FROM follow_page
316 WHERE follow_page.page_id = this_1_min.page_id;
317
318 IF NOT FOUND THEN
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;
324 END IF;
325
326 IF last_page IS NULL
327 OR this_page != last_page THEN
328 last_page := this_page;
329
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.
334
335 -- Remember the page is converted.
336 UPDATE follow_page
337 SET done = TRUE
338 WHERE follow_page.page_id = this_1_min.page_id;
339
340 -- Find the sheet on which the interval belongs.
341 SELECT sheets.sheetid
342 INTO this_sheetid
343 FROM follows
344 JOIN followparts
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
352 LIMIT 1;
353
354 IF NOT FOUND THEN
355 -- Create a sheet, into which we will put this interval.
356
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.
361 BEGIN
362 SELECT TRIM(BOTH FROM page_followtype.temptype)
363 INTO STRICT tmp_followtype
364 FROM page_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;
368
369 EXCEPTION
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
375 || ')';
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
381 || ')';
382 END;
383
384 -- Remember having looked at the followtype per page.
385 UPDATE page_followtype
386 SET done = TRUE
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;
392 END IF;
393
394 -- Create the sheets row.
395
396 IF this_location = '' THEN
397 this_location = NULL;
398 END IF;
399
400 INSERT INTO sheets (ffid, followtype, page, location)
401 VALUES(this_ffid
402 , tmp_followtype
403 , this_page
404 , this_location);
405
406 this_sheetid = currval('sheets_sheetid_seq'::regclass);
407 END IF;
408 END IF;
409
410 -- follow_1_minute:
411
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
414
415 -- Clean up the comment
416 interval_comment := TRIM(BOTH FROM this_1_min.comment);
417 IF interval_comment = '' THEN
418 interval_comment := NULL;
419 END IF;
420
421 -- Create the interval.
422 INSERT INTO intervals (followid
423 , sheetid
424 , time
425 , comment)
426 VALUES (currval('follows_followid_seq'::regclass)
427 , currval('sheets_sheetid_seq'::regclass)
428 , this_1_min.interval_time
429 , interval_comment);
430
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
437 WHEN 'Y' THEN
438 default_mom_uncertain := TRUE;
439 default_infant_uncertain := TRUE;
440 default_sib_uncertain := TRUE;
441
442 WHEN 'M' THEN
443 default_mom_uncertain := TRUE;
444
445 WHEN 'I' THEN
446 default_infant_uncertain := TRUE;
447
448 WHEN 'S' THEN
449 default_sib_uncertain := TRUE;
450
451 WHEN 'M/I' THEN
452 default_mom_uncertain := TRUE;
453 default_infant_uncertain := TRUE;
454
455 WHEN 'M/S' THEN
456 default_mom_uncertain := TRUE;
457 default_sib_uncertain := TRUE;
458
459 WHEN 'I/S' THEN
460 default_infant_uncertain := TRUE;
461 default_sib_uncertain := TRUE;
462
463 ELSE
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;
469 END CASE;
470 END IF;
471
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[];
482
483 -- Get all the related 1 minute rows.
484 SELECT *
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;
488
489 -- follow_1_minute_infant:
490 -- Grooms_other | Groomed_by | Plays | Cries | Vocalizations
491
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
494
495
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;
503 END IF;
504
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;
511 END IF;
512
513 -- Nursing
514 IF this_1_min_infant.nursing IS NOT NULL THEN
515 CASE this_1_min_infant.nursing
516 WHEN 'Y' THEN
517 this_uncertain := default_infant_uncertain;
518 WHEN '?' THEN
519 this_uncertain := TRUE;
520 ELSE
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;
527 END CASE;
528
529 -- Nursing event.
530 INSERT INTO events (intid
531 , behavior
532 , position
533 , baboon
534 , uncertain
535 , comment)
536 VALUES (currval('intervals_intid_seq'::regclass)
537 , 'N'
538 , _gen_pos('N')
539 , FALSE
540 , this_uncertain
541 , NULL);
542
543 -- Nursing participants, mom.
544 INSERT INTO parts (eid, part, participant)
545 VALUES (CURRVAL('events_eid_seq'::regclass)
546 , 'gmi_actee'
547 , this_follow.mother_id);
548
549 -- Nursing participants, infant.
550 INSERT INTO parts (eid, part, participant)
551 VALUES (CURRVAL('events_eid_seq'::regclass)
552 , 'gmi_actor'
553 , this_follow.infant_id);
554 END IF;
555
556 -- Eating
557 IF this_1_min_infant.eating IS NOT NULL THEN
558 CASE this_1_min_infant.eating
559 WHEN 'Y' THEN
560 this_uncertain := default_infant_uncertain;
561 WHEN '?' THEN
562 this_uncertain := TRUE;
563 ELSE
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;
570 END CASE;
571
572 INSERT INTO events (intid
573 , behavior
574 , position
575 , baboon
576 , uncertain
577 , comment)
578 VALUES (currval('intervals_intid_seq'::regclass)
579 , '_ca_eating'
580 , _gen_pos('_ca_eating')
581 , FALSE
582 , this_uncertain
583 , NULL);
584
585 -- Participant.
586 INSERT INTO parts (eid, part, participant)
587 VALUES (CURRVAL('events_eid_seq'::regclass)
588 , 'gmi_self'
589 , this_follow.infant_id);
590 END IF;
591
592 -- Walks
593 IF this_1_min_infant.walks IS NOT NULL THEN
594 CASE this_1_min_infant.walks
595 WHEN 'Y' THEN
596 this_uncertain := default_infant_uncertain;
597 WHEN '?' THEN
598 this_uncertain := TRUE;
599 ELSE
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;
606 END CASE;
607
608 INSERT INTO events (intid
609 , behavior
610 , position
611 , baboon
612 , uncertain
613 , comment)
614 VALUES (currval('intervals_intid_seq'::regclass)
615 , 'T'
616 , _gen_pos('T')
617 , FALSE
618 , this_uncertain
619 , NULL);
620
621 -- Participant.
622 INSERT INTO parts (eid, part, participant)
623 VALUES (CURRVAL('events_eid_seq'::regclass)
624 , 'gmi_self'
625 , this_follow.infant_id);
626 END IF;
627
628 -- Playing (solitary)
629 IF this_1_min_infant.playing IS NOT NULL THEN
630 CASE this_1_min_infant.playing
631 WHEN 'Y' THEN
632 this_play := 'Y';
633 this_uncertain := default_infant_uncertain;
634 WHEN 'C' THEN
635 this_play := 'C';
636 this_uncertain := default_infant_uncertain;
637 WHEN 'D' THEN
638 this_play := 'DP';
639 this_uncertain := default_infant_uncertain;
640 WHEN 'O' THEN
641 this_play := 'OP';
642 this_uncertain := default_infant_uncertain;
643 WHEN 'D/O' THEN
644 this_play := 'DO';
645 this_uncertain := default_infant_uncertain;
646 WHEN 'D/C' THEN
647 this_play := 'DC';
648 this_uncertain := default_infant_uncertain;
649 WHEN '?' THEN
650 this_play := 'Y';
651 this_uncertain := TRUE;
652 ELSE
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;
659 END CASE;
660
661 INSERT INTO events (intid
662 , behavior
663 , position
664 , baboon
665 , uncertain
666 , comment)
667 VALUES (currval('intervals_intid_seq'::regclass)
668 , 'P'
669 , _gen_pos('P')
670 , FALSE
671 , this_uncertain
672 , NULL);
673
674 INSERT INTO plays (eid, play)
675 VALUES (CURRVAL('events_eid_seq'::regclass)
676 , this_play);
677
678 -- Participant.
679 INSERT INTO parts (eid, part, participant)
680 VALUES (CURRVAL('events_eid_seq'::regclass)
681 , 'gmi_self'
682 , this_follow.infant_id);
683 END IF;
684
685 -- Resting
686 IF this_1_min_infant.resting IS NOT NULL THEN
687 CASE this_1_min_infant.resting
688 WHEN 'Y' THEN
689 this_uncertain := default_infant_uncertain;
690 WHEN '?' THEN
691 this_uncertain := TRUE;
692 ELSE
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;
699 END CASE;
700
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
737 , behavior
738 , position
739 , baboon
740 , uncertain
741 , comment)
742 VALUES (currval('intervals_intid_seq'::regclass)
743 , 'R'
744 , _gen_pos('R')
745 , FALSE
746 , this_uncertain
747 , NULL);
748
749 -- Participant.
750 INSERT INTO parts (eid, part, participant)
751 VALUES (CURRVAL('events_eid_seq'::regclass)
752 , 'gmi_self'
753 , this_follow.infant_id);
754
755 -- Rests
756 INSERT INTO rests (eid, rest)
757 VALUES (CURRVAL('events_eid_seq'::regclass)
758 , '9');
759 END IF;
760 END IF;
761
762 -- Rides_on_stomach
763 IF this_1_min_infant.rides_on_stomach IS NOT NULL THEN
764 CASE this_1_min_infant.rides_on_stomach
765 WHEN 'Y' THEN
766 this_uncertain := default_infant_uncertain;
767 this_participant := this_follow.mother_id;
768 WHEN '?' THEN
769 this_uncertain := TRUE;
770 this_participant := 'UNK';
771 ELSE
772 -- An individual
773 this_uncertain := default_infant_uncertain;
774 this_participant := convert_animid(this_1_min_infant.rides_on_stomach, this_follow.follow_date);
775 END CASE;
776
777 INSERT INTO events (intid
778 , behavior
779 , position
780 , baboon
781 , uncertain
782 , comment)
783 VALUES (currval('intervals_intid_seq'::regclass)
784 , 'RI'
785 , _gen_pos('RI')
786 , FALSE
787 , this_uncertain
788 , NULL);
789
790 INSERT INTO rides (eid, ride)
791 VALUES (CURRVAL('events_eid_seq'::regclass)
792 , 'V');
793
794 -- Participant -- focal
795 INSERT INTO parts (eid, part, participant)
796 VALUES (CURRVAL('events_eid_seq'::regclass)
797 , 'gmi_actor'
798 , this_follow.infant_id);
799
800 -- Participant -- ridee
801 INSERT INTO parts (eid, part, participant)
802 VALUES (CURRVAL('events_eid_seq'::regclass)
803 , 'gmi_actee'
804 , this_participant);
805 END IF;
806
807 -- Rides_on_back
808 IF this_1_min_infant.rides_on_back IS NOT NULL THEN
809 CASE this_1_min_infant.rides_on_back
810 WHEN 'Y' THEN
811 this_uncertain := default_infant_uncertain;
812 this_participant := this_follow.mother_id;
813 WHEN '?' THEN
814 this_uncertain := TRUE;
815 this_participant := 'UNK';
816 ELSE
817 -- An individual
818 this_uncertain := default_infant_uncertain;
819 this_participant := convert_animid(this_1_min_infant.rides_on_back, this_follow.follow_date);
820 END CASE;
821
822 INSERT INTO events (intid
823 , behavior
824 , position
825 , baboon
826 , uncertain
827 , comment)
828 VALUES (currval('intervals_intid_seq'::regclass)
829 , 'RI'
830 , _gen_pos('RI')
831 , FALSE
832 , this_uncertain
833 , NULL);
834
835 INSERT INTO rides (eid, ride)
836 VALUES (CURRVAL('events_eid_seq'::regclass)
837 , 'D');
838
839 -- Participant -- focal
840 INSERT INTO parts (eid, part, participant)
841 VALUES (CURRVAL('events_eid_seq'::regclass)
842 , 'gmi_actor'
843 , this_follow.infant_id);
844
845 -- Participant -- ridee
846 INSERT INTO parts (eid, part, participant)
847 VALUES (CURRVAL('events_eid_seq'::regclass)
848 , 'gmi_actee'
849 , this_participant);
850 END IF;
851
852
853 -- Dangles
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.
890
891 CASE this_1_min_infant.dangles
892 WHEN 'Y' THEN
893 this_uncertain := default_infant_uncertain;
894 this_participant := this_follow.mother_id;
895 WHEN '?' THEN
896 this_uncertain := TRUE;
897 this_participant := 'UNK';
898 ELSE
899 -- An individual
900 this_uncertain := default_infant_uncertain;
901 this_participant := convert_animid(this_1_min_infant.dangles
902 , this_follow.follow_date);
903 END CASE;
904
905 INSERT INTO events (intid
906 , behavior
907 , position
908 , baboon
909 , uncertain
910 , comment)
911 VALUES (currval('intervals_intid_seq'::regclass)
912 , 'DA'
913 , _gen_pos('DA')
914 , FALSE
915 , this_uncertain
916 , NULL);
917
918 -- Participant -- focal
919 INSERT INTO parts (eid, part, participant)
920 VALUES (CURRVAL('events_eid_seq'::regclass)
921 , 'gmi_actor'
922 , this_follow.infant_id);
923
924 -- Participant -- dangled from
925 INSERT INTO parts (eid, part, participant)
926 VALUES (CURRVAL('events_eid_seq'::regclass)
927 , 'gmi_actee'
928 , this_participant);
929 ELSE
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;
946
947 END IF;
948 END IF;
949 END IF;
950
951
952 -- Contacts_other
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
956 , ',');
957 FOREACH animid IN ARRAY animids LOOP
958 animid := convert_animid(TRIM(BOTH FROM animid)
959 , this_follow.follow_date);
960 this_baboon := FALSE;
961 CASE animid
962 WHEN 'Y' THEN
963 this_participant := 'UNK';
964 WHEN '?' THEN
965 this_uncertain := TRUE;
966 this_participant := 'UNK';
967 WHEN '_baboon_id' THEN
968 this_uncertain := default_infant_uncertain;
969 this_baboon := TRUE;
970 ELSE
971 -- Another individual
972 this_uncertain := default_infant_uncertain;
973 this_participant := animid;
974 END CASE;
975
976 INSERT INTO events (intid
977 , behavior
978 , position
979 , baboon
980 , uncertain
981 , comment)
982 VALUES (currval('intervals_intid_seq'::regclass)
983 , 'TA'
984 , _gen_pos('TA')
985 , this_baboon
986 , this_uncertain
987 , NULL);
988
989 -- Participant.
990 INSERT INTO parts (eid, part, participant)
991 VALUES (CURRVAL('events_eid_seq'::regclass)
992 , 'gmi_participant'
993 , this_follow.infant_id);
994
995 IF NOT(this_baboon) THEN
996 -- Participant -- one touched
997 INSERT INTO parts (eid, part, participant)
998 VALUES (CURRVAL('events_eid_seq'::regclass)
999 , 'gmi_participant'
1000 , this_participant);
1001 END IF;
1002 END LOOP;
1003 END IF;
1004
1005
1006 -- Grooms_other
1007 IF this_1_min_infant.grooms_other IS NOT NULL THEN
1008
1009 animids := regexp_split_to_array(this_1_min_infant.grooms_other
1010 , ',');
1011
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';
1019
1020 CASE animid
1021 WHEN '?' THEN
1022 this_participant := 'UNK';
1023 this_uncertain := TRUE;
1024 WHEN 'Y' THEN
1025 this_participant := 'UNK';
1026 WHEN this_follow.infant_id THEN
1027 -- Self grooming.
1028 this_part := 'gmi_self';
1029 WHEN '_baboon_id' THEN
1030 this_baboon := TRUE;
1031 ELSE
1032 this_participant := animid;
1033 END CASE;
1034
1035 -- Insert a row into events.
1036 INSERT INTO events (intid
1037 , behavior
1038 , position
1039 , baboon
1040 , uncertain
1041 , comment)
1042 VALUES (
1043 currval('intervals_intid_seq'::regclass)
1044 , 'G'
1045 , _gen_pos('G')
1046 , this_baboon
1047 , this_uncertain
1048 , NULL);
1049
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);
1055
1056 IF this_participant IS NOT NULL THEN
1057 INSERT INTO parts (eid, part, participant)
1058 VALUES(currval('events_eid_seq'::regclass)
1059 , 'gmi_actee'
1060 , this_participant);
1061
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;
1068 infant_mom_events
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;
1075 infant_sib_events
1076 := infant_sib_events
1077 || currval('events_eid_seq'::regclass)::INT;
1078 ELSE
1079 NULL;
1080 END CASE;
1081 END IF; -- NULL participant
1082 END LOOP; -- Over grooms_other
1083 END IF;
1084
1085
1086 -- Groomed_by
1087 IF this_1_min_infant.groomed_by IS NOT NULL THEN
1088
1089 animids := regexp_split_to_array(this_1_min_infant.groomed_by
1090 , ',');
1091
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;
1097
1098 CASE animid
1099 WHEN '?' THEN
1100 this_participant := 'UNK';
1101 this_uncertain := TRUE;
1102 WHEN 'Y' THEN
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;
1113 ELSE
1114 this_participant := animid;
1115 END CASE;
1116
1117 -- Insert a row into events.
1118 INSERT INTO events (intid
1119 , behavior
1120 , position
1121 , baboon
1122 , uncertain
1123 , comment)
1124 VALUES (
1125 currval('intervals_intid_seq'::regclass)
1126 , 'G'
1127 , _gen_pos('G')
1128 , this_baboon
1129 , this_uncertain
1130 , NULL);
1131
1132 -- The infant participant.
1133 INSERT INTO parts (eid, part, participant)
1134 VALUES(currval('events_eid_seq'::regclass)
1135 , 'gmi_actee'
1136 , this_follow.infant_id);
1137
1138 IF NOT(this_baboon) THEN
1139 INSERT INTO parts (eid, part, participant)
1140 VALUES(currval('events_eid_seq'::regclass)
1141 , 'gmi_actor'
1142 , this_participant);
1143
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;
1150 infant_mom_events
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;
1157 infant_sib_events
1158 := infant_sib_events
1159 || currval('events_eid_seq'::regclass)::INT;
1160 ELSE
1161 NULL;
1162 END CASE;
1163 END IF; -- baboon
1164 END LOOP; -- Over groomed_by
1165 END IF;
1166
1167 -- Plays
1168 IF this_1_min_infant.plays IS NOT NULL THEN
1169
1170 animids := regexp_split_to_array(this_1_min_infant.plays
1171 , ',');
1172
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;
1178
1179 CASE animid
1180 WHEN '?' THEN
1181 this_participant := 'UNK';
1182 this_uncertain := TRUE;
1183 WHEN 'Y' THEN
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;
1195 ELSE
1196 this_participant := animid;
1197 END CASE;
1198
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';
1204 ELSE
1205 this_part1 := 'gmi_participant';
1206 this_part2 := 'gmi_participant';
1207 END IF;
1208
1209 -- Insert a row into events.
1210 INSERT INTO events (intid
1211 , behavior
1212 , position
1213 , baboon
1214 , uncertain
1215 , comment)
1216 VALUES (
1217 currval('intervals_intid_seq'::regclass)
1218 , 'P'
1219 , _gen_pos('P')
1220 , this_baboon
1221 , this_uncertain
1222 , NULL);
1223
1224 -- The plays table
1225 INSERT INTO plays (eid, play)
1226 VALUES(currval('events_eid_seq'::regclass)
1227 , 'Y');
1228
1229 -- The infant participant.
1230 INSERT INTO parts (eid, part, participant)
1231 VALUES(currval('events_eid_seq'::regclass)
1232 , this_part1
1233 , this_follow.infant_id);
1234
1235 IF NOT(this_baboon) THEN
1236 INSERT INTO parts (eid, part, participant)
1237 VALUES(currval('events_eid_seq'::regclass)
1238 , this_part2
1239 , this_participant);
1240 END IF;
1241
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;
1248 infant_mom_events
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;
1255 infant_sib_events
1256 := infant_sib_events
1257 || currval('events_eid_seq'::regclass)::INT;
1258 ELSE
1259 NULL;
1260 END CASE;
1261 END LOOP; -- Over plays
1262 END IF;
1263
1264
1265 -- Displays
1266 IF this_1_min_infant.displays IS NOT NULL THEN
1267 CASE this_1_min_infant.displays
1268 WHEN 'Y' THEN
1269 this_uncertain := default_infant_uncertain;
1270 WHEN '?' THEN
1271 this_uncertain := TRUE;
1272 ELSE
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;
1279 END CASE;
1280
1281 INSERT INTO events (intid
1282 , behavior
1283 , position
1284 , baboon
1285 , uncertain
1286 , comment)
1287 VALUES (currval('intervals_intid_seq'::regclass)
1288 , 'DI'
1289 , _gen_pos('DI')
1290 , FALSE
1291 , this_uncertain
1292 , NULL);
1293
1294 -- Participant.
1295 INSERT INTO parts (eid, part, participant)
1296 VALUES (CURRVAL('events_eid_seq'::regclass)
1297 , 'gmi_self'
1298 , this_follow.infant_id);
1299 END IF;
1300
1301 -- Refused
1302 IF this_1_min_infant.refused IS NOT NULL THEN
1303 CASE this_1_min_infant.refused
1304 WHEN 'Y' THEN
1305 this_uncertain := default_infant_uncertain;
1306 WHEN '?' THEN
1307 this_uncertain := TRUE;
1308 ELSE
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;
1315 END CASE;
1316
1317 INSERT INTO events (intid
1318 , behavior
1319 , position
1320 , baboon
1321 , uncertain
1322 , comment)
1323 VALUES (currval('intervals_intid_seq'::regclass)
1324 , 'RJ'
1325 , _gen_pos('RJ')
1326 , FALSE
1327 , this_uncertain
1328 , NULL);
1329
1330 -- Participant.
1331 INSERT INTO parts (eid, part, participant)
1332 VALUES (CURRVAL('events_eid_seq'::regclass)
1333 , 'gmi_actee'
1334 , this_follow.infant_id);
1335
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)
1339 , 'gmi_actor'
1340 , 'UNK');
1341 END IF;
1342
1343
1344 -- Begs
1345 IF this_1_min_infant.begs IS NOT NULL THEN
1346 this_baboon := FALSE;
1347 CASE this_1_min_infant.begs
1348 WHEN 'Y' THEN
1349 this_uncertain := default_infant_uncertain;
1350 this_participant := 'UNK';
1351 WHEN '?' THEN
1352 this_uncertain := TRUE;
1353 this_participant := 'UNK';
1354 WHEN '_baboon_id' THEN
1355 this_baboon := TRUE;
1356 ELSE
1357 -- Another individual
1358 this_uncertain := default_infant_uncertain;
1359 this_participant := convert_animid(this_1_min_infant.begs
1360 , this_follow.follow_date);
1361 END CASE;
1362
1363 INSERT INTO events (intid
1364 , behavior
1365 , position
1366 , baboon
1367 , uncertain
1368 , comment)
1369 VALUES (currval('intervals_intid_seq'::regclass)
1370 , 'BE'
1371 , _gen_pos('BE')
1372 , this_baboon
1373 , this_uncertain
1374 , NULL);
1375
1376 -- Participant.
1377 INSERT INTO parts (eid, part, participant)
1378 VALUES (CURRVAL('events_eid_seq'::regclass)
1379 , 'gmi_actor'
1380 , this_follow.infant_id);
1381
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)
1386 , 'gmi_actee'
1387 , this_participant);
1388 END IF;
1389 END IF;
1390
1391
1392 -- Aggression
1393 IF this_1_min_infant.aggression IS NOT NULL THEN
1394 this_baboon := FALSE;
1395 CASE this_1_min_infant.aggression
1396 WHEN 'Y' THEN
1397 this_uncertain := default_infant_uncertain;
1398 this_participant := 'UNK';
1399 WHEN '?' THEN
1400 this_uncertain := TRUE;
1401 this_participant := 'UNK';
1402 WHEN '_baboon_id' THEN
1403 this_baboon := TRUE;
1404 ELSE
1405 -- Another individual
1406 this_uncertain := default_infant_uncertain;
1407 this_participant := convert_animid(this_1_min_infant.aggression
1408 , this_follow.follow_date);
1409 END CASE;
1410
1411 INSERT INTO events (intid
1412 , behavior
1413 , position
1414 , baboon
1415 , uncertain
1416 , comment)
1417 VALUES (currval('intervals_intid_seq'::regclass)
1418 , 'ASSR'
1419 , _gen_pos('ASSR')
1420 , this_baboon
1421 , this_uncertain
1422 , NULL);
1423
1424 -- Participant.
1425 INSERT INTO parts (eid, part, participant)
1426 VALUES (CURRVAL('events_eid_seq'::regclass)
1427 , 'gmi_actor'
1428 , this_follow.infant_id);
1429
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)
1434 , 'gmi_actee'
1435 , this_participant);
1436 END IF;
1437 END IF;
1438
1439
1440 -- DEVBEH! Comment
1441 IF this_1_min.comment ~ 'DEVBEH!'
1442 -- Is nothing else
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
1458
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;
1466 END IF;
1467
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;
1472 ELSE
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
1478 , this_devbeh
1479 , 2);
1480 IF TRIM(BOTH FROM interval_comment) = '' THEN
1481 interval_comment := NULL;
1482 END IF;
1483
1484 -- Save the new interval comment.
1485 UPDATE intervals
1486 SET comment = interval_comment
1487 WHERE
1488 intervals.intid = CURRVAL('intervals_intid_seq'::regclass);
1489 END IF;
1490 END IF;
1491
1492
1493 INSERT INTO events (intid
1494 , behavior
1495 , position
1496 , baboon
1497 , uncertain
1498 , comment)
1499 VALUES (currval('intervals_intid_seq'::regclass)
1500 , 'O'
1501 , _gen_pos('O')
1502 , FALSE
1503 , default_infant_uncertain
1504 , this_devbeh);
1505
1506 -- Participant -- focal
1507 INSERT INTO parts (eid, part, participant)
1508 VALUES (CURRVAL('events_eid_seq'::regclass)
1509 , 'gmi_self'
1510 , this_follow.infant_id);
1511 END IF;
1512
1513
1514 -- Distance to mom.
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
1520
1521 this_error := FALSE;
1522 this_distance := NULL;
1523 this_uncertain := default_infant_uncertain;
1524
1525 IF this_1_min_infant.in_contact IS NOT NULL THEN
1526 this_distance := '1';
1527 CASE this_1_min_infant.in_contact
1528 WHEN 'Y' THEN NULL;
1529 WHEN '?' THEN
1530 this_uncertain := TRUE;
1531 ELSE
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;
1538 END CASE;
1539 END IF;
1540
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
1545 WHEN 'Y' THEN NULL;
1546 WHEN '?' THEN
1547 this_uncertain := TRUE;
1548 ELSE
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;
1555 END CASE;
1556 END IF;
1557
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
1562 WHEN 'Y' THEN NULL;
1563 WHEN '?' THEN
1564 this_uncertain := TRUE;
1565 ELSE
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;
1572 END CASE;
1573 END IF;
1574
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
1579 WHEN 'Y' THEN NULL;
1580 WHEN '?' THEN
1581 this_uncertain := TRUE;
1582 ELSE
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;
1589 END CASE;
1590 END IF;
1591
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
1596 WHEN 'Y' THEN NULL;
1597 WHEN '?' THEN
1598 this_uncertain := TRUE;
1599 ELSE
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;
1606 END CASE;
1607 END IF;
1608
1609 IF this_error THEN
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;
1615 END IF;
1616
1617 INSERT INTO distances (intid, uncertain, distance)
1618 VALUES (currval('intervals_intid_seq'::regclass)
1619 , this_uncertain
1620 , this_distance);
1621
1622 -- Infant.
1623 INSERT INTO distanceparts (distid, role, participant)
1624 VALUES (CURRVAL('distances_distid_seq'::regclass)
1625 , 'I'
1626 , this_follow.infant_id);
1627
1628 -- Mom.
1629 INSERT INTO distanceparts (distid, role, participant)
1630 VALUES (CURRVAL('distances_distid_seq'::regclass)
1631 , 'M'
1632 , this_follow.mother_id);
1633 END IF;
1634
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;
1645 END IF;
1646
1647 -- Cries
1648 IF this_1_min_infant.cries IS NOT NULL THEN
1649 CASE this_1_min_infant.cries
1650 WHEN 'Y' THEN
1651 this_uncertain := default_infant_uncertain;
1652 WHEN '?' THEN
1653 this_uncertain := TRUE;
1654 ELSE
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;
1661 END CASE;
1662
1663 INSERT INTO events (intid
1664 , behavior
1665 , position
1666 , baboon
1667 , uncertain
1668 , comment)
1669 VALUES (currval('intervals_intid_seq'::regclass)
1670 , 'V'
1671 , _gen_pos('V')
1672 , FALSE
1673 , this_uncertain
1674 , NULL);
1675
1676 INSERT INTO vocs (eid, vocalization)
1677 VALUES (CURRVAL('events_eid_seq'::regclass)
1678 , 'LIA');
1679
1680 -- Participant.
1681 INSERT INTO parts (eid, part, participant)
1682 VALUES (CURRVAL('events_eid_seq'::regclass)
1683 , 'gmi_self'
1684 , this_follow.infant_id);
1685 END IF;
1686
1687 -- Infant vocalizations.
1688 IF this_1_min_infant.vocalizations IS NOT NULL THEN
1689
1690 old_voc := TRIM(BOTH FROM this_1_min_infant.vocalizations);
1691
1692 -- Defaults.
1693 this_voc_uncertain := default_infant_uncertain;
1694
1695 -- Convert the vocalization code.
1696 CASE old_voc
1697 WHEN 'Chk' THEN
1698 new_voc := 'CHK';
1699
1700 WHEN 'Fg' THEN
1701 new_voc := 'FG';
1702
1703 WHEN 'Hoo' THEN
1704 new_voc := 'HOO';
1705
1706 WHEN 'Lia' THEN
1707 new_voc := 'LIA';
1708
1709 WHEN 'Pb' THEN
1710 new_voc := 'PB';
1711
1712 WHEN 'Pg' THEN
1713 new_voc := 'PG';
1714
1715 WHEN 'Ph' THEN
1716 new_voc := 'PH';
1717
1718 WHEN 'Scr' THEN
1719 new_voc := 'SCR';
1720
1721 WHEN 'Wb' THEN
1722 new_voc := 'WB';
1723
1724 WHEN 'Wh' THEN
1725 new_voc := 'WH';
1726
1727 WHEN 'Multi' THEN
1728 new_voc := 'MUL';
1729
1730 WHEN 'Other' THEN
1731 new_voc := 'OTH';
1732
1733 WHEN 'Grunt' THEN
1734 new_voc := 'GR';
1735
1736 WHEN 'Y' THEN
1737 new_voc := 'Y';
1738
1739 WHEN '?' THEN
1740 new_voc := 'Y';
1741 this_voc_uncertain := TRUE;
1742
1743 ELSE
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
1750 , old_voc;
1751
1752 END CASE;
1753
1754 -- Save vocalization event.
1755 INSERT INTO events (intid
1756 , behavior
1757 , position
1758 , baboon
1759 , uncertain
1760 , comment)
1761 VALUES (
1762 currval('intervals_intid_seq'::regclass)
1763 , 'V'
1764 , _gen_pos('V')
1765 , FALSE
1766 , this_voc_uncertain
1767 , NULL);
1768
1769 INSERT INTO vocs (eid, vocalization)
1770 VALUES (currval('events_eid_seq'::regclass)
1771 , new_voc);
1772
1773 -- Participant.
1774 INSERT INTO parts (eid, part, participant)
1775 VALUES (CURRVAL('events_eid_seq'::regclass)
1776 , 'gmi_self'
1777 , this_follow.infant_id);
1778
1779 END IF; -- There is a infant vocalization.
1780
1781 --
1782 -- follow_1_minute_mother:
1783 --
1784
1785 SELECT *
1786 INTO this_1_min_mom
1787 FROM follow_1_minute_mother
1788 WHERE follow_1_minute_mother.interval_id = this_1_min.interval_id;
1789
1790 -- | Behavior | Grooms_other | Groomed_by |
1791
1792 -- tableid | Interval_id | Mother_id | Behavior | Grooms_other | Groomed_by | Vocalizations
1793 -- 1 | 11/08/2010_GM_01_01 | GM | Rest | | |
1794
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;
1802 END IF;
1803
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;
1809 END IF;
1810
1811 --
1812 -- Mom behavior.
1813 --
1814
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)
1822
1823 -- Split into separate codes.
1824 behaviors := regexp_split_to_array(this_1_min_mom.behavior, ';');
1825
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);
1832 CASE old_behavior
1833
1834 WHEN 'Grooming' THEN
1835 grooming_found := TRUE;
1836
1837 WHEN 'Play' THEN
1838 play_found := TRUE;
1839
1840 ELSE NULL;
1841 END CASE;
1842 END LOOP;
1843
1844 -- Test for grooming and play mixed.
1845 grooming_and_play := grooming_found AND play_found;
1846
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[];
1856
1857 eventcnt := 0;
1858 FOREACH old_behavior IN ARRAY behaviors LOOP
1859
1860 old_behavior := TRIM(BOTH FROM old_behavior);
1861 this_uncertain := default_mom_uncertain;
1862
1863 CASE old_behavior
1864
1865 WHEN '?' THEN
1866 new_behavior := 'UNK';
1867 this_uncertain := TRUE;
1868
1869 WHEN '-' THEN
1870 CONTINUE; -- no event
1871
1872 WHEN 'ASSR' THEN
1873 new_behavior := 'ASSR';
1874
1875 WHEN 'Bad obs' THEN
1876 new_behavior := 'BAD';
1877 this_uncertain := TRUE;
1878
1879 WHEN 'Build nest' THEN
1880 new_behavior := 'BN';
1881
1882 WHEN 'Climb' THEN
1883 new_behavior := 'C';
1884
1885 WHEN 'Drink' THEN
1886 new_behavior := 'DR';
1887
1888 WHEN 'Eat' THEN
1889 new_behavior := '_ca_eating';
1890
1891 WHEN 'Groomed' THEN
1892 new_behavior := 'G';
1893
1894 WHEN 'Grooming' THEN
1895 new_behavior := 'G';
1896
1897 WHEN 'Multi' THEN
1898 new_behavior := 'MULTI';
1899 multi_found := TRUE;
1900
1901 WHEN 'Multigroom' THEN
1902 new_behavior := 'G';
1903
1904 WHEN 'Other' THEN
1905 new_behavior := 'O';
1906
1907 WHEN 'Play' THEN
1908 new_behavior := 'P';
1909
1910 WHEN 'Rest' THEN
1911 new_behavior := 'R';
1912
1913 WHEN 'Self-groom' THEN
1914 new_behavior := 'G';
1915
1916 WHEN 'Travel' THEN
1917 new_behavior := 'T';
1918
1919 WHEN 'Vua' THEN
1920 new_behavior := 'VUA';
1921
1922 ELSE
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
1929 , old_behavior;
1930 END CASE;
1931
1932 -- Setup defaults.
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;
1940
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;
1950 END IF;
1951
1952 event_behavior[eventcnt] := new_behavior;
1953
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
1962 , ',');
1963 animid_cnt := ARRAY_LENGTH(animids, 1);
1964
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;
1969 END IF;
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';
1979 ELSE
1980 event_behavior[eventcnt] := new_behavior;
1981 END IF;
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';
1992 ELSE
1993 part1_parts[eventcnt] := 'gmi_actor';
1994 part2_parts[eventcnt] := 'gmi_actee';
1995 END IF;
1996 part1_participants[eventcnt] := this_follow.mother_id;
1997 CASE animid
1998 WHEN '?' THEN
1999 part2_participants[eventcnt] := 'UNK';
2000 event_uncertain[eventcnt] := TRUE;
2001 WHEN 'Y' THEN
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.';
2009 END IF;
2010 ELSE
2011 IF (grooming_and_play
2012 AND animid_cnt > 1
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';
2022 END IF;
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';
2026 gp_unknown := TRUE;
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';
2037 gp_unknown := TRUE;
2038
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;
2055 ELSE
2056 part2_participants[eventcnt] := animid;
2057 END IF;
2058 END CASE;
2059
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;
2069 END IF;
2070 END IF;
2071 END IF;
2072
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
2080 , ',');
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;
2086 END IF;
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';
2097 ELSE
2098 event_behavior[eventcnt] := new_behavior;
2099 event_comment[eventcnt] := NULL;
2100 END IF;
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';
2106 CASE animid
2107 WHEN '?' THEN
2108 part2_participants[eventcnt] := 'UNK';
2109 event_uncertain[eventcnt] := TRUE;
2110 WHEN 'Y' THEN
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;
2123 ELSE
2124 part2_participants[eventcnt] := animid;
2125 END CASE;
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;
2135 END IF;
2136 END IF;
2137 END IF;
2138 END LOOP; -- Over mom behaviors
2139
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];
2145
2146
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.
2157
2158 i_found := FALSE;
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.
2167 UPDATE events
2168 SET uncertain = TRUE
2169 WHERE events.eid = infant_mom_events[j];
2170 END IF;
2171
2172 -- Note that we've done with it by removing the array
2173 -- element.
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];
2180 i_found := TRUE;
2181 EXIT;
2182 END IF;
2183 END LOOP;
2184 END IF;
2185
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;
2194 END IF;
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.
2205
2206 i_found := FALSE;
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.
2215 UPDATE events
2216 SET uncertain = TRUE
2217 WHERE events.eid = infant_mom_events[j];
2218 END IF;
2219
2220 -- Note that we've done with it by removing the array
2221 -- element.
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];
2228 i_found := TRUE;
2229 EXIT;
2230 END IF;
2231 END LOOP;
2232 END IF;
2233
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;
2242 END IF;
2243
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.
2254
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.
2261 UPDATE parts
2262 SET part = 'gmi_participant'
2263 WHERE parts.eid = infant_mom_events[j];
2264
2265 IF infant_mom_uncertains[j] != event_uncertain[i]
2266 AND NOT(infant_mom_uncertains[j]) THEN
2267 -- Must make the recorded behavior uncertain.
2268 UPDATE events
2269 SET uncertain = TRUE
2270 WHERE events.eid = infant_mom_events[j];
2271 END IF;
2272
2273 EXIT;
2274 END IF;
2275 END LOOP;
2276 END IF;
2277 ELSE
2278
2279 -- Insert a row into events.
2280 INSERT INTO events (intid
2281 , behavior
2282 , position
2283 , baboon
2284 , uncertain
2285 , comment)
2286 VALUES (
2287 currval('intervals_intid_seq'::regclass)
2288 , event_behavior[i]
2289 , _gen_pos(event_behavior[i])
2290 , event_baboon[i]
2291 , event_uncertain[i]
2292 , event_comment[i]);
2293
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]);
2299
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]);
2305
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
2327 || '{play}';
2328 mom_sib_uncertains = mom_sib_uncertains
2329 || event_uncertain[i];
2330 END IF;
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
2335
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');
2344 END IF;
2345
2346 END IF;
2347 END LOOP; -- Over participants
2348 END IF;
2349 END IF;
2350
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;
2361 END IF;
2362
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;
2372 END IF;
2373
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];
2389 END IF;
2390 END LOOP;
2391 END IF;
2392
2393 -- Mother vocalizations.
2394 IF this_1_min_mom.vocalizations IS NOT NULL THEN
2395
2396 old_voc := TRIM(BOTH FROM this_1_min_mom.vocalizations);
2397
2398 -- Defaults.
2399 this_voc_uncertain := default_mom_uncertain;
2400
2401 -- Convert the vocalization code.
2402 CASE old_voc
2403 WHEN 'Chk' THEN
2404 new_voc := 'CHK';
2405
2406 WHEN 'Fg' THEN
2407 new_voc := 'FG';
2408
2409 WHEN 'Hoo' THEN
2410 new_voc := 'HOO';
2411
2412 WHEN 'Lia' THEN
2413 new_voc := 'LIA';
2414
2415 WHEN 'Pb' THEN
2416 new_voc := 'PB';
2417
2418 WHEN 'Pg' THEN
2419 new_voc := 'PG';
2420
2421 WHEN 'Ph' THEN
2422 new_voc := 'PH';
2423
2424 WHEN 'Scr' THEN
2425 new_voc := 'SCR';
2426
2427 WHEN 'Wb' THEN
2428 new_voc := 'WB';
2429
2430 WHEN 'Wh' THEN
2431 new_voc := 'WH';
2432
2433 WHEN 'Multi' THEN
2434 new_voc := 'MUL';
2435
2436 WHEN 'Other' THEN
2437 new_voc := 'OTH';
2438
2439 WHEN 'Grunt' THEN
2440 new_voc := 'GR';
2441
2442 WHEN 'Y' THEN
2443 new_voc := 'Y';
2444
2445 WHEN '?' THEN
2446 new_voc := 'Y';
2447 this_voc_uncertain := TRUE;
2448
2449 ELSE
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
2456 , old_voc;
2457
2458 END CASE;
2459
2460 -- Save vocalization event.
2461 INSERT INTO events (intid
2462 , behavior
2463 , position
2464 , baboon
2465 , uncertain
2466 , comment)
2467 VALUES (
2468 currval('intervals_intid_seq'::regclass)
2469 , 'V'
2470 , _gen_pos('V')
2471 , FALSE
2472 , this_voc_uncertain
2473 , NULL);
2474
2475 INSERT INTO vocs (eid, vocalization)
2476 VALUES (currval('events_eid_seq'::regclass)
2477 , new_voc);
2478
2479 -- Participant.
2480 INSERT INTO parts (eid, part, participant)
2481 VALUES (CURRVAL('events_eid_seq'::regclass)
2482 , 'gmi_self'
2483 , this_follow.mother_id);
2484
2485 END IF; -- There is a mom vocalization.
2486
2487 --
2488 -- follow_1_minute_sibling:
2489 --
2490
2491 SELECT *
2492 INTO this_1_min_sib
2493 FROM follow_1_minute_sibling
2494 WHERE follow_1_minute_sibling.interval_id = this_1_min.interval_id;
2495
2496 sibling_found := FOUND;
2497 IF sibling_found THEN
2498 -- | Behavior | Grooms_other | Groomed_by |
2499
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
2502
2503
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;
2513 END IF;
2514
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;
2521 END IF;
2522
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
2527 WHEN 'Y' THEN
2528 default_sib_uncertain := TRUE;
2529 WHEN '-' THEN
2530 NULL; -- Ignore.
2531 ELSE
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;
2539 END CASE;
2540 END IF;
2541
2542 -- Sibling behavior.
2543 grooms_other_converted := FALSE;
2544 groomed_by_converted := FALSE;
2545 plays_converted := FALSE;
2546
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.
2552
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[];
2562
2563 -- Split into separate codes.
2564 behaviors := regexp_split_to_array(this_1_min_sib.behavior, ';');
2565 eventcnt := 0;
2566
2567 FOREACH old_behavior IN ARRAY behaviors LOOP
2568
2569 old_behavior := TRIM(BOTH FROM old_behavior);
2570 this_uncertain := default_sib_uncertain;
2571
2572 CASE old_behavior
2573
2574 WHEN 'Nurse' THEN
2575 new_behavior := 'N';
2576
2577 WHEN 'Ride' THEN
2578 new_behavior := 'RI';
2579
2580 WHEN '?' THEN
2581 new_behavior := 'UNK';
2582 this_uncertain := TRUE;
2583
2584 WHEN '-' THEN
2585 CONTINUE; -- no event
2586
2587 WHEN 'ASSR' THEN
2588 new_behavior := 'ASSR';
2589
2590 WHEN 'Bad obs' THEN
2591 new_behavior := 'BAD';
2592 this_uncertain := TRUE;
2593
2594 WHEN 'Build nest' THEN
2595 new_behavior := 'BN';
2596
2597 WHEN 'Climb' THEN
2598 new_behavior := 'C';
2599
2600 WHEN 'Drink' THEN
2601 new_behavior := 'DR';
2602
2603 WHEN 'Eat' THEN
2604 new_behavior := '_ca_eating';
2605
2606 WHEN 'Groomed' THEN
2607 new_behavior := 'G';
2608
2609 WHEN 'Grooming' THEN
2610 new_behavior := 'G';
2611
2612 WHEN 'Multi' THEN
2613 new_behavior := 'MULTI';
2614
2615 WHEN 'Multigroom' THEN
2616 new_behavior := 'G';
2617
2618 WHEN 'Other' THEN
2619 new_behavior := 'O';
2620
2621 WHEN 'Play' THEN
2622 new_behavior := 'P';
2623
2624 WHEN 'Rest' THEN
2625 new_behavior := 'R';
2626
2627 WHEN 'Self-groom' THEN
2628 new_behavior := 'G';
2629
2630 WHEN 'Travel' THEN
2631 new_behavior := 'T';
2632
2633 WHEN 'Vua' THEN
2634 new_behavior := 'VUA';
2635
2636 ELSE
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
2643 , old_behavior;
2644 END CASE;
2645
2646 -- Setup defaults.
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;
2654
2655 event_behavior[eventcnt] := new_behavior;
2656
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;
2666 END IF;
2667
2668 past_first := FALSE;
2669
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
2681 , ',');
2682 ELSE -- play
2683 plays_converted := TRUE;
2684 animids := regexp_split_to_array(this_1_min_sib.plays, ',');
2685 END IF;
2686
2687 eventcnt := eventcnt - 1;
2688 FOREACH animid IN ARRAY animids LOOP
2689 eventcnt := eventcnt + 1;
2690 past_first := TRUE;
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';
2704 ELSE
2705 part1_parts[eventcnt] := 'gmi_actor';
2706 part2_parts[eventcnt] := 'gmi_actee';
2707 END IF;
2708 CASE animid
2709 WHEN '?' THEN
2710 part2_participants[eventcnt] := 'UNK';
2711 event_uncertain[eventcnt] := TRUE;
2712 WHEN 'Y' THEN
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;
2721 ELSE
2722 part2_participants[eventcnt] := animid;
2723 END CASE;
2724
2725 END LOOP; -- Over grooms_other/plays animids
2726 ELSE
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;
2742 END IF;
2743 END IF;
2744 END IF;
2745
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
2752 , ',');
2753 IF NOT(past_first) THEN
2754 -- Have not yet filled the first participant.
2755 eventcnt := eventcnt - 1;
2756 END IF;
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';
2768 CASE animid
2769 WHEN '?' THEN
2770 part2_participants[eventcnt] := 'UNK';
2771 event_uncertain[eventcnt] := TRUE;
2772 WHEN 'Y' THEN
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;
2785 ELSE
2786 part2_participants[eventcnt] := animid;
2787 END CASE;
2788
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;
2798 END IF;
2799 END IF;
2800 END IF;
2801
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';
2812 END IF;
2813
2814 END LOOP; -- Over sibling behaviors
2815
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
2820
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.
2831
2832 i_found := FALSE;
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.
2841 UPDATE events
2842 SET uncertain = TRUE
2843 WHERE events.eid = infant_sib_events[j];
2844 END IF;
2845
2846 -- Note that we've done with it by removing the array
2847 -- element.
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];
2854 i_found := TRUE;
2855 EXIT;
2856 END IF;
2857 END LOOP;
2858 END IF;
2859
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;
2868 END IF;
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.
2879
2880 i_found := FALSE;
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.
2889 UPDATE events
2890 SET uncertain = TRUE
2891 WHERE events.eid = infant_sib_events[j];
2892 END IF;
2893
2894 -- Note that we've done with it by removing the array
2895 -- element.
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];
2902 i_found := TRUE;
2903 EXIT;
2904 END IF;
2905 END LOOP;
2906 END IF;
2907
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;
2916 END IF;
2917
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.
2928
2929
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
2935 -- the sibling.
2936 -- Make the play mutual.
2937 UPDATE parts
2938 SET part = 'gmi_participant'
2939 WHERE parts.eid = infant_sib_events[j];
2940
2941 IF infant_sib_uncertains[j] != event_uncertain[i]
2942 AND NOT(infant_sib_uncertains[j]) THEN
2943 -- Must make the recorded behavior uncertain.
2944 UPDATE events
2945 SET uncertain = TRUE
2946 WHERE events.eid = infant_sib_events[j];
2947 END IF;
2948
2949 EXIT;
2950 END IF;
2951 END LOOP;
2952 END IF;
2953
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.
2964
2965 i_found := FALSE;
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.
2974 UPDATE events
2975 SET uncertain = TRUE
2976 WHERE events.eid = mom_sib_events[j];
2977 END IF;
2978
2979 -- Note that we've done with it by removing the array
2980 -- element.
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];
2987 i_found := TRUE;
2988 EXIT;
2989 END IF;
2990 END LOOP;
2991 END IF;
2992
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;
3001 END IF;
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.
3012
3013 i_found := FALSE;
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.
3022 UPDATE events
3023 SET uncertain = TRUE
3024 WHERE events.eid = mom_sib_events[j];
3025 END IF;
3026
3027 -- Note that we've done with it by removing the array
3028 -- element.
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];
3035 i_found := TRUE;
3036 EXIT;
3037 END IF;
3038 END LOOP;
3039 END IF;
3040
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;
3049 END IF;
3050
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.
3061
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.
3068 UPDATE parts
3069 SET part = 'gmi_participant'
3070 WHERE parts.eid = mom_sib_events[j];
3071
3072 IF mom_sib_uncertains[j] != event_uncertain[i]
3073 AND NOT(mom_sib_uncertains[j]) THEN
3074 -- Must make the recorded behavior uncertain.
3075 UPDATE events
3076 SET uncertain = TRUE
3077 WHERE events.eid = mom_sib_events[j];
3078 END IF;
3079
3080 EXIT;
3081 END IF;
3082 END LOOP;
3083 END IF;
3084
3085 ELSE
3086 -- Insert a row into events.
3087 INSERT INTO events (intid
3088 , behavior
3089 , position
3090 , baboon
3091 , uncertain
3092 , comment)
3093 VALUES (
3094 currval('intervals_intid_seq'::regclass)
3095 , event_behavior[i]
3096 , _gen_pos(event_behavior[i])
3097 , event_baboon[i]
3098 , event_uncertain[i]
3099 , NULL);
3100
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]);
3106
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]);
3112 END IF;
3113
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');
3126 END IF;
3127 END IF; -- Check for duplicates regards focals
3128
3129 END LOOP; -- Over participants
3130 END IF;
3131 END IF;
3132
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;
3143 END IF;
3144
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;
3154 END IF;
3155
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;
3165 END IF;
3166
3167 -- Distance_from_mother
3168
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
3172
3173 IF this_1_min_sib.distance_from_mother = '?' THEN
3174 this_distance := '99';
3175 this_uncertain := TRUE;
3176 ELSE
3177 this_distance := this_1_min_sib.distance_from_mother;
3178 this_uncertain := default_sib_uncertain;
3179 END IF;
3180
3181 -- Create the distances row.
3182 INSERT INTO distances (intid, uncertain, distance)
3183 VALUES (currval('intervals_intid_seq'::regclass)
3184 , this_uncertain
3185 , this_distance);
3186
3187 -- Create the distanceparts for the mom.
3188 INSERT INTO distanceparts (distid, role, participant)
3189 VALUES (currval('distances_distid_seq'::regclass)
3190 , 'M'
3191 , this_follow.mother_id);
3192
3193 -- Create the distanceparts for the sibling.
3194 INSERT INTO distanceparts (distid, role, participant)
3195 VALUES (currval('distances_distid_seq'::regclass)
3196 , 'S'
3197 , this_follow.sibling_id);
3198
3199 END IF; -- There is a sibling-mom distance.
3200
3201 -- Distance_from_infant
3202
3203
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
3207
3208 IF this_1_min_sib.distance_from_infant = '?' THEN
3209 this_distance := '99';
3210 this_uncertain := TRUE;
3211 ELSE
3212 this_distance := this_1_min_sib.distance_from_infant;
3213 this_uncertain := default_sib_uncertain;
3214 END IF;
3215
3216 -- Create the distances row.
3217 INSERT INTO distances (intid, uncertain, distance)
3218 VALUES (currval('intervals_intid_seq'::regclass)
3219 , this_uncertain
3220 , this_distance);
3221
3222 -- Create the distanceparts for the sibling.
3223 INSERT INTO distanceparts (distid, role, participant)
3224 VALUES (currval('distances_distid_seq'::regclass)
3225 , 'S'
3226 , this_follow.sibling_id);
3227
3228 -- Create the distanceparts for the infant.
3229 INSERT INTO distanceparts (distid, role, participant)
3230 VALUES (currval('distances_distid_seq'::regclass)
3231 , 'I'
3232 , this_follow.infant_id);
3233
3234 END IF; -- There is a sibling-infant distance.
3235
3236 -- Sibling vocalizations.
3237
3238 IF this_1_min_sib.vocalizations IS NOT NULL THEN
3239
3240 old_voc := TRIM(BOTH FROM this_1_min_sib.vocalizations);
3241
3242 -- Defaults.
3243 this_voc_uncertain := default_sib_uncertain;
3244
3245 -- Convert the vocalization code.
3246 CASE old_voc
3247 WHEN 'Chk' THEN
3248 new_voc := 'CHK';
3249
3250 WHEN 'Fg' THEN
3251 new_voc := 'FG';
3252
3253 WHEN 'Hoo' THEN
3254 new_voc := 'HOO';
3255
3256 WHEN 'Lia' THEN
3257 new_voc := 'LIA';
3258
3259 WHEN 'Pb' THEN
3260 new_voc := 'PB';
3261
3262 WHEN 'Pg' THEN
3263 new_voc := 'PG';
3264
3265 WHEN 'Ph' THEN
3266 new_voc := 'PH';
3267
3268 WHEN 'Scr' THEN
3269 new_voc := 'SCR';
3270
3271 WHEN 'Wb' THEN
3272 new_voc := 'WB';
3273
3274 WHEN 'Wh' THEN
3275 new_voc := 'WH';
3276
3277 WHEN 'Multi' THEN
3278 new_voc := 'MUL';
3279
3280 WHEN 'Other' THEN
3281 new_voc := 'OTH';
3282
3283 WHEN 'Grunt' THEN
3284 new_voc := 'GR';
3285
3286 WHEN 'Y' THEN
3287 new_voc := 'Y';
3288
3289 WHEN '?' THEN
3290 new_voc := 'Y';
3291 this_voc_uncertain := TRUE;
3292
3293 ELSE
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
3300 , old_voc;
3301
3302 END CASE;
3303
3304 -- Save vocalization event.
3305 INSERT INTO events (intid
3306 , behavior
3307 , position
3308 , baboon
3309 , uncertain
3310 , comment)
3311 VALUES (
3312 currval('intervals_intid_seq'::regclass)
3313 , 'V'
3314 , _gen_pos('V')
3315 , FALSE
3316 , this_voc_uncertain
3317 , NULL);
3318
3319 INSERT INTO vocs (eid, vocalization)
3320 VALUES (currval('events_eid_seq'::regclass)
3321 , new_voc);
3322
3323 -- Participant.
3324 INSERT INTO parts (eid, part, participant)
3325 VALUES (CURRVAL('events_eid_seq'::regclass)
3326 , 'gmi_self'
3327 , this_follow.sibling_id);
3328
3329 END IF; -- There is a sib vocalization.
3330 END IF; -- There is a sibling.
3331
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];
3344 END IF;
3345 END LOOP;
3346 END IF;
3347
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];
3363 END IF;
3364 END LOOP;
3365 END IF;
3366
3367 -- Remember we converted this minute.
3368 UPDATE follow_1_minute
3369 SET done = TRUE
3370 WHERE tableid = this_1_min.tableid;
3371
3372 -- Remember we converted this mother/minute
3373 UPDATE follow_1_minute_mother
3374 SET done = TRUE
3375 WHERE tableid = this_1_min_mom.tableid;
3376
3377 -- Remember we converted this infant/minute.
3378 UPDATE follow_1_minute_infant
3379 SET done = TRUE
3380 WHERE tableid = this_1_min_infant.tableid;
3381
3382 IF sibling_found THEN
3383 -- Remember we converted this sibling/minute.
3384 UPDATE follow_1_minute_sibling
3385 SET done = TRUE
3386 WHERE tableid = this_1_min_sib.tableid;
3387 END IF;
3388
3389 END LOOP; -- Over 1 minutes
3390
3391 -- Remember we converted this follow.
3392 UPDATE follow
3393 SET done = TRUE
3394 WHERE tableid = this_follow.tableid;
3395
3396 -- Keep track of how many follows we converted.
3397 converted := converted + 1;
3398 END LOOP;
3399
3400 RETURN converted;
3401
3402 END;
3403 $$;
3404
3405
3406 grant_func_priv(`prepared_data.convert_access(DATE, DATE)')