awk '
   function max(m,n) {return m<n?n:m }
   $0!~/DRIFT/ && $0!~/MATRIX/ && NR>48 {apx=10*sqrt($4*3e-6/489237.8342+($10*0.4e-2)^2);
                                         apy=55*sqrt($5*40e-9/489237.8342);
                                         ap=max(apx,apy);
					if (ap<12e-3)  
						ap=12e-3;
                                         if (AP[$1] < ap)  
						AP[$1]=ap;}
    END {for (el in AP) print el, ", APERTURE=", AP[el], ";"}' twiss.bds > Apertures4MAD.madx



#read the twiss file and reads the elementes and calcualtes the biggest needed apperture, and print out the bigges one for the ellements in case they are repeated. 06/01/2010
