      subroutine FilterNeuralNetRegres(idowhat,nx0,nobs0,nh0,
     &xsamp0,ysamp0,yatxsamp0,rsq,w,bph,q,bend,xnew,ypred)
      implicit none
!
!=============Inputs regardless of idowhat=========================
!idowhat: =1, fit the data and estimate the coefficients. Provide the
!             initial guess for the coefficients or set bend to -9999
!         =2, coefficients are already available, calculate y at xnew
!nx0: the number of independent (x) variables
!nobs0: the total number of samples
!nh0: the total number of hidden nodes to use. One hidden layer is
!     assumed.
!============When idowhat=1========================================
!       --------Inputs--------
!xsamp0: the values of the independent (x) variables
!ysamp0: the values of the dependent (y) variable. y is one dimension.
!       --------Outputs-------
!w: the slope coefficient to time the normalized x in the activation function
!bph: the intercept coefficient in the activation function
!q: the coefficient to time the value of the activation function
!bend: the residual constant in the neural network regression
!yatxsamp0: the predicted y value at xsamp0
!rsq: R squared
!============When idowhat=2=========================================
!       --------Inputs--------
!w: the slope coefficient to time the normalized x in the activation function
!bph: the intercept coefficient in the activation function
!q: the coefficient to time the value of the activation function
!bend: the residual constant in the neural network regression
!xnew: the new x point who y value is to be estimated (when idowhat=2)
!       --------Outputs-------
!ypred: the predicted y value at xnew
!
      integer idowhat,nx0,nobs0,nh0
      double precision xsamp0(nobs0,nx0),ysamp0(nobs0),
     &  yatxsamp0(nobs0),rsq,w(nx0,nh0),bph(nh0),q(nh0),
     &  bend,xnew(nx0),ypred,fn9999,dif_y(nobs0)
      parameter(fn9999=-9999.0d0)
!============Locals=========================================
      integer i,j,m,n,nobs,isoutlier_2sides
!
      if(idowhat.eq.1)then
        nobs=nobs0
10      n=0
        do i=1,nobs
          if(dabs(ysamp0(i)-fn9999).gt.1.0d-6)then
            n=n+1
            ysamp0(n)=ysamp0(i)
            do j=1,nx0
              xsamp0(n,j)=xsamp0(i,j)
            enddo
          endif
        enddo
        nobs=n
        call NeuralNetRegres(idowhat,nx0,nobs,nh0,xsamp0(1:nobs,1:nx0),
     &ysamp0,yatxsamp0,rsq,w(1:nx0,1:nh0),bph,q,bend,xnew,ypred)
        do i=1,nobs
          dif_y(i)=ysamp0(i)-yatxsamp0(i)
        enddo
        m=isoutlier_2sides(nobs,dif_y)
        if(m.gt.0)then
          ysamp0(m)=fn9999
          goto 10
        endif
        return
      endif
      if(idowhat.eq.2)call NeuralNetRegres(idowhat,nx0,nobs0,nh0,
     &xsamp0(1:nobs0,1:nx0),ysamp0,yatxsamp0,rsq,w(1:nx0,1:nh0),bph,
     &q,bend,xnew,ypred)
      return
      end
!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
