c	Program pumprechv9.for							c	Program pumprechv9.for
c	Author: Steve Larson, SSPA						c	Author: Steve Larson, SSPA

c	Purpose: Processes Kansas wateruse data to produce groundwater		c	Purpose: Processes Kansas wateruse data to produce groundwater
c	pumping and recharge files for the RRCA Groundwater Model		c	pumping and recharge files for the RRCA Groundwater Model
c	Version: This version is the same as pumprechv4.for except		c	Version: This version is the same as pumprechv4.for except
c	for the removal of the bi-linear interpolation.				c	for the removal of the bi-linear interpolation.
c										c
c     Version 9 (spp, 4/15/2015): modified to write two additional fields to 	c     Version 9 (spp, 4/15/2015): modified to write two additional fields to 
c       Added fields are acres(jloc,iloc), to show cumulative gw acres in cel	c       Added fields are acres(jloc,iloc), to show cumulative gw acres in cel
c       node number [node = 326*(iloc - 1) + jloc for row iloc and column jlo	c       node number [node = 326*(iloc - 1) + jloc for row iloc and column jlo
c										c
c     Version 8 (spp, 7/8/2011): modified to calculate pd-pu distances betwee	c     Version 8 (spp, 7/8/2011): modified to calculate pd-pu distances betwee
c       within a group(miles) and its corresponding place of use for each tra	c       within a group(miles) and its corresponding place of use for each tra
c       and a nominal range of distance for pds within the group, given by	c       and a nominal range of distance for pds within the group, given by
c         pdrange_mi = sqrt[(max(xi - xj))^2 + (max(yi - yj))^2]/5280 for pds	c         pdrange_mi = sqrt[(max(xi - xj))^2 + (max(yi - yj))^2]/5280 for pds
c         output file authacresYYYY.out, added in Version 6 (see below), was 	c         output file authacresYYYY.out, added in Version 6 (see below), was 
c         coordinates of each place of use (xloc,yloc), centroid of points of	c         coordinates of each place of use (xloc,yloc), centroid of points of
c         variables distpdpu_mi and pdrange_mi.					c         variables distpdpu_mi and pdrange_mi.
c										c
c     Version 7 (spp, 3/2011): modified to reference a single set of static i	c     Version 7 (spp, 3/2011): modified to reference a single set of static i
c       for current and future program runs to process pumping data.		c       for current and future program runs to process pumping data.
c										c
c     Version 6 (spp): writes additional diagnostic output file of authorized	c     Version 6 (spp): writes additional diagnostic output file of authorized
c       --shows orig. input plus (x,y) coords (ft), (row,col) indices, cumul.	c       --shows orig. input plus (x,y) coords (ft), (row,col) indices, cumul.
c       --spp Apr 21, 2010							c       --spp Apr 21, 2010

c     Version 5 changes by spp:							c     Version 5 changes by spp:
c     a) July 2006: correct monthly factors for irrigation pumping distributi	c     a) July 2006: correct monthly factors for irrigation pumping distributi
c          monthlyfactorsbycounty.txt so that the same factors are used in bo	c          monthlyfactorsbycounty.txt so that the same factors are used in bo
c          and ground programs swrechv2 and pumprechv5.				c          and ground programs swrechv2 and pumprechv5.

c     b) July 2006: calculate node from row and column and write to plot file	c     b) July 2006: calculate node from row and column and write to plot file
c        node number can be used in GIS to join data to grid for viewing.	c        node number can be used in GIS to join data to grid for viewing.

c     c) July 2006: increase dimensioning for working arrays gwretloc, acresl	c     c) July 2006: increase dimensioning for working arrays gwretloc, acresl
c          from 3000 to nGroups; define the parameter nGroups=5000.  Groups 4	c          from 3000 to nGroups; define the parameter nGroups=5000.  Groups 4
c          were added to the dataset in RRCS_Overlap_Groups_For2005_Final.xls	c          were added to the dataset in RRCS_Overlap_Groups_For2005_Final.xls
c          two Nebraska wells with place of use in Kansas.			c          two Nebraska wells with place of use in Kansas.
c										c
c     d) July 2008: add some documentation (definitions for input variables r	c     d) July 2008: add some documentation (definitions for input variables r
c          and YYYYmi.txt).							c          and YYYYmi.txt).

c     Version 5h only: additional change by spp:				c     Version 5h only: additional change by spp:
c     a) April 2006: read header records from files monthlyfactorsbycounty.tx	c     a) April 2006: read header records from files monthlyfactorsbycounty.tx
c          authacresYYYY.txt, pumprech2005.txt and 2005mi.txt.			c          authacresYYYY.txt, pumprech2005.txt and 2005mi.txt.
c										c
c     Compiled and linked with Lahey 95 v5.6 as follows:			c     Compiled and linked with Lahey 95 v5.6 as follows:
c       lf95 pumprechv5.for -chk -lst						c       lf95 pumprechv5.for -chk -lst
! source code notes								! source code notes

