Home > vbmeg > external > mne > fiff_read_meas_info.m

fiff_read_meas_info

PURPOSE ^

SYNOPSIS ^

function [info,meas] = fiff_read_meas_info(source,tree)

DESCRIPTION ^

 [info,meas] = fiff_read_meas_info(source,tree)

 Read the measurement info

 If tree is specified, source is assumed to be an open file id,
 otherwise a the name of the file to read. If tree is missing, the
 meas output argument should not be specified.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [info,meas] = fiff_read_meas_info(source,tree)
0002 %
0003 % [info,meas] = fiff_read_meas_info(source,tree)
0004 %
0005 % Read the measurement info
0006 %
0007 % If tree is specified, source is assumed to be an open file id,
0008 % otherwise a the name of the file to read. If tree is missing, the
0009 % meas output argument should not be specified.
0010 %
0011 
0012 
0013 %
0014 %   Author : Matti Hamalainen, MGH Martinos Center
0015 %   License : BSD 3-clause
0016 %
0017 %
0018 %   Revision 1.14  2009/03/31 01:12:30  msh
0019 %   Improved ID handling
0020 %
0021 %   Revision 1.13  2008/04/16 22:24:57  msh
0022 %   Added megacq parameters to the measurement info
0023 %
0024 %   Revision 1.12  2008/03/13 19:18:06  msh
0025 %   Read and write FIFF_MEAS_DATE from/to FIFFB_MEAS_INFO as appropriate
0026 %
0027 %   Revision 1.11  2007/11/13 10:55:32  msh
0028 %   Specify the second argument to all calls to the exist function
0029 %
0030 %   Revision 1.10  2006/09/24 18:52:43  msh
0031 %   Added FIFFV_REF_MEG_CH to fiff_define_constants.
0032 %   Added coord_frame field to dig point structure.
0033 %
0034 %   Revision 1.9  2006/09/08 19:27:13  msh
0035 %   Added KIT coil type to mne_load_coil_def
0036 %   Allow reading of measurement info by specifying just a file name.
0037 %
0038 %   Revision 1.8  2006/04/23 15:29:40  msh
0039 %   Added MGH to the copyright
0040 %
0041 %   Revision 1.7  2006/04/18 20:44:46  msh
0042 %   Added reading of forward solution.
0043 %   Use length instead of size when appropriate
0044 %
0045 %   Revision 1.6  2006/04/14 15:49:49  msh
0046 %   Improved the channel selection code and added ch_names to measurement info.
0047 %
0048 %   Revision 1.5  2006/04/13 23:09:46  msh
0049 %   Further streamlining of the coordinate transformations.
0050 %
0051 %   Revision 1.4  2006/04/13 22:37:03  msh
0052 %   Added head_head_trans field to info.
0053 %
0054 %   Revision 1.3  2006/04/13 17:05:45  msh
0055 %   Added reading of bad channels to fiff_read_meas_info.m
0056 %   Added mne_pick_channels_cov.m
0057 %
0058 %   Revision 1.2  2006/04/12 10:29:02  msh
0059 %   Made evoked data writing compatible with the structures returned in reading.
0060 %
0061 %   Revision 1.1  2006/04/10 23:26:54  msh
0062 %   Added fiff reading routines
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 %   Find the desired blocks
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 %   Read measurement info
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 %   Check that we have everything we need
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 %   Locate the Polhemus data
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 %   Locate the acquisition information
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 %   Load the SSP data
0256 %
0257 projs = fiff_read_proj(fid,meas_info);
0258 %
0259 %   Load the CTF compensation data
0260 %
0261 comps = fiff_read_ctf_comp(fid,meas_info,chs);
0262 %
0263 %   Load the bad channel list
0264 %
0265 bads = fiff_read_bad_channels(fid,meas_info);
0266 %
0267 %   Put the data together
0268 %
0269 if ~isempty(tree.id)
0270     info.file_id = tree.id;
0271 else
0272     info.file_id = [];
0273 end
0274 %
0275 %  Make the most appropriate selection for the measurement id
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 %   Add the channel information and make a list of channel names
0313 %   for convenience
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 %  Add the coordinate transformations
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 %   All kinds of auxliary stuff
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

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005