-
Notifications
You must be signed in to change notification settings - Fork 0
/
RipplePollDirectoryForNevRecordings.m
207 lines (163 loc) · 5.94 KB
/
RipplePollDirectoryForNevRecordings.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
% $Id$
% =========================================================================
%
% PURPOSE: Offline data analysis tool set
% AUTHOR(S): Andrew Wilder
% CONTACT: support@rppl.com
%
% (c) Copyright 2012 Ripple, LLC. All rights reserved.
%
% This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
%
% =========================================================================
%% Neuroshare Exists
% Check to make sure neuroshare functions are in the MATLAB path
if ~exist('ns_CloseFile.m', 'file')
disp('WARNING => the neuroshare functions were not found in your MATLAB path.');
disp('looking in directory ../');
path(path, '../');
if exist('ns_CloseFile.m', 'file')
disp('This script found the neuroshare functions in ../');
else
disp('In a standard Trellis installation these functions are located in:');
disp(' <trellis_install_dir>/Tools/matlab/neuroshare>');
end
% add the path as it is in SVN
path(path, '..');
if ~exist('ns_CloseFile.m', 'file')
return;
end
end
%% Setup
close all;
clear all;
disp(' ');
disp(' ');
disp('Starting Trellis polling demo.');
disp(' ');
% misc variable initialization
sep = filesep;
% NEV extensions
nevExts = {'nev', 'ns1', 'ns2', 'ns3', 'ns4', 'ns5'};
% get the user's home directory
if ispc
userHomeDir = getenv('USERPROFILE');
else
userHomeDir= getenv('HOME');
end
% if this fails just start at the system root
if ~exist(userHomeDir, 'file')
userHomeDir = '';
end
% select a directory to poll
pollDirPath = uigetdir(userHomeDir,'Select A Directory To Poll');
% see if the user has pressed "Cancel"
if pollDirPath == 0
disp(' ');
disp('Goodbye!');
disp(' ');
break;
end
% get the current contents of the poll dir
items = dir(pollDirPath);
processedDirExists = false;
for i=1:length(items)
if (items(i).isdir) && (strcmp(items(i).name, 'processed'))
processedDirExists = true;
break;
end
end
% if it does not exist create a directory for processed data files
if ~processedDirExists
[success, message, messageID] = mkdir(pollDirPath, 'processed');
if ~success
disp(message);
break;
end
end
processedDirPath = [pollDirPath sep 'processed'];
%% Execute
disp('#');
disp('# Grapevine data processing script');
disp('#');
disp('#');
% data structure to keep trak of file size. need this to figure out whether
% a NEV recording is complete
recordingSizes = containers.Map();
% poll the directory for new NEV recordings
while(1)
% keep a list of recordings and their cumulative size (in bytes)
latestSizes = containers.Map();
% list the directory contents
items = dir(pollDirPath);
for i=1:length(items)
% if the item is a file check if it is from a NEV recording
if ~(items(i).isdir)
% parse the extension from the name
[baseName, remain] = strtok(items(i).name, '.');
[ext, remain2] = strtok(remain, '.');
% check if this is a NEV recording file
if sum(strcmp(ext, nevExts))
% keep track of the file size
recordingSize = items(i).bytes;
% if we've seen other files from this recording compute
% the sum of the sizes
if( latestSizes.isKey(baseName) )
recordingSize = recordingSize + latestSizes(baseName);
end
% keep track of the latest recording size
latestSizes(baseName) = recordingSize;
end
end
end
% loop through the latest recording sizes and see if they match
% previous recording sizes
latestRecordings = latestSizes.keys;
processedCount = 0;
for i=1:latestSizes.Count
% recording name
recordingName = latestRecordings{i};
% latest recording size
latestSize = latestSizes(recordingName);
% check if we've seen this recording before
% and if the recording size is unchanged since the last check
if (recordingSizes.isKey(recordingName)) ...
&& (recordingSizes(recordingName) == latestSize)
% close all current plots
close('all');
% run the neuroshare demo
disp(['Visualizing NEV recording: ' recordingName]);
RippleNeuroshareDemo1(strcat(pollDirPath, sep, recordingName));
processedCount = processedCount + 1;
% remove the recording from the list of recording sizes
recordingSizes.remove(recordingName);
% move all the files associated with this recording to
% the processed dir
fromPath = strcat(pollDirPath, sep, recordingName, '*');
toPath = processedDirPath;
[status, message, messageid] = movefile(fromPath, toPath);
if ~status
disp(['ERROR: could not move files from recording [' ...
recordingName ...
'] to processed dir.']);
end
% else keep track of the latest size of this recording
else
recordingSizes(recordingName) = latestSize;
end
end
% generate status information
dt = fix(clock);
dtStr = [ num2str(dt(4), '%02d') ':' ...
num2str(dt(5), '%02d') ':' ...
num2str(dt(6), '%02d') ];
countStr = num2str(processedCount);
infoStr = [ dtStr ' - No new NEV recordings'];
% display status information
fprintf(1,infoStr);
% wait for a while before checking again for NEV recordings
pause(1);
% clear the status info
for i=1:length(infoStr) fprintf(1,'\b'); end
end