%%%%%%09/04/2016 AndreaD : modified for compatibility wit MVP tools and LATEXtools
%from original script by Alice Pietri
% needs ixingOceanographicToolbox_EM-APEX

%clear;close all;
LATEXtools_param
LATEXtools_cruise
MVP_cruise_params;

transectname=[Transect_Names{transchoice}];%'Transect_03_qc_2'; 

[MVP_lon,MVP_lat,MVP_dist,MVP_pres,MVP_temp,MVP_sal,MVP_dens]=load_MVPtransect(pathL2,transectname);


dist=MVP_dist(:,1);%AD[0 cumsum(whdist(MVP_lat,MVP_lon))]./1000;
g=9.81;

%badprofile=[5 23 24];

%Marco Comment 02_06
badprofile=[];


% ADDED MARCO selezione della pressione massima da considerare per il cast
ii = find(MVP_pres(1,:)<260);

svan=sw_svan(MVP_sal(:,ii),MVP_temp(:,ii),MVP_pres(:,ii)); %%% Specific Volume Anomaly  [m^3 kg^-1]

% steric height (m)
for i=1:size(svan,1) %%%AD cycle on profiles
    h(i)=(1/g)*nansum(1e4*svan(i,1:end).*presDelta);
end
h(badprofile)=NaN;

%geo current (m/s)
coriolis=1.04e-4 %2omegasinphi %s^-1
v=nan(size(dist,1),1);
for i=3:size(dist,1)-1;
    dx=(dist(i-1)-dist(i+1))*1000; %[m]
    dh=(h(i-1)-h(i+1));
    v(i)=g/coriolis*dh/dx;
end

figure

subplot(3,1,1)

plot(MVP_dist(:,1),h)
axis([-0.5 round(max(MVP_dist(:,1)))+0.5 min(h)-0.05 max(h)+0.05])
grid on
title([transectname],'interpreter','none');
ylabel('steric height [m]')
%(,'geostrophic current [m/s]')
xlabel('distance [km]')



subplot(3,1,2)

plot(MVP_dist(:,1),v)
%axis([-0.5 round(max(MVP_dist(:,1)))+0.5 min(h)-0.05 max(h)+0.05])
grid on
title([transectname],'interpreter','none');
ylabel('geostrophic current [m/s]')
%(,'geostrophic current [m/s]')
xlabel('distance [km]')




subplot(3,1,3);hold on

contourf(MVP_dist,MVP_pres,MVP_dens);
plot(MVP_dist,MVP_pres,'m.');
axis([-0.5 round(max(MVP_dist(:,1)))+0.5 presMin presMax])
axis ij
title([transectname,'   density'],'interpreter','none');
ylabel('pressure [dBar]')
xlabel('distance [km]')

break


% FIGURE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% parameters %%%%%%%%
  zsc = get(0,'MonitorPositions');
  screenx =  zsc(3)

clear all
load('../Satellite_Trace_altika647b.mat')
MVP_dens([47 52],:)=[];MVP_sal([47 52],:)=[];MVP_temp([47 52],:)=[];
MVP_lon([47 52])=[];MVP_lat([47 52])=[];
pp=[1:350];

dist=fliplr([0 cumsum(whdist(flipud(MVP_lat),flipud(MVP_lon)))])./1000;

% specific volume anomaly
MVP_sal(:,1:6)=repmat(MVP_sal(:,7),[1,6]);
MVP_temp(:,1:6)=repmat(MVP_temp(:,7),[1,6]);
for i=1:length(pp)
	svan(:,i)=sw_svan(MVP_sal(:,i),MVP_temp(:,i),pp(i));
end
 %steric height
for i=1:size(MVP_dens,1)
	h(i)=nanmean(MVP_dens(:))*nansum(svan(i,1:275));
end

% parameters %%%%%%%%
  zsc = get(0,'MonitorPositions');
  screenx =  zsc(3)/2; screeny = zsc(4)/2;
  lx = 16; ly = 12;
  coeffposi  = 50;  fs=12; fn='helvetica';
  ctrs=[27:.5:31];
  clims=[13.5 18];

