forked from nmfs-ost/ss3-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SS_expval.tpl
1126 lines (1086 loc) · 54.8 KB
/
SS_expval.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SS_Label_file #15. **SS_expval.tpl**
// SS_Label_file # * <u>Get_expected_values()</u> // for a specified season and subseason that has data, updates the ALK for that time and calculates expected values for any type of data from any fleet/survey
// SS_Label_file #
//********************************************************************
/* SS_Label_FUNCTION 46 Get_expected_values: check for data */
FUNCTION void Get_expected_values(const int y, const int t);
{
dvariable temp1;
dvar_vector age_exp(0, nages2);
for (subseas = 1; subseas <= N_subseas; subseas++)
{
// make age-length key if needed
ALK_idx = (s - 1) * N_subseas + subseas;
ALK_time = (y - styr) * nseas * N_subseas + (s - 1) * N_subseas + subseas;
if (ALK_subseas_update(ALK_idx) == 1 || have_data(ALK_time, 0, 0, 0) > 0) // need ALK update for growth reasons or for data reasons
{
get_growth3(y, t, s, subseas);
Make_AgeLength_Key(s, subseas);
}
for (f = 1; f <= Nfleet; f++)
{
if (have_data(ALK_time, f, 0, 0) > 0)
{
p = fleet_area(f);
timing = data_time(ALK_time, f, 2) * seasdur(s); // within season elapsed time same for all datatypes of this fleet x time
// make selected age-length sample for this fleet and with this timing
{
exp_AL.initialize();
exp_AL_ret.initialize();
for (g = 1; g <= gmorph; g++)
if (use_morph(g) > 0)
{
int ALK_finder = (ALK_idx - 1) * gmorph + g;
ivector ALK_range_lo = ALK_range_g_lo(ALK_finder);
ivector ALK_range_hi = ALK_range_g_hi(ALK_finder);
gg = sx(g);
if (gg == 2)
{
L1 = nlength1;
L2 = nlength2;
A2 = nages + 1;
} // move over on length dimension to store males
else
{
L1 = 1;
L2 = nlength;
A2 = 0;
}
/*
if(F_Method==1 && surveytime(f)<0.0) // Pope's approximation
{tempvec_a=elem_prod(Nmid(g),sel_a(y,f,gg));} // CHECK Nmid may not exist correctly unless still within the area loop
else if(surveytime(f)<0.0) // mimic fishery catch, but without Hrate so gets available numbers
{tempvec_a=elem_prod(natage(t,p,g),elem_prod(Zrate2(p,g),sel_a(y,f,gg)));}
else // explicit timing
{tempvec_a=elem_prod(natage(t,p,g),elem_prod(mfexp(-Z_rate(t,p,g)*timing),sel_a(y,f,gg)));}
*/
if (timing >= 0.0)
{
tempvec_a = elem_prod(natage(t, p, g), elem_prod(mfexp(-Z_rate(t, p, g) * timing), sel_a(y, f, gg)));
} // explicit timing
else
{
if (F_Method > 1) // mimic fishery catch, so gets mean available numbers
{
tempvec_a = elem_prod(natage(t, p, g), elem_prod(Zrate2(p, g), sel_a(y, f, gg)));
}
else // Pope's approximation
{
Nmid(g) = elem_prod(natage(t, p, g), surv1(s, GP3(g))); // numbers-at-age(g,a) surviving to middle of time period
tempvec_a = elem_prod(Nmid(g), sel_a(y, f, gg));
}
}
tempvec_a = elem_prod(tempvec_a, keep_age(g, ALK_idx));
if (Do_Retain(f) == 0)
{
if (dolen(f) == 1)
{
for (a = 0; a <= nages; a++)
{
temp = tempvec_a(a);
for (z = ALK_range_lo(a); z <= ALK_range_hi(a); z++)
{
exp_AL(a + A2, L1 - 1 + z) += temp * ALK(ALK_idx, g, a, z) * sel_l(y, f, gg, z);
; // note that A2 and L1 depend on what sex g is
}
}
}
else
{
for (a = 0; a <= nages; a++)
{
temp = tempvec_a(a);
for (z = ALK_range_lo(a); z <= ALK_range_hi(a); z++)
{
exp_AL(a + A2, L1 - 1 + z) += temp * ALK(ALK_idx, g, a, z);
}
}
}
exp_AL_ret = exp_AL; // in case user has retain code 2 rather than 0
}
else // need retain matrix
{
if (dolen(f) == 1) // need retention and length
{
for (a = 0; a <= nages; a++)
{
temp = tempvec_a(a);
temp1 = tempvec_a(a) * retain_a(y, f, gg, a);
for (z = ALK_range_lo(a); z <= ALK_range_hi(a); z++)
{
exp_AL(a + A2, L1 - 1 + z) += temp * ALK(ALK_idx, g, a, z) * sel_l(y, f, gg, z); // note that A2 and L1 depend on what sex g is
exp_AL_ret(a + A2, L1 - 1 + z) += temp1 * ALK(ALK_idx, g, a, z) * sel_l(y, f, gg, z) * retain(y, f, L1 - 1 + z); // note that A2 and L1 depend on what sex g is
}
}
}
else // need retention, but no length selex
{
for (a = 0; a <= nages; a++)
{
temp = tempvec_a(a);
temp1 = tempvec_a(a) * retain_a(y, f, gg, a);
for (z = ALK_range_lo(a); z <= ALK_range_hi(a); z++)
{
exp_AL(a + A2, L1 - 1 + z) += temp * ALK(ALK_idx, g, a, z);
exp_AL_ret(a + A2, L1 - 1 + z) += temp1 * ALK(ALK_idx, g, a, z) * retain(y, f, L1 - 1 + z);
}
}
}
}
// code below once erroneously built up catch by morph from exp_AL
// that approach is incorrect, because exp_AL already accumulates the morphs!!!!!
// putting a morph accumulation into the code above would slow computations for everyone in order to have this rarely used feature
// so instead, replicate the above to store directly into morphcomp_exp, rather than into the exp_AL matrix
// note that partition is read with morphcomp data, but it is not used
// fleets with no defined retention function base morphcomp off total catch
// fleets with retention defined, use retained catch
if (have_data(ALK_time, f, 8, 0) > 0) // morphcomp observation
{
int j = have_data(ALK_time, f, 8, 1); // observation number
// {Morphcomp_exp(j,5+GP4(g))+=sum(exp_AL);
if (Do_Retain(f) == 0)
{
if (dolen(f) == 1) // uses length selectivity
{
for (a = 0; a <= nages; a++)
{
temp = tempvec_a(a);
for (z = ALK_range_lo(a); z <= ALK_range_hi(a); z++)
{
Morphcomp_exp(j, 5 + GP4(g)) += temp * ALK(ALK_idx, g, a, z) * sel_l(y, f, gg, z);
; // note that A2 and L1 depend on what sex g is
}
}
}
else
{
for (a = 0; a <= nages; a++)
{
temp = tempvec_a(a);
for (z = ALK_range_lo(a); z <= ALK_range_hi(a); z++)
{
Morphcomp_exp(j, 5 + GP4(g)) += temp * ALK(ALK_idx, g, a, z);
}
}
}
}
else // must base sample on retained catch from a fleet that has retention defined
{
if (dolen(f) == 1) // need retention and length
{
for (a = 0; a <= nages; a++)
{
temp = tempvec_a(a);
temp1 = tempvec_a(a) * retain_a(y, f, gg, a);
for (z = ALK_range_lo(a); z <= ALK_range_hi(a); z++)
{
Morphcomp_exp(j, 5 + GP4(g)) += temp1 * ALK(ALK_idx, g, a, z) * sel_l(y, f, gg, z) * retain(y, f, L1 - 1 + z); // note that A2 and L1 depend on what sex g is
}
}
}
else // need retention, but no length selex
{
for (a = 0; a <= nages; a++)
{
temp = tempvec_a(a);
temp1 = tempvec_a(a) * retain_a(y, f, gg, a);
for (z = ALK_range_lo(a); z <= ALK_range_hi(a); z++)
{
Morphcomp_exp(j, 5 + GP4(g)) += temp1 * ALK(ALK_idx, g, a, z) * retain(y, f, L1 - 1 + z);
}
}
}
}
if (g == gmorph)
{
k = 5 + Morphcomp_nmorph;
Morphcomp_exp(j)(6, k) /= sum(Morphcomp_exp(j)(6, k));
Morphcomp_exp(j)(6, k) += Morphcomp_mincomp;
Morphcomp_exp(j)(6, k) /= 1. + Morphcomp_mincomp * Morphcomp_nmorph;
}
}
} //close gmorph loop
exp_l_temp = colsum(exp_AL); // total size composition
agetemp = rowsum(exp_AL); // total age composition
#ifdef DO_ONCE
if (do_once == 1)
echoinput << "yr " << y << ", seas: " << s << ", fleet:" << f << endl
<< " sampled size " << exp_l_temp << endl
<< " sampled age " << agetemp << endl;
#endif
if (Do_Retain(f) > 0)
{
exp_l_temp_ret = colsum(exp_AL_ret);
exp_truea_ret = rowsum(exp_AL_ret);
#ifdef DO_ONCE
if (do_once == 1)
echoinput << " retained size " << exp_l_temp_ret << endl
<< " retained age " << exp_truea_ret << endl;
;
#endif
}
else
{
exp_truea_ret = agetemp; // covers cases where retention not used, but observations have partition=2
exp_l_temp_ret = exp_l_temp;
}
// end creation of selected A-L
}
if (sum(exp_l_temp) < 1.0e-8)
{
if (do_once == 1)
{
N_warn++;
warning << N_warn << " " << current_phase() << " " << niter << "warn in first call: Nil selected fish for year, seas, fleet " << y << " " << s << " " << f << "; SS may recover; suggest check initial parm. values for selectivity and growth" << endl;
}
exp_l_temp += 1.0e-09;
}
for (data_type = 1; data_type <= 9; data_type++)
{
switch (data_type)
{
case (1): // surveyindex
{
/* SS_Label_46.1 expected abundance index */
// NOTE that the Q scaler is factored in later on
j = have_data(ALK_time, f, data_type, 0); // number of observations for this time,f,type
if (j > 0)
{
j = have_data(ALK_time, f, data_type, 1); // for now, only one observations is allowed for surveys
switch (Svy_units(f))
{
case 1: // biomass
{
if (WTage_rd == 1) // using empirical wt-at-age; note that this cannot use GP specific bodyweights
// should sum over all g, but the g is already subsummed when agetemp is created by gender
{
vbio = 0.0;
if (Do_Retain(f) == 0) // all retained
{
for (a = 0; a <= nages; a++)
vbio += Wt_Age_t(y, f, 1, a) * agetemp(a);
if (gender == 2)
{
for (a = 0; a <= nages; a++)
vbio += Wt_Age_t(y, f, 2, a) * agetemp(a + nages + 1);
}
}
else
{
for (a = 0; a <= nages; a++)
vbio += Wt_Age_t(y, f, 1, a) * exp_truea_ret(a);
if (gender == 2)
{
for (a = 0; a <= nages; a++)
vbio += Wt_Age_t(y, f, 2, a) * exp_truea_ret(a + nages + 1);
}
}
}
else
{
if (Do_Retain(f) == 0)
{
vbio = exp_l_temp * wt_len2(s, 1); // biomass TEMPORARY CODE. Using gp=1 wt at length
}
else
{
vbio = exp_l_temp_ret * wt_len2(s, 1);
}
}
break;
}
case 0: // numbers
{
if (Do_Retain(f) == 0)
{
vbio = sum(exp_l_temp);
}
else
{
vbio = sum(exp_l_temp_ret);
}
break;
}
case 2: // F rate
{
if (fleet_type(f) < 4)
{
vbio = Hrate(f, t);
}
else
{
vbio = pred_M2(predator_rev(f), t);
}
break;
}
case 30: // spawning biomass #30
{
if (pop == 1 || fleet_area(f) == 0)
{
vbio = SSB_current;
}
else
{
vbio = sum(SSB_pop_gp(y, fleet_area(f)));
}
break;
}
case 31: // recruitment deviation #31
{
if (y >= recdev_start && y <= recdev_end)
{
vbio = mfexp(recdev(y));
}
else
{
vbio = 1.0;
}
break;
}
case 32: // recruitment without density-dependence (for pre-recruit survey) #32
{
if (y >= recdev_start && y <= recdev_end)
{
vbio = SSB_current * mfexp(recdev(y));
}
else
{
vbio = SSB_current;
}
break;
}
case 33: // recruitment #33
{
vbio = Recruits;
break;
}
case 34: // spawning biomass depletion
{
if (pop == 1 || fleet_area(f) == 0)
{
vbio = (SSB_current + 1.0e-06) / (SSB_virgin + 1.0e-06);
}
else
{
vbio = (sum(SSB_pop_gp(y, fleet_area(f))) + 1.0e-06) / (SSB_virgin + 1.0e-06);
}
break;
}
case 35: // parm deviation #35
{
k = Q_setup(f, 2); // specify which parameter's time-vary vector will be compared to this survey
if (y >= parm_dev_minyr(k) && y <= parm_dev_maxyr(k))
{
vbio = parm_dev(k, y);
// can the mean dev for years with surveys be calculated here?
}
else
{
vbio = 0.0;
}
break;
}
}
Svy_selec_abund(f, j) = value(vbio);
// get catchability
if (Q_setup(f, 1) == 2) // mirror Q from lower numbered survey
{
Svy_log_q(f, j) = Svy_log_q(Q_setup(f, 2), 1);
Q_parm(Q_setup_parms(f, 1)) = Svy_log_q(f, 1); // base Q So this sets parameter equal to the scaling coefficient and can then have a prior
}
else if (Q_setup(f, 1) == 4)
{
Svy_log_q(f, j) = Svy_log_q(Q_setup(f, 2), 1) + Q_parm(Q_setup_parms(f, 1) + 1);
Q_parm(Q_setup_parms(f, 1)) = Svy_log_q(f, 1); // base Q So this sets parameter equal to the scaling coefficient and can then have a prior
}
else // Q from parameter
{
if (Qparm_timevary(Q_setup_parms(f, 1)) == 0) // not time-varying
{
Svy_log_q(f, j) = Q_parm(Q_setup_parms(f, 1)); // set to base parameter value
}
else
{
Svy_log_q(f, j) = parm_timevary(Qparm_timevary(Q_setup_parms(f, 1)), Svy_yr(f, j));
}
}
// SS_Label_Info_25.1.3 #log or not
if (Svy_errtype(f) == -1) // normal
{
Svy_q(f) = Svy_log_q(f); // q already in arithmetic space
}
else // lognormal, or t-distribution in lognormal
{
Svy_q(f) = mfexp(Svy_log_q(f)); // get q in arithmetic space
}
// SS_Label_Info_46.1.1 #note order of operations, vbio raised to a power, then constant is added, then later multiplied by Q. Needs work
/*
// old code here
switch (Q_setup(f,1)) // switch is based on the link type: (1=simple q; 2=mirror; 3=q and power; 4=mirror with offset in q)
{
case 2:
{
// no break, so do same as case 1
}
case 4:
{
// no break, so do same as case 1
}
case 1:
{
if(Q_setup(f,5)==1 && y<=endyr) // float Q will be calculated and applied in objfun section, so temporarily store vbio in svy_est.
// note that if y is in forecast, then Q has already been calculated so can be applied in the else clause here
{
if(Svy_errtype(f)>=0) // lognormal or T-distribution
{Svy_est(f,j)=log(vbio+0.000001);}
else
{Svy_est(f,j)=vbio;}
}
else // Q based on parameters, or in forecast so Q has already been calculated through float approach in the objfun
{
if(Svy_errtype(f)>=0) // lognormal or T-distribution
{Svy_est(f,j)=log(vbio+0.000001)+Svy_log_q(f,j);}
else
{Svy_est(f,j)=vbio*Svy_q(f,j);}
}
break;
}
case 3: // link is power function
{
vbio=pow(vbio,1.0+Q_parm(Q_setup_parms(f,1)+1)); // raise vbio to a power
if(Svy_errtype(f)>=0) // lognormal or T-distribution
{Svy_est(f,j)=log(vbio+0.000001)+Svy_log_q(f,j);}
else
{Svy_est(f,j)=vbio*Svy_q(f,j);}
break;
}
}
*/
if (Q_setup(f, 1) == 3) // link is power function
{
vbio = pow(vbio, 1.0 + Q_parm(Q_setup_parms(f, 1) + 1));
} // raise vbio to a power
if (Svy_errtype(f) >= 0) // lognormal or T-distribution
{
Svy_est(f, j) = log(vbio + 0.000001);
}
else
{
Svy_est(f, j) = vbio;
}
if (Q_setup(f, 5) == 0 || y > endyr) // apply Q, but note: float Q will be calculated and applied in objfun section, so temporarily store vbio in svy_est.
// if y is in forecast, then Q has already been calculated so can be applied here
{
if (Svy_errtype(f) >= 0) // lognormal or T-distribution
{
Svy_est(f, j) += Svy_log_q(f, j);
}
else
{
Svy_est(f, j) *= Svy_q(f, j);
}
}
}
break;
} // end survey index
case (2): // DISCARD_OUTPUT
/* SS_Label_46.2 expected discard amount */
{
if (have_data(ALK_time, f, data_type, 0) > 0) // number of observations
{
j = have_data(ALK_time, f, data_type, 1); // only getting first observation for now
if (fleet_type(f) <= 2)
{
if (catch_ret_obs(f, t) > 0.0 || y > endyr)
{
if (disc_units(f) == 3) // numbers regardless of catchunits for retained catch
{
exp_disc(f, j) = catch_fleet(t, f, 4) - catch_fleet(t, f, 6);
}
else if (catchunits(f) == 1) // biomass units for retained and discarded catch
{
exp_disc(f, j) = catch_fleet(t, f, 1) - catch_fleet(t, f, 3); // discard in biomass
if (disc_units(f) == 2)
exp_disc(f, j) /= (catch_fleet(t, f, 1) + 0.0000001);
}
else // numbers for retained and discarded catch
{
exp_disc(f, j) = catch_fleet(t, f, 4) - catch_fleet(t, f, 6); // discard in numbers
if (disc_units(f) == 2)
exp_disc(f, j) /= (catch_fleet(t, f, 4) + 0.0000001);
}
if (exp_disc(f, j) < 0.0)
{
N_warn++;
warning << N_warn << " " << f << " " << j << " " << exp_disc(f, j) << " catches " << catch_fleet(t, f) << endl;
}
}
else
{
exp_disc(f, j) = -1.;
}
}
else if (fleet_type(f) == 4) // predator consumption stored in discard
{
if (disc_units(f) == 3) // numbers
{
exp_disc(f, j) = catch_fleet(t, f, 4); // consumption in numbers
}
else
{
exp_disc(f, j) = catch_fleet(t, f, 1); // consumption in biomass
}
}
else
{
exp_disc(f, j) = -1.;
}
}
break;
} // end discard
case (3): // mean body weight
/* SS_Label_46.3 expected mean body weight */
{
if (have_data(ALK_time, f, data_type, 0) > 0) // number of observations
{
for (int reps = 1; reps <= have_data(ALK_time, f, data_type, 0); reps++)
{
j = have_data(ALK_time, f, data_type, reps); // observation number in overall list
z = mnwtdata(5, j); // type 1=length, 2=weight
int parti = mnwtdata(4, j); // parrtition: 0=all, 1=discard, 2=retained
switch (parti)
{
case 0:
{
if (z == 2)
{
exp_mnwt(j) = (exp_l_temp * wt_len2(s, 1)) / sum(exp_l_temp);
} // total sample
else
{
exp_mnwt(j) = (exp_l_temp * len_bins_m2) / sum(exp_l_temp);
}
break;
}
case 1:
{
if (z == 2)
exp_mnwt(j) = (exp_l_temp - exp_l_temp_ret) * wt_len2(s, 1) / (sum(exp_l_temp) - sum(exp_l_temp_ret)); // discard sample
else
{
exp_mnwt(j) = (exp_l_temp - exp_l_temp_ret) * len_bins_m2 / (sum(exp_l_temp) - sum(exp_l_temp_ret));
}
break;
}
case 2:
{
if (z == 2)
exp_mnwt(j) = (exp_l_temp_ret * wt_len2(s, 1)) / sum(exp_l_temp_ret); // retained only
else
{
exp_mnwt(j) = (exp_l_temp_ret * len_bins_m2) / sum(exp_l_temp_ret);
}
break;
}
}
}
}
break;
}
case (4): // length composition
/* SS_Label_46.4 length composition */
{
if (have_data(ALK_time, f, data_type, 0) > 0)
{
for (j = 1; j <= have_data(ALK_time, f, data_type, 0); j++) // loop all obs of this type
{
i = have_data(ALK_time, f, data_type, j);
if (LenBin_option > 1)
{
if (mkt_l(f, i) == 0)
{
exp_l(f, i) = make_len_bin * exp_l_temp;
} // expected size comp MAtrix * vector = vector
else if (mkt_l(f, i) == 1)
{
exp_l(f, i) = make_len_bin * (exp_l_temp - exp_l_temp_ret);
} // discard sample
else
{
exp_l(f, i) = make_len_bin * exp_l_temp_ret;
} // retained only
}
else // using data_bins same as pop_bins
{
if (mkt_l(f, i) == 0)
{
exp_l(f, i) = exp_l_temp;
} // expected size comp MAtrix * vector = vector
else if (mkt_l(f, i) == 1)
{
exp_l(f, i) = (exp_l_temp - exp_l_temp_ret);
} // discard sample
else
{
exp_l(f, i) = exp_l_temp_ret;
} // retained only
}
if (docheckup == 1)
echoinput << " len obs " << mkt_l(f, i) << " " << tails_l(f, i) << endl
<< obs_l(f, i) << endl
<< exp_l(f, i) << endl;
// code for tail compression, etc in the likelihood section to allow for superyear combinations // mkt=0 Do nothing
} // end lengthcomp loop
}
break;
} // end length composition
case (5): // age composition
/* SS_Label_46.5 age composition */
{
if (have_data(ALK_time, f, data_type, 0) > 0)
{
for (j = 1; j <= have_data(ALK_time, f, data_type, 0); j++) // loop all obs of this type
{
i = have_data(ALK_time, f, data_type, j);
k = ageerr_type_a(f, i); // age-err type
if (use_Lbin_filter(f, i) == 0)
{ // sum across all length bins
if (mkt_a(f, i) == 0)
age_exp = agetemp;
if (mkt_a(f, i) == 1)
age_exp = agetemp - exp_truea_ret; // discard sample
if (mkt_a(f, i) == 2)
age_exp = exp_truea_ret; // retained only
}
else
{ // only use ages from specified range of size bins
// Lbin_filter is a vector with 0 for unselected size bins and 1 for selected bins
if (mkt_a(f, i) == 0)
age_exp = exp_AL * Lbin_filter(f, i);
if (mkt_a(f, i) == 1)
age_exp = (exp_AL - exp_AL_ret) * Lbin_filter(f, i); // discard sample
if (mkt_a(f, i) == 2)
age_exp = exp_AL_ret * Lbin_filter(f, i); // retained only
}
exp_a(f, i) = age_age(k) * age_exp;
if (docheckup == 1)
echoinput << "Lbin " << Lbin_filter(f, i) << endl
<< " obs " << obs_a(f, i) << endl
<< "expected " << age_exp << endl
<< "exp with ageerr " << exp_a(f, i) << endl;
// add code here to store exp_a_true(f,i)=age_exp
// then in data generation the sample can be from true age before ageing error is applied
// if(docheckup==1) echoinput<<" real age "<<age_exp<<endl<<"Lbin "<<Lbin_filter(f,i)<<endl<<" obs "<<obs_a(f,i)<<endl<<" exp with ageerr "<<exp_a(f,i)<<endl;
} // end agecomp loop within fleet/time
}
break;
} // end age composition
case (6): // weight composition (generalized size composition)
/* SS_Label_46.6 weight composition (generalized size composition) */
{
if (SzFreq_Nmeth > 0) // have some sizefreq data
{
if (have_data(ALK_time, f, data_type, 0) > 0)
{
for (j = 1; j <= have_data(ALK_time, f, data_type, 0); j++) // loop all obs of this type
{
iobs = have_data(ALK_time, f, data_type, j); // observation index
SzFreqMethod = SzFreq_obs_hdr(iobs, 6);
SzFreqMethod_seas = nseas * (SzFreqMethod - 1) + s; // index that combines sizefreqmethod and season and used in SzFreqTrans
if (SzFreq_obs_hdr(iobs, 9) > 0) // first occurrence of this method at this time is with fleet = f
{
if (do_once == 1 || (MG_active(3) > 0 && (timevary_MG(y, 3) > 0))) // calc matrix because wtlen parameters have changed
{
for (gg = 1; gg <= gender; gg++)
{
if (gg == 1)
{
z1 = 1;
z2 = nlength;
ibin = 0;
ibinsave = 0;
} // female
else
{
z1 = nlength1;
z2 = nlength2;
ibin = 0;
ibinsave = SzFreq_Nbins(SzFreqMethod);
} // male
topbin = 0.;
botbin = 0.;
// NOTE: wt_len_low is calculated separately for each growth pattern (GPat)
// but the code below still just uses GPat=1 for calculation of the sizefreq transition matrix
switch (SzFreq_units(SzFreqMethod)) // biomass vs. numbers are accumulated in the bins
{
case (1): // units are biomass, so accumulate body weight into the bins; Assume that bin demarcations are also in biomass
{
if (SzFreq_Omit_Small(SzFreqMethod) == 1)
{
while (wt_len_low(s, 1, z1 + 1) < SzFreq_bins(SzFreqMethod, 1) && z1 < z2)
{
z1++;
}
} // ignore tiny fish
if (z1 + 1 >= z2)
{
N_warn++;
cout << " EXIT - see warning " << endl;
warning << N_warn << " "
<< " error: max population size " << wt_len_low(s, 1, z1) << " is less than first data bin " << SzFreq_bins(SzFreqMethod, 1) << " for SzFreqMethod " << SzFreqMethod << endl;
exit(1);
}
if (wt_len_low(s, 1, nlength2) < SzFreq_bins(SzFreqMethod, SzFreq_Nbins(SzFreqMethod)))
{
N_warn++;
cout << " EXIT - see warning " << endl;
warning << N_warn << " "
<< " error: max population size " << wt_len_low(s, 1, nlength2) << " is less than max data bin " << SzFreq_bins(SzFreqMethod, SzFreq_Nbins(SzFreqMethod)) << " for SzFreqMethod " << SzFreqMethod << endl;
exit(1);
}
for (z = z1; z <= z2; z++)
{
if (ibin == SzFreq_Nbins(SzFreqMethod))
{
SzFreqTrans(SzFreqMethod_seas, z, ibinsave) = wt_len2(s, 1, z);
}
else
{
if (wt_len_low(s, 1, z) >= topbin)
{
ibin++;
ibinsave++;
}
if (ibin > 1)
{
botbin = SzFreq_bins2(SzFreqMethod, ibin);
}
if (ibin == SzFreq_Nbins(SzFreqMethod))
{
SzFreqTrans(SzFreqMethod_seas, z, ibinsave) = wt_len2(s, 1, z);
topbin = 99999.;
}
else
{
topbin = SzFreq_bins2(SzFreqMethod, ibin + 1);
if (wt_len_low(s, 1, z) >= botbin && wt_len_low(s, 1, z + 1) <= topbin)
{
SzFreqTrans(SzFreqMethod_seas, z, ibinsave) = wt_len2(s, 1, z);
}
else
{
temp = (wt_len_low(s, 1, z + 1) - topbin) / wt_len_fd(s, 1, z); // frac in pop bin above (data bin +1)
temp1 = wt_len_low(s, 1, z) + (1. - temp * 0.5) * wt_len_fd(s, 1, z); // approx body wt for these fish
temp2 = wt_len_low(s, 1, z) + (1. - temp) * 0.5 * wt_len_fd(s, 1, z); // approx body wt for fish below
SzFreqTrans(SzFreqMethod_seas, z, ibinsave + 1) = temp * temp1;
SzFreqTrans(SzFreqMethod_seas, z, ibinsave) = (1. - temp) * temp2;
}
}
}
}
if (SzFreq_scale(SzFreqMethod) == 2 && gg == gender) // convert to pounds
{
SzFreqTrans(SzFreqMethod_seas) /= 0.4536;
}
break;
} // end of units in biomass
// NOTE: even though the transition matrix is currently in units of biomass distribution, there is no need to
// normalize to sum to 1.0 here because the normalization will occur after it gets used to create SzFreq_exp
case (2): // units are numbers
{
if (SzFreq_scale(SzFreqMethod) <= 2) // bin demarcations are in weight units (1=kg, 2=lbs), so uses wt_len to compare to bins
{
if (SzFreq_Omit_Small(SzFreqMethod) == 1)
{
while (wt_len_low(s, 1, z1 + 1) < SzFreq_bins(SzFreqMethod, 1) && z1 < z2)
{
z1++;
}
} // ignore tiny fish
if (z1 + 1 >= z2)
{
N_warn++;
cout << " EXIT - see warning " << endl;
warning << N_warn << " "
<< " error: max population size " << wt_len_low(s, 1, z1) << " is less than first data bin " << SzFreq_bins(SzFreqMethod, 1) << " for SzFreqMethod " << SzFreqMethod << endl;
exit(1);
}
if (wt_len_low(s, 1, nlength2) < SzFreq_bins(SzFreqMethod, SzFreq_Nbins(SzFreqMethod)))
{
N_warn++;
cout << " EXIT - see warning " << endl;
warning << N_warn << " "
<< " error: max population size " << wt_len_low(s, 1, nlength2) << " is less than max data bin " << SzFreq_bins(SzFreqMethod, SzFreq_Nbins(SzFreqMethod)) << " for SzFreqMethod " << SzFreqMethod << endl;
exit(1);
}
for (z = z1; z <= z2; z++)
{
if (ibin == SzFreq_Nbins(SzFreqMethod))
{
SzFreqTrans(SzFreqMethod_seas, z, ibinsave) = 1.;
} //checkup<<" got to last ibin, so put rest of popbins here"<<endl;
else
{
if (wt_len_low(s, 1, z) >= topbin)
{
ibin++;
ibinsave++;
} //checkup<<" incr ibin "<<z<<" "<<ibin<<" "<<len_bins(z)<<" "<<len_bins_dat(ibin);
if (ibin > 1)
{
botbin = SzFreq_bins2(SzFreqMethod, ibin);
}
if (ibin == SzFreq_Nbins(SzFreqMethod)) // checkup<<" got to last ibin, so put rest of popbins here"<<endl;
{
SzFreqTrans(SzFreqMethod_seas, z, ibinsave) = 1.;
topbin = 99999.;
}
else
{
topbin = SzFreq_bins2(SzFreqMethod, ibin + 1);
if (wt_len_low(s, 1, z) >= botbin && wt_len_low(s, 1, z + 1) <= topbin) //checkup<<" pop inside dat, put here"<<endl;
{
SzFreqTrans(SzFreqMethod_seas, z, ibinsave) = 1.;
}
else // checkup<<" overlap"<<endl;
{
SzFreqTrans(SzFreqMethod_seas, z, ibinsave + 1) = (wt_len_low(s, 1, z + 1) - topbin) / wt_len_fd(s, 1, z);
SzFreqTrans(SzFreqMethod_seas, z, ibinsave) = 1. - SzFreqTrans(SzFreqMethod_seas, z, ibinsave + 1);
}
}
}
}
}
else // bin demarcations are in length unit (3=cm, 4=inch) so uses population len_bins to compare to data bins
{
if (SzFreq_Omit_Small(SzFreqMethod) == 1)
{
while (len_bins2(z1 + 1) < SzFreq_bins(SzFreqMethod, 1))
{
z1++;
}
// echoinput<<"accumulate starting at bin: "<<z1<<endl;
} // ignore tiny fish
if (len_bins2(nlength2) < SzFreq_bins(SzFreqMethod, SzFreq_Nbins(SzFreqMethod)))
{
N_warn++;
cout << " EXIT - see warning " << endl;
warning << N_warn << " "
<< " error: max population len bin " << len_bins2(nlength2) << " is less than max data bin " << SzFreq_bins(SzFreqMethod, SzFreq_Nbins(SzFreqMethod)) << " for SzFreqMethod " << SzFreqMethod << endl;
exit(1);
}
for (z = z1; z <= z2; z++)
{
if (ibin == SzFreq_Nbins(SzFreqMethod))
{
SzFreqTrans(SzFreqMethod_seas, z, ibinsave) = 1.;
} //checkup<<" got to last ibin, so put rest of popbins here"<<endl;
else
{
if (len_bins2(z) >= topbin)
{
ibin++;
ibinsave++;
} //checkup<<" incr ibin "<<z<<" "<<ibin<<" "<<len_bins(z)<<" "<<len_bins_dat(ibin);
if (ibin > 1)
{
botbin = SzFreq_bins2(SzFreqMethod, ibin);
}
if (ibin == SzFreq_Nbins(SzFreqMethod)) // checkup<<" got to last ibin, so put rest of popbins here"<<endl;
{
SzFreqTrans(SzFreqMethod_seas, z, ibinsave) = 1.;
topbin = 99999.;
}
else
{
topbin = SzFreq_bins2(SzFreqMethod, ibin + 1);
if (len_bins2(z) >= botbin && len_bins2(z + 1) <= topbin) //checkup<<" pop inside dat, put here"<<endl;
{
SzFreqTrans(SzFreqMethod_seas, z, ibinsave) = 1.;
}
else // checkup<<" overlap"<<endl;
{
SzFreqTrans(SzFreqMethod_seas, z, ibinsave + 1) = (len_bins2(z + 1) - topbin) / (len_bins2(z + 1) - len_bins2(z));
SzFreqTrans(SzFreqMethod_seas, z, ibinsave) = 1. - SzFreqTrans(SzFreqMethod_seas, z, ibinsave + 1);
}
}
}
}
}
break;
} // end of units in numbers
}
if (docheckup == 1 && gg == gender)
echoinput << " sizefreq trans_matrix: method/season " << SzFreqMethod << " / " << s << endl
<< trans(SzFreqTrans(SzFreqMethod_seas)) << endl;
} // end gender loop
} // end needing to calc the matrix because it may have changed
} // done calculating the SzFreqTransition matrix for this method
switch (SzFreq_obs_hdr(iobs, 5)) // discard/retained partition
{
case (0):
{
SzFreq_exp(iobs) = trans(SzFreqTrans(SzFreqMethod_seas)) * exp_l_temp;
break;
}
case (1):
{
SzFreq_exp(iobs) = trans(SzFreqTrans(SzFreqMethod_seas)) * (exp_l_temp - exp_l_temp_ret);
break;
}
case (2):
{
SzFreq_exp(iobs) = trans(SzFreqTrans(SzFreqMethod_seas)) * exp_l_temp_ret;
break;
}
}
#ifdef DO_ONCE
if (do_once == 1)
echoinput << y << " " << f << " szfreq_exp_initial " << SzFreq_exp(iobs) << endl;
#endif
if (gender == 2)
{
k = SzFreq_obs_hdr(iobs, 8); // max bins for this method
switch (SzFreq_obs_hdr(iobs, 4)) // combine, select or each gender
{
case (0): // combine genders
{
for (ibin = 1; ibin <= k; ibin++)
SzFreq_exp(iobs, ibin) += SzFreq_exp(iobs, k + ibin);
SzFreq_exp(iobs)(k + 1, 2 * k) = 0.0;
SzFreq_exp(iobs)(1, k) /= sum(SzFreq_exp(iobs)(1, k));
if (SzFreq_mincomp(SzFreqMethod) > 0.0)
{
SzFreq_exp(iobs)(1, k) += SzFreq_mincomp(SzFreqMethod);
SzFreq_exp(iobs)(1, k) /= sum(SzFreq_exp(iobs)(1, k));
}
break;
}
case (1): // female only
{
SzFreq_exp(iobs)(k + 1, 2 * k) = 0.0; // zero out the males so will not interfere with data generation
SzFreq_exp(iobs)(1, k) /= sum(SzFreq_exp(iobs)(1, k));
if (SzFreq_mincomp(SzFreqMethod) > 0.0)