Home > functions > device > meg > yokogawa > vb_meg_yokogawa_store_bin_channel.m

vb_meg_yokogawa_store_bin_channel

PURPOSE ^

load measurement data and store it in a binary format for each channel

SYNOPSIS ^

function [data] = vb_meg_yokogawa_store_bin_channel(ykgwfile,meg_info, out_base, verbose_swt)

DESCRIPTION ^

 load measurement data and store it in a binary format for each channel

 [usage]
   [data] = vb_meg_yokogawa_store_bin_channel(ykgwfile, ...
     meg_info, out_base, verbose_swt)

 [input]
      ykgwfile : <required> Yokogawa file
      meg_info : <required> <<struct>> meg header information
      out_base : <required> base directory of data directory
               :            normally, it is the path of new MEG-MAT file.
   verbose_swt : <optional> <<boolean>> switch to output verbose message [true]

 [output]
          data : loaded measurement data [n_channel x n_sample x n_trial]

 [note]
   <<prior conditions>>
     the directory on which data will be stored has been already made.
     MEGinfo.saveman.precision has set something value like 'float64'.

   See also
     vb_meg_yokogawa_store_bin

 [history]
   2007-06-28 (Sako) initial version
   2007-08-22 (Sako) changed stored data directory specification
   2008-02-08 (Sako) modified according to change of channel spec
   2009-06-23 (Sako) supported new data type : 'Evoked_Raw'
   2009-07-13 (Sako) modiefied buffering ch_file, chf_id (second argument)
   2011-02-17 (Sako) added new argument 'out_base'
   2011-06-22 (Sako) modified to fit the new Yokogawa library

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [data] = vb_meg_yokogawa_store_bin_channel(ykgwfile, ...
0002   meg_info, out_base, verbose_swt)
0003 % load measurement data and store it in a binary format for each channel
0004 %
0005 % [usage]
0006 %   [data] = vb_meg_yokogawa_store_bin_channel(ykgwfile, ...
0007 %     meg_info, out_base, verbose_swt)
0008 %
0009 % [input]
0010 %      ykgwfile : <required> Yokogawa file
0011 %      meg_info : <required> <<struct>> meg header information
0012 %      out_base : <required> base directory of data directory
0013 %               :            normally, it is the path of new MEG-MAT file.
0014 %   verbose_swt : <optional> <<boolean>> switch to output verbose message [true]
0015 %
0016 % [output]
0017 %          data : loaded measurement data [n_channel x n_sample x n_trial]
0018 %
0019 % [note]
0020 %   <<prior conditions>>
0021 %     the directory on which data will be stored has been already made.
0022 %     MEGinfo.saveman.precision has set something value like 'float64'.
0023 %
0024 %   See also
0025 %     vb_meg_yokogawa_store_bin
0026 %
0027 % [history]
0028 %   2007-06-28 (Sako) initial version
0029 %   2007-08-22 (Sako) changed stored data directory specification
0030 %   2008-02-08 (Sako) modified according to change of channel spec
0031 %   2009-06-23 (Sako) supported new data type : 'Evoked_Raw'
0032 %   2009-07-13 (Sako) modiefied buffering ch_file, chf_id (second argument)
0033 %   2011-02-17 (Sako) added new argument 'out_base'
0034 %   2011-06-22 (Sako) modified to fit the new Yokogawa library
0035 %
0036 % Copyright (C) 2011, ATR All Rights Reserved.
0037 % License : New BSD License(see VBMEG_LICENSE.txt)
0038 
0039 % --- CHECK ARGUMENTS --- %
0040 if ~exist('ykgwfile', 'var'), ykgwfile = ''; end
0041 if ~exist('meg_info', 'var'), meg_info = []; end
0042 if ~exist('out_base', 'var'), out_base = ''; end
0043 if ~exist('verbose_swt', 'var'), verbose_swt = []; end
0044 [ykgwfile, meg_info, out_base, VERBOSE] = ...
0045   inner_check_arguments(ykgwfile, meg_info, out_base, verbose_swt);
0046 
0047 % --- MAIN PROCEDURE --------------------------------------------------------- %
0048 %
0049 const = vb_define_yokogawa;
0050 func_ = mfilename;
0051 
0052 data = [];  % unused
0053 
0054 % --- channel label list
0055 meg_ch_str = vb_meginfo_get_channel_label_meg(meg_info);
0056 ext_ch_str = vb_meginfo_get_channel_label_extra(meg_info);
0057 ref_ch_str = vb_meginfo_get_channel_label_refmg(meg_info);
0058 combined_ch_str = vb_meginfo_get_channel_label_whole(meg_info);
0059 
0060 % --- NOTICE --- %
0061 % As yokogawa channel name is made from numeric data,
0062 % channel name re-convert to numeric data here for convenience.
0063 Nch_meg = length(meg_ch_str);
0064 meg_ch = zeros(Nch_meg,1);
0065 for i_ch = 1:Nch_meg
0066   meg_ch(i_ch) = str2double(meg_ch_str(i_ch));
0067 end
0068 
0069 Nch_ext = length(ext_ch_str);
0070 ext_ch = zeros(Nch_ext,1);
0071 for i_ch = 1:Nch_ext
0072   ext_ch(i_ch) = str2double(ext_ch_str(i_ch));
0073 end
0074 
0075 Nch_ref = length(ref_ch_str);
0076 ref_ch = zeros(Nch_ref,1);
0077 for i_ch = 1:Nch_ref
0078   ref_ch(i_ch) = str2double(ref_ch_str(i_ch));
0079 end
0080 
0081 Nch = length(combined_ch_str);
0082 combined_ch = zeros(Nch,1);
0083 for i_ch = 1:Nch
0084   combined_ch(i_ch) = str2double(combined_ch_str(i_ch));
0085 end
0086 
0087 % --- number of channels
0088 Nchannel_meg = vb_meginfo_get_channel_number_meg(meg_info);
0089 Nchannel_ext = vb_meginfo_get_channel_number_extra(meg_info);
0090 Nchannel_ref = vb_meginfo_get_channel_number_refmg(meg_info);
0091 Nchannel_all = Nchannel_meg + Nchannel_ext + Nchannel_ref;
0092 
0093 % --- number of sample
0094 Nsample      = vb_meginfo_get_sample_number(meg_info);
0095 
0096 % binary data type which will be used for fwrite
0097 PRECISION = vb_meginfo_get_precision(meg_info);
0098 
0099 % make buffers of file name
0100 ch_file = cell(Nchannel_all,1);
0101 
0102 % make file specifications
0103 chf_fid = zeros(Nchannel_all,1);
0104 
0105 % ---
0106 % --- OPEN BINARY FILES FOR EACH CHANNEL
0107 % ---
0108 
0109 data_dir = [out_base '/' vb_saveman_get_dir(meg_info.saveman)];
0110 
0111 % --- CHANNEL
0112 for ic = 1:Nchannel_all
0113   ch_file{ic} = sprintf('%s%s%d.%s', ...
0114     data_dir, filesep, combined_ch(ic), const.EXT_CHANNEL_BIN);
0115   
0116   chf_fid(ic) = fopen(ch_file{ic}, 'wb');
0117   if chf_fid(ic) == -1
0118     error('(%s)cannot open file (%s)', func_, ch_file{ic});
0119   end
0120 end
0121 
0122 if VERBOSE
0123   fprintf('opening channel file (%d) done.\n', Nchannel_all);
0124 end
0125 
0126 AD_label = 0:Nchannel_all-1;
0127 
0128 %
0129 % --- read Nblock samples and store for each channel
0130 %
0131 
0132 % maximum memory
0133 MemoryMax = meg_info.memory_max;
0134 
0135 Nblock = fix(MemoryMax/Nchannel_all);
0136 
0137 acq_type = vb_meginfo_get_acqtype(meg_info);
0138 
0139 % +1 is for channel name
0140 block = zeros(Nchannel_all, Nblock+1);
0141 % my_data =zeros(1,Nblock);
0142 
0143 nstart = 0;
0144 
0145 if VERBOSE
0146   digit_num = vb_util_get_digit_number(Nsample);
0147   MSG_LEN = inner_print_progress(nstart, Nsample, digit_num);
0148 end
0149 
0150 if strcmp(acq_type, 'Continuous_Raw') || strcmp(acq_type, 'Evoked_Ave')
0151   while nstart < Nsample
0152       ndata = nstart + Nblock;
0153       if ndata > Nsample
0154       if nstart == 0
0155         Nblock = Nsample;
0156       else
0157         Nblock = Nsample - nstart;
0158       end
0159     end
0160 
0161     block = getYkgwData(ykgwfile, nstart, Nblock);
0162     % --- (notice) return value of getYkgwData is calibrated yet.
0163     
0164     nstart = nstart + Nblock;
0165         
0166       for ic = 1:Nchannel_all
0167 
0168       CUR_CH = combined_ch(ic);
0169 
0170       block_idx = CUR_CH == AD_label(:);
0171       my_data = block(block_idx,:);
0172 
0173       fwrite(chf_fid(ic), my_data(:), PRECISION);
0174     end
0175 
0176     % indicator
0177       if VERBOSE
0178         for ib = 1:MSG_LEN
0179           fprintf('\b');
0180         end
0181         inner_print_progress(nstart, Nsample, digit_num);
0182       end
0183   end
0184 
0185 elseif strcmp(acq_type, 'Evoked_Raw')
0186   Ntrial = vb_info_get_trial_number(meg_info);
0187   
0188   if VERBOSE, n_line = 1; end
0189 
0190   % --- load 'Evoked_Raw' data by trial
0191   for i_trial = 1:Ntrial
0192     block = getYkgwData(ykgwfile, i_trial, 1);
0193 
0194     % --- store data by channel
0195     for i_ch = 1:Nchannel_all
0196 
0197       CUR_CH = combined_ch(i_ch);
0198       block_idx = CUR_CH == AD_label(:);
0199 
0200            my_data = block(block_idx,:);
0201           fwrite(chf_fid(i_ch), my_data(:), PRECISION);
0202     end
0203 
0204     if VERBOSE
0205       fprintf('.');
0206       if (i_trial/50) == n_line
0207         fprintf('[%d/%d]\n', i_trial, Ntrial);
0208         n_line = n_line + 1;
0209       end
0210     end
0211   end % --- end of Ntrial
0212 
0213 else
0214   error('(%s) unknown acq_type : %s', mfilename, acq_type);
0215 end
0216 
0217 if VERBOSE
0218   fprintf('\n');
0219 end
0220 
0221 for ic = 1:Nchannel_all
0222   fclose(chf_fid(ic));
0223 end
0224 
0225 return;
0226 %
0227 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0228 
0229 
0230 % --- INNER FUNCTIONS -------------------------------------------------------- %
0231 %
0232 % --- inner_check_arguments()
0233 %
0234 function [ykgwfile, meg_info, out_base, verbose_swt] = ...
0235   inner_check_arguments(ykgwfile, meg_info, out_base, verbose_swt)
0236 
0237 func_ = mfilename;
0238 
0239 if isempty(ykgwfile)
0240   error('(%s) ykgwfile is a required parameter', func_);
0241 end
0242 
0243 if isempty(meg_info)
0244   error('(%s) meg_info is a required parameter', func_);
0245 end
0246 
0247 if isempty(out_base)
0248   error('(%s) out_base is a required parameter', func_);
0249 end
0250 
0251 if isempty(verbose_swt)
0252   verbose_swt = true;
0253 end
0254 return;
0255 %
0256 % --- end of inner_check_arguments()
0257 
0258 % --- inner_print_progress()
0259 %
0260 function str_len = inner_print_progress(present_num, whole_num, digit_num)
0261 
0262 cmd_tmp = sprintf('[%%0%dd / %%0%dd] (%%3d%%%%)', digit_num, digit_num);
0263 prog_rate = floor((present_num / whole_num) * 100);
0264 cmd_str = sprintf( ...
0265   'fprintf(''--- done : %s ...'', present_num, whole_num, prog_rate)', ...
0266     cmd_tmp);
0267 eval(cmd_str);
0268 
0269 % --- "--- done : [0000001 / 1234567] (  0%) ..."
0270 %       4    7     5+digit_num*2         7    4
0271 str_len = 4+7+5+digit_num*2+7+4;
0272 return;
0273 %
0274 % --- end of inner_print_progress()
0275 %
0276 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0277 
0278 % --- END OF FILE --- %

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005