0001 function [fid,cals] = fiff_start_writing_raw(name,info,sel)
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 me='MNE:fiff_start_writing_raw';
0034 if nargin ~= 2 && nargin ~= 3
0035     error(me,'Incorrect number of arguments');
0036 end
0037 
0038 global FIFF;
0039 if isempty(FIFF)
0040     FIFF = fiff_define_constants();
0041 end
0042 
0043 
0044 
0045 data_type = 4;
0046 if nargin == 2
0047     sel = 1:info.nchan;
0048 end
0049 chs = info.chs(sel);
0050 nchan = length(chs);
0051 
0052 
0053 
0054 fid = fiff_start_file(name);
0055 fiff_start_block(fid,FIFF.FIFFB_MEAS);
0056 fiff_write_id(fid,FIFF.FIFF_BLOCK_ID);
0057 if ~isempty(info.meas_id)
0058     fiff_write_id(fid,FIFF.FIFF_PARENT_BLOCK_ID,info.meas_id);
0059 end
0060 
0061 
0062 
0063 
0064 fiff_start_block(fid,FIFF.FIFFB_MEAS_INFO);
0065 
0066 
0067 
0068 blocks = [ FIFF.FIFFB_SUBJECT FIFF.FIFFB_HPI_MEAS FIFF.FIFFB_HPI_RESULT FIFF.FIFFB_ISOTRAK FIFF.FIFFB_PROCESSING_HISTORY ];
0069 have_hpi_result = false;
0070 have_isotrak    = false;
0071 if length(blocks) > 0 && isfield(info,'filename') && ~isempty(info.filename)
0072     [ fid2, tree ] = fiff_open(info.filename);
0073     for k = 1:length(blocks)
0074         nodes = fiff_dir_tree_find(tree,blocks(k));
0075         fiff_copy_tree(fid2,tree.id,nodes,fid);
0076         if blocks(k) == FIFF.FIFFB_HPI_RESULT && length(nodes) > 0
0077             have_hpi_result = true;
0078         end
0079         if blocks(k) == FIFF.FIFFB_ISOTRAK && length(nodes) > 0
0080             have_isotrak = true;
0081         end
0082     end
0083     fclose(fid2);
0084 end
0085 
0086 
0087 
0088 if ~isempty(info.acq_pars) || ~isempty(info.acq_stim)
0089     fiff_start_block(fid,FIFF.FIFFB_DACQ_PARS);
0090     if ~isempty(info.acq_pars)
0091         fiff_write_string(fid,FIFF.FIFF_DACQ_PARS, ...
0092             info.acq_pars);
0093     end
0094     if ~isempty(info.acq_stim)
0095         fiff_write_string(fid,FIFF.FIFF_DACQ_STIM, ...
0096             info.acq_stim);
0097     end
0098     fiff_end_block(fid,FIFF.FIFFB_DACQ_PARS);
0099 end
0100 
0101 
0102 
0103 if ~have_hpi_result
0104     if ~isempty(info.dev_head_t)
0105         fiff_write_coord_trans(fid,info.dev_head_t);
0106     end
0107     if ~isempty(info.ctf_head_t)
0108         fiff_write_coord_trans(fid,info.ctf_head_t);
0109     end
0110 end
0111 
0112 
0113 
0114 if ~isempty(info.dig) && ~have_isotrak
0115     fiff_start_block(fid,FIFF.FIFFB_ISOTRAK);
0116     for k = 1:length(info.dig)
0117         fiff_write_dig_point(fid,info.dig(k))
0118     end
0119     fiff_end_block(fid,FIFF.FIFFB_ISOTRAK);
0120 end
0121 
0122 
0123 
0124 fiff_write_proj(fid,info.projs);
0125 
0126 
0127 
0128 fiff_write_ctf_comp(fid,info.comps);
0129 
0130 
0131 
0132 if length(info.bads) > 0
0133     fiff_start_block(fid,FIFF.FIFFB_MNE_BAD_CHANNELS);
0134     fiff_write_name_list(fid,FIFF.FIFF_MNE_CH_NAME_LIST,info.bads);
0135     fiff_end_block(fid,FIFF.FIFFB_MNE_BAD_CHANNELS);
0136 end
0137 
0138 
0139 
0140 fiff_write_float(fid,FIFF.FIFF_SFREQ,info.sfreq);
0141 fiff_write_float(fid,FIFF.FIFF_HIGHPASS,info.highpass);
0142 fiff_write_float(fid,FIFF.FIFF_LOWPASS,info.lowpass);
0143 fiff_write_int(fid,FIFF.FIFF_NCHAN,nchan);
0144 fiff_write_int(fid,FIFF.FIFF_DATA_PACK,data_type);
0145 if [ ~isempty(info.meas_date) ]
0146     fiff_write_int(fid,FIFF.FIFF_MEAS_DATE,info.meas_date);
0147 end
0148 
0149 
0150 
0151 for k = 1:nchan
0152     
0153     
0154     
0155     chs(k).scanno = k;
0156     chs(k).range  = 1.0;
0157     cals(k) = chs(k).cal;
0158     fiff_write_ch_info(fid,chs(k));
0159 end
0160 
0161 
0162 fiff_end_block(fid,FIFF.FIFFB_MEAS_INFO);
0163 
0164 
0165 
0166 fiff_start_block(fid,FIFF.FIFFB_RAW_DATA);
0167 
0168 return;