1 | function varargout = loadorbit(varargin) |
---|
2 | %LOADORBIT - Loads orbit by file/directory specification - or - Golden %orbit by default |
---|
3 | %[X,Y] = loadorbit(BPMxList,BPMyList,DirSpec,FileName,<'x'/'z'/'b'>,<'struct'>,<'auto'>,) |
---|
4 | % |
---|
5 | % Inputs: |
---|
6 | % 1a. varargin = If varargin contains keyword 'struct' return entire matrix structure |
---|
7 | % 1b. varargin = If varargin contains keyword 'auto' do not use file browser |
---|
8 | % 1c. varargin = If varargin contains keyword 'x' or 'z' only return horizontal or vertical orbit |
---|
9 | % [X] = loadorbit(BPMxList,[], DirSpec, FileName, 'x', 'Struct'); |
---|
10 | % [Z] = loadorbit([], BPMyList,DirSpec, FileName, 'z', 'Struct'); |
---|
11 | % [X,Z] = loadorbit(BPMxList,BPMyList,DirSpec, FileName, 'b', 'Struct','Auto'); |
---|
12 | % Dimension: 1, 'x' - return only horizontal coordinates |
---|
13 | % Dimension: 2, 'z' - return only vertical coordinates |
---|
14 | % Dimension: 3, 'b' - return both planes |
---|
15 | % - defaults to both planes (Dimension 3) |
---|
16 | % Assumes BPM families are 'BPMx' and 'BPMz' |
---|
17 | % BPMlist ([Sector Device #] or [element #]) (default: all BPMs) |
---|
18 | % 2. DirSpec = Directory name for orbit data file |
---|
19 | % 3. FileName = File name for orbit data |
---|
20 | % |
---|
21 | % OUPUTS: |
---|
22 | % 1. Beam position |
---|
23 | |
---|
24 | % |
---|
25 | % Written by J. Corbett 7/12/03 |
---|
26 | % Modified by Laurent S. Nadolski |
---|
27 | |
---|
28 | % TODO clean up, a bit messy |
---|
29 | |
---|
30 | % %remove [] from varargin |
---|
31 | % for k = length(varargin):-1:1 |
---|
32 | % if isempty(varargin{k}) |
---|
33 | % varargin(k) = []; |
---|
34 | % end |
---|
35 | % end |
---|
36 | |
---|
37 | |
---|
38 | %Default input values - assign opsdata directory and golden file |
---|
39 | BPMxFamily = gethbpmfamily; |
---|
40 | BPMyFamily = getvbpmfamily; |
---|
41 | BPMxList = find(getfamilydata(BPMxFamily,'Status')); |
---|
42 | BPMyList = find(getfamilydata(BPMyFamily,'Status')); |
---|
43 | DirSpec = getfamilydata('Directory','BPMData'); %default to BPM data directory |
---|
44 | FileName = []; %no default file |
---|
45 | XPlaneFlag = 0; |
---|
46 | YPlaneFlag = 0; |
---|
47 | XYPlaneFlag= 0; |
---|
48 | StructOutputFlag = 0; %default numeric output (not structure) |
---|
49 | AutoReadFlag = 0; %default pop file read browser |
---|
50 | |
---|
51 | %Check for structure output request |
---|
52 | [varargin, StructOutputFlag] = findkeyword(varargin,'struct'); |
---|
53 | |
---|
54 | %Check for auto read request |
---|
55 | % [varargin, AutoReadFlag] = findkeyword(varargin,'auto'); |
---|
56 | |
---|
57 | %Check for plane request |
---|
58 | [varargin, XPlaneFlag] = findkeyword(varargin,'x'); |
---|
59 | [varargin, YPlaneFlag] = findkeyword(varargin,'z'); |
---|
60 | [varargin, XYPlaneFlag]= findkeyword(varargin,'b'); |
---|
61 | |
---|
62 | %Evaluate the dimension (default 3=both) |
---|
63 | Dim = 0; %unspecified |
---|
64 | if XPlaneFlag |
---|
65 | Dim = 1; |
---|
66 | elseif YPlaneFlag |
---|
67 | Dim = 2; |
---|
68 | elseif XYPlaneFlag |
---|
69 | Dim = 3; |
---|
70 | end |
---|
71 | |
---|
72 | %assign values specified by varargin |
---|
73 | if nargin>=1 |
---|
74 | if size(varargin,2) >= 1 |
---|
75 | BPMxList=varargin{1}; |
---|
76 | end |
---|
77 | if size(varargin,2) >= 2 |
---|
78 | BPMyList=varargin{2}; |
---|
79 | end |
---|
80 | if size(varargin,2) >= 3 DirSpec=varargin{3}; end |
---|
81 | if size(varargin,2) == 4 FileName=[varargin{4} '.mat']; end |
---|
82 | end |
---|
83 | |
---|
84 | X=[]; |
---|
85 | Z=[]; |
---|
86 | |
---|
87 | if AutoReadFlag == 0 %browse for file |
---|
88 | [FileName, DirSpec,FilterIndex]=uigetfile('*.mat','Select Orbit Archive File',[DirSpec FileName]); |
---|
89 | if FilterIndex==0 |
---|
90 | disp(' Warning: No orbit loaded'); |
---|
91 | if Dim==1 |
---|
92 | varargout{1}=[]; |
---|
93 | elseif Dim==2 |
---|
94 | varargout{1}=[]; |
---|
95 | elseif Dim==0 | Dim==3 |
---|
96 | varargout{1}=[]; |
---|
97 | varargout{2}=[]; |
---|
98 | end |
---|
99 | return |
---|
100 | end |
---|
101 | end |
---|
102 | |
---|
103 | FileSpec=[DirSpec FileName]; |
---|
104 | if exist(FileSpec,'file')==0 | exist(FileSpec,'file')==7 |
---|
105 | error(['File not found in loadorbit: ' FileSpec]); |
---|
106 | disp(' Warning: No orbit loaded'); |
---|
107 | if Dim==1 |
---|
108 | varargout{1}=[]; |
---|
109 | elseif Dim==2 |
---|
110 | varargout{1}=[]; |
---|
111 | elseif Dim==0 | Dim==3 |
---|
112 | varargout{1}=[]; |
---|
113 | varargout{2}=[]; |
---|
114 | end |
---|
115 | return |
---|
116 | end |
---|
117 | |
---|
118 | orbitdata = load(FileSpec); %load orbit archive file - always a structure in archive |
---|
119 | |
---|
120 | structnames = fieldnames(orbitdata); |
---|
121 | |
---|
122 | %Note that structure names can be any string (not just X, Z) |
---|
123 | for ii=1:size(structnames,1) |
---|
124 | DataStruct=orbitdata.(structnames{ii}); |
---|
125 | if iscell(DataStruct) DataStruct=DataStruct{1}; end |
---|
126 | if strcmp(DataStruct.FamilyName,BPMxFamily ) %DataStruct.FamilyName==BPMxFamily |
---|
127 | X = DataStruct; |
---|
128 | elseif strcmp(DataStruct.FamilyName,BPMyFamily ) %DataStruct.FamilyName==BPMyFamily |
---|
129 | Z = DataStruct; |
---|
130 | end |
---|
131 | end |
---|
132 | |
---|
133 | %check requested data exists |
---|
134 | if (Dim==1 | Dim==3) & isempty(X) error(['horizontal data not available in orbit file: ' FileSpec]); end |
---|
135 | if (Dim==2 | Dim==3) & isempty(Z) error([ 'vertical data not available in orbit file: ' FileSpec]); end |
---|
136 | |
---|
137 | if ~isempty(X.DeviceList) BPMxList=dev2elem(BPMxFamily,X.DeviceList); end |
---|
138 | if ~isempty(Z.DeviceList) BPMyList=dev2elem(BPMyFamily,Z.DeviceList); end |
---|
139 | [ierror]=checkdevicelist(X,BPMxList,Z,BPMyList,Dim); |
---|
140 | if ierror==1 return; end |
---|
141 | |
---|
142 | if Dim==0 %no specific plane request |
---|
143 | if size(structnames,1)==1 && ~isempty(X) Dim=1; end %put data into X=varargout{1} |
---|
144 | if size(structnames,1)==1 && ~isempty(Z) Dim=2; end %put data into Z=varargout{2} |
---|
145 | if size(structnames,1)==2 Dim=3; end %put data into X=varargout{1}, Z=varargout{1} |
---|
146 | end |
---|
147 | |
---|
148 | %Reduce orbit if necessary |
---|
149 | if Dim == 1 | Dim == 3 |
---|
150 | X.Data = X.Data; |
---|
151 | X.DeviceList=elem2dev(BPMxFamily,BPMxList); |
---|
152 | end |
---|
153 | if Dim == 2 | Dim == 3 |
---|
154 | Z.Data = Z.Data; |
---|
155 | Z.DeviceList=elem2dev(BPMyFamily,BPMyList); |
---|
156 | end |
---|
157 | |
---|
158 | X.FileName = FileSpec; |
---|
159 | Z.FileName = FileSpec; |
---|
160 | |
---|
161 | if StructOutputFlag == 0 %caller wants numeric output |
---|
162 | X = X.Data; |
---|
163 | Z = Z.Data; |
---|
164 | end |
---|
165 | |
---|
166 | %Output requested planes |
---|
167 | if Dim==1 |
---|
168 | varargout{1}=X; |
---|
169 | if nargout==2 varargout{2}=[]; end %Dim=0 (unspecified) but caller expects two outputs |
---|
170 | elseif Dim==2 |
---|
171 | if nargout==2 %Dim=0 (unspecified) but caller expects two outputs |
---|
172 | varargout{1}=[]; |
---|
173 | varargout{2}=Z; |
---|
174 | else |
---|
175 | varargout{1}=Z; |
---|
176 | end |
---|
177 | elseif Dim==3 |
---|
178 | varargout{1}=X; |
---|
179 | varargout{2}=Z; |
---|
180 | end |
---|
181 | |
---|
182 | |
---|
183 | %========================================================== |
---|
184 | function ierror=checkdevicelist(X,BPMxList,Z,BPMyList,Dim); |
---|
185 | %========================================================== |
---|
186 | ierror=0; |
---|
187 | %check for BPMs requested but not supplied |
---|
188 | if Dim==1 |
---|
189 | [ElemListNotFound, iNotFound]=setdiff(BPMxList, dev2elem(BPMxFamily,X.DeviceList)); |
---|
190 | if iNotFound>0 |
---|
191 | ierror=1; |
---|
192 | for ii=1:iNotFound |
---|
193 | disp(['Warning: BPM value not in Archive Orbit: ' getfamilydata(BPMxFamily,'CommonNames',ElemListNotFound(ii))]); |
---|
194 | end |
---|
195 | return |
---|
196 | end |
---|
197 | |
---|
198 | elseif Dim==2 |
---|
199 | [ElemListNotFound, iNotFound]=setdiff(BPMyList, dev2elem(BPMyFamily,Z.DeviceList)); |
---|
200 | if iNotFound>0 |
---|
201 | ierror=1; |
---|
202 | for ii=1:iNotFound |
---|
203 | disp(['Warning: BPM value not in Archive Orbit: ' getfamilydata(BPMyFamily,'CommonNames',ElemListNotFound(ii))]); |
---|
204 | end |
---|
205 | return |
---|
206 | end |
---|
207 | |
---|
208 | elseif Dim==3 |
---|
209 | [ElemListNotFound, iNotFound]=setdiff(BPMxList, dev2elem(BPMxFamily,X.DeviceList)); |
---|
210 | if iNotFound>0 |
---|
211 | ierror=1; |
---|
212 | for ii=1:iNotFound |
---|
213 | disp(['Warning: BPM value not in Archive Orbit: ' getfamilydata(BPMyFamily,'CommonNames',ElemListNotFound(ii))]); |
---|
214 | end |
---|
215 | return |
---|
216 | end |
---|
217 | |
---|
218 | [ElemListNotFound, iNotFound]=setdiff(BPMyList, dev2elem(BPMyFamily,Z.DeviceList)); |
---|
219 | if iNotFound>0 |
---|
220 | ierror=1; |
---|
221 | for ii=1:iNotFound |
---|
222 | disp(['Warning: BPM value not in Archive Orbit: ' getfamilydata(BPMyFamily,'CommonNames',ElemListNotFound(ii))]); |
---|
223 | end |
---|
224 | return |
---|
225 | end |
---|
226 | |
---|
227 | end |
---|