This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindheader.m
110 lines (95 loc) · 2.54 KB
/
findheader.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
% function [res,pos]=findheader(header,what,kind)
% used to read header of edf-files (.edf), info-file (.info) and parameter file (.par)
% reads value of what from the string header
% what can be 'date'
% to obtain only the hour, use res(12:19)
% kind is integer, float, motor, counter or string
% origin: Peter Cloetens ESRF
function [res,pos]=findheader(hd,what,kind)
if nargin < 3
disp('3 input arguments required:')
help findheader
return
end
offset = 16;
% unused space between start of what and value of what
% contains what,'=' or ' =', and a number of blanks
% the exact offset for an edf-file is 16, for the .info file it is 24
% 16 works for both
if strcmp(what,'date')
pos=findstr(hd,what);
if ~isempty(pos)
pos_equal=findstr(hd(pos:end),'=');
pos_equal=pos(1)+pos_equal(1)-1;
pos_semicolon=findstr(hd(pos_equal:end),';');
pos_semicolon=pos_equal+pos_semicolon(1)-1;
res=hd(pos_equal+2:pos_semicolon-2);
else % what is not in header
res = [];
end
elseif strcmp(kind,'motor')
pos=findstr(hd,'motor_mne');
if ~isempty(pos)
posend=findstr(hd(pos:end),';');
posend=pos-2+posend(1);
pos=pos+offset+1;
motnum=1;
res = []; % in case motor does not exist
while pos<posend; % looking number of motor
motor=sscanf(hd(pos:posend),'%s',1);
if strcmp(motor,what) % motor was found -> taking corresponding position
pos=findstr(hd,'motor_pos');
pos=pos+offset+1;
res=sscanf(hd(pos:end),'%g',motnum);
res=res(motnum);
break
end
motnum=motnum+1;
pos=pos+1+length(motor);
end
else
res = [];
end
elseif strcmp(kind,'counter')
pos=findstr(hd,'counter_mne');
if not(isempty(pos))
posend=findstr(hd(pos:end),';');
posend=pos-2+posend(1);
pos=pos+offset+1;
motnum=1;
res = []; % in case counter does not exist
while pos<posend; % looking number of motor
motor=sscanf(hd(pos:posend),'%s',1);
if strcmp(motor,what) % motor was found -> taking corresponding position
pos=findstr(hd,'counter_pos');
pos=pos+offset+1;
res=sscanf(hd(pos:end),'%g',motnum);
res=res(motnum);
break
end
motnum=motnum+1;
pos=pos+1+length(motor);
end
else
res = [];
end
else % kind is not 'motor' 'counter' 'date'
pos=findstr(hd,what);
if not(isempty(pos))
pos = pos(1);
pos_equal=findstr(hd(pos:end),'=');
pos_equal=pos+pos_equal(1);
switch kind
case 'integer',
res=sscanf(hd(pos_equal:end),'%i');
case 'float',
res=sscanf(hd(pos_equal:end),'%f');
case 'string',
res=sscanf(hd(pos_equal:end),'%s',1);
otherwise,
res=[];
end
else
res=[];
end
end