fig = figure('menubar','none','PaperUnits','points','paperpositionmode', 'auto','color','w');
set(fig,'PaperPosition',[0 0 lx*coeffposi ly*coeffposi],...
	 'Posi',[screenx screeny 1.5*lx*coeffposi ly*coeffposi]);
 
 a=axes('position',[.15 .15 .65 .5],'FontSize',fs,'fontname',fn,...
      'XLim',[0 max(dist)],'YLim',[-300 0],'box','on');
 hold on;grid on; ylabel('depth (m)','FontSize',fs,'fontname',fn);
  xlabel('distance (km)','FontSize',fs,'fontname',fn)

    pcolor(dist,-pp,MVP_temp');shading flat
 	caxis(clims);
    [C,hc]=contour(dist,-pp,[MVP_dens-1000]',ctrs,'w');
	tc=clabel(C,hc);
	set(tc,'color','w','fontsize',fs-2) 

% colorbar
	cl = clrbr(a,[clims(1):1e-2:clims(2)],[.82 .15 .02 .5],fs-1);
	set(cl,'fontsize',fs-1)
	set(get(cl,'ylabel'),'string','°C','fontsize',fs-1,'fontname',fn);
  	
	b=axes('parent',gcf,'position',get(a,'pos'),'XLim',get(a,'Xlim'),...
    'YLim',get(a,'Ylim'),'YTick',get(a,'Ytick'),'XTick',get(a,'xtick'),...
    'YTicklabel',[],'XTicklabel',[],'box','on');
	grid on; set(gca,'color','none');


 a2=axes('position',[.15 .7 .65 .2],'FontSize',fs,'fontname',fn,...
      'XLim',[MVP_lat(1) MVP_lat(end)],'YLim',[-20 -13],...
       'ytick',[-22:2:-12],'xaxislocation','top','xdir','rev','box','on');
 hold on;grid on; ylabel('steric height (cm)','FontSize',fs,'fontname',fn);
  xlabel('latitude','FontSize',fs,'fontname',fn)

 plot(MVP_lat,h.*100,'-*k')
   
cd ..
	print(gcf,'-dpng','-r150',['sh_altika647b.png'])  
	close 
cd scripts/





clear all
load('../Satellite_Trace_altika429.mat')
supp=[1 9 19:28];
MVP_dens(supp,:)=[];MVP_sal(supp,:)=[];MVP_temp(supp,:)=[];
MVP_lon(supp)=[];MVP_lat(supp)=[];
pp=[1:350];
dist=[0 cumsum(whdist(MVP_lat,MVP_lon))]./1000;

% specific volume anomaly
MVP_sal(:,1:6)=repmat(MVP_sal(:,6),[1,6]);
MVP_temp(:,1:6)=repmat(MVP_temp(:,6),[1,6]);
for i=1:length(pp)
	svan(:,i)=sw_svan(MVP_sal(:,i),MVP_temp(:,i),pp(i));
end
 %steric height
for i=1:size(MVP_dens,1)
	h(i)=nanmean(MVP_dens(:))*nansum(svan(i,1:275));
end

% parameters %%%%%%%%
  zsc = get(0,'MonitorPositions');
  screenx =  zsc(3)/2; screeny = zsc(4)/2;
  lx = 16; ly = 12;
  coeffposi  = 50;  fs=12; fn='helvetica';
  ctrs=[27:.5:31];
  clims=[13.5 19];

fig = figure('menubar','none','PaperUnits','points','paperpositionmode', 'auto','color','w');
set(fig,'PaperPosition',[0 0 lx*coeffposi ly*coeffposi],...
	 'Posi',[screenx screeny 1.5*lx*coeffposi ly*coeffposi]);
 
 a=axes('position',[.15 .15 .65 .5],'FontSize',fs,'fontname',fn,...
      'XLim',[0 max(dist)],'YLim',[-300 0],'box','on');
 hold on;grid on; ylabel('depth (m)','FontSize',fs,'fontname',fn);
  xlabel('distance (km)','FontSize',fs,'fontname',fn)

    pcolor(dist,-pp,MVP_temp');shading flat
 	caxis(clims);
    [C,hc]=contour(dist,-pp,[MVP_dens-1000]',ctrs,'w');
	tc=clabel(C,hc);
	set(tc,'color','w','fontsize',fs-2) 

% colorbar
	cl = clrbr(a,[clims(1):1e-2:clims(2)],[.82 .15 .02 .5],fs-1);
	set(cl,'fontsize',fs-1)
	set(get(cl,'ylabel'),'string','°C','fontsize',fs-1,'fontname',fn);
  	
	b=axes('parent',gcf,'position',get(a,'pos'),'XLim',get(a,'Xlim'),...
    'YLim',get(a,'Ylim'),'YTick',get(a,'Ytick'),'XTick',get(a,'xtick'),...
    'YTicklabel',[],'XTicklabel',[],'box','on');
	grid on; set(gca,'color','none');


 a2=axes('position',[.15 .7 .65 .2],'FontSize',fs,'fontname',fn,...
      'XLim',[MVP_lat(1) MVP_lat(end)],'YLim',[-18 -6],...
       'ytick',[-20:5:0],'xaxislocation','top','box','on');
 hold on;grid on; ylabel('steric height (cm)','FontSize',fs,'fontname',fn);
  xlabel('latitude','FontSize',fs,'fontname',fn)

 plot(MVP_lat,h.*100,'*-k')
   
cd ..
	print(gcf,'-dpng','-r150',['sh_altika429.png'])  
	close 
cd scripts/

screeny = zsc(4)/2;
  lx = 16; ly = 12;
  coeffposi  = 50;  fs=12; fn='helvetica';
  ctrs=[27:.5:31];
  clims=[13.5 18];

fig = figure('menubar','none','PaperUnits','points','paperpositionmode', 'auto','color','w');
set(fig,'PaperPosition',[0 0 lx*coeffposi ly*coeffposi],...
	 'Posi',[screenx screeny 1.5*lx*coeffposi ly*coeffposi]);
 
 a=axes('position',[.15 .15 .65 .5],'FontSize',fs,'fontname',fn,...
      'XLim',[0 max(dist)],'YLim',[-300 0],'box','on');
 hold on;grid on; ylabel('depth (m)','FontSize',fs,'fontname',fn);
  xlabel('distance (km)','FontSize',fs,'fontname',fn)
% steric height

    pcolor(dist,-pp,MVP_temp');shading flat
 	caxis(clims);
    [C,hc]=contour(dist,-pp,[MVP_dens-1000]',ctrs,'w');
	tc=clabel(C,hc);
	set(tc,'color','w','fontsize',fs-2) 

% colorbar
	cl = clrbr(a,[clims(1):1e-2:clims(2)],[.82 .15 .02 .5],fs-1);
	set(cl,'fontsize',fs-1)
	set(get(cl,'ylabel'),'string','°C','fontsize',fs-1,'fontname',fn);
  	
	b=axes('parent',gcf,'position',get(a,'pos'),'XLim',get(a,'Xlim'),...
    'YLim',get(a,'Ylim'),'YTick',get(a,'Ytick'),'XTick',get(a,'xtick'),...
    'YTicklabel',[],'XTicklabel',[],'box','on');
	grid on; set(gca,'color','none');


 a2=axes('position',[.15 .7 .65 .2],'FontSize',fs,'fontname',fn,...
      'XLim',[MVP_lat(end) MVP_lat(1)],'YLim',[-21 -15],...
       'ytick',[-22:2:-12],'xaxislocation','top','xdir','rev','box','on');
 hold on;grid on; ylabel('steric height (cm)','FontSize',fs,'fontname',fn);
  xlabel('latitude','FontSize',fs,'fontname',fn)

 plot(MVP_lat,h.*100,'*-k')
   
cd ..
	print(gcf,'-dpng','-r150',['sh_altika647a.png'])  
	close 
cd scripts/

