function varargout=parse_cnv_data(cruiseid)
% PARSE_CNV_DATA - parse data columns read in from raw CNV files                     .
%
% CTD Calibration toolbox.
%
% USAGE:
% cnv_load=parse_cnv_data(cnv);
%
% 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.6 $
%	$Date: 2008/02/18 19:40:34 $
%
% CHANGELOG: 
%
%	2007/12/17 - CMDR
%	* read cruise id from the file in the current directory

%%%%%%%%%%% CMDR read cruiseid if we can
if nargin==0, cruiseid=getcruiseid; end;
if isempty(cruiseid), cruiseid=getcruiseid; end;
%%%%%%%%%%%
% Retrieve the necessary preferences for this cruise
group = group_name(cruiseid);

% cruise_dir is the main calibration data directory 
if ispref(group,'cal_data_dir')
	cruise_dir=getpref(group,'cal_data_dir');
else
	error(['The cal_data_dir preference was not set for cruise: ' ...
		 cruiseid '.  Run register_cruise.m']);
end

% Load data from the cruise database
db_file = fullfile(cruise_dir,[cruiseid '_db.mat']);
if exist(db_file)==2
    ld_file=(['load ',db_file,' cnv']);
    eval(ld_file)
else
	error(['The cruise data base file was not found on the path.  '  ...
		db_file]);
end
n=who('cnv');
if isempty(n)==1
    error(['cnv not found. Run load_cnv ']);
    return;
end

aux_stations=vertcat(cnv.station);
disp('****************************************** ')
disp('PARSE CNV DATA')
disp('****************************************** ')
disp('The data found in cnv goes from') 
disp(['the station ',num2str(min(aux_stations)),' to the station ', ...
		num2str(max(aux_stations)),'.'] ) 
disp('****************************************** ')
%
staa=input('Input first station: ');
stab=input('Input last station: ');

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

%%%%%%
%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;
    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;
        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);
    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
    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
    copr(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,cond1_idx);
    cose(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,cond2_idx);
    oxv(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,oxv1_idx);
    oxvs(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,oxv2_idx);
    ox(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,ctd_o2_idx);
    oxs(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,ctd_o2_2_idx);
    sapr(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,sal1_idx);
    sase(first_idx:first_idx-1+length(cnv(i).data(:,pr_idx)),1)= ...
		cnv(i).data(:,sal2_idx);
    iql=2222222+pres*0;

% 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;

% saving the structure
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).pressure=pres;
cnv_load(i).temppri=tepr;
cnv_load(i).tempsec=tese;
cnv_load(i).condpri=copr;
cnv_load(i).condsec=cose;
cnv_load(i).salpri=sapr;
cnv_load(i).salsec=sase;
cnv_load(i).oxvpri=oxv;
cnv_load(i).oxvsec=oxvs;
cnv_load(i).oxmllpri=ox;
cnv_load(i).oxmllsec=oxs;
cnv_load(i).thetapri=thpr;
cnv_load(i).thetasec=thse;
cnv_load(i).sigmapri=sgpr;
cnv_load(i).sigmasec=sgse;
cnv_load(i).quality=iql;
cnv_load(i).pcor=0;
cnv_load(i).tcor=0;
clear pres tepr tese copr cose sapr sase oxv oxvs ox oxs
end
%Save the conductivity summary structure.
save(db_file,'cnv_load','-append');

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

