!======================================================================
SUBROUTINE GetAllFieldValues( grdI,nAreas,maxRec,nRec,xRec,yRec,srfFldVal, &
                              lDep,isMult )

USE sagdef_fd
USE sagstr_fd
USE sagerr_fd
USE PtrGrdStrItf
USE extract_fi
USE multcomp_fd
USE scipuff_fi , ONLY: ntypm, material,mat_mc
USE plotlist_fi

IMPLICIT NONE

INTEGER,                                INTENT( INOUT ) :: grdI
INTEGER,                                INTENT( IN    ) :: nAreas
INTEGER,                                INTENT( IN    ) :: maxRec
INTEGER, DIMENSION(nAreas),             INTENT( IN    ) :: nRec
REAL, DIMENSION(nAreas,maxRec),         INTENT( IN    ) :: xRec, yRec
REAL, DIMENSION(nAreas,maxRec,nKind,2), INTENT( INOUT ) :: srfFldVal
LOGICAL,                                INTENT( IN    ) :: lDep, isMULT

TYPE( SAGgrid_str ), POINTER :: grd

INTEGER irv, astat, ios
INTEGER i, j, iKind, iMult, iSp
INTEGER nFld

INTEGER, DIMENSION(:),ALLOCATABLE :: ifld
REAL,    DIMENSION(:),ALLOCATABLE :: fpnt

REAL    xpnt, ypnt

INTEGER, EXTERNAL :: SAG_ReadHeaderID, SAG_ReadGridID, SAG_ReadMCDataID
INTEGER, EXTERNAL :: SAG_BottomValueID
INTEGER, EXTERNAL :: output_groups

CALL init_error()

grd => SAG_PtrGrdStr( grdI ) ! Associate "local" grid structure pointer

irv = SAG_ReadHeaderID( grdI )

IF( irv == SAG_EOF )GOTO 9999
IF( irv /= SAG_OK )THEN
  nError   = UK_ERROR
  eRoutine = 'InitGrid'
  eMessage = 'Error reading SAG file'
  CALL ReportFileName( eInform,'File=',grd%file )
  GOTO 9999
END IF

!------ Allocate grid and data fields for ncells

grd%mxgrd = grd%ncells
grd%mxfld = grd%nvart  ! All variables
grd%naux  = 0          ! turn off any auxiliary fields

ALLOCATE( grd%ipgrd(grd%mxgrd),STAT=ios )
IF( ios /= 0 )THEN
  nError   = UK_ERROR
  eRoutine = 'ReadSrfField'
  eMessage = 'Error allocating surface grid arrays'
  GOTO 9999
END IF

ALLOCATE( grd%ipdat(grd%mxgrd*grd%mxfld),STAT=ios )
IF( ios /= 0 )THEN
  nError   = UK_ERROR
  eRoutine = 'ReadSrfField'
  eMessage = 'Error allocating surface grid arrays'
  GOTO 9999
END IF

!------ Read grid

irv = SAG_ReadGridID( grdI )
IF( irv /= SAG_OK )THEN
  nError   = UK_ERROR
  eRoutine = 'ReadSrfField'
  eMessage = 'Error reading surface grid'
  GOTO 9999
END IF

!------ Read specified fields

irv = SAG_ReadMCDataID( grdI ) ! All fields
IF( irv /= SAG_OK )THEN
  nError   = UK_ERROR
  eRoutine = 'ReadSrfField'
  eMessage = 'Error reading surface grid'
  GOTO 9999
END IF

nfld = grd%mxfld

ALLOCATE(ifld(nfld),fpnt(nfld),STAT=astat)

DO i = 1,nfld
  ifld(i) = i
END DO

irv = SAG_BottomValueID( grdI,nfld,ifld )

DO i = 1,nAreas
  DO j = 1,nRec(i)

    xpnt = xRec(i,j)
    ypnt = yRec(i,j)

    fpnt = 0.

    CALL GetFieldMCValue( grdI,Field,PlotType,xpnt,ypnt,fpnt )
    IF( nError /= NO_ERROR )THEN
      GOTO 9999
    END IF

    IF( isMult )THEN

      IF( lDep )THEN

        iMult = grd%nvart - 2*nKind

        DO iKind = 1,nKind
          iSp = 2*iKind - 1 + iMult
          srfFldVal(i,j,iKind,1) = fpnt(iSp)     ! Dry
          srfFldVal(i,j,iKind,2) = fpnt(iSp+1)   ! Wet
        END DO

      ELSE

        iMult = grd%nvart - nKind

        DO iKind = 1,nKind
          iSp = iMult + iKind
          srfFldVal(i,j,iKind,1) = fpnt(iSp)     ! Conc
        END DO

      END IF

    ELSE

      srfFldVal(i,j,1,1) = fpnt(1)

    END IF

  END DO

END DO

9999 CONTINUE

IF( ALLOCATED(ifld) )DEALLOCATE(ifld,fpnt,STAT=astat)

IF( ASSOCIATED(grd%ipgrd) )THEN
  DEALLOCATE(grd%ipgrd,STAT=astat)
  NULLIFY(grd%ipgrd)
END IF

IF( ASSOCIATED(grd%ipdat) )THEN
  DEALLOCATE(grd%ipdat,STAT=astat)
  NULLIFY(grd%ipdat)
END IF

grd%mxgrd = 0

RETURN
END
