function plot_historical_data(cruiseid)
% plot_historical_data: Plot historical Abico data
%
% CTD Calibration toolbox.
%
% INPUT: cruiseid 
%
% USAGE: 
% plot_historical_data: Loads abaco_historical_db and uses the longitude  
%                     and latitude to plot the relevant data based on the 
%                     search radius (km) designated
% 
%
% AUTHORS:James Hooper
%         UM/CIMAS
%         Tue April 31 EDT 2012

close all;
% Load historical data.
% db variables: lat lon oxygen prs ptmp sal stt tmp
load /Users/jhooper/Documents/Work_data/CTDCAL/Jay_toolbox/abaco_historical_db;


%%%%%%%%%%%%%%%%%%%%%%%% Plot all cruise data vs all historical %%%%%%%%%%% 
all_data=input(['Would you like to plot all Calibrated data vs. historical?: \n'...
		'   yes = 1\n' ...
		'    no = 0\n' ...
		'Select: ']);
    % Retrieve the necessary preferences for this cruise
    group = group_name(cruiseid);

    % cruise_dir is the main calibration data directory 
    % it should contain the subdirectories proc_data, raw_data etc.
    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]);
        eval(ld_file)
    else
        error(['The cruise data base file was not found on the path.  ' db_file '.  Check path']);
    end
    
    if all_data,
        figure;
        font(14)
        plot(sal,ptmp,'color',[.5 .5 .5])
        hold on;
        for i= 1:length(vertcat(cnv_cal.station))
            plot(cnv_cal(i).salt,cnv_cal(i).pottemp,'k')
            hold on;
        end;
        ylabel('Potential Temperature (\circC) ','fonts',16);
        xlabel('Salinity (psu) ','fonts',16);
        ylim(0,30);
        title('\theta-S vs. Historical data','fonts',16)
        
        print -depsc ts_historical
        
        figure;
        font(14)
        plot(sal,ptmp,'color',[.5 .5 .5])
        hold on;
        for i= 1:length(vertcat(cnv_cal.station))
            plot(cnv_cal(i).salt,cnv_cal(i).pottemp,'k')
            hold on;
        end;
        ylabel('Potential Temperature (\circC) ','fonts',16);
        xlabel('Salinity (psu) ','fonts',16);
        ylim(0,9);
        title('\theta-S vs. Historical data (deep water)','fonts',16)
        
        print -depsc ts_historical_deep
        %%%%%%%%%%%%%%%%%% oxygen %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        figure;
        font(14)
        plot(oxygen,ptmp,'color',[.5 .5 .5])
        hold on;
        for i= 1:length(vertcat(cnv_cal.station))
            plot(cnv_cal(i).oxyumol,cnv_cal(i).pottemp,'k')
            hold on;
        end;
        ylabel('Potential Temperature (\circC) ','fonts',16);
        xlabel('Oxygen (\muMol/kg) ','fonts',16);
        ylim(0,30);
        xlim(100,300)
        title('\theta-O vs. Historical data','fonts',16)
        
        print -depsc to_historical
        
        figure;
        font(14)
        plot(oxygen,ptmp,'color',[.5 .5 .5])
        hold on;
        for i= 1:length(vertcat(cnv_cal.station))
            plot(cnv_cal(i).oxyumol,cnv_cal(i).pottemp,'k')
            hold on;
        end;
        ylabel('Potential Temperature (\circC) ','fonts',16);
        xlabel('Oxygen (\muMol/kg) ','fonts',16);
        ylim(0,9);
        xlim(100,300)
        title('\theta-O vs. Historical data (deep water)','fonts',16)
        
        print -depsc to_historical_deep
        
        return
    end;
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    


radius = input(['What is your search radius (km)?:']);

prop_var = input(['What variable would you like to compare?: \n'...
		'   Salinity = 0\n' ...
		'    Oxygen = 1\n' ...
		'Select datatype: ']);    

sta = input(['What station would you like to compare?:']);

% Retrieve the necessary preferences for this cruise
group = group_name(cruiseid);

% cruise_dir is the main calibration data directory 
% it should contain the subdirectories proc_data, raw_data etc.
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]);
    eval(ld_file)
else
	error(['The cruise data base file was not found on the path.  ' db_file '.  Check path']);
end

type_plot = input(['Which data would you like to use?: \n' ...
		'    raw data = 0\n' ...
		'    delooped data = 1\n' ...
		'    calibrated data = 2\n' ...
		'Select datatype: ']);    

