source: aedes_readjcamp.m @ 189

Last change on this file since 189 was 189, checked in by tjniskan, 7 years ago
  • Fixed a small bug in reading JCAMP format files (Bruker header files).
  • Fixed contrast/brightness auto-balance for non-float image data.

M aedes.m
M aedes_readjcamp.m
M aedes_revision.m

File size: 3.7 KB
Line 
1function jdx = aedes_readjcamp(filename)
2% AEDES_READJCAMP - Read JCAMP DX format files (Bruker parameter files)
3%   
4%
5% Synopsis:
6%       jdx=aedes_readjcamp(filename)
7%
8% Description:
9%       The function reads the JCAMP DX files and returns a
10%       structure with parameters as structure fields. The input
11%       argument is a string containing the full path to the file.
12%
13% Examples:
14%       jdx=aedes_readjcamp('C:\path\to\jcamp_dx_file')
15%
16% See also:
17%       AEDES_READBRUKER, AEDES_DATA_READ, AEDES
18
19jdx = [];
20
21% Prompt for a file if not given as an input argument
22if nargin == 0
23        [fn,fp] = uigetfile({'*.*','All Files (*.*)'},'Open a JCAMP DX file');
24        if isequal(fn,0)
25                return
26        end
27        filename = [fp,fn];
28elseif nargin > 1
29        error('Too many input arguments.');
30end
31
32% Open the file for reading
33fid = fopen(filename,'r');
34if fid < 0
35        error('Could not open file "%s" for reading.',filename);
36end
37
38% Check that the file is a JCAMP DX file
39str = fread(fid,20,'char=>char');
40if isempty(regexp(str.','^\s*##TITLE'))
41        fclose(fid);
42        error('File "%s" is not a valid JCAMP DX file.',filename)
43end
44fseek(fid,0,-1); % Rewind file
45
46C = fread(fid,inf,'char');
47fclose(fid);
48
49% Remove carriage returns
50C(C==13)=[];
51
52% Convert to string
53C = char(C.');
54
55% Remove comment lines
56C = regexprep(C,'\$\$([^\n]*)\n','');
57
58% Remove unnecessary line breaks
59f = @l_RemoveLineBreaks;
60C=regexprep(C,'^(\s*[^#].*?)(?=\n\s*#)','${f($1)}','lineanchors');
61C=regexprep(C,'(\([^\)]+?)\n(.*?\))','${f([$1,$2])}','lineanchors');
62CC = regexp(C,'\s*##','split');
63CC(1)=[];
64
65% Parse the file line-by-line
66for ii=1:length(CC)
67       
68        str = CC{ii};
69        if strncmp(str,'END=',4)
70                continue
71        end
72       
73        param = regexp(str,'^(.*)=','tokens','once');
74        param = strrep(param{1},'$','');
75        param = l_CheckParameter(param);
76       
77        if any(str==sprintf('\n'))
78                % Get size
79                sz = regexp(str,'=\s*\((.*?)\)\s*\n','tokens','once');
80                sz = str2num(['[',sz{1},']']);
81               
82                % Parse value
83                value = regexp(str,'\n(.*)$','tokens','once');
84                value = value{1};
85                value = l_CheckValue(value,sz);
86        else
87                value = regexp(str,'=\s*(.*)','tokens','once');
88                value = value{1};
89                value = l_CheckValue(value);
90        end
91       
92        % Add to structure
93        jdx.(param) = value;
94       
95end
96
97
98
99
100
101% ==========================
102% - Subfunctions -
103% ==========================
104
105% - Remove linebreaks
106function out = l_RemoveLineBreaks(str)
107
108out = strrep(str,sprintf('\n'),'');
109
110
111% - Check parameter value --------------------------
112function out = l_CheckValue(val,sz)
113
114if nargin == 1
115        sz = 0;
116end
117
118% Remove insignificant whitespace
119val = strtrim(val);
120
121if isempty(val)
122        out = val;
123        return
124end
125
126% Remove string identifiers
127if val(1) == '<' && val(end) == '>'
128        val = val(2:end-1);
129        out = val;
130        return
131end
132
133% Check if the string contains numbers at all before tryin to convert to a
134% number. str2num uses eval command and if the string matches to a
135% function name strange things can happen...
136tmp2 = regexp(val,'.*\d.*');
137if isempty(tmp2)
138        out = val;
139        return
140end
141
142% Convert value to numeric if possible
143tmp = str2num(val);
144if ~isempty(tmp) && isreal(tmp)
145        if length(sz)>1
146                tmp = reshape(tmp,sz(2),sz(1),[]);
147                tmp = permute(tmp,[2 1 3]);
148        end
149        out = tmp;
150        return
151end
152
153out = val;
154
155% - Check parameter strings -------------------------
156function out = l_CheckParameter(param)
157
158alphabets = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
159numbers = '1234567890';
160
161% Remove insignificant whitespace
162param = strtrim(param);
163
164if isempty(param)
165        out = [];
166        return
167end
168
169% Check parameter starts with a valid structure field character
170if ~any(param(1)==alphabets)
171        param = ['PAR_',param];
172end
173
174% Check that the parameter string does not contain any illegal characters
175% (for Matlab structure fields)
176ind = ~ismember(param,[alphabets,numbers,'_']);
177if any(ind)
178        param(ind) = '_';
179end
180
181out = param;
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008