Skip to content

Instantly share code, notes, and snippets.

@James-E-A
Last active December 12, 2024 01:48
Show Gist options
  • Save James-E-A/c38ad3fa3ad114e1dcea8385960dfd86 to your computer and use it in GitHub Desktop.
Save James-E-A/c38ad3fa3ad114e1dcea8385960dfd86 to your computer and use it in GitHub Desktop.
MATLAB load OceanView TXT files
function [intensities, wavelengths, t] = load_oceanView(filesGlobString)
%load_oceanVew
% - output is INSTANTLY ready to be graphed
% via surf(t, wavelengths, intensities)
% - does NOT fail on data that's non-uniform in time
% e.g. when OceanView malfunctions
files = matlab.buildtool.io.FileCollection.fromPaths( ...
replace(filesGlobString,'/',filesep));
paths = files.paths;
n_records = length(paths);
f = fopen(paths{1}); % if this fails your glob matched ZERO files, surely an accident
tline = fgetl(f); %begin do-while
while ischar(tline)
m = regexp(tline, '^Number of Pixels in Spectrum: (\d+)', 'tokens', 'once');
if ~isempty(m)
n_lines = str2double(m{1});
break;
end
tline = fgetl(f); %end do-while
end
fclose(f);
intensities = zeros(n_lines, n_records);
wavelengths = zeros(n_lines, n_records);
timestamps = NaT(1, n_records);
heuristics = nan(2, n_records);
for i = 1:length(paths)
fname = paths{i};
f = fopen(fname);
tline = fgetl(f); %begin do-while
while ~strcmp(tline, '>>>>>Begin Spectral Data<<<<<')
m = regexp(tline, '^Date: (.*?) ?(\w+)?( ?\d{4})$', 'tokens', 'once');
if ~isempty(m)
timestamp_str = append(m{1}, m{3});
dst = ~isempty(regexp(m{2}, 'DT$', 'once'));
d = datetime(timestamp_str, 'InputFormat','eee MMM dd HH:mm:ss yyyy');
m = regexp(fname, sprintf("_%02d-%02d-%02d-(\\d{0,3})\\.txt$", hour(d), minute(d), second(d)), 'tokens', 'once');
if ~isempty(m)
% timestamp filenames
heuristics(1,i) = str2double(m{1});
else
m = regexp(fname, "_(\d+)\.txt$", 'tokens', 'once');
if ~isempty(m)
% serial filenames
heuristics(2,i) = str2double(m{1});
end
end
timestamps(i) = d - hours(dst); % I will be DAMNED if this code breaks during an experiment at 2am in November
end
tline = fgetl(f); %end do-while
end
for j = 1:n_lines
row = textscan(f, "%f\t%f\n");
wavelengths(j,i) = row{1};
intensities(j,i) = row{2};
end
fclose(f);
end
heuristics_result = ~any(isnan(heuristics), 2)';
if heuristics_result(1)
timestamps = timestamps + milliseconds(heuristics(1,:));
t = timestamps - min(timestamps);
elseif heuristics_result(2)
tmp = fit( ...
heuristics(2,[1 end])', ...
milliseconds(timestamps([1 end]) - min(timestamps))', ...
'poly1');
t = milliseconds(feval(tmp, heuristics(2,:))');
else
t = timestamps - min(timestamps);
end
[t, I] = sort(t, 'ascend'); % OS does not guarantee files will have been loaded in chronological order
intensities = intensities(:,I);
wavelengths = wavelengths(:,I);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment