0001 function [data] = fiff_setup_read_raw(fname,allow_maxshield)
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 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
0069
0070 fprintf(1,'Opening raw data file %s...\n',fname);
0071 [ fid, tree ] = fiff_open(fname);
0072
0073
0074
0075 [ info, meas ] = fiff_read_meas_info(fid,tree);
0076
0077
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
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
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
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
0120
0121 if dir(first).kind == FIFF.FIFF_DATA_SKIP
0122
0123
0124
0125 tag = fiff_read_tag(fid,dir(first).pos);
0126 first_skip = tag.data;
0127 first = first + 1;
0128 end
0129
0130
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
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
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
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
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
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
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