%% hotelfile_Remus_mat
% Generate hotel file from Remus MAT files
%%
% <latex>\index{Scripts!hotelfile\_Remus\_mat}</latex>
%
%%% Syntax
%   User Script:  hotelfile_Remus_mat
%
% * [file_num_range] Scan input files within the specified range.
% * [file_base_name] Base name of the input files. 
% * []
% * [output_file_name] Hotel file created with the specified name. Default
%         value generates a name derrived from the input file names.
%
%%% Description
% Generate a hotel-file from the Matlab mission-files of a Remus AUV that
% were generated by Hydroid-supplied software. The resulting hotel file
% contains the subset of data from the mission-files that are useful for
% converting a RSI data file into physical units and for other purposes. 
%
% The speed information in the hotel-file is used by $\texttt{odas\_p2mat}$
% to convert data into physical units. All vectors that are extracted from
% the mission-file are interpolated to the RSI $\texttt{t\_fast}$ and
% $\texttt{t\_slow}$ time vectors. The mission data vectors are renamed
% using $\texttt{\_fast}$ and $\texttt{\_slow}$ postfixes. A direct
% comparison of mission-file data and RSI recorded data is then possible. 
%
%%% Examples
% The hotel-file can be generated by navigating to the directory containing
% the mission mat-files.  This script is then executed.
%
%    >> hotelfile_Remus_mat
%
% The resulting hotel-file can then be used with odas_p2mat. In this
% example, a RSI raw binary data file ending in $\texttt{16}$ is used.
%
%    >> odas_p2mat('*16', 'vehicle', 'auv', ...
%                  'hotel_file', 'my_hotel_file_name.mat');
%
% If there is a time offset between the RSI data and the data supplied
% within in the mission-file, a time offset can be added to the RSI
% instrument time.
%
%    >> odas_p2mat('*16', 'vehicle', 'auv', ...
%                  'hotel_file', 'my_hotel_file_name.mat', ...
%                  'time_offset', -5);
%
%
% This is a script that you must edit. The parameters
% $\texttt{file\_num\_range}$, $\texttt{file\_base\_name}$ and
% $\texttt{output\_file\_name}$ are near the top of the function. The names
% of the desired data in the mission-file, and the names that they will
% attain in your RSI data mat-file are tabulated just below the parameter
% names. 

% Version History:
%
% 2015-04-19 RGL, Original version
% 2015-04-20 WID, Slightly modified, days_since_1970 bug fixed
% 2015-10-17 WID, Complete rewrite - support for modern hotel files
% 2015-11-18 RGL, Update documentation.

clear result

file_num_range = 1:2;
file_base_name = 'STATE_Run';
output_file_name = sprintf('hotelfile_%s_%d_%d', file_base_name,    ...
                                                 file_num_range(1), ...
                                                 file_num_range(end));
var_data = {
%   HYDROID variable name    ODAS_P2MAT required name
    'mission_time'          'time'
    'days_since_1970'       'days_since_1970'
    'estimated_velocity'    'speed'
    'depth'                 'depth'
    'pitch'                 'pitch'
    'roll'                  'roll'
};

% Construct input data file names
inputfiles = {};
for file_num = file_num_range
    inputfiles{end+1} = sprintf('%s_%d', file_base_name, file_num);
end

for sensor = var_data'
    data = [];
    for file = inputfiles
        try
            hydroid_var = load(file{1}, sensor{1});
            data = [data hydroid_var.(sensor{1})];
        catch; end
    end
    if strcmpi(sensor{2}, 'time')
        time = data(:);
    elseif strcmpi(sensor{2}, 'days_since_1970')
        time(time<0) = time(time<0) + 2^31; % Compensate for bug if present
        time = datenum(1970,1,data(:)+1,0,0,time/1000);
    elseif ~isempty(data) && ~isempty(time)
        result.(sensor{2}).data = data(:);
        result.(sensor{2}).time = time;
    end
end

if ~exist('result', 'var')
    error('Hotel file data not found.');
end

% Output file declared at the begining of the file.
save(output_file_name, '-struct', 'result')

