%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%                      RIVV                         %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%Victor Chavarrias (victor.chavarrias@deltares.nl)
%
%$Revision$
%$Date$
%$Author$
%$Id$
%$HeadURL$
%
%MATLAB BUGS:
%   -The command to change font name does not work. It does not give error
%   but it does not change the font [151102].
%   -When getting and setting position of ylabels, axis, colorbars,
%   etcetera, if the figure is open in screensize the result is different
%   than if it is not. Moreover, you may need to put a pause(1) after getting
%   positions and setting them [151105].
%   -When something is out of the axes (the box delimited by 'Position')
%   (e.g. text outside the axes), the continuous colors (e.g. from a plot
%   like 'area') have weird lines in .eps.
%   -FontName if interpreter LaTeX: check post 114116
%	-When adding text in duration axis, scatter interprets days while surf interprets hours

function fig_his_sal_02(in_p)

if isfield(in_p,'fig_visible')==0
    in_p.fig_visible=0;
end
if isfield(in_p,'fig_print')==0
    in_p.fig_print=0;
end
if isfield(in_p,'fname')==0
    in_p.fname='fig';
end
if isfield(in_p,'fig_size')==0
    in_p.fig_size=[0,0,15,15];
end
if isfield(in_p,'fig_overwrite')==0
    in_p.fig_overwrite=1;
end
if isfield(in_p,'fid_log')==0
    in_p.fid_log=NaN;
end
in_p=isfield_default(in_p,'tol_ldb',10000);
in_p=isfield_default(in_p,'xlims',[NaN,NaN]);
in_p=isfield_default(in_p,'ylim_wl',[NaN,NaN]);
in_p=isfield_default(in_p,'ylim_sal',[NaN,NaN]);
in_p=isfield_default(in_p,'lan','en');

v2struct(in_p)

[lims_time,ylim_wl]=xlim_ylim(xlims,ylim_wl,tim_wl,wl);
[~,ylim_sal]=xlim_ylim(xlims,ylim_sal,tim_wl,val);

%% check if printing
print_fig=check_print_figure(in_p);
if ~print_fig
    return
end

%figure input
prnt.filename=fname;
prnt.size=[0,0,14.5,10.5]; %slide=[0,0,25.4,19.05]; slide16:9=[0,0,33.867,19.05] tex=[0,0,11.6,..]; deltares=[0,0,14.5,22]
npr=3; %number of plot rows
npc=2; %number of plot columns
marg.mt=1.5; %top margin [cm]
marg.mb=1.5; %bottom margin [cm]
marg.mr=1.5; %right margin [cm]
marg.ml=1.5; %left margin [cm]
marg.sh=1.0; %horizontal spacing [cm]
marg.sv=1.5; %vertical spacing [cm]

prop.c1='k';
prop.c2='b';
prop.ms1=20; 
prop.mf1='r'; 
prop.mt1='o'; 
prop.lw1=1;
prop.ls1='-'; %'-','--',':','-.'
prop.lsm='-';
prop.cm='r';
prop.ls={'-','--',':','-.'};
prop.m1='none'; % 'o', '+', '*', ...
prop.fs=9;
prop.fn='Helvetica';
prop.color=[... %>= matlab 2014b default
 0.0000    0.4470    0.7410;... %blue
 0.8500    0.3250    0.0980;... %red
 0.9290    0.6940    0.1250;... %yellow
 0.4940    0.1840    0.5560;... %purple
 0.4660    0.6740    0.1880;... %green
 0.3010    0.7450    0.9330;... %cyan
 0.6350    0.0780    0.1840];   %brown
% prop.color=[... %<  matlab 2014b default
%  0.0000    0.0000    1.0000;... %blue
%  0.0000    0.5000    0.0000;... %green
%  1.0000    0.0000    0.0000;... %red
%  0.0000    0.7500    0.7500;... %cyan
%  0.7500    0.0000    0.7500;... %purple
%  0.7500    0.7500    0.0000;... %ocre
%  0.2500    0.2500    0.2500];   %grey
% set(groot,'defaultAxesColorOrder',prop.color)
% set(groot,'defaultAxesColorOrder','default') %reset the color order to the default value

