function completed = NBP1201_climate_TS_and_MVP(fname)
%
% =========================================================================
% NBP1201_climate_TS_and_MVP  Version 1.0 01-Jan-2012 (Happy New Year!!)
%
% Usage:
%   NBP1201_climate_TS_and_MVP(filename)
%
% Description:
%   This MATLAB function opens an MVP .mat file and the Ross Sea TS
%   climatology.  The climatology is plotted in a light grey background.
%   The recent MVP TS data are plotted in color overlaid on the
%   climatology.  As the MVP data "ages" it's color is converted to black.
%
% Input:
%   An MVP .mat data filename
%   Note: Set MVP_counter as a global variable in the Matlab workspace prior
%   to executing this function for the first time.
%
% Output:
%   - MATLAB .mat file with MVP TS data set
%
% Author:
%   Blair Greenan
%   Bedford Institute of Oceanography
%   01-Jan-2012
%   Matlab 7.13
%   blair.greenan@dfo-mpo.gc.ca
%   Based on code provided by Olga Kosnyrev (WHOI)
%
% Revision History:
%
% =========================================================================
%
%
% MVP temperature and salinity matrices
global MVP_tem_downcast MVP_sal_downcast MVP_tem_upcast MVP_sal_upcast
%
% Change to the working directory
cd 'S:\mvp\nbp1201\rois\vprlog\'
%
% Open the MVP .mat file to load the TS data from an individual profile
fid = fopen(fname);
%
% Open the MVP_TS .mat file to load the TS data from all profiles to date
fid2 = fopen('MVP_TS_downcast.mat');
fid3 = fopen('MVP_TS_upcast.mat');
%
% Check to see if the TS climatology plot already exists.  If so, clear the
% figure for this update
hfig = findobj('Type','Figure');
if (any(hfig == 99))
    clf(99)
end
% If Figure 99 exists, then make it the active figure window.  If Figure 99
% does not exist, then create it.
figure(99)
%
% Set up the color scheme for the TS plot. The Orsi climatology will be set
% as a light grey background
climate_color=[0.8 0.8 0.8];
mvp_color_upcast=[1 0 0];
mvp_color_downcast=[0 1 0];

%#######################################################
%###############   CLIMATE DATA    #####################
%#######################################################
load .\rossclimII_update;
%> whos
% Name              Size                   Bytes  Class     Attributes
%
%  ans               1x11                      22  char
%  batg_array      207x436                 722016  double
%  ggam            207x436x40            28880640  double
%  goxy            207x436x40            28880640  double
%  gprs            207x436x40            28880640  double
%  gptm            207x436x40            28880640  double
%  gsal            207x436x40            28880640  double
%  gtem            207x436x40            28880640  double
%  latg_array      207x436                 722016  double
%  levels           40x1                      320  double
%  long_array      207x436                 722016  double
%
S_all=gsal(:,:,:);
T_all=gtem(:,:,:);
%
%#######################################################
%###############      MVP DATA     #####################
%#######################################################
% load the current MVP profile
load(fname);

%%%%%%%%%% DOWNCAST %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% add the current MVP temperature and salinity data to the MVP_TS matrices
% and save the .mat file
len_downcast = length(Temperature_downcast);
% determine the size of the MVP_TS matrices
size_TS_downcast = size(MVP_tem_downcast);
% special case for the first profile
if (size_TS_downcast(2)==1)
    % if there is data in the first column then add a second column
    if (any(~isnan(MVP_tem_downcast(:,1))))
        MVP_tem_downcast(1:len_downcast,(size_TS_downcast(2)+1)) = Temperature_downcast;
        MVP_sal_downcast(1:len_downcast,(size_TS_downcast(2)+1)) = Salinity_downcast;
        % fill the remainder of the column with NaN
        if (len_downcast<1000)
            MVP_tem_downcast(len_downcast+1:end,(size_TS_downcast(2)+1))=NaN;
            MVP_sal_downcast(len_downcast+1:end,(size_TS_downcast(2)+1))=NaN;
        end
    else
        MVP_tem_downcast(1:len_downcast,size_TS_downcast(2)) = Temperature_downcast;
        MVP_sal_downcast(1:len_downcast,size_TS_downcast(2)) = Salinity_downcast;
        % fill the remainder of the column with NaN
        if (len_downcast<1000)
            MVP_tem_downcast(len_downcast+1:end,size_TS_downcast(2))=NaN;
            MVP_sal_downcast(len_downcast+1:end,size_TS_downcast(2))=NaN;
        end
    end
else
    MVP_tem_downcast(1:len_downcast,(size_TS_downcast(2)+1)) = Temperature_downcast;
    MVP_sal_downcast(1:len_downcast,(size_TS_downcast(2)+1)) = Salinity_downcast;
    % fill the remainder of the column with NaN
    if (len_downcast<1000)
        MVP_tem_downcast(len_downcast+1:end,(size_TS_downcast(2)+1))=NaN;
        MVP_sal_downcast(len_downcast+1:end,(size_TS_downcast(2)+1))=NaN;
    end

