0001 function fiff_write_raw_segment(fname, raw, from, to, sel, drop_small_buffer, buffer_size)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 global FIFF;
0026 if isempty(FIFF)
0027 FIFF = fiff_define_constants();
0028 end
0029
0030 me = 'MNE:fiff_write_raw_segment';
0031
0032 if nargin < 2
0033 error(me, 'Incorrect number of arguments');
0034 end
0035 if nargin < 3 | isempty(from)
0036 from = raw.first_samp;
0037 end
0038 if nargin < 4 | isempty(to)
0039 to = raw.last_samp;
0040 end
0041 if nargin < 5 | isempty(sel)
0042 sel = 1:raw.info.nchan;
0043 end
0044 if nargin < 6
0045 drop_small_buffer = false;
0046 end
0047 if nargin < 7
0048 buffer_size_sec = 25;
0049 buffer_size = ceil(buffer_size_sec * raw.info.sfreq);
0050 end
0051
0052 [outfid, cals] = fiff_start_writing_raw(fname, raw.info, sel);
0053
0054 first_buffer = true;
0055 for first = from:buffer_size:to
0056 last = first + buffer_size - 1;
0057 if last > to
0058 last = to;
0059 end
0060 try
0061 [ data, times ] = fiff_read_raw_segment(raw, first, last, sel);
0062 catch
0063 fclose(raw.fid);
0064 fclose(outfid);
0065 error(me, '%s', mne_omit_first_line(lasterr));
0066 end
0067 if drop_small_buffer && first_buffer == false && length(times) < buffer_size
0068 fprintf(1, 'Skipping due to small buffer ... [done]\n');
0069 break
0070 end
0071
0072
0073
0074 fprintf(1, 'Writing...');
0075 if first_buffer
0076 if first > 0
0077 fiff_write_int(outfid, FIFF.FIFF_FIRST_SAMPLE, first);
0078 end
0079 first_buffer = false;
0080 end
0081 fiff_write_raw_buffer(outfid, data, cals);
0082 fprintf(1, '[done]\n');
0083 end
0084
0085 fiff_finish_writing_raw(outfid);