*$ CREATE MAGFLD.FOR
*COPY MAGFLD
*
*===magfld=============================================================*
*
      SUBROUTINE MAGFLD ( X, Y, Z, BTX, BTY, BTZ, B, NREG, IDISC )

      INCLUDE '(DBLPRC)'
      INCLUDE '(DIMPAR)'
      INCLUDE '(IOUNIT)'
*
*----------------------------------------------------------------------*
*                                                                      *
*     Copyright (C) 1988-2010      by Alberto Fasso` & Alfredo Ferrari *
*     All Rights Reserved.                                             *
*                                                                      *
*                                                                      *
*     Created  in     1988         by    Alberto Fasso`                *
*                                                                      *
*                                                                      *
*     Last change on 06-Nov-10     by    Alfredo Ferrari               *
*                                                                      *
*     Input variables:                                                 *
*            x,y,z = current position                                  *
*            nreg  = current region                                    *
*     Output variables:                                                *
*            btx,bty,btz = cosines of the magn. field vector           *
*            B = magnetic field intensity (Tesla)                      *
*            idisc = set to 1 if the particle has to be discarded      *
*                                                                      *
*----------------------------------------------------------------------*
*
      INCLUDE '(CMEMFL)'
      INCLUDE '(CSMCRY)'
*
*     Variables storing region number of the magnetic regions:
      SAVE NTURN1, NTURN2
*
*     lfirst, to flag first call of routine
      LOGICAL LFIRST
      DATA    LFIRST / .TRUE. /
      SAVE    LFIRST
*
*  +-------------------------------------------------------------------*
*	Initialize Core Routine                                          *
*  +-------------------------------------------------------------------*
      IDISC = 0
*
* Default vaules - No magnetic field
      BTX = ZERZER
      BTY = ONEONE
      BTZ = ZERZER
      B   = ZERZER

*----------------------------------------------------------------------*
*     first call: initialisation
*----------------------------------------------------------------------*
*
      IF ( LFIRST ) THEN
*
*        retrieve the indices of the magnetic regions starting from their
*        names, and store them
         CALL GEON2R( "TurnVac1", NTURN1, IERR )
         CALL GEON2R( "TurnVac2", NTURN2, IERR )
*
*         avoid to initialise again the routine:
          LFIRST  = .FALSE.
      END IF

* 0.793392 T fields in 2 regions TurnVac1 BTY = 1.0, TurnVac2 BTY = -1.0  *
      IF ( NREG .EQ. NTURN1 ) THEN
*        First linac turn, 0.793392 T in positive y-axis
         BTX = ZERZER
         BTY = ONEONE
         BTZ = ZERZER
         B   = 0.793392
      ELSEIF ( NREG .EQ. NTURN2 ) THEN
*        Second linac turn, 0.793392 T in negative y-axis
         BTX = ZERZER
         BTY = -ONEONE
         BTZ = ZERZER
         B   = 0.793392
      ENDIF

      RETURN
*=== End of subroutine Magfld =========================================*
      END
