 
function s=add_beamstopmask(s,a) %mask file contains 1's and 0's

if (nargin==2)
    s.bsmask_configuration(1)=a.bsmaskshape;
    s.bsmask_configuration(2)=a.raw_center(1);
    s.bsmask_configuration(3)=a.raw_center(2);
    s.bsmask_configuration(4)=a.bsmask_size;
    s.bsmask_configuration(5)=a.bsmaskarm_angle;
    s.bsmask_configuration(6)=a.bsmaskarm_width;
end
s.bsmask_pixels=create_beamstopmask(s);

function pixels=create_beamstopmask(s) %mask file contains 1's and 0's
pixels=1+0.*s.raw_pixels;
%first lets make R and phi values for each pixel

colcen=s.raw_center(1);
rowcen=s.raw_center(2);

[rowmax,colmax]=size(s.raw_pixels);
[col,row]=meshgrid(1:colmax,1:rowmax);
R=sqrt(((col-colcen)).^2+((row-rowcen)).^2);
deltaphicorr=(s.bsmask_configuration(5))/180*pi;
deltaphi=mod(-atan2((row-rowcen),-(col-colcen))-deltaphicorr,2*pi);

armdist=abs(R.*sin(deltaphi)); %distance in pixels from arm
armdistmax=s.bsmask_configuration(6)/2;
bstoprad=s.bsmask_configuration(4)/2;
half_pi=pi/2;
%First the beamstop
if (s.bsmask_configuration(1)==0)
    pixels(R<bstoprad)=0;
end
%then the beamstoparm (a little bit more tricky)
if (s.bsmask_configuration(6)>0)
    pixels=pixels.*(1-(armdist<armdistmax).*(abs((mod(deltaphi+half_pi,2*pi)-half_pi))>pi/2));
end