%set interpreter to Latex (to have bold text use \bfseries{})
% set(groot,'defaultTextInterpreter','Latex'); 
% set(groot,'defaultAxesTickLabelInterpreter','Latex'); 
% set(groot,'defaultLegendInterpreter','Latex');
set(groot,'defaultTextInterpreter','tex'); 
set(groot,'defaultAxesTickLabelInterpreter','tex'); 
set(groot,'defaultLegendInterpreter','tex');

%colorbar
% kr=1; kc=1;
% cbar(kr,kc).displacement=[0.0,0.0,0,0]; 
% cbar(kr,kc).location='northoutside';
% cbar(kr,kc).label='distance [m]';
% 
% kr=2; kc=1;
% cbar(kr,kc).displacement=[0.0,0.0,0,0]; 
% cbar(kr,kc).location='northoutside';
% cbar(kr,kc).label='salinity [psu]';

%text
%     %irregulra
% kr=1; kc=1;
% texti.sfig(kr,kc).pos=[0.015,0.5e-3;0.03,-0.5e-3;0.005,-1e-3];
% texti.sfig(kr,kc).tex={'1','2','a'};
% texti.sfig(kr,kc).clr={prop.color(1,:),prop.color(2,:),'k'};
% texti.sfig(kr,kc).ref={'ul'};
% texti.sfig(kr,kc).fwe={'bold','normal'};
% texti.sfig(kr,kc).rot=[0,90];
% 
%     %regular
% text_str={'a','b','c';'d','e','f';'g','h','i';'j','k','l';'m','n','o'};
% text_str={'a','b';'c','d';'e','f';'g','h'};
% for kr=1:npr
%     for kc=1:npc
% % kr=1; kc=1;
% texti.sfig(kr,kc).pos=[0.5,0.5];
% texti.sfig(kr,kc).tex={text_str{kr,kc}}; %#ok
% texti.sfig(kr,kc).clr={'k'};
% texti.sfig(kr,kc).ref={'lr'};
% texti.sfig(kr,kc).fwe={'bold'};
% texti.sfig(kr,kc).rot=[0,90];
%     end
% end
%     %regular more than one
% text_str={'Hir.','Hir.','Hir.';'Ia','Ia','Ia';'Ib','Ib','Ib';'IIa','IIa','IIa';'IIb','IIb','IIb';'IIc','IIc','IIc';'IId','IId','IId'};
% text_str2={'a','b','c';'d','e','f';'g','h','i';'j','k','l';'m','n','o';'p','q','r';'s','t','u'};
% for kr=1:npr
%     for kc=1:npc
% % kr=1; kc=1;
% texti.sfig(kr,kc).pos=[0.5,0.5;0.5,0.5];
% texti.sfig(kr,kc).tex={text_str{kr,kc},text_str2{kr,kc}}; %#ok
% texti.sfig(kr,kc).clr={'k','k'};
% texti.sfig(kr,kc).ref={'ll','lr'};
% texti.sfig(kr,kc).fwe={'bold','normal'};
% texti.sfig(kr,kc).rot=[0,90];
%     end
% end

%data rework

%axes and limits
kr=1; kc=1;
lims.y(kr,kc,1:2)=station_xy(2)+[-tol_ldb,tol_ldb];
lims.x(kr,kc,1:2)=station_xy(1)+[-tol_ldb,tol_ldb];
% lims.c(kr,kc,1:2)=clims;
% xlabels{kr,kc}='distance along profile [m]';
% ylabels{kr,kc}='depth [m]';

kr=1; kc=2;
lims.y(kr,kc,1:2)=ylim_wl;
% lims.x(kr,kc,1:2)=x_lims;
% lims.c(kr,kc,1:2)=clims;
% xlabels{kr,kc}='distance along profile [m]';
% ylabels{kr,kc}=labels4all('etaw',1,lan);
lims_d.x(kr,kc,1:2)=lims_time;

