function varargout=parse_cnv_data_all_beckman(cnv)
% PARSE_CNV_DATA_ALL_BECKMAN - 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
%
%
% VERSION: 
%	$Revision: 1.5 $
%	$Date: 2008/04/30 16:12:06 $
%	$Id: parse_cnv_data_all_beckman.m,v 1.5 2008/04/30 16:12:06 duncombe Exp duncombe $
% 
% CHANGELOG: 
% Modified to use with YSI beckman sensors
%  2008/04/09 CMDR
%	* bring up to date with changes in parse_cnv_data_all.m
%

% PROGRAMMING NOTES
% This module can be incorporated in parse_cnv_data_all.m and then discarded!

aux_stations=vertcat(cnv.station);
disp(['   Parse CNV data: station ',num2str(min(aux_stations))] ) 

%
[staa,sta_min_idx]=min([cnv.station]);
[stab,sta_max_idx]=max([cnv.station]);

%%%%%%
%Initial setup
%   these conversion flags are used to convert from temperature scales 
%	T68 to T90
%   flag_conv1 is for the primary temperature sensor
%   flag_conv2 is for the secondary temperature sensor
%   flag = 0 = do NOT convert (leave values as they appear in cnv files)
%   flag = 1 = DO convert (values were T68 and need to change to T90)
flag_conv1=0;
flag_conv2=0;


%%%%%%%%
% Find the location of the fields in each .cnv files
%   e.g. cnv.data is just the square data matrix where each column is
%   defined by cnv.names.  Below essentially finds the index of each column
% Note performing this in a loop allows each CNV file to have a 
% different column structure  
for i=sta_min_idx:1:sta_max_idx
    sta_idx=i;
%%%%%%%%%% CMDR make sure all variables have a value
	cond1_idx=NaN;
	cond2_idx=NaN;
	ctd_o2_2_idx=NaN;
	ctd_o2_idx=NaN;
	oxv1_idx=NaN;
	oxv2_idx=NaN;
        oxc1_idx=NaN;
        oxct1_idx=NaN;
        oxc2_idx=NaN;
        oxct2_idx=NaN;
	pr_idx=NaN;
	sal1_idx=NaN;
	sal2_idx=NaN;
	temp1_idx=NaN;
	temp2_idx=NaN;
	time_idx=NaN;
%%%%%%%%%%
    for j=1:length(cnv(sta_idx).names);
        if strncmp(cnv(sta_idx).names(j),'timeS',5),
            time_idx=j;  
        elseif strncmp(cnv(sta_idx).names(j),'prDM',4),
            pr_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'sbeox0V',7),
            oxv1_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'sbeox0ML/L',10),
            ctd_o2_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'sbeox1V',7),
            oxv2_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'sbeox1ML/L',10),
            ctd_o2_2_idx=j;
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %fix for ab01
         elseif strncmp(cnv(sta_idx).names(j),'oxC',3),
            oxc1_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'oxTC',4),
            oxct1_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'oxsC',4),
            oxc2_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'oxsTC',5),
            oxct2_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'oxML/L',6),
            ctd_o2_1_idx=j;   
        elseif strncmp(cnv(sta_idx).names(j),'oxsML/L',7),
            ctd_o2_2_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'sbeox0Mm/Kg',11)% JH ab1202
            ctd_MmKg_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'sbeox1Mm/Kg',11)% JH ab1202
            ctd_MmKg2_idx=j;
        %%%%%%%%%%%%%%%%      
        %fix for ab0302
        %elseif strncmp(cnv(sta_idx).names(j),'oxC',3),
        %    oxv2_idx=j;
        %    elseif strncmp(cnv(sta_idx).names(j),'oxML/L',6),
        %    ctd_o2_2_idx=j;
        %%%%%%%%%%%%%%%%    
        elseif strncmp(cnv(sta_idx).names(j),'c0S/m',5),
            cond1_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'c1S/m',5),
            cond2_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'t090C',5),
            temp1_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'t068C',5),
            temp1_idx=j;
            flag_conv1=1;
            disp('Primary temperatures are given in T68.')
            disp(['This program will convert to T90 to make the ' ...
		'necessary calculations'])
        elseif strncmp(cnv(sta_idx).names(j),'t190C',5),
            temp2_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'t168C',5),
            temp2_idx=j;
            flag_conv2=1;
            disp('Secondary temperatures are given in T68.')
            disp(['This program will convert to T90 to make the ' ...
		'necessary calculations'])
        elseif strncmp(cnv(sta_idx).names(j),'sal00',5),
            sal1_idx=j;
        elseif strncmp(cnv(sta_idx).names(j),'sal11',5),
            sal2_idx=j;
         end;
    end;
      
    first_idx=cnv(i).data(1,pr_idx);
