1 | function [Data, iNotFound] = getphysdata(Family, Field, DeviceList) |
---|
2 | %GETPHYSDATA - Gets physics data |
---|
3 | % Data = getphysdata(Family, Field, DeviceList) |
---|
4 | % Data = getphysdata(Family, Field) |
---|
5 | % Data = getphysdata(DataStruct, Field) |
---|
6 | % Data = getphysdata(Family) |
---|
7 | % Data = getphysdata |
---|
8 | % |
---|
9 | % INPUTS |
---|
10 | % 1. Family = Family name |
---|
11 | % 2. Field = Field name ('Offset', 'Gain', etc) |
---|
12 | % 3. DeviceList = Device list for that family |
---|
13 | % or |
---|
14 | % 1. DataStruct = Data structure (.FamilyName and .DeviceList fields are used, |
---|
15 | % unless DeviceList is an input) |
---|
16 | % 2. Field = Field name ('Offset', 'Gain', etc) |
---|
17 | % |
---|
18 | % OUTPUT |
---|
19 | % 1. Data = Physics data |
---|
20 | % |
---|
21 | % NOTES |
---|
22 | % 1. If the inputs are cell arrays, then the output is a cell array |
---|
23 | % 2. If Family is a cell array, then DeviceList must also be a cell array, |
---|
24 | % however, Field can either be a cell array array or a string. |
---|
25 | % 3. If DeviceList is not an input, then the exact contents of the field is returned |
---|
26 | % If DeviceList exists, then Data is indexed according to DeviceList (vector output) |
---|
27 | % If DeviceList is empty, [], then the entire DeviceList is returned |
---|
28 | % |
---|
29 | % EXAMPLES |
---|
30 | % 1. getphysdata: return full structure |
---|
31 | % 2. getphysdata('BPMx') |
---|
32 | % 3. getphysdata('BPMx','golden') |
---|
33 | % 4. getphysdata(getx('Struct'),'Golden') returns a structure |
---|
34 | % |
---|
35 | % See Also setphysdata, makephysdata |
---|
36 | |
---|
37 | % |
---|
38 | % Written by Gregory J. Portmann |
---|
39 | |
---|
40 | if nargin == 0 |
---|
41 | Data = getphysdatalocal; |
---|
42 | return |
---|
43 | end |
---|
44 | |
---|
45 | |
---|
46 | %%%%%%%%%%%%%%%%%%%% |
---|
47 | % Cell array input % |
---|
48 | %%%%%%%%%%%%%%%%%%%% |
---|
49 | if iscell(Family) |
---|
50 | if nargin >= 2 |
---|
51 | if iscell(Field) |
---|
52 | if length(Family) ~= length(Field) |
---|
53 | error('If Field is a cell array, then it must be the same size as Family.'); |
---|
54 | end |
---|
55 | end |
---|
56 | end |
---|
57 | for i = 1:length(Family) |
---|
58 | if nargin == 1 |
---|
59 | Data{i} = getphysdata(Family{i}); |
---|
60 | elseif nargin == 2 |
---|
61 | if iscell(Field) |
---|
62 | Data{i} = getphysdata(Family{i}, Field{i}); |
---|
63 | else |
---|
64 | Data{i} = getphysdata(Family{i}, Field); |
---|
65 | end |
---|
66 | else |
---|
67 | if iscell(Field) |
---|
68 | if iscell(DeviceList) |
---|
69 | Data{i} = getphysdata(Family{i}, Field{i}, DeviceList{i}); |
---|
70 | else |
---|
71 | Data{i} = getphysdata(Family{i}, Field{i}, DeviceList); |
---|
72 | end |
---|
73 | else |
---|
74 | if iscell(DeviceList) |
---|
75 | Data{i} = getphysdata(Family{i}, Field, DeviceList{i}); |
---|
76 | else |
---|
77 | Data{i} = getphysdata(Family{i}, Field, DeviceList); |
---|
78 | end |
---|
79 | end |
---|
80 | end |
---|
81 | end |
---|
82 | return |
---|
83 | end % End cell inputs |
---|
84 | |
---|
85 | %%%%%%%%%%%%%%%% |
---|
86 | % Get the data % |
---|
87 | %%%%%%%%%%%%%%%% |
---|
88 | % Load a full PhysData structure |
---|
89 | |
---|
90 | DataStruct = getphysdatalocal; |
---|
91 | |
---|
92 | if nargin == 1 |
---|
93 | if isstruct(Family) |
---|
94 | Family = Family.FamilyName; |
---|
95 | end |
---|
96 | if isfield(DataStruct, Family) |
---|
97 | Data = DataStruct.(Family); |
---|
98 | else |
---|
99 | error(sprintf('%s family not found in the Physics Data Structure', Family)); |
---|
100 | end |
---|
101 | return |
---|
102 | end |
---|
103 | |
---|
104 | %% STRUCTURE CASE |
---|
105 | |
---|
106 | if isstruct(Family) |
---|
107 | if nargin < 3 |
---|
108 | % Only override the DeviceList if it is not input |
---|
109 | if isfield(Family, 'DeviceList') |
---|
110 | DeviceList = Family.DeviceList; |
---|
111 | else |
---|
112 | DeviceList = Family.Monitor.DeviceList; |
---|
113 | end |
---|
114 | end |
---|
115 | Family = Family.FamilyName; |
---|
116 | StructFlag = 1; |
---|
117 | else |
---|
118 | if nargin < 3 |
---|
119 | DeviceList = []; |
---|
120 | end |
---|
121 | StructFlag = 0; |
---|
122 | end |
---|
123 | if isempty(DeviceList) |
---|
124 | try |
---|
125 | DeviceList = getlist(Family); |
---|
126 | catch |
---|
127 | % Not a family. Return whatever is there. |
---|
128 | end |
---|
129 | end |
---|
130 | |
---|
131 | Data = DataStruct.(Family); |
---|
132 | |
---|
133 | if isfield(DataStruct.(Family), Field) |
---|
134 | Data = DataStruct.(Family).(Field); |
---|
135 | |
---|
136 | if isstruct(Data) |
---|
137 | % Data structure is in the data field of PhysData |
---|
138 | if nargin < 3 & StructFlag==1 |
---|
139 | % Return the entire structure |
---|
140 | [i, iNotFound] = findrowindex(DeviceList, Data.DeviceList); |
---|
141 | Data.Data = Data.Data(i); |
---|
142 | Data.DeviceList = Data.DeviceList(i); |
---|
143 | if isfield(Data,'Status') |
---|
144 | if size(Data.Status,1) == size(Data.DeviceList,1) |
---|
145 | Data.Status = Data.Status(i); |
---|
146 | end |
---|
147 | end |
---|
148 | return |
---|
149 | else |
---|
150 | % Return the indexed data |
---|
151 | if isfield(Data, 'DeviceList') |
---|
152 | % Data structure |
---|
153 | DeviceListTotal = Data.DeviceList; |
---|
154 | Data = Data.Data; |
---|
155 | elseif isfield(Data, 'Monitor') |
---|
156 | if isfield(Data.Monitor, 'DeviceList') |
---|
157 | % Response matrix structure (or dispersion) |
---|
158 | DeviceListTotal = Data.Monitor.DeviceList; |
---|
159 | Data = Data.Data; |
---|
160 | else |
---|
161 | error('Not sure how to index the data structure'); |
---|
162 | end |
---|
163 | else |
---|
164 | error('Not sure how to index the data structure'); |
---|
165 | end |
---|
166 | end |
---|
167 | |
---|
168 | elseif isempty(DeviceList) |
---|
169 | return |
---|
170 | else |
---|
171 | % All data must be stored including bad devices or as a structure |
---|
172 | DeviceListTotal = family2dev(Family,0); |
---|
173 | end |
---|
174 | |
---|
175 | % Data vector was saved |
---|
176 | if length(Data) == 1 |
---|
177 | % Repeat the one row Data vector as many times as in the rows of DeviceListTotal |
---|
178 | Data = ones(size(DeviceList,1),1) * Data; |
---|
179 | else |
---|
180 | [i, iNotFound] = findrowindex(DeviceList, DeviceListTotal); |
---|
181 | Data = Data(i,:); |
---|
182 | |
---|
183 | if ~isempty(iNotFound) |
---|
184 | for i = 1:length(iNotFound) |
---|
185 | fprintf(' Can''t find %s.%s(%d,%d) in the family DeviceList.\n', Family, Field, DeviceList(iNotFound(i),1), DeviceList(iNotFound(i),2)); |
---|
186 | end |
---|
187 | %error(sprintf('%d Devices not found', length(iNotFound))); |
---|
188 | fprintf(' Missing data replaced with NaN.\n'); |
---|
189 | for j = 1:length(iNotFound) |
---|
190 | if iNotFound(j) == 1 |
---|
191 | Data = [NaN*ones(1,size(Data,2)); Data(1:end,:)]; |
---|
192 | else |
---|
193 | Data = [Data(1:iNotFound(j)-1,:); NaN*ones(1,size(Data,2)); Data(iNotFound(j):end,:)]; |
---|
194 | end |
---|
195 | end |
---|
196 | end |
---|
197 | |
---|
198 | end |
---|
199 | else |
---|
200 | error(sprintf('%s family, %s field not found in the Physics Data Structure', Family, Field)); |
---|
201 | end |
---|
202 | |
---|
203 | |
---|
204 | %% Subfunction |
---|
205 | %=================================== |
---|
206 | function Data = getphysdatalocal |
---|
207 | %=================================== |
---|
208 | |
---|
209 | % Physics data is saved in this file |
---|
210 | FileName = getfamilydata('OpsData','PhysDataFile'); |
---|
211 | |
---|
212 | %FileName = [getfamilydata('Directory','OpsData') getfamilydata('OpsData','PhysDataFile')]; |
---|
213 | |
---|
214 | %Machine = lower(getfamilydata('Machine')); |
---|
215 | %FileName = which([Machine, 'physdata','.mat']); |
---|
216 | |
---|
217 | %tmp = load([DirectoryName FileName]); |
---|
218 | tmp = load(FileName); |
---|
219 | |
---|
220 | fname = fieldnames(tmp); |
---|
221 | if length(fname) == 1 |
---|
222 | Data = tmp.(fname{1}); |
---|
223 | else |
---|
224 | try |
---|
225 | Data = tmp.('PhysData'); |
---|
226 | catch |
---|
227 | error('More than 2 variables exist in the calibration file %s, and PhysData is not one of them',FileName); |
---|
228 | end |
---|
229 | end |
---|
230 | |
---|