source: MML/trunk/mml/gettuneresp.m @ 4

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

Initial import--MML version from SOLEIL@2013

File size: 8.8 KB
Line 
1function [TuneMatrix, FileName] = gettuneresp(varargin)
2%GETTUNERESP - Loads the tune response vector (or matrix) for multiple quadrupole families
3%  [TuneMatrix, FileName] = gettuneresp(FamilyName1, DeviceList1, FamilyName2, DeviceList2, ... , FileName, GeV)
4%  [TuneMatrix, FileName] = gettuneresp(DataStructure1, DataStructure2, ... , FileName, GeV)
5%
6%  INPUTS
7%  1. FamilyName - Quadrupole family name
8%  2. DeviceList - DeviceList for a quadrupole family.  If [] or no input, then TuneMatrix
9%                  will be a column vector which is the cumulative sum of all magnets in the family.
10%  3. FileName - File name to look for the response matrix (or cell array of file names)
11%                '' prompts the user to choose a response matrix file
12%  4. GeV will linearly scale the response matrix from the measured energy {Default: getenergy}.
13%
14%  OUTPUTS
15%  1. TuneMatrix = Response matrix
16%
17%     It is assumed that most common use of this function is with the 
18%     Q7 and Q9 families on a ganged power supply.  Hence, the default
19%     behavior is to return the cumulative sum of all the magnets in the chain.
20%     If there is more than one FamilyName, then TuneMatrix will be a matrix 
21%     where each column is the sum of the contribution of all magnets in that family.
22%
23%     To get the response matrix for individual magnets in the family use getrespmat:
24%     getrespmat('TUNE', [1 1;1 2], MagnetFamilyName, MagnetDeviceList)
25%     For instance, getrespmat('TUNE', [1 1;1 2], 'Q1', [])
26%
27%  EXAMPLES
28%  1. M = gettuneresp
29%     M = gettuneresp({'Q7','Q9'})
30%     M = gettuneresp({'Q7','Q9'},{[],[]})
31%     M = gettuneresp('Q7',getlist('Q9'))
32%     All returns the same 2x2 matrix of Q7 and Q9 to horizontal and vertical tune
33%
34%  2. M = gettuneresp('Q7')
35%     M = gettuneresp('Q7', [])
36%     Returns a 2x1 matrix representing the cumulative sum of all the magnets in the chain
37%
38%  3. Q9_DataStruct = getsp('Q9','Struct');
39%     M = gettuneresp(Q9_DataStruct);
40%     Returns a 2x1 matrix representing the cumulative sum of all the magnets in the chain
41%
42%  4. Change the tune by [.01; -.01] using the entire 'Q7' and 'Q9' families (see stepchro)
43%     DeltaTune = [.01; -.01];
44%     DeltaAmps = inv(gettuneresp) * DeltaTune;
45%     setsp({'Q7', 'Q9'}, {getsp('Q7')+DeltaAmps(1), getsp('Q9')+DeltaAmps(2)});
46
47%
48%  Written by Gregory J. Portmann
49%  Modified by Laurent S. Nadolski
50
51FileName = '';
52NumericFlag = 1;
53InputFlags = {};
54for i = length(varargin):-1:1
55    if isstruct(varargin{i})
56        % Ignore structures
57    elseif iscell(varargin{i})
58        % Ignore cells
59    elseif strcmpi(varargin{i},'Struct')
60        % Remove
61        varargin(i) = [];
62    elseif strcmpi(varargin{i},'Numeric')
63        % Remove
64        varargin(i) = [];
65    elseif strcmpi(varargin{i},'Online')
66        % Remove
67        fprintf('GETTUNERESP WARNING: ''Online'' input ignored.  Used meastuneresp to get the chromaticity response matrix.');
68        varargin(i) = [];
69    elseif strcmpi(varargin{i},'Simulator')
70        % Remove
71        fprintf('GETTUNERESP WARNING: ''Simulator'' input ignored.  Used meastuneresp to get the chromaticity response matrix.');
72        varargin(i) = [];
73    elseif strcmpi(varargin{i},'Model')
74        % Remove
75        fprintf('GETTUNERESP WARNING: ''Model'' input ignored.  Used meastuneresp to get the model chromaticity response matrix.');
76        varargin(i) = [];
77    elseif strcmpi(varargin{i},'Model')
78        fprintf('WARNING: ''Model'' input ignored.  Used meastuneresp to get the model tune response matrix.');
79        varargin(i) = [];
80    elseif strcmpi(varargin{i},'NoEnergyScaling')
81        InputFlags = [InputFlags varargin(i)];
82        varargin(i) = [];
83    elseif strcmpi(varargin{i},'EnergyScaling')
84        InputFlags = [InputFlags varargin(i)];
85        varargin(i) = [];
86    elseif strcmpi(varargin{i},'Physics')
87        InputFlags = [InputFlags varargin(i)];
88        varargin(i) = [];
89    elseif strcmpi(varargin{i},'Hardware')
90        InputFlags = [InputFlags varargin(i)];
91        varargin(i) = [];
92    elseif strcmpi(varargin{i},'FileName')
93        if length(varargin) >= i+1 & ischar(varargin{i+1})
94            FileName = varargin{i+1};
95            varargin(i:i+1) = [];
96        else
97            varargin(i) = [];
98        end
99        if isempty(FileName)
100            DirectoryName = getfamilydata('Directory', 'TuneResponse');
101            [FileName, DirectoryName] = uigetfile('*.mat', 'Select a TUNE response matrix file', DirectoryName);
102            InputFlags = [InputFlags {[DirectoryName FileName]}];
103        else
104            InputFlags = [InputFlags {FileName}];
105        end
106    end
107end
108
109ActuatorFamilyDefault = findmemberof('Tune Corrector')';
110if isempty(ActuatorFamilyDefault)
111    ActuatorFamilyDefault = {'Q7','Q9'};
112end
113
114if length(varargin) == 0
115    FamilyNameCell = ActuatorFamilyDefault;
116    DeviceListCell = cell(size(ActuatorFamilyDefault));
117    NumFamilies = length(FamilyNameCell);
118   
119elseif length(varargin) == 1 & isempty(varargin{1})
120    FamilyNameCell = ActuatorFamilyDefault;
121    DeviceListCell = cell(size(ActuatorFamilyDefault));
122    NumFamilies = length(FamilyNameCell);
123   
124elseif iscell(varargin{1})
125    FamilyNameCell = varargin{1};
126    varargin(1) = [];
127    if length(varargin) >= 1
128        DeviceListCell = varargin{1};
129        varargin(1) = [];
130    else
131        for i = 1:length(FamilyNameCell)
132            DeviceListCell{i} = [];
133        end
134    end
135    NumFamilies = length(FamilyNameCell);
136    if ~iscell(DeviceListCell)
137        error('If FamilyName is a cell array then DeviceList must be a cell array')
138    end
139   
140elseif isstruct(varargin{1})
141    NumFamilies = 0;
142    while ~isempty(varargin)
143        % Look for Family and DeviceList
144        if length(varargin) >= 1
145            if ~isstruct(varargin{1})
146                break
147            end
148            if ~isfamily(varargin{1}.FamilyName)
149                error('Unknown family name in data structure');
150            end
151            NumFamilies = NumFamilies + 1;
152            FamilyNameCell{NumFamilies} = varargin{1}.FamilyName;
153            DeviceListCell{NumFamilies} = varargin{1}.DeviceList;
154            varargin(1) = [];
155        end
156    end
157   
158else
159    NumFamilies = 0;
160    while ~isempty(varargin)
161        % Look for Family and DeviceList
162        if length(varargin) >= 1
163            if ~isfamily(varargin{1})
164                break
165            end
166            NumFamilies = NumFamilies + 1;
167            FamilyNameCell{NumFamilies} = varargin{1};
168            varargin(1) = [];
169        end
170        if length(varargin) >= 1
171            if isnumeric(varargin{1}) | isempty(varargin{1})
172                DeviceListCell{NumFamilies} = varargin{1};
173                varargin(1) = [];
174            else
175                DeviceListCell{NumFamilies} = [];
176            end
177        else
178            DeviceListCell{NumFamilies} = [];
179        end
180    end
181end
182
183% FileName should be the next input (if a string, or [] for dialog box)
184if length(varargin) >= 1
185    if ischar(varargin{1}) | isempty(varargin{1})
186        FileName = varargin{1};
187        varargin(1) = [];
188
189        if isempty(FileName)
190            % Note: This only works if all families are in the same file
191            DirectoryName = getfamilydata('Directory', 'TuneResponse'); 
192            [FileName, DirectoryName, FilterIndex] = uigetfile('*.mat','Select Quadrupole-to-Tune Response Matrix File', DirectoryName);
193            if FilterIndex == 0
194                TuneMatrix = [];
195                FileName = [];
196                return
197            end
198           
199            FileName = [DirectoryName FileName];
200            InputFlags = [{FileName} InputFlags];
201        end
202    end
203end
204
205
206% The only thing left on the input line can be energy which can be left in varargin
207
208
209% Get the response matrix
210if NumericFlag == 1
211    TuneMatrix = [];
212end
213for i = 1:NumFamilies
214    if NumericFlag == 1
215        try
216            [M, FileName] = getrespmat('TUNE', [1 1;1 2], FamilyNameCell{i}, DeviceListCell{i}, 'Numeric', InputFlags{:}, varargin{:});
217        catch
218            fprintf('   Could not find a TUNE response matrix file, so using the model (%s).\n', FamilyNameCell{i});
219            M = meastuneresp(FamilyNameCell{i}, DeviceListCell{i}, 'Model', 'Numeric', InputFlags{:}, varargin{:});
220            %M = measrespmat('TUNE', [1 1;1 2], FamilyNameCell{i}, DeviceListCell{i}, 'Model', 'Numeric', InputFlags{:}, varargin{:});
221            FileName = '';
222        end
223        TuneMatrix = [TuneMatrix sum(M,2)];
224    else
225        try
226            [TuneMatrix(1,i), FileName] = getrespmat('TUNE', [1 1;1 2], FamilyNameCell{i}, DeviceListCell{i}, 'Struct', InputFlags{:}, varargin{:});
227        catch
228            fprintf('   Could not find a TUNE response matrix file, so using the model (%s).\n', FamilyNameCell{i});
229            TuneMatrix(1,i) = meastuneresp(FamilyNameCell{i}, DeviceListCell{i}, 'Model', 'Struct', InputFlags{:}, varargin{:});
230            FileName = '';
231        end
232    end
233end
234
235
Note: See TracBrowser for help on using the repository browser.