function data = read_raw(filename)

% EMS wave logger

OPT = struct('frequency', .25);

%%
fid = fopen(filename);
temp = textscan(fid, '%s',...
    'delimiter', ',');
fclose(fid);
D = temp{1};
Sid = find(strcmp(D, '$S'));
Eid = find(strcmp(D, '$E'));

data = repmat(struct(...
    'number', [],...
    'code', [],...
    'starttime', [],...
    'endtime', [],...
    'data', []), 1, length(Sid));

for i = 1:length(Sid)
    data(i).number = str2double(D{Sid(i)+1});
    
    data(i).code = str2double(D{Sid(i)+3});
   
    t0 = cellfun(@str2double, regexp(D{Sid(i)+2}, '[^:]+', 'match'));
    d0 = cellfun(@str2double, regexp(D{Sid(i)+4}, '[^/]+', 'match'));
    d0(end) = d0(end)+2000;
    data(i).starttime = datestr([fliplr(d0) t0]);
    
    data(i).raw = cellfun(@str2double, D(Sid(i)+7:3:Eid(i)));
    
    t1 = cellfun(@str2double, regexp(D{Eid(i)+2}, '[^:]+', 'match'));
    d1 = datevec(datenum([fliplr(d0) t0]) + length(data(i).raw)*OPT.frequency/3600/24);
    data(i).endtime = datestr([d1(1:3) t1]);
end