kr=2; kc=1;
lims.y(kr,kc,1:2)=ylim_sal;
% lims.x(kr,kc,1:2)=x_lims;
% lims.c(kr,kc,1:2)=clims;
% xlabels{kr,kc}='distance along profile [m]';
ylabels{kr,kc}=labels4all('sal',1,lan);

lims_d.x(kr,kc,1:2)=lims_time;

kr=2; kc=2;
lims.y(kr,kc,1:2)=ylim_sal*1000./1.80655; %conversion factor from psu tp mgCl/l;
ylabels{kr,kc}=labels4all('cl',1,lan);
% lims.x(kr,kc,1:2)=x_lims;
% lims.c(kr,kc,1:2)=clims;
% xlabels{kr,kc}='distance along profile [m]';
% lims_d.x(kr,kc,1:2)=lims_time;

% kr=2; kc=2;
% lims.y(kr,kc,1:2)=ylim_err;
% % lims.x(kr,kc,1:2)=x_lims;
% % lims.c(kr,kc,1:2)=clims;
% % xlabels{kr,kc}='distance along profile [m]';
% ylabels{kr,kc}='distance to measurement [m]';
% lims_d.x(kr,kc,1:2)=lims_time;

% lims.y(kr,kc,1:2)=ylim2;
% lims.x(kr,kc,1:2)=xlim2;
% lims.c(kr,kc,1:2)=clims;
% xlabels{kr,kc}='time';
% ylabels{kr,kc}='elevation [m]';

% lims_d.x(kr,kc,1:2)=seconds([3*3600+20*60,6*3600+40*60]); %duration
% lims_d.x(kr,kc,1:2)=[datetime(x_mea(1),'ConvertFrom','datenum'),datetime(x_mea(end),'ConvertFrom','datenum')]; %time

% brewermap('demo')
% cmap=brewermap(100,'RdYlBu');
% cmap=improved_turbo(round(clims(end)-clims(1)));
% cmap_dist=brewermap(100,'YlOrRd');

%center around 0
% ncmap=1000;
% cmap1=brewermap(ncmap,'RdYlGn');
% cmap=flipud([flipud(cmap1(1:ncmap/2-ncmap*0.05,:));flipud(cmap1(ncmap/2+ncmap*0.05:end,:))]);

%cutted centre colormap
% ncmap=100;
% cmap=flipud(brewermap(ncmap,'RdBu'));
% fact=0.1; %percentage of values to remove from the center
% cmap=[cmap(1:(ncmap-round(fact*ncmap))/2,:);cmap((ncmap+round(fact*ncmap))/2:end,:)];
% %compressed colormap
% ncmap=100;
% cmap=flipud(brewermap(ncmap,'RdYlBu'));
% p1=0.5; %fraction of cmap compressed in p2
% p2=0.7; %fraction of
% np1=round(ncmap*p1);
% np2=round(ncmap*p2);
% x=1:1:ncmap;
% x1=1:1:np1; %x vector
% x2=1:1:ncmap-np1; %x vector
% y1=cmap(1:np1,:); 
% y2=cmap(np1+1:end,:); 
% xq1=linspace(1,np1,np2); %query vector 1
% xq2=linspace(1,ncmap-np1,ncmap-np2); %query vector 2
% vq1=interp1(x1,y1,xq1);
% vq2=interp1(x2,y2,xq2);
% cmap=[vq1;vq2];
% %gauss colormap
% ncmap=100;
% cmap=flipud(brewermap(ncmap,'RdYlBu'));
% x=linspace(0,1,ncmap);
% xs=normcdf(x,0.5,0.25);
% plot(x,xs)
% cmap2=interp1(x,cmap,xs);
% cmap=cmap2;
% %merge 2 colormaps at a specific value. cmap1 spans between [clim_l(1),aux_cmap_change] and cmap 2 between [aux_cmap_change,clim_l(2)]
% ncmap=100; %total number of colors (will be rounded)
% aux_cmap_change=1; %value in which the colormaps change. 
% aux_cmap1_n=round(ncmap*(aux_cmap_change-clim_l(1))/(clim_l(2)-clim_l(1)));
% aux_cmap2_n=round(ncmap*(clim_l(2)-aux_cmap_change)/(clim_l(2)-clim_l(1)));
% cmap1=flipud(brewermap(aux_cmap1_n,'Reds'));
% cmap2=brewermap(aux_cmap2_n,'Greens');
% cmap=[cmap1;cmap2];

