source: MML/trunk/machine/SOLEIL/StorageRing/insertions/IDStarter/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: 6.2 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
6resFileNames = {};
7resErrorFlag = 0;
8numUndParams = length(undParams);
9resUnd = 0;
10
11%"Nested For" algorithm
12curParamName = undParams{1}{1};
13curParamValues = undParams{1}{2};
14curParamAbsTol = undParams{1}{3};
15numCurParamValues = length(curParamValues);
16
17nextFileNameCoreBase = strcat(fileNameCore, '_');
18nextFileNameCoreBase = strcat(nextFileNameCoreBase, curParamName);
19
20for j = 1:numCurParamValues
21   
22        curParamValue = curParamValues(j);
23   
24    strParamValue = sprintf('%f', curParamValue);
25    strParamValue = strrep(strParamValue, '.', '_');
26    lenOrigStrParamValue = length(strParamValue);
27    numTrailZeros = 0; %removing trailing zeros
28    for k = 1:lenOrigStrParamValue
29        if strcmp(strParamValue(lenOrigStrParamValue - k + 1), '0')
30            numTrailZeros = numTrailZeros + 1;
31        else
32            if (numTrailZeros > 0) && strcmp(strParamValue(lenOrigStrParamValue - k + 1), '_')
33                numTrailZeros = numTrailZeros + 1;
34            end
35            break;
36        end
37    end
38    if(numTrailZeros > 0)
39        strParamValueFin = '';
40        for k = 1:lenOrigStrParamValue - numTrailZeros
41            strParamValueFin = strcat(strParamValueFin, strParamValue(k));
42        end
43    else
44        strParamValueFin = strParamValue; %OC fix(?)
45    end
46    nextFileNameCore = strcat(nextFileNameCoreBase, strParamValueFin);
47   
48    %%%%%%%%%%%Change Undulator parameter (gap, phase or current)
49        resUnd = idSetUndParamSync(idName, curParamName, curParamValue, curParamAbsTol);
50    if(resUnd ~= 0)
51        fprintf('Execution terminated since Undulator Parameter can not be set\n');
52        resErrorFlag = -1; return;
53    end
54   
55        if(numUndParams > 1)
56        %Preparing arguments for nested call
57        nextUndParams = {};
58        for k = 1:(numUndParams - 1)
59            for i = 1:3
60                nextUndParams{k}{i} = undParams{k + 1}{i};
61            end
62            nextUndParams{k}{4} = 'nested'; %marking the set of params as for the nested call
63        end
64       
65        %%%%%%%%%%%Do nested call
66        [resNextFileNamesStruct, resNextErrorFlag] = idMeasElecBeamVsUndParam(idName, nextUndParams, undParamsBkg, freqBkgMeas, inclPerturbMeas, nextFileNameCore, dispData);
67        resNextFileNames = resNextFileNamesStruct.filelist;
68        numNextFileNames = length(resNextFileNames);
69       
70        if(resNextErrorFlag ~= 0)
71            fprintf('Execution terminated because of error in the inner loop\n');
72            resErrorFlag = resNextErrorFlag; return;
73        end
74
75        numCurFileNames = length(resFileNames);
76        for k = 1:numNextFileNames
77                resFileNames{numCurFileNames + k} = resNextFileNames{k};
78        end
79
80    else
81        %%%%%%%%%%%Measure e-beam COD
82        outStruct = idMeasElecBeamUnd(idName, inclPerturbMeas, nextFileNameCore, dispData);
83        numCurFileNames = length(resFileNames);
84        resFileNames{numCurFileNames + 1} = outStruct.file;
85        end
86end
87resFileNamesStruct.filelist = resFileNames;
88
89numUndParamsBkg = length(undParamsBkg);
90if((numUndParams == freqBkgMeas) && (numUndParamsBkg > 0)) %Measure Background if necessary
91    %numUndParamsBkg = length(undParamsBkg);
92
93    if(numUndParamsBkg > 0)
94        nextFileNameCore = strcat(fileNameCore, '_bkg');
95        undParamsBeforeBkgMeas = {};
96        for k = 1:numUndParamsBkg
97            curParamName = undParamsBkg{k}{1};
98            curParamValue = undParamsBkg{k}{2};
99            curParamAbsTol = undParamsBkg{k}{3};
100            undParamsBeforeBkgMeas{k} = idGetUndParam(idName, curParamName);
101           
102            %%%%%%%%%%%Change Undulator parameter (gap, phase or current) to prepare for background measurement
103            resUnd = idSetUndParamSync(idName, curParamName, curParamValue, curParamAbsTol);
104            if(resUnd ~= 0)
105                fprintf('Execution terminated since Undulator Parameter can not be set\n');
106                resErrorFlag = -1; return;
107            end
108        end
109
110        %%%%%%%%%%%Measure e-beam COD background
111        outStruct = idMeasElecBeamUnd(idName, inclPerturbMeas, nextFileNameCore, dispData);
112        numCurFileNames = length(resFileNames);
113        resFileNames{numCurFileNames + 1} = outStruct.file;
114        resFileNamesStruct.filelist = resFileNames;
115
116        %Returning to the state before background measurement, only in the case of nested call
117        if(length(undParams{1}) > 3)
118            if strcmp(undParams{1}{4}, 'nested')
119                for k = 1:numUndParamsBkg
120                    curParamName = undParamsBkg{k}{1};
121                    curParamAbsTol = undParamsBkg{k}{3};
122                    curParamValue = undParamsBeforeBkgMeas{k};
123           
124                    %%%%%%%%%%%Change Undulator parameter (gap, phase or current) - return to the last state before background measurement
125                    resUnd = idSetUndParamSync(idName, curParamName, curParamValue, curParamAbsTol);
126                    if(resUnd ~= 0)
127                        fprintf('Execution terminated since Undulator Parameter can not be set\n');
128                        resErrorFlag = -1; return;
129                    end
130                end
131            end
132        end
133    end
134end
135
136if((length(undParams{1}) <= 3) || (strcmp(undParams{1}{4}, 'nested') == 0)) %i.e. this is not a nested call
137%iall job is done, writing and saving the summary structure
138
139        lastParamAr = undParams{numUndParams}{2};
140        perMeasBkg = length(lastParamAr);
141        if(freqBkgMeas > 1)
142        for k = 1:(numUndParams - 1)
143            perMeasBkg = perMeasBkg*length(undParams{numUndParams - k}{2});
144        end
145        end
146
147    resFileNamesStruct.filelist = resFileNames;
148        resFileNamesStruct.params = undParams;
149        resFileNamesStruct.filenames_meas_bkg = idAuxPrepFileNameListMeasAndBkg(resFileNames, perMeasBkg);
150   
151        fileNameCoreSummary = strcat(fileNameCore, '_summary');
152        idSaveStruct(resFileNamesStruct, fileNameCoreSummary, idName, 0);
153end
Note: See TracBrowser for help on using the repository browser.