source: MML/trunk/mml/getmachineconfig.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: 13.7 KB
Line 
1function [ConfigSetpoint, ConfigMonitor, FileName] = getmachineconfig(varargin)
2%GETMACHINECONFIG - Returns or saves to file the present storage ring setpoints and monitors
3%  [ConfigSetpoint, ConfigMonitor, FileName] = getmachineconfig(Family, FileName, ExtraInputs ...)
4%  [ConfigSetpoint, ConfigMonitor, FileName] = getmachineconfig(FileName, ExtraInputs ...)
5
6%  INPUTS
7%  1. Family - String, string matrix, cell array of families
8%              {Default: all families that are a memberof 'MachineConfig'}
9%  2. FileName - File name to storage data (if necessary, include full path)
10%                'Archive' will archive to the default <Directory\ConfigData\CNFArchiveFile>
11%                          use '' to browse for a directory and file.  As usual, 'Archive',''
12%                          and 'Archive', Filename will also work.
13%                'Golden' will make the present lattice the "golden lattice" which is
14%                         stored in <OpsData.LatticeFile>
15%                If FileName is not input, then the configuration will not be saved to file.
16%  3. ExtraInputs - Extra inputs get passed to getsp and getam (like 'Online' or 'Simulator')
17%
18%  OUTPUTS
19%  1. ConfigSetpoint - Structure of setpoint structures
20%                      each field being a family
21%  2. ConfigMonitor - Structure of monitor structures
22%                     each field being a family
23%  3. FileName - If data was archived, filename where the data was saved (including the path)
24%
25%  NOTE
26%  1. Use setmachineconfig to save a configuration to file
27%  2. Unknown families will be ignored
28%  3. Use getmachineconfig('Golden') to store the default golden lattice
29%  4. Use getmachineconfig('Archive') to archive a lattice
30%
31%  See also setmachineconfig, getproductionlattice, getinjectionlattice
32
33%
34% Written by Jeff Corbett & Greg Portmann
35% Laurent S. Nadolski - October 2010 Modification
36% Main Config if output is empty
37% Main Config result in a temporary file
38
39
40DirStart = pwd;
41
42ConfigSetpoint = [];
43ConfigMonitor = [];
44if nargout == 0
45    ArchiveFlag = 1;
46    FileName = '';
47else
48    ArchiveFlag = 0;
49    FileName = -1;
50end
51DisplayFlag = 1;
52
53FileName = '';
54
55% Look for keywords on the input line
56InputFlags = {};
57for i = length(varargin):-1:1
58    if isstruct(varargin{i})
59        % Ignor structures
60    elseif iscell(varargin{i})
61        % Ignor cells
62    elseif strcmpi(varargin{i},'struct')
63        % Remove
64        varargin(i) = [];
65    elseif strcmpi(varargin{i},'numeric')
66        % Remove
67        varargin(i) = [];
68    elseif strcmpi(varargin{i},'simulator') || strcmpi(varargin{i},'model')
69        InputFlags = [InputFlags varargin(i)];
70        varargin(i) = [];
71    elseif strcmpi(varargin{i},'Online')
72        InputFlags = [InputFlags varargin(i)];
73        varargin(i) = [];
74    elseif strcmpi(varargin{i},'Manual')
75        InputFlags = [InputFlags varargin(i)];
76        varargin(i) = [];
77    elseif strcmpi(varargin{i},'physics')
78        InputFlags = [InputFlags varargin(i)];
79        varargin(i) = [];
80    elseif strcmpi(varargin{i},'hardware')
81        InputFlags = [InputFlags varargin(i)];
82        varargin(i) = [];
83    elseif strcmpi(varargin{i},'Display')
84        DisplayFlag = 1;
85        varargin(i) = [];
86    elseif strcmpi(varargin{i},'NoDisplay')
87        DisplayFlag = 0;
88        varargin(i) = [];
89    elseif strcmpi(varargin{i},'archive')
90        ArchiveFlag = 1;
91        if length(varargin) > i
92            % Look for a filename as the next input
93            if ischar(varargin{i+1})
94                FileName = varargin{i+1};
95                varargin(i+1) = [];
96            end
97        end
98        varargin(i) = [];
99    elseif strcmpi(varargin{i},'noarchive')
100        ArchiveFlag = 0;
101        varargin(i) = [];
102    end
103end
104
105
106if isempty(varargin)
107    FamilyName = getfamilylist;
108else
109    if iscell(varargin{1})
110        FamilyName = varargin{1};
111    elseif size(varargin{1},1) > 1
112        FamilyName = varargin{1};
113    elseif isfamily(varargin{1})
114        FamilyName = varargin{1};
115    else
116        FamilyName = getfamilylist;
117        FileName = varargin{1};
118        ArchiveFlag = 1;
119    end
120    varargin(1) = [];
121end
122if length(varargin) >= 1
123    FileName = varargin{1};
124    varargin(1) = [];
125    ArchiveFlag = 1;
126end
127
128
129
130% Archive data structure
131if ArchiveFlag
132    if isempty(FileName)
133        FileName = appendtimestamp(getfamilydata('Default', 'CNFArchiveFile'));
134        DirectoryName = getfamilydata('Directory','ConfigData');
135        if isempty(DirectoryName)
136            DirectoryName = [getfamilydata('Directory','DataRoot') 'MachineConfig', filesep];
137        else
138            % Make sure default directory exists
139            DirStart = pwd;
140            [DirectoryName, ErrorFlag] = gotodirectory(DirectoryName);
141            cd(DirStart);
142        end
143        [FileName, DirectoryName] = uiputfile('*.mat', 'Save Lattice to ...', [DirectoryName FileName]);
144        if FileName == 0
145            disp('   Lattice configuration not saved (getmachineconfig).');
146            return
147        end
148        FileName = [DirectoryName, FileName];
149       
150    elseif FileName == -1
151        FileName = appendtimestamp(getfamilydata('Default', 'CNFArchiveFile'));
152        DirectoryName = getfamilydata('Directory','ConfigData');
153        if isempty(DirectoryName)
154            DirectoryName = [getfamilydata('Directory','DataRoot') 'MachineConfig', filesep];
155        end
156        FileName = [DirectoryName, FileName];
157       
158    elseif strcmpi(FileName, 'Golden') || strcmpi(FileName, 'Production')
159        % Get the production file name (full path)
160        % AD.OpsData.LatticeFile could have the full path else default to AD.Directory.OpsData
161        FileName = getfamilydata('OpsData','LatticeFile');
162        [DirectoryName, FileName, Ext, VerNumber] = fileparts(FileName);
163        if isempty(DirectoryName)
164            DirectoryName = getfamilydata('Directory', 'OpsData');
165        end
166        FileNameGolden = [FileName, '.mat'];
167        FileName = fullfile(DirectoryName,[FileName, '.mat']);
168       
169        if exist(FileName,'file')
170            AnswerString = questdlg({'Are you sure you want to overwrite the default lattice file?',sprintf('%s',FileName)},'Default Lattice','Yes','No','No');
171        else
172            AnswerString = 'Yes';
173        end
174
175        if ~strcmp(AnswerString,'Yes')
176            disp('   Lattice configuration not saved (getmachineconfig).');
177            return;
178        end
179       
180        % Backup first
181        if exist(FileName,'file')
182            DirStart = pwd;
183            %BackupDirectoryName = [getfamilydata('Directory','DataRoot') 'Backup' filesep];
184            %BackupDataFileName  = prependtimestamp(FileNameGolden);
185            BackupDirectoryName = [getfamilydata('Directory','ConfigData'), 'GoldenBackup', filesep];
186
187            try
188                load(FileName,'ConfigSetpoint');
189                Fields = fieldnames(ConfigSetpoint);
190                BackupDataFileName = prependtimestamp(FileNameGolden, ConfigSetpoint.(Fields{1}).Setpoint.TimeStamp);
191                clear ConfigSetpoint
192            catch
193                fprintf('   Unknown time stamp on the old production lattice file, so backup file has the present time in the filename.\n');
194                BackupDataFileName = prependtimestamp(FileNameGolden);
195            end
196
197            [FinalDir, ErrorFlag] = gotodirectory(BackupDirectoryName);
198            if ~ErrorFlag
199                copyfile(FileName, [BackupDirectoryName, BackupDataFileName], 'f');
200                fprintf('   File %s backup to %s\n', FileName, [BackupDirectoryName, BackupDataFileName]);
201            else
202                fprintf('   Problem finding/creating backup directory, hence backup made to the present directory.\n');
203                copyfile(FileName, BackupDataFileName, 'f');
204            end
205            cd(DirStart);
206        end
207    elseif strcmpi(FileName, 'Injection')
208        % Get the injection file name (full path)
209        % AD.OpsData.InjectionFile could have the full path else default to AD.Directory.OpsData
210        FileName = getfamilydata('OpsData','InjectionFile');
211        [DirectoryName, FileName, Ext, VerNumber] = fileparts(FileName);
212        if isempty(DirectoryName)
213            DirectoryName = getfamilydata('Directory', 'OpsData');
214        end
215        FileNameGolden = [FileName, '.mat'];
216        FileName = fullfile(DirectoryName,[FileName, '.mat']);
217               
218        if exist(FileName,'file')
219            AnswerString = questdlg({'Are you sure you want to overwrite the default injection file?',sprintf('%s',FileName)},'Default Lattice','Yes','No','No');
220        else
221            AnswerString = 'Yes';
222        end
223        if ~strcmp(AnswerString,'Yes')
224            disp('   Injection configuration not saved (getmachineconfig).');
225            return;
226        end
227       
228        % Backup first
229        if exist(FileName,'file')
230            DirStart = pwd;
231            %BackupDirectoryName = [getfamilydata('Directory','DataRoot') 'Backup' filesep];
232            BackupDirectoryName = [getfamilydata('Directory','ConfigData'), 'GoldenBackup', filesep];
233
234            try
235                load(FileName,'ConfigSetpoint');
236                Fields = fieldnames(ConfigSetpoint);
237                BackupDataFileName = prependtimestamp(FileNameGolden, ConfigSetpoint.(Fields{1}).Setpoint.TimeStamp);
238                clear ConfigSetpoint
239            catch
240                fprintf('   Unknown time stamp on the old injection lattice file, so backup file has the present time in the filename.\n');
241                BackupDataFileName = prependtimestamp(FileNameGolden);
242            end
243
244            [FinalDir, ErrorFlag] = gotodirectory(BackupDirectoryName);
245            if ~ErrorFlag
246                copyfile(FileName, [BackupDirectoryName, BackupDataFileName], 'f');
247                fprintf('   File %s backup to %s\n', FileName, [BackupDirectoryName, BackupDataFileName]);
248            else
249                fprintf('   Problem finding/creating backup directory, hence backup made to the present directory.\n');
250                copyfile(FileName, BackupDataFileName, 'f');
251            end
252            cd(DirStart);
253        end
254    end
255end
256
257
258% Get the number of families
259if iscell(FamilyName)
260    N = length(FamilyName);
261else
262    N = size(FamilyName,1);
263end
264
265
266% Loop over all families
267for i = 1:N
268    if iscell(FamilyName)
269        Family = deblank(FamilyName{i});       
270    else
271        Family = deblank(FamilyName(i,:));
272    end
273           
274   
275    % Get the setpoint
276    if ismemberof(Family, 'MachineConfig')
277        % Get the Setpoint field
278        Field = 'Setpoint';
279        try
280            if ~isempty(getfamilydata(Family, Field))
281                ConfigSetpoint.(Family).(Field) = getpv(Family, Field, 'Struct', InputFlags{:});
282            end
283        catch
284            fprintf('   Trouble with getpv(''%s'',''%s''), hence ignored (getmachineconfig)\n', Family, Field);
285        end
286    end
287   
288    % Look if any other fields are part of the MachineConfig
289    AOFamily = getfamilydata(Family);
290    FieldNameCell = fieldnames(AOFamily);
291    for j = 1:size(FieldNameCell,1)
292        if isfield(AOFamily.(FieldNameCell{j}),'MemberOf')
293            if any(strcmpi(AOFamily.(FieldNameCell{j}).MemberOf, 'MachineConfig'))
294                try
295                    ConfigSetpoint.(Family).(FieldNameCell{j}) = getpv(Family, FieldNameCell{j}, 'Struct', InputFlags{:});
296                catch
297                    fprintf('   Trouble with getpv(''%s'',''%s''), hence ignored (getmachineconfig)\n', Family, FieldNameCell{j});
298                end               
299            end
300        end
301    end
302   
303   
304    % Get the monitors
305    if nargout >= 2 || ArchiveFlag
306        if ismemberof(Family, 'MachineConfig') || ismemberof(Family, 'Setpoint', 'MachineConfig') || ismemberof(Family, 'BPM') || strcmp(Family, 'DCCT')
307            try
308                if ~isempty(getfamilydata(Family, 'Monitor'))
309                    ConfigMonitor.(Family).Monitor = getam(Family, 'Struct', InputFlags{:});
310                end
311            catch
312                fprintf('   Trouble with getam(%s), hence ignored (getmachineconfig)\n', Family);
313            end
314        end
315    end
316end
317
318
319% % Get other RF channels
320% if nargout >= 2 | ArchiveFlag
321%     try
322%         ConfigMonitor.RFPower = getpv('RF', 'Power', 'Struct', InputFlags{:});
323%         ConfigMonitor.RFVoltage = getpv('RF', 'Voltage', 'Struct', InputFlags{:});
324%         ConfigMonitor.KlysPower = getpv('RF', 'KlysPower', 'Struct', InputFlags{:});
325%     catch
326%         %fprintf('   Trouble getting RF KlysPower, hence ignored (getmachineconfig)\n');
327%     end
328% end
329
330
331
332% Put fields in alphabetical order
333% (Not a good idea to change the set order)
334% if ~isempty(ConfigSetpoint)
335%     ConfigSetpoint = orderfields(ConfigSetpoint);
336% end
337
338
339if nargout >= 2 || ArchiveFlag
340    if ~isempty(ConfigMonitor)
341        ConfigMonitor = orderfields(ConfigMonitor);
342    end
343end
344
345
346if ArchiveFlag
347    % If the filename contains a directory then make sure it exists
348    [DirectoryName, FileName, Ext] = fileparts(FileName);
349    DirStart = pwd;
350    [DirectoryName, ErrorFlag] = gotodirectory(DirectoryName);
351    save(FileName, 'ConfigMonitor', 'ConfigSetpoint');
352    cd(DirStart);
353    FileName = [DirectoryName FileName];
354   
355    if DisplayFlag
356        fprintf('   Machine configuration data saved to %s.mat\n', FileName);
357        if ErrorFlag
358            fprintf('   Warning:  The lattice file was saved, but it did not go the desired directory');
359            fprintf('   Check %s for your data\n', DirectoryName);
360        end
361    end
362else
363    FileName = '';
364end
365
366
367
368% Special function call for further updates
369% Note that the eval allows one to run it has a script (for better or worse).
370ExtraSetFunction = getfamilydata('getmachineconfigfunction');
371
372if ~isempty(ExtraSetFunction)
373    try
374        eval(ExtraSetFunction);
375    catch
376        fprintf('\n%s\n', lasterr);
377        fprintf('   Warning: %s did not compete without error in getmachineconfig.', ExtraSetFunction);
378    end
379end
Note: See TracBrowser for help on using the repository browser.