! READ magnetic field file

 subroutine get_g2_fields(file, x,B)
   use bmad
   implicit none
 logical first/.true./
 type(em_field_struct), allocatable :: inject(:)
 

 if(first)call read_field(file,Bfield)
 
 

end

subroutine read_field(file, Bfield)

   use bmad
   implicit none

   type(em_field_struct), allocatable :: inject(:)
   type magfield_struct 
      real(rp) x(3), B(3), H
   end type
   type(magfield_struct), allocatable :: position(:)
   integer iu, istat, nlines
   character(:)  file

!type em_field_struct
!  real(rp) E(3)         ! electric field
!  real(rp) B(3)         ! magnetic field
!  real(rp) dE(3,3)      ! electric field gradient
!  real(rp) dB(3,3)      ! magnetic field gradient
!end type

   iu = lunget()
   open(unit=iu, file = file, status = 'old', action='read', err=9000)
   nlines = 0
   do while(.true.)

     read(iu, *, IOSTAT = istat ) string
     if(index('"',string) /= 0)cycle
     if(istat < 0) exit
     nlines = nlines + 1
   end do

   allocate(position(1:nlines+100))
   rewind(unit=iu)

   do while(.true.)
     read(iu,*,IOSTAT = istat) position(i)%x(1:3), position(i)%B(1:3), position(i)%H
     if(index('"',string) /= 0)cycle
     if(istat < 0)exit
  end do
