--- /dev/null
+## Copyright (C) 2010 Peter Cloetens
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING. If not, see
+## <http://www.gnu.org/licenses/>.
+
+## findheader
+## function [res, pos] = findheader(hd, what, kind)
+## read a given info from header of edf-files (.edf), info-file (.info) or parameter file (.par)
+##
+## arguments
+## argument 1 : header or info string
+## argument 2 : parameter to be read (can be 'date')
+## argument 3 : kind|type of parameter
+## possible values are: 'motor', 'counter', 'integer', 'float', 'list' or 'string'
+
+## Author: Peter Cloetens cloetens@esrf.eu
+## Created: 2010-12-10
+## * main change from historical version: avoid any assumption on offset, change help
+
+function [res, pos] = findheader(hd, what, kind)
+
+ if nargin < 3
+ disp('3 input arguments required:')
+ help findheader
+ return
+ end
+
+ 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)
+ 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;
+ pos = pos_equal+1;
+ posend = pos_semicolon-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_equal = findstr(hd(pos:end),'=');
+ pos_equal = pos(1)+pos_equal(1)-1;
+ res = sscanf(hd(pos_equal+1:end),'%g',motnum);
+ res = res(motnum);
+ break
+ endif
+ motnum++;
+ pos += findstr(hd(pos:posend), motor)(1) + length(motor);
+ endwhile
+ else
+ res = [];
+ endif
+ elseif strcmp(kind,'counter')
+ pos=findstr(hd,'counter_mne');
+ if not(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;
+ pos = pos_equal+1;
+ posend = pos_semicolon-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_equal = findstr(hd(pos:end),'=');
+ pos_equal = pos(1)+pos_equal(1)-1;
+ res = sscanf(hd(pos_equal+1:end),'%g',motnum);
+ res = res(motnum);
+ break
+ endif
+ motnum++;
+ pos += findstr(hd(pos:posend), motor)(1) + length(motor);
+ endwhile
+ else
+ res = [];
+ endif
+ 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);
+ case 'list',
+ pos_end = findstr(hd(pos_equal:end),'}');
+ res = hd(pos_equal:pos_equal+pos_end-1);
+ otherwise,
+ res = [];
+ endswitch
+ else
+ res = [];
+ endif
+ endif
+endfunction