Commit 0a9130da authored by Simone Liuzzo's avatar Simone Liuzzo

Merge branch 'opticsfiles' into 'master'

Opticsfiles

See merge request !3
parents f652e0da 59b636f5
......@@ -129,7 +129,10 @@ classdef model < atmodel
idx=atgetcells(this.ring,'Class','Monitor');
case 'steerh'
idx=atgetcells(this.ring,'FamName','SH..') | ...
atgetcells(this.ring,'Class','Sextupole');
atgetcells(this.ring,'Class','Sextupole');%| ...
%atgetcells(this.ring,'FamName','DQ\w*'); % this is
% needed if DQ steerers are introduced. However
% slidced DQ for pinholes give problems
case 'steerv'
idx=atgetcells(this.ring,'FamName','SH..') | ...
atgetcells(this.ring,'Class','Sextupole');
......
......@@ -19,7 +19,8 @@ if nargin<2, decodef=@ebs.steerindex; end
[sname,salgo,~]=celldeal(regexp(s2,',','split'));
gradient=str2double(s1);
idx=decodef(sname);
resp=1.0E-3*str2double(strs'); % convert mm to m
%resp=1.0E-3*str2double(strs'); % convert mm to m
resp=str2double(strs'); % response already in m
algo=str2double(salgo);
if isnan(algo), algo=1; end
end
......@@ -18,12 +18,12 @@ function [oh,ov]=load_fresp(filename,pm)
%See also: ebs.load_disp
if nargin<2
[~,cur,rsp]=ebs.load_steerresp(fullfile(filename,'steerF2H000'), @(x)0);
[~,cur,rsp]=ebs.load_steerresp(fullfile(filename,'steerF2H00'), @(x)0);
oh=rsp/cur;
[~,cur,rsp]=ebs.load_steerresp(fullfile(filename,'steerF2V000'), @(x)0);
[~,cur,rsp]=ebs.load_steerresp(fullfile(filename,'steerF2V00'), @(x)0);
ov=rsp/cur;
else
cnst=-pm.ll/pm.alpha/992/2.997924E8;
cnst=-pm.ll/pm.alpha/992/PhysConstant.speed_of_light_in_vacuum.value;
if isdir(filename)
if exist(fullfile(filename, 'dispersion'),'file') ~= 0
[dh,dv]=ebs.load_orbit(fullfile(filename, 'dispersion'));
......
......@@ -7,7 +7,7 @@ function [resp,gradients,algos,steerlist]=load_resp(filename,plane,varargin)
%
%DIRNAME: Directory containing measured data or
% file containing a full response matrix
%PLANE: 'h|H|x|X|1' 'v|V|z|Z|2' 'h2v|H2V|x2z|X2Z|3' 'v2h|V2H|z2x|Z2X|4'
%PLANE: 'h|H' 'v|V' 'h2v|H2V' 'v2h|V2H'
%STEERLIST: list of steerers (1..288)
%
%RESPONSE: BPM response [m]
......@@ -26,14 +26,31 @@ function [resp,gradients,algos,steerlist]=load_resp(filename,plane,varargin)
% See also
nb=320;
nkt=288;
[elzeros,options]=getoption(varargin,'Zeros',1);
[elfixed,options]=getoption(options,'Fixed',1);
[eltoolarge,options]=getoption(options,'TooLarge',1);
args={1:nkt};
args(1:length(options))=options;
steerlist=args{1};
switch upper(plane)
case {'V','V2H'}
nkt=288;
allsteer=(1:nkt);
args={1:nkt};
args(1:length(options))=options;
steerlist=args{1};
case {'H','H2V'}
nkt=384;
allsteer=(1:nkt);
allsteer([5:12:end,7:12:end,8:12:end])=[];
args={1:length(allsteer)};
args(1:length(options))=options;
steerlist=allsteer(args{1});
end
if length(steerlist)==nkt
warning('full RM measurement expected')
end
fn=fullfile(filename,['steer' selectplane(plane,{'H','V','V2H','H2V'}) '%.3d']);
nk=length(steerlist);
resp=NaN(nb,nk);
......@@ -43,10 +60,14 @@ if isdir(filename)
for col=1:nk
steer=steerlist(col);
[idx,grad,rsp,alg]=ebs.load_steerresp(sprintf(fn,steer));
if idx == steer
if allsteer(idx) == steer
resp(:,col)=rsp;
gradients(col)=grad;
algos(col)=alg;
else
error(['Measured Steerer idx ' numstr(allsteer(idx))...
' is different from the required steerer index: '...
num2str(steer)]);
end
end
else % the input DIRNAME is filename
......
......@@ -11,8 +11,8 @@ function [strength,devlist] = read_mag(varargin)
%
%[...,DEVNAME]=EBS.READ_MAG(...)
% Return in addition the device names
strength_attribute_name = 'CorrectionStrength';
devlist=getargs(varargin,{ebs.magname()});
strength=cellfun(@(d)tango.Attribute(d).setpoint('',NaN),devlist,'un',0);
strength=cellfun(@(d)tango.Attribute([d,'/',strength_attribute_name]).setpoint('',NaN),devlist,'un',1);
end
......@@ -161,24 +161,34 @@ end
% function to loop trough cameras measurement. will be replaced by
% srdiag/beam-emittance/all/Emittance_H (?)
function [ex,ey]=readpinholes()
function [ex,ey]=readpinholes(doplot)
ey = NaN(size(pinholes_list));
ex = NaN(size(pinholes_list));
for ip = 1:length(pinholes_list)
try
ph = phdev_list{ip};
ex(ip) = ph.Emittance_H;
ey(ip) = ph.Emittance_V;
ex(ip) = ph.Emittance_h.read;
ey(ip) = ph.Emittance_v.read;
catch errpin
warning(['impossible to read device pinhole: ' pinholes_list{ip}])
disp(errpin)
end
end
end
if doplot
figure;
bar([ex;ey]');
bp = gca;
legend('hor','ver');
bp.XTick=1:length(pinholes_list);
bp.XTickLabels=pinholes_list;
bp.XTickLabelRotation = 45;
end
end
[ex,ey]=readpinholes(); %#ok<ASGLU>
[ex,ey]=readpinholes(true); %#ok<ASGLU>
save(['ApparentEmittances' measurementtime],'pinholes_list','ex','ey');
......@@ -247,6 +257,7 @@ NBPM = length(o0(1,:));
disp(['File ' filename ' already exists. Skip measurement']);
a = load(filename,'response');
response=a.response;
filenametotxt;
return
end
......@@ -273,6 +284,7 @@ NBPM = length(o0(1,:));
disp(['File ' filename ' already exists. Skip measurement']);
a = load(filename,'response');
response=a.response;
filenametotxt;
return
end
% check if it is enabled
......@@ -294,6 +306,7 @@ NBPM = length(o0(1,:));
disp(['File ' filename ' already exists. Skip measurement']);
a = load(filename,'response');
response=a.response;
filenametotxt;
return
end
......@@ -368,23 +381,26 @@ NBPM = length(o0(1,:));
% save file
%matlab
save(filename,'response','variation','mode');
%text (same file names and format of respmat.py
switch mode
case 'H'
fh = fopen([filename ''],'w');
fv = fopen([strrep(filename,'H','H2V') ''],'w');
case 'V'
fh = fopen([strrep(filename,'V','V2H') ''],'w');
fv = fopen([filename ''],'w');
case 'F'
fh = fopen([strrep(filename,'F','F2H') ''],'w');
fv = fopen([strrep(filename,'F','F2V') ''],'w');
end
fprintf(fh,['%.8f\t%s,%.1d\t' repmat('%.5f\t',1,NBPM)],variation,hnames{actuatorindex},algo,response(1,:));
fclose(fh);
fprintf(fv,['%.8f\t%s,%.1d\t' repmat('%.5f\t',1,NBPM)],variation,vnames{actuatorindex},algo,response(2,:));
fclose(fv);
function filenametotxt
%text (same file names and format of respmat.py
switch mode
case 'H'
fh = fopen([filename ''],'w');
fv = fopen([strrep(filename,'H','H2V') ''],'w');
case 'V'
fh = fopen([strrep(filename,'V','V2H') ''],'w');
fv = fopen([filename ''],'w');
case 'F'
fh = fopen([strrep(filename,'F','F2H') ''],'w');
fv = fopen([strrep(filename,'F','F2V') ''],'w');
end
fprintf(fh,['%.8f\t%s,%.1d\t' repmat('%.5f\t',1,NBPM-1) '%.5f'],variation,hnames{actuatorindex},algo,response(1,:));
fclose(fh);
fprintf(fv,['%.8f\t%s,%.1d\t' repmat('%.5f\t',1,NBPM-1) '%.5f'],variation,vnames{actuatorindex},algo,response(2,:));
fclose(fv);
end
end
......@@ -532,7 +548,11 @@ disp('End of measurement');
% csv files for operation
switch obj.machine
case 'ebs-simu'
% ebs.autocor_model(obj.rmodel,'exp',pwd)
if NH == NHall && NV == NVall
ebs.autocor_model(obj.rmodel,'exp',pwd);
else
warning('CSV file not created, partial measurement only.')
end
otherwise
end
......
function varargout=nselect(step,func,i,varargin)
%NSELECT decimate calls to a function
%
persistent is
if i==1
is=step-1;
end
if is==0
[varargout{1:nargout}]=func(i,varargin{:});
is=step;
else
varargout=cell(nargout,1);
end
is=is-1;
end
function [normI,phi,firstX,MresH,MresVH] = CalMresfdaH(n_and_dateX,Xamp,qemres)
%uses calculated sine and cosine instead of fda CorNSTEXZstuf signals with a shift by 22 samples for the Q Cor signal
% to get the Iand Q data
% set_path % TO BE COMMENTED OUT WHEN MERGING WITH QEMPANEL
tic
%load lastMresV
[Xsnif, Zsnif] =getFDAreaderXZ(n_and_dateX);
[XCor, ZCor] =getFDAreaderXZCor(n_and_dateX);
siz=size(Xsnif);
NBPM=siz(1); NSTE=size(XCor,1);
MresH=zeros(NBPM,NSTE);
% MresV=zeros(NBPM,NSTE);
MresVH=zeros(NBPM,NSTE);
% MresHI=zeros(NBPM,NSTE);
% MresVI=zeros(NBPM,NSTE);
% MresHQ=zeros(NBPM,NSTE);
% MresVQ=zeros(NBPM,NSTE);
% MresHVQ=zeros(NBPM,NSTE);
%for i=1:NSTE
% CorNSTEXstuf(i,1:siz(2))=XCor(i,1:siz(2))-mean(XCor(i,:));
%end
CorNSTEXstuf=XCor-repmat(mean(XCor,2)',siz(2),1)';
disp(['Xamp=' num2str(Xamp) ...
' # steerers=', num2str(size(CorNSTEXstuf,1)) ...
' buffer size=',num2str(size(CorNSTEXstuf,2)) ])
[ CXI,CXQ,firstX,lastX,lengthX] = Corfast_fda2(CorNSTEXstuf,Xamp,Xamp/20);
% Xres=zeros(1,siz(2));
% Zres=zeros(1,siz(2));
% CX=zeros(1,siz(2));
% CZ=zeros(1,siz(2));
C=CXI./repmat(sum(abs(CXI),2),1,siz(2));
A=max(abs(CXI),[],2);
MresHI=Xsnif*C'./repmat(A',NBPM,1);
MresVI=Zsnif*C'./repmat(A',NBPM,1);
CQ=CXQ./repmat(sum(abs(CXQ),2),1,siz(2));
AQ=max(abs(CXQ),[],2);
MresHQ=Xsnif*CQ'./repmat(AQ',NBPM,1);
MresVQ=Zsnif*CQ'./repmat(AQ',NBPM,1);
% for j=1:NSTE
% j
%
% CX(1:siz(2))=CXI(j,1:siz(2));
% C=CX/sum(abs(CX))/max(abs(CX));
%
% [ DX] = CXQ(j,1:siz(2));
% D=DX/sum(abs(DX))/max(abs(DX));
%
% for k=1:NBPM
%
% Xres(1:siz(2))=Xsnif(k,1:siz(2));
% Zres(1:siz(2))=Zsnif(k,1:siz(2));
%
%
% MresHI(k,j)=MresH(k,j)+Xres*C';
% MresVI(k,j)=MresV(k,j)+Zres*C';
%
% MresHQ(k,j)=MresH(k,j)+Xres*D';
% MresVQ(k,j)=MresV(k,j)+Zres*D';
%
% end
%
% end
stdHI=std(MresHI);
stdHQ=std(MresHQ);
% stdVI=std(MresVI);
% stdVQ=std(MresVQ);
%calibration factor OK for.1A=32768
G=1e9*.2/pi*2*.5/32768;
normI=sqrt(stdHI.^2+stdHQ.^2)./stdHI;
normQ=sqrt(stdHI.^2+stdHQ.^2)./stdHQ;
phi=stdHQ./sqrt(stdHI.^2+stdHQ.^2);
for i=1:NSTE
if phi(i)< .8
MresH(:,i)=MresHI(:,i)*normI(i)/G;
MresVH(:,i)=MresVI(:,i)*normI(i)/G;
else
MresH(:,i)=MresHQ(:,i)*normQ(i)/G;
MresVH(:,i)=MresVQ(:,i)*normQ(i)/G;
end
end
time=time_label;
file=['TimeLabelMresX',time];
save(fullfile(qemres.datadir,file),'n_and_dateX' , 'MresH','MresVH');
toc
\ No newline at end of file
function [normI,phi,firstZ,MresHV,MresV] = CalMresfdaV(n_and_dateZ,Zamp,qemres)
%uses calculated sine and cosine instead of fda CorNSTEXZstuf signals with a shift by 22 samples for the Q Cor signal
% to get the Iand Q data
% set_path % TO BE COMMENTED OUT WHEN MERGING WITH QEMPANEL
tic
%load lastMresV
[Xsnif, Zsnif] =getFDAreaderXZ(n_and_dateZ);
[XCor, ZCor] =getFDAreaderXZCor(n_and_dateZ);
siz=size(Xsnif);
NBPM=siz(1); NSTE=size(XCor,1);
% MresH=zeros(NBPM,NSTE);
MresV=zeros(NBPM,NSTE);
MresHV=zeros(NBPM,NSTE);
% MresHI=zeros(NBPM,NSTE);
% MresVI=zeros(NBPM,NSTE);
% MresHQ=zeros(NBPM,NSTE);
% MresVQ=zeros(NBPM,NSTE);
% MresHVQ=zeros(NBPM,NSTE);
% for i=1:NSTE
% CorNSTEZstuf(i,1:siz(2))=ZCor(i,1:siz(2))-mean(ZCor(i,:));
% end
CorNSTEZstuf=ZCor-repmat(mean(ZCor,2)',siz(2),1)';
disp(['Zamp=' num2str(Zamp) ...
' # steerers=', num2str(size(CorNSTEZstuf,1)) ...
' buffer size=',num2str(size(CorNSTEZstuf,2)) ])
[ CZI,CZQ,firstZ,lastZ,lengthZ] = Corfast_fda2(CorNSTEZstuf,Zamp,Zamp/10);
% Xres=zeros(1,siz(2));
% Zres=zeros(1,siz(2));
% CX=zeros(1,siz(2));
% CZ=zeros(1,siz(2));
C=CZI./repmat(sum(abs(CZI),2),1,siz(2));
A=max(abs(CZI),[],2);
MresHI=Xsnif*C'./repmat(A',NBPM,1);
MresVI=Zsnif*C'./repmat(A',NBPM,1);
CQ=CZQ./repmat(sum(abs(CZQ),2),1,siz(2));
AQ=max(abs(CZQ),[],2);
MresHQ=Xsnif*CQ'./repmat(AQ',NBPM,1);
MresVQ=Zsnif*CQ'./repmat(AQ',NBPM,1);
% for j=1:NSTE
% j
%
% for k=1:NBPM
%
% Xres(1:siz(2))=Xsnif(k,1:siz(2));
% Zres(1:siz(2))=Zsnif(k,1:siz(2));
% %CX(1:siz(2))=CXI(j,1:siz(2));
% CZ(1:siz(2))=CZI(j,1:siz(2));
%
% C=CZ/sum(abs(CZ));
% A=max(abs(CZ));
%
%
%
% MresHI(k,j)=MresH(k,j)+Xres*C'/A;
% MresVI(k,j)=MresV(k,j)+Zres*C'/A;
%
%
% [ CZ ] = CZQ(j,1:siz(2));
%
%
% C=CZ/sum(abs(CZ));
% A=max(abs(CZ));
%
%
% MresHQ(k,j)=MresH(k,j)+Xres*C'/A;
% MresVQ(k,j)=MresV(k,j)+Zres*C'/A;
%
%
% end
%
% end
% stdHI=std(MresHI);
% stdHQ=std(MresHQ);
stdVI=std(MresVI);
stdVQ=std(MresVQ);
%calibration factor OK for.1A=32768
G=1e9*.2/pi*2*.5/32768;
normI=sqrt(stdVI.^2+stdVQ.^2)./stdVI;
normQ=sqrt(stdVI.^2+stdVQ.^2)./stdVQ;
phi=stdVQ./sqrt(stdVI.^2+stdVQ.^2);
for i=1:NSTE
if phi(i)< .8
MresV(:,i)=MresVI(:,i)*normI(i)/G;
MresHV(:,i)=MresHI(:,i)*normI(i)/G;
else
MresV(:,i)=MresVQ(:,i)*normQ(i)/G;
MresHV(:,i)=MresHQ(:,i)*normQ(i)/G;
end
end
time=time_label;
file=['TimeLabelMresZ',time];
save(fullfile(qemres.datadir,file),'n_and_dateZ' , 'MresV','MresHV');
toc
\ No newline at end of file
function[ CorI,CorQ,first,last,length] = Corfast_fda2( Cor,CorAmp ,mx)
%generate a signal CorQ pi/2 shifted in phase with respect to the sine signal Cor; CorI and corQ are true sine and cosine signals
%at the frequencies used for each corrector by the script getAllSnifferXZandCorTestSigNew2.
%ftab=[1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,1,1,1,1,1,1,1,1,1];
sizeCor=size(Cor);
NSTE=sizeCor(1);
if NSTE ~= 96
error('in Corfast_fda2 # of steerers must be 96, because of reordering from cell 1 to cell 4 at the end of the script');
end
n=sizeCor(2);
amp=4*CorAmp;
CorQ=zeros(NSTE,n);
CorI=zeros(NSTE,n);
first=20*ones(1,NSTE);
last=zeros(1,NSTE);
length=zeros(1,NSTE);
for i=1:NSTE
m=0;
for j=5:n
if (abs(Cor(i,j)<mx && m==0 ))||((abs(Cor(i,j))>=mx && abs(Cor(i,j+25))<CorAmp*2 && m==0))
first(i)=j+1;
else
first(i);
length(i)=5071;
m=1;
end
end
%First=first(i)
end
%for i=1:48
%for j=1:2
%if first(2*(i-1)+j)<74932
%fir=first(2*(i-1)+j);
%ps=round(length(i)/256);
%CorI(2*(i-1)+j,fir:fir+5066+j)=Cor(2*(i-1)+j,fir:fir+5066+j);
%CorQ(2*(i-1)+j,fir+ps:fir+5066+j)=CorI(2*(i-1)+j,fir:fir+5066+j-ps);
%CorQ(2*(i-1)+j,fir:fir+ps-1)=-CorI(2*(i-1)+j,fir+5066+j-ps+1:fir+5066+j);
%else
%CorI(2*(i-1)+j,:)=Cor(2*(i-1)+j,:);
% end
%end
%end
for i=1:NSTE
if first(i)<n-5072
sp=abs(fft(Cor(i,first(i):first(i)+5071)));
[mf,k]=max(sp(1:5071));
f=((k-1)*2);
for j=1:5072
%CorI(i,first(i)+j-1)=amp*sin(2*pi*j*(114+2*ftab(i))/10135) ;
%CorQ(i,first(i)+j-1)=amp*cos(2*pi*j*(114+2*ftab(i))/10135) ;
CorI(i,first(i)+j-1)=amp*sin(2*pi*j*(f)/10144) ;
CorQ(i,first(i)+j-1)=amp*cos(2*pi*j*(f)/10144) ;
end
else
CorI(i,:)=Cor(i,:);
end
end
sizeCor=size(CorI);
Cor=zeros(NSTE,sizeCor(2));
Cor(1:87,:)=CorI(10:NSTE,:);
Cor(88:NSTE,:)=CorI(1:9,:);
CorI=Cor;
Cor=zeros(NSTE,sizeCor(2));
Cor(1:87,:)=CorQ(10:NSTE,:);
Cor(88:NSTE,:)=CorQ(1:9,:);
CorQ=Cor;
function ReadFullRespMatData(dirrmdata, fileRMACraw, fileMeasuredGain)
% converts AC RM data from data archiver to qempanel usable format
%
% INPUT:
% fileRMACraw : name of AC RM file containing variables MresH MresV MresVH
% MresHV
%
%
%see also:
if nargin<3
fileMeasuredGain = 'Gain_new_180604.mat';
end
%======== AC ORM
load(fileRMACraw,'MresH','MresV','MresVH','MresHV') % load file with measured AC RM raw
ACresph=MresH; ACrespv=MresV; ACresphv=MresVH; ACrespvh=MresHV;
% calibration coefficents (empirically found during MDT)
ACresph_norm=-ACresph/2.8; ACrespvh_norm=-ACrespvh/3.8;
ACrespv_norm=-ACrespv/3.8; ACresphv_norm=-ACresphv/2.8;
% apply measured Gain (fine adjustment, cable lengths)
load(fileMeasuredGain,'GainX','GainZ');
% apply new calibration factors
ACresph_norm = bsxfun(@rdivide, ACresph_norm, GainX);
ACrespv_norm = bsxfun(@rdivide, ACrespv_norm, GainZ);
%%======== save for qempanel
fileformat=['%.4f\t%s,%d' repmat('\t%.5f',1,224)];
deltaH=0.08; % from respmat.py
deltaV=0.10; % from respmat.py
for ist=1:96
f=fopen([dirrmdata '/steerH' num2str(ist,'%.2d')],'w');
fprintf(f,fileformat,deltaH,sr.steername(ist,'SR/ST-H%i/C%i/CURRENT'),0,ACresph_norm(:,ist)*1e3);
fclose(f);
f=fopen([dirrmdata '/steerV' num2str(ist,'%.2d')],'w');
fprintf(f,fileformat,deltaV,sr.steername(ist,'SR/ST-V%i/C%i/CURRENT'),0,ACrespv_norm(:,ist)*1e3);
fclose(f);
% off diagonal
f=fopen([dirrmdata '/steerH2V' num2str(ist,'%.2d')],'w');
fprintf(f,fileformat,deltaH,sr.steername(ist,'SR/ST-H%i/C%i/CURRENT'),0,ACresphv_norm(:,ist)*1e3);
fclose(f);
f=fopen([dirrmdata '/steerV2H' num2str(ist,'%.2d')],'w');
fprintf(f,fileformat,deltaV,sr.steername(ist,'SR/ST-V%i/C%i/CURRENT'),0,ACrespvh_norm(:,ist)*1e3);
fclose(f);
end
end
function [n_and_dateX] =StartMresStimX(n,Xamp,qemres)