% 
% Copyright (c) 2006-2013 by University of Washington.  All rights reserved. Confidential
%
% KUTI Dec 2015 
% bross: hack of unpack_data.m to create a DivePlot even if the dive had no valid CTD data.


% see get_dive_data()
% assumes results are in variables [loginfo,eng,results] = get_dive_data('GGGYYYY')

%UNUSED but available globals = results.GLOBALS;
% dive_scalars
avg_lat = results.avg_latitude; % average latitude between GPS2 (dive) and GPS (surfacing)

% depth-average currents not available
  u_h2o = 0;
  v_h2o = 0;
  u_av = 0;
  v_av = 0;

iter =  1; % MADE UP ?? dive_scalars(9); % number of TSV iterations
%bross%converged = (str2num(results.hdm_qc) == QC_GOOD); % did TSV converge?
%bross%CTD_qc = str2num(results.CTD_qc);

% dive_results, per data point
dive_start_time = eng.start_ts; % unix epoch seconds (see note below)
sg_epoch_time = results.time;
sg_time = sg_epoch_time - dive_start_time; % elapsed
time = eng.elaps_t; % elapsed time in secs since start of dive
% vmtime = time;
% vmdepth = eng.depth; % meters
%bross%ctd_epoch_time = results.ctd_time;
%bross%ctd_time = ctd_epoch_time - dive_start_time; % elapsed

%bross%ctd_np = length(ctd_time); % total number of CTD points
sg_np = length(time); % total number of SG data points
mp = sg_np;  % BACKCOMPAT
unknown_sg_v = nan(sg_np,1);
%bross%qc_missing_sg_v = QC_MISSING*ones(sg_np,1);

% various indices for different events.
%?? apogee_pump_start_i = directives.pump_start;

sg_press    = results.pressure; % [dbar]
sg_depth_m  = results.depth; % [m] the measured depth
%bross%ctd_depth_m = results.ctd_depth;
%bross%ct_depth_m  = ctd_depth_m; % BACKCOMPAT

% w = m2cm.*ctr1stdiffderiv(-sg_depth_m,sg_time);

%bross%DAC_qc = str2num(results.depth_avg_curr_qc); % trust the depth-average current info?

serial_date_time = unix_to_datenum(dive_start_time);
secs_per_day  = 86400.0;
day = serial_date_time + time/secs_per_day; %bross%% the fraction of days since start of dive
%bross%lat = results.latitude; % latitude
%bross%lon = results.longitude; % longitude
                         
% if GPS2_ok:
dtgps2E = loginfo.GPS_t - loginfo.GPS2_t; % elapsed time between $GPS and $GPS2
midpoint_i = find(time > dtgps2E/2,1,'first'); %bross%
if (isempty(midpoint_i))
  midpoint_i = find(time > time(end)/2,1,'first'); %bross%
end
midpoint_day = day(midpoint_i);
%bross%midpoint_lat = lat(midpoint_i);
%bross%midpoint_lon = lon(midpoint_i);

%bross%if (isfield(results,'start_of_climb_time'))
%bross%  start_of_climb_time = results.start_of_climb_time; % elapsed
%bross%  start_of_climb_i = find(ctd_time >= start_of_climb_time,1,'first');
%bross%else
  fprintf(1,'Unable to determine start of climb index. Old NC file?\n');
  start_of_climb_i = find(sg_depth_m == max(sg_depth_m),1,'first');
  start_of_climb_time = sg_time(start_of_climb_i); % elapsed secs
%bross%end

%bross%spd_model = results.speed; % [m/s] predicted speed through the water
%bross%glideangle_model = results.glide_angle;  % [deg] glide angle
%bross%w_model = results.vert_speed; % [m/s] vertical velocity

%bross%temp_raw = results.temperature_raw; % [deg]
%bross%temp_raw_qc = str2num(results.temperature_raw_qc);
%bross%cond_raw = results.conductivity_raw; % [...] lag-corrected temperature
%bross%cond_raw_qc = str2num(results.conductivity_raw_qc);
%bross%salin_raw = results.salinity_raw; % [PSU] lag-corrected temperature
%bross%salin_raw_qc = str2num(results.salinity_raw_qc); 

%bross%temp = results.temperature; % [deg] lag-corrected temperature
%bross%temp_qc = str2num(results.temperature_qc);
%bross%cond = results.conductivity; % [...] lag-corrected conductivity
%bross%cond_qc = str2num(results.conductivity_qc);
%bross%salin = results.salinity; % [PSU] lag-corrected salinity
%bross%salin_qc = str2num(results.salinity_qc); 
%bross%salinity_error = zeros(ctd_np,1); % TODO replace this if we return this

% createa a fake directives structure
directives = ([]); 
directives.start_of_dive = 1;
directives.start_of_climb = start_of_climb_i;
directives.end_of_climb = sg_np;
%bross%directives.interpolated_salinity = find(salin_qc == QC_INTERPOLATED);
directives.stable_ts = []; % sigh..


compute_dive_climb_indices2