%figure initialize
han.fig=figure('name',prnt.filename);
set(han.fig,'paperunits','centimeters','paperposition',prnt.size,'visible',fig_visible)
set(han.fig,'units','normalized','outerposition',[0,0,1,1]) %full monitor 1
% set(han.fig,'units','normalized','outerposition',[-1,0,1,1]) %full monitor 2
[mt,mb,mr,ml,sh,sv]=pre_subaxis(han.fig,marg.mt,marg.mb,marg.mr,marg.ml,marg.sh,marg.sv);

%subplots initialize
    %if regular
% for kr=1:npr
%     for kc=1:npc
%         han.sfig(kr,kc)=subaxis(npr,npc,kc,kr,1,1,'mt',mt,'mb',mb,'mr',mr,'ml',ml,'sv',sv,'sh',sh);
%     end
% end
    %if irregular
han.sfig(1,1)=subaxis(npr,npc,1,1,1,1,'mt',mt,'mb',mb,'mr',mr,'ml',ml,'sv',sv,'sh',sh);
han.sfig(1,2)=subaxis(npr,npc,2,1,1,1,'mt',mt,'mb',mb,'mr',mr,'ml',ml,'sv',sv,'sh',sh);
han.sfig(2,1)=subaxis(npr,npc,1,2,2,2,'mt',mt,'mb',mb,'mr',mr,'ml',ml,'sv',sv,'sh',sh);

    %add axis to sub1
kr=2;kc=2;
pos.sfig=han.sfig(2,1).Position; % position of first axes    
han.sfig(kr,kc)=axes('units','normalized','Position',pos.sfig,'XAxisLocation','bottom','YAxisLocation','right');
% han.sfig(kr,kc).YLabel.String=ylabels{kr,kc};
% hold(han.sfig(kr,kc),'on')
% grid(han.sfig(kr,kc),'on')
% han.sfig(kr,kc).Box='on';
% % han.sfig(kr,kc).XLim=lims.x(kr,kc,:);
% han.sfig(kr,kc).YLim=lims.y(kr,kc,:);
% han.sfig(kr,kc).XColor='none';
% % han.sfig(kr,kc).YColor=prop.c2;
% han.sfig(kr,kc).Color='none';
    
% pos.sfig=[0.18,0.7,0.25,0.25]; % position of first axes    
% han.sfig(kr,kc)=axes('units','normalized','Position',pos.sfig,'XAxisLocation','bottom','YAxisLocation','left');
% han.sfig(kr,kc).YLabel.String='sediment transport rate (coarse) [g/min]';
kr=1; kc=1;
hold(han.sfig(kr,kc),'on')
grid(han.sfig(kr,kc),'on')
han.sfig(kr,kc).Box='on';
axis(han.sfig(kr,kc),'equal')
han.sfig(kr,kc).XLim=lims.x(kr,kc,:);
han.sfig(kr,kc).YLim=lims.y(kr,kc,:);
% han.sfig(kr,kc).XLabel.String=xlabels{kr,kc};
% han.sfig(kr,kc).YLabel.String=ylabels{kr,kc};
han.sfig(kr,kc).XTickLabel='';
han.sfig(kr,kc).YTickLabel='';
% han.sfig(kr,kc).XTick=[];  
% han.sfig(kr,kc).YTick=[];  
% han.sfig(kr,kc).XScale='log';
% han.sfig(kr,kc).YScale='log';
% han.sfig(kr,kc).Title.String=station;
% han.sfig(kr,kc).XColor='r';
% han.sfig(kr,kc).YColor='k';
% han.sfig(kr,kc).Units='centimeters';
aux=han.sfig(kr,kc).Position;
% han.sfig(kr,kc).Position=aux;
han.sfig(kr,kc).Position=aux+[-0.2,-0.02,aux(3)*1,aux(4)*1];

