global img intmat intmat1 choice flag

init

%pps=avsread2('/nfs/ir6/w/fyz/2dd/huber/hc/prompt1e6.50.fld');
pps=avsread2('/nfs/ir6/w/fyz/2dd/huber/lc/prompt3e5.50.fld');
myout1=zeros(128,128,50); myout2=zeros(128,128,50);
myout3=zeros(128,128,50); myout4=zeros(128,128,50); %myout5=zeros(128,128,50);
load lsets

for ii=1:50,
	ii
	!rm -f pp.fld
	avswrite('pp.fld',pps(:,:,ii));
lset=sqrt((xxm-43).^2*1.5+(yym-62).^2/2)-8; nb1=(lset<19 & lset>-2);
lset=sqrt((xxm-98).^2*1.5+(yym-62).^2/2)-8; nb2=(lset<19 & lset>-2);
lset=sqrt((xxm-67).^2+(yym-90).^2)-3; nb3=(lset<10 & lset>-2);
lset=sqrt((xxm-67).^2/2+(yym-66).^2)-40; nb4=(lset<7 & lset>-7);
eee=0.45;
wt=((1-or(or(lsets(:,:,1,ii)<0,sqrt((xxm-43).^2*1.5+(yym-62).^2/2)-8<-2),...
 or(lsets(:,:,2,ii)<0,sqrt((xxm-98).^2*1.5+(yym-62).^2/2)-8<-2)))+eee)/(1+eee);
wt=wt';
dmap=min(min(abs(lsets(:,:,1,ii)).*nb1+(1-nb1)*1000,...
	abs(lsets(:,:,2,ii)).*nb2+(1-nb2)*1000), ...
        min(abs(lsets(:,:,3,ii)).*nb3+(1-nb3)*1000,...
	abs(lsets(:,:,4,ii)).*nb4+(1-nb4)*1000));
xmap=zeros(N,N); ymap=zeros(N,N); penal=zeros(N,N,4); penal1=zeros(N,N,4);
xmap(2:N,:)=min(dmap(1:N-1,:),dmap(2:N,:));
ymap(:,2:N)=min(dmap(:,1:N-1),dmap(:,2:N));
penal(:,:,1)=ymap'; penal(:,:,2)=xmap';

	choice=1; penal1(:,:,1:2)=hh(penal(:,:,1:2)); flag=1;
	if flag==1,penal1(:,:,1)=penal1(:,:,1).*wt;
		penal1(:,:,2)=penal1(:,:,2).*wt;end
	!rm -f pen.fld
	avswrite('pen.fld',penal1);
	!rm -f out.fld
	!reconstruct 16 120

        img=avsread('out.fld')'; myout1(:,:,ii)=img';

        choice=2; penal1(:,:,1:2)=hh(penal(:,:,1:2));
        if flag==1,penal1(:,:,1)=penal1(:,:,1).*wt;
                penal1(:,:,2)=penal1(:,:,2).*wt;end
        !rm -f pen.fld
        avswrite('pen.fld',penal1);
        !rm -f out.fld
        !reconstruct 16 120

        img=avsread('out.fld')'; myout2(:,:,ii)=img';

        choice=3; penal1(:,:,1:2)=hh(penal(:,:,1:2)); 
        if flag==1,penal1(:,:,1)=penal1(:,:,1).*wt;
                penal1(:,:,2)=penal1(:,:,2).*wt;end
        !rm -f pen.fld
        avswrite('pen.fld',penal1);
        !rm -f out.fld
        !reconstruct 16 120

        img=avsread('out.fld')'; myout3(:,:,ii)=img';

        choice=4; penal1(:,:,1:2)=hh(penal(:,:,1:2));
        if flag==1,penal1(:,:,1)=penal1(:,:,1).*wt;
                penal1(:,:,2)=penal1(:,:,2).*wt;end
        !rm -f pen.fld
        avswrite('pen.fld',penal1);
        !rm -f out.fld
        !reconstruct 16 120

        img=avsread('out.fld')'; myout4(:,:,ii)=img';

	if mod(ii,5)==0,
		save myout1 myout1;
		save myout2 myout2;
		save myout3 myout3;
		save myout4 myout4
		%save myout5 myout5;
	end
end
