DESCRIPTION. What does the function do? INPUT path An optional path. filename An optional filename. OUTPUT wave_data A MATLAB structure with the following fields: path: The path to the file filename: The filename compass: An m x 4 matrix, the status data (3) and data_index time: An m x 4 matrix, with start_time, start_index, end_time and end_index. raw_data: An n x 3 matrix. The raw data burst_data: A structure with m fields containing the bursts (subsets) of the data in raw_data NOTES This code is based on several Read_logger*.m files floating about. Some of these codes also calibrate the data using known scale (nee slope) and offset (intercept) for each sensor. This function does not do that, it is up to the user to apply the correct calibration. NB: The general output format is *not* compatible with this old code. Adjust your approach accordingly. AUTHORS Bjoern Elsaesser Daniel Pritchard LICENCE Code distributed as part of the MRG toolbox from the Marine Research Group at Queens University Belfast (QUB) School of Planning Architecture and Civil Engineering (SPACE). Distributed under a creative commons CC BY-SA licence, retaining full copyright of the original authors. http://creativecommons.org/licenses/by-sa/3.0/ http://www.qub.ac.uk/space/ http://www.qub.ac.uk/research-centres/eerc/ DEVELOPMENT v 1.0 2014-02-04 First version. DP Brings together several codes from BEs work folder namely: Read_logger3c_data; Read_logger_card.m and; Read_logger_cardv2.m 'Compass' information is now read inline, rather than being read into 'data' and then later extracted... % Function Begin! Is path supplied
0001 function wave_data = mrg_read_3chan_logger(path,filename) 0002 % DESCRIPTION. What does the function do? 0003 % 0004 % INPUT 0005 % path An optional path. 0006 % filename An optional filename. 0007 % 0008 % OUTPUT 0009 % wave_data A MATLAB structure with the following fields: 0010 % path: The path to the file 0011 % filename: The filename 0012 % compass: An m x 4 matrix, the status data (3) and data_index 0013 % time: An m x 4 matrix, with start_time, start_index, 0014 % end_time and end_index. 0015 % raw_data: An n x 3 matrix. The raw data 0016 % burst_data: A structure with m fields containing the bursts 0017 % (subsets) of the data in raw_data 0018 % 0019 % NOTES 0020 % This code is based on several Read_logger*.m files floating about. Some 0021 % of these codes also calibrate the data using known scale (nee slope) 0022 % and offset (intercept) for each sensor. This function does not do that, 0023 % it is up to the user to apply the correct calibration. 0024 % NB: The general output format is *not* compatible with this old code. 0025 % Adjust your approach accordingly. 0026 % 0027 % AUTHORS 0028 % Bjoern Elsaesser 0029 % Daniel Pritchard 0030 % 0031 % LICENCE 0032 % Code distributed as part of the MRG toolbox from the Marine Research 0033 % Group at Queens University Belfast (QUB) School of Planning 0034 % Architecture and Civil Engineering (SPACE). Distributed under a 0035 % creative commons CC BY-SA licence, retaining full copyright of the 0036 % original authors. 0037 % 0038 % http://creativecommons.org/licenses/by-sa/3.0/ 0039 % http://www.qub.ac.uk/space/ 0040 % http://www.qub.ac.uk/research-centres/eerc/ 0041 % 0042 % DEVELOPMENT 0043 % v 1.0 2014-02-04 0044 % First version. DP 0045 % Brings together several codes from BEs work folder namely: 0046 % Read_logger3c_data; Read_logger_card.m and; 0047 % Read_logger_cardv2.m 0048 % 'Compass' information is now read inline, rather than being 0049 % read into 'data' and then later extracted... 0050 % 0051 %% Function Begin! 0052 % Is path supplied 0053 old_path = cd(); 0054 if ~exist('filename', 'var') 0055 [filename,path] = uigetfile('*.dat','Select a *.dat file from the 3-channel logger'); 0056 cd(path); 0057 if isempty(filename) 0058 return 0059 end 0060 end 0061 % Open file handle 0062 fid = fopen([path filename], 'r'); 0063 0064 %% Setup... 0065 n = 1; % An odd-even ticker 0066 m = 1; % The row index for time and compass 0067 j = 1; % The data row index 0068 0069 % Preallocate some memory: 0070 data = zeros(200000,3); 0071 time = zeros(200,4); 0072 compass = zeros(200,4); 0073 0074 tline = fgetl(fid); 0075 while tline~=-1 0076 % If it degins with 'H' is is the status line, with date/time info... 0077 if tline(1) == 'H' 0078 % If 'n' is odd (1,3,5,...) then it is a 'start' time so it gets 0079 % written to column 1 and the index gets writen to column 2. 0080 % Furthermore, the next line is 'compass' / status information... 0081 % We read this now with a new call to fgetl, rather than have it 0082 % get mixed up the with other data and have to extract it later 0083 0084 % If 'n' is even then it is a 'end' time. This gets written to 0085 % column 3 and the index to column 4. Also we increment 'm' so that 0086 % the next encounter gets writen to the correct row. 0087 if(bitget(abs(n),1)) % Is odd 0088 time(m,1) = datenum(tline(6:end),'dd:mm:HH:MM:SS'); 0089 time(m,2) = j; % j is an index into the 'data' object 0090 tline = fgetl(fid); 0091 compass(m,1:3) = str2num(tline); 0092 compass(m,4) = j; 0093 else % Is even 0094 time(m,3) = datenum(tline(6:end),'dd:mm:HH:MM:SS'); 0095 time(m,4) = j-1; % j is an index into the 'data' object 0096 m = m + 1; % Increment 'm' 0097 end 0098 % Increment n (this needs to happen everytime to keep the odd/even 0099 % marker ticking over 0100 n = n + 1; 0101 else 0102 data(j,1:3) = str2num(tline); 0103 j = j + 1; 0104 end 0105 % If preallocated space is getting low, top it up! 0106 % NB: This is probably not needed as MATLAB dynmically exapnd matrixes 0107 % that are indexed beyond their limits (e.g. x=[];x(3,2)=2;) but it 0108 % might be more efficient 0109 if j > length(data) 0110 data(j:j+199999,1:3) = zeros(200000,3); % Add more data if needed... 0111 end 0112 if m > length(time) % NB: 'time' and 'compass' should be the same length! 0113 time(n:n+199,1:4) = zeros(200,4); 0114 compass(m:m+199,1:4) = zeros(200,4); 0115 end 0116 % Read a new line and repeat! 0117 tline = fgetl(fid); 0118 end 0119 fclose(fid); 0120 0121 % Clean up unused pre-allocated space! 0122 data = data(1:j-1,:); 0123 time = time(1:m-1,:); 0124 compass = compass(1:m-1,:); 0125 0126 disp('All data read from file...') 0127 0128 %% Setup return object 0129 wave_data.path = path; 0130 wave_data.filename = filename; 0131 wave_data.compass = compass; 0132 wave_data.time = time; 0133 wave_data.raw_data = data; 0134 0135 0136 %% Split into separate files and write out 0137 for n = 1:length(time); 0138 M = data(time(n,2):time(n,4),1:3); % Use the start/stop time indexes 0139 %name = [filename(1:end-4),'_chunk_',num2str(n),'.dat']; 0140 %dlmwrite(name, M, 'precision', '%.6f','newline', 'pc'); 0141 wave_data.burst_data.(['burst_',num2str(n)]) = M; 0142 end; 0143 disp('Individual bursts extracted...') 0144 0145 %% Finally, be a good lad: Go home... 0146 cd(old_path) 0147 disp('Done!') 0148 end