%properties
    %sub11
kr=1; kc=2;   
hold(han.sfig(kr,kc),'on')
grid(han.sfig(kr,kc),'on')
% axis(han.sfig(kr,kc),'equal')
han.sfig(kr,kc).Box='on';
% han.sfig(kr,kc).XLim=lims.x(kr,kc,:);
% han.sfig(kr,kc).YLim=lims.y(kr,kc,:);
% han.sfig(kr,kc).XLabel.String=xlabels{kr,kc};
% han.sfig(kr,kc).YLabel.String=ylabels{kr,kc};
% han.sfig(kr,kc).XTickLabel='';
% han.sfig(kr,kc).YTickLabel='';
% han.sfig(kr,kc).XTick=[];  
% han.sfig(kr,kc).YTick=[];  
% han.sfig(kr,kc).XScale='log';
% han.sfig(kr,kc).YScale='log';
han.sfig(kr,kc).Title.String=labels4all('etaw',1,lan);
% han.sfig(kr,kc).XColor='r';
% han.sfig(kr,kc).YColor='k';

kr=2; kc=1;   
hold(han.sfig(kr,kc),'on')
grid(han.sfig(kr,kc),'on')
% axis(han.sfig(kr,kc),'equal')
han.sfig(kr,kc).Box='on';
% han.sfig(kr,kc).XLim=lims.x(kr,kc,:);
if ~isnan(ylim_sal)
han.sfig(kr,kc).YLim=lims.y(kr,kc,:);
end
% han.sfig(kr,kc).XLabel.String=xlabels{kr,kc};
han.sfig(kr,kc).YLabel.String=ylabels{kr,kc};
% han.sfig(kr,kc).XTickLabel='';
% han.sfig(kr,kc).YTickLabel='';
% han.sfig(kr,kc).XTick=[];  
% han.sfig(kr,kc).YTick=[];  
% han.sfig(kr,kc).XScale='log';
% han.sfig(kr,kc).YScale='log';
stationnodot=strrep(station,'.','');
switch lan
    case 'en'
        str_dep='at depth';
    case 'nl'
        str_dep='diepte';
    otherwise
        error('ups...')
end
[str_sta,str_found]=waterDictionary(stationnodot(~isspace(stationnodot)),NaN,'normal','dict','rwsNames.csv');
% if ~str_found
%     %read from vertabel
%     path_measurements_conversionlist
%     %check location clear in data_stations
% end
han.sfig(kr,kc).Title.String={strrep(str_sta,'_','\_');sprintf('%s %5.2f m',str_dep,elev)};
% han.sfig(kr,kc).XColor='r';
% han.sfig(kr,kc).YColor='k';

kr=2; kc=2;   
hold(han.sfig(kr,kc),'on')
grid(han.sfig(kr,kc),'off')
% axis(han.sfig(kr,kc),'equal')
han.sfig(kr,kc).Box='off';
% han.sfig(kr,kc).XLim=lims.x(kr,kc,:);
if ~isnan(ylim_sal)
han.sfig(kr,kc).YLim=lims.y(kr,kc,:);
end
% han.sfig(kr,kc).XLabel.String=xlabels{kr,kc};
han.sfig(kr,kc).YLabel.String=ylabels{kr,kc};
% han.sfig(kr,kc).XTickLabel='';
% han.sfig(kr,kc).YTickLabel='';
% han.sfig(kr,kc).XTick=[];  
% han.sfig(kr,kc).YTick=[];  
% han.sfig(kr,kc).XScale='log';
% han.sfig(kr,kc).YScale='log';
% stationnodot=strrep(station,'.','');
% switch lan
%     case 'en'
% %         han.sfig(kr,kc).Title.String={strrep(station,'_','\_');sprintf('at depth %5.2f m',y_mea(1))};
%         han.sfig(kr,kc).Title.String={strrep(waterDictionary(stationnodot(~isspace(stationnodot)),NaN,'normal','dict','rwsNames.csv'),'_','\_');sprintf('at depth %5.2f m',y_mea(1))};
%     case 'nl'
% %         han.sfig(kr,kc).Title.String={strrep(station,'_','\_');sprintf('diepte %5.2f m',y_mea(1))};
%         han.sfig(kr,kc).Title.String={strrep(waterDictionary(stationnodot(~isspace(stationnodot)),NaN,'normal','dict','rwsNames.csv'),'_','\_');sprintf('diepte %5.2f m',y_mea(1))};
%     otherwise
%         error('ups...')
% end
% han.sfig(kr,kc).XColor='r';
% han.sfig(kr,kc).YColor='k';
han.sfig(kr,kc).XColor='none';
han.sfig(kr,kc).YColor='b';
han.sfig(kr,kc).Color='none';

