subroutine calculate_initial_magnetic_field(F)
TYPE(FIELDS), INTENT(INOUT) :: F
REAL(rp),dimension(F%dims(1),F%dims(3),2) :: gradA
INTEGER :: ii
INTEGER :: jj
REAL(rp) :: psip_conv
psip_conv=F%psip_conv
call EZspline_interp(bfield_2d%A,F%dims(1),F%dims(3),F%X%R, F%X%Z, &
F%PSIp, ezerr)
call EZspline_error(ezerr)
! FR = (dA/dZ)/R
call EZspline_gradient(bfield_2d%A,F%dims(1),F%dims(3),F%X%R, F%X%Z, &
gradA, ezerr)
call EZspline_error(ezerr)
do ii=1,F%dims(1)
F%B_2D%R(ii,:) = psip_conv*gradA(ii,:,2)/F%X%R(ii)
F%B_2D%PHI(ii,:) = -F%Bo*F%Ro/F%X%R(ii)
F%B_2D%Z(ii,:) = -psip_conv*gradA(ii,:,1)/F%X%R(ii)
end do
! write(output_unit_write,'("AR",E17.10)') gradA(1)
! write(output_unit_write,'("AZ",E17.10)') gradA(2)
end subroutine calculate_initial_magnetic_field