      subroutine M3AVE
      include 'SIZE.INC'
      include 'GLOBAL.INC'
      include 'LOCAL.INC'
      include 'UNITS.INC'
      include 'LOCAL1.INC'
      real OUTDAT(6), PERCET, TOTMAS
      integer IALPHA(7)
      integer KKNTW, KKNTS, III(2), IV, VRAIL
      integer IFIRST(2), ILAST(2)
      integer I, ICHEK, ISTART, J, K, K1, KK, II, IONTES, NNDAT,
     &        NTAB, NTABLE, N1, M1, M2
      logical MULTYR
      character*1 NTYPX(5), NTYPN(5)
      character ITOP(4)*4, OUTLIN(2)*64, PIECE(7)*4
      character DASH*1, DOT*1, KOUT*2, YRCH(2)*4, STAR*1
      data IALPHA/25,21,17,1,5,9,13/
      data PIECE /'(-3)','(-2)','(-1)',' (0)','(+1)','(+2)','(+3)'/
      data DASH,DOT,STAR/'-','.','*'/
5000  format ('1','Exposure Analysis Modeling System -- EXAMS Version ',
     &   F4.2,', Mode',I2/' Ecosystem: ',50A1)
5010  format (' Chemical:  ',50A1)
5020  format (1X,63('-'))
      if (MACHNO.eq.11) then
         open (unit=SSLUN(MACHNO),status='new',access='sequential',form=
     &      'unformatted',FILE='SSOUT.PLT')
      else
         open (unit=SSLUN(MACHNO),status='unknown',access='sequential',
     &      form='unformatted',FILE='SSOUT.PLT')
         if (.not.(TOPOPN(MACHNO))) rewind SSLUN(MACHNO)
      endif
      write (SSLUN(MACHNO)) KCHEM,MODEG,KOUNT
      do 100 KK = 1, KCHEM
         write (SSLUN(MACHNO)) (CHEMNA(I,KK),I=1,50)
100   continue
      write (SSLUN(MACHNO)) (ECONAM(I),I=1,50)
      if (KOUNTW.le.0.0) KOUNTW = 1.0
      if (KOUNTS.le.0.0) KOUNTS = 1.0
      KKNTW = ifix(KOUNTW)
      KKNTS = ifix(KOUNTS)
      MULTYR = .false.
      if (LASTYR.gt.FRSTYR) MULTYR = .true.
      write (YRCH(1),5030) FRSTYR
5030  format (I4)
      if (MULTYR) then
         write (YRCH(2),5030) LASTYR
         do 120 K = 1, 2
            do 110 I = 1, 4
               if (YRCH(K)(I:I).eq.' ') YRCH(K)(I:I) = '0'
110         continue
120      continue
      else
         do 130 I = 1, 4
            if (YRCH(1)(I:I).eq.' ') YRCH(1)(I:I) = '0'
130      continue
      endif
      III(1) = VRAIL(ECONAM)
      do 450 KK = 1, KCHEM
         III(2) = VRAIL(CHEMNA(1,KK))
         do 140 I = 1, 6
            Z(I,KK) = 0.0
140      continue
         do 150 I = 1, 12
            Z2(I) = 0.
150      continue
         do 170 J = 1, KOUNT
            if (TYPEG(J).eq.'B') go to 160
            Z(1,KK) = Z(1,KK)+(YSUMS(29,J,KK)+YSUMS(31,J,KK))/YCOUNT
            Z(2,KK) = Z(2,KK)+YSUMS(30,J,KK)/YCOUNT
            Z(5,KK) = Z(5,KK)+YTOT(3,J,KK)/YCOUNT
            go to 170
160         continue
            Z(3,KK) = Z(3,KK)+(YSUMS(29,J,KK)+YSUMS(31,J,KK))/YCOUNT
            Z(4,KK) = Z(4,KK)+YSUMS(30,J,KK)/YCOUNT
            Z(6,KK) = Z(6,KK)+YTOT(3,J,KK)/YCOUNT
170      continue
         Z(1,KK) = Z(1,KK)/KOUNTW
         Z(2,KK) = Z(2,KK)/KOUNTW
         Z(3,KK) = Z(3,KK)/KOUNTS
         Z(4,KK) = Z(4,KK)/KOUNTS
         write(RPTLUN(MACHNO),5000) VERSN,MODEG,(ECONAM(IV),IV=1,III(1))
         write (RPTLUN(MACHNO),5010) (CHEMNA(IV,KK),IV=1,III(2))
         write (RPTLUN(MACHNO),5020)
         write (KOUT,5040) KK
5040     format (I2)
         if (KOUT(1:1).eq.' ') KOUT(1:1) = '0'
         if (MULTYR) then
            write (RPTLUN(MACHNO),5050) KOUT,YRCH(1),DASH,YRCH(2)
         else
            write (RPTLUN(MACHNO),5050) KOUT,YRCH(1),DOT
         endif
