%load_rbr_csv
clear
rsk = CSV2RSK('RBR-205880.csv');
rsk=RSKderiveseapressure(rsk);
rsk=RSKderivesalinity(rsk);
rsk = RSKderivedepth(rsk);
rsk = RSKtimeseries2profiles(rsk);
j=1;
for i=1:size(rsk.data,2)
    if(strcmp(rsk.data(i).direction,'up'))
        rsk_up_data(j) =rsk.data(i);
        j=j+1;
    end
end
figure(); hold on
for i = 1:size(rsk_up_data,2)
    plot(rsk_up_data(i).tstamp,-rsk_up_data(i).values(:,13),'.');
    % plot(-rsk_up_data(i).values(:,7),-rsk_up_data(i).values(:,3),'.');
end
colorbar()
ylabel('depth(m)')
xlabel('Vrse')
%%
n = numel(rsk_up_data); 
x = NaN(n,1);
z = NaN(n,1500);
for i = 1:n
    x(i) = rsk_up_data(n).tstamp(1);
    z(i) = rsk_up_data(n).values(:,7);
end
%%
x = rsk_up_data.tstamp; % 1xn
z = -rsk_up_data.values(:,7);
% contourf(x,)
%%
rsk = CSV2RSK('RBR-205880.csv');
rsk=RSKderiveseapressure(rsk);
rsk=RSKderivesalinity(rsk);
rsk = RSKderivedepth(rsk);
rsk = RSKtimeseries2profiles(rsk);
RSK2CSV(rsk,'direction','up','outputdir','C:\Users\bwerb\Documents\MATLAB\WetLabk0\WireWalker_04_29_25','profile',130);
RSKplotprofiles(rsk,'direction','up','profile',130);
%% Assemble a CSV file
% Function to download data
rsk = CSV2RSK('RBR-205880.csv');
rsk=RSKderiveseapressure(rsk);
rsk=RSKderivesalinity(rsk);
rsk = RSKderivedepth(rsk);
rsk = RSKtimeseries2profiles(rsk);

k2 = -0.001081;
k0 = -1.48321167902191;

tblcols = []; % Initialize accumulator

for i = 1:size(rsk.data, 2)
    if strcmp(rsk.data(i).direction, 'up')
        temp = rsk.data(i);
        data = temp.values;

        % Expand timestamp
        if isscalar(temp.tstamp)
            tstamp = repmat(temp.tstamp, size(data, 1), 1);
        else
            tstamp = temp.tstamp;
        end

        % Add profile number as a column
        profnum = repmat(temp.profilenumber, size(data, 1), 1);

        % Combine time, profile number, and data
        newprof = [tstamp, profnum, data];

        % Accumulate all profiles
        tblcols = [tblcols; newprof];
    end
end

varNames = ['DateTime','profile' , matlab.lang.makeValidName({rsk.channels.longName})];
WW = array2table(tblcols, 'VariableNames', varNames);
WW.DateTime = datetime(datevec(WW.DateTime),'TimeZone','UTC');
WW.Properties.VariableNames{'pH'} = 'Vrse';
WW.pH = calc_pHext(WW.Vrse,WW.Temperature,WW.Salinity,k0,k2); % Need pcoefs!!