1 | function DataOut = raw2real(varargin) |
---|
2 | %RAW2REAL - Converts raw control system data to calibrated values |
---|
3 | % RealData = raw2real(Family, Field, RawData, DeviceList) |
---|
4 | % RealData = raw2real(DataStruct) |
---|
5 | % |
---|
6 | % INPUTS |
---|
7 | % 1. Family - Family Name |
---|
8 | % DataStruct - Data Structure (.FamilyName, .Field, and .DeviceList fields are used if not input) |
---|
9 | % 2. RawData - Raw control system data |
---|
10 | % 3. DeviceList - Device list for that family |
---|
11 | % 4. Flag |
---|
12 | % 'Absolute' - Include the offset {Default (unless it's a response matrix)} |
---|
13 | % 'Incremental' - Gain only {Response matrices are incremental only} |
---|
14 | % 'Numeric' - forces numeric output for structure inputs |
---|
15 | % |
---|
16 | % OUTPUTS |
---|
17 | % 1. RealData - Calibrated data |
---|
18 | % RealData = Gain .* (RawData - Offset) |
---|
19 | % |
---|
20 | % ALGORITHM |
---|
21 | % RawData = (RealData ./ Gain) + Offset |
---|
22 | % RealData = Gain .* (RawData - Offset) |
---|
23 | % |
---|
24 | % NOTES |
---|
25 | % 1. If the input is a structure, then the output will be a structure |
---|
26 | % 2. If the inputs are cell arrays, then the output is a cell array |
---|
27 | % 3. If Family is a cell array, then all other inputs must be cell arrays |
---|
28 | % 4. The Offset is always in hardware units. |
---|
29 | % |
---|
30 | % See also real2raw, physics2hw, hw2physics, getoffset, getgain, getgolden |
---|
31 | % |
---|
32 | % Written by Greg Portmann |
---|
33 | |
---|
34 | |
---|
35 | %%%%%%%%%%%%%%%%% |
---|
36 | % Input parsing % |
---|
37 | %%%%%%%%%%%%%%%%% |
---|
38 | UnitsFlag = ''; |
---|
39 | NumericFlag = []; |
---|
40 | IncrementalFlag = {}; |
---|
41 | InputFlags = {}; |
---|
42 | for i = length(varargin):-1:1 |
---|
43 | if isstruct(varargin{i}) |
---|
44 | % Ignor structures |
---|
45 | elseif iscell(varargin{i}) |
---|
46 | % Ignor cells |
---|
47 | elseif strcmpi(varargin{i},'struct') |
---|
48 | NumericFlag = 0; |
---|
49 | InputFlags = [InputFlags varargin(i)]; |
---|
50 | varargin(i) = []; |
---|
51 | elseif strcmpi(varargin{i},'numeric') |
---|
52 | NumericFlag = 1; |
---|
53 | InputFlags = [InputFlags varargin(i)]; |
---|
54 | varargin(i) = []; |
---|
55 | elseif strcmpi(varargin{i},'Incremental') |
---|
56 | IncrementalFlag = varargin(i); |
---|
57 | varargin(i) = []; |
---|
58 | elseif strcmpi(varargin{i},'Absolute') |
---|
59 | IncrementalFlag = varargin(i); |
---|
60 | InputFlags = [InputFlags varargin(i)]; |
---|
61 | varargin(i) = []; |
---|
62 | elseif strcmpi(varargin{i},'simulator') || strcmpi(varargin{i},'model') || strcmpi(varargin{i},'Online') || strcmpi(varargin{i},'Manual') |
---|
63 | % Remove and ignor |
---|
64 | varargin(i) = []; |
---|
65 | elseif strcmpi(varargin{i},'physics') |
---|
66 | fprintf(' WARNING: ''Physics'' flag ignored by raw2real\n'); |
---|
67 | varargin(i) = []; |
---|
68 | elseif strcmpi(varargin{i},'hardware') |
---|
69 | fprintf(' WARNING: ''Hardware'' flag ignored by raw2real\n'); |
---|
70 | varargin(i) = []; |
---|
71 | end |
---|
72 | end |
---|
73 | |
---|
74 | |
---|
75 | if isempty(varargin) |
---|
76 | error('Must have at least 1 input for structures and 3 for non-structures.'); |
---|
77 | else |
---|
78 | Family = varargin{1}; |
---|
79 | if length(varargin) >= 2 |
---|
80 | Field = varargin{2}; |
---|
81 | end |
---|
82 | if length(varargin) >= 3 |
---|
83 | DataIn = varargin{3}; |
---|
84 | end |
---|
85 | if length(varargin) >= 4 |
---|
86 | DeviceList = varargin{4}; |
---|
87 | end |
---|
88 | end |
---|
89 | |
---|
90 | |
---|
91 | %%%%%%%%%%%%%%%%%%%% |
---|
92 | % Cell Array Input % |
---|
93 | %%%%%%%%%%%%%%%%%%%% |
---|
94 | if iscell(Family) |
---|
95 | if length(varargin) >= 2 |
---|
96 | if ~iscell(DataIn) |
---|
97 | error('If Family is a cell array, then RawData must be a cell array.'); |
---|
98 | end |
---|
99 | if length(Family) ~= length(DataIn) |
---|
100 | error('RawData and Family must be the same size cell arrays'); |
---|
101 | end |
---|
102 | end |
---|
103 | if length(varargin) >= 3 |
---|
104 | if ~iscell(DeviceList) |
---|
105 | error('If Family is a cell array, then DeviceList must be a cell array.'); |
---|
106 | end |
---|
107 | if length(Family) ~= length(DeviceList) |
---|
108 | error('Family and DeviceList must be the same size cell arrays'); |
---|
109 | end |
---|
110 | end |
---|
111 | for i = 1:length(Family) |
---|
112 | if length(varargin) == 1 |
---|
113 | DataOut{i} = raw2real(Family{i}, InputFlags{:}); |
---|
114 | elseif length(varargin) == 2 |
---|
115 | DataOut{i} = raw2real(Family{i}, Field{i}, InputFlags{:}); |
---|
116 | elseif length(varargin) == 3 |
---|
117 | DataOut{i} = raw2real(Family{i}, Field{i}, DataIn{i}, InputFlags{:}); |
---|
118 | else |
---|
119 | DataOut{i} = raw2real(Family{i}, Field{i}, DataIn{i}, DeviceList{i}, InputFlags{:}); |
---|
120 | end |
---|
121 | end |
---|
122 | return |
---|
123 | end |
---|
124 | % End cell inputs |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | %%%%%%%%%%%%%%%%%%%% |
---|
129 | % Structure Inputs % |
---|
130 | %%%%%%%%%%%%%%%%%%%% |
---|
131 | if isstruct(Family) |
---|
132 | % Convert entire data structure |
---|
133 | DataOut = Family; |
---|
134 | |
---|
135 | for j = 1:size(DataOut,1) |
---|
136 | for k = 1:size(DataOut,2) |
---|
137 | |
---|
138 | if isfield(DataOut(j,k),'Monitor') && isfield(DataOut(j,k),'Actuator') |
---|
139 | % Response matrix structure |
---|
140 | |
---|
141 | if strcmpi(IncrementalFlag, 'Absolute') && j==1 && k==1 |
---|
142 | fprintf(' WARNING: ''Absolute'' flag passed to raw2real for a response matrix. Flag ignored!'); |
---|
143 | end |
---|
144 | |
---|
145 | % Incrementally scale rows by Monitor and Columns by Actuators |
---|
146 | DataOut(j,k).Data = raw2real(DataOut(j,k).Monitor.FamilyName, DataOut(j,k).Monitor.Field, DataOut(j,k).Data, DataOut(j,k).Monitor.DeviceList, 'Incremental'); |
---|
147 | DataOut(j,k).Data = raw2real(DataOut(j,k).Actuator.FamilyName, DataOut(j,k).Actuator.Field, DataOut(j,k).Data', DataOut(j,k).Actuator.DeviceList, 'Incremental')'; |
---|
148 | |
---|
149 | DataOut(j,k).Monitor = raw2real(DataOut(j,k).Monitor, 'Absolute'); |
---|
150 | DataOut(j,k).Actuator = raw2real(DataOut(j,k).Actuator, 'Absolute'); |
---|
151 | if isfield(DataOut(j,k), 'ActuatorDelta') |
---|
152 | DataOut(j,k).ActuatorDelta = raw2real(DataOut(j,k).Actuator.FamilyName, DataOut(j,k).Actuator.Field, DataOut(j,k).ActuatorDelta, [1 1], 'Incremental'); |
---|
153 | end |
---|
154 | |
---|
155 | elseif isfield(DataOut(j,k),'FamilyName') && isfield(DataOut(j,k),'Field') |
---|
156 | % Data structure |
---|
157 | DataOut(j,k).Data = raw2real(DataOut(j,k).FamilyName, DataOut(j,k).Field, DataOut(j,k).Data, DataOut(j,k).DeviceList, IncrementalFlag{:}); |
---|
158 | |
---|
159 | else |
---|
160 | error('Unknown data structure type'); |
---|
161 | end |
---|
162 | |
---|
163 | if isfield(DataOut(j,k), 'CreatedBy') |
---|
164 | DataOut(j,k).CreatedBy = ['raw2real,', DataOut(j,k).CreatedBy]; |
---|
165 | end |
---|
166 | end |
---|
167 | end |
---|
168 | |
---|
169 | % Make numeric if requested |
---|
170 | if ~isempty(NumericFlag) && NumericFlag == 1 |
---|
171 | DataOutNew = []; |
---|
172 | for j = 1:size(DataOut,1) |
---|
173 | OutCol = []; |
---|
174 | for k = 1:size(DataOut,2) |
---|
175 | OutCol = [OutCol DataOut(j,k).Data]; |
---|
176 | end |
---|
177 | DataOutNew = [DataOutNew; OutCol]; |
---|
178 | end |
---|
179 | DataOut = DataOutNew; |
---|
180 | end |
---|
181 | |
---|
182 | return; |
---|
183 | end |
---|
184 | |
---|
185 | |
---|
186 | %%%%%%%%%%%%%%%%% |
---|
187 | % Main Function % |
---|
188 | %%%%%%%%%%%%%%%%% |
---|
189 | |
---|
190 | % Family string input |
---|
191 | FamilyName = Family; |
---|
192 | if length(varargin) < 3 |
---|
193 | error('No RawData input'); |
---|
194 | end |
---|
195 | if length(varargin) < 4 |
---|
196 | DeviceList = []; |
---|
197 | end |
---|
198 | if isempty(IncrementalFlag) |
---|
199 | IncrementalFlag = 'Absolute'; |
---|
200 | end |
---|
201 | |
---|
202 | |
---|
203 | % Defaults |
---|
204 | if isempty(Field) |
---|
205 | if ismember(FamilyName,'BPM') |
---|
206 | Field = 'Monitor'; |
---|
207 | else |
---|
208 | Field = 'Setpoint'; |
---|
209 | end |
---|
210 | end |
---|
211 | if isempty(DeviceList) |
---|
212 | DeviceList = family2dev(FamilyName); |
---|
213 | end |
---|
214 | if (size(DeviceList,2) == 1) |
---|
215 | DeviceList = elem2dev(FamilyName, DeviceList); |
---|
216 | end |
---|
217 | |
---|
218 | |
---|
219 | %%%%%%%%%%%%%%%%%%%% |
---|
220 | % Convert the data % |
---|
221 | %%%%%%%%%%%%%%%%%%%% |
---|
222 | |
---|
223 | Gain = getgain(FamilyName, Field, DeviceList); |
---|
224 | if strcmpi(IncrementalFlag, 'Incremental') |
---|
225 | Offset = 0; |
---|
226 | else |
---|
227 | Offset = getoffset(FamilyName, Field, DeviceList, 'Hardware'); |
---|
228 | end |
---|
229 | |
---|
230 | |
---|
231 | for i = 1:size(DataIn,2) |
---|
232 | DataOut(:,i) = Gain .* (DataIn(:,i) - Offset); |
---|
233 | end |
---|
234 | |
---|
235 | |
---|
236 | %raw2real: DataOut(:,i) = Gain .* (DataIn(:,i) - Offset); |
---|
237 | %real2raw: DataOut(:,i) = (DataIn(:,i) ./ Gain) + Offset; |
---|
238 | |
---|
239 | |
---|
240 | |
---|