% note assumes that your cnv data file is sorted in pressure with 
% 1dbar increment
    pres(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,pr_idx);
    tepr(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,temp1_idx);
    if flag_conv1==1,
        tepr=t68tot90(tepr);   
    end;
%%%%%%%%%%%% CMDR include tests for missing second sensor
	if ~isnan(temp2_idx), 
    tese(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,temp2_idx);
    if flag_conv2==1,
		tese=t68tot90(tese);   
    end;
	end;
    copr(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,cond1_idx);
	if ~isnan(cond2_idx),
    cose(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,cond2_idx);
	end;
    %%%%%%
    oxc(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,oxc1_idx);
	if ~isnan(oxc2_idx),
    oxcs(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,oxc2_idx);
	end;
    oxct(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,oxct1_idx);
	if ~isnan(oxct2_idx),
    oxcts(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,oxct2_idx);         
	end;
	if ~isnan(ctd_o2_1_idx),
    ox(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,ctd_o2_1_idx);
	end;
	if ~isnan(ctd_o2_2_idx),
    oxs(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,ctd_o2_2_idx);
	end;
    
    %if ~isnan(ctd_MmKg_idx),
    %oxkg(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
	%	cnv(i).data(:,ctd_MmKg_idx);
	%end;
	%if ~isnan(ctd_MmKg2_idx),
    %oxkgs(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
	%	cnv(i).data(:,ctd_MmKg2_idx);
	%end;
    
    %%%%
    sapr(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,sal1_idx);
	if ~isnan(sal2_idx),
    sase(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,sal2_idx);
    
	end;
%%%%%%%%%%%
    iql=2222222+pres.*0;

    % Fill missing surface values with NaNs
    pres(1:first_idx-1) = [1:first_idx-1];
    tepr(1:first_idx-1) = NaN;
    tese(1:first_idx-1) = NaN;
    copr(1:first_idx-1) = NaN;
    cose(1:first_idx-1) = NaN;
    oxc(1:first_idx-1) = NaN;
    oxcs(1:first_idx-1) = NaN;
    oxct(1:first_idx-1) = NaN;
    oxcts(1:first_idx-1) = NaN;
    ox(1:first_idx-1) = NaN;
    oxs(1:first_idx-1) = NaN;
    oxkg(1:first_idx-1) = NaN;
    oxkgs(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);
    sgpr=sw_pden(sapr,tepr,pres,0)-1000;
%%%%%%%%%% CMDR fix for missing secondary sensors
	if ~isnan(sal2_idx) && ~isnan(temp2_idx)  ...
	&& ~isempty(sal2_idx) && ~isempty(temp2_idx),
    		sgse=sw_pden(sase,tese,pres,0)-1000;
    		thse=sw_ptmp(sase,tese,pres,0);
	else
		sgse=[];
		thse=[];
	end;
%%%%%%%%

%%%%%%%%%% CMDR rearrange and make some tests. collapse everything into a loop
% saving the structure
cnv_load(i).cruiseid=cnv(i).cruiseid;
cnv_load(i).station=cnv(i).station;
cnv_load(i).latitude=cnv(i).lat;
cnv_load(i).longitude=cnv(i).lon;
cnv_load(i).gtime=cnv(i).gtime;
cnv_load(i).pcor=0;
cnv_load(i).tcor=0;

sensorvar={'pressure', 'temppri', 'condpri', 'salpri', 'oxvpri', ...
	'oxcpri', 'oxctpri', ...
	'oxmllpri', 'thetapri', 'sigmapri','oxuMkgpri', 'quality', 'tempsec', ...
	'condsec', 'salsec', 'oxvsec', ...
	'oxcsec', 'oxctsec',  ...
	'oxmllsec', 'thetasec', 'sigmasec','oxuMkgsec'};
sensordat={'pres',     'tepr',    'copr',    'sapr',   'oxv',    ...
	'oxc', 'oxct',  ...
	'ox',       'thpr',     'sgpr',  'oxkg',  'iql',     'tese',    ...
	'cose',    'sase',   'oxvs',   ...
	'oxcs', 'oxcts',  ...
	'oxs',      'thse',     'sgse',   'oxkgs'};

for k=1:length(sensorvar),
	if exist(sensordat{k})==1,
	if eval(['~any(~isnan(' sensordat{k} '))']), 
		eval([sensordat{k} '=[];']); 
	end;
	eval(['cnv_load(i).(sensorvar{k})=' sensordat{k} ';']);
	end;
end;

%%%%%%%%% 

clear pres tepr tese copr cose sapr sase oxv oxvs ox oxs
end;

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

return;

