0001 function [newring,indx,newrefs] = atreduce(oldring,oldrefs)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 lg=numel(oldring);
0019 refs=false(lg+1,1);
0020 if nargin < 2
0021 elseif islogical(oldrefs)
0022 refs(1:length(oldrefs))=oldrefs(:);
0023 else
0024 refs(oldrefs)=true;
0025 end
0026 refend=refs(lg+1);
0027 refs=refs(1:lg);
0028
0029 keep=~atgetcells(oldring(:),'PassMethod','IdentityPass') | refs;
0030 newring=oldring(keep);
0031 indx=keep;
0032
0033 bring=cellfun(@(el) elstrip(el,{'PassMethod','PolynomA','PolynomB',...
0034 'MaxOrder','RApertures','EApertures'}),newring,'UniformOutput',false);
0035 bring(refs(indx))={NaN};
0036 bends=atgetcells(newring,'BendingAngle');
0037 keep=true(size(newring));
0038 ba=zeros(size(newring));
0039 invrad=zeros(size(newring));
0040 ll=atgetfieldvalues(newring,'Length');
0041 ba(bends)=atgetfieldvalues(newring(bends),'BendingAngle');
0042 invrad(bends)=ba(bends)./ll(bends);
0043 islikenext=cellfun(@isequal,[bring(2:end);{NaN}],bring) & abs(invrad([2:end 1])-invrad)<5*eps(invrad);
0044 islikeprev=islikenext([end 1:end-1]);
0045 arrayfun(@group,find(islikenext & ~islikeprev),find(~islikenext & islikeprev));
0046 newring=newring(keep);
0047 indx(indx)=keep;
0048
0049 if nargout >= 3
0050 if nargin >= 2 && islogical(oldrefs)
0051 if length(oldrefs)==lg
0052 newrefs=refs(indx);
0053 else
0054 newrefs=[refs(indx);refend];
0055 end
0056 else
0057 newrefs=find([refs(indx);refend]);
0058 end
0059 end
0060
0061 function el2=elstrip(el,fname)
0062 if isfield(el,'Length') && el.Length>0
0063 fnms=fieldnames(el);
0064 dd=~cellfun(@(ff) any(strcmp(ff,fname)),fnms);
0065 el2=rmfield(el,fnms(dd));
0066 else
0067 el2=NaN;
0068 end
0069 end
0070
0071 function group(i1,i2)
0072 newelem=newring{i1};
0073 newelem.Length=sum(ll(i1:i2));
0074 if bends(i1)
0075 newelem.BendingAngle=sum(ba(i1:i2));
0076 newelem.ExitAngle=newring{i2}.ExitAngle;
0077 end
0078 if isfield(newring{i2},'T2'), newelem.T2=newring{i2}.T2; end
0079 if isfield(newring{i2},'R2'), newelem.R2=newring{i2}.R2; end
0080 newelem.FamName=gname(atgetfieldvalues(newring(i1:i2),'FamName'));
0081 newring{i1}=newelem;
0082 keep(i1+1:i2)=false;
0083 end
0084
0085 function nm=gname(names)
0086 lmin=min(cellfun(@length,names));
0087 nm=names{1};
0088 cellfun(@cmps,names);
0089 if lmin > 0, nm=nm(1:lmin); end
0090
0091 function cmps(name)
0092 lmin=find([name(1:lmin) 'a'] ~= [nm(1:lmin) 'b'],1)-1;
0093 if (lmin>0) && (nm(lmin)=='_' || nm(lmin)=='-'), lmin=lmin-1; end
0094 end
0095 end
0096 end