source: MML/trunk/machine/SOLEIL/StorageRing/insertions/idMeasElecBeamVsUndParam.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 10 years ago

Initial import--MML version from SOLEIL@2013

File size: 8.3 KB
Line 
1function [resFileNamesStruct, resErrorFlag] = idMeasElecBeamVsUndParam(idName, undParams, undParamsBkg, freqBkgMeas, inclPerturbMeas, fileNameCore, dispData)
2% Measure e-beam COD at different undulator parameters (gap, phase or currents in main coils)
3% if(freqBkgMeas == 1) - measure bakground once each time after passing the in-most loop
4% if(freqBkgMeas == n) - measure bakground once after passing n nested loops
5
6%==========================================================================
7UpdateOfCOD=1; 
8% if 0 => no FFWD, no {SOFB + FOFB} : in case of first iteration of tables
9% if 1 => FFWD but no {SOFB + FOFB} : in case of higher iteration
10%==========================================================================
11Debug=1;    % if 1 => no check of SOFB and FOFB
12
13resFileNamesStruct=struct;
14resFileNames = {};
15resErrorFlag = 0;
16numUndParams = length(undParams);
17resUnd = 0;
18absCurrentThreshold=0.01;   % To check if cor currents are zero
19
20%% CHECK FFWD, SOFB AND FOFB STATE
21    dServName = idGetUndDServer(idName);
22    if dServName ==-1
23        fprintf('IdName ''%s'' is incorrect\n', idName);
24        return
25    end
26    tempStructure=idGetCorrectionStatus(idName);
27    fFwdState=tempStructure.ffwd;
28    if (fFwdState==1)
29        if ~UpdateOfCOD
30            fprintf ('FFWD is active! Desactive it to continue\n')
31            return
32        end
33    end
34 
35    sOfbIsActive=tempStructure.sofb;
36    if (sOfbIsActive==1)
37        if ~Debug
38            fprintf ('SOFB is active! Desactive it to continue\n')
39            return
40        end
41    end
42   
43    fOfbIsActive=tempStructure.fofb;
44    if (fOfbIsActive==1)
45        if ~Debug
46            fprintf ('FOFB is active! Desactive it to continue\n')
47            return
48        end
49    end
50
51%% CHECK FFWD CORRECTION CURRENTS
52
53    CurrentCVE=tango_read_attribute(dServName, 'currentCVE');
54    CurrentCVE=CurrentCVE.value(1);
55    CurrentCHE=tango_read_attribute(dServName, 'currentCHE');
56    CurrentCHE=CurrentCHE.value(1);
57    CurrentCVS=tango_read_attribute(dServName, 'currentCVS');
58    CurrentCVS=CurrentCVS.value(1);
59    CurrentCHS=tango_read_attribute(dServName, 'currentCHS');
60    CurrentCHS=CurrentCHS.value(1);
61    if (abs(CurrentCVE)>absCurrentThreshold||abs(CurrentCHE)>absCurrentThreshold||abs(CurrentCVS)>absCurrentThreshold||abs(CurrentCHS)>absCurrentThreshold)
62        if ~UpdateOfCOD
63            fprintf('Set correction currents to zero to continue\n');
64            return
65        end
66    end
67
68%% Script from Oleg
69%"Nested For" algorithm
70curParamName = undParams{1}{1}; % Parameter name ('gap')
71curParamValues = undParams{1}{2}; % Parameter value [5.5 6 10]
72curParamAbsTol = undParams{1}{3}; % Parameter absolute tolerance {0.01}
73numCurParamValues = length(curParamValues);
74
75% generates filename for saving data
76nextFileNameCoreBase = strcat(fileNameCore, '_');
77nextFileNameCoreBase = strcat(nextFileNameCoreBase, curParamName);
78
79for j = 1:numCurParamValues
80   
81        curParamValue = curParamValues(j);
82   
83    strParamValue = sprintf('%f', curParamValue);
84    strParamValue = strrep(strParamValue, '.', '_');
85    lenOrigStrParamValue = length(strParamValue);
86    numTrailZeros = 0; %removing trailing zeros
87    for k = 1:lenOrigStrParamValue
88        if strcmp(strParamValue(lenOrigStrParamValue - k + 1), '0')
89            numTrailZeros = numTrailZeros + 1;
90        else
91            if (numTrailZeros > 0) && strcmp(strParamValue(lenOrigStrParamValue - k + 1), '_')
92                numTrailZeros = numTrailZeros + 1;
93            end
94            break;
95        end
96    end
97    if(numTrailZeros > 0)
98        strParamValueFin = '';
99        for k = 1:lenOrigStrParamValue - numTrailZeros
100            strParamValueFin = strcat(strParamValueFin, strParamValue(k));
101        end
102    else
103        strParamValueFin = strParamValue; %OC fix(?)
104    end
105    nextFileNameCore = strcat(nextFileNameCoreBase, strParamValueFin);
106   
107    %%%%%%%%%%%Change Undulator parameter (gap, phase or current)
108        resUnd = idSetUndParamSync(idName, curParamName, curParamValue, curParamAbsTol);
109    if(resUnd ~= 0)
110        fprintf('Execution terminated since Undulator Parameter can not be set\n');
111        resErrorFlag = -1; return;
112    end
113   
114        if(numUndParams > 1)
115        %Preparing arguments for nested call
116        nextUndParams = {};
117        for k = 1:(numUndParams - 1)
118            for i = 1:3
119                nextUndParams{k}{i} = undParams{k + 1}{i};
120            end
121            nextUndParams{k}{4} = 'nested'; %marking the set of params as for the nested call
122        end
123       
124        %%%%%%%%%%%Do nested call
125        [resNextFileNamesStruct, resNextErrorFlag] = idMeasElecBeamVsUndParam(idName, nextUndParams, undParamsBkg, freqBkgMeas, inclPerturbMeas, nextFileNameCore, dispData);
126        resNextFileNames = resNextFileNamesStruct.filelist;
127        numNextFileNames = length(resNextFileNames);
128       
129        if(resNextErrorFlag ~= 0)
130            fprintf('Execution terminated because of error in the inner loop\n');
131            resErrorFlag = resNextErrorFlag; return;
132        end
133
134        numCurFileNames = length(resFileNames);
135        for k = 1:numNextFileNames
136                resFileNames{numCurFileNames + k} = resNextFileNames{k};
137        end
138
139    else
140        %%%%%%%%%%%Measure e-beam COD
141        getx();getz();
142        outStruct = idMeasElecBeamUnd(idName, inclPerturbMeas, nextFileNameCore, 1, dispData);
143        numCurFileNames = length(resFileNames);
144        resFileNames{numCurFileNames + 1} = outStruct.file;
145        end
146end
147resFileNamesStruct.filelist = resFileNames;
148
149numUndParamsBkg = length(undParamsBkg);
150if((numUndParams == freqBkgMeas) && (numUndParamsBkg > 0)) %Measure Background if necessary
151    %numUndParamsBkg = length(undParamsBkg);
152
153    if(numUndParamsBkg > 0)
154        nextFileNameCore = strcat(fileNameCore, '_bkg');
155        undParamsBeforeBkgMeas = {};
156        for k = 1:numUndParamsBkg
157            curParamName = undParamsBkg{k}{1};
158            curParamValue = undParamsBkg{k}{2};
159            curParamAbsTol = undParamsBkg{k}{3};
160            undParamsBeforeBkgMeas{k} = idGetUndParam(idName, curParamName);
161           
162            %%%%%%%%%%%Change Undulator parameter (gap, phase or current) to prepare for background measurement
163         %   resUnd = idSetUndParamSync(idName, curParamName, curParamValue, curParamAbsTol);
164          %  if(resUnd ~= 0)
165          %     fprintf('Execution terminated since Undulator Parameter can not be set\n');
166          %      resErrorFlag = -1; return;
167          %  end
168        end
169
170        %%%%%%%%%%%Measure e-beam COD background
171        getx();getz();
172        outStruct = idMeasElecBeamUnd(idName, inclPerturbMeas, nextFileNameCore, 1, dispData);
173        numCurFileNames = length(resFileNames);
174        resFileNames{numCurFileNames + 1} = outStruct.file;
175        resFileNamesStruct.filelist = resFileNames;
176
177        %Returning to the state before background measurement, only in the case of nested call
178        if(length(undParams{1}) > 3)
179            if strcmp(undParams{1}{4}, 'nested')
180                for k = 1:numUndParamsBkg
181                    curParamName = undParamsBkg{k}{1};
182                    curParamAbsTol = undParamsBkg{k}{3};
183                    curParamValue = undParamsBeforeBkgMeas{k};
184           
185                    %%%%%%%%%%%Change Undulator parameter (gap, phase or current) - return to the last state before background measurement
186                %    resUnd = idSetUndParamSync(idName, curParamName, curParamValue, curParamAbsTol);
187                %    if(resUnd ~= 0)
188                %        fprintf('Execution terminated since Undulator Parameter can not be set\n');
189                %        resErrorFlag = -1; return;
190                %   end
191                end
192            end
193        end
194    end
195end
196
197if((length(undParams{1}) <= 3) || (strcmp(undParams{1}{4}, 'nested') == 0)) %i.e. this is not a nested call
198%iall job is done, writing and saving the summary structure
199
200        lastParamAr = undParams{numUndParams}{2};
201        perMeasBkg = length(lastParamAr);
202        if(freqBkgMeas > 1)
203        for k = 1:(numUndParams - 1)
204            perMeasBkg = perMeasBkg*length(undParams{numUndParams - k}{2});
205        end
206        end
207
208    resFileNamesStruct.filelist = resFileNames;
209        resFileNamesStruct.params = undParams;
210        resFileNamesStruct.filenames_meas_bkg = idAuxPrepFileNameListMeasAndBkg(resFileNames, perMeasBkg);
211   
212        fileNameCoreSummary = strcat(fileNameCore, '_summary');
213        idSaveStruct(resFileNamesStruct, fileNameCoreSummary, idName, 1, 0);
214end
Note: See TracBrowser for help on using the repository browser.