Home > vbmeg > external > mne > mne_rt_data_client.m

mne_rt_data_client

PURPOSE ^

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 classdef mne_rt_data_client < mne_rt_client
0002     %MNE_RT_DATA_CLIENT is a class to parse incomming fiff data tags send
0003     % by mne_rt_server using the fiff data port
0004     %
0005     %   This class is inherited of mne_rt_client.
0006     %
0007     %   Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center
0008     %   License : BSD 3-clause
0009     %
0010     properties
0011         m_clientID = -1; % The client ID assigned by mne_rt_server
0012     end
0013     
0014     methods
0015         % =================================================================
0016         %% mne_rt_data_client
0017         function obj = mne_rt_data_client(host, port, numOfRetries)
0018             %
0019             % obj = mne_rt_data_client(host, port, numOfRetries)
0020             %
0021             % Constructor
0022             %
0023             % host          - ip adress of the mne_rt_Server
0024             % port          - fiff data port of the mne_rt_Server
0025             % numOfRetries  - number of connection retries, when a
0026             %                 connection fails
0027             %
0028             %   Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center
0029             %   License : BSD 3-clause
0030             %
0031             if (nargin < 3)
0032                 numOfRetries = 20; % set to -1 for infinite
0033             end
0034             obj = obj@mne_rt_client(host, port, numOfRetries);%Superclass call
0035             obj.getClientId();
0036         end % mne_rt_data_client
0037         
0038         % =================================================================
0039         %% readInfo
0040         function [info] = readInfo(obj)
0041             % reads the measurement info
0042             import java.net.Socket
0043             import java.io.*
0044             
0045             global FIFF;
0046             if isempty(FIFF)
0047                 FIFF = fiff_define_constants();
0048             end
0049             
0050             if ~isempty(obj.m_DataInputStream)
0051                 
0052                 t_bReadMeasBlockStart = false;
0053                 t_bReadMeasBlockEnd = false;
0054                 %
0055                 % Find the start
0056                 %
0057                 while(~t_bReadMeasBlockStart)
0058                     tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0059 
0060                     if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_MEAS_INFO)
0061                         disp('FIFF_BLOCK_START FIFFB_MEAS_INFO'); 
0062                         t_bReadMeasBlockStart = true;
0063                     end
0064                 end
0065 
0066                 %
0067                 % Parse until the endblock
0068                 %
0069                 
0070                 info.dev_head_t = [];
0071                 info.ctf_head_t = [];
0072                 info.dev_ctf_t = [];
0073                 info.acq_pars = [];
0074                 info.acq_stim = [];
0075                 info.chs = [];
0076   
0077                 dev_head_t_read = false;
0078                 ctf_head_t_read = false;
0079 
0080                 while(~t_bReadMeasBlockEnd)
0081                     tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0082                     %
0083                     %  megacq parameters
0084                     %
0085                     if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_DACQ_PARS)                        
0086                         while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_DACQ_PARS)
0087                             tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0088                             if(tag.kind == FIFF.FIFF_DACQ_PARS)
0089                                 info.acq_pars = tag.data;
0090                             elseif(tag.kind == FIFF.FIFF_DACQ_STIM)
0091                                 info.acq_stim = tag.data;
0092                             end
0093                         end
0094                     end
0095                     %
0096                     %    Coordinate transformations if the HPI result block was not there
0097                     %
0098                     if (tag.kind == FIFF.FIFF_COORD_TRANS)
0099                         if (~dev_head_t_read)
0100                             info.dev_head_t = tag.data;
0101                             dev_head_t_read = true;
0102                         elseif (~ctf_head_t_read)
0103                             info.ctf_head_t = tag.data;
0104                             ctf_head_t_read = true;
0105                         end
0106                     end
0107                     %
0108                     %    Polhemus data
0109                     %
0110                     if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_ISOTRAK)
0111                        info.dig = [];
0112                        while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_ISOTRAK)
0113                             tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0114                             if(tag.kind == FIFF.FIFF_DIG_POINT)
0115                                 info.dig = [info.dig tag.data];
0116                             end
0117                         end
0118                     end
0119                     %
0120                     %    Projectors
0121                     %
0122                     if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_PROJ)
0123                        info.projs = [];
0124                        while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_PROJ)
0125                             tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0126                             if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_PROJ_ITEM)
0127                                proj = [];
0128                                while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_PROJ_ITEM)
0129                                     tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0130                                     switch tag.kind
0131                                         case FIFF.FIFF_NAME
0132                                             proj.desc = tag.data;
0133                                         case FIFF.FIFF_PROJ_ITEM_KIND
0134                                             proj.kind = tag.data;
0135                                         case FIFF.FIFF_NCHAN
0136                                             proj.data.ncol = tag.data;
0137                                         case FIFF.FIFF_PROJ_ITEM_NVEC
0138                                             proj.data.nrow = tag.data;
0139                                         case FIFF.FIFF_MNE_PROJ_ITEM_ACTIVE
0140                                             proj.active = tag.data;
0141                                         case FIFF.FIFF_PROJ_ITEM_CH_NAME_LIST
0142                                             proj.data.col_names = fiff_split_name_list(tag.data);
0143                                         case FIFF.FIFF_PROJ_ITEM_VECTORS
0144                                             proj.data.data = tag.data;
0145                                     end
0146                                 end
0147                             end
0148                             
0149                             if ~isempty(proj)
0150                                 info.projs = [info.projs proj];
0151                             end
0152                         end
0153                     end
0154                     %
0155                     %    CTF compensation info
0156                     %
0157                     if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_MNE_CTF_COMP)
0158                        info.comps = [];
0159                        while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_MNE_CTF_COMP)
0160                             tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0161                             if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_MNE_CTF_COMP_DATA)
0162                                comp = [];
0163                                while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_MNE_CTF_COMP_DATA)
0164                                     tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0165                                     switch tag.kind
0166                                         case FIFF.FIFF_MNE_CTF_COMP_KIND
0167                                             comp.ctfkind = tag.data;
0168                                         case FIFF.FIFF_MNE_CTF_COMP_CALIBRATED
0169                                             comp.save_calibrated = tag.data;
0170                                         case FIFF.FIFF_MNE_CTF_COMP_DATA
0171                                             comp.data = tag.data;
0172                                     end
0173                                 end
0174                             end
0175                             
0176                             if ~isempty(comp)
0177                                 info.comps = [info.comps comp];
0178                             end
0179                         end
0180                     end
0181                     %
0182                     %    Bad channels
0183                     %
0184                     if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_MNE_BAD_CHANNELS)
0185                        info.bads = [];
0186                        while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_MNE_BAD_CHANNELS)
0187                             tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0188                             if(tag.kind == FIFF.FIFF_MNE_CH_NAME_LIST)
0189                                 info.bads = fiff_split_name_list(tag.data);
0190                             end
0191                         end
0192                     end
0193                     %
0194                     %    General
0195                     %
0196                     if (tag.kind == FIFF.FIFF_SFREQ)
0197                         info.sfreq = tag.data;
0198                     elseif (tag.kind == FIFF.FIFF_HIGHPASS)
0199                         info.highpass = tag.data;
0200                     elseif (tag.kind == FIFF.FIFF_LOWPASS)
0201                         info.lowpass = tag.data;
0202                     elseif (tag.kind == FIFF.FIFF_NCHAN)
0203                         info.nchan = tag.data;
0204                     elseif (tag.kind == FIFF.FIFF_MEAS_DATE)
0205                         info.highpass = tag.data;
0206                     end
0207                         
0208                     
0209                     if (tag.kind == FIFF.FIFF_CH_INFO)
0210                         info.chs = [info.chs tag.data];
0211                     end
0212                     
0213                     % END MEAS
0214                     if(tag.kind == FIFF.FIFF_BLOCK_END && tag.data == FIFF.FIFFB_MEAS_INFO)
0215                         disp('FIFF_BLOCK_END FIFFB_MEAS_INFO'); 
0216                         t_bReadMeasBlockEnd = true;
0217                     end
0218                 end
0219             else
0220                 error('mne_rt_data_client: no available TcpSocket, call init to establish a connection.');
0221             end
0222         end
0223         
0224         % =================================================================
0225         %% readRawBuffer
0226         function [kind, data] = readRawBuffer(obj, p_nChannels)
0227             %
0228             % [kind, data] = readRawBuffer(obj, p_nChannels)
0229             %
0230             % reads a raw buffer
0231             %
0232             % p_nChannels - number of channels to reshape the incomming
0233             %               float array
0234             %
0235             % kind        - FIFF_DATA_BUFFER ->
0236             %               FIFF_BLOCK_START -> data = FIFFB_RAW_DATA
0237             %               FIFF_BLOCK_END -> data = FIFFB_RAW_DATA
0238             % data        - the read buffer
0239             
0240             %
0241             %   Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center
0242             %   License : BSD 3-clause
0243             %
0244 
0245             import java.net.Socket
0246             import java.io.*
0247               
0248             global FIFF;
0249             if isempty(FIFF)
0250                 FIFF = fiff_define_constants();
0251             end
0252             
0253             data = [];
0254             kind = [];
0255             
0256             if ~isempty(obj.m_DataInputStream)
0257 
0258                 bytes_available = obj.m_DataInputStream.available;
0259                 % Wait for incomming bytes
0260                 while(bytes_available == 0)
0261                     bytes_available = obj.m_DataInputStream.available;
0262                 end
0263 
0264                 tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0265                 
0266                 kind = tag.kind;
0267                 
0268                 if(tag.kind == FIFF.FIFF_DATA_BUFFER)
0269                     nSamples = length(tag.data)/p_nChannels;
0270                     data = reshape(tag.data, p_nChannels, nSamples);
0271                 else
0272                     data = tag.data;
0273                 end
0274             else
0275                 error('mne_rt_data_client: no available TcpSocket, call init to establish a connection.');
0276             end
0277         end
0278         
0279         % =================================================================
0280         %% setClientAlias
0281         function setClientAlias(obj, alias)
0282             %
0283             % setClientAlias(obj, alias)
0284             %
0285             % sets the alias of the fiff data client -> for convinient
0286             % identification
0287             %
0288             % alias - name which should be used
0289             %
0290 
0291             %
0292             %   Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center
0293             %   License : BSD 3-clause
0294             %
0295 
0296             import java.net.Socket
0297             import java.io.*
0298 
0299             global MNE_RT;
0300             if isempty(MNE_RT)
0301                 MNE_RT = mne_rt_define_commands();
0302             end
0303             
0304             if ~isempty(obj.m_DataOutputStream)
0305                 mne_rt_data_client.sendFiffCommand(obj.m_DataOutputStream, MNE_RT.MNE_RT_SET_CLIENT_ALIAS, alias)
0306             end
0307         end
0308         
0309         % =================================================================
0310         %% getClientId
0311         function [id] = getClientId(obj)
0312             %
0313             % [id] = getClientId(obj)
0314             %
0315             % sets and returns the client id which was assigned by
0316             % mne_rt_server
0317 
0318             %
0319             %   Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center
0320             %   License : BSD 3-clause
0321             %
0322             
0323             import java.net.Socket
0324             import java.io.*
0325             if(obj.m_clientID == -1)
0326                 global FIFF;
0327                 if isempty(FIFF)
0328                     FIFF = fiff_define_constants();
0329                 end
0330                 global MNE_RT;
0331                 if isempty(MNE_RT)
0332                     MNE_RT = mne_rt_define_commands();
0333                 end
0334 
0335                 if ~isempty(obj.m_DataOutputStream)
0336 
0337                     mne_rt_data_client.sendFiffCommand(obj.m_DataOutputStream, MNE_RT.MNE_RT_GET_CLIENT_ID)
0338 
0339                     % ID is send as answer
0340                     tag = obj.read_tag(obj.m_DataInputStream);
0341                     if (tag.kind == FIFF.FIFF_MNE_RT_CLIENT_ID)
0342                         obj.m_clientID = tag.data;
0343                     end                
0344                 end
0345             end
0346             id = obj.m_clientID;
0347         end
0348     end
0349     
0350     methods(Static)
0351         % =================================================================
0352         %% sendFiffCommand
0353         function sendFiffCommand(p_DataOutputStream, p_Cmd, p_data)
0354             %
0355             % sendFiffCommand(p_DataOutputStream, p_Cmd, p_data)
0356             %
0357             % sends a fiff encoded command to mne_rt_server using the fiff
0358             % data port
0359             % command and data are encoded into the fiff tag data
0360             %
0361             % p_DataOutputStream - open output data stream
0362             % p_Cmd              - command to send
0363             % p_data             - data which should be also encoded
0364             %
0365 
0366             %
0367             %   Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center
0368             %   License : BSD 3-clause
0369             %
0370 
0371             import java.net.Socket
0372             import java.io.*
0373             
0374             global FIFF;
0375             if isempty(FIFF)
0376                 FIFF = fiff_define_constants();
0377             end
0378             
0379             if (nargin == 3)
0380                 data = char(p_data);
0381             elseif(nargin == 2)
0382                 data = [];
0383             else
0384                 error('Wrong number of arguments.');
0385             end
0386             
0387             kind = FIFF.FIFF_MNE_RT_COMMAND;
0388             type = FIFF.FIFFT_VOID;
0389             size = 4+length(data);% first 4 bytes are the command code
0390             next = 0;
0391             
0392             p_DataOutputStream.writeInt(kind);
0393             p_DataOutputStream.writeInt(type);
0394             p_DataOutputStream.writeInt(size);
0395             p_DataOutputStream.writeInt(next);
0396             p_DataOutputStream.writeInt(p_Cmd);% first 4 bytes are the command code
0397             if(~isempty(data))
0398                 p_DataOutputStream.writeBytes(data);
0399             end
0400             p_DataOutputStream.flush;
0401         end
0402         
0403         % =================================================================
0404         %% read_tag
0405         function [tag] = read_tag(p_DataInputStream)
0406             %
0407             % [tag] = read_tag(p_DataInputStream)
0408             %
0409             % reads a fiff encoded real-time data stream
0410             %
0411             % p_DataInputStream - open data stream
0412             %
0413             
0414             %
0415             %   Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center
0416             %   License : BSD 3-clause
0417             %
0418             import java.net.Socket
0419             import java.io.*
0420             
0421             me='MNE_RT_DATA_CLIENT:read_tag';
0422             
0423             %
0424             % read the tag info
0425             %
0426             tagInfo = mne_rt_data_client.read_tag_info(p_DataInputStream);
0427 
0428             %
0429             % read the tag data
0430             %
0431             tag = mne_rt_data_client.read_tag_data(p_DataInputStream, tagInfo);
0432         end
0433         
0434         % =================================================================
0435         %% read_tag_data
0436         function [tag] = read_tag_data(p_DataInputStream, p_tagInfo, pos)
0437         %
0438         % [tag] = read_tag_data(p_dInputStream, pos)
0439         %
0440         % Reads the tag data from a fif stream.
0441         % if pos is not provided, reading starts from the current stream position
0442         %
0443         % p_DataInputStream - the open data stream
0444         % p_tagInfo         - the tag info
0445         % pos               - number of bytes to be skipped
0446         %
0447         
0448         %
0449         %   Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center
0450         %   License : BSD 3-clause
0451         %
0452             import java.net.Socket
0453             import java.io.*
0454             
0455             global FIFF;
0456             if isempty(FIFF)
0457                 FIFF = fiff_define_constants();
0458             end
0459 
0460             me='MNE_RT_DATA_CLIENT:read_tag_data';
0461 
0462             if nargin == 3
0463                 p_DataInputStream.skipBytes(pos);
0464             elseif nargin ~= 2
0465                 error(me,'Incorrect number of arguments');
0466             end
0467 
0468             tag = p_tagInfo;
0469 
0470             %
0471             %   The magic hexadecimal values
0472             %
0473             is_matrix           = 4294901760; % ffff0000
0474             matrix_coding_dense = 16384;      % 4000
0475             matrix_coding_CCS   = 16400;      % 4010
0476             matrix_coding_RCS   = 16416;      % 4020
0477             data_type           = 65535;      % ffff
0478             %
0479             if tag.size > 0
0480                 matrix_coding = bitand(is_matrix,tag.type);
0481                 if matrix_coding ~= 0
0482                     matrix_coding = bitshift(matrix_coding,-16);
0483                     %
0484                     %   Matrices
0485                     %
0486                     if matrix_coding == matrix_coding_dense
0487                         %
0488                         % Find dimensions and return to the beginning of tag data
0489                         %
0490                         
0491 % Check can't be done in real-time --> moved to the end for reshape
0492 %                         pos = ftell(fid);
0493 %                         fseek(fid,tag.size-4,'cof');
0494 %                         ndim = fread(fid,1,'int32');
0495 %                         fseek(fid,-(ndim+1)*4,'cof');
0496 %                         dims = fread(fid,ndim,'int32');
0497 %                         %
0498 %                         % Back to where the data start
0499 %                         %
0500 %                         fseek(fid,pos,'bof');
0501 
0502                         matrix_type = bitand(data_type,tag.type);
0503 
0504                         el_size = tag.size - 3*4; % 3*4 --> case 2D matrix; ToDo calculate el_size through
0505 
0506                         switch matrix_type
0507 %                             case FIFF.FIFFT_INT
0508 %                                 tag.data = zeros(el_size/4, 1);
0509 %                                 for i = 1:el_size/4
0510 %                                     tag.data(i) = p_DataInputStream.readInt;%idata = fread(fid,dims(1)*dims(2),'int32=>int32');
0511 %                                 end
0512 %                             case FIFF.FIFFT_JULIAN
0513 %                                 tag.data = zeros(el_size/4, 1);
0514 %                                 for i = 1:el_size/4
0515 %                                     tag.data(i) = p_DataInputStream.readInt;%idata = fread(fid,dims(1)*dims(2),'int32=>int32');
0516 %                                 end
0517                             case FIFF.FIFFT_FLOAT
0518                                 t_MNERTBufferReader = mne_rt_buffer_reader(p_DataInputStream);
0519                                 tmp = t_MNERTBufferReader.readBuffer(el_size);
0520                                 tag.data = typecast(tmp, 'single');%fdata = fread(fid,dims(1)*dims(2),'single=>double');
0521                                 tag.data = swapbytes(tag.data);
0522                             otherwise
0523                                 error(me,'Cannot handle a matrix of type %d yet',matrix_type)
0524                         end
0525                         
0526                         % ToDo consider 3D case --> do that by using tag->size
0527                         dims = zeros(1, 2);
0528                        
0529                         dims(1) = p_DataInputStream.readInt;
0530                         dims(2) = p_DataInputStream.readInt;
0531                         
0532                         ndim = p_DataInputStream.readInt;
0533                         
0534                         tag.data = reshape(tag.data,dims(1),dims(2))';
0535                     else
0536                         error(me,'Cannot handle other than dense or sparse matrices yet')
0537                     end
0538                 else
0539                     %
0540                     %   All other data types
0541                     %
0542                     switch tag.type
0543                         %
0544                         %   Simple types
0545                         %
0546                         case FIFF.FIFFT_INT
0547 %                             tag.data = zeros(tag.size/4, 1);
0548 %                             for i = 1:tag.size/4
0549 %                                 tag.data(i) = p_DataInputStream.readInt;%fread(fid,tag.size/4,'int32=>int32');
0550 %                             end
0551                             t_MNERTBufferReader = mne_rt_buffer_reader(p_DataInputStream);
0552                             tmp = t_MNERTBufferReader.readBuffer(tag.size);
0553                             tag.data = typecast(tmp, 'int32');%fread(fid,tag.size/4,'int32=>int32');
0554                             tag.data = swapbytes(tag.data);
0555                         case FIFF.FIFFT_FLOAT
0556                             t_MNERTBufferReader = mne_rt_buffer_reader(p_DataInputStream);
0557                             tmp = t_MNERTBufferReader.readBuffer(tag.size);
0558                             tag.data = typecast(tmp, 'single');%fread(fid,tag.size/4,'single=>double');
0559                             tag.data = swapbytes(tag.data);
0560                         case FIFF.FIFFT_STRING
0561                             t_MNERTBufferReader = mne_rt_buffer_reader(p_DataInputStream);
0562                             tag.data = t_MNERTBufferReader.readBuffer(tag.size);%fread(fid,tag.size,'uint8=>char')';
0563                             tag.data = char(tag.data);                            
0564                         case FIFF.FIFFT_ID_STRUCT
0565                             tag.data.version = p_DataInputStream.readInt;%fread(fid,1,'int32=>int32');
0566                             tag.data.machid = zeros(2,1);
0567                             tag.data.machid(1)  = p_DataInputStream.readInt;%fread(fid,2,'int32=>int32');
0568                             tag.data.machid(2)  = p_DataInputStream.readInt;
0569                             tag.data.secs    = p_DataInputStream.readInt;%fread(fid,1,'int32=>int32');
0570                             tag.data.usecs   = p_DataInputStream.readInt;%fread(fid,1,'int32=>int32');
0571                         case FIFF.FIFFT_DIG_POINT_STRUCT
0572                             tag.data.kind    = p_DataInputStream.readInt;%fread(fid,1,'int32=>int32');
0573                             tag.data.ident   = p_DataInputStream.readInt;%fread(fid,1,'int32=>int32');
0574                             tag.data.r = zeros(3,1);
0575                             for i = 1:3
0576                                 tag.data.r(i)    = p_DataInputStream.readFloat;%fread(fid,3,'single=>single');
0577                             end
0578                             tag.data.coord_frame = 0;
0579                         case FIFF.FIFFT_COORD_TRANS_STRUCT
0580                             tag.data.from = p_DataInputStream.readInt;%fread(fid,1,'int32=>int32');
0581                             tag.data.to   = p_DataInputStream.readInt;%fread(fid,1,'int32=>int32');
0582                             rot = zeros(9,1);
0583                             for i = 1:9
0584                                 rot(i) = p_DataInputStream.readFloat;%fread(fid,9,'single=>double');
0585                             end
0586                             rot = reshape(rot,3,3)';
0587                             move = zeros(3,1);
0588                             for i = 1:3
0589                                 move(i) = p_DataInputStream.readFloat;%fread(fid,3,'single=>double');
0590                             end
0591                             tag.data.trans = [ rot move ; [ 0  0 0 1 ]];
0592                             %
0593                             % Skip over the inverse transformation
0594                             % It is easier to just use inverse of trans in Matlab
0595                             %
0596                             for i = 1:12 %fseek(fid,12*4,'cof');
0597                                 p_DataInputStream.readFloat;
0598                             end
0599                         case FIFF.FIFFT_CH_INFO_STRUCT
0600                             tag.data.scanno    = p_DataInputStream.readInt;%fread(fid,1,'int32=>int32');
0601                             tag.data.logno     = p_DataInputStream.readInt;%fread(fid,1,'int32=>int32');
0602                             tag.data.kind      = p_DataInputStream.readInt;%fread(fid,1,'int32=>int32');
0603                             tag.data.range     = p_DataInputStream.readFloat;%fread(fid,1,'single=>double');
0604                             tag.data.cal       = p_DataInputStream.readFloat;%fread(fid,1,'single=>double');
0605                             tag.data.coil_type = p_DataInputStream.readInt;%fread(fid,1,'int32=>int32');
0606                             %
0607                             %   Read the coil coordinate system definition
0608                             %
0609                             tag.data.loc = zeros(12,1);
0610                             for i = 1:12
0611                                 tag.data.loc(i) = p_DataInputStream.readFloat;%fread(fid,12,'single=>double');
0612                             end
0613                             tag.data.coil_trans  = [];
0614                             tag.data.eeg_loc     = [];
0615                             tag.data.coord_frame = FIFF.FIFFV_COORD_UNKNOWN;
0616                             %
0617                             %   Convert loc into a more useful format
0618                             %
0619                             loc = tag.data.loc;
0620                             if tag.data.kind == FIFF.FIFFV_MEG_CH || tag.data.kind == FIFF.FIFFV_REF_MEG_CH
0621                                 tag.data.coil_trans  = [ [ loc(4:6) loc(7:9) loc(10:12) loc(1:3) ] ; [ 0 0 0 1 ] ];
0622                                 tag.data.coord_frame = FIFF.FIFFV_COORD_DEVICE;
0623                             elseif tag.data.kind == FIFF.FIFFV_EEG_CH
0624                                 if norm(loc(4:6)) > 0
0625                                     tag.data.eeg_loc     = [ loc(1:3) loc(4:6) ];
0626                                 else
0627                                     tag.data.eeg_loc = [ loc(1:3) ];
0628                                 end
0629                                 tag.data.coord_frame = FIFF.FIFFV_COORD_HEAD;
0630                             end
0631                             %
0632                             %   Unit and exponent
0633                             %
0634                             tag.data.unit     = p_DataInputStream.readInt;%fread(fid,1,'int32=>int32');
0635                             tag.data.unit_mul = p_DataInputStream.readInt;%fread(fid,1,'int32=>int32');
0636                             %
0637                             %   Handle the channel name
0638                             %
0639                             ch_name = zeros(1, 16, 'uint8');
0640                             for i = 1:16
0641                                 ch_name(i) = p_DataInputStream.readByte;
0642                             end
0643                             ch_name   = char(ch_name);
0644                             %
0645                             % Omit nulls
0646                             %
0647                             len = 16;
0648                             for k = 1:16
0649                                 if ch_name(k) == 0
0650                                     len = k-1;
0651                                     break
0652                                 end
0653                             end
0654                             tag.data.ch_name = ch_name(1:len);
0655                         otherwise
0656                             error(me,'Unimplemented tag data type %d',tag.type);
0657 
0658                     end
0659                 end
0660             end
0661 
0662             % if tag.next ~= FIFF.FIFFV_NEXT_SEQ
0663             %     fseek(fid,tag.next,'bof');
0664             % end
0665 
0666             return;
0667         end
0668             
0669         % =================================================================
0670         %% read_tag_info
0671         function [tag] = read_tag_info(p_DataInputStream, pos)
0672         %
0673         % [tag] = read_tag_info(p_inStream, pos)
0674         %
0675         % Read tag info from a fif stream.
0676         % if pos is not provided, reading starts from the current stream position
0677         %
0678         % p_DataInputStream - the open data stream
0679         % pos               - number of bytes to be skipped
0680         %
0681         
0682         %
0683         %   Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center
0684         %   License : BSD 3-clause
0685         %
0686             import java.net.Socket
0687             import java.io.*
0688             
0689             global FIFF;
0690             if isempty(FIFF)
0691                 FIFF = fiff_define_constants();
0692             end
0693 
0694             me='MNE_RT_DATA_CLIENT:read_tag_info';
0695             
0696             if nargin == 2
0697                 p_DataInputStream.skipBytes(pos);
0698             elseif nargin ~= 1
0699                 error(me,'Incorrect number of arguments');
0700             end
0701             
0702 %             while true
0703 %                 bytes_available = p_inStream.available;
0704 %                 if(bytes_available >= 16)
0705             tag.kind = p_DataInputStream.readInt;
0706             tag.type = p_DataInputStream.readInt;
0707             tag.size = p_DataInputStream.readInt;
0708             tag.next = p_DataInputStream.readInt;
0709 %                     break;
0710 %                 end
0711 %                 % pause 100ms before retrying
0712 %                 pause(0.1);
0713 %             end
0714             
0715             return;
0716         end                    
0717     end
0718 end
0719

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