0001 function [info,meas] = fiff_read_meas_info(source,tree)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 global FIFF;
0067 if isempty(FIFF)
0068 FIFF = fiff_define_constants();
0069 end
0070
0071 me='MNE:fiff_read_meas_info';
0072
0073 if nargin ~= 2 & nargin ~= 1
0074 error(me,'Incorrect number of arguments');
0075 end
0076
0077 if nargin == 1 & nargout == 2
0078 error(me,'meas output argument is not allowed with file name specified');
0079 end
0080
0081 if nargin == 1
0082 [ fid, tree ] = fiff_open(source);
0083 open_here = true;
0084 else
0085 fid = source;
0086 open_here = false;
0087 end
0088
0089
0090
0091
0092 meas = fiff_dir_tree_find(tree,FIFF.FIFFB_MEAS);
0093 if length(meas) == 0
0094 if open_here
0095 fclose(fid);
0096 end
0097 error(me,'Could not find measurement data');
0098 end
0099
0100 meas_info = fiff_dir_tree_find(meas,FIFF.FIFFB_MEAS_INFO);
0101 if length(meas_info) == 0
0102 if open_here
0103 fclose(fid);
0104 end
0105 error(me,'Could not find measurement info');
0106 end
0107
0108
0109
0110 dev_head_t=[];
0111 ctf_head_t=[];
0112 meas_date=[];
0113 p = 0;
0114 for k = 1:meas_info.nent
0115 kind = meas_info.dir(k).kind;
0116 pos = meas_info.dir(k).pos;
0117 switch kind
0118 case FIFF.FIFF_NCHAN
0119 tag = fiff_read_tag(fid,pos);
0120 nchan = tag.data;
0121 case FIFF.FIFF_SFREQ
0122 tag = fiff_read_tag(fid,pos);
0123 sfreq = tag.data;
0124 case FIFF.FIFF_CH_INFO
0125 p = p+1;
0126 tag = fiff_read_tag(fid,pos);
0127 chs(p) = tag.data;
0128 case FIFF.FIFF_LOWPASS
0129 tag = fiff_read_tag(fid,pos);
0130 lowpass = tag.data;
0131 case FIFF.FIFF_HIGHPASS
0132 tag = fiff_read_tag(fid,pos);
0133 highpass = tag.data;
0134 case FIFF.FIFF_MEAS_DATE
0135 tag = fiff_read_tag(fid,pos);
0136 meas_date = tag.data;
0137 case FIFF.FIFF_COORD_TRANS
0138 tag = fiff_read_tag(fid,pos);
0139 cand = tag.data;
0140 if cand.from == FIFF.FIFFV_COORD_DEVICE && ...
0141 cand.to == FIFF.FIFFV_COORD_HEAD
0142 dev_head_t = cand;
0143 elseif cand.from == FIFF.FIFFV_MNE_COORD_CTF_HEAD && ...
0144 cand.to == FIFF.FIFFV_COORD_HEAD
0145 ctf_head_t = cand;
0146 end
0147 end
0148 end
0149
0150
0151
0152 if ~exist('nchan','var')
0153 if open_here
0154 fclose(fid);
0155 end
0156 error(me,'Number of channels in not defined');
0157 end
0158 if ~exist('sfreq','var')
0159 if open_here
0160 fclose(fid);
0161 end
0162 error(me,'Sampling frequency is not defined');
0163 end
0164 if ~exist('chs','var')
0165 if open_here
0166 fclose(fid);
0167 end
0168 error(me,'Channel information not defined');
0169 end
0170 if length(chs) ~= nchan
0171 if open_here
0172 fclose(fid);
0173 end
0174 error(me,'Incorrect number of channel definitions found');
0175 end
0176
0177
0178 if isempty(dev_head_t) || isempty(ctf_head_t)
0179 hpi_result = fiff_dir_tree_find(meas_info,FIFF.FIFFB_HPI_RESULT);
0180 if length(hpi_result) == 1
0181 for k = 1:hpi_result.nent
0182 kind = hpi_result.dir(k).kind;
0183 pos = hpi_result.dir(k).pos;
0184 if kind == FIFF.FIFF_COORD_TRANS
0185 tag = fiff_read_tag(fid,pos);
0186 cand = tag.data;
0187 if cand.from == FIFF.FIFFV_COORD_DEVICE && ...
0188 cand.to == FIFF.FIFFV_COORD_HEAD
0189 dev_head_t = cand;
0190 elseif cand.from == FIFF.FIFFV_MNE_COORD_CTF_HEAD && ...
0191 cand.to == FIFF.FIFFV_COORD_HEAD
0192 ctf_head_t = cand;
0193 end
0194 end
0195 end
0196 end
0197 end
0198
0199
0200
0201 isotrak = fiff_dir_tree_find(meas_info,FIFF.FIFFB_ISOTRAK);
0202
0203 dig=struct('kind',{},'ident',{},'r',{},'coord_frame',{});
0204 coord_frame = FIFF.FIFFV_COORD_HEAD;
0205 if length(isotrak) == 1
0206 p = 0;
0207 for k = 1:isotrak.nent
0208 kind = isotrak.dir(k).kind;
0209 pos = isotrak.dir(k).pos;
0210 if kind == FIFF.FIFF_DIG_POINT
0211 p = p + 1;
0212 tag = fiff_read_tag(fid,pos);
0213 dig(p) = tag.data;
0214 else
0215 if kind == FIFF.FIFF_MNE_COORD_FRAME
0216 tag = fiff_read_tag(fid,pos);
0217 coord_frame = tag.data;
0218 elseif kind == FIFF.FIFF_COORD_TRANS
0219 tag = fiff_read_tag(fid,pos);
0220 dig_trans = tag.data;
0221 end
0222 end
0223 end
0224 end
0225 for k = 1:length(dig)
0226 dig(k).coord_frame = coord_frame;
0227 end
0228
0229 if exist('dig_trans','var')
0230 if (dig_trans.from ~= coord_frame && dig_trans.to ~= coord_frame)
0231 clear('dig_trans');
0232 end
0233 end
0234
0235
0236
0237 acqpars = fiff_dir_tree_find(meas_info,FIFF.FIFFB_DACQ_PARS);
0238 acq_pars = [];
0239 acq_stim = [];
0240 if length(acqpars) == 1
0241 for k = 1:acqpars.nent
0242 kind = acqpars.dir(k).kind;
0243 pos = acqpars.dir(k).pos;
0244 if kind == FIFF.FIFF_DACQ_PARS
0245 tag = fiff_read_tag(fid,pos);
0246 acq_pars = tag.data;
0247 else if kind == FIFF.FIFF_DACQ_STIM
0248 tag = fiff_read_tag(fid,pos);
0249 acq_stim = tag.data;
0250 end
0251 end
0252 end
0253 end
0254
0255
0256
0257 projs = fiff_read_proj(fid,meas_info);
0258
0259
0260
0261 comps = fiff_read_ctf_comp(fid,meas_info,chs);
0262
0263
0264
0265 bads = fiff_read_bad_channels(fid,meas_info);
0266
0267
0268
0269 if ~isempty(tree.id)
0270 info.file_id = tree.id;
0271 else
0272 info.file_id = [];
0273 end
0274
0275
0276
0277 if isempty(meas_info.parent_id)
0278 if isempty(meas_info.id)
0279 if isempty(meas.id)
0280 if isempty(meas.parent_id)
0281 info.meas_id = info.file_id;
0282 else
0283 info.meas_id = meas.parent_id;
0284 end
0285 else
0286 info.meas_id = meas.id;
0287 end
0288 else
0289 info.meas_id = meas_info.id;
0290 end
0291 else
0292 info.meas_id = meas_info.parent_id;
0293 end
0294 if isempty(meas_date)
0295 info.meas_date = [ info.meas_id.secs info.meas_id.usecs ];
0296 else
0297 info.meas_date = meas_date;
0298 end
0299 info.nchan = nchan;
0300 info.sfreq = sfreq;
0301 if exist('highpass','var')
0302 info.highpass = highpass;
0303 else
0304 info.highpass = 0;
0305 end
0306 if exist('lowpass','var')
0307 info.lowpass = lowpass;
0308 else
0309 info.lowpass = info.sfreq/2.0;
0310 end
0311
0312
0313
0314
0315 info.chs = chs;
0316 for c = 1:info.nchan
0317 info.ch_names{c} = info.chs(c).ch_name;
0318 end
0319
0320
0321
0322 info.dev_head_t = dev_head_t;
0323 info.ctf_head_t = ctf_head_t;
0324 if ~isempty(info.dev_head_t) && ~isempty(info.ctf_head_t)
0325 info.dev_ctf_t = info.dev_head_t;
0326 info.dev_ctf_t.to = info.ctf_head_t.from;
0327 info.dev_ctf_t.trans = inv(ctf_head_t.trans)*info.dev_ctf_t.trans;
0328 else
0329 info.dev_ctf_t = [];
0330 end
0331
0332
0333
0334 info.dig = dig;
0335 if exist('dig_trans','var')
0336 info.dig_trans = dig_trans;
0337 end
0338 info.bads = bads;
0339 info.projs = projs;
0340 info.comps = comps;
0341 info.acq_pars = acq_pars;
0342 info.acq_stim = acq_stim;
0343
0344 if open_here
0345 fclose(fid);
0346 end
0347
0348 return;
0349
0350 function [tag] = find_tag(node,findkind)
0351
0352 for p = 1:node.nent
0353 if node.dir(p).kind == findkind
0354 tag = fiff_read_tag(fid,node.dir(p).pos);
0355 return;
0356 end
0357 end
0358 tag = [];
0359 end
0360
0361 end