% Script to transform UpRISEE griddede glider in matlab format 
%   to tabular format (exported as .csv)
%   see BCO-DMO dataset: https://www.bco-dmo.org/dataset/751128 for more details

clc
clear

infile = 'GriddedGlider_TandCHL.mat';
outfile = 'gridded_glider.csv';

%load into obj to get info about what is in the file first
matObj = matfile(infile);

%   Properties:
%       Properties.Source: 'GriddedGlider_TandCHL.mat'
%     Properties.Writable: false                                    
%                   BINID: [1x1606  double]                         
%                     CHL: [80x1606 double]                         
%                     GMT: [80x1606 double]                         
%                     LAT: [1x1606  double]                         
%                     LON: [1x1606  double]                         
%                    TEMP: [80x1606 double]                         
%                    YEAR: [1606x1  double]     

disp(['Loading ' infile])
disp(matObj);
load(infile);

%details = whos(matObj);
%% Prepare data for the table
disp(['Transforming ' infile ' data to table'])

%pick one of the matricies to get the dimensions
[m,n]=size(CHL);

% The data submitter indicated that each row of the matricies correspond to 1m depths. Row 1 is 1m depth, Row 2 is 2m depth etc...to 80m.
DEPTH = repmat(1:80,1,n)';  %use repmat not repelem because we want 1...80 repeated 1606 times not 1 x1606 times then 2x1606 times etc.

%convert matlab datenum to date string in ISO format with time zone
ISO_DateTime_UTC=datestr(GMT(:),'yyyy-mm-ddTHH:MM:SSZ');

%to solve 'NaN-NaN-NaNTNaN:NaNZ% in the datetimes get index and replace with NaN
nan_ind = isnan(GMT(:));
ISO_DateTime_UTC(nan_ind,:) = NaN;

%get rid of padding whitespaces
ISO_DateTime_UTC = deblank(ISO_DateTime_UTC);
    
%round otherwise these columns are written to csv with 13 decimal places 
LAT = round(LAT,5);
LON = round(LON,5);
CHL = round(CHL,3);
TEMP = round(TEMP,3);

%transform variables to fit in a tabular dataset
    % matricies:
    %   * e.g. CHL 80x1606 double 
    %   * expand with (:) so all the matrix columns are
    %   * horizontally 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.
T = table(...
    repelem(BINID',m),...
    GMT(:),...
    ISO_DateTime_UTC,...
    repelem(YEAR,m),... %don't flip this one with ' it's already in a column
    repelem(LAT',m),...
    repelem(LON',m),... 
    DEPTH,...
    round(CHL(:),3),...
    round(TEMP(:),3),...
    'VariableNames',{'BINID','DATENUM_GMT','ISO_DateTime_UTC','YEAR','LAT','LON','DEPTH','CHL','TEMP'}...
);

disp(head(T));

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

system(['head ' outfile ' -n 5']);
disp('...');

% BINID,DATENUM_GMT,ISO_DateTime_UTC,YEAR,LAT,LON,DEPTH,CHL,TEMP
% 30312,735305.908656208,2013-03-12T21:48Z,2013,33.47163,-118.51058,1,3.348,14.607
% 30312,735305.908559185,2013-03-12T21:48Z,2013,33.47163,-118.51058,2,3.958,14.171
% 30312,735305.908816574,2013-03-12T21:48Z,2013,33.47163,-118.51058,3,4.806,14.094
% 30312,735305.908488558,2013-03-12T21:48Z,2013,33.47163,-118.51058,4,3.901,14.029
% ...

disp('Done!')
