c     Error handling subroutines
c
c     Find the type of error that occurred and call
c     the appropriate subroutine
      subroutine errHand(nErrLog,ierr,errStr)
      implicit none
      integer nErrLog,ierr
      character*80 errStr
c
      if (ierr.gt.0.and.ierr.lt.10) then
        call rdFileErr(nErrLog,ierr,errStr)
      elseif (ierr.ge.10.and.ierr.lt.20) then
        call wrtErr(nErrLog,ierr,errStr)
      elseif (ierr.ge.20.and.ierr.lt.30) then
        call geomErr(nErrLog,ierr,errStr)
      elseif (ierr.ge.30.and.ierr.lt.40) then
        call missData(nErrLog,ierr,errStr)
      elseif (ierr.gt.60.and.ierr.lt.70) then
        call overData(ierr)
      elseif (ierr.ge.70.and.ierr.lt.80) then
        call numerErr(nErrLog,ierr)
      elseif (ierr.ge.80.and.ierr.lt.90) then
        call specErr(nErrLog,ierr)
      elseif (ierr.ge.90.and.ierr.lt.100) then
        call timeErr(nErrLog,ierr,errStr)
      endif
      return
      end

      subroutine countErr(nErrLog,ierr,errStr,compCnt)
      implicit none
      integer nErrLog,ierr,compCnt
      character*80 errStr
c
      if (ierr.eq.73 .and. compCnt.lt.30) then
        errStr='50 iteration threshold exceeded in N compensation'
        goto 900
      endif
      return
c     write the error string for less than 30 lines
900   write(nErrLog,110) errStr
      return
110   format(A80)
      end

      subroutine rdFileErr(nErrLog,ierr,errStr)
      implicit none
      character*80 errStr
      integer nErrLog,ierr
c
      if (ierr.eq.1) then
        write(nErrLog,110) errStr
      elseif (ierr.eq.2) then
        write(nErrLog,120) errStr
      elseif (ierr.eq.3) then
        write(nErrLog,130) errStr
      elseif (ierr.eq.4) then
        write(nErrLog,140) errStr
      elseif (ierr.eq.5) then
        write(nErrLog,150) errStr
      elseif (ierr.eq.6) then
        write(nErrLog,160) errStr
      elseif (ierr.eq.8) then
        write(nErrLog,180) errStr
      elseif (ierr.eq.9) then
        write(nErrLog,190)
      endif
      stop
110   format(' Failure to open file ',A20)
120   format(' Incorrect number of entries in file ',A20)
130   format(' Read error in file ',A20)
140   format(' Incorrect number of lines in file ',A20)
150   format(' Incorrect date format specification in file ',
     ;       A20)
160   format(' Incorrect specification of ID numbers in ',
     ;       A20)
180   format(' Material IDs do not match with Soil.in ',A20)
190   format(
     ;' DataPath.txt file must be in same folder as executable')
      end

      subroutine overData(ierr)
      implicit none
      include "defs.fi"
      integer ierr
c
c     nErrLog defined in defs.fi
      if (ierr.eq.61) then
        write(nErrLog,110) nTOutD
      endif
      stop
110   format(' Size overrun: Output times specified > ',I5)
      end

      subroutine wrtErr(nErrLog,ierr,errStr)
      implicit none
      integer nErrLog,ierr
      character*80 errStr
      if (ierr.eq.10) then
        write(nErrLog,110) errStr
      endif
      ierr=0
      return
110   format(' Write to file error for ',A20)
      end

      subroutine missData(nErrLog,ierr,errStr)
      implicit none
      integer nErrLog,ierr
      character*80 errStr
      if(ierr.eq.30) then
        errStr='Crop ID number error may be CropSched.in or Plant.in'
        goto 900
      elseif (ierr.eq.31) then
        errStr=
     ;  'Crop schedule error possible out of order dates, CropSched.in'
        goto 900
      endif
900   write(nErrLog,110) errStr
      stop
110   format(A80)
      end

      subroutine numerErr(nErrLog,ierr,errStr)
      implicit none
      integer nErrLog,ierr
      character*80 errStr
      if(ierr.eq.70) then
        errStr='Convergence error in nitrate uptake, check Bound.in'
        goto 910
      elseif(ierr.eq.71) then
        errStr='Poor convergence in water flow module'
        goto 900
      elseif(ierr.eq.72 .or. ierr.eq.73) then
        errStr='Computed actual N uptake exceeds potential'
        goto 910
      endif
900   write(nErrLog,110) errStr
      return
910   write(nErrLog,110) errStr
      stop
110   format(A80)
      end

      subroutine geomErr(nErrLog,ierr,errStr)
      implicit none
      integer nErrLog,ierr
      character*80 errStr
      if(ierr.eq.20) then
        errStr='Geometry error, check depth specs, Basic.in'
        goto 900
      endif
      return
900   write(nErrLog,110) errStr
      stop
110   format(A80)
      end

      subroutine specErr(nErrLog,ierr)
      implicit none
      integer nErrLog,ierr
      character*80 errStr
      if(ierr.eq.80) then
        errStr='Specification error for solution ID numbers'
        goto 900
      endif
      if(ierr.eq.81) then
        errStr=
     ; 'Fertilization event ID exceeds number of events'
        goto 900
      endif
      if(ierr.eq.82) then
        errStr=
     ;  'Number of sets of soil properties exceeds maximum'
      endif
      if(ierr.eq.83) then
        errStr=
     ;'Incorrect value h threshold or hPWP,Plant.in'
      endif
	  if(ierr.eq.84) then
        errStr=
     ;'Initial moisture content: 1-head, 2-water content,Init.IN'
      endif
      return
900   write(nErrLog,110) errStr
      stop
110   format(A80)
      end

      subroutine timeErr(nErrLog,ierr,errStr)
      implicit none
      integer nErrLog,ierr
      character*80 errStr,errMsg1,errMsg2
      if(ierr.eq.90) then
        errMsg1='Non-sequential specification of output times'
        goto 900
      endif
      if(ierr.eq.91) then
        errMsg1='Non-sequential boundary condition times'
        goto 900
      endif
c
c     Same error could occur in either Bound.in or Input.in
      if(ierr.eq.92 .or. ierr.eq.93) then
        errMsg1='Date format wrong or incompatible TInit?'
        errMsg2='Correct date format is mm/dd/yyyy'
        goto 902
      endif
      return
900   write(nErrLog,110) errMsg1
      stop
902   write(nErrLog,120) errMsg1,errMsg2,errStr
      stop
110   format(A80)
120   format(A80,/,A80,/,'Filename: ',A80)
      end