5050  format                                                            
     &(' Table 15.',A2,'. Average distribution of chemical during ',A4, 
     &A1,:,A4,'.')
         write (RPTLUN(MACHNO),5060)
5060     format (1X,63('-'))
         write (RPTLUN(MACHNO),5070)
5070  format                                                            
     &(' Seg',2X,'Resident Mass',3X,8('*'),1X,'Chemical Concentrations',
     &1X,9('*'))
         write (RPTLUN(MACHNO),5080)
5080  format                                                            
     &('  #',21X,'Total',4X,'Dissolved',2X,'Sediments',4X,'Biota')
         write (RPTLUN(MACHNO),5090)
5090     format                                                         
     &(6X,'Kilos',6X,'%',7X,'mg/*',5X,'mg/L **',5X,'mg/kg',7X,'ug/g')
         write (RPTLUN(MACHNO),5100)
5100     format (1X,3('-'),1X,8('-'),1X,6('-'),4(2X,9('-')))
         write (RPTLUN(MACHNO),5110)
5110     format (' In the Water Column:')
         do 190 J = 1, KOUNT
            if (TYPEG(J).eq.'B') go to 190
            Z2(6) = YTOT(3,J,KK)/YCOUNT
            Z2(5) = 1.0E+03*Z2(6)/AREAG(J)
            Z2(7) = Z2(7)+Z2(5)
            PERCET = 0.0
            if (Z(5,KK).eq.0.0) go to 180
            PERCET = Z2(6)*100./Z(5,KK)
180         continue
            Z2(1) = YTOT(1,J,KK)/YCOUNT
            Z2(8) = Z2(8)+Z2(1)
            Z2(2) = (YSUMS(29,J,KK)+YSUMS(31,J,KK))/YCOUNT
            Z2(3) = YSUMS(30,J,KK)/YCOUNT
            Z2(4) = YSUMS(32,J,KK)/YCOUNT
            Z2(9) = Z2(9)+Z2(4)
            write (RPTLUN(MACHNO),5120) J,Z2(6),PERCET,(Z2(I),I=1,4)
            write (SSLUN(MACHNO)) KK,J,TYPEG(J),Z2(5),Z2(6),PERCET,(Z2(I
     &         ),I=1,4)
190      continue
         write (SSLUN(MACHNO)) KK,KKNTW,STAR,(Z(I,KK),I=1,6),Z2(1)
5120     format (1X,I3,1PG9.2,1X,0PF6.2,1X,1PG10.3,3(1X,G10.3))
         Z2(7) = Z2(7)/KOUNTW
         Z2(8) = Z2(8)/KOUNTW
         Z2(9) = Z2(9)/KOUNTW
         PERCET = 0.0
         if (Z(5,KK).eq.0.0.and.Z(6,KK).eq.0.0) go to 200
         PERCET = Z(5,KK)*100./(Z(5,KK)+Z(6,KK))
200      continue
         write (RPTLUN(MACHNO),5130) Z(5,KK),PERCET
5130     format (5X,8('='),1X,6('=')/4X,1PG9.2,1X,0PF6.2)
         write (RPTLUN(MACHNO),5140)
5140     format (/'  and in the Benthic Sediments:')
         do 220 J = 1, KOUNT
            if (TYPEG(J).ne.'B') go to 220
            Z2(6) = YTOT(3,J,KK)/YCOUNT
            Z2(5) = 1.E+03*Z2(6)/AREAG(J)
            Z2(10) = Z2(10)+Z2(5)
            PERCET = 0.0
            if (Z(6,KK).eq.0.0) go to 210
            PERCET = Z2(6)*100./Z(6,KK)
210         continue
            Z2(1) = YTOT(2,J,KK)/YCOUNT
            Z2(11) = Z2(11)+Z2(1)
            Z2(2) = (YSUMS(29,J,KK)+YSUMS(31,J,KK))/YCOUNT
            Z2(3) = YSUMS(30,J,KK)/YCOUNT
            Z2(4) = YSUMS(32,J,KK)/YCOUNT
            Z2(12) = Z2(12)+Z2(4)
            write (RPTLUN(MACHNO),5120) J,Z2(6),PERCET,(Z2(I),I=1,4)
            write (SSLUN(MACHNO)) KK,J,TYPEG(J),Z2(5),Z2(6),PERCET,(Z2(I
     &         ),I=1,4)