%% plots

%% ldb with station
kr=1; kc=1;    
nldb=numel(ldb);
for kldb=1:nldb
    plot(ldb(kldb).cord(:,1),ldb(kldb).cord(:,2),'parent',han.sfig(kr,kc),'color',prop.c1,'linewidth',prop.lw1,'linestyle',prop.ls1,'marker',prop.m1)
end
han.p(kr,kc,1)=scatter(station_xy(1,1),station_xy(1,2),prop.ms1,prop.mt1,'filled','parent',han.sfig(kr,kc),'markerfacecolor',prop.mf1);

%% water level
kr=1; kc=2;    
% han.p(kr,kc,1)=plot(datetime(wl_t,'ConvertFrom','datenum','TimeZone',tzone_mea),wl,'parent',han.sfig(kr,kc),'color',prop.c1,'linewidth',1,'linestyle','-');
han.p(kr,kc,1)=plot(tim_wl,wl,'parent',han.sfig(kr,kc),'color',prop.c1,'linewidth',1,'linestyle','-');

kr=2; kc=1;    
%why do we have it in a cell array?
% if ~isempty(x_mea{1,1})
%     nmea=numel(x_mea);
%     for kmea=1:nmea
%         han.p(kr,kc,1)=plot(datetime(x_mea{kmea,1},'ConvertFrom','datenum','TimeZone',tzone_mea),v_mea{kmea,1},'parent',han.sfig(kr,kc),'color',prop.cm,'linewidth',1,'linestyle',prop.lsm);
%         han.p(kr,kc,3)=plot(datetime(x_mea{kmea,1},'ConvertFrom','datenum','TimeZone',tzone_mea),verr{kmea,1},'parent',han.sfig(kr,kc),'color',[0.1,0.1,0.1],'linewidth',1,'linestyle','--');
%     end
% else
%     han.p(kr,kc,1)=plot(datetime(1,'ConvertFrom','datenum','TimeZone',tzone_mea),0,'parent',han.sfig(kr,kc),'color',prop.cm,'linewidth',1,'linestyle',prop.lsm);
%     han.p(kr,kc,3)=plot(datetime(1,'ConvertFrom','datenum','TimeZone',tzone_mea),0,'parent',han.sfig(kr,kc),'color',[0.1,0.1,0.1],'linewidth',1,'linestyle','--');
% end
% han.p(kr,kc,2)=plot(datetime(t_sim_atmea_allt,'ConvertFrom','datenum','TimeZone',tzone_mea),v_sim_atmea,'parent',han.sfig(kr,kc),'color',prop.c1,'linewidth',1,'linestyle','-');
if ~isempty(tim_mea)
    han.p(kr,kc,1)=plot(tim_mea,v_mea,'parent',han.sfig(kr,kc),'color',prop.cm,'linewidth',1,'linestyle',prop.lsm);
    han.p(kr,kc,3)=plot(tim_mea,verr ,'parent',han.sfig(kr,kc),'color',[0.1,0.1,0.1],'linewidth',1,'linestyle','--');
