      subroutine y_aPLUSbx(npoints0,x0,y0,a,b)
      implicit none
!fit for y=a+bx
      integer npoints0
      double precision x0(npoints0),y0(npoints0),a,b
      integer i,npoints
      double precision xmean,ymean,lxx,lyy,lxy,fn9999,tiny,
     &x(npoints0),y(npoints0)
      parameter(fn9999=-9999.0d0,tiny=1.0d-7)

      npoints=0
      do i=1,npoints0
        if(dabs(x0(i)-fn9999).gt.tiny.and.
     &dabs(y0(i)-fn9999).gt.tiny)then
          npoints=npoints+1
          x(npoints)=x0(i)
          y(npoints)=y0(i)
        endif
      enddo
      xmean=0.0d0
      ymean=0.0d0
      do i=1,npoints
        xmean=xmean+x(i)
        ymean=ymean+y(i)
      enddo
      xmean=xmean/dble(npoints)
      ymean=ymean/dble(npoints)
      lxx=0.0d0
      lyy=0.0d0
      lxy=0.0d0
      do i=1,npoints
        lxx=lxx+(x(i)-xmean)**2
        lyy=lyy+(y(i)-ymean)**2
        lxy=lxy+(x(i)-xmean)*(y(i)-ymean)
      enddo
      if(lxx.ne.0.0d0)then
        b=lxy/lxx
        a=ymean-b*xmean
      else
        b=-9999.0d0
        a=-9999.0d0
      endif
      return
      end