end
% update the MVP_TS_downcast.mat file
save MVP_TS_downcast.mat MVP_tem_downcast MVP_sal_downcast

%%%%%%%%%% UPCAST %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% add the current MVP temperature and salinity data to the MVP_TS matrices
% and save the .mat file
len_upcast = length(Temperature_upcast);
% determine the size of the MVP_TS matrices
size_TS_upcast = size(MVP_tem_upcast);
% special case for the first profile
if (size_TS_upcast(2)==1)
    % if there is data in the first column then add a second column
    if (any(~isnan(MVP_tem_upcast(:,1))))
        MVP_tem_upcast(1:len_upcast,(size_TS_upcast(2)+1)) = Temperature_upcast;
        MVP_sal_upcast(1:len_upcast,(size_TS_upcast(2)+1)) = Salinity_upcast;
        % fill the remainder of the column with NaN
        if (len_upcast<1000)
            MVP_tem_upcast(len_upcast+1:end,(size_TS_upcast(2)+1))=NaN;
            MVP_sal_upcast(len_upcast+1:end,(size_TS_upcast(2)+1))=NaN;
        end
    else
        MVP_tem_upcast(1:len_upcast,size_TS_upcast(2)) = Temperature_upcast;
        MVP_sal_upcast(1:len_upcast,size_TS_upcast(2)) = Salinity_upcast;
        % fill the remainder of the column with NaN
        if (len_upcast<1000)
            MVP_tem_upcast(len_upcast+1:end,size_TS_upcast(2))=NaN;
            MVP_sal_upcast(len_upcast+1:end,size_TS_upcast(2))=NaN;
        end
    end
else
    MVP_tem_upcast(1:len_upcast,(size_TS_upcast(2)+1)) = Temperature_upcast;
    MVP_sal_upcast(1:len_upcast,(size_TS_upcast(2)+1)) = Salinity_upcast;
    % fill the remainder of the column with NaN
    if (len_upcast<1000)
        MVP_tem_upcast(len_upcast+1:end,(size_TS_upcast(2)+1))=NaN;
        MVP_sal_upcast(len_upcast+1:end,(size_TS_upcast(2)+1))=NaN;
    end

end
% update the MVP_TS_upcast.mat file
save MVP_TS_upcast.mat MVP_tem_upcast MVP_sal_upcast

%****************************************************
%******************  PLOT the data    ***************
%****************************************************
% (1) Climate.
B=batg_array(:);
% Use the points which bottom depth is less than 2000m.
r_shallow=find(B(:)<2000);
hold on;
for ii=1:40
    T=T_all(:,:,ii);
    S=S_all(:,:,ii);
    S=S(:);
    T=T(:);
    S=S(r_shallow);
    T=T(r_shallow);
    plot(S,T,'.','color',climate_color,'markersize',1);
end
% (2) MVP.
% Set a counter so that the color fades from red to black for the last five
% MVP profiles and then remains black for all older MVP data to overlay the
% gray Orsi TS climatology for the Ross Sea.  The size of the five most recent
% MVP profiles also decreases the markersize from 11 to 1 in addition to changing the color
%
% special case for first profile

%{ 
Stop plotting downcast until salinity issue is resolved
if (size_TS_downcast(2) == 1)
    plot(MVP_sal_downcast(:,1),MVP_tem_downcast(:,1),'.','color',mvp_color_downcast,'markersize',21);
else
    kk = 0;
    for jj = size_TS_downcast(2)+1:-1:1
        plot(MVP_sal_downcast(:,jj),MVP_tem_downcast(:,jj),'.','color',mvp_color_downcast,'markersize',21-(kk*4));
        if (kk < 5)
            %mvp_color_downcast(2) = mvp_color_downcast(2) - 0.2;
            kk = kk + 1;
        end
    end
end
%} 

% special case for first profile
if (size_TS_upcast(2) == 1)
    plot(MVP_sal_upcast(:,1),MVP_tem_upcast(:,1),'.','color',mvp_color_upcast,'markersize',21);
else
    kk = 0;
    for jj = size_TS_upcast(2)+1:-1:1
        plot(MVP_sal_upcast(:,jj),MVP_tem_upcast(:,jj),'.','color',mvp_color_upcast,'markersize',21-(kk*4));
        if (kk < 5)
            %mvp_color_upcast(1) = mvp_color_upcast(1) - 0.2;
            kk = kk + 1;
        end
    end
end

% (3) Temp 1.937 line
xlim = get(gca,'xlim');
plot([xlim(1) xlim(2)],[-1.937 -1.937],'k-','color',[0.4 0.4 0.4],'linewidth',0.5);
hold off;
axis square;
set(gca,'xlim', [33 35], 'ylim', [-2.5 2.5])
xlabel('Salinity');
ylabel('Temperature (\circC)');
title('NBP1201 MVP and Climate TS diagram');
print -djpeg -r600 S:\MVP\nbp1201\rois\vprlog\TS_climate.jpg

completed = 1;