if type_plot == 1
    n=who('cnv_dlp');
    if isempty(n)==1
        fprintf (1,[['WARNING COMPARE_CTD_SENSOR: Selected delooped cnv files.  \n'] ...
        ['    Variable cnv_dlp not found. \n'] ...
        ['    Proceeding with raw CNV files.    \n'] ...
        ['    If delooped data desired hit CTRL-C and run despike.m and deloop.m before proceeding.\n'] ...
        ['    Hit <RETURN> to continue with raw CNV files.\n']]);
        pause
        cnv_compare = cnv_load;
    else
        cnv_compare = cnv_dlp;
    end
elseif type_plot == 2
    n=who('cnv_cal');
    if isempty(n)==1
        fprintf (1,[['WARNING COMPARE_CTD_SENSOR: Selected calibrated files.  \n'] ...
        ['    Variable cnv_cal not found. \n'] ...
        ['    Proceeding with raw CNV files.    \n'] ...
        ['    If calibrated data desired hit CTRL-C and run make_.m before proceeding.\n'] ...
        ['    Hit <RETURN> to continue with raw CNV files.\n']]);
        pause
        cnv_compare = cnv_load;
    else
        cnv_compare = cnv_cal;
    end
else
    cnv_compare = cnv_load;
end

aux_stations=vertcat(cnv_compare.station);

%for jj=1:59,
i=find(aux_stations == sta);
%end;
%calculate the radius of the earth at the mean latitude
r=6378e03*cos(deg2rad(mean(vertcat(cnv_compare.latitude))));
rs=num2str(r);
rn=str2num(rs(1:4));
%convert radius (km) to degrees
deg=km2deg(radius,rn)
%Calculate longitude search range
clon=nanmean(cnv_compare(i).longitude)
    clonmn=clon-deg;
    clonmx=clon+deg;
%Calculate latitude search range
clat=cnv_compare(i).latitude
    clatmn=clat-deg;
    clatmx=clat+deg;

%finds the data within the search criteria
jj=find(lon>=clonmn & lon<=clonmx & lat>=clatmn & lat<=clatmx);

if prop_var == 0,
    %plot climatology data
    hf = plot(sal(:,jj),ptmp(:,jj),'color',0.5*[1 1 1]);
    hold on;
    if (type_plot == 0 | type_plot == 1)
        %plot cruise pre cal data
        plot(cnv_compare(i).salpri,cnv_compare(i).thetapri,'b')
        hold on;
        plot(cnv_compare(i).salsec,cnv_compare(i).thetasec,'--b')
    else
        plot(cnv_compare(i).salt,cnv_compare(i).pottemp,'b')
        hold on;    
    end;
        xlabel('Salinity (PSU)','fonts',14)
        ylabel('Potential Temperature','fonts',14);
        title(['Station ', num2str(cnv_compare(i).station) ...
        ': T-S vs. Historical data (', num2str(radius), ' km radius)'],'fonts',14);
        font(14);
else
    
    hf = plot(oxygen(:,jj),prs(:,jj),'color',0.5*[1 1 1]);
    hold on;
    revyaxis

    if (type_plot == 0 | type_plot == 1)
        %plot cruise pre cal data
        if ~isfield(cnv_compare,'oxmmpri') & ~isfield(cnv_compare, 'oxmmsec'),
            cnv_compare(i).oxmmpri = ox_units(cnv_compare(i).oxmllpri, ...
                cnv_compare(i).salpri,cnv_compare(i).thetapri,'mll2mmk');
            cnv_compare(i).oxmmsec = ox_units(cnv_compare(i).oxmllsec, ...
                cnv_compare(i).salsec,cnv_compare(i).thetasec,'mll2mmk');
            
            plot(cnv_compare(i).oxmmpri,cnv_compare(i).pressure,'b')
            hold on;
            plot(cnv_compare(i).oxmmsec,cnv_compare(i).pressure,'--b')
        else
        plot(cnv_compare(i).oxmmpri,cnv_compare(i).pressure,'b')
        hold on;
        plot(cnv_compare(i).oxmmsec,cnv_compare(i).pressure,'--b')
        end;
    else
        plot(cnv_compare(i).oxygen,cnv_compare(i).pressure,'b')
        hold on;    
    end;
        xlabel('Oxygen (uM/kg)','fonts',14)
        ylabel('Pressure','fonts',14);
        title(['Station ', num2str(cnv_compare(i).station) ...
        ': Oxygen vs. Historical data (', num2str(radius), ' km radius)'],'fonts',14);
        font(14);   
end;

return