220      continue
         write (SSLUN(MACHNO)) KK,KKNTS,STAR,(Z(I,KK),I=1,6),Z2(1)
         Z2(10) = Z2(10)/KOUNTS
         Z2(11) = Z2(11)/KOUNTS
         Z2(12) = Z2(12)/KOUNTS
         PERCET = 0.0
         if (Z(5,KK).eq.0.0.and.Z(6,KK).eq.0.0) go to 230
         PERCET = Z(6,KK)*100./(Z(5,KK)+Z(6,KK))
230      continue
         write (RPTLUN(MACHNO),5130) Z(6,KK),PERCET
         TOTMAS = Z(5,KK)+Z(6,KK)
         write (RPTLUN(MACHNO),5150) TOTMAS
5150     format (1X,'Total Mass (kilograms) =',3X,1PG11.4)
         write (RPTLUN(MACHNO),5060)
         write (RPTLUN(MACHNO),5160)
5160  format ('  * Units: mg/L in Water Column; mg/kg in Benthos.'/     
     &' ** Includes complexes with "dissolved" organics.')
         IONTES = 0
         do 240 II = 1, 7
            if (SPFLGG(II,KK).eq.1) IONTES = IONTES+1
240      continue
         NTABLE = 1
         if (IONTES.gt.4) NTABLE = 2
         ISTART = 0
         do 250 ICHEK = 7, 5, -1
            ISTART = ISTART+1
            if (SPFLGG(ICHEK,KK).eq.1) go to 270
250      continue
         do 260 ICHEK = 1, 4
            ISTART = ISTART+1
            if (SPFLGG(ICHEK,KK).eq.1) go to 270
260      continue
         write (TTYOUT(MACHNO),5170)
         IFLAG = 8
         go to 460
5170  format (' Code malfunction in speciation section of'/             
     &' subroutine M3AVE.  Please report problem to author.')
270      continue
         IFIRST(1) = ISTART
         if (IONTES.gt.4) go to 280
         ILAST(1) = ISTART+IONTES-1
         go to 290
280      continue
         ILAST(1) = ISTART+3
         IFIRST(2) = ISTART+4
         ILAST(2) = IFIRST(2)+IONTES-5
290      continue
         do 360 NTAB = 1, NTABLE
            write (RPTLUN(MACHNO),5000) VERSN,MODEG,(ECONAM(IV),IV=1,III
     &         (1))
            write (RPTLUN(MACHNO),5010) (CHEMNA(IV,KK),IV=1,III(2))
            write (RPTLUN(MACHNO),5020)
            write (RPTLUN(MACHNO),5180) KOUT,NTAB
5180  format (' Table 16.',A2,'.',I1,                                   
     &'.  Distribution of average concentrations among'/                
     &' aqueous chemical species.  All concentrations in ug/L (ppb).')
            write (RPTLUN(MACHNO),5060)
            write (RPTLUN(MACHNO),5190)
5190        format (1X,'Seg',1X,'T*',1X,'  Total  ','    DOC   ',3X,'-',
     &         1X,'Chemical Species (by valency)',1X,'-')
            do 300 I = 1, 4
               ITOP(I) = '    '
300         continue
            K1 = 1
            do 310 K = IFIRST(NTAB), ILAST(NTAB)
               ITOP(K1) = PIECE(K)
               K1 = K1+1
310         continue
            write (RPTLUN(MACHNO),5200) (ITOP(I),I=1,4)
5200        format (2X,'#',2X,'y',1X,'  Aqueous ',' Complexed',4X,A4,5X,
     &         A4,5X,A4,5X,A4)
            write (RPTLUN(MACHNO),5060)
            do 350 J = 1, KOUNT
               OUTDAT(1) = 1000.*(YSUMS(29,J,KK)+YSUMS(31,J,KK))/YCOUNT
               do 320 I = 2, 6
                  OUTDAT(I) = 0.0
320            continue
               OUTDAT(2) = 1000.0*YSUMS(31,J,KK)/YCOUNT
               NNDAT = 3
               do 330 I = IFIRST(NTAB), ILAST(NTAB)
                  OUTDAT(NNDAT) = 1000.0*YSUMS(IALPHA(I),J,KK)/YCOUNT
                  NNDAT = NNDAT+1
330            continue
               write (OUTLIN(1),5210) J,TYPEG(J),(OUTDAT(I),I=1,6)
5210           format (1X,I3,1X,A1,1X,1PG10.3,G10.3,1X,4G9.2)
               if (OUTDAT(2).eq.0.0) OUTLIN(1)(18:27) = '          '
               N1 = 3
               do 340 I = 29, 56, 9
                  if (OUTDAT(N1).eq.0.0) OUTLIN(1)(I:I+8) = '         '
                  N1 = N1+1
340            continue
               write (RPTLUN(MACHNO),5220) OUTLIN(1)
5220           format (A64)
350         continue
            write (RPTLUN(MACHNO),5060)
            write (RPTLUN(MACHNO),5230)
