    subroutine plot_field(the_field,file_name)

    use constants, only : dp, zero, eps, tpi
    use pw_types,  only : field, available, destroy, field_ft, &
                          direct_to_reciprocal_space, assignment(=), &
                          direct_space
    use utilities, only : error

    implicit none

    character (len=*), intent(in) :: file_name
    type(field), intent(in)  :: the_field
    type(field)              :: aux_field
    real(dp)                 :: r0(3),dr1(3),dr2(3)
    integer                  :: n1=0,n2=0
    integer                  :: ig,i1,i2
    complex(dp)              :: the_value_in_r
    real(dp)                 :: arg ,capel,r(3)

 
    if(.not.available(the_field)) call error('plot_field', &
       ' cannot plot an idle field',1)
    aux_field = the_field

    if (aux_field%state==DIRECT_SPACE) &
        call field_ft(aux_field,DIRECT_TO_RECIPROCAL_SPACE)

    write(*,'(a)') ' plotting a 2D cut of the field '
    write(*,advance='no',fmt='(a)') ' n1, n2 > '
    read(*,*) n1,n2
    write(*,advance='no',fmt='(a)') ' origin of the 2D plot > '
    read(*,*) r0(:)
    read_directions: do
       write(*,advance='no',fmt='(a)') ' first direction > '
       read(*,*) dr1(:)
       write(*,advance='no',fmt='(a)') ' second direction > '
       read(*,*) dr2(:)
       if (abs(dot_product(dr1,dr2))> eps) then 
          call error ('plot_field', &
               'the two directions are not orthogonal',-1)
          cycle read_directions
       end if
       exit read_directions
    end do read_directions

    open(unit=3,file=file_name,status='unknown')
    capel = zero
    do i1=0,n1
       write (3,*)
       do i2=0, n2
          r = r0 + float(i1)/n1*dr1 + float(i2)/n2*dr2
          the_value_in_r = zero 
          do ig=1,aux_field%ngm
             arg = TPI * dot_product(r,aux_field%g(:,ig))
             the_value_in_r = the_value_in_r + &                   
                              cmplx(cos(arg),sin(arg)) * &
                              aux_field%val( aux_field%ig1(ig), &
                                             aux_field%ig2(ig), &
                                             aux_field%ig3(ig) )
          end do
          capel = capel + abs(imag(the_value_in_r))
          write (3,fmt='(f10.5)') real(the_value_in_r)
       end do
    end do
    if (capel.gt.eps) then
       write (*,*) ' sum of immaginary part = ', capel
       call error('plot_field','the plotted field was not real',1)
    end if

    call destroy(aux_field)

    close(unit=3)

    return

    end subroutine plot_field

