#include "OPENAD_OPTIONS.h"

      subroutine template()
C     !FUNCTIONS
      use OAD_tape
      use OAD_rev
      use OAD_cp


! original arguments get inserted before version
!         ! and declared here together with all local variables
!         ! generated by xaifBooster

!use mode_variables

!$TEMPLATE_PRAGMA_DECLARATIONS
      type(modeType) :: our_orig_mode

      integer iaddr
      external iaddr

!LOCAL VARIABLES:
      CHARACTER*(2) adpref
      CHARACTER*(80) fname
      INTEGER il
      INTEGER myNr
      LOGICAL useCurrentDir
      Real*8  active_var_p(1-OLx:sNx+OLx,1-OLy:sNy+OLy,Nr,nSx,nSy)
      INTEGER  ILNBLNK
      EXTERNAL ILNBLNK

#ifdef ALLOW_OPENAD_ACTIVE_READ_XYZ

      myNr = Nr
      useCurrentDir = .FALSE.

      if (our_rev_mode%plain) then
         our_orig_mode = our_rev_mode
! original function
         active_var_p = active_var%v
! set up for plain execution
         our_rev_mode%arg_store=.FALSE.
         our_rev_mode%arg_restore=.FALSE.
         our_rev_mode%plain=.TRUE.
         our_rev_mode%tape=.FALSE.
         our_rev_mode%adjoint=.FALSE.
         call ACTIVE_READ_3D_RL(
     &        active_var_file, active_var_p, doglobalread,
     &        useCurrentDir, lAdInit, iRec, myNr,
     &        FORWARD_SIMULATION, myOptimIter, myThid )
! reset the mode
         our_rev_mode=our_orig_mode
! copy back
         active_var%v = active_var_p
      end if

      if (our_rev_mode%tape) then
! taping
         our_orig_mode=our_rev_mode
         our_rev_mode%arg_store=.FALSE.
         our_rev_mode%arg_restore=.FALSE.
         our_rev_mode%plain=.TRUE.
         our_rev_mode%tape=.FALSE.
         our_rev_mode%adjoint=.FALSE.
         if (oad_st_sz.lt.oad_st_ptr) call oad_st_grow()
         oad_st(oad_st_ptr) = active_var_file 
         oad_st_ptr = oad_st_ptr+1 
         if (oad_it_sz.lt.oad_it_ptr) call oad_it_grow()
         oad_it(oad_it_ptr) = iRec
         oad_it_ptr = oad_it_ptr+1 
         call ACTIVE_READ_3D_RL(
     &        active_var_file, active_var, doglobalread,
     &        useCurrentDir, lAdInit, iRec, myNr,
     &        FORWARD_SIMULATION, myOptimIter, myThid )
         our_rev_mode=our_orig_mode
      end if

      if (our_rev_mode%adjoint) then
! adjoint
         oad_st_ptr = oad_st_ptr-1
         active_var_file = oad_st(oad_st_ptr) 
         oad_it_ptr = oad_it_ptr-1
         iRec = oad_it(oad_it_ptr) 
         adpref = 'ad'
         il   = ILNBLNK( active_var_file )
         WRITE(fname(1:80),'(A)') ' '
         WRITE(fname(1:2+il),'(2A)') adpref, active_var_file(1:il)
!         WRITE(fname(1:2+il),'(2A)') adpref, active_var_file
         active_var_p = active_var%d
! set up for plain execution
         our_orig_mode=our_rev_mode
         our_rev_mode%arg_store=.FALSE.
         our_rev_mode%arg_restore=.FALSE.
         our_rev_mode%plain=.TRUE.
         our_rev_mode%tape=.FALSE.
         our_rev_mode%adjoint=.FALSE.
         call ACTIVE_READ_3D_RL(
     &        fname, active_var_p, doglobalread,
     &        useCurrentDir, lAdInit, iRec, myNr,
     &        REVERSE_SIMULATION, myOptimIter, myThid )
! reset the mode
         our_rev_mode=our_orig_mode
! copy back
         active_var%d = active_var_p
      end if

#endif /* ALLOW_OPENAD_ACTIVE_READ_XYZ */

      end