else
    han.p(kr,kc,1)=plot(datetime(1,'ConvertFrom','datenum'),0,'parent',han.sfig(kr,kc),'color',prop.cm,'linewidth',1,'linestyle',prop.lsm);
    han.p(kr,kc,3)=plot(datetime(1,'ConvertFrom','datenum'),0,'parent',han.sfig(kr,kc),'color',[0.1,0.1,0.1],'linewidth',1,'linestyle','--');
end
han.p(kr,kc,2)=plot(tim_sim,v_sim,'parent',han.sfig(kr,kc),'color',prop.c1,'linewidth',1,'linestyle','-');

%duration ticks
% xtickformat(han.sfig(kr,kc),'hh:mm')
kr=1;kc=2;
han.sfig(kr,kc).XLim=lims_d.x(kr,kc,:);

kr=2;kc=1;
han.sfig(kr,kc).XLim=lims_d.x(kr,kc,:);

% kr=2;kc=2;
% han.sfig(kr,kc).XLim=lims_d.x(kr,kc,:);

% kr=1; kc=1;
% aux_datenum=han.sfig(kr,kc).XTick;
% han.sfig(kr,kc).XTickLabel=datestr(aux_datenum);
% han.sfig(kr,kc).XTickLabelRotation=45;

%colormap
% kr=1; kc=1;
% view(han.sfig(kr,kc),[0,90]);
% colormap(han.sfig(kr,kc),cmap_dist);
% if caxis_do
% caxis(han.sfig(kr,kc),lims.c(kr,kc,1:2));
% end
% 
% kr=2; kc=1;
% view(han.sfig(kr,kc),[0,90]);
% colormap(han.sfig(kr,kc),cmap);
% if caxis_do
% caxis(han.sfig(kr,kc),lims.c(kr,kc,1:2));
% end

%text
    %if irregular
% which_pos_text=[1,1;2,1;3,1;3,2];
% nsf=size(which_pos_text);
% for ksf=1:nsf
%     kr=which_pos_text(ksf,1);
%     kc=which_pos_text(ksf,2);
%         ntxt=numel(texti.sfig(kr,kc).tex);
%         for ktx=1:ntxt
%             %if the specified values are in cm 
%             aux.pos=cm2ax(texti.sfig(kr,kc).pos(ktx,:),han.fig,han.sfig(kr,kc),'reference',texti.sfig(kr,kc).ref{ktx});
% %             text(texti.sfig(kr,kc).pos(1,1),texti.sfig(kr,kc).pos(1,2),texti.sfig(kr,kc).tex{ktx},'parent',han.sfig(kr,kc),'color',texti.sfig(kr,kc).clr{ktx},'fontweight','bold')
%             text(aux.pos(1,1),aux.pos(1,2),texti.sfig(kr,kc).tex{ktx},'parent',han.sfig(kr,kc),'color',texti.sfig(kr,kc).clr{ktx},'fontweight',texti.sfig(kr,kc).fwe{ktx},'rotation',texti.sfig(kr,kc).rot(ktx))
%         end
% end
%     %if regular
% for kr=1:npr
%     for kc=1:npc
%         ntxt=numel(texti.sfig(kr,kc).tex);
%         for ktx=1:ntxt
%             %if the specified values are in cm 
%             aux.pos=cm2ax(texti.sfig(kr,kc).pos(ktx,:),han.fig,han.sfig(kr,kc),'reference',texti.sfig(kr,kc).ref{ktx});
% %             text(texti.sfig(kr,kc).pos(1,1),texti.sfig(kr,kc).pos(1,2),texti.sfig(kr,kc).tex{ktx},'parent',han.sfig(kr,kc),'color',texti.sfig(kr,kc).clr{ktx},'fontweight','bold')
%             text(aux.pos(1,1),aux.pos(1,2),texti.sfig(kr,kc).tex{ktx},'parent',han.sfig(kr,kc),'color',texti.sfig(kr,kc).clr{ktx},'fontweight',texti.sfig(kr,kc).fwe{ktx},'rotation',texti.sfig(kr,kc).rot(ktx))
%         end
%     end
% end

