source: MML/trunk/applications/orbit/lib/readwrite.m

Last change on this file was 4, checked in by zhangj, 11 years ago

Initial import--MML version from SOLEIL@2013

File size: 11.3 KB
Line 
1%=============================================================
2function varargout = readwrite(action, varargin)
3%=============================================================
4% readwrite supplies a switchyard of routines
5% to read and write data files in SPEAR formats
6%WriteReference
7%ReadDispersion
8%ReadBResponse
9
10orbfig = findobj(0,'tag','orbfig');  %orbfig "global"
11global BPM BL COR RSP SYS
12% SYS=getappdata(0,'SYS');
13% BPM=getappdata(0,'BPM');
14% COR=getappdata(0,'COR');
15% RSP=getappdata(0,'RSP');
16% BL =getappdata(0,'BL' );
17switch action
18
19    %=============================================================
20    case 'LogFdbkData'                       % *** LogFdbkData ***
21        %=============================================================
22        if strcmp(SYS.machine,'SPEAR2')
23            [SYS.beam, SYS.engy,SYS.curr,SYS.lt,SYS.ahr]=getset('GetSPEAR2Params',SYS.mode);
24            setappdata(0,'SYS',SYS);
25        elseif strcmp(SYS.machine,'SPEAR3')
26        end
27        BPMLog(SYS,BPM);
28        CORLog(SYS,COR);
29
30        %=============================================================
31    case 'ReadBPMReference'             % *** ReadBPMReference ***
32        %=============================================================
33        %read archive orbit as reference
34        refplane=varargin{1};
35        [varargin,GoldenFlag]=findkeyword(varargin,'Golden');
36
37        families={gethbpmfamily; getvbpmfamily};
38        if GoldenFlag   %retrieve data from PhysData Structure
39
40            X = getgolden(families{1},'struct');
41            Y = getgolden(families{2},'struct');
42
43        else
44
45            [X,Y] = loadorbit([],[],'struct');     %select file from browser
46
47        end
48
49        if isempty(X) && isempty(Y)
50            disp('   Warning: No orbit loaded')
51            return
52        end
53
54        x{1}=X;
55        x{2}=Y;
56
57        %process for orbit program
58        for k=1:2
59            struct=x{k};
60            family=families{k};
61            status=getfamilydata(family,'Status');
62            BPM(k).ref=zeros(length(status),1);
63            BPM(k).des=zeros(length(status),1);
64            BPM(k).abs=zeros(length(status),1);
65
66            [BPM(k).iref,istat,idev]=intersect(find(status),(1:length(status))');   %good status and in orbit structure
67
68  %          if strcmpi(SYS.datamode,'REAL')  %convert to REAL coordinates
69  %              struct.Data(idev)=BPM(k).gain(BPM(k).iref).*(struct.Data(idev)-BPM(k).offset(BPM(k).iref));
70  %          end
71
72%             BPM(k).ref(BPM(k).iref)=struct.Data(idev);
73%             BPM(k).des(BPM(k).iref)=struct.Data(idev);
74%             BPM(k).abs(BPM(k).iref)=struct.Data(idev);
75
76            if SYS.relative==1   %display mode
77                ntbpm=length(BPM(k).name(:,1));
78                BPM(k).abs=zeros(1,ntbpm)';
79            end
80
81            [BPM]=sortbpms(BPM,RSP);
82
83        end
84
85        setappdata(0,'BPM',BPM);
86
87        orbgui('LBox','Reference orbit loaded');
88        orbgui('RefreshOrbGUI');
89
90        %===============================================================
91    case 'ArchiveBPMOrbit'                 % *** ArchiveBPMOrbit ***
92        %===============================================================
93        %archive present orbit as reference orbitref
94        %'X','Y','B' options
95        %'Golden' option
96
97        refplane=varargin{1};
98        [varargin,GoldenFlag]=findkeyword(varargin,'Golden');
99
100        BPMxData = getx('struct');         %raw data for archive
101        BPMxData.CreatedBy='OrbitControl';
102        BPMxData.iFit=BPM(1).ifit;         %BPMs on for fitting (contstraint)
103
104        BPMyData = gety('struct');         %raw data for archive
105        BPMyData.CreatedBy='OrbitControl';
106        BPMyData.iFit=BPM(2).ifit;
107
108        if GoldenFlag
109            tmp = questdlg('Save Golden Reference Orbit (solid blue line)?','Golden Orbit','YES','NO','YES');
110
111            if strcmpi(tmp,'NO')
112                orbgui('LBox', ' Golden orbit save aborted');
113                return
114            end
115
116            needs work!!!!
117
118            physdata.BPMx.Golden=BPMxData;
119            physdata.BPMy.Golden=BPMyData;
120            save GoldenPhysData physdata
121            orbgui('LBox', ' Golden orbit saved');
122            %bpmgui('UpdateRef');%establish new reference orbit, refreshorbgui
123
124        else
125
126            DirStart = pwd;
127            DirectoryName = getfamilydata('Directory','BPMData');
128            [DirectoryName, ErrorFlag] = gotodirectory(DirectoryName);
129            FileName = appendtimestamp(getfamilydata('Default', 'BPMArchiveFile'), clock);
130            save(FileName, 'BPMxData', 'BPMyData');
131            cd(DirStart);
132            orbgui('LBox',[' Archive file: ' FileName]);
133
134        end
135
136        %=============================================================
137    case 'ReadDispersion'                % *** ReadDispersion ***
138        %=============================================================
139        %read dispersion orbit
140        refplane=varargin{1};
141        [varargin,GoldenFlag]=findkeyword(varargin,'Golden');
142
143        families={gethbpmfamily; getvbpmfamily};
144
145        if GoldenFlag   %retrieve data from PhysData Structure
146
147            X = getdisp(families{1},'struct');
148            Y = getdisp(families{2},'struct');
149
150        else
151
152            [X,Y] = loadorbit([],[],'struct');     %select file from browser
153
154        end
155
156        x{1}=X;
157        x{2}=Y;
158
159        %process for orbit program
160        for k=1:2
161            struct=x{k};
162            family=families{k};
163            status=getfamilydata(family,'Status');
164
165            % RSP(1).eta=rsp(1).eta;
166            % RSP(1).drf=rsp(1).drf;
167
168
169            BPM(k).ref=zeros(length(status),1);
170            BPM(k).des=zeros(length(status),1);
171            BPM(k).abs=zeros(length(status),1);
172
173            [BPM(k).iref,istat,idev]=intersect(find(status),(1:length(status))');   %good status and in orbit structure
174
175 %           if strcmpi(SYS.datamode,'REAL')  %convert to REAL coordinates
176 %               struct.Data(idev)=BPM(k).gain(BPM(k).iref).*(struct.Data(idev)-BPM(k).offset(BPM(k).iref));
177 %           end
178
179        end
180
181        setappdata(0,'BPM',BPM);
182
183        orbgui('LBox','Dispersion orbit loaded');
184        orbgui('RefreshOrbGUI');
185
186        %=============================================================
187    case 'DialogBox'               % *** DialogBox ***
188        %=============================================================
189        figtitle=varargin(1);    %cell array
190        figtitle=char(figtitle);
191        ftype=varargin(2);              %type of file, eg ReadReference: must be case in readwrite
192        ftype=char(ftype);
193
194        %read file options
195        extype='*.dat';
196        if strcmp(ftype,'ReadDispersion')
197
198            [FileName,DirSpec]=uigetfile(extype,[ftype, ' - please supply .m extension']);
199
200            ts = datestr(now,0);
201            if isequal(FileName,0)|isequal(DirSpec,0)
202                disp('File ',[DirSpec FileName],' not found');
203                orbgui('LBox',[ ts ': File ',[DirSpec FileName],' not found']);
204                return
205            else
206                disp(['File ', [DirSpec FileName], ' found']);
207                orbgui('LBox',['File ', [DirSpec FileName], ' found']);
208                readwrite('ProcessDialog',ftype,DirSpec,FileName);
209                return
210            end
211        end
212
213        %write file options
214        extype='*.dat';
215        if strcmp(ftype,'WriteResponse')
216
217            [FileName,DirSpec]=uiputfile(extype,ftype);
218            if isempty(findstr(FileName,'.m'))
219                FileName = [FileName,'.m'];
220            end
221
222            ts = datestr(now,0);
223            if isequal(FileName,0)|isequal(DirSpec,0)
224                disp('File not open');
225                orbgui('LBox',[ ts ': File not opened...']);
226                return
227            else
228                disp(['File ', [DirSpec FileName], ' open']);
229                orbgui('LBox',['File ', [DirSpec FileName], ' open']);
230                readwrite('ProcessDialog',ftype,DirSpec,FileName);
231                return
232            end
233        end
234
235        %==================
236    case 'ReadResponse'
237        %==================
238
239        DirectoryName = getfamilydata('Directory', 'BPMResponse');
240        [FileName, DirectoryName] = uigetfile('*.mat', 'Select a configuration file to load', DirectoryName);
241        FileName = [DirectoryName FileName];
242
243        t=load(FileName);
244
245        temp=t.Rmat;
246
247        %temp=getbpmresp('BPMx',[],'BPMy',[],'HCM',[],'VCM',[],FileName,'struct');
248
249        if isempty(temp)
250            return
251        end
252
253        for k=1:2
254            RSP(k)=response2rsp(temp(k,k),RSP(k),k);
255        end
256
257        if strcmpi(SYS.datamode,'REAL')   %convert to REAL units
258            for k=1:2
259                ib=RSP(k).ib;
260                ic=RSP(k).ic;
261                RSP(k).c(ib,ic)=diag(BPM(k).gain(ib))*RSP(k).c(ib,ic);
262            end
263        end
264
265        BPM=SortBPMs(BPM,RSP);       %sort for avail, ifit
266        COR=SortCORs(COR,RSP);       %sort for avail, ifit
267
268        setappdata(0,'BPM',BPM);
269        setappdata(0,'COR',COR);
270        setappdata(0,'RSP',RSP);
271
272        orbgui('RefreshOrbGUI');
273
274        %==============================================================
275    case 'ProcessDialog'                    % *** ProcessDialog ***
276        %==============================================================
277        ftype=varargin(1);       ftype=char(ftype);
278        DirSpec=varargin(2);     DirSpec=char(DirSpec);
279        FileName=varargin(3);    FileName=char(FileName);
280
281
282        switch ftype
283
284            %===================
285            case 'ReadDispersion'
286                %===================
287                [fid,message]=fopen(FileName,'r');
288                if fid==-1
289                    disp(['WARNING: Dispersion orbit file not found:' [DirSpec FileName]]);
290                    disp(message);
291                    return
292                end
293                timestamp=fgetl(fid)
294                fclose(fid);
295
296                if     SYS.machine=='SPEAR2'
297                    rsp=ReadSPEAR2Dispersion(SYS.etafil,'auto');
298                elseif SYS.machine=='SPEAR3'
299                    rsp=ReadSPEAR3Dispersion(SYS.etafil,'auto');
300                end
301                RSP(1).eta=rsp(1).eta;
302                RSP(1).drf=rsp(1).drf;
303                RSP(2).eta=rsp(2).eta;
304                RSP(2).drf=rsp(2).drf;
305                setappdata(0,'RSP',RSP);
306
307                if strcmpi(SYS.datamode,'REAL')
308                    for k=1:2
309                        status=BPM(k).status;
310                        RSP(k).eta(status)=BPM(k).gain(status).*RSP(k).eta(status);   %scale by gain
311                    end
312                end
313
314
315                clear rsp;
316
317            otherwise
318                disp(['Warning: no CASE found in SPEAR 3 readwrite/Dialog Box: ' action]);
319        end   %end of ProcessDialog switch yard
320        %pause(3);    delete(gcf);
321
322        %=============================================================
323    case 'OpenHelp'                            % *** OpenHelp ***
324        %==============================================================
325        FileName = 'HelpD.m';
326        fid = fopen(FileName, 'r');
327
328        if fid < 0
329            disp(['**Warning: Help file "', FileName, '" cannot be found.']);
330            return
331        end
332
333        while feof(fid) == 0
334            t = fgetl(fid);
335            disp(t);
336        end
337
338        fclose(fid);
339
340        %==============================================================
341    otherwise
342        disp(['Warning: no CASE found in readwrite: ' action]);
343end  %end switchyard
Note: See TracBrowser for help on using the repository browser.