function [Ez Er Bt] = interpolFieldmap(map,r,z,cavity_info)
% returns the longitudinal and radial electrical field as well as 
% the transverse magnetic field at one point; this routine can be found in
% the ImpactT source code, e.g. in SolRF.F90
%
% Author: Christof Kraus christof.kraus@psi.ch
%
% map          is the array of fourier coefficients to be used
% z            the longitudinal position at whicht the field should be
%              interpolated 
% r            the transverse position at whicht the field should be
%              interpolated
% cavity_info  an array containing the length and the frequency (in this
%              order!)


clight = 299792458;
b = cavity_info(1);
freq = cavity_info(2);

ez = real(map(1))/2;
ezp = 0.;
ezpp = 0.;
ezppp = 0.;

for i = 2:size(map,2)
    loccos = cos((i-1)*pi*z/b);
    locsin = sin((i-1)*pi*z/b);
    
    tmpfact = (i-1)*pi/b;
    
    tmpfactn = 1;
    ez = ez + real(map(i))*loccos + imag(map(i))*locsin;
    
    tmpfactn = tmpfactn * tmpfact;
    ezp = ezp + tmpfactn*(-real(map(i))*locsin + imag(map(i))*loccos);
    
    tmpfactn = tmpfactn * tmpfact;
    ezpp = ezpp - tmpfactn*(real(map(i))*loccos + imag(map(i))*locsin);
    
    tmpfactn = tmpfactn * tmpfact;
    ezppp = ezppp - tmpfactn*(-real(map(i))*locsin + imag(map(i))*loccos);
    
end
f1 = -(ezpp + ez*freq^2/clight^2)/4;
f1p = -(ezppp + ezp*freq^2/clight^2)/4;
    
Er = -r*(ezp/2+f1p*r*r/4);
Ez = ez + f1*r*r;
Bt = -r*(ez/2 + f1*r*r/4)*freq/clight^2;
