!============================================================================
!                       General Documentation Section
! Name:
!   LAMBDA_LOG_DISP_WRAP
!
! Purpose:
!   A main program "wrapper" that executes LAMBDA_LOG_DISP using parameters 
!   that are read from stdin, and write out output to stdout.  One purpose 
!   of this program is to allow IDL to call the executable of this program 
!   using SPAWN.
!
! Calling Sequence:
!   Main program.
!
! Example:
!   None.
!
! File I/O:
!   Read/write to/from stdin/stdout.
!
! Entry and Exit States:
!   N/A.
!
! RCS Information:
!   $Id: lambda_log_disp_wrap.f90,v 1.1 2003/04/04 04:39:27 jlin Exp jlin $
!
! Revision History:
! - 27 May 2002:  Original by Johnny Lin, CIRES/University of Colorado, 
!   Boulder.  Email:  air_jlin@yahoo.com.  Passed minimally reasonably
!   passable tests.
!
! Notes:
! - Subprograms required:  LAMBDA_LOG_DISP.
! - Because all I/O is via stdin/stdout pipe interface, numeric input and 
!   output will be truncated to the default formatting settings of stdin/
!   stdout.
! - There were some problems with IDL reading the array write(*,*) state-
!   ments if the writing was using an implicit do loop, so for both the
!   read/writes to/from stdin/stdout, I use an explicit single elemment 
!   read/write.
!
! Copyright (c) 2002 by Johnny Lin.  For licensing and contact information
! see http://www.johnny-lin.com/flib.html.
!----------------------------------------------------------------------------
!                Variable Declaration and Description Section

! Overall Routine Declaration:
    program lambda_log_disp_wrap
    implicit none

! Include Files:
!   None.

! Modules Used and Module Variables Changed by Program:
!   None.

! Parameters:
!   None.

! Input Variables:
    real, allocatable    :: x0(:)   !- data vector
    integer, allocatable :: k(:)    !- evol. time vector
    integer :: nx0                  !- num. data vector elements
    integer :: nk                   !- num. evol. time vector elements
    integer :: m                    !- embedding time
    integer :: L                    !- delay time
    integer :: ie_value             !- ruler initialization seed
    integer :: tan_motion_lim       !- limit to remove tang. motion effects
    integer :: normalize_input      !- flag to normalize input

! Output Variables:
    real, allocatable    :: out_lambda(:,:)     !- lambda
    real, allocatable    :: out_log_disp(:,:)   !- log disp. curve values
    integer, allocatable :: out_npts(:,:)       !- number pts. in avging.
    real                 :: out_shells(9)       !- shell boundaries

! Local Variables:
    integer :: i, j     !- usually used for array indices
    integer :: status   !- status variable

! Interface Blocks:
    interface
       subroutine lambda_log_disp( x0, k, m, L, ie_value  &
                                 , tan_motion_lim  &
                                 , normalize_input  &
                                 , out_lambda, out_log_disp  &
                                 , out_npts, out_shells )
          real, intent(in) :: x0(:)                     !- input arguments
          integer, intent(in) :: k(:)
          integer, intent(in) :: m
          integer, intent(in) :: L
          integer, intent(in) :: ie_value
          integer, intent(in) :: tan_motion_lim
          integer, intent(in) :: normalize_input
          real, intent(out) :: out_lambda(SIZE(k),9)    !-output arguments
          real, intent(out) :: out_log_disp(SIZE(k),9)
          integer, intent(out) :: out_npts(SIZE(k),9)
          real, intent(out) :: out_shells(9)
       end subroutine lambda_log_disp
    end interface
!============================================================================




! --- Read-In Array Bounds, Arrays, and Allocate Input Allocatable Arrays ---

read(*,*) nx0                       !- read-in no. of pts. in timeseries
allocate( x0(nx0), Stat=status )    !- allocate array
if (status .ne. 0) then             !- check allocation worked
   write(*,*) 'error--bad alloc'
   stop
endif
do i=1,nx0                          !- read-in data vector
   read(*,*) x0(i)
enddo

read(*,*) nk                        !- read-in actual no. of evolution times
allocate( k(nk), Stat=status )      !- allocate array
if (status .ne. 0) then             !- check allocation worked
   write(*,*) 'error--bad alloc'
   stop
endif
do i=1,nk                           !- read-in evolution times vector
   read(*,*) k(i)
enddo




! -------------------- Allocate Output Allocatable Arrays -------------------

allocate( out_lambda(nk,9), out_log_disp(nk,9), out_npts(nk,9), Stat=status )

if (status .ne. 0) then             !- check allocation worked
   write(*,*) 'error--bad alloc'
   stop
endif




! ------------------------ Read-In Initial Parameters -----------------------

read(*,*) m
read(*,*) L
read(*,*) ie_value
read(*,*) tan_motion_lim
read(*,*) normalize_input




! --------------------- Call LAMBDA_LOG_DISP Subroutine ---------------------

call LAMBDA_LOG_DISP( x0, k, m, L, ie_value  &
                    , tan_motion_lim  &
                    , normalize_input  &
                    , out_lambda, out_log_disp  &
                    , out_npts, out_shells )




! ------------------------- Write Results to Stdout -------------------------

do j=1,9                              !- write out_lambda
   do i=1,nk
      write(*,*) out_lambda(i,j)
   enddo
enddo

do j=1,9                              !- write out_log_disp
   do i=1,nk
      write(*,*) out_log_disp(i,j)
   enddo
enddo

do j=1,9                              !- write out_npts
   do i=1,nk
      write(*,*) out_npts(i,j)
   enddo
enddo

do i=1,9                              !- write out_shells
   write(*,*) out_shells(i)
enddo




! --------------------- Dellocate All Allocatable Arrays --------------------

deallocate( x0, k, out_lambda, out_log_disp, out_npts, Stat=status )

if (status .ne. 0) then             !- check allocation worked
   write(*,*) 'error--bad alloc'
   stop
endif




end program lambda_log_disp_wrap

!======== end of file ========
