module planefit_mod

  use adj_mod

  implicit none

  integer,              private, save          :: nd
  real, dimension(:,:), private, save, pointer :: xy

contains

  ! --------------------------------------------------------------------------------	
  subroutine planefit_init( xy_in, nd_in )
    integer                        :: nd_in
    real, dimension(:,:)           :: xy_in

    nd = nd_in
    allocate( xy(nd,2) )
    xy = xy_in

  end subroutine planefit_init
  ! --------------------------------------------------------------------------------	

  ! --------------------------------------------------------------------------------	
  integer function planefit_lop( adj, add, coeff, zdata )
    integer                        :: id
    logical, intent (in)           :: adj, add
    real                           :: x, y
    real, dimension(:)             :: zdata, coeff

    call adjnull( adj, add, coeff, zdata)

    ! ------------------------------------------------------------------------------	
    ! !!!!!!!!!!!!!!!! HEY, LOOK HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    ! ------------------------------------------------------------------------------	
    !
    !   | x1 y1 1 | |A|   |z1|
    !   | x2 y2 1 | |B| = |z2|
    !   | x3 y3 1 | |C|   |z3|      Code this linear operation and its adjoint!
    !   |         |       |  |
    !   | |  |  | |       ||||
    !   | V  V  V |       |VV|
    !   |         |       |  |
    !   | xn yn 1 |       |zn|
    !
    ! ------------------------------------------------------------------------------	

    do id=1,nd
       x = xy(id,1);  y = xy(id,2); 

       if( adj) then

       else

       end if

    end do

    planefit_lop = 0
  end function planefit_lop
  ! --------------------------------------------------------------------------------	

  ! --------------------------------------------------------------------------------	
  subroutine planefit_close()
    deallocate( xy)
  end subroutine planefit_close
  ! --------------------------------------------------------------------------------	

end module
