MNE-CPP  beta 1.0
mne.cpp
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "mne.h"
42 #include <fiff/fiff.h>
43 #include <iostream>
44 
45 
46 //*************************************************************************************************************
47 //=============================================================================================================
48 // QT INCLUDES
49 //=============================================================================================================
50 
51 #include <QFile>
52 #include <QDebug>
53 
54 
55 //*************************************************************************************************************
56 //=============================================================================================================
57 // USED NAMESPACES
58 //=============================================================================================================
59 
60 using namespace MNELIB;
61 
62 
63 //*************************************************************************************************************
64 //=============================================================================================================
65 // DEFINE MEMBER METHODS
66 //=============================================================================================================
67 
68 bool MNE::read_events(QIODevice &p_IODevice, MatrixXi& eventlist)
69 {
70  //
71  // Open file
72  //
73  FiffStream::SPtr t_pFile(new FiffStream(&p_IODevice));
74  FiffDirTree t_Tree;
75  QList<FiffDirEntry> t_Dir;
76 
77  if(!t_pFile->open(t_Tree, t_Dir))
78  {
79 // if(t_pTree)
80 // delete t_pTree;
81 // if(t_pDir)
82 // delete t_pDir;
83 
84  return false;
85  }
86 
87  //
88  // Find the desired block
89  //
90  QList<FiffDirTree> events = t_Tree.dir_tree_find(FIFFB_MNE_EVENTS);
91 
92  if (events.size() == 0)
93  {
94  printf("Could not find event data\n");
95  return false;
96  }
97 
98  qint32 k, nelem;
99  fiff_int_t kind, pos;
100  FiffTag::SPtr t_pTag;
101  quint32* serial_eventlist_uint = NULL;
102  qint32* serial_eventlist_int = NULL;
103 
104  for(k = 0; k < events[0].nent; ++k)
105  {
106  kind = events[0].dir[k].kind;
107  pos = events[0].dir[k].pos;
108  if (kind == FIFF_MNE_EVENT_LIST)
109  {
110  FiffTag::read_tag(t_pFile.data(),t_pTag,pos);
111  if(t_pTag->type == FIFFT_UINT)
112  {
113  serial_eventlist_uint = t_pTag->toUnsignedInt();
114  nelem = t_pTag->size()/4;
115  }
116 
117  if(t_pTag->type == FIFFT_INT)
118  {
119  serial_eventlist_int = t_pTag->toInt();
120  nelem = t_pTag->size()/4;
121  }
122 
123  break;
124  }
125  }
126 
127  if(serial_eventlist_uint == NULL && serial_eventlist_int == NULL)
128  {
129  printf("Could not find any events\n");
130  return false;
131  }
132  else
133  {
134  eventlist.resize(nelem/3,3);
135  if(serial_eventlist_uint != NULL)
136  {
137  for(k = 0; k < nelem/3; ++k)
138  {
139  eventlist(k,0) = serial_eventlist_uint[k*3];
140  eventlist(k,1) = serial_eventlist_uint[k*3+1];
141  eventlist(k,2) = serial_eventlist_uint[k*3+2];
142  }
143  }
144 
145  if(serial_eventlist_int != NULL)
146  {
147  for(k = 0; k < nelem/3; ++k)
148  {
149  eventlist(k,0) = serial_eventlist_int[k*3];
150  eventlist(k,1) = serial_eventlist_int[k*3+1];
151  eventlist(k,2) = serial_eventlist_int[k*3+2];
152  }
153  }
154  }
155 
156  return true;
157 }
158 
159 
160 //*************************************************************************************************************
QList< FiffDirTree > dir_tree_find(fiff_int_t p_kind) const
QSharedPointer< FiffTag > SPtr
Definition: fiff_tag.h:166
static bool read_tag(FiffStream *p_pStream, FiffTag::SPtr &p_pTag, qint64 pos=-1)
Definition: fiff_tag.cpp:204
static bool read_events(QIODevice &p_IODevice, MatrixXi &eventlist)
Definition: mne.cpp:68
QSharedPointer< FiffStream > SPtr
Definition: fiff_stream.h:132
Directory tree structure.
Definition: fiff_dir_tree.h:80
#define FIFF_MNE_EVENT_LIST
FIFF class declaration, which provides static wrapper functions to stay consistent with mne matlab to...
FIFF File I/O routines.
Definition: fiff_stream.h:129