source: MML/trunk/machine/SOLEIL/StorageRing/orbit/readwrite.m @ 17

Last change on this file since 17 was 17, checked in by zhangj, 10 years ago

To have a stable version on the server.

  • Property svn:executable set to *
File size: 21.6 KB
Line 
1function readwrite(action, varargin)
2%READWRITE - Multipurpose read/write function for orbit correction applicaiton
3% function varargout = readwrite(action, varargin)
4% readwrite supplies a switchyard of routines
5% to read and write data files in solorbit formats
6%
7% ReadCorrectors
8% WriteCorrectors
9% ReadBPMReference
10% ArchiveBPMOrbit
11% DialogBox
12% ReadResponse
13% ProcessDialog
14%
15
16
17%
18% Written by William J. Corbett
19% Modified by Laurent S. Nadolski
20
21%% TODO NEED adaption for SOLEIL
22
23% orbfig = findobj(0,'tag','orbfig');  %orbfig "global"
24global BPM COR RSP SYS
25
26[BPMxFamily, BPMzFamily] = BPM.AOFamily;
27
28switch action
29
30%     %=============================================================
31%     case 'LogFdbkData'                       % *** LogFdbkData ***
32%         %=============================================================
33%         if strcmp(SYS.machine,'SPEAR2')
34%             [SYS.energy,SYS.curr,SYS.lt,SYS.ahr] = getset('GetRingParams',SYS.mode);
35%             setappdata(0,'SYS',SYS);
36%         elseif strcmp(SYS.machine,'SOLEIL')
37%            % TODO
38%            disp('LogFdbckData TODO')
39%         end
40%
41%         BPMLog(SYS,BPM);
42%         CORLog(SYS,COR);
43
44        %=============================================================
45    case 'ReadCorrectors'               % *** ReadCorrectors ***
46        %=============================================================
47        % read corrector strength files produced by this program
48        FileName = char(varargin(1));
49        auto     = char(varargin(2));    %cell array
50       
51        [fid,message] = fopen(FileName,'r');
52        if fid == -1
53            disp(['WARNING: Corrector file not found:' FileName]);
54            disp(message);
55            return
56        end
57
58        header    = fgetl(fid);
59        disp(header);
60        timestamp = fgetl(fid);
61        disp(timestamp);
62        comment   = fgetl(fid);
63        disp(comment);
64
65        if ~strcmp(auto,'auto') == 1
66            answer = input('Read Corrector File? Y/N [Y]: ','s');
67            if isempty(answer), answer = 'n'; end
68            if answer=='n' || answer=='N'
69                disp('WARNING: Corrector File NOT LOADED');
70                fclose(fid);
71                return
72            end
73        end
74
75        disp(['Reading corrector file: ' FileName]);
76        COR(1).ntcor = fscanf(fid,'%d\n',1);
77        COR(2).ntcor = fscanf(fid,'%d\n',1);
78        disp([COR(1).ntcor,COR(2).ntcor]);
79        setappdata(0,'COR',COR);
80
81        fscanf(fid,'%d %f %*s',[3,54]);  %ntbpm rows, 5 columns wide
82        fclose(fid);
83
84        %=============================================================
85    case 'WriteCorrectors'               % *** WriteCorrectors ***
86        %=============================================================
87        % write reference orbit file in format of orbit program       
88        FileName = char(varargin(1));
89        comment  = char(varargin(2));
90
91        [fid,message] = fopen(FileName,'w');
92        if fid == -1
93            disp(['WARNING: Unable to open file to write correctors :' FileName]);
94            disp(message);
95            return
96        end
97        disp(['Writing corrector file: ' FileName]);
98        fprintf(fid,'%s\n','Correctors');
99        fprintf(fid,'%s\n',['timestamp: ' datestr(now,0)]);
100        fprintf(fid,'%s\n', comment);
101        fprintf(fid,'%d %d\n', COR(1).ntcor, COR(2).ntcor);
102     
103        for ii = 1:COR(2).ntcor,
104            fprintf(fid,'%3d %6.3f %s\n',...
105                ii, COR(1).act(ii),COR(1).name(ii,:)); %3 columns wide
106        end
107
108        for ii = 1:COR(2).ntcor,
109            fprintf(fid,'%3d %6.3f %s\n',...
110                ii, COR(2).act(ii),COR(2).name(ii,:)); %3 columns wide
111        end
112        fclose(fid);
113
114%         %=============================================================
115%     case 'WriteResponse'                     % *** WriteResponse ***
116%         %=============================================================
117%         %read in terms of RSP, write in terms of BPM, COR
118%         %readwrite('WriteSPEAR3Response','dummy','auto');
119%         FileName = char(varargin(1));   
120%         comment  = char(varargin(2));
121% %         WriteSPEAR2Response(FileName,comment,BPM,BL,COR,RSP);
122%         disp('WriteResponse TODO')
123
124%         %=============================================================
125%     case 'ReadBPMxReference'         % *** ReadBPMxReference ***
126%         %=============================================================
127%         %read archive orbit as reference
128%
129%         [X,Z] = loadorbit([],[],'struct');     %select file from browser
130%
131%         %process for orbit program
132%         if isempty(X)
133%             orbgui('LBox','X-Reference orbit not available');
134%         else
135%             BPM(1).iref = dev2elem(BPMxFamily,X.DeviceList);
136%             BPM(1).ref  = X.Data;
137%             BPM(1).des  = X.Data;
138%             BPM(1).abs  = X.Data;
139%             orbgui('LBox','X-Reference orbit loaded');
140%         end
141%
142%         if SYS.relative == 1   %absolute mode
143%             ntbpm      = length(BPM(1).name(:,1));
144%             BPM(1).abs = zeros(1,ntbpm)';
145%         end
146%
147%         [BPM] = SortBPMs(BPM,RSP);
148%
149%         setappdata(0,'BPM',BPM);
150%
151%         bpmgui('RePlot');        %reference, desired, absolute
152%         orbgui('RefreshOrbGUI');
153%
154        %=============================================================
155    case 'ReadBPMReference'             % *** ReadBPMReference ***
156        %=============================================================
157        %read archive orbit as reference
158        refplane = varargin{1};
159        [varargin, GoldenFlag] = findkeyword(varargin,'Golden');
160
161        if GoldenFlag
162            %retrieve data from PhysData Structure
163%             physdatafile = getfamilydata('OpsData', 'PhysDataFile');
164%             datastruct   = load(physdatafile);
165%             X = datastruct.PhysData.BPMx.Golden;
166%             Z = datastruct.PhysData.BPMz.Golden;
167            %datastruct = getphysdata({BPMxFamily,BPMzFamily});
168            %X          = datastruct{1}.Golden;
169            %Z          = datastruct{2}.Golden;           
170             X = getgolden(BPMxFamily,'Struct');
171             Z = getgolden(BPMzFamily, 'Struct');
172            %   DirSpec  = getfamilydata('Directory','OpsData');
173            %   FileName = getfamilydata('OpsData', 'BPMGoldenFile');
174            %   [X,Z] = loadorbit([],[],DirSpec,FileName,'b','Struct','Auto');  %both planes, structure, automatic read
175        else
176            [X,Z] = loadorbit([],[],'struct');     %select file from browser
177        end
178
179        %process for orbit program
180        switch upper(refplane)
181            case 'X' % Horizontal plane
182                if isempty(X)
183                    orbgui('LBox','X-Reference orbit not available');
184                    return
185                else
186                    BPM(1).iref=dev2elem(BPMxFamily,X.DeviceList);
187                    %decompress data
188                    temp        = getfamilydata(BPMxFamily,'Status');
189                    status      = find(temp);
190                    temp(status)= X.Data;
191                    BPM(1).ref  = temp;
192                    BPM(1).des  = temp;
193                    BPM(1).abs  = temp;
194                    orbgui('LBox','X-Reference orbit loaded');
195                    if isfield(X,'FileName'), orbgui('LBox',X.FileName);
196                    else
197                        orbgui('LBox','Horizontal reference orbit loaded');
198                    end
199                end
200
201            case 'Z' % Vertical plane
202                if isempty(Z)
203                    orbgui('LBox','Z-Reference orbit not available');
204                    return
205                else
206                    BPM(2).iref = dev2elem(BPMzFamily,Z.DeviceList);
207                    %decompress data
208                    temp        = getfamilydata(BPMzFamily,'Status');
209                    status      = find(temp);
210                    temp(status)= Z.Data;
211                    BPM(2).ref  = temp;
212                    BPM(2).des  = temp;
213                    BPM(2).abs  = temp;
214                    orbgui('LBox','Z-Reference orbit loaded');
215                    if isfield(Z,'FileName'), orbgui('LBox',Z.FileName);
216                    else
217                        orbgui('LBox','Vertical reference orbit loaded');
218                    end
219                end
220
221            case 'XZ' % both planes
222                if isempty(X)
223                    orbgui('LBox','X-Reference orbit not available');
224                else
225                    BPM(1).iref = dev2elem(BPMxFamily,X.DeviceList);
226                    %decompress data
227                    temp        = getfamilydata(BPMxFamily,'Status');
228                    status      = find(temp);
229                    temp(status)= X.Data;
230                    BPM(1).ref  = temp;
231                    BPM(1).des  = temp;
232                    BPM(1).abs  = temp;
233                    orbgui('LBox','X-Reference orbit loaded');
234                    if isfield(X,'FileName'), orbgui('LBox',X.FileName);
235                    else
236                        orbgui('LBox','Horizontal reference orbit loaded');
237                    end
238                end
239                if isempty(Z)
240                    orbgui('LBox','Z-Reference orbit not available');
241                else
242                    BPM(2).iref = dev2elem(BPMzFamily,Z.DeviceList);
243                    %decompress data
244                    temp        = getfamilydata(BPMzFamily,'Status');
245                    status      = find(temp);
246                    temp(status)= Z.Data;
247                    BPM(2).ref  = temp;
248                    BPM(2).des  = temp;
249                    BPM(2).abs  = temp;
250                    orbgui('LBox','Z-Reference orbit loaded');
251                    if isfield(Z,'FileName'), orbgui('LBox',Z.FileName);
252                    else
253                        orbgui('LBox','Vertical reference orbit loaded');
254                    end
255                end
256
257        end %end case
258
259        if SYS.relative==1   %absolute mode
260            ntbpm      = length(BPM(1).name(:,1));
261            BPM(1).abs = zeros(1,ntbpm)';
262            ntbpm      = length(BPM(2).name(:,1));
263            BPM(2).abs = zeros(1,ntbpm)';
264        end
265
266        [BPM] = SortBPMs(BPM,RSP);
267
268        setappdata(0,'BPM',BPM);
269
270        bpmgui('RePlot');        %reference, desired, absolute
271        orbgui('RefreshOrbGUI');
272
273        %===============================================================
274    case 'ArchiveBPMOrbit'                 % *** ArchiveBPMOrbit ***
275        %===============================================================
276        %archive present orbit as reference orbitref
277        %'X','Z','XZ' options
278        %'Golden' option
279
280        refplane              = varargin{1};
281        [varargin,GoldenFlag] = findkeyword(varargin,'Golden');
282
283        if GoldenFlag
284%             DirectoryName  = getfamilydata('Directory','OpsData');
285%             FileName = getfamilydata('OpsData', 'BPMGoldenFile');
286            tmp = questdlg('Save Golden Reference Orbit (solid blue line)?','Golden Orbit','YES','NO','YES');
287            if strcmp(tmp,'NO')
288                orbgui('LBox', ' Golden orbit save aborted');
289                return
290            end
291        else %search for BPM directory
292            DirStart      = pwd;
293            DirectoryName = getfamilydata('Directory','BPMData');
294            [DirectoryName, ErrorFlag] = gotodirectory(DirectoryName);
295        end
296
297        switch refplane % Select plane to save
298            case 'X' % Horizontal plane
299                BPMxData           = getx('struct');
300                % Modified creator field
301                BPMxData.CreatedBy ='solorbit-readwrite';
302%                 BPMxData.iFit      = BPM(1).ifit;         %on for fitting (contstraint)
303                if GoldenFlag % Golden orbit
304%                     physdatafile   = getfamilydata('OpsData', 'PhysDataFile');
305%                     datastruct     = load(physdatafile); % read old one to get the right format
306%                     PhysData             = getphysdata;
307%                     PhysData.BPMx.Golden = BPMxData;                   
308%                     save GoldenPhysData PhysData
309                    setphysdata(BPMxFamily,'Golden',BPMxData);
310                else % not a golden orbit
311                    FileName = appendtimestamp([getfamilydata('Default', 'BPMArchiveFile'),'x'], clock);
312                    save(FileName, 'BPMxData');
313                end
314                orbgui('LBox', ' BPMx-Reference orbit archived');
315
316            case 'Z' % Vertical plane
317                BPMzData           = getz('struct');
318                BPMzData.CreatedBy = 'solorbit-readwrite';
319%                 BPMzData.iFit      = BPM(2).ifit;         %on for fitting (contstraint)
320                if GoldenFlag
321%                     physdatafile=getfamilydata('OpsData', 'PhysDataFile');
322%                     datastruct=load(physdatafile);
323%                     PhysData             = getphysdata;
324%                     PhysData.BPMz.Golden = BPMzData;                   
325                    setphysdata(BPMzFamily,'Golden',BPMzData);
326%                     datastruct.PhysData.BPMz.Golden=BPMzData;
327%                     PhysData=datastruct.PhysData;
328%                     save GoldenPhysData PhysData
329                else % Not a golden orbit
330                    FileName = appendtimestamp([getfamilydata('Default', 'BPMArchiveFile'),'z'], clock);
331                    save(FileName, 'BPMzData');
332                end
333                orbgui('LBox', ' BPMz-Reference orbit archived');
334
335            case 'XZ' % Both planes
336                BPMxData           = getx('struct');
337                BPMxData.CreatedBy = 'solorbit-readwrite';
338%                 BPMxData.iFit      = BPM(1).ifit;         %on for fitting (contstraint)
339                BPMzData           = getz('struct');
340                BPMzData.CreatedBy = 'solorbit-readwrite';
341%                 BPMzData.iFit      = BPM(2).ifit;
342                if GoldenFlag
343                    setphysdata({BPMxFamily, BPMzFamily},{'Golden', 'Golden'}, {BPMxData, BPMzData});
344%                     physdatafile=getfamilydata('OpsData', 'PhysDataFile');
345%                     datastruct=load(physdatafile);
346%                     datastruct.PhysData.BPMx.Golden=BPMxData;
347%                     datastruct.PhysData.BPMz.Golden=BPMzData;
348%                     PhysData=datastruct.PhysData;
349%                     PhysData             = getphysdata;
350%                     PhysData.BPMx.Golden = BPMxData;                   
351%                     PhysData.BPMx.Golden = BPMzData;                   
352%                     save GoldenPhysData PhysData
353                else % not a golden orbit
354                    FileName = appendtimestamp(getfamilydata('Default', 'BPMArchiveFile'), clock);
355                    save(FileName, 'BPMxData', 'BPMzData');
356                end
357                orbgui('LBox', ' BPMx/z-Reference orbit archived');
358        end
359
360
361        if GoldenFlag, bpmgui('UpdateRef');  %establish new reference orbit, refreshorbgui
362        else
363            cd(DirStart);
364            orbgui('LBox',[' Archive file: ' FileName]);
365        end
366       
367        %=============================================================
368    case 'DialogBox'               % *** DialogBox ***
369        %=============================================================       
370%         figtitle = char(varargin(1));
371        ftype    = char(varargin(2));    %type of file, eg ReadReference: must be case in readwrite
372       
373        %read file options
374       
375        cd(SYS.localdata);
376
377        extype = '*.dat';
378        if strcmp(ftype,'ReadDispersion')  ||...
379           strcmp(ftype,'ReadCorrectors')  ||...
380           strcmp(ftype,'RestoreSystem')
381       
382            if strcmp(ftype,'RestoreSystem')
383                extype = '*.m';
384            elseif strcmp(ftype,'ReadDispersion')
385                extype = '*.mat';
386                cd(SYS.etafile);               
387            end
388
389            [FileName,DirSpec] = uigetfile(extype,[ftype, ' - please supply .m extension']);
390
391            ts = datestr(now,0);
392           
393            if isequal(FileName,0) || isequal(DirSpec,0)
394                disp(['File ',[DirSpec FileName],' not found']);
395                orbgui('LBox',[ ts ': File ',[DirSpec FileName],' not found']);
396                return
397            else
398                disp(['File ', [DirSpec FileName], ' found']);
399                orbgui('LBox',['File ', [DirSpec FileName], ' found']);
400                readwrite('ProcessDialog',ftype,DirSpec,FileName);
401                return
402            end
403        end
404
405        %write file options
406        extype = '*.dat';
407        if strcmp(ftype,'WriteCorrectors')  ||...
408                strcmp(ftype,'WriteResponse')    ||...
409                strcmp(ftype,'SaveSystem')
410            if strcmp(ftype,'SaveSystem'), extype = '*.m'; end
411
412            [FileName,DirSpec] = uiputfile(extype,ftype);
413            if isempty(findstr(FileName,'.m')) && strcmpi(ftype,'SaveSystem')
414                FileName = [FileName,'.m'];
415            elseif isempty(findstr(FileName,'.dat')) && ~strcmpi(ftype,'SaveSystem')
416                FileName = [FileName,'.dat'];
417            end
418
419            ts = datestr(now,0);
420            if isequal(FileName,0) || isequal(DirSpec,0)
421                disp('File not open');
422                orbgui('LBox',[ ts ': File not opened...']);
423                return
424            else
425                disp(['File ', [DirSpec FileName], ' open']);
426                orbgui('LBox',['File ', [DirSpec FileName], ' open']);
427                readwrite('ProcessDialog',ftype,DirSpec,FileName);
428                return
429            end
430        end
431
432        %==================
433    case 'ReadResponse'
434        %==================
435
436        FileName =''; %asks user to select Rmatrix
437       
438        temp = getbpmresp(BPM(1).AOFamily, BPM(2).AOFamily, COR(1).AOFamily, ...
439            COR(2).AOFamily,FileName,'struct');
440
441        if isempty(temp)
442            disp('No Response matrix read');
443            return
444        end
445
446        % Fils up matrix reponse structure
447        RSP(1) = response2rsp(temp(1,1),RSP(1),1);
448        RSP(2) = response2rsp(temp(2,2),RSP(2),2);
449
450        BPM = sortbpms(BPM,RSP); %sort for avail, ifit
451        COR = sortcors(COR,RSP); %sort for avail, ifit
452
453        % Updates values into workspace
454        setappdata(0,'BPM',BPM);
455        setappdata(0,'COR',COR);
456        setappdata(0,'RSP',RSP);
457
458        orbgui('RefreshOrbGUI');
459
460        %==============================================================
461    case 'ProcessDialog'                    % *** ProcessDialog ***
462        %==============================================================
463        ftype    = char(varargin(1));
464        DirSpec  = char(varargin(2));
465        FileName = char(varargin(3));
466
467        switch ftype
468
469                %===================
470            case 'ReadDispersion'
471                %===================
472
473                filename = [DirSpec FileName];
474               
475                %% load file
476                temp = load(filename);
477               
478                %% Check data contents
479                if ~isfield(temp,'BPMxDisp') && ~isfield(temp,'BPMyDisp')
480                    disp(['WARNING: Dispersion orbit file not found:' ...
481                        [DirSpec FileName]]);                   
482                    return
483                else % Load data (Dispersion in HW means frequency)
484                    RSP(1).eta = temp.BPMxDisp.Data;
485                    RSP(2).eta = temp.BPMyDisp.Data;
486                end
487
488                %Update RSP
489                setappdata(0,'RSP',RSP);
490
491                %===================
492            case 'ReadCorrectors'
493                %===================
494                [fid,message] = fopen([DirSpec FileName],'r');
495                if fid == -1
496                    disp(['WARNING: Corrector file not found:' [DirSpec FileName]]);
497                    disp(message);
498                    return
499                end
500                fclose(fid);
501
502                readwrite(ftype,[DirSpec FileName],'auto');
503
504                %===================
505            case 'WriteCorrectors'
506                %===================
507                comment = 'Write Correctors';
508                readwrite(ftype,[DirSpec FileName],comment);
509
510                %===================
511            case 'WriteResponse'
512                %===================
513                comment = 'Write Response';
514                readwrite(ftype,[DirSpec FileName],comment);
515
516                %===================
517            case 'RestoreSystem'
518                %===================
519                isok = exist([DirSpec FileName],'file');
520                if ~isok == 2
521                    disp(['WARNING: Restore file not found:' [DirSpec FileName]]);
522                    return
523                end
524                orbgui(ftype,DirSpec,FileName,'auto');
525
526                %===================
527            case 'SaveSystem'
528                %===================
529                comment = 'Save System';
530                orbgui(ftype,[DirSpec FileName],comment);
531
532            otherwise
533                disp(['Warning: no CASE found in readwrite/Dialog Box: ' action]);
534        end   %end of ProcessDialog switch yard
535        %pause(3);    delete(gcf);
536        %=============================================================
537
538%         %=============================================================
539%     case 'OpenHelp'                            % *** OpenHelp ***
540%         %==============================================================
541%         FileName = 'HelpD.m';
542%         fid = fopen(FileName, 'r');
543%
544%         if fid < 0
545%             disp(['**Warning: Help file "', FileName, '" cannot be found.']);
546%             return
547%         end
548%
549%         while feof(fid) == 0
550%             t = fgetl(fid);
551%             disp(t);
552%         end
553%
554%         fclose(fid);
555%
556        %==============================================================
557    otherwise
558        disp(['Warning: no CASE found in readwrite: ' action]);
559end  %end switchyard
Note: See TracBrowser for help on using the repository browser.