      SUBROUTINE SACO_BASIS_elev(KD0,SECKD0,ITER0,DELT,IN,X,Y,NN,NE)
C-----------------------------------------------------------------------
C-----------------------------------------------------------------------
      INCLUDE 'CASCO.DIM'
      INCLUDE 'observes.h'
      INCLUDE 'weights.h'
C
C Argument Declaration
C
C   FEM Arrays
      INTEGER IN(3,NEDIM)
      REAL X(NNDIM),Y(NNDIM)
C
C   Horizontal, temporal, and vertical weights at observation locations
      REAL BXY(3)
      write(*,*)'sbe',KD0,seckd0,KD_EOBS(1),SEC_EOBS(1)
C
C Ensure routine is called with ITER=0
      IF(ITER0.NE.0)THEN
         WRITE(*,'(3A)')'SUBROUTINE SACO_BASIS_ELEV MUST BE CALLED ',
     &   'WITH TIMING PARAMETERS WHICH CORRESPOND TO THE BEGINNING OF',
     &   'THE FORWARD MODEL'
         STOP
      ENDIF
C
C Observation Loop
      DO II=1,n_eobs
C
C   Determine horizontal weights
         CALL BASIS2D(NNDIM,NEDIM,IN,X,Y,NN,NE,
     &	              x_eobs(II),y_eobs(II),NCLOSEST,NELEM,BXY)
         IF(NELEM.EQ.0)THEN
            WRITE(*,*)'OBSERVATIONS LIE OUTSIDE HORIZONTAL DOMAIN'
            STOP
         ENDIF
         ebasisobs(1,II)=FLOAT(NELEM)
         ebasisobs(2,II)=BXY(1)
         ebasisobs(3,II)=BXY(2)
         ebasisobs(4,II)=BXY(3)
      ENDDO
C
C Initialize for temporal loop
      KD=KD0
      SECKD=SECKD0
      ITER=ITER0
      IOBS=1
C Temporal loop to determine temporal weights
      DO WHILE(IOBS.LE.n_eobs) 
         DTSEC=(KD_EOBS(IOBS)-KD)*24.0*3600.0+SEC_EOBS(IOBS)-SECKD
         IF(DTSEC.LT.0.0)THEN
         ELSEIF(DTSEC.GE.DELT)THEN
            ITER=ITER+1
            SECKD=SECKD+DELT
            IF(SECKD.GE.86400.)CALL UP_DATE(KD,SECKD)
         ELSE
            ebasisobs(5,IOBS)=real(ITER+1)
            ebasisobs(6,IOBS)=real(DTSEC/DELT)
            IOBS=IOBS+1
         ENDIF
      ENDDO
      
c      open(59,FILE='sacobasis.elev.dat')
c      do i=1,n_eobs
c       write(59,'(i6,f10.2,f10.2,f10.2,i6,f10.2)')int(ebasisobs(1,i)),
c     &ebasisobs(2,i),ebasisobs(3,i),
c     &ebasisobs(4,i),int(ebasisobs(5,i)),ebasisobs(6,i)
c      enddo
c      close(59)
      
      RETURN
      END
C***********************************************************************
      SUBROUTINE saco_eerror(ITER,IN,elev)
C-----------------------------------------------------------------------
C Reports the unexplained observations. 
C    Observations - Model      (I think CGH Nov 98) 
C-----------------------------------------------------------------------
C-----------------------------------------------------------------------
      INCLUDE 'CASCO.DIM'
      INCLUDE 'observes.h'
      INCLUDE 'errors.h'
      INCLUDE 'weights.h'
C
C Argument Declaration
C
C   FEM Arrays
      INTEGER IN(3,NEDIM)
      REAL Elev(NNDIM),elevm
C
C   Horizontal basis functions
      REAL BXY(3)
C
C Save necessary arguments
      SAVE IIeOBS,IIeOBSDONE
C
C Initialize IIeOBS during first iteration
      IF(ITER.EQ.0)THEN
         IIeOBS=1
         IIeOBSDONE=0
      ENDIF
C
C Return if iteration number precedes that required for the first
C observational location or is after that required for the last
C observational location 
c      write(*,*)'saco_eerror:',ITER,INT(EBASISOBS(5,1))-1,
c     &IIeOBSDONE,N_EOBS
      IF(ITER.LT.INT(EBASISOBS(5,1))-1)RETURN
      IF(IIeOBSDONE.EQ.N_EOBS)RETURN
C
C Perform calculations
C
C  SACO time level just after observational location
      DO II=IIeOBSDONE+1,IIeOBS-1
      
         NELEM=INT(ebasisobs(1,II))
         BXY(1)=ebasisobs(2,II)
         BXY(2)=ebasisobs(3,II)
         BXY(3)=ebasisobs(4,II)
         TWEIGHT=ebasisobs(6,II)
	 elevm=tweight*(BXY(1)*elev(in(1,nelem))+
     &                  BXY(2)*elev(in(2,nelem))+
     &                  BXY(3)*elev(in(3,nelem)))
         eerror(II)=eobs(II)-(eerror(II)+elevm)
      ENDDO
      IIeOBSDONE=IIeOBS-1
C
C  SACO time level just prior to observational location
 2    IF(ITER.EQ.INT((ebasisobs(5,IIeOBS)-1)))THEN
         NELEM=INT(ebasisobs(1,IIeOBS))
         BXY(1)=ebasisobs(2,IIeOBS)
         BXY(2)=ebasisobs(3,IIeOBS)
         BXY(3)=eBASISOBS(4,IIeOBS)
         TWEIGHT=1.0-ebasisobs(6,IIeOBS)
	 elevm=tweight*(BXY(1)*elev(in(1,nelem))+
     &                  BXY(2)*elev(in(2,nelem))+
     &                  BXY(3)*elev(in(3,nelem)))
     
         eerror(IIeOBS)=elevm
         IIeOBS=IIeOBS+1
         GO TO 2
      ENDIF
      RETURN
      END
C***********************************************************************
C***********************************************************************
