% Script to transform UpRISEE gridded glider O2 data from matlab format 
%   to tabular format (exported as .csv)
%   see BCO-DMO dataset: https://www.bco-dmo.org/dataset/768685 for more details

clc
clear

infile = 'GliderO2_v2.mat';
outfile = 'GliderO2_v2.csv';

%load into obj to get info about what is in the file first
matObj = matfile(infile);
disp(['Loading ' infile])
disp(matObj);
whos(matObj);

load(infile);
disp(GliderO2)

%% What is in this file?

%     Properties:
%       Properties.Source: 'GliderO2.mat'
%     Properties.Writable: false                                          
%                GliderO2: [1x1 struct]      

% disp(GliderO2)
%     Oxygen13: [91×4891 double]
%      Depth13: [91×4891 double]
%        lat13: [91×4891 double]
%       Time13: [91×4891 double]
%        Lon13: [91×4891 double]
%      Depth14: [98×7411 double]
%        lat14: [98×7411 double]
%        Lon14: [98×7411 double]
%     Oxygen14: [98×7411 double]
%       Time14: [98×7411 double]

% Two sets of matricies in this .mat file, one for year 2013 (variable names
% end in 13 and 2014 (variable namnes end in 14)


%% Prepare data for the table
disp(['Transforming ' infile ' data to table'])

% supply struct and year
T13 = getTable(GliderO2,'13');
T14 = getTable(GliderO2,'14');

T = [T13;T14];

writetable(T,outfile,'Encoding','UTF-8');
disp(['Transformed ' infile ' to a tabular dataset exported as ' outfile]);
disp(' ');

disp('...');


disp('Done!')

%%
function T = getTable(strct,year)
        
    dnum =strct.(['Time' year]);
    depth = strct.(['Depth' year]);
    lat = strct.(['Lat' year]);
    lon = strct.(['Lon' year]);
    o2 = strct.(['Oxygen' year]);
    
    
    %pick one of the matricies to get the dimensions
    [m,n]=size(o2);
    
    %BINID was present in other .mat files from this project so adding it.
    BINID = [1:n];
    
    %convert matlab datenum to date string in ISO format with time zone
    ISO_DateTime_UTC=datestr(dnum(:),'yyyy-mm-ddTHH:MM:SSZ');
    
    
    %to solve 'NaN-NaN-NaNTNaN:NaNZ% in the datetimes get index and replace with NaN
    nan_ind = isnan(dnum(:));
    ISO_DateTime_UTC(nan_ind,:) = NaN;
    
    %get rid of padding whitespaces
    ISO_DateTime_UTC = deblank(ISO_DateTime_UTC);
        
    %transform variables to fit in a tabular dataset
        % matricies:
        %   * e.g. o2 80x1606 double 
        %   * expand with (:) so all the matrix columns are
        %   * vertically concatenated into one column.
        % single vectors: 
        %   * e.g. lat 1x1606 double
        %   * repeat orig vector m times with repelem, vertically concat into final table column.  m being the number of columns of the matrix variables.
%% with rounding
       %have to round or else table -> csv has a ~13 decimal places.        
    T = table(...
        repelem(BINID',m),...
        dnum(:),...
        ISO_DateTime_UTC,...
        ISO_DateTime_UTC(:,1:4),...%year
        round(lat(:),5),...
        round(lon(:),5),... 
        round(depth(:),0),...
        round(o2(:),3),...
        'VariableNames',{'BINID','DATENUM_GMT','ISO_DateTime_UTC','YEAR','LAT','LON','DEPTH','OXYGEN'}...
    );

%% with no rounding

       %have to round or else table -> csv has a ~13 decimal places.        
%     T = table(...
%         repelem(BINID',m),...
%         dnum(:),...
%         ISO_DateTime_UTC,...
%         ISO_DateTime_UTC(:,1:4),...%year
%         lat(:),...
%         lon(:),... 
%         depth(:),...
%         o2(:),...
%         'VariableNames',{'BINID','DATENUM_GMT','ISO_DateTime_UTC','YEAR','LAT','LON','DEPTH','OXYGEN'}...
%     );
%%
    disp(['Data for year ' year])
    disp(head(T));

end

