function varargout=parse_cnv_data_all(cnv)
% Parse data columns read in from raw CNV files                     .
%
% CTD Calibration toolbox.
%
% USAGE:
% cnv_load=parse_cnv_data(cnv);
%
% This usage is useful for load_cnv_molly - where cnv files are
% automatically translated into the "standard" format.
%
% INPUT: cnv = raw loaded cnv file from load_cnv.
%   
% OUTPUT: cnv_load = structure variable with sensible names for derived
% quantities.
%          
% DESCRIPTION: This function reformats the cnv files from Seabird SEASAVE and SBEProcessing.
%
% AUTHORS:  Molly Baringer March 2006
%
% CHANGELOG: 
%
% * Feb 2012 - rewritten to parse the output from ctd_rd that reads the new
% SBE cnv files.  No longer has to find the data column.  Now just needs to
% find the variable names it is expecting.  If they do not exist must rerun
% datcnv etc to generate them.
%

%
% NOTES:
% ctd_rd.m reads in CNV files and uses the SBE data labels as structure
% elements.  For example during ab1202 cnv files produced included the
% following:
%  cnv
%    name
%    station
%    model
%    tempsn
%    condsn
%    varlabel
%    spans
%    samp_interval
%    latitude
%    longitude
%    gtime
%    mtime
%    tzone
%    scan
%    depSM
%    prDM
%    t090C
%    t190C
%    c0S
%    c1S
%    sbeox0V
%    sbeox1V
%    sbeox0Mm
%    sbeox1Mm
%    upoly0
%    c0mS
%    c1mS
%    sal00
%    sal11
%    timeY
%    sbeox0ML
%    sbeox1ML
%    potemp090C
%    potemp190C
%    sigma
%    avgsvCM
%    nbin
%
% Parse_cnv_load2 function is supposed to write the standard cnv_load form
% that is later used by the CTD calibration toolbox.
% The output should look like:
%
%  cnv_load.
%    station
%    latitude
%    longitude
%    gtime
%    pressure
%    temppri
%    tempsec
%    condpri
%    condsec
%    salpri
%    salsec
%    oxvpri
%    oxvsec
%    oxmllpri
%    oxmllsec
%    thetapri
%    thetasec
%    sigmapri
%    sigmasec
%    quality
%    pcor
%    tcor
%
% The SBE processed files are assumed to be 1db incremetns that *do not*
% include the surface bin, but rather typically start at 3-5 dbar (first
% good surface value).  The cnv_load files must go all the way to the
% surface.  Interpolated values should be marked with a WHP quality flag.
%
% The WHP CTD data quality codes are:
%   1   Not calibrated.
%   2 	Acceptable measurement.
%   3 	Questionable measurement.
%   4 	Bad measurement.
%   5 	Not reported.
%   6 	Interpolated over >2 dbar interval.
%   7 	Despiked.
%   8 	Not assigned for CTD data.
%   9 	Not sampled.
%
%

aux_stations=vertcat(cnv.station);

%
staa=min(aux_stations);
stab=max(aux_stations);

for j=1:1:length(cnv)
    if staa==cnv(j).station
        sta_min_idx=j;
    end;
    if stab==cnv(j).station
        sta_max_idx=j;
    end;
end;

for II = 1: length(cnv)      
    first_idx=ceil(abs(cnv(II).prDM(1)));
% note assumes that your cnv data file is sorted in pressure with 1dbar increment
    num_pts = length(cnv(II).prDM);
    
    jtime(first_idx:first_idx-1+num_pts,1) = cnv(II).timeJ(:);
    stime(first_idx:first_idx-1+num_pts,1) = cnv(II).timeS(:);
    pres(first_idx:first_idx-1+num_pts,1) = cnv(II).prDM(:);
    tepr(first_idx:first_idx-1+num_pts,1) = cnv(II).t090C(:);
    tese(first_idx:first_idx-1+num_pts,1) = cnv(II).t190C(:);
    copr(first_idx:first_idx-1+num_pts,1) = cnv(II).c0S(:);
    cose(first_idx:first_idx-1+num_pts,1) = cnv(II).c1S(:);