! Compiling with Gnu Fortran:							! Compiling with Gnu Fortran:
! Prior to compiling, linking or running:					! Prior to compiling, linking or running:
!   set path=%path%;C:\MinGW\bin\						!   set path=%path%;C:\MinGW\bin\
! To compile:									! To compile:
!   Switches: -c: Compile without linking; -o link without compiling; -ffree-	!   Switches: -c: Compile without linking; -o link without compiling; -ffree-
!   gfortran  -c -Wall -Wextra -fcheck=bounds -Fbacktrace pumprechv9.for	!   gfortran  -c -Wall -Wextra -fcheck=bounds -Fbacktrace pumprechv9.for
!   gfortran -static -o pumprechv9 pumprechv9.o					!   gfortran -static -o pumprechv9 pumprechv9.o
c										c
      program pumprechv9							      program pumprechv9

									      >	      parameter (ny=165,nx=326)
									      >	      parameter (gdx=5280,gdy=5280)
									      >	      parameter (xg0=266023,yg0=14092806)
									      >
      dimension pump(400,400),gwret(400,400),acres(400,400),			      dimension pump(400,400),gwret(400,400),acres(400,400),
     + dx(400),dy(400),sx(401),sy(401),ibound(400,400),pumpmi(400,400),		     + dx(400),dy(400),sx(401),sy(401),ibound(400,400),pumpmi(400,400),
     + icty(400,400),xmonfactors(44,12),work(400,400),				     + icty(400,400),xmonfactors(44,12),work(400,400),
     + xcoord(20,45,36,16),ycoord(20,45,36,16)					     + xcoord(20,45,36,16),ycoord(20,45,36,16)

      parameter (nGroups=5000)      !dimensions for overlap groups		      parameter (nGroups=5000)      !dimensions for overlap groups
      dimension authtot(nGroups), gwretloc(nGroups), acresloc(nGroups)		      dimension authtot(nGroups), gwretloc(nGroups), acresloc(nGroups)

      dimension almenapdivs(500)						      dimension almenapdivs(500)

      character*2 county,monthno(12)						      character*2 county,monthno(12)
      character*4 dspm,year							      character*4 dspm,year
      character*10 chalmena_factor						      character*10 chalmena_factor
      character*11 pumpfile,gwrechfile						      character*11 pumpfile,gwrechfile
      character*16 text								      character*16 text
      character*128 header  !added for header records in two files (see v.2h 	      character*128 header  !added for header records in two files (see v.2h 
      integer twnshp,range,sect,tract,grp,eof,pdiv,almenapdivs			      integer twnshp,range,sect,tract,grp,eof,pdiv,almenapdivs
      logical continue								      logical continue
c       added for version pumprechv6.for -spp 3/10/2011				c       added for version pumprechv6.for -spp 3/10/2011
      character*40 chstatic !relative reference to folder path with static in	      character*40 chstatic !relative reference to folder path with static in
      character*4 chout ! reference to subfolder out/ for output files --spp  |	      character*3 chout ! reference to subfolder out/ for output files --spp

      integer npd(nGroups) !counter for no. pds per group			      integer npd(nGroups) !counter for no. pds per group
      dimension xpd(nGroups), ypd(nGroups)        !pd projected coordinates (	      dimension xpd(nGroups), ypd(nGroups)        !pd projected coordinates (
      dimension xpdmin(nGroups), xpdmax(nGroups)  !min & max projected x-coor	      dimension xpdmin(nGroups), xpdmax(nGroups)  !min & max projected x-coor
      dimension ypdmin(nGroups), ypdmax(nGroups)  !min & max projected y-coor	      dimension ypdmin(nGroups), ypdmax(nGroups)  !min & max projected y-coor
      dimension pdrange(nGroups)                  !range of pds within group,	      dimension pdrange(nGroups)                  !range of pds within group,

      data dspm/'DSAA'/								      data dspm/'DSAA'/
      data monthno/'01','02','03','04','05','06','07','08','09','10',		      data monthno/'01','02','03','04','05','06','07','08','09','10',
     +   '11','12'/								     +   '11','12'/

c       relative reference to folder path with static input files, esp. 40coo	c       relative reference to folder path with static input files, esp. 40coo
      data chstatic /'..\..\..\static\'/					      data chstatic /'..\..\..\static\'/
      data chout /'out\'/   !reference to subfolder for output files	      |	      data chout /'ks/'/   !reference to subfolder for output files
c										c
      ier0=0									      ier0=0
      if (IARGC().ne.2) then							      if (IARGC().ne.2) then
        write(*,*) 'usage: pumprechv9 [year] [almena factor]'			        write(*,*) 'usage: pumprechv9 [year] [almena factor]'
        write(*,*) 'ex. for 2010: pumprechv9 2010 0.7187758'			        write(*,*) 'ex. for 2010: pumprechv9 2010 0.7187758'
        STOP									        STOP
      end if									      end if
c										c
c Note: Almena area factor = gw-exclusive irrigated area as fraction of total	c Note: Almena area factor = gw-exclusive irrigated area as fraction of total

      call GETARG(1,year)            !arg 1: year (declared as character*4)	      call GETARG(1,year)            !arg 1: year (declared as character*4)
      call GETARG(2,chalmena_factor) !arg 2: almena factor (text version, dec	      call GETARG(2,chalmena_factor) !arg 2: almena factor (text version, dec
      write(*,*) 'run for year '//year//					      write(*,*) 'run for year '//year//
     |           ', almena factor='//chalmena_factor				     |           ', almena factor='//chalmena_factor
      read(chalmena_factor,'(G32.0)') almenafactor      !replace trim(chalmen |	      read(chalmena_factor,'(G32.0)') almenafactor
      write(*,*) ' real version: ',almenafactor					      write(*,*) ' real version: ',almenafactor
ccc      STOP									ccc      STOP

      open(12,file=trim(chstatic)//'pumprech.par') !added relative path chsta |	C     open(12,file=trim(chstatic)//'pumprech.par') !added relative path chsta
      read(12,*) ny,nx                             !no. rows, no. columns     |	C     read(12,*) ny,nx                             !no. rows, no. columns
      read(12,*) gdx,gdy						      |	C     read(12,*) gdx,gdy
      read(12,*) xg0,yg0						      |	C     read(12,*) xg0,yg0
ccc      read(12,*) year  !get year from cmd line (above) so that input file 	ccc      read(12,*) year  !get year from cmd line (above) so that input file 
      close(12)								      |	C     close(12)
      write(*,*) '..Processing year: ',year,					      write(*,*) '..Processing year: ',year,
     |           ' Almena gw-ex area fraction=',almenafactor			     |           ' Almena gw-ex area fraction=',almenafactor
ccc      pause									ccc      pause

      ygtop=yg0+ny*gdy								      ygtop=yg0+ny*gdy
      zmin=-1000.								      zmin=-1000.
      zmax=10000000.								      zmax=10000000.
c read ibound array								c read ibound array
      write(*,*) '..reading ibound'						      write(*,*) '..reading ibound'
      open(11,file=trim(chstatic)//'02.ibound') !added relative path chstatic |	      open(11,file='../static/02.ibound',STATUS='OLD') !added relative path c
          do i=1,ny								          do i=1,ny
              read(11,'(326i2)') (ibound(j,i),j=1,nx)				              read(11,'(326i2)') (ibound(j,i),j=1,nx)
          end do								          end do
      close(11)									      close(11)
c read county flags								c read county flags
      write(*,*) '..reading county flags'					      write(*,*) '..reading county flags'
      open(11,file=trim(chstatic)//'cty.flg') !added relative path chstatic - |	      open(11,file='../data0/cty.flg',STATUS='OLD') !added relative path chst
      read(11,*)								      read(11,*)
          do i=1,ny								          do i=1,ny
              read(11,'(326i2)') (icty(j,i),j=1,nx)				              read(11,'(326i2)') (icty(j,i),j=1,nx)
          end do								          end do
      close(11)									      close(11)
c read monthly factors for counties						c read monthly factors for counties
      write(*,*) '..reading monthly factors by county'				      write(*,*) '..reading monthly factors by county'
      open(11,file=trim(chstatic)//'monthlyfactorsbycounty.txt') !added relat |	      open(11,file='../data/ks/gw/monthlyfactorsbycounty.txt',
									      >	     |     STATUS='OLD') !added relative path chstatic --spp 3/10/2011
ccc      read(11,'(a)') header !added header to verify columns in file (versi	ccc      read(11,'(a)') header !added header to verify columns in file (versi
      write(*,*) 'Expected fields: index,may,jun,jul,aug,sep,oct,county'	      write(*,*) 'Expected fields: index,may,jun,jul,aug,sep,oct,county'
      do nc=1,44								      do nc=1,44
          read(11,*) ncx,(xmonfactors(nc,im),im=5,10)				          read(11,*) ncx,(xmonfactors(nc,im),im=5,10)
      end do									      end do
      close(11)									      close(11)
ccc      pause									ccc      pause

c read pdivs for almena district						c read pdivs for almena district
      write(*,*) '..reading almena points of diversion'				      write(*,*) '..reading almena points of diversion'
      open(11,file=trim(chstatic)//'almenapdivs'//'.txt')		      |	      open(11,file='../data/ks/gw/almenapdivs'//year//'.txt',
									      >	     |     STATUS='OLD')
      read(11,*) nalmenapdivs							      read(11,*) nalmenapdivs
      do n=1,nalmenapdivs							      do n=1,nalmenapdivs
          read(11,*) almenapdivs(n)						          read(11,*) almenapdivs(n)
      end do									      end do
      close(11)									      close(11)

c     zero working arrays							c     zero working arrays
      do n=1,nGroups								      do n=1,nGroups
         gwretloc(n)=0.								         gwretloc(n)=0.
         acresloc(n)=0.								         acresloc(n)=0.
         authtot(n)=0.								         authtot(n)=0.
      end do									      end do

      pump=0.									      pump=0.
      gwret=0.									      gwret=0.
      acres=0.									      acres=0.
      pumpmi=0.									      pumpmi=0.

      mxGroup = 0  !initialize largest group no. read from file			      mxGroup = 0  !initialize largest group no. read from file

      sumgwret=0.								      sumgwret=0.
      sumacres=0.								      sumacres=0.
      acresmax=0.								      acresmax=0.
      acresmin=0.								      acresmin=0.
c     total auth acres for each grp						c     total auth acres for each grp
      write(*,*) '..reading authorized acres by group'				      write(*,*) '..reading authorized acres by group'
      open(20,file='authacres'//year//'.txt')				      |	      open(20,file='../data/ks/gw/authacres'//year//'.txt',STATUS='OLD')
ccc      read(20,'(a)') header !added to verify columns in file (version 5h o	ccc      read(20,'(a)') header !added to verify columns in file (version 5h o
      write(*,*)								      write(*,*)
     1  'Expected fields: grp,twnshp,range,sect,tract,authacres'		     1  'Expected fields: grp,twnshp,range,sect,tract,authacres'
c        1         2         3         4         5         6         7 2	c        1         2         3         4         5         6         7 2
      sumauth=0.								      sumauth=0.
      do									      do
         read(20,*,iostat=eof) grp,twnshp,range,sect,tract,authacres		         read(20,*,iostat=eof) grp,twnshp,range,sect,tract,authacres
         if (eof.eq.-1) exit							         if (eof.eq.-1) exit

         if (grp.gt.mxGroup) mxGroup = grp					         if (grp.gt.mxGroup) mxGroup = grp
         if (grp.le.nGroups)							         if (grp.le.nGroups)
     1     authtot(grp)=authtot(grp)+authacres					     1     authtot(grp)=authtot(grp)+authacres
         sumauth=sumauth+authacres						         sumauth=sumauth+authacres
      end do									      end do
      write(*,*) 'sum authorized acres by group = ',sumauth			      write(*,*) 'sum authorized acres by group = ',sumauth
ccc      pause									ccc      pause
c      do n=1,2905								c      do n=1,2905
c          if(authtot(n).le.0.) write(*,*) n,authtot(n)				c          if(authtot(n).le.0.) write(*,*) n,authtot(n)
c      end do									c      end do
      rewind(20)								      rewind(20)

c     open files for i/o							c     open files for i/o
      write(*,*) '..reading pumping file'					      write(*,*) '..reading pumping file'
      open(13,file='pumprech'//year//'.txt')				      |	      open(13,file='../data/ks/gw/pumprech'//year//'.txt',STATUS='OLD')
ccc      read(13,'(a)') header !added to verify columns in file (version 5h o	ccc      read(13,'(a)') header !added to verify columns in file (version 5h o
      open(14,file=chout//'output'//year//'.dat')   !add path to subfolder ch |	      open(14,file=chout//'output'//year//'.dat',STATUS='UNKNOWN')   !add pat
      open(9,file=chout//'check'//year//'.out')     !add path to subfolder ch |	      open(9,file=chout//'check'//year//'.out',STATUS='UNKNOWN')     !add pat
      nout=0									      nout=0
      almenaacres=0.								      almenaacres=0.
	acresnotauthorized=0.								acresnotauthorized=0.
	gwretnotauthorized=0.								gwretnotauthorized=0.

c       initialize location arrays for groups:					c       initialize location arrays for groups:
      npd=0									      npd=0
      xpd=0									      xpd=0
      ypd=0									      ypd=0
      xpdmin=0									      xpdmin=0
      xpdmax=0									      xpdmax=0
      ypdmin=0									      ypdmin=0
      ypdmax=0									      ypdmax=0
      pdrange=0									      pdrange=0

c loop over wells and accumulate totals in work arrays				c loop over wells and accumulate totals in work arrays
c PUMPING									c PUMPING
      do									      do
c        definitions: fields read from file pumprechYYYY.txt:			c        definitions: fields read from file pumprechYYYY.txt:
c            var name | spreadsheet field name, definition (Rptd_GW_Irr_Use_Y	c            var name | spreadsheet field name, definition (Rptd_GW_Irr_Use_Y
c            grp=group_no, pdiv=pdiv_id,					c            grp=group_no, pdiv=pdiv_id,
c            (xloc,yloc) = pt of diversion projected coordinates (UTM-14 NAD2	c            (xloc,yloc) = pt of diversion projected coordinates (UTM-14 NAD2
c            rfpercent = recharge rate (return flow fraction of pumping),	c            rfpercent = recharge rate (return flow fraction of pumping),
c            county = county abbreviation (2 characters),			c            county = county abbreviation (2 characters),
c            pumploc = reported pumping (af),					c            pumploc = reported pumping (af),
c            gwloc = return flow (af) = rfpercent*pumploc,			c            gwloc = return flow (af) = rfpercent*pumploc,
c            acloc = reported irrigation acres.					c            acloc = reported irrigation acres.
c        do n=1,4689								c        do n=1,4689
         mxGroup=0								         mxGroup=0
         read(13,*,iostat=eof) grp,pdiv,xloc,yloc,rfpercent,county,		         read(13,*,iostat=eof) grp,pdiv,xloc,yloc,rfpercent,county,
     +                         pumploc,gwloc,acloc				     +                         pumploc,gwloc,acloc
         if (eof.eq.-1) exit							         if (eof.eq.-1) exit

         xoff=xloc-xg0								         xoff=xloc-xg0
         yoff=ygtop-yloc							         yoff=ygtop-yloc
         jloc=xoff/gdx+1.0							         jloc=xoff/gdx+1.0
         iloc=yoff/gdy+1.0							         iloc=yoff/gdy+1.0
         if(jloc.lt.1.or.jloc.gt.nx) then					         if(jloc.lt.1.or.jloc.gt.nx) then
            nout=nout+1								            nout=nout+1
            write(14,*) nout,iloc,jloc,' pumping'				            write(14,*) nout,iloc,jloc,' pumping'
            cycle								            cycle
ccc            pause								ccc            pause
         endif									         endif
         if(iloc.lt.1.or.iloc.gt.ny) then					         if(iloc.lt.1.or.iloc.gt.ny) then
            nout=nout+1								            nout=nout+1
            write(14,*) nout,iloc,jloc,' pumping'				            write(14,*) nout,iloc,jloc,' pumping'
            cycle								            cycle
ccc            pause								ccc            pause
         endif									         endif
         if(ibound(jloc,iloc).eq.0) then					         if(ibound(jloc,iloc).eq.0) then
            nout=nout+1								            nout=nout+1
             write(14,*) iloc,jloc,pumploc,' pumping'				             write(14,*) iloc,jloc,pumploc,' pumping'
             cycle								             cycle
         endif									         endif
         if(pumploc.gt.0.) then							         if(pumploc.gt.0.) then
            pump(jloc,iloc)=pump(jloc,iloc)+pumploc				            pump(jloc,iloc)=pump(jloc,iloc)+pumploc
         endif									         endif

         if(grp.lt.1.or.grp.gt.nGroups)						         if(grp.lt.1.or.grp.gt.nGroups)
     1     write(*,*) 'Group ',grp,' outside dimension limits.'			     1     write(*,*) 'Group ',grp,' outside dimension limits.'
         if(grp.gt.mxGroup) mxGroup = grp					         if(grp.gt.mxGroup) mxGroup = grp

c        calculate avg location of pds within each grp in active model domain	c        calculate avg location of pds within each grp in active model domain
         npd(grp)=npd(grp)+1							         npd(grp)=npd(grp)+1
         xpd(grp)=xpd(grp)+xloc							         xpd(grp)=xpd(grp)+xloc
         ypd(grp)=ypd(grp)+yloc							         ypd(grp)=ypd(grp)+yloc

         if (npd(grp).eq.1) then						         if (npd(grp).eq.1) then
           xpdmin(grp)=xloc							           xpdmin(grp)=xloc
           xpdmax(grp)=xloc							           xpdmax(grp)=xloc
           ypdmin(grp)=yloc							           ypdmin(grp)=yloc
           ypdmax(grp)=yloc							           ypdmax(grp)=yloc
         else									         else
           xpdmin(grp)=MIN(xpdmin(grp),xloc)					           xpdmin(grp)=MIN(xpdmin(grp),xloc)
           xpdmax(grp)=MAX(xpdmax(grp),xloc)					           xpdmax(grp)=MAX(xpdmax(grp),xloc)
           ypdmin(grp)=MIN(ypdmin(grp),yloc)					           ypdmin(grp)=MIN(ypdmin(grp),yloc)
           ypdmax(grp)=MAX(ypdmax(grp),yloc)					           ypdmax(grp)=MAX(ypdmax(grp),yloc)
         endif									         endif

c        total recorded gw return and irr acres for each grp			c        total recorded gw return and irr acres for each grp
         gwretloc(grp)=gwretloc(grp)+gwloc					         gwretloc(grp)=gwretloc(grp)+gwloc
         aclocfactor=1.0000000							         aclocfactor=1.0000000
         do nalmena=1,nalmenapdivs						         do nalmena=1,nalmenapdivs
            if(pdiv.eq.almenapdivs(nalmena)) then				            if(pdiv.eq.almenapdivs(nalmena)) then
               aclocfactor=almenafactor						               aclocfactor=almenafactor
               almenaacres=almenaacres+acloc					               almenaacres=almenaacres+acloc
c                write(*,*) pdiv,almenapdivs(n),acloc,almenaacres		c                write(*,*) pdiv,almenapdivs(n),acloc,almenaacres
			 if(authtot(grp).eq.0.) then							 if(authtot(grp).eq.0.) then
                     write(*,*) 'grp ',grp,					                     write(*,*) 'grp ',grp,
     |                          ' gwret ',gwret,' acloc ',acloc			     |                          ' gwret ',gwret,' acloc ',acloc
c        1         2         3         4         5         6         7 2	c        1         2         3         4         5         6         7 2
                     pause						      |	ccc                     pause
			 endif										 endif
            endif								            endif
         end do									         end do
c	   write(*,*) aclocfactor,acloc						c	   write(*,*) aclocfactor,acloc
ccc	   pause								ccc	   pause
         acresloc(grp)=acresloc(grp)+acloc*aclocfactor				         acresloc(grp)=acresloc(grp)+acloc*aclocfactor
         sumgwret=sumgwret+gwloc						         sumgwret=sumgwret+gwloc
         sumacres=sumacres+acloc*aclocfactor					         sumacres=sumacres+acloc*aclocfactor
	   if (authtot(grp).eq.0.) then							   if (authtot(grp).eq.0.) then
		  acresnotauthorized=acresnotauthorized+acloc*aclocfactor			  acresnotauthorized=acresnotauthorized+acloc*aclocfactor
		  gwretnotauthorized=gwretnotauthorized+gwloc					  gwretnotauthorized=gwretnotauthorized+gwloc
		  write(14,*) grp,gwloc,acloc,gwretnotauthorized,				  write(14,*) grp,gwloc,acloc,gwretnotauthorized,
     +          acresnotauthorized						     +          acresnotauthorized
	   endif									   endif
      end do									      end do
      close(13)									      close(13)
      write(*,*) 'sums (sumgwret, sumacres,almenaacres,'			      write(*,*) 'sums (sumgwret, sumacres,almenaacres,'
      write(*,*) '      gwretnotauthorized, acresnotauthorized): '		      write(*,*) '      gwretnotauthorized, acresnotauthorized): '
      write(*,*) sumgwret,sumacres,almenaacres,gwretnotauthorized,		      write(*,*) sumgwret,sumacres,almenaacres,gwretnotauthorized,
     +          acresnotauthorized						     +          acresnotauthorized
      write(14,*) sumgwret,sumacres,almenaacres,gwretnotauthorized,		      write(14,*) sumgwret,sumacres,almenaacres,gwretnotauthorized,
     +          acresnotauthorized						     +          acresnotauthorized

      if (mxGroup.gt.nGroups) then						      if (mxGroup.gt.nGroups) then
        write(*,*) 'WARNING: largest group no.',mxGroup,			        write(*,*) 'WARNING: largest group no.',mxGroup,
     1     ' exceeds array dimension limit nGroups,',nGroups			     1     ' exceeds array dimension limit nGroups,',nGroups
      end if									      end if

c sum the acreage used over the user groups					c sum the acreage used over the user groups
	sumtest=0.									sumtest=0.
      do grp=1,nGroups								      do grp=1,nGroups
        sumtest=sumtest+acresloc(grp)						        sumtest=sumtest+acresloc(grp)
c         centroid of each group's pds:					      |	c         centroid of each group pds:
        if (npd(grp).gt.1) then							        if (npd(grp).gt.1) then
          xpd(grp)=xpd(grp)/npd(grp)						          xpd(grp)=xpd(grp)/npd(grp)
          ypd(grp)=ypd(grp)/npd(grp)						          ypd(grp)=ypd(grp)/npd(grp)
          pdrange(grp) = SQRT((xpdmax(grp) - xpdmin(grp))**2 +			          pdrange(grp) = SQRT((xpdmax(grp) - xpdmin(grp))**2 +
     |                        (ypdmax(grp) - ypdmin(grp))**2)/5280		     |                        (ypdmax(grp) - ypdmin(grp))**2)/5280
        endif									        endif
      end do									      end do
c        1         2         3         4         5         6         7 2	c        1         2         3         4         5         6         7 2
	write(*,*) 'sum over acresloc(grp):', sumtest					write(*,*) 'sum over acresloc(grp):', sumtest
ccc      pause									ccc      pause
c     load tract coord arrays							c     load tract coord arrays
      write(*,*) '..reading coordinates of 40-acre tracts'			      write(*,*) '..reading coordinates of 40-acre tracts'
      open(21,file=trim(chstatic)//'40coords.dat') !added relative path chsta |	      open(21,file='../data/ks/40coords.dat',STATUS='OLD') !added relative pa
      continue=.true.								      continue=.true.
      do while(continue.eqv..true.)						      do while(continue.eqv..true.)
         read(21,*,iostat=eof) twnshp,range,sect,tract,x,y			         read(21,*,iostat=eof) twnshp,range,sect,tract,x,y
         if (eof.eq.-1) then							         if (eof.eq.-1) then
              continue=.false.							              continue=.false.
              exit								              exit
         else									         else
              continue=.true.							              continue=.true.
         endif									         endif
         xcoord(twnshp,range,sect,tract)=x					         xcoord(twnshp,range,sect,tract)=x
         ycoord(twnshp,range,sect,tract)=y					         ycoord(twnshp,range,sect,tract)=y
      end do									      end do
      close(21)									      close(21)

c REPORTED GW RETURN AND IRRIGATED ACRES BY TRACT				c REPORTED GW RETURN AND IRRIGATED ACRES BY TRACT

c       for output: add (x,y) coords (ft), (row,col) indices, cumul. area in 	c       for output: add (x,y) coords (ft), (row,col) indices, cumul. area in 
      open(30,file=chout//'authacres'//year//'.out')  !add subfolder path for |	      open(30,file=chout//'authacres'//year//'.out',STATUS='UNKNOWN')  !add s
      write(30,*)								      write(30,*)
     |  ' grp',' twnshp',' range',' sect',' tract', ' authacres',		     |  ' grp',' twnshp',' range',' sect',' tract', ' authacres',
     |  ' fct', ' xloc',' yloc',' row',' col',' fct=authacres/authtot',		     |  ' fct', ' xloc',' yloc',' row',' col',' fct=authacres/authtot',
     |  ' ibound(jloc,iloc)',' npd(grp)',' xpd(grp)',' ypd(grp)',		     |  ' ibound(jloc,iloc)',' npd(grp)',' xpd(grp)',' ypd(grp)',
     |  ' distpdpu_mi',' pdrange_mi',' acres(jloc,iloc)',' node'		     |  ' distpdpu_mi',' pdrange_mi',' acres(jloc,iloc)',' node'
c        1         2         3         4         5         6         7 2	c        1         2         3         4         5         6         7 2
      ygtop=yg0+ny*gdy								      ygtop=yg0+ny*gdy
      do									      do
         read(20,*,iostat=eof) grp,twnshp,range,sect,tract,authacres		         read(20,*,iostat=eof) grp,twnshp,range,sect,tract,authacres
         if (eof.eq.-1) exit     !terminate do loop upon reaching end of file	         if (eof.eq.-1) exit     !terminate do loop upon reaching end of file
c         if(gwretloc(grp).le.0.) cycle						c         if(gwretloc(grp).le.0.) cycle
         xloc=xcoord(twnshp,range,sect,tract)					         xloc=xcoord(twnshp,range,sect,tract)
         yloc=ycoord(twnshp,range,sect,tract)					         yloc=ycoord(twnshp,range,sect,tract)

         if (npd(grp).ge.1) then						         if (npd(grp).ge.1) then
           distpdpu=SQRT((xpd(grp)-xloc)**2 + (ypd(grp)-yloc)**2)/5280		           distpdpu=SQRT((xpd(grp)-xloc)**2 + (ypd(grp)-yloc)**2)/5280
         else									         else
           distpdpu=0								           distpdpu=0
         endif									         endif
c        1         2         3         4         5         6         7 2	c        1         2         3         4         5         6         7 2

         xoff=xloc-xg0								         xoff=xloc-xg0
         yoff=ygtop-yloc							         yoff=ygtop-yloc
         jloc=xoff/gdx+1.0							         jloc=xoff/gdx+1.0
         iloc=yoff/gdy+1.0							         iloc=yoff/gdy+1.0
         node=326*(iloc-1)+jloc							         node=326*(iloc-1)+jloc
         fct=authacres/authtot(grp)						         fct=authacres/authtot(grp)
c        1         2         3         4         5         6         7 2	c        1         2         3         4         5         6         7 2
         if (jloc.gt.0.and.jloc.le.nx.and.iloc.gt.0.and.iloc.le.ny)then		         if (jloc.gt.0.and.jloc.le.nx.and.iloc.gt.0.and.iloc.le.ny)then
           ibnd=ibound(jloc,iloc)						           ibnd=ibound(jloc,iloc)
         else									         else
           ibnd=-99								           ibnd=-99
         endif									         endif
c         write(14,*) n,nout,county,ibound(jloc,iloc),xoff,yoff,jloc,iloc	c         write(14,*) n,nout,county,ibound(jloc,iloc),xoff,yoff,jloc,iloc
         if(jloc.lt.1.or.jloc.gt.nx) then					         if(jloc.lt.1.or.jloc.gt.nx) then
            nout=nout+1								            nout=nout+1
            write(14,*) nout,iloc,jloc,' return flow 1'				            write(14,*) nout,iloc,jloc,' return flow 1'
            cycle								            cycle
ccc            pause								ccc            pause
         endif									         endif
         if(iloc.lt.1.or.iloc.gt.ny) then					         if(iloc.lt.1.or.iloc.gt.ny) then
            nout=nout+1								            nout=nout+1
            write(14,*) nout,iloc,jloc,'return flow '				            write(14,*) nout,iloc,jloc,'return flow '
            cycle								            cycle
ccc            pause								ccc            pause
         endif									         endif
         if(ibound(jloc,iloc).eq.0) then					         if(ibound(jloc,iloc).eq.0) then
            nout=nout+1								            nout=nout+1
            write(14,*) grp,tract,iloc,jloc,gwretloc(grp)*fct,			            write(14,*) grp,tract,iloc,jloc,gwretloc(grp)*fct,
     +        acresloc(grp)*fct,'I'						     +        acresloc(grp)*fct,'I'
            cycle								            cycle
         endif									         endif
c         if(gwretloc(grp).gt.0.) then						c         if(gwretloc(grp).gt.0.) then
c            if(acresloc(grp).eq.0) acresloc=pumploc/1.055888173		c            if(acresloc(grp).eq.0) acresloc=pumploc/1.055888173
            gwret(jloc,iloc)=gwret(jloc,iloc)+fct*gwretloc(grp)			            gwret(jloc,iloc)=gwret(jloc,iloc)+fct*gwretloc(grp)
            acres(jloc,iloc)=acres(jloc,iloc)+fct*acresloc(grp)			            acres(jloc,iloc)=acres(jloc,iloc)+fct*acresloc(grp)

         write(30,*,iostat=eof) grp,twnshp,range,sect,tract,authacres,		         write(30,*,iostat=eof) grp,twnshp,range,sect,tract,authacres,
     |     fct,xloc,yloc,iloc,jloc,fct,ibnd,npd(grp),xpd(grp),ypd(grp),		     |     fct,xloc,yloc,iloc,jloc,fct,ibnd,npd(grp),xpd(grp),ypd(grp),
     |     distpdpu,pdrange(grp),acres(jloc,iloc),node				     |     distpdpu,pdrange(grp),acres(jloc,iloc),node

      end do									      end do
      close(20)									      close(20)
      close(30)									      close(30)


c output totals to plot files							c output totals to plot files
      write(*,*) '..writing plot files'						      write(*,*) '..writing plot files'
      open(8,file=chout//'pumpgrid'//year//'.dat')  !add subfolder path for o |	      open(8,file=chout//'pumpgrid'//year//'.dat',STATUS='UNKNOWN')  !add sub
      do i=1,ny									      do i=1,ny
          do j=1,nx								          do j=1,nx
              if(ibound(j,i).ne.0.and.pump(j,i).gt.0.) then			              if(ibound(j,i).ne.0.and.pump(j,i).gt.0.) then
                xj=j								                xj=j
                xloc=(xj-0.5)*gdx+xg0						                xloc=(xj-0.5)*gdx+xg0
                yj=ny-i+1							                yj=ny-i+1
                yloc=(yj-0.5)*gdy+yg0						                yloc=(yj-0.5)*gdy+yg0
                node=nx*(i-1)+j          !node number (use to join data to gr	                node=nx*(i-1)+j          !node number (use to join data to gr
                write(8,*) xloc,yloc,pump(j,i),i,j,node				                write(8,*) xloc,yloc,pump(j,i),i,j,node
c                if(icty(j,i).gt.22.or.icty(j,i).lt.9) then			c                if(icty(j,i).gt.22.or.icty(j,i).lt.9) then
c                  write(*,*) j,i,icty(j,i)					c                  write(*,*) j,i,icty(j,i)
ccc                  pause							ccc                  pause
c                endif								c                endif
              endif								              endif
          end do								          end do
      end do									      end do
      close(8)									      close(8)
      open(8,file=chout//'gwretgrid'//year//'.dat')  !add subfolder path for  |	      open(8,file=chout//'gwretgrid'//year//'.dat',STATUS='UNKNOWN')  !add su
      do i=1,ny									      do i=1,ny
          do j=1,nx								          do j=1,nx
              if(ibound(j,i).ne.0.and.gwret(j,i).gt.0.) then			              if(ibound(j,i).ne.0.and.gwret(j,i).gt.0.) then
                xj=j								                xj=j
                xloc=(xj-0.5)*gdx+xg0						                xloc=(xj-0.5)*gdx+xg0
                yj=ny-i+1							                yj=ny-i+1
                yloc=(yj-0.5)*gdy+yg0						                yloc=(yj-0.5)*gdy+yg0
                node=nx*(i-1)+j          !node number (use to join data to gr	                node=nx*(i-1)+j          !node number (use to join data to gr
                write(8,*) xloc,yloc,gwret(j,i),i,j,node			                write(8,*) xloc,yloc,gwret(j,i),i,j,node
c                if(icty(j,i).gt.22.or.icty(j,i).lt.9) then			c                if(icty(j,i).gt.22.or.icty(j,i).lt.9) then
c                  write(*,*) j,i,icty(j,i)					c                  write(*,*) j,i,icty(j,i)
ccc                  pause							ccc                  pause
c                endif								c                endif
              endif								              endif
          end do								          end do
      end do									      end do
      close(8)									      close(8)
      open(8,file=chout//'acresgrid'//year//'.dat')  !add subfolder path for  |	      open(8,file=chout//'acresgrid'//year//'.dat',STATUS='UNKNOWN')  !add su
      do i=1,ny									      do i=1,ny
          do j=1,nx								          do j=1,nx
              if(ibound(j,i).ne.0.and.acres(j,i).gt.0.) then			              if(ibound(j,i).ne.0.and.acres(j,i).gt.0.) then
                xj=j								                xj=j
                xloc=(xj-0.5)*gdx+xg0						                xloc=(xj-0.5)*gdx+xg0
                yj=ny-i+1							                yj=ny-i+1
                yloc=(yj-0.5)*gdy+yg0						                yloc=(yj-0.5)*gdy+yg0
                node=nx*(i-1)+j          !node number (use to join data to gr	                node=nx*(i-1)+j          !node number (use to join data to gr
                write(8,*) xloc,yloc,acres(j,i),i,j,node			                write(8,*) xloc,yloc,acres(j,i),i,j,node
              endif								              endif
          end do								          end do
      end do									      end do
      close(8)									      close(8)
c output data to rrpp files							c output data to rrpp files
c pumping and groundwater recharge files					c pumping and groundwater recharge files
c      open(9,file=chout//'check.out')  !add subfolder path for output files  |	c      open(9,file=chout//'check.out',STATUS='UNKNOWN')  !add subfolder path 
      write(*,*) '..writing pmp, rcg, and agw files'				      write(*,*) '..writing pmp, rcg, and agw files'
      sumtp=0.									      sumtp=0.
      sumtr=0.									      sumtr=0.
      do im=5,10								      do im=5,10
         pumpfile=year//'.'//monthno(im)//'.pmp'				         pumpfile=year//'.'//monthno(im)//'.pmp'
         write(9,*) pumpfile							         write(9,*) pumpfile
         open(8,file=chout//pumpfile)  !add subfolder path for output files c |	         open(8,file=chout//pumpfile,STATUS='UNKNOWN')  !add subfolder path f
         sump=0.								         sump=0.
         do i=1,ny								         do i=1,ny
            do j=1,nx								            do j=1,nx
              work(j,i)=0.							              work(j,i)=0.
              if(pump(j,i).gt.0.) then						              if(pump(j,i).gt.0.) then
                ic=icty(j,i)							                ic=icty(j,i)
                if(ic.eq.0) ic=1						                if(ic.eq.0) ic=1
                factor=xmonfactors(ic,im)					                factor=xmonfactors(ic,im)
                work(j,i)=pump(j,i)*factor					                work(j,i)=pump(j,i)*factor
                sump=sump+work(j,i)						                sump=sump+work(j,i)
              endif								              endif
            end do								            end do
            write(8,'(326f6.1)') (work(j,i),j=1,nx)				            write(8,'(326f6.1)') (work(j,i),j=1,nx)
         end do									         end do
         close(8)								         close(8)

         gwrechfile=year//'.'//monthno(im)//'.rcg'				         gwrechfile=year//'.'//monthno(im)//'.rcg'
         write(9,*) gwrechfile							         write(9,*) gwrechfile
         open(8,file=chout//gwrechfile)  !add subfolder path for output files |	         open(8,file=chout//gwrechfile,STATUS='UNKNOWN')  !add subfolder path
         sumr=0.								         sumr=0.
         do i=1,ny								         do i=1,ny
            do j=1,nx								            do j=1,nx
              work(j,i)=0.							              work(j,i)=0.
              if(gwret(j,i).gt.0.) then						              if(gwret(j,i).gt.0.) then
                ic=icty(j,i)							                ic=icty(j,i)
                if(ic.eq.0) ic=1						                if(ic.eq.0) ic=1
                factor=xmonfactors(ic,im)					                factor=xmonfactors(ic,im)
                work(j,i)=gwret(j,i)*factor					                work(j,i)=gwret(j,i)*factor
                sumr=sumr+work(j,i)						                sumr=sumr+work(j,i)
              endif								              endif
            end do								            end do
            write(8,'(326f6.1)') (work(j,i),j=1,nx)				            write(8,'(326f6.1)') (work(j,i),j=1,nx)
         end do									         end do
         close(8)								         close(8)
         write(9,*) im,sump,sumr						         write(9,*) im,sump,sumr
         sumtp=sumtp+sump							         sumtp=sumtp+sump
         sumtr=sumtr+sumr							         sumtr=sumtr+sumr
      end do									      end do
c write out acres irrigated for the year					c write out acres irrigated for the year
         open(8,file=chout//year//'.agw')  !add subfolder path for output fil |	         open(8,file=chout//year//'.agw',STATUS='UNKNOWN')  !add subfolder pa
         suma=0.								         suma=0.
         do i=1,ny								         do i=1,ny
            do j=1,nx								            do j=1,nx
                suma=suma+acres(j,i)						                suma=suma+acres(j,i)
            end do								            end do
            write(8,'(326f6.1)') (acres(j,i),j=1,nx)				            write(8,'(326f6.1)') (acres(j,i),j=1,nx)
         end do									         end do
         close(8)								         close(8)
	   write(9,*) 'pumping'								   write(9,*) 'pumping'
         write(9,*) sumtp							         write(9,*) sumtp
	   write(9,*) 'gw returns'							   write(9,*) 'gw returns'
         write(9,*) sumtr							         write(9,*) sumtr
	   write(9,*) 'gw acres'							   write(9,*) 'gw acres'
         write(9,*) suma							         write(9,*) suma

c process annual m&i pumping data into model grid and into .mi file		c process annual m&i pumping data into model grid and into .mi file
      write(*,*) '..reading m&i file'						      write(*,*) '..reading m&i file'
      open(13,file=year//'mi.txt')					      |	      open(13,file='../data/ks/gw/'//year//'mi.txt',STATUS='UNKNOWN')
ccc      read(13,'(a)') header        !added to verify columns in file (versi	ccc      read(13,'(a)') header        !added to verify columns in file (versi
      sumout=0.									      sumout=0.
      nmi=0  !initialize no. m&i recs read					      nmi=0  !initialize no. m&i recs read
      do									      do
c        definitions: fields read from file 2007mi.txt:				c        definitions: fields read from file 2007mi.txt:
c            var name | spreadsheet field name, definition (Rptd_GW_Irr_Use_2	c            var name | spreadsheet field name, definition (Rptd_GW_Irr_Use_2
c            grp=group_no							c            grp=group_no
c            pdiv=pdiv_id							c            pdiv=pdiv_id
c            (xloc,yloc) = (x_mod,y_mod): pt of diversion projected coordinat	c            (xloc,yloc) = (x_mod,y_mod): pt of diversion projected coordinat
c            rfpercent = recharge rate (return flow fraction of pumping),	c            rfpercent = recharge rate (return flow fraction of pumping),
c            county = county abbreviation (2 characters),			c            county = county abbreviation (2 characters),
c            pumploc = reported pumping (af),					c            pumploc = reported pumping (af),
c            gwretmiloc = return flow (af) = rfpercent*pumploc,			c            gwretmiloc = return flow (af) = rfpercent*pumploc,
c            xnetloc = net pumping = pumploc - gwretmiloc.			c            xnetloc = net pumping = pumploc - gwretmiloc.

         read(13,*,iostat=eof) grp,pdiv,xloc,yloc,rfpercent,pumploc,		         read(13,*,iostat=eof) grp,pdiv,xloc,yloc,rfpercent,pumploc,
     +       gwretmiloc,xnetloc							     +       gwretmiloc,xnetloc

         if (eof.eq.-1) exit     !terminate do loop upon reaching end of file	         if (eof.eq.-1) exit     !terminate do loop upon reaching end of file
         nmi=nmi+1								         nmi=nmi+1
ccc         pause								ccc         pause
c         write(*,*) xloc,yloc,rfpercent,county,pumploc,			c         write(*,*) xloc,yloc,rfpercent,county,pumploc,
c     +       gwretmiloc,xnetloc						c     +       gwretmiloc,xnetloc
ccc         pause								ccc         pause
         xoff=xloc-xg0								         xoff=xloc-xg0
         yoff=ygtop-yloc							         yoff=ygtop-yloc
         jloc=xoff/gdx+1.0							         jloc=xoff/gdx+1.0
         iloc=yoff/gdy+1.0							         iloc=yoff/gdy+1.0
         inside=0								         inside=0
         if(jloc.lt.1.or.jloc.gt.nx) then					         if(jloc.lt.1.or.jloc.gt.nx) then
            nout=nout+1								            nout=nout+1
            sumout=sumout+xnetloc						            sumout=sumout+xnetloc
            write(14,*) nout,iloc,jloc,grp,pdiv,xnetloc,			            write(14,*) nout,iloc,jloc,grp,pdiv,xnetloc,
     +           sumout,' miloc'						     +           sumout,' miloc'
            cycle								            cycle
ccc            pause								ccc            pause
         endif									         endif
         if(iloc.lt.1.or.iloc.gt.ny) then					         if(iloc.lt.1.or.iloc.gt.ny) then
            nout=nout+1								            nout=nout+1
            sumout=sumout+xnetloc						            sumout=sumout+xnetloc
            write(14,*) nout,iloc,jloc,grp,pdiv,xnetloc,			            write(14,*) nout,iloc,jloc,grp,pdiv,xnetloc,
     +          sumout,' miloc'							     +          sumout,' miloc'
            cycle								            cycle
ccc            pause								ccc            pause
         endif									         endif
         if(ibound(jloc,iloc).eq.0) then					         if(ibound(jloc,iloc).eq.0) then
            nout=nout+1								            nout=nout+1
            write(14,*) nout,jloc,iloc,' miloc'					            write(14,*) nout,jloc,iloc,' miloc'
            cycle								            cycle
         endif									         endif
         if(xnetloc.gt.0.) then							         if(xnetloc.gt.0.) then
            pumpmi(jloc,iloc)=pumpmi(jloc,iloc)+xnetloc				            pumpmi(jloc,iloc)=pumpmi(jloc,iloc)+xnetloc
         endif									         endif
      end do									      end do
      close(13)									      close(13)
      write(*,*) nmi,' m&i recs read'						      write(*,*) nmi,' m&i recs read'

c output totals to plot files							c output totals to plot files
      write(*,*) '..write mi plot file'						      write(*,*) '..write mi plot file'
      open(8,file=chout//year//'migrid.dat')  !add subfolder path for output  |	      open(8,file=chout//year//'migrid.dat',STATUS='UNKNOWN')  !add subfolder
      do i=1,ny									      do i=1,ny
          do j=1,nx								          do j=1,nx
              if(ibound(j,i).ne.0.and.pumpmi(j,i).gt.0.) then			              if(ibound(j,i).ne.0.and.pumpmi(j,i).gt.0.) then
                xj=j								                xj=j
                xloc=(xj-0.5)*gdx+xg0						                xloc=(xj-0.5)*gdx+xg0
                yj=ny-i+1							                yj=ny-i+1
                yloc=(yj-0.5)*gdy+yg0						                yloc=(yj-0.5)*gdy+yg0
                node=nx*(i-1)+j          !node number (use to join data to gr	                node=nx*(i-1)+j          !node number (use to join data to gr
                write(8,*) xloc,yloc,pumpmi(j,i),i,j,node			                write(8,*) xloc,yloc,pumpmi(j,i),i,j,node
c                if(icty(j,i).gt.22.or.icty(j,i).lt.9) then			c                if(icty(j,i).gt.22.or.icty(j,i).lt.9) then
c                  write(*,*) j,i,icty(j,i)					c                  write(*,*) j,i,icty(j,i)
ccc                  pause							ccc                  pause
c                endif								c                endif
              endif								              endif
          end do								          end do
      end do									      end do
      close(8)									      close(8)
c write out m&i pumping for the year						c write out m&i pumping for the year
         open(8,file=chout//year//'.mi')  !add subfolder path for output file |	         open(8,file=chout//year//'.mi',STATUS='UNKNOWN')  !add subfolder pat
         suma=0.								         suma=0.
         do i=1,ny								         do i=1,ny
            do j=1,nx								            do j=1,nx
                suma=suma+pumpmi(j,i)						                suma=suma+pumpmi(j,i)
            end do								            end do
            write(8,'(326f6.1)') (pumpmi(j,i),j=1,nx)				            write(8,'(326f6.1)') (pumpmi(j,i),j=1,nx)
         end do									         end do
         close(8)								         close(8)

	   write(9,*) 'm&i pumping'							   write(9,*) 'm&i pumping'
         write(9,*) suma							         write(9,*) suma
	stop										stop
	end										end