%legend
kr=2; kc=1;
% pos.sfig=han.sfig(kr,kc).Position;
% %han.leg=legend(han.leg,{'hyperbolic','elliptic'},'location','northoutside','orientation','vertical');
% han.leg(kr,kc)=legend(han.sfig(kr,kc),reshape(han.p(kr,kc,1:2),1,2),{'\tau<1','\tau>1'},'location','south');
[~,mea_str]=labels4all('measurement',1,lan);
[~,sim_str]=labels4all('simulation',1,lan);
[~,diff_str]=labels4all('difference',1,lan);
han.leg(kr,kc)=legend(han.sfig(kr,kc),reshape(han.p(kr,kc,:),1,numel(han.p(kr,kc,:))),{mea_str,sim_str,diff_str},'location','northeast');
% pos.leg=han.leg(kr,kc).Position;
% han.leg.Position=pos.leg(kr,kc)+[0,0,0,0];
% han.sfig(kr,kc).Position=pos.sfig;

posfig=han.sfig(2,1).Position;
dim=[posfig(1),posfig(2)+posfig(4)-posfig(4)/4,posfig(3)/4,posfig(4)/4];
unit_str='psu';
str={sprintf('bias = %5.2f %s',vbias,unit_str),sprintf('std \t = %5.2f %s',vstd,unit_str),sprintf('rmse \t = %5.2f %s',vrmse,unit_str),sprintf('rho \t = %5.3f    ',corr_R)};
if ~isnan(vbias)
annotation('textbox',dim,'String',str,'FitBoxToText','off','edgecolor','none','horizontalalignment','right','color',[0.9290 0.6940 0.1250]);
end

%colorbar
% kr=1; kc=1;
% pos.sfig=han.sfig(kr,kc).Position;
% han.cbar=colorbar(han.sfig(kr,kc),'location',cbar(kr,kc).location);
% pos.cbar=han.cbar.Position;
% han.cbar.Position=pos.cbar+cbar(kr,kc).displacement;
% han.sfig(kr,kc).Position=pos.sfig;
% han.cbar.Label.String=cbar(kr,kc).label;
% 	%set the marks of the colorbar according to your vector, the number of lines and colors of the colormap is np1 (e.g. 20). The colorbar limit is [1,np1].
% aux2=fliplr(d1_r./La_v); %we have plotted the colors in the other direction, so here we can flip it
% v2p=[1,5,11,15,np1];
% han.cbar.Ticks=v2p;
% aux3=aux2(v2p);
% aux_str=cell(1,numel(v2p));
% for ka=1:numel(v2p)
%     aux_str{ka}=sprintf('%5.3f',aux3(ka));
% end
% han.cbar.TickLabels=aux_str;

% kr=2; kc=1;
% pos.sfig=han.sfig(kr,kc).Position;
% han.cbar=colorbar(han.sfig(kr,kc),'location',cbar(kr,kc).location);
% pos.cbar=han.cbar.Position;
% han.cbar.Position=pos.cbar+cbar(kr,kc).displacement;
% han.sfig(kr,kc).Position=pos.sfig;
% han.cbar.Label.String=cbar(kr,kc).label;

%general
set(findall(han.fig,'-property','FontSize'),'FontSize',prop.fs)
set(findall(han.fig,'-property','FontName'),'FontName',prop.fn) %!!! attention, there is a bug in Matlab and this is not enforced. It is necessary to change it in the '.eps' to 'ArialMT' (check in a .pdf)
% han.fig.Renderer='painters';

%print
% print(han.fig,strcat(prnt.filename,'.eps'),'-depsc2','-loose','-cmyk')
if print_fig
print(han.fig,strcat(prnt.filename,'.png'),'-dpng','-r300')
messageOut(fid_log,sprintf('printed figure %s \n',strcat(prnt.filename,'.png')));
close(han.fig)
end
% print(han.fig,strcat(prnt.filename,'.jpg'),'-djpeg','-r300')