360      continue
5230  format (2X,                                                       
     &'* Segment types: Littoral, Epilimnetic, Hypolimnetic, Benthic.')
        write (RPTLUN(MACHNO),5000) VERSN,MODEG,(ECONAM(IV),IV=1,III(1))
         write (RPTLUN(MACHNO),5010) (CHEMNA(IV,KK),IV=1,III(2))
         write (RPTLUN(MACHNO),5020)
         write (RPTLUN(MACHNO),5240) KOUT
5240  format (' Table 17.',A2,                                          
     &'.  System-wide concentration means and extrema.'/                
     &' Number in parens (Seg) indicates segment where value was found.'
     &)
         write (RPTLUN(MACHNO),5060)
         write (RPTLUN(MACHNO),5250)
5250  format (9X,'Total',9X,'Dissolved',5X,'Sediments',7X,'Biota',/,6X,'
     &Seg   mg/*',5X,'Seg  mg/L **',3X,'Seg  mg/kg',5X,'Seg  ug/gram',/ 
     &4X,4('  -------------')/' Water Column:')
         write (RPTLUN(MACHNO),5260) Z2(8),Z(1,KK),Z(2,KK),Z2(9)
5260     format (' Mean',4X,1PG10.3,3(5X,G10.3))
         write (SSLUN(MACHNO)) KK,Z2(8),Z(1,KK),Z(2,KK),Z2(9),Z2(7)
         write (OUTLIN(1),5270) (MAXSEG(I,KK),PEAKLT(I,KK),I=1,4)
5270     format (' Max',4(1X,I3,')',1PG10.3))
         do 370 J = 1, 5
            NTYPX(J) = TYPEG(MAXSEG(J,KK))
            NTYPN(J) = TYPEG(MINSEG(J,KK))
370      continue
         write (SSLUN(MACHNO)) KK,(MAXSEG(I,KK),I=1,5),NTYPX,
     &                            (PEAKLT(I,KK),I=1,5)
         write (OUTLIN(2),5280) (MINSEG(I,KK),YMINLT(I,KK),I=1,4)
5280     format (' Min',4(1X,I3,')',1PG10.3))
         write (SSLUN(MACHNO)) KK,(MINSEG(I,KK),I=1,5),NTYPN,(YMINLT(I,
     &      KK),I=1,5)
         do 400 I = 1, 2
            do 390 M1 = 7, 52, 15
               do 380 M2 = M1, M1-2, -1
                  if (OUTLIN(I)(M2:M2).ne.' ') go to 380
                  OUTLIN(I)(M2:M2) = '('
                  go to 390
380            continue
390         continue
            write (RPTLUN(MACHNO),5220) OUTLIN(I)
400      continue
         write (RPTLUN(MACHNO),5290)
5290     format (/1X,'Benthic Sediments:')
         write (RPTLUN(MACHNO),5260) Z2(11),Z(3,KK),Z(4,KK),Z2(12)
         write (SSLUN(MACHNO)) KK,Z2(11),Z(3,KK),Z(4,KK),Z2(12),Z2(10)
         write (OUTLIN(1),5270) (MAXSEG(I,KK),PEAKLT(I,KK),I=6,9)
         do 410 J = 6, 10
            NTYPX(J-5) = TYPEG(MAXSEG(J,KK))
            NTYPN(J-5) = TYPEG(MINSEG(J,KK))
410      continue
         write (SSLUN(MACHNO)) KK,(MAXSEG(I,KK),I=6,10),NTYPX,
     &                            (PEAKLT(I,KK),I=6,10)
         write (OUTLIN(2),5280) (MINSEG(I,KK),YMINLT(I,KK),I=6,9)
         write (SSLUN(MACHNO)) KK,(MINSEG(I,KK),I=6,10),NTYPN,(YMINLT(I,
     &      KK),I=6,10)
         do 440 I = 1, 2
            do 430 M1 = 7, 52, 15
               do 420 M2 = M1, M1-2, -1
                  if (OUTLIN(I)(M2:M2).ne.' ') go to 420
                  OUTLIN(I)(M2:M2) = '('
                  go to 430
420            continue
430         continue
            write (RPTLUN(MACHNO),5220) OUTLIN(I)
440      continue
         write (RPTLUN(MACHNO),5060)
         write (RPTLUN(MACHNO),5160)
         YBARLT(1,KK) = Z2(8)
         YBARLT(2,KK) = Z(1,KK)
         YBARLT(4,KK) = Z2(9)
         YBARLT(6,KK) = Z2(11)
         YBARLT(7,KK) = Z(3,KK)
         YBARLT(9,KK) = Z2(12)
450   continue
460   continue
      close (unit=SSLUN(MACHNO))
      return
      end
