1 | function convertBPMData2CERNformat(CERNFileName,varargin) |
---|
2 | %convertBPMData2CERNformat - Convert X and Z BPM data to ASCII file : |
---|
3 | % 0 for H plan, 1 for V plane |
---|
4 | % BPM number or Device list |
---|
5 | % Data |
---|
6 | % |
---|
7 | % INPUTS |
---|
8 | % 1. CERNFileName - output filename |
---|
9 | % 2. BPMStructureName - BPM data structure |
---|
10 | % |
---|
11 | % OPTIONAL |
---|
12 | % 1. 'File' - Select file with gui |
---|
13 | % 2. 'File' followed by fileNale - Open directly the data file |
---|
14 | % |
---|
15 | % EXAMPLES |
---|
16 | % 1. convertBPMData2CERNformat('data4cernbis.txt', 'File', 'BPMTurnByTurn_2009-03-16_2kV_horizontal_ 4kV_vertical.mat') |
---|
17 | |
---|
18 | % CERN experiment the 29th and 30rd of March 2009 |
---|
19 | % Written by M.A. Tordeux |
---|
20 | % Modified by Laurent S. Nadolski |
---|
21 | |
---|
22 | DeviceFlag = 0; |
---|
23 | DisplayFlag = 0; |
---|
24 | FileName = ''; |
---|
25 | FileFlag = 0; |
---|
26 | RawDataFlag = 1; |
---|
27 | |
---|
28 | for i = length(varargin):-1:1 |
---|
29 | if strcmpi(varargin{i},'Display') |
---|
30 | DisplayFlag = 1; |
---|
31 | varargin(i) = []; |
---|
32 | elseif strcmpi(varargin{i},'NoDisplay') |
---|
33 | DisplayFlag = 0; |
---|
34 | varargin(i) = []; |
---|
35 | elseif strcmpi(varargin{i},'RawData') |
---|
36 | RawDataFlag = 1; |
---|
37 | varargin(i) = []; |
---|
38 | elseif strcmpi(varargin{i},'RealData') |
---|
39 | RawDataFlag = 0; |
---|
40 | varargin(i) = []; |
---|
41 | elseif strcmpi(varargin{i},'File') |
---|
42 | FileFlag = 1; |
---|
43 | if length(varargin) > i |
---|
44 | % Look for a filename as the next input |
---|
45 | if ischar(varargin{i+1}) |
---|
46 | FileName = varargin{i+1}; |
---|
47 | [a a ext] = fileparts(FileName); |
---|
48 | if isempty(ext) |
---|
49 | FileName = [FileName, '.mat']; |
---|
50 | end |
---|
51 | varargin(i+1) = []; |
---|
52 | end |
---|
53 | end |
---|
54 | varargin(i) = []; |
---|
55 | end |
---|
56 | end |
---|
57 | |
---|
58 | % Reads data structure if given as an input parameter |
---|
59 | if length(varargin) > 1 |
---|
60 | BPMStructureName = varargin{2}; |
---|
61 | FileFlag = 0; |
---|
62 | end |
---|
63 | |
---|
64 | % If filename |
---|
65 | if FileFlag |
---|
66 | if ~isempty(FileName) && exist(FileName, 'file') |
---|
67 | a = load(FileName); |
---|
68 | %BPMStructureName = getfield(a, 'AM'); |
---|
69 | BPMStructureName = getfield(a, 'RawData'); |
---|
70 | else |
---|
71 | DirectoryName = getfamilydata('Directory','BPMData'); |
---|
72 | pwd_old = pwd; |
---|
73 | cd(DirectoryName); |
---|
74 | [Filename PathName] = uigetfile('BPMTurnByTurn*'); |
---|
75 | cd(pwd_old); |
---|
76 | if isequal(FileName,0) |
---|
77 | disp('User pressed cancel') |
---|
78 | exit(0); |
---|
79 | else |
---|
80 | a = load(fullfile(PathName, Filename)); |
---|
81 | %AM = getfield(a, 'AM'); |
---|
82 | AM = getfield(a, 'RawData'); |
---|
83 | end |
---|
84 | end% |
---|
85 | end |
---|
86 | |
---|
87 | % Check if output file alread exists |
---|
88 | if exist(CERNFileName, 'file') |
---|
89 | text = sprintf('Filename %s already exists. Continue (y/N) ? \n', CERNFileName); |
---|
90 | reply = input(text, 's'); |
---|
91 | if isempty(reply) || ~strcmpi(reply, 'Y') |
---|
92 | fprintf('Action aborted\n') |
---|
93 | return; |
---|
94 | end |
---|
95 | end |
---|
96 | |
---|
97 | fid = fopen(CERNFileName, 'wt'); |
---|
98 | BPMStructureName.Data; |
---|
99 | BPMStructureName.DeviceList; |
---|
100 | if RawDataFlag |
---|
101 | DataLength = size(BPMStructureName.Data.X, 2); |
---|
102 | else |
---|
103 | DataLength = size(BPMStructureName.Data.Xreal, 2); |
---|
104 | end |
---|
105 | %DataLength = 2000; |
---|
106 | BPMListLength = size(BPMStructureName.DeviceList,1); % nombre de lignes (au cas où un seul BPM) |
---|
107 | |
---|
108 | fprintf(fid, '#Synchrotron SOLEIL: %s \n', datestr(clock)); % plan H |
---|
109 | F1 = repmat('% 6.4e ',1, DataLength); % format avec repere des BPM par le numéro du BPM |
---|
110 | F0 = '%3d '; |
---|
111 | strElemList = [F0 ' bpm%03d %6.3f ' F1 '\n']; |
---|
112 | strDeviceList = [ F0 F0 F0 F1 '\n']; |
---|
113 | |
---|
114 | if DeviceFlag |
---|
115 | %% format avec repere des BPM par la DeviceList |
---|
116 | for ibpm = 1:BPMListLength, |
---|
117 | if RawDataFlag |
---|
118 | fprintf(fid, strDeviceList, 0, BPMStructureName.DeviceList(ibpm,1), BPMStructureName.DeviceList(ibpm,2),BPMStructureName.Data.X(ibpm,1:DataLength)); % plan H |
---|
119 | fprintf(fid, strDeviceList, 1, BPMStructureName.DeviceList(ibpm,1), BPMStructureName.DeviceList(ibpm,2),BPMStructureName.Data.Z(ibpm,1:DataLength)); % plan V |
---|
120 | else |
---|
121 | fprintf(fid, strDeviceList, 0, BPMStructureName.DeviceList(ibpm,1), BPMStructureName.DeviceList(ibpm,2),BPMStructureName.Data.Xreal(ibpm,1:DataLength)); % plan H |
---|
122 | fprintf(fid, strDeviceList, 1, BPMStructureName.DeviceList(ibpm,1), BPMStructureName.DeviceList(ibpm,2),BPMStructureName.Data.Zreal(ibpm,1:DataLength)); % plan V |
---|
123 | end |
---|
124 | end |
---|
125 | else |
---|
126 | |
---|
127 | %% format avec repere des BPM par le numéro du BPM |
---|
128 | ElementList = dev2elem('BPMx', BPMStructureName.DeviceList); |
---|
129 | spos = getspos('BPMx', BPMStructureName.DeviceList); |
---|
130 | for ibpm = 1:BPMListLength |
---|
131 | if RawDataFlag |
---|
132 | fprintf(fid, strElemList, 0, ElementList(ibpm), spos(ibpm), BPMStructureName.Data.X(ibpm, 1:DataLength)); % plan H |
---|
133 | fprintf(fid, strElemList, 1, ElementList(ibpm), spos(ibpm), BPMStructureName.Data.Z(ibpm, 1:DataLength)); % plan V |
---|
134 | else |
---|
135 | fprintf(fid, strElemList, 0, ElementList(ibpm), spos(ibpm), BPMStructureName.Data.Xreal(ibpm, 1:DataLength)); % plan H |
---|
136 | fprintf(fid, strElemList, 1, ElementList(ibpm), spos(ibpm), BPMStructureName.Data.Zreal(ibpm, 1:DataLength)); % plan V |
---|
137 | end |
---|
138 | end |
---|
139 | end |
---|
140 | |
---|
141 | fclose(fid) |
---|
142 | disp('end') |
---|