%     oxv (first_idx:first_idx-1+num_pts,1) = cnv(II).sbeox0V(:);
%     oxvs(first_idx:first_idx-1+num_pts,1) = cnv(II).sbeox1V(:);
%     ox  (first_idx:first_idx-1+num_pts,1) = cnv(II).sbeox0ML(:);
%     oxs (first_idx:first_idx-1+num_pts,1) = cnv(II).sbeox1ML(:);
    %sapr(first_idx:first_idx-1+num_pts,1) = cnv(II).sal00(:);
    %sase(first_idx:first_idx-1+num_pts,1) = cnv(II).sal11(:);
    iql=2111111+pres*0;

    % Fill missing surface values with NaNs
    pres(1:first_idx-1) = [1:first_idx-1];
    jtime(1:first_idx-1) = NaN;
    stime(1:first_idx-1) = NaN;
    tepr(1:first_idx-1) = NaN;
    tese(1:first_idx-1) = NaN;
    copr(1:first_idx-1) = NaN;
    cose(1:first_idx-1) = NaN;
%     oxv(1:first_idx-1) = NaN;
%     oxvs(1:first_idx-1) = NaN;
%     ox(1:first_idx-1) = NaN;
%     oxs(1:first_idx-1) = NaN;
%     sapr(1:first_idx-1) = NaN;
%     sase(1:first_idx-1) = NaN;
    iql(1:first_idx-1) = 9999999;
    
% compute thetas and sigma-thetas

%     thpr=sw_ptmp(sapr,tepr,pres,0);
%     thse=sw_ptmp(sase,tese,pres,0);
%     sgpr=sw_pden(sapr,tepr,pres,0)-1000;
%     sgse=sw_pden(sase,tese,pres,0)-1000;
    %if conductivity is read in ms
    %copr=copr/10;
    %cose=cose/10;
    
% saving the structure
    cnv_load(II).station=cnv(II).station;
    cnv_load(II).latitude=cnv(II).latitude;
    cnv_load(II).longitude=cnv(II).longitude;
    cnv_load(II).gtime=cnv(II).gtime;
    cnv_load(II).pressure=pres;
    cnv_load(II).timeS=stime;
    cnv_load(II).timeJ=jtime;
    cnv_load(II).temppri=tepr;
    cnv_load(II).tempsec=tese;
    cnv_load(II).condpri=copr;
    cnv_load(II).condsec=cose;
%     cnv_load(II).salpri=sapr;
%     cnv_load(II).salsec=sase;
%     cnv_load(II).oxvpri=oxv;
%     cnv_load(II).oxvsec=oxvs;
%     cnv_load(II).oxmllpri=ox;
%     cnv_load(II).oxmllsec=oxs;
%     cnv_load(II).thetapri=thpr;
%     cnv_load(II).thetasec=thse;
%     cnv_load(II).sigmapri=sgpr;
%     cnv_load(II).sigmasec=sgse;
    cnv_load(II).quality=iql;
    cnv_load(II).pcor=0;
    cnv_load(II).tcor=0;
    clear pres tepr tese copr cose sapr sase oxv oxvs ox oxs jtime stime

% add some new things that would be nice to keep
    %cnv_load(II).nbin(first_idx:first_idx-1+num_pts) = cnv(II).nbin(:);
    cnv_load(II).tempsn = cnv(II).tempsn;
    cnv_load(II).tempsn = cnv(II).tempsn;
    cnv_load(II).varlabel = cnv(II).tempsn;
    cnv_load(II).tzone = cnv(II).tzone;
end

if nargout==0
	assignin('caller','ans',cnv_load);
    return
  else
    varargout{1}=cnv_load;
    return
end
 return   