forked from wintermind/bestpred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bestpred.f90
3449 lines (3326 loc) · 165 KB
/
bestpred.f90
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
!e!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! NAME: bestpred.f90
! VERSION: 2.0 beta
! RELEASED: 01 AUGUST 2007
! AUTHORS: Paul M. VanRaden (paul@aipl.arsusda.gov)
! John B. Cole (john.cole@ars.usda.gov)
! DESCRIPTION: Reads program parameters from the file bestpred.par.
! This program is part of the bestpred package from AIPL.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! aiplDCR: Fortran / written by Paul VanRaden 11-99
! Animal Improvement Programs Lab, USDA
! paul@aipl.arsusda.gov
!
! Computes: 1) Data Collection Rating from test day data.
! 2) yields (projected 305, partial, 305, 365)
! 3) milk, fat, protein, and somatic cell score
! 4) persistency and reliability of persistency
! 5) factors to adjust for 3X milking
!
! Methods: Multi-trait or single-trait best prediction using
! a correlation matrix among test day yields.
!
! References: 1996 J. Dairy Science 79(Suppl. 1):143 (abstr.)
! 1997 J. Dairy Science 80:3015
! 1998 Proc. 6th World Conference on Genetics
! Applied to Livestock Production 23: 347
! .....................................................................
! Definitions (ST=single-trait,MT=multi-trait,m=milk,f=fat,p=protein)
! Control variables
! mtrait = 1 does only ST to save CPU time
! = 3 does MT m,f,p; 4 does MT m,f,p,scs
! mature = 1 adjusts for age etc., 0 doesn't,
! -1 is for pre-adjusted deviations
! and inputs f,p lbs instead of %
! use3X = 0 doesn't adjust for 3X milking,
! 1 uses old factors, 2 new factors,
! 3 uses phased-in factors over time
! maxprnt = number of graphs to display, use
! -1 to display the input variables
subroutine bestpred &
! Control
(mtrait,use3X,maxprnt &
! Input
,tstdays,dim,super,Xmilk,weigh,sample,MRD, yield &
,herdavg,agefac,cowid,fresh,parity,length &
! Output
,DCRm,DCRc,DCRs,YLDvec,PERSvec,RELyld,RELpers &
! Optional
,Yvec,bump,BLUPout,BLUPn &
! Added by JBC
,GRAFplot,DEBUGmsgs,ONscreen &
,laclen,dailyfreq, INTmethod, maxlen &
,DAILYbp,DAILYherd,WRITEcurve,CURVEfile &
,WRITEdata,DATAfile,plotfreq,INTmethodSCS &
,READparms,UNITSin, UNITSout, breedUNK, dim0 &
,dim0flag, LOGon, LOGfile, LOGfreq, maxshow &
,herdstate, CURVEsmall, CURVEsingle &
,region, season &
)
! .....................................................................
! Input
! tstdays is number of test days
! dim contains days in milk at each test day
! next: supervision codes, milking and sampling frequencies
! MRD is number of milk recorded days averaged together (LER)
! yield contains m,f,p, and scs data for each test day
! herdavg 305-d herd means for m,f,p,scs adjusted for age, not 3X;
!!! called herd305 in bestpref_main and bestpred_fmt.
! agefac has adjustment factors for m,f,p,scs
! cowid is 17-byte American ID of cow
! fresh is 8-byte YYYYMMDD fresh date
! parity is lactation number (assumed / if parity = 0)
! length is days from fresh date to termination date
! Output
! DCRm = MT m DCR
! DCRc = MT f,p DCR
! DCRs = MT scs DCR
! YLDvec = ME : 305 m,f,p,scs, 365 m,f,p,scs, maxlen m,f,p,scs, laclen m,f,p,scs
! Actual: 305 m,f,p,scs, 365 m,f,p,scs, maxlen m,f,p,scs, laclen m,f,p,scs
! PERSvec= MT m,f,p,scs persistency, ST m,f,p,scs persistency
! RELyld = MT m,f,p,scs Reliability, ST m,f,p,scs Reliability
! RELpers= MT m,f,p,scs Rel(persist),ST m,f,p,scs Rel(persist)
! ST values are substituted if no MT values
! Optional
! GRAFplot indicates which MFPS plots should be drawn
! DEBUGmsgs is used to display debugging messages (0/1)
! ONscreen toggles output to the screen on (1) and off (0)
! laclen specifies default lactation length that is used to
! figure out what actuals to return.
! dailyfreq specifies how often actual dailies are calculated
! INTmethod specifies the inerpolation method to be used for
! calculating lactation curves.
! maxlen specifies the longest possible lactation length
! to be used in calculating lactation curves.
! READparms is a flag passed downstream from maindcr to fmt4dcr
! and then to aipldcr that indicates which parms should be
! used by that particular subroutine -- those passed in from
! maindcr (0) or those stored in the file maindcr.par (1).
! maxshow specified how many plots should be drawn on the screen.
! ....................................................................
! OTHER OPTIONS
! integer,save :: mbreed
integer,parameter :: last=2, part=1
integer,parameter :: nbump=0
integer,save :: nbmp
integer,save :: lacn
integer,save :: month
integer,save :: ncall=0
integer,save :: precise=1
integer :: READparms, maxshow
real,save :: maxbump
real, dimension(:,:,:), allocatable, save :: dyield
real*8,save :: lplus
real*8, dimension(:,:,:), allocatable, save :: meanyld
real*8, dimension(:,:,:,:,:), allocatable, save :: covari
real*8,save :: corr305(4,4)
real*8, dimension(:,:,:,:,:), allocatable, save :: covd
real*8,save :: meanp(2,4)
real*8,save :: varp(2,4)
real*8,save :: lacdif
real*8,save :: sddif
real*8,save :: stdvar(4,last)
real*8, dimension(:,:,:), allocatable, save :: sd
real*8,save :: summ ! I renamed this because it was colliding with the
! Fortran 90 array function SUM().
real*8,save :: dV1(4,4)
real*8,save :: dVd(4,4)
!real*8,save :: d0(4)
real*8 :: d0(2,4)
real*8,save :: vijkl
real*8,save :: qpq=0.d0
real*8,save :: tdhi
real*8,save :: tdlo
real*8,save :: zero
integer :: dim0(8), dim0flag
real :: dim0wgts(2) = (/ 0.285, 0.715 /) ! Should sum to 1.
! DAILYbp and DAILYherd will contain daily BP of yield for cows
! and herds, respectively, unless dailyfreq=0. If dailyfreq is
! 0 then they will contain only zeroes.
real*8 :: DAILYbp(2,4,maxlen)
real*8 :: DAILYherd(4,maxlen)
!character, parameter :: mybreed='H'
character :: INTmethod, INTmethodSCS, UNITSin, UNITSout
character*2 :: herdstate
integer :: region, season
integer, save :: oldregion, oldseason
integer, parameter :: maxy=200, maxtd=50
!
! mybreed= breed letter for stats display
! last = last lactation group (2 is 2nd&later)
! nbump reduces REL of worst 1/10**nbump lacts
! nbump ranges from 1-5 (see Fval table)
! = 0 will not reduce REL of bad data
! maxtd is maximum test days in a lactation
! maxy is maximum number of milk, fat, and
! protein obs in any lact (<= 4*maxtd)
integer mtrait,use3X,maxprnt, &
dim(maxtd),Xmilk(maxtd),weigh(maxtd),sample(maxtd), &
MRD(maxtd),super(maxtd),tstdays,length, &
parity,BLUPn(10), DEBUGmsgs &
,ONscreen, laclen, maxlen
character(100) BLUPout(10)
real*8 DCRm,DCRc,DCRs,YLDvec(2,16),PERSvec(4),RELyld(4),RELpers(4), &
var(maxy,maxy),ymean,yield(2,4,maxtd), &
vary,covary,vari(4,4),dev(maxy,1), &
multi(4,1), &
herdavg(2,4), &
herd305(2,4),hratio(4),herd365(2,4),herd999(2,4),herdpart(2,4), &
sum305(4),sum365(4),sum999(4),sumpart(4), &
dvari(4,4),dcov(4,maxy),dcovp(maxy,4), &
persist(4,1),pers(4,1), &
partial(4,maxy),partrec(4,1), &
xsingl(4),xmulti(4),covsum(4,maxy), &
agefac(4),varfac(4),test3X(4,maxtd),fact3X(4),part3X(4), &
Yvec(2,4),DCRvec(4),lacwt(4),milk3X, &
qCVC1(4,4),Rvar(2,2),Rvec(12), &
Rvecs,Rvecm,Rvecf,Rvecp,Rcorr, &
Zdev(maxtd),Zdif,regrel(4), &
tdregr,bump(4),bumpsd(4),bigbump(4),varb(4)
real*8, dimension(:,:), allocatable :: curve, curvem, graph1
real*8, dimension(:,:,:), allocatable :: grafall
real*8, dimension(:,:,:), allocatable :: curves
real*8, dimension(:), allocatable :: freq3X
real*8, dimension(:,:,:), allocatable :: std, tempSTD!, tempDEV
real*8, dimension(:,:), allocatable :: stdslice, tempSTDslice, tempTD
! These matrices are used to catch covariance between TD and a given DIM.
! '305' is used for 305 d lactations, '365' for 365 d lactations, and
! '999' for user-defined lactation lengths. The 'part's are used for
! calculating actual yields based on DIM of most recent test for RIP.
real*8 cov305(4,maxy),covp305(maxy,4), &
covar305(4,maxy),vari305(4,4),multi305(4,1), &
cov365(4,maxy),covp365(maxy,4), &
covar365(4,maxy),vari365(4,4),multi365(4,1), &
cov999(4,maxy),covp999(maxy,4), &
covar999(4,maxy),vari999(4,4), &
multi999(4,1),single305(4,1),single365(4,1), &
single999(4,1)
integer dailyfreq, st_start, plotfreq, breedUNK
character*8 :: BESTPREDversion = '2.0rc7'
character*10 :: BESTPREDdate = '11/26/2014'
character*64 :: BESTPREDname = 'John B. Cole'
character*64 :: BESTPREDemail = 'john.cole@ars.usda.gov'
character*64 :: BESTPREDwebsite = 'http://www.aipl.arusda.gov/software/bestpred/'
!
real, save :: zero0=0.d0, lb=2.205
!
! Daily yields of milk, fat, protein, SCS
! tested monthly (first lactation curve)
real, save :: dyld(12,4,last)=reshape((/ &
53.3,63.,66.3,65.5,63.9,62.,60.1,58.1,55.6,52.3,49.,47. &
,2.3,2.32,2.31,2.28,2.25,2.22,2.19,2.16,2.11,2.03,1.95,1.9 &
,1.81,1.9,1.99,2.01,2.01,1.99,1.96,1.92,1.87,1.8,1.74,1.67 &
,3.38,2.63,2.41,2.44,2.48,2.52,2.57,2.6,2.66,2.74,2.82,2.9 &
! later lactation curve
,75.1,86.,86.9,82.2,77.2,72.2,67.2,62.1,56.2,49.8,46.,42. &
,3.33,3.2,3.0,2.85,2.71,2.58,2.45,2.29,2.12,1.92,1.81,1.7 &
,2.6,2.55,2.55,2.49,2.4,2.3,2.17,2.04,1.89,1.72,1.62,1.55 &
,3.35,2.84,2.8,2.9,3.02,3.15,3.28,3.43,3.58,3.76,3.88,4.0/) &
,(/12,4,last/)) &
! Daily s.d. of milk, fat, protein, SCS
,dsd(12,4,last)=reshape((/ &
10.7,10.6,9.9,9.6,9.5,9.5,9.5,9.6,9.8,10.3,10.7,11.2 &
,.51,.45,.41,.39,.38,.38,.37,.37,.37,.39,.40,.41 &
,.34,.31,.28,.28,.28,.29,.29,.29,.30,.32,.33,.34 &
,1.51,1.45,1.42,1.41,1.4,1.41,1.41,1.41,1.41,1.42,1.42,1.42 &
! later lactation s.d.
,15.,15.3,14.4,13.8,13.3,13.,12.9,13.,13.6,14.7,15.,15.5 &
,.75,.69,.62,.58,.55,.52,.51,.51,.53,.57,.58,.59 &
,.50,.43,.40,.38,.38,.38,.38,.39,.42,.46,.47,.49 &
,1.63,1.69,1.71,1.69,1.66,1.61,1.56,1.51,1.47,1.45,1.44,1.4/) &
,(/12,4,last/))
! Mature breed means for 1995
character breed6*6
character, save :: breed(6)=(/'A', 'B', 'G', 'H', 'J', 'M'/)
real, save :: brdyld(6,4,2)=reshape((/ &
15080., 16616., 13864., 20845., 14120., 14472. &
, 589., 668., 625., 763., 662., 518. &
, 505., 586., 481., 656., 531., 476. &
, 3.16, 3.22, 3.35, 3.20, 3.31, 2.87 &
,18532., 21577., 16850., 25658., 18161., 17475. & ! and for 2000
, 713., 867., 745., 935., 833., 624. &
, 582., 714., 551., 771., 644., 544. &
, 2.95, 2.93, 3.29, 3.08, 3.32, 3.06/),(/6,4,2/))
real, save :: brdsd(6,4)=reshape((/ &
2300., 2530., 2324., 2946., 2128., 2332. &
, 115., 115., 112., 119., 109., 112. &
, 92., 92., 92., 97., 89., 92. &
, 1.28, 1.21, 1.35, 1.34, 1.18, 1.30/),(/6,4/))
! Squared correlations of monthly, daily testing
real, save :: rmonth(4,last)=reshape((/ &
.962,.960,.962,.943, &
.958,.956,.958,.960/),(/4,last/))
! F values for test day bumpiness
real Fvalue
real, save :: Fval(5,10)=reshape((/ &
! F1 F01 F001 F0001 F00001
2.70555, 6.63495, 10.8277, 15.1369, 19.5118, &
2.30260, 4.60521, 6.9079, 9.2105, 11.5132, &
2.08381, 3.78166, 5.4222, 7.0360, 8.6341, &
1.94487, 3.31921, 4.6168, 5.8783, 7.1185, &
1.84728, 3.01729, 4.1031, 5.1491, 6.1714, &
1.77412, 2.80202, 3.7430, 4.6428, 5.5180, &
1.71673, 2.63937, 3.4746, 4.2683, 5.0371, &
1.67021, 2.51131, 3.2656, 3.9786, 4.6666, &
1.63153, 2.40737, 3.0975, 3.7468, 4.3713, &
1.59873, 2.32096, 2.9589, 3.5565, 4.1298/),(/5,10/))
! Expansion amount: full=1 or reduced=.6
! real, save :: expamt=1.0
real, save :: expamt=0.6 &
! Repeatabilities and correlations of
! random effects in yield and persistency
,repty(4)=(/.55, .55, .55, .30/) &
,reptp(4)=(/.44, .44, .44, .20/) &
,reptyp(4)=(/.00, .00, .00, .00/)
! If no repeated records, substitute
! heritabilities and genetic correlation
! ,repty(4)=(/.25, .25, .25, .10/) &
! ,reptp(4)=(/.10, .10, .10, .05/) &
! ,reptyp(4)=(/.00, .00, .00, .00/)
! DIM midpoints for persistency
! If value is 0, DIM will be chosen
! to make yield and pers uncorrelated
! Now passed in from parm file (JBC, 11/14/2007)
! integer, save :: dim0(4)=(/161,159,166,155/)
! integer, save :: dim0(4)=(/ 0, 0, 0, 0/)
! LSCS is sum / 305, * 100
real, save :: X305(4)=(/1., 1., 1., 305./)
real, save :: X100(4)=(/1., 1., 1., 100./)
integer q(6),qq(6,maxy),size,i,j,k,l,m,n,i6,brd &
,ntests(4),maxyld,minyld,middle(maxtd) &
,dimmilk(maxtd),dimsort(maxtd) &
,mstart,mstop,kk,MRDi &
,testday,yearfr
integer, save :: oldbrd
integer usetd
integer :: maxyield(4),minyield(4)
integer, dimension(:), allocatable :: dimvec
integer :: LOGon, LOGfreq
character cowid*17,fresh*8,brd1*1
character, save :: plus*1='+'
character(4) percent
character(4), dimension(:), allocatable :: fatpct, propct, scs
character plot(40),plt !,tests(maxlen/plotfreq)
character, dimension(:), allocatable :: tests
! tests(maxlen/6)
character, save :: capital*13='LUSBACOQPRVXD',small*13='lusbacoqprvxd'
character(2) MTorST(4)
character(7), save :: trt(-3:8)=(/'devMilk','dev.Fat','devProt','dev.SCS' &
, ' Milk',' Fat',' Prot',' SCS' &
, 'ME.Milk','ME..Fat','ME.Prot','ME..SCS'/)
!
integer :: GRAFplot(4), GRAFplotMIXED, GRAFplotSUM
character(4), save :: GRAFname(4)=(/'Milk','Fat ','Prot','SCS '/)
character(1), save :: SHORTtrait(4) = (/'M','F','P','S'/)
character(2), save :: STorMTlabel(2) = (/'ST','MT'/)
integer :: WRITEcurve, WRITEdata, CURVEsmall
character(len=*) :: CURVEfile, DATAfile, LOGfile
integer :: YIELDunits(4) = (/5,1,1,1/)
integer :: YIELDsteps(4) = (/1,1,1,1/)
character*80 :: LogMessage
character*4 :: int2str
integer :: overlaps, ntd, ndup, CURVEsingle
external vary, covary
equivalence (breed,breed6)
! .................................................. STARTUP MESSAGE
if ( ONscreen == 1 .and. ncall == 0 ) then
print *,'--------------------------------------------------------------------------------'
print *,'BESTPRED version ', trim(BESTPREDversion)
print *,' Released: ', trim(BESTPREDdate)
print *,' Support : ', trim(BESTPREDname), ' (',trim(BESTPREDemail),')'
print *,' Website : ', trim(BESTPREDwebsite)
print *,'--------------------------------------------------------------------------------'
end if
! Log program start-up
! You should assign the message to LogMessage rather than passing it as a string literal
! in the subroutine call. If you pass the string without assignment it will be papped to
! 80 characters using NULLs, which will be written to the log file.
!if ( LOGon == 1 ) then
! LogMessage = 'Starting BESTPRED '//trim(BESTPREDversion)
! call log_message(LOGfile,LogMessage)
!end if
! ---------------------------------------------- Allocate the arrays declared
! as allocatable above.
! print *, "tstdays on bestpred() entry: ", tstdays
if ( DEBUGmsgs > 1 ) print *,'Allocating memory'
if ( ncall == 0 ) then
if ( LOGon == 1 .and. LOGfreq > 0 ) then
LogMessage = 'Allocating matrices for means, SD, and covariances'
call log_message(LOGfile,LogMessage)
end if
allocate(dyield(maxlen,4,last))
allocate(meanyld(4,maxlen,last))
allocate(sd(4,maxlen,last))
allocate(covari(4,4,maxlen,maxlen,last))
allocate(covd(4,4,maxlen,maxlen,last))
end if
if ( LOGon == 1 .and. LOGfreq > 0 .and. mod(ncall,LOGfreq) == 0 ) then
LogMessage = 'Allocating arrays in call '//trim(char(ncall+ICHAR('0')))
call log_message(LOGfile,LogMessage)
end if
allocate(curve(maxlen,maxy))
allocate(curves(maxlen,maxy,4))
allocate(curvem(maxlen,maxy))
allocate(graph1(maxlen,1))
allocate(grafall(2,maxlen,4))
allocate(freq3X(maxlen))
allocate(std(2,4,maxlen))
allocate(stdslice(1,maxlen))
allocate(tempTD(4,maxlen))
allocate(tempSTD(2,4,maxlen))
allocate(tempSTDslice(1,maxlen))
! allocate(tempDEV(2,4,maxlen))
allocate(fatpct(maxlen))
allocate(propct(maxlen))
allocate(scs(maxlen))
allocate(tests(maxlen/plotfreq))
allocate(dimvec(maxlen))
! tempTD is used for storing TD values to be written to an output file.
! If a given day is a test day then tempTD stores the milk/components
! test value. Otherwise, it holds -999.0.
std = -999.0
tempTD = -999.0
tempSTD = -999.0
! tempDEV = -999.0
DAILYbp = -999.0
DAILYherd = 0.0
sum305 = 0.
sum365 = 0.
sum999 = 0.
sumpart = 0.
!herdstate = '00'
!!! print *, dim(1:10)
!!! print *, yield(1,1:10)
! print *, '[bestpred]: length upon entry to bestpred: ', length
! print *, yield(1,1,1:tstdays)
! print *, yield(1,2,1:tstdays)
! print *, yield(1,3,1:tstdays)
! print *, yield(1,4,1:tstdays)
! print *, '[bestpred]: herdavg upon entry to bestpred: ', herdavg
! print *, '[bestpred]: dev: ', dev
if ( DEBUGmsgs > 1 ) then
print *, 'UNITSin: ', UNITSin
print *, 'UNITSout: ', UNITSout
end if
! if ( DEBUGmsgs > 1 ) print *, '[bestpred]: herd305 passed from bestpred_fmt4()', herdavg
! If the user is sending in pounds we need to convert them to kg.
if ( UNITSin == 'P' ) then
if ( LOGon == 1 .and. LOGfreq > 0 .and. mod(ncall,LOGfreq) == 0 ) then
LogMessage = 'Converting from lbs to kg for interal calculations'
call log_message(LOGfile,LogMessage)
end if
yield(:,1,:) = yield(:,1,:) / lb
herdavg(:,1:3) = herdavg(:,1:3) / lb
! Note that dyld, dsd, brdyld, and brdsd are SAVEd so we only
! need to convert them once.
if ( ncall == 0 ) then
dyld(:,1:3,:) = dyld(:,1:3,:) / lb
dsd(:,1:3,:) = dsd(:,1:3,:) / lb
brdyld(:,1:3,:) = brdyld(:,1:3,:) / lb
brdsd(:,1:3) = brdsd(:,1:3) / lb
end if
end if
! The herd average SCS DOES NOT need to be divided by 100. For Format 4 records that
! is done on bestpred_fmt4() before bestpred is called(). For the edits system, that
! is sone before herd averages are passed to bestpred().
! herdavg(:,4) = herdavg(:,4) / 100
if ( dim0flag == 1 ) then
dim0 = 0
d0 = 0
else
d0 = 0
do j=1,4
d0(1,j) = dim0(j)
d0(2,j) = dim0(4+j)
end do
end if
! Loop over the elements of GRAFplot to determine the appropriate
! labels for the output. This is a little tricky b/c all, some,
! or no plats can be requested, each with a separate flag.
GRAFplotMIXED = 0
GRAFplotSUM = sum(GRAFplot)
if ( GRAFplotSUM > 0 ) then
do j = 1, 4
if ( j == 1 .and. GRAFplot(j) == 1 ) GRAFplotMIXED = 1
if ( j == 1 .and. GRAFplot(j) == 2 ) GRAFplotMIXED = 2
if ( j > 1 .and. GRAFplot(j) == 1 .and. GRAFplotMIXED == 2 ) GRAFplotMIXED = 3
if ( j > 1 .and. GRAFplot(j) == 2 .and. GRAFplotMIXED == 1 ) GRAFplotMIXED = 3
end do
end if
! Print input parameters
if ( ncall == 0 .and. DEBUGmsgs > 0 ) then
print *,'[bestpred]: Tipping points (1): ', dim0(1:4)
print *,'[bestpred]: Tipping points (2): ', dim0(5:8)
end if
!if ( maxprnt < 0 ) then
if ( DEBUGmsgs > 0 ) then
print *,'======================================================'
print *,'Inputs to bestpred for cow=',cowid,' fresh=',fresh
print *,' tstdays=',tstdays,' parity=',parity, &
' length=',length, ' maxprnt=',maxprnt
print *,' dim sup frq way sam mrd milk fat prot scs'
do j = 1,min(tstdays,maxtd)
if ( UNITSin == 'P' ) then
print 1,dim(j),super(j),Xmilk(j),weigh(j),sample(j),mrd(j),yield(1,1,j)*lb,yield(1,2:4,j)
else
print 1,dim(j),super(j),Xmilk(j),weigh(j),sample(j),mrd(j),yield(1,:,j)
end if
1 format(6i4,4f6.1)
if(tstdays > maxtd) print *,tstdays,' tests but',maxtd,' used'
enddo
if ( UNITSin == 'P' ) then
print 2,'Herd average mlk fat pro scs =', herdavg(:,1:3)*lb, herdavg(:,4)*100
else
print 2,'Herd average mlk fat pro scs =', herdavg
end if
2 format(a,8f10.2)
print *,'======================================================'
end if
if ( DEBUGmsgs > 1 ) then
print *,'======================================================'
print *,"John's arguments"
print *," GRAFplot :", GRAFplot
print *," DEBUGmsgs :", DEBUGmsgs
print *," ONscreen :", ONscreen
print *," laclen :", laclen
print *," dailyfreq :", dailyfreq
print *," INTmethod :", INTmethod
print *," maxlen :", maxlen
print *," WRITEcurve :", WRITEcurve
print *," CURVEfile :", CURVEfile
print *," WRITEdata :", WRITEdata
print *," DATAfile :", DATAfile
print *," plotfreq :", plotfreq
print *," INTmethodSCS :", INTmethodSCS
print *," READparms :", READparms
print *," UNITSin :", UNITSin
print *," UNITSout :", UNITSout
print *," breedUNK :", breedUNK
print *," dim0 :", dim0
print *," dim0 :", dim0
print *," dim0flag :", dim0flag
print *," LOGon :", LOGon
print *," LOGfile :", LOGfile
print *," LOGfreq :", LOGfreq
print *," agefac :", agefac
print *,'======================================================'
end if
! print *," agefac :", agefac
! Moved up from line ~566 so that breed can be passed to interpolate
read(cowid,'(a1)') brd1
brd = index(breed6,brd1)
! If brd == 0 then then brd1 wasn't found in breed6. Set the default to
! breedUNK and print a warning.
if ( brd == 0 ) then
if ( DEBUGmsgs == 1 ) print *, "[WARNING]: Unknown breed ", brd1, &
"provided; defaulting to ", breedUNK, " (", breed(breedUNK), ")."
if ( LOGon == 1 .and. LOGfreq > 0 .and. mod(ncall,LOGfreq) == 0 ) then
LogMessage = 'Unknown breed '//trim(char(brd+ICHAR('0')))//' provided; defaulting to ' &
//trim(char(breedUNK+ICHAR('0')))//' ('//trim(breed(breedUNK))//')'
call log_message(LOGfile,LogMessage)
end if
brd = breedUNK
end if
! If herdstate is 00 then bestpred() is being called by the edits system. We don't want to call interpolate()
! every tine the season of calving changes since we're not using regional or seasonal effects, so we're fixing
! the region and season at the defaults.
if ( ( INTmethod == 'L' .or. INTmethod == 'W' ) .and. ( INTmethodSCS == 'L' .or. INTmethodSCS == 'G' ) ) then
herdstate = '00' ! Avoid flip-flopping on interpolate() unless were using seasons and/or regions
end if
if ( herdstate .eq. '00' ) then
region = 2 ! Midwest
season = 1 ! Spring
else
call state_to_region(herdstate, region, DEBUGmsgs)
call date_to_season(fresh, season, DEBUGmsgs)
end if
if ( ncall == 0 ) then
oldbrd = brd
oldregion = region
oldseason = season
end if
! -------------------------------------- SECTION DONE DURING FIRST CALL
if ( ncall > 0 .and. brd .eq. oldbrd .and. dim0flag == 0 .and. season .eq. oldseason .and. region .eq. oldregion ) go to 20
if ( maxprnt > 0 .and. ncall == 0 ) then
print *,'Lactation yields are MATURE (adjusted for age, etc.) and ACTUAL (unadjusted) values'
if ( GRAFplotMIXED == 0 ) print *, 'No lactation curves are being plotted.'
if ( GRAFplotMIXED == 1 ) print *, 'Plotted lactation curves are MATURE (adjusted for age, etc.)'
if ( GRAFplotMIXED == 2 ) print *, 'Plotted lactation curves are ACTUAL (unadjusted for age, etc.)'
if ( GRAFplotMIXED == 3 ) &
print *, 'Plotted lactation curves are a mix of MATURE (adjusted for age, etc.) and ACTUAL (unadjusted) values'
if ( mtrait > 1 ) then
print *,'Multi-trait methods include data for',trt(1:mtrait)
else
print *,'!! Only single-trait methods were used !!'
end if
if ( use3X == 0 ) print *,'3X adjustments were not used'
if ( use3X == 1 ) print *,'Old 3X factors were used'
if ( use3X == 2 ) print *,'New 3X factors were used'
if ( use3X == 3 ) print *,'New 3X factors phased in over time'
end if
if ( nbump > 0 ) then
if ( maxprnt > 0 ) print *, &
'DCR was reduced for bumpiest 1 /',10**nbump,'lacts'
nbmp = nbump
maxbump = 1.0
else
maxbump = 99.
if ( maxprnt > 0 .and. ncall == 0 ) print *,'DCRs were not adjusted for bumpiness'
end if
! Compute means, s.d. of test days
! within each lactation group
do 19 lacn = last,1,-1
! Interpolate between monthly means and sd
! Note that interpolate() has to be called once for each
! trait under evaluation.
do j = 1,4
if ( j <= 3 ) then
if ( DEBUGmsgs > 0 ) then
print *,'[bestpred]: Calling interpolate for trait ', j, ' with method ', INTmethod, ' and breed ', brd &
, ' and region ', region, ' and season ', season
end if
if ( LOGon == 1 .and. LOGfreq > 0 .and. mod(ncall,LOGfreq) == 0 ) then
LogMessage = '[bestpred]: Calling interpolate(): trait '//trim(char(j+ICHAR('0')))//', method ' &
//trim(INTmethod)//', breed '//trim(char(brd+ICHAR('0')))//', region ' &
//trim(char(region+ICHAR('0')))//', and season '//trim(char(season+ICHAR('0')))
call log_message(LOGfile,LogMessage)
end if
call interpolate(j, month, dyield, lacn, dyld, summ, meanyld, meanp &
, sd, dsd, INTmethod, DEBUGmsgs, maxlen, brd, region, season)
else
if ( LOGon == 1 .and. LOGfreq > 0 .and. mod(ncall,LOGfreq) == 0 ) then
!LogMessage = 'Calling interpolate with method '//trim(INTmethod)//' and breed ' &
! //trim(char(brd+ICHAR('0')))
LogMessage = '[bestpred]: Calling interpolate(): trait '//trim(char(j+ICHAR('0')))//', method ' &
//trim(INTmethodSCS)//', breed '//trim(char(brd+ICHAR('0')))//', region ' &
//trim(char(region+ICHAR('0')))//', and season '//trim(char(season+ICHAR('0')))
call log_message(LOGfile,LogMessage)
end if
if ( DEBUGmsgs > 0 ) then
print *,'[bestpred]: Calling interpolate for trait ', j, ' with method ', INTmethodSCS, ' and breed ', brd &
, ' and region ', region, ' and season ', season
end if
call interpolate(j, month, dyield, lacn, dyld, summ, meanyld, meanp &
, sd, dsd, INTmethodSCS, DEBUGmsgs, maxlen, brd, region, season)
end if
end do
do j = 1,4
stdvar(j,lacn) = 0.
stdvar(j,lacn) = covary(j,0,0,305,maxlen,1,covari,covd,sd,lacn,last,precise,1)
end do
lplus = (precise - 1)/2.
do 8 j=1,4
do 7 k=1,4
! Compute 305-d covariances from test day
! Get persistency statistics
corr305(j,k) = 0.d0
dV1(j,k) = 0.d0
dVd(j,k) = 0.d0
do 7 i=1,305
! Save CPU if precise > 1
do 7 l=1,305,precise
vijkl = vary(i,j,1,2,2,1,l,k,1,2,2,1,sd,lacn,last,maxlen)
vijkl = vijkl*min(precise,306-l)
corr305(j,k) = corr305(j,k) + vijkl
dV1(j,k) = dV1(j,k) + i*vijkl
7 dVd(j,k) = dVd(j,k) + i*(l+lplus)*vijkl
if ( dim0flag == 1 ) then
dim0(4*(lacn-1)+j) = dV1(j,j) / stdvar(j,lacn)
d0(lacn,j) = dim0(4*(lacn-1)+j)
end if
! 8 d0(j) = dim0(j)
8 continue
do 10 j=1,4
meanp(lacn,j) = meanp(lacn,j) - d0(lacn,j)*meanyld(j,305,lacn)
varp(lacn,j) = dVd(j,j) - 2*dV1(j,j)*d0(lacn,j) + stdvar(j,lacn)*d0(lacn,j)**2
do 10 k=1,4
do 10 l=1,maxlen
10 covd(j,k,l,305,lacn) = (covd(j,k,l,305,lacn) - &
covari(j,k,l,305,lacn)*d0(lacn,j)) / dsqrt(varp(lacn,j))
do 12 i=1,305
tdhi = dyield(i,1,lacn) + tdregr
tdlo = dyield(i,1,lacn) - tdregr
12 qpq = qpq + (i - d0(lacn,1))**2
if ( maxprnt > 0 .and. ncall == 0 ) then
if ( lacn == last ) print *,'=======================' &
,'=============================================='
if ( lacn == last ) print *,'Breed ',brd1,' Lact Mean, ' &
,'St.Dev. Max.DCR Lactation Corr. mid-DIM'
if ( lacn < last ) plus = ' '
end if
do 18 j=1,4
lacdif = meanyld(j,305,lacn) / meanyld(j,305,last)
zero = 0.
sddif = dsqrt(stdvar(j,lacn) / stdvar(j,last))
if ( maxprnt > 0 .and. ncall == 0 ) then
if ( UNITSout .eq. 'P' ) then
print 17, &
trt(j+4),lacn,plus,brdyld(brd,j,2)*X100(j) &
*lacdif*lb,brdsd(brd,j)*X100(j)*sddif*lb,100./rmonth(j,lacn) &
,(corr305(j,k)/dsqrt(stdvar(j,lacn)*stdvar(k,lacn)) &
,k=1,4),d0(lacn,j)
else
print 17, &
trt(j+4),lacn,plus,brdyld(brd,j,2)*X100(j) &
*lacdif,brdsd(brd,j)*X100(j)*sddif,100./rmonth(j,lacn) &
,(corr305(j,k)/dsqrt(stdvar(j,lacn)*stdvar(k,lacn)) &
,k=1,4),d0(lacn,j)
end if
end if
17 format(a7,i4,a1,2f8.0,f9.1,1x,4f6.2,f8.0)
18 continue
! print *,' qpq = ',qpq
!print *, lacn, ': corr305: ', corr305
!print *, lacn, ': stdvar: ', stdvar(:,lacn)
19 continue
if ( dim0flag == 1 ) then
print *,'New tipping points (1): ', dim0(1:4)
print *,'New tipping points (2): ', dim0(5:8)
end if
if ( oldbrd .ne. brd ) then
! if ( DEBUGmsgs > 0 ) print *, '[bestpred]: Breed changed from ', &
! breed(oldbrd), ' to ', breed(brd), ' in call ', ncall+1, '.'
oldbrd = brd
end if
if ( oldregion .ne. region ) oldregion = region
if ( oldseason .ne. season ) oldseason = season
!! if ( maxprnt <= 0 ) go to 20
if ( maxprnt > 0 .and. ncall == 0 ) then
print *,'===========================================' &
,'=========================='
end if
! -------------------------------------------------- DEFINE PLOT LETTERS
if ( GRAFplot(1) > 0 .or. GRAFplot(2) > 0 .or. GRAFplot(3) > 0 .or. GRAFplot(4) > 0 ) then
print*,'Compute DCR, predict yield of cow ---, graph contemps ...'
print*,'Supervised =S ampm1of2=A 2of3=B 1of3=C' &
,' ver=V LER=L dup=D'
print*,'OwnerSample=O OSampm1of2=P 2of3=Q 1of3=R' &
,' ownerLER=U, bad=X'
print*,'Capital letters if milk was sampled, small if milk-only'
end if
!
! ------------------------------------ SECTION DONE FOR EVERY LACTATION
20 ncall = ncall + 1
! Assign parities to age group
lacn = min(parity,last)
if(lacn <= 0) lacn = last
read(cowid,'(a1)') brd1
! Check for 0 tests or length > maxlen
if ( length > maxlen ) length = maxlen
if ( tstdays <= 0 ) then
ntd = 1
dim(1) = maxlen + 1
else
ntd = tstdays
end if
! print *, "ntd :", ntd
! print *, "tstdays:", tstdays
if ( ntd > maxtd ) ntd = maxtd
! Allow for unsorted test day segments
! Exclude duplicate test days
! Check DIM and MRD ranges
dimvec = 0
MRDi = 0
do 23 i = 1,ntd
! if ( i >= 20 ) then
! print *, 'cow ', cowid, ', test ', i, ' milk:', yield(1,1,i), ' fat :', yield(1,2,i) &
! , ' prot:', yield(1,3,i), ' SCS :', yield(1,4,i), ' MRD: ', MRD(i)
! end if
if ( dim(i) <= maxlen ) then
if ( dim(i) < 1 ) then
print *,'Bad DIM',dim(i),' cow ',cowid,' test',i
else
if ( dim(i) - MRD(i) < 0 ) MRD(i) = dim(i)
MRDi = MRD(i)
do 22 k=1,MRDi
if ( dimvec(dim(i)-k+1) > 0 ) then
if ( MRDi == 1 ) then
ndup = ndup + 1
if ( ndup <= 10 ) print * &
,'Warning: cow ',cowid,' has duplicate tests ' &
,'lact',parity,' day',dim(i)
else
overlaps = overlaps + 1
if ( overlaps <= 10 ) print * &
,'Warning: cow ',cowid,' LER segments overlap' &
,' lact',parity,' day',dim(i)-k+1
MRD(i) = k - 1
go to 23
end if
else
dimvec(dim(i)-k+1) = i
!!! print *, 'dimvec(', dim(i)-k+1, '):', dimvec(dim(i)-k+1)
end if
22 continue
end if
end if
23 continue
! Adjustments for 3X milking
read(fresh,'(i4)') yearfr
!print *, '[bestpred]: dim: ', dim
!print *, 'cowid: ', cowid, ' (', parity, ')'
!print *, '[bestpred]: meanyld before 3X adjustment', meanyld
! print *, '[bestpred]: herdavg before 3X adjustment', herdavg
! print *, '[bestpred]: ntd before 3X adjustment', ntd
! print *, '[bestpred]: dim before 3X adjustment', dim
call adjust3X &
(ntd,dim,length,yearfr,parity,Xmilk,meanyld, &
test3X,fact3X,part3X,use3X,maxtd,last,maxlen)
! print *, '[bestpred]: herdavg after 3X adjustment', herdavg
do j=1,4
! If there is no herd average use the breed mean
if ( herdavg(1,j) <= 0.d0 ) herdavg(1,j) = brdyld(brd,j,lacn)
! Convert SCS mean to sum
herd305(1,j) = herdavg(1,j)*X305(j)
! Adjust herd mean for 3X milking
herd305(1,j) = herd305(1,j)*fact3X(j)
! Deviations are already adjusted
if ( herd305(1,j) <= 0.d0 ) then
if(yearfr < 2000) herd305(1,j) = brdyld(brd,j,1)*X305(j)
if(yearfr >= 2000) herd305(1,j) = brdyld(brd,j,2)*X305(j)
endif
hratio(j) = herd305(1,j) / meanyld(j,305,lacn)
!print *, '[bestpred]: herd305 after 3X adjustment', herd305
! Create the herd standard curve using a breed mean.
do i = 1,maxlen
std(1,j,i) = ymean(j,i,maxlen,1,dyield,lacn,last,hratio,herd305(1,j))
! Accumulate lactation totals based on the standard curve.
if ( i <= 305 ) sum305(j) = sum305(j) + std(1,j,i)
if ( i <= 365 ) sum365(j) = sum365(j) + std(1,j,i)
if ( i <= laclen ) sum999(j) = sum999(j) + std(1,j,i)
if ( i <= length ) sumpart(j) = sumpart(j) + std(1,j,i)
end do
! If a herd average was provided by the user adjust the standard curve calculated
! using the breed mean to a curve based on the herd average by adjusting the curve
! up or down, as appropriate.
!!! This looks like it never gets invoked because of the code at ca. 797. It's
!!! also not clear to me as of 04/21/2015 exactly what is supposed to happen
!!! here.
if ( herdavg(1,j) <= 0.d0 ) then
herd305(1,j) = herd305(1,j) * ( herd305(1,j) / sum305(j) )
end if
!!!
herd365(1,j) = herd305(1,j) * ( sum365(j) / sum305(j) )
herd999(1,j) = herd305(1,j) * ( sum999(j) / sum305(j) )
herdpart(1,j) = herd305(1,j) * ( sumpart(j) / sum305(j) )
! Actual variances change with:
! lactation, breed, and age
varfac(j) = 0.
varfac(j) = brdsd(brd,j)*305./dsqrt(stdvar(j,lacn))
! minyield and maxyield are 4-by-1 vectors that contain the min and
! maxyield for each trait for use in plotting.
maxyield(j) = ymean(j,60,maxlen,1,dyield,lacn,last &
,hratio,herd305) / (agefac(j)*part3X(j))
minyield(j) = ymean(j,maxlen,maxlen,1,dyield,lacn,last &
,hratio,herd305) / (agefac(j)*part3X(j))
! Multiply the components min and max by 10. These values are used only
! for plotting so we don't have to keep them in their original units.
if ( j == 2 .or. j == 3 ) then
minyield(j) = minyield(j)*minyield(1)*10
maxyield(j) = maxyield(j)*minyield(1)*10
end if
if ( j == 4 ) then
minyield(j) = minyield(j)*10
maxyield(j) = maxyield(j)*10
end if
! Note that we have to swap the high and low values for SCS.
if ( j == 4 ) then
k = minyield(j)
minyield(j) = maxyield(j)
maxyield(j) = k
end if
end do
! print *, '[bestpred]: herdavg after all adjustments', herdavg
! print *, '[bestpred]: herd305 after all adjustments', herd305
! maxyld and minyld are the minimum and maximum yields for MILK
! used by PVR's original plotting code.
maxyld = ymean(1,60,maxlen,1,dyield,lacn,last &
,hratio,herd305(1,:)) / (agefac(1)*part3X(1))
minyld = ymean(1,maxlen,maxlen,1,dyield,lacn,last &
,hratio,herd305(1,:)) / (agefac(1)*part3X(1))
size = 0
!if ( DEBUGmsgs > 1 ) then
! print *, '[bestpred]: herdavg :', herdavg(1,:)
! print *, '[bestpred]: herd305 :', herd305(1,:)
! print *, '[bestpred]: herd365 :', herd365(1,:)
! print *, '[bestpred]: herd999 :', herd999(1,:)
! print *, '[bestpred]: herdpart: ', herdpart(1,:)
! print *, '[bestpred]: minyield: ', minyield
! print *, '[bestpred]: maxyield: ', maxyield
!end if
! ................................... BEGIN MULTI-TRAIT BEST PREDICTION
! Always do MT if mtrait is 3 (MFP) or 4 (MFPS).
if ( mtrait > 1 ) then
if ( DEBUGmsgs > 0 ) print *,'Doing MT prediction, mtrait= ', mtrait
! Edit test day data
! print *, '[bestpred]: dev before adjusting for 3X, etc.: ', dev
size = 0
ntests = 0
usetd = 0
do 40 i=1,ntd
if(dim(i) < 1) go to 40
if(dim(i) > maxlen) go to 40
if(dimvec(dim(i)) .ne. i) go to 40
usetd = usetd + 1
dimsort(usetd) = dim(i)
if(sample(i) > Xmilk(i)) sample(i) = Xmilk(i)
if(MRD(i) > 1) weigh(i) = Xmilk(i)
!!! 08/28/2007 JBC -- This fixes the problem with out-of-bounds indexing
!!! into dimsort. I think this is okay since dimsort is
!!! used only for milk.
if ( j == 1 ) dimsort(i) = dim(i)
do 35 j=1,4
if(weigh(i) < 1) yield(1,j,i) = zero
if(sample(i) < 1) then
yield(1,2,i) = zero
yield(1,3,i) = zero
! Allow SCS with 0 samples
if(yield(1,4,i) > zero) sample(i) = 1
end if
if(yield(1,j,i) <= zero) go to 35
! Change % to lbs
if ( j == 2 ) yield(1,j,i) = yield(1,1,i)*yield(1,j,i)/100.d0
if ( j == 3 ) yield(1,j,i) = yield(1,1,i)*yield(1,j,i)/100.d0
if(yield(1,1,i) > maxyld) maxyld = yield(1,1,i)
if(yield(1,1,i) < minyld) minyld = yield(1,1,i)
if ( j > mtrait ) go to 35
if ( size == maxy ) then
print *,' Too much data (size > maxy) for cow ', cowid
go to 35
end if
size = size + 1
!print *, "i: ", i, " dim(i): ", dim(i), " j: ", j, " ntests(j): ", ntests(j)
if(dim(i) <= maxlen) then
ntests(j) = ntests(j) + 1
if ( j == 1 ) ntests(1) = ntests(1) + MRD(i) - 1
end if
q(1) = dim(i)
q(2) = j
q(3) = super(i)
q(4) = Xmilk(i)
if(j == 1) q(5) = weigh(i)
if(j > 1) q(5) = sample(i)
q(6) = MRD(i)
qq(:,size) = q(:)
! Adjust cow for 3X,
! Contemp mean for age
dev(size,1) = yield(1,j,i)*test3X(j,i) - &
ymean(j,dim(i),maxlen,MRD(i),dyield,lacn,last &
,hratio,herd305(1,:))/agefac(j)
! Put the unadjusted TD value into tempTD for later use
if ( yield(1,j,i) /= 0.0 ) tempTD(j,dim(i)) = yield(1,j,i)
! tempDEV(1,j,dim(i)) = dev(size,1)
! GET TEST DAY VARIANCE
do k=1,size
kk = qq(2,k)
var(size,k) = vary(q(1),q(2),q(3),q(4),q(5),q(6), &
qq(1,k),qq(2,k),qq(3,k),qq(4,k),qq(5,k),qq(6,k), &
sd,lacn,last,maxlen) &
* varfac(j)*varfac(kk) / (agefac(j)*agefac(kk))
var(k,size) = var(size,k)
end do
! ----------------------------------------- Store covariances for graph
do m = 1,maxlen/dailyfreq
do l = 1,4
curves(m,size,l) = vary(q(1),q(2),q(3),q(4),q(5),q(6), &
m*dailyfreq,l,1,2,2,1,sd,lacn,last,maxlen) &
* varfac(j)*varfac(l) / (agefac(j)*agefac(l))
end do
end do
! Look up covariance with 305
! covary() returns the covariance between TD DIM and lactation yields.
! Note that we're calculating for three endpoints: 305 d, 365 d, and a
! user-defined lactation length (maxlen). If maxlen is equal to either
! 305 or 365 we'll only do the calculation once.
do 34 k=1,4
! 305-d lactation
cov305(k,size) = covary(k,j,dim(i),305,maxlen,MRD(i),covari,covd, &
sd,lacn,last,precise,1) * varfac(j)*varfac(k) / agefac(j)
covp305(size,k) = cov305(k,size)
! 365-d lactation
cov365(k,size) = covary(k,j,dim(i),365,maxlen,MRD(i),covari,covd, &
sd,lacn,last,precise,1) * varfac(j)*varfac(k) / agefac(j)
covp365(size,k) = cov365(k,size)
! If laclen is either 305 or 365 don't redo those calculations.
if ( laclen .ne. 305 .and. laclen .ne. 365 ) then
cov999(k,size) = covary(k,j,dim(i),laclen,maxlen,MRD(i),covari, &
covd,sd,lacn,last,precise,1) * varfac(j)*varfac(k) / agefac(j)
covp999(size,k) = cov999(k,size)
end if
dcov(k,size) = covary(k,j,dim(i),305,maxlen,MRD(i),covari,covd, &
sd,lacn,last,precise,2) * varfac(j)*varfac(k) / agefac(j)
dcovp(size,k) = dcov(k,size)
! Sum covariances for part record
covsum(k,size) = 0.d0
if ( part == 0 ) go to 34
do 33 m = 1,length
33 covsum(k,size) = covsum(k,size) + vary(q(1),q(2),q(3), &
q(4),q(5),q(6),m,k,1,2,2,1,sd,lacn,last,maxlen) &
* varfac(j)*varfac(k) / agefac(j)
34 continue
! Save sorted milk tests for graph