1 | function setphysdata(varargin) |
---|
2 | %SETPHYSDATA - Set physics data |
---|
3 | % For vector inputs: |
---|
4 | % setphysdata(Family, Field, Data, DeviceList) |
---|
5 | % setphysdata(Family, Field, Data) |
---|
6 | % |
---|
7 | % For structure inputs: |
---|
8 | % setphysdata(DataStruct, Field) % Family, Data, and DeviceList are in DataStruct |
---|
9 | % setphysdata(Family, DataStruct) % DataStruct is the entire family structure |
---|
10 | % setphysdata(DataStruct) % DataStruct is the entire Physics Data Structure |
---|
11 | % |
---|
12 | % INPUTS |
---|
13 | % 1. Family = Family name (or first field name in the physdata structure) |
---|
14 | % 2. Field = Field name ('Offset', 'Gain', 'Golden', etc) |
---|
15 | % 3. Data = New data values |
---|
16 | % 4. DeviceList = Device list for that family |
---|
17 | % or |
---|
18 | % 1. DataStruct = .FamilyName, .DeviceList, and .Data fields are used if they exist |
---|
19 | % or DataStruct is saved according to the Field/Family |
---|
20 | % 2. Field = Field name ('Offset', 'Gain', 'Golden', etc) |
---|
21 | % |
---|
22 | % NOTE |
---|
23 | % 1. If Data is a cell array, then Family, Field, and DeviceList must also be a cell arrays. |
---|
24 | % 2. If the family or field does not exist, then it will be created! |
---|
25 | % When a new field is created a message will be printed to the screen. |
---|
26 | % |
---|
27 | % EXAMPLES |
---|
28 | % 1. To set the offset orbit for BPM(3,2) to 1.234 |
---|
29 | % setphysdata('BPMx', 'Offset', 1.234, [3 2]); |
---|
30 | % |
---|
31 | % See Also makephysdata, getphysdata |
---|
32 | |
---|
33 | % |
---|
34 | % NOTE: MML creators are phasing out the use of physdata. |
---|
35 | % |
---|
36 | % Written by Greg Portmann |
---|
37 | |
---|
38 | |
---|
39 | % ArchiveFlag is a backup to DataRoot\PhysData |
---|
40 | ArchiveFlag = 1; |
---|
41 | |
---|
42 | % Look for keywords on the input line |
---|
43 | for i = length(varargin):-1:1 |
---|
44 | if isstruct(varargin{i}) |
---|
45 | % Ignor structures |
---|
46 | elseif iscell(varargin{i}) |
---|
47 | % Ignor cells |
---|
48 | elseif strcmpi(varargin{i},'NoArchive') |
---|
49 | ArchiveFlag = 0; |
---|
50 | varargin(i) = []; |
---|
51 | elseif strcmpi(varargin{i},'Archive') |
---|
52 | ArchiveFlag = 1; |
---|
53 | varargin(i) = []; |
---|
54 | end |
---|
55 | end |
---|
56 | |
---|
57 | % setphysdata(Family, Field, Data, DeviceList) |
---|
58 | if length(varargin) == 0 |
---|
59 | error('At least one input required'); |
---|
60 | end |
---|
61 | if length(varargin) >= 1 |
---|
62 | Family = varargin{1}; |
---|
63 | end |
---|
64 | if length(varargin) >= 2 |
---|
65 | Field = varargin{2}; |
---|
66 | end |
---|
67 | if length(varargin) >= 3 |
---|
68 | Data = varargin{3}; |
---|
69 | end |
---|
70 | if length(varargin) >= 4 |
---|
71 | DeviceList = varargin{4}; |
---|
72 | else |
---|
73 | DeviceList = []; |
---|
74 | end |
---|
75 | |
---|
76 | %%%%%%%%%%%%%%%%%%%% |
---|
77 | % Cell array input % |
---|
78 | %%%%%%%%%%%%%%%%%%%% |
---|
79 | if iscell(Family) |
---|
80 | if length(varargin) < 2 |
---|
81 | error('For cell array inputs, Data and Family must exist'); |
---|
82 | end |
---|
83 | if ~iscell(Family) |
---|
84 | error('If Data is a cell array, then Family must be a cell array.'); |
---|
85 | end |
---|
86 | if length(Family) ~= length(Data) |
---|
87 | error('Data and Family must be the same size cell arrays'); |
---|
88 | end |
---|
89 | if length(varargin) >= 3 |
---|
90 | if ~iscell(Field) |
---|
91 | error('If Data is a cell array, then Field must be a cell array.'); |
---|
92 | end |
---|
93 | if length(Family) ~= length(Field) |
---|
94 | error('Data and Field must be the same size cell arrays'); |
---|
95 | end |
---|
96 | end |
---|
97 | if length(varargin) >= 4 |
---|
98 | if ~iscell(DeviceList) |
---|
99 | error('If Data is a cell array, then DeviceList must be a cell array.'); |
---|
100 | end |
---|
101 | if length(Family) ~= length(DeviceList) |
---|
102 | error('Data and DeviceList must be the same size cell arrays'); |
---|
103 | end |
---|
104 | end |
---|
105 | for i = 1:length(Data) |
---|
106 | if length(varargin) == 2 |
---|
107 | setphysdata(Family{i}, Field{i}); |
---|
108 | elseif length(varargin) == 3 |
---|
109 | setphysdata(Family{i}, Field{i}, Data{i}); |
---|
110 | else |
---|
111 | setphysdata(Family{i}, Field{i}, Data{i}, DeviceList{i}); |
---|
112 | end |
---|
113 | end |
---|
114 | return |
---|
115 | end % End cell inputs |
---|
116 | |
---|
117 | |
---|
118 | %%%%%%%%%%%%%%%% |
---|
119 | % Set the data % |
---|
120 | %%%%%%%%%%%%%%%% |
---|
121 | |
---|
122 | if length(varargin) == 1 |
---|
123 | if isstruct(Family) |
---|
124 | PhysData = Family; |
---|
125 | fprintf(' WARNING: The entire Physics Data Structure will be written over!'); |
---|
126 | savephysdatalocal(PhysData, ArchiveFlag); |
---|
127 | else |
---|
128 | error('For 1 input, the Data input must be the entire Physics Data Structure'); |
---|
129 | end |
---|
130 | return; |
---|
131 | end |
---|
132 | |
---|
133 | |
---|
134 | % Get the entire structure |
---|
135 | PhysData = getphysdata; |
---|
136 | |
---|
137 | |
---|
138 | if isstruct(Family) |
---|
139 | if isfield(Family, 'FamilyName') |
---|
140 | % setphysdata(DataStruct, Field) |
---|
141 | DeviceList = Family.DeviceList; |
---|
142 | Data = Family.Data; |
---|
143 | Family = Family.FamilyName; |
---|
144 | else |
---|
145 | error('When using setphysdata(DataStruct, Field), DataStruct must have .FamilyName, .Data, .DeviceList fields'); |
---|
146 | end |
---|
147 | elseif isstruct(Field) |
---|
148 | % setphysdata(Family, Data) |
---|
149 | % The data is in the field input |
---|
150 | if ~isfield(PhysData, Family) |
---|
151 | fprintf(' %s family will be created in the Physics Data Structure', Family); |
---|
152 | end |
---|
153 | PhysData.(Family) = Field; |
---|
154 | savephysdatalocal(PhysData, ArchiveFlag); |
---|
155 | return |
---|
156 | else |
---|
157 | if length(varargin) == 2 |
---|
158 | error('The Physics Data Structure for a family should be a structure'); |
---|
159 | end |
---|
160 | if length(varargin) == 3 |
---|
161 | DeviceList = []; |
---|
162 | end |
---|
163 | end |
---|
164 | if isempty(DeviceList) |
---|
165 | try |
---|
166 | DeviceList = getlist(Family, 0); |
---|
167 | catch |
---|
168 | % Not a family. Set the entire field |
---|
169 | if length(varargin) ~= 3 |
---|
170 | error('When not using a family the number of inputs must be 3 (Family, Field, Data)'); |
---|
171 | end |
---|
172 | DataOld = PhysData.(Family).(Field); |
---|
173 | if any(size(DataOld) ~= size(Data)) |
---|
174 | fprintf('\n WARNING: Data is not part of a family and length of old data does not match the length of the new data.\n'); |
---|
175 | fprintf(' If that is not ok, hopefully you saved a backup file\n\n'); |
---|
176 | end |
---|
177 | PhysData.(Family).(Field) = Data; |
---|
178 | savephysdatalocal(PhysData, ArchiveFlag); |
---|
179 | return |
---|
180 | end |
---|
181 | end |
---|
182 | |
---|
183 | |
---|
184 | % If Data is a structure, then use the Data and DeviceList field |
---|
185 | if isstruct(Data) |
---|
186 | if length(varargin) >= 4 |
---|
187 | fprintf('WARNING: Input DeviceList ignored. Using the DeviceList in the data structure.'); |
---|
188 | end |
---|
189 | if isfield(Data, 'DeviceList') |
---|
190 | DeviceList = Data.DeviceList; |
---|
191 | else |
---|
192 | DeviceList = Data.Monitor.DeviceList; |
---|
193 | end |
---|
194 | Data = Data.Data; |
---|
195 | end |
---|
196 | |
---|
197 | if ~isfield(PhysData, Family) |
---|
198 | fprintf(' %s family, %s field will be created in the Physics Data Structure', Family, Field); |
---|
199 | PhysData.(Family) = []; |
---|
200 | else |
---|
201 | if ~isfield(PhysData.(Family), Field) |
---|
202 | fprintf(' %s field will be created in the %s family in the Physics Data Structure', Field, Family); |
---|
203 | PhysData.(Family).(Field) = []; |
---|
204 | end |
---|
205 | end |
---|
206 | |
---|
207 | if size(DeviceList,2) == 1 |
---|
208 | DeviceList = elem2dev(Family, DeviceList); |
---|
209 | end |
---|
210 | |
---|
211 | |
---|
212 | DeviceListTotal = getlist(Family, 0); |
---|
213 | |
---|
214 | [i, iNotFound] = findrowindex(DeviceList, DeviceListTotal); |
---|
215 | if ~isempty(iNotFound) |
---|
216 | error('Device not found in the family'); |
---|
217 | end |
---|
218 | |
---|
219 | % Save data as a vector using the entire family (or change the device list in the structure?) |
---|
220 | if isfield(PhysData.(Family).(Field), 'DeviceList') |
---|
221 | % A data structure was stored. Convert it to an entire family list. |
---|
222 | j = findrowindex(PhysData.(Family).(Field).DeviceList, DeviceListTotal); |
---|
223 | DataTotal = NaN * ones(size(DeviceListTotal,1), size(PhysData.(Family).(Field).Data,2)); |
---|
224 | DataTotal(j,:) = PhysData.(Family).(Field).Data; |
---|
225 | |
---|
226 | % Fill the new data to the structure |
---|
227 | DataTotal(i,:) = Data; |
---|
228 | PhysData.(Family).(Field).Data = DataTotal; |
---|
229 | PhysData.(Family).(Field).DeviceList = DeviceListTotal; |
---|
230 | if isfield(PhysData.(Family).(Field),'Status') |
---|
231 | % Status isn't relevant |
---|
232 | PhysData.(Family).(Field) = rmfield(PhysData.(Family).(Field),'Status'); |
---|
233 | end |
---|
234 | elseif isfield(PhysData.(Family).(Field), 'Monitor') |
---|
235 | % A data structure was stored. Convert it to an entire family list. |
---|
236 | j = findrowindex(PhysData.(Family).(Field).Monitor.DeviceList, DeviceListTotal); |
---|
237 | DataTotal = NaN * ones(size(DeviceListTotal,1), size(PhysData.(Family).(Field).Data,2)); |
---|
238 | DataTotal(j,:) = PhysData.(Family).(Field).Data; |
---|
239 | |
---|
240 | % Fill the new data to the structure |
---|
241 | DataTotal(i,:) = Data; |
---|
242 | PhysData.(Family).(Field).Data = DataTotal; |
---|
243 | PhysData.(Family).(Field).DeviceList = DeviceListTotal; |
---|
244 | if isfield(PhysData.(Family).(Field),'Status') |
---|
245 | % Status isn't relevant |
---|
246 | PhysData.(Family).(Field) = rmfield(PhysData.(Family).(Field),'Status'); |
---|
247 | end |
---|
248 | else |
---|
249 | % Fill the new data |
---|
250 | DataTotal = PhysData.(Family).(Field); |
---|
251 | DataTotal(i,:) = Data; |
---|
252 | PhysData.(Family).(Field) = DataTotal; |
---|
253 | end |
---|
254 | |
---|
255 | |
---|
256 | % if length(i) == size(Data,1) |
---|
257 | % DataVec = getphysdata(Family, Field); |
---|
258 | % DataVec(i) = Data; |
---|
259 | % PhysData.(Family).(Field) = DataVec; |
---|
260 | % elseif length(Data) == 1 |
---|
261 | % PhysData.(Family).(Field) = Data; |
---|
262 | % else |
---|
263 | % error('The number of elements in the input data vector does not match the DeviceList'); |
---|
264 | % end |
---|
265 | |
---|
266 | |
---|
267 | savephysdatalocal(PhysData, ArchiveFlag); |
---|
268 | |
---|
269 | |
---|
270 | |
---|
271 | function savephysdatalocal(PhysData, ArchiveFlag) |
---|
272 | |
---|
273 | % Physics data is saved in this file |
---|
274 | FileName = getfamilydata('OpsData','PhysDataFile'); |
---|
275 | |
---|
276 | %FileName = [getfamilydata('Directory','OpsData') getfamilydata('OpsData','PhysDataFile')]; |
---|
277 | |
---|
278 | %Machine = lower(getfamilydata('Machine')); |
---|
279 | %FileName = which([Machine, 'physdata','.mat']); |
---|
280 | |
---|
281 | if ArchiveFlag |
---|
282 | tmp = questdlg({... |
---|
283 | sprintf('%s', FileName), ... |
---|
284 | 'is where many important parameters are saved to operate', ... |
---|
285 | 'this machine. A backup of this file will be made first. However, ', ... |
---|
286 | 'Are you sure you want to change the Physics Data Structure?'},... |
---|
287 | 'SETPHYSDATA','YES','NO','NO'); |
---|
288 | else |
---|
289 | tmp = questdlg({... |
---|
290 | sprintf('%s', FileName), ... |
---|
291 | 'is where many important parameters are saved to operate', ... |
---|
292 | 'this machine. You are about to change this file without a backup!', ... |
---|
293 | 'Are you sure you want to change the Physics Data Structure?'},... |
---|
294 | 'SETPHYSDATA','YES','NO','NO'); |
---|
295 | end |
---|
296 | if ~strcmpi(tmp,'YES') |
---|
297 | fprintf(' No change made to the Physics Data Structure\n'); |
---|
298 | return |
---|
299 | end |
---|
300 | |
---|
301 | if ArchiveFlag |
---|
302 | % Save a backup first |
---|
303 | DirStart = pwd; |
---|
304 | %DirectoryBackUp = getfamilydata('Directory','DataRoot'); |
---|
305 | %DirectoryBackUp = fullfile(DirectoryBackUp,'PhysData'); |
---|
306 | DirectoryBackUp = [getfamilydata('Directory','DataRoot'), 'Backup', filesep]; |
---|
307 | |
---|
308 | %FileNameBackUp = appendtimestamp('Physdata'); |
---|
309 | FileNameBackUp = prependtimestamp('Physdata'); |
---|
310 | |
---|
311 | [DirectoryBackUp, DirectoryErrorFlag] = gotodirectory(DirectoryBackUp); |
---|
312 | save(FileNameBackUp, 'PhysData'); |
---|
313 | fprintf(' Physics Data Structure backup to %s\n', [pwd, filesep, FileNameBackUp]); |
---|
314 | cd(DirStart); |
---|
315 | if DirectoryErrorFlag |
---|
316 | fprintf(' WARNING: The PhysData file was saved, but it did not go the desired directory!\n Look in %s\n', DirectoryBackUp); |
---|
317 | end |
---|
318 | end |
---|
319 | |
---|
320 | save(FileName, 'PhysData'); |
---|
321 | fprintf(' Physics Data Structure updated in %s\n', FileName); |
---|