Home > vbmeg > external > mne > fiff_setup_read_raw.m

fiff_setup_read_raw

PURPOSE ^

SYNOPSIS ^

function [data] = fiff_setup_read_raw(fname,allow_maxshield)

DESCRIPTION ^

 [data] = fiff_setup_read_raw(fname,allow_maxshield)

 Read information about raw data file

 fname               Name of the file to read
 allow_maxshield     Accept unprocessed MaxShield data

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [data] = fiff_setup_read_raw(fname,allow_maxshield)
0002 %
0003 % [data] = fiff_setup_read_raw(fname,allow_maxshield)
0004 %
0005 % Read information about raw data file
0006 %
0007 % fname               Name of the file to read
0008 % allow_maxshield     Accept unprocessed MaxShield data
0009 %
0010 
0011 %
0012 %   Author : Matti Hamalainen, MGH Martinos Center
0013 %   License : BSD 3-clause
0014 %
0015 %   Revision 1.12  2009/03/30 11:37:37  msh
0016 %   Added copying of measurement info blocks from the original like in mne_browse_raw
0017 %
0018 %   Revision 1.11  2008/09/30 17:22:39  msh
0019 %   Added possibility to load unprocessed MaxShield data
0020 %
0021 %   Revision 1.10  2008/06/16 20:26:03  msh
0022 %   Added some semicolons to avoid unncessary output.
0023 %
0024 %   Revision 1.9  2008/06/16 19:48:32  msh
0025 %   Added some comments
0026 %
0027 %   Revision 1.8  2008/06/16 19:46:30  msh
0028 %   Fixed handling of initial skip so that the result is compatible with the event files
0029 %
0030 %   Revision 1.7  2007/06/08 14:44:16  msh
0031 %   Fixed problem appearing with multiple skips in a data file
0032 %
0033 %   Revision 1.6  2006/05/03 18:53:05  msh
0034 %   Approaching Matlab 6.5 backward compatibility
0035 %
0036 %   Revision 1.5  2006/04/23 15:29:40  msh
0037 %   Added MGH to the copyright
0038 %
0039 %   Revision 1.4  2006/04/21 16:17:48  msh
0040 %   Added handling of CTF compensation
0041 %
0042 %   Revision 1.3  2006/04/21 14:23:16  msh
0043 %   Further improvements in raw data reading
0044 %
0045 %   Revision 1.2  2006/04/21 11:33:18  msh
0046 %   Added raw segment reading routines
0047 %
0048 %   Revision 1.1  2006/04/10 23:26:54  msh
0049 %   Added fiff reading routines
0050 %
0051 %
0052 
0053 global FIFF;
0054 if isempty(FIFF)
0055     FIFF = fiff_define_constants();
0056 end
0057 
0058 me='MNE:fiff_setup_read_raw';
0059 
0060 if nargin ~= 1 && nargin ~= 2
0061     error(me,'Incorrect number of arguments');
0062 end
0063 
0064 if nargin == 1
0065     allow_maxshield = false;
0066 end
0067 %
0068 %   Open the file
0069 %
0070 fprintf(1,'Opening raw data file %s...\n',fname);
0071 [ fid, tree ] = fiff_open(fname);
0072 %
0073 %   Read the measurement info
0074 %
0075 [ info, meas ] = fiff_read_meas_info(fid,tree);
0076 %
0077 %   Locate the data of interest
0078 %
0079 raw = fiff_dir_tree_find(meas,FIFF.FIFFB_RAW_DATA);
0080 if isempty(raw)
0081     raw = fiff_dir_tree_find(meas,FIFF.FIFFB_CONTINUOUS_DATA);
0082     if allow_maxshield
0083         raw = fiff_dir_tree_find(meas,FIFF.FIFFB_SMSH_RAW_DATA);
0084         if isempty(raw)
0085             error(me,'No raw data in %s',fname);
0086         end
0087     else
0088         if isempty(raw)
0089             error(me,'No raw data in %s',fname);
0090         end
0091     end
0092 end
0093 %
0094 %   Set up the output structure
0095 %
0096 info.filename   = fname;
0097 data.fid        = fid;
0098 data.info       = info;
0099 data.first_samp = 0;
0100 data.last_samp  = 0;
0101 %
0102 %   Process the directory
0103 %
0104 dir          = raw.dir;
0105 nent         = raw.nent;
0106 nchan        = info.nchan;
0107 first        = 1;
0108 first_samp   = 0;
0109 first_skip   = 0;
0110 %
0111 %  Get first sample tag if it is there
0112 %
0113 if dir(first).kind == FIFF.FIFF_FIRST_SAMPLE
0114     tag = fiff_read_tag(fid,dir(first).pos);
0115     first_samp = tag.data;
0116     first = first + 1;
0117 end
0118 %
0119 %  Omit initial skip
0120 %
0121 if dir(first).kind == FIFF.FIFF_DATA_SKIP
0122     %
0123     %  This first skip can be applied only after we know the buffer size
0124     %
0125     tag = fiff_read_tag(fid,dir(first).pos);
0126     first_skip = tag.data;
0127     first = first + 1;
0128 end
0129 %
0130 %  Get first sample tag if it is there
0131 %
0132 if dir(first).kind == FIFF.FIFF_FIRST_SAMPLE
0133     tag = fiff_read_tag(fid,dir(first).pos);
0134     first_samp = first_samp + tag.data;
0135     first = first + 1;
0136 end
0137 data.first_samp = first_samp;
0138 %
0139 %   Go through the remaining tags in the directory
0140 %
0141 rawdir = struct('ent',{},'first',{},'last',{},'nsamp',{});
0142 nskip = 0;
0143 ndir  = 0;
0144 for k = first:nent
0145     ent = dir(k);
0146     if ent.kind == FIFF.FIFF_DATA_SKIP
0147         tag = fiff_read_tag(fid,ent.pos);
0148         nskip = tag.data;
0149     elseif ent.kind == FIFF.FIFF_DATA_BUFFER
0150         %
0151         %   Figure out the number of samples in this buffer
0152         %
0153         switch ent.type
0154             case FIFF.FIFFT_DAU_PACK16
0155                 nsamp = ent.size/(2*nchan);
0156             case FIFF.FIFFT_SHORT
0157                 nsamp = ent.size/(2*nchan);
0158             case FIFF.FIFFT_FLOAT
0159                 nsamp = ent.size/(4*nchan);
0160             case FIFF.FIFFT_INT
0161                 nsamp = ent.size/(4*nchan);
0162             otherwise
0163                 fclose(fid);
0164                 error(me,'Cannot handle data buffers of type %d',ent.type);
0165         end
0166         %
0167         %  Do we have an initial skip pending?
0168         %
0169         if first_skip > 0
0170             first_samp = first_samp + nsamp*first_skip;
0171             data.first_samp = first_samp;
0172             first_skip = 0;
0173         end
0174         %
0175         %  Do we have a skip pending?
0176         %
0177         if nskip > 0
0178             ndir        = ndir+1;
0179             rawdir(ndir).ent   = [];
0180             rawdir(ndir).first = first_samp;
0181             rawdir(ndir).last  = first_samp + nskip*nsamp - 1;
0182             rawdir(ndir).nsamp = nskip*nsamp;
0183             first_samp = first_samp + nskip*nsamp;
0184             nskip = 0;
0185         end
0186         %
0187         %  Add a data buffer
0188         %
0189         ndir               = ndir+1;
0190         rawdir(ndir).ent   = ent;
0191         rawdir(ndir).first = first_samp;
0192         rawdir(ndir).last  = first_samp + nsamp - 1;
0193         rawdir(ndir).nsamp = nsamp;
0194         first_samp = first_samp + nsamp;
0195     end
0196 end
0197 data.last_samp  = first_samp - 1;
0198 %
0199 %   Add the calibration factors
0200 %
0201 cals = zeros(1,data.info.nchan);
0202 for k = 1:data.info.nchan
0203     cals(k) = data.info.chs(k).range*data.info.chs(k).cal;
0204 end
0205 %
0206 data.cals       = cals;
0207 data.rawdir     = rawdir;
0208 data.proj       = [];
0209 data.comp       = [];
0210 %
0211 fprintf(1,'\tRange : %d ... %d  =  %9.3f ... %9.3f secs\n',...
0212     data.first_samp,data.last_samp,...
0213     double(data.first_samp)/data.info.sfreq,...
0214     double(data.last_samp)/data.info.sfreq);
0215 fprintf(1,'Ready.\n');
0216 fclose(data.fid);
0217 data.fid = -1;
0218 return;
0219 
0220     function [tag] = find_tag(node,findkind)
0221 
0222         for p = 1:node.nent
0223             kind = node.dir(p).kind;
0224             pos  = node.dir(p).pos;
0225             if kind == findkind
0226                 tag = fiff_read_tag(fid,pos);
0227                 return;
0228             end
0229         end
0230         tag = [];
0231         return
0232 
0233     end
0234 
0235 end

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