]> Creatis software - CreaPhase.git/blob - utilities_ESRF/findheader.m
useful functions for simulations, created by ESRF people mainly (free to use)
[CreaPhase.git] / utilities_ESRF / findheader.m
1 ## Copyright (C) 2010 Peter Cloetens
2 ## 
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ## 
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ## 
13 ## You should have received a copy of the GNU General Public License
14 ## along with Octave; see the file COPYING.  If not, see
15 ## <http://www.gnu.org/licenses/>.
16
17 ## findheader
18 ## function [res, pos] = findheader(hd, what, kind)
19 ##      read a given info from header of edf-files (.edf), info-file (.info) or parameter file (.par)
20 ##
21 ##      arguments
22 ##      argument 1 : header or info string
23 ##      argument 2 : parameter to be read (can be 'date')
24 ##      argument 3 : kind|type of parameter
25 ##              possible values are: 'motor', 'counter', 'integer', 'float', 'list' or 'string'
26
27 ## Author: Peter Cloetens cloetens@esrf.eu
28 ## Created: 2010-12-10
29 ## * main change from historical version: avoid any assumption on offset, change help
30
31 function [res, pos] = findheader(hd, what, kind)
32     
33     if nargin < 3
34         disp('3 input arguments required:')
35         help findheader
36         return
37     end
38
39     if strcmp(what,'date')
40         pos = findstr(hd,what);
41         if !isempty(pos)
42             pos_equal = findstr(hd(pos:end),'=');
43             pos_equal = pos(1)+pos_equal(1)-1;
44             pos_semicolon = findstr(hd(pos_equal:end),';');
45             pos_semicolon = pos_equal+pos_semicolon(1)-1;
46             res = hd(pos_equal+2:pos_semicolon-2);
47         else    % what is not in header
48             res = [];
49         end
50     elseif strcmp(kind,'motor')
51         pos = findstr(hd,'motor_mne');
52         if !isempty(pos)
53             pos_equal = findstr(hd(pos:end),'=');
54             pos_equal = pos(1)+pos_equal(1)-1;
55             pos_semicolon = findstr(hd(pos_equal:end),';');
56             pos_semicolon = pos_equal+pos_semicolon(1)-1;
57             pos = pos_equal+1;
58             posend = pos_semicolon-1;
59             motnum = 1;
60             res = []; % in case motor does not exist
61             while pos<posend; % looking number of motor
62                 motor = sscanf(hd(pos:posend),'%s',1);
63                 if strcmp(motor, what) % motor was found -> taking corresponding position
64                     pos = findstr(hd,'motor_pos');
65                     pos_equal = findstr(hd(pos:end),'=');
66                     pos_equal = pos(1)+pos_equal(1)-1;
67                     res = sscanf(hd(pos_equal+1:end),'%g',motnum);
68                     res = res(motnum);
69                     break
70                 endif
71                 motnum++;
72                 pos += findstr(hd(pos:posend), motor)(1) + length(motor);
73             endwhile
74         else
75             res = [];
76         endif
77     elseif strcmp(kind,'counter')
78         pos=findstr(hd,'counter_mne');
79         if not(isempty(pos))
80             pos_equal = findstr(hd(pos:end),'=');
81             pos_equal = pos(1)+pos_equal(1)-1;
82             pos_semicolon = findstr(hd(pos_equal:end),';');
83             pos_semicolon = pos_equal+pos_semicolon(1)-1;
84             pos = pos_equal+1;
85             posend = pos_semicolon-1;
86             motnum = 1;
87             res = []; % in case counter does not exist
88             while pos<posend; % looking number of motor
89                 motor = sscanf(hd(pos:posend),'%s',1);
90                 if strcmp(motor,what) % motor was found -> taking corresponding position
91                     pos = findstr(hd,'counter_pos');
92                     pos_equal = findstr(hd(pos:end),'=');
93                     pos_equal = pos(1)+pos_equal(1)-1;
94                     res = sscanf(hd(pos_equal+1:end),'%g',motnum);
95                     res = res(motnum);
96                     break
97                 endif
98                 motnum++;
99                 pos += findstr(hd(pos:posend), motor)(1) + length(motor);
100             endwhile
101         else
102             res = [];
103         endif
104     else # kind is not 'motor' 'counter' 'date'
105         pos=findstr(hd,what);
106         if not(isempty(pos))
107             pos = pos(1);
108             pos_equal = findstr(hd(pos:end),'=');
109             pos_equal = pos+pos_equal(1);
110             switch kind
111                 case 'integer',
112                     res = sscanf(hd(pos_equal:end),'%i');
113                 case 'float',
114                     res = sscanf(hd(pos_equal:end),'%f');
115                 case 'string',
116                     res = sscanf(hd(pos_equal:end),'%s',1);
117                 case 'list',
118                     pos_end = findstr(hd(pos_equal:end),'}');
119                     res = hd(pos_equal:pos_equal+pos_end-1);
120                 otherwise,
121                     res = [];
122             endswitch
123         else
124             res = [];
125         endif
126     endif
127 endfunction