%%%%%%%% plot maps
clear
start
close all


load auxmat.mat

micolor=[.5 0 0]; 

playa_lat=39.4; %Spiaggia Portu Tramatzu
playa_lon=9.7;





%disp(['load allmaps_',experiment,'_',int2str(yearini),'to',int2str(yearfin),'.mat'])
eval(['load allmaps_',experiment,'_',int2str(yearini),'to',int2str(yearfin),'.mat'])


 

namemonth={ 'Jan' ; 'Feb' ; 'Mar'; 'Apr' ; 'May' ; 'Jun'; 'Jul'; 'Aug' ; 'Sep'; 'Oct' ; 'Nov' ; 'Dec' } ;
%{}
clear miinidia,clear miinimes,[miinidia,miinimes]=quefecha_fun(inicio,2010); %myyear);
clear mifindia,clear mifinmes,[mifindia,mifinmes]=quefecha_fun(fin,2010); %myyear);
clear tit0,
%tit0=['\color{red}Physalia entering MedSea  from ',int2str(miinidia),' ',char(namemonth(miinimes)),'  to  ',...
%int2str(mifindia),' ',char(namemonth(mifinmes))];
tit0=['Physalia entering MedSea  from  ',int2str(miinidia),char(namemonth(miinimes)),'  to  ',...
int2str(mifindia),char(namemonth(mifinmes))];

titprob='\color{black}Physalia MAX Presence Probability: WHEN and WHERE';
titrisk='\color{blue}Physalia MAX Encounter Risk: WHEN and WHERE';
%ytit=['\color{red}Database includes   ',int2str(yearini),'  to  ',int2str(yearfin)];
ytit=['Database includes   ',int2str(yearini),'  to  ',int2str(yearfin)];
%--- Probability for an specific day

diafecha=[26];
mesfecha=[8];
for kk=1:length(diafecha)
  clear quedia %sera el julianday
   quedia=julianday_fun(diafecha(kk),mesfecha(kk),2020);

clear tit1,
tit1=[int2str(diafecha(kk)),' ',char(namemonth(mesfecha(kk)))];



%Maps per days
[maxi,maxj]=size(boxid);

  %mat_certainty_free_fract=ones(size(mat_certainty_free_fract));

clear mimat,clear mival
mat_certainty_free_fract(mat_certainty_free_fract==0)=nan;
mat_encounter_risk(mat_encounter_risk==0)=nan;

mival=mat_certainty_free_fract(:,quedia); 
mivalencounter=mat_encounter_risk(:,quedia);
  for i=1:maxi
     for j=1:maxj
      mimat(boxi(i,j),boxj(i,j))=mival(boxid(i,j));
      mimatencounter(boxi(i,j),boxj(i,j))=mivalencounter(boxid(i,j));
     end %for j
    
  end %for i

  
num0=8;
num=.5;
blabel={'Low';'Medium';'High'};


  pintame=0;
   if pintame
  figure,
  imagescnan(boxlon',boxlat',mimat'),clear b,b=colorbar;medcoast
colormap(flipud(hot)) %darkred=max to white=zero
caxis([0 1])
title(['Physalia presence  PROBABILITY on  ',tit1],'fontsize',17)
xlabel(tit0,'fontsize',19,'color',micolor)
ylabel(ytit,'fontsize',19,'color',micolor)
set(b,'ydir','reverse')

hold on,plot(playa_lon,playa_lat,'k+','markersize',num0,'linewidth',num)


%--check position pixels
%plot(mireg_lon,mireg_lat,'m','linewidth',2),hold on
  %%text((minlonreg+maxlonreg)/2,maxlatreg+maxlatreg/100,'PhysaliaSIM domain','fontsize',10,'color','m')

figure,
imagescnan(boxlon',boxlat',mimatencounter'),clear b,b=colorbar;medcoast
%colormap(flipud(hot)) %darkred=max to white=zero
 colormap(jet)
%colormap(jet(3))
colormap([0 1 0;1 1 0; 1 0 0])
caxis([0 10])



set(b,'ytick',[10/6:10/3:10])
set(b,'yticklabel',[blabel])
title(['Physalia ENCOUNTER RISK on  ',tit1],'fontsize',17)
xlabel(tit0,'fontsize',19,'color',micolor)
ylabel(ytit,'fontsize',19,'color',micolor)
hold on,plot(playa_lon,playa_lat,'k+','markersize',num0,'linewidth',num)
end %if pintame
end %for kk
  

%%%%%%%%%%%%%%%%%%%%%%%%%%
%---- Probability max, Risk max: where on which month?
ind1=1:31;
ind2=(ind1(end)+1):(ind1(end)+28);
ind3=(ind2(end)+1):(ind2(end)+31);
ind4=(ind3(end)+1):(ind3(end)+30);
ind5=(ind4(end)+1):(ind4(end)+31);
ind6=(ind5(end)+1):(ind5(end)+30);
ind7=(ind6(end)+1):(ind6(end)+31);
ind8=(ind7(end)+1):(ind7(end)+31);
ind9=(ind8(end)+1):(ind8(end)+30);
ind10=(ind9(end)+1):(ind9(end)+31);
ind11=(ind10(end)+1):(ind10(end)+30);
ind12=(ind11(end)+1):(ind11(end)+31);

month_maxprob(1:12)=nan;
month_maxrisk(1:12)=nan;

for i=1:maxi*maxj %num total filas (y pixels despues)

[maxprob(i),imaxprob(i)]=nanmax(mat_certainty_free_fract(i,:));
[maxrisk(i),imaxrisk(i)]=nanmax(mat_encounter_risk(i,:));

maxriskday(i)=imaxrisk(i); %AQUI

if isnan(maxprob(i))==0;
    for k=1:12
    if imaxprob(i)>=eval(['ind',int2str(k),'(1)']) & imaxprob(i)<=eval(['ind',int2str(k),'(end)'])
    month_maxprob(i)=k;break
     end %if
    end %for k
end %if

if isnan(maxrisk(i))==0;
    for k=1:12
    if imaxrisk(i)>=eval(['ind',int2str(k),'(1)']) & imaxrisk(i)<=eval(['ind',int2str(k),'(end)'])
    month_maxrisk(i)=k;break
     end %if
    end %for k
end %if

    
end %for i

month_maxprob(isnan(maxprob)==1)=nan; %Evito month=0
month_maxprob(month_maxprob==0)=nan;

month_maxrisk(isnan(maxrisk)==1)=nan; %Evito month=0
month_maxrisk(month_maxrisk==0)=nan;



clear mat_maxprob,clear mat_monthmaxprob
clear mat_maxrisk,clear mat_monthmaxrisk,clear mat_maxriskday

  for i=1:maxi
     for j=1:maxj
      mat_maxprob(boxi(i,j),boxj(i,j))=maxprob(boxid(i,j));
      mat_monthmaxprob(boxi(i,j),boxj(i,j))=month_maxprob(boxid(i,j));
      
      mat_maxrisk(boxi(i,j),boxj(i,j))=maxrisk(boxid(i,j));
      mat_monthmaxrisk(boxi(i,j),boxj(i,j))=month_maxrisk(boxid(i,j));
      mat_maxriskday(boxi(i,j),boxj(i,j))=maxriskday(boxid(i,j));
     end %for j
    
  end %for i

  
  mat_monthmaxprob(isnan(mat_monthmaxrisk)==1)=nan;
  mat_monthmaxrisk(isnan(mat_monthmaxprob)==1)=nan;
 
 %---Figures
  
  %figure,imagescnan(boxlon',boxlat',mat_maxprob'),colorbar,medcoast
  figure(1)
  imagescnan(boxlon',boxlat',mat_monthmaxprob'),clear b,b=colorbar;medcoast

  maxcolorbar=12;%max(get(b,'ytick'));
colormap(hsv),colormap(hsv(maxcolorbar))
%colormap(jet),colormap(jet(maxcolorbar))
caxis([0.5 12.5])
set(b,'ytick',[1:maxcolorbar])
set(b,'yticklabel',[namemonth(1:maxcolorbar)])
set(b,'fontsize',20)
set(b,'ydir','reverse')

title(titprob,'fontsize',20)
xlabel(tit0,'fontsize',19,'color',micolor)
ylabel(ytit,'fontsize',19,'color',micolor)
hold on,plot(playa_lon,playa_lat,'k+','markersize',num0,'linewidth',num)
%--check position pixels
%hold on
%plot(mireg_lon,mireg_lat,'m','linewidth',2)


figure(2)
  imagescnan(boxlon',boxlat',mat_monthmaxrisk'),clear b,b=colorbar;medcoast

  maxcolorbar=12;%max(get(b,'ytick'));
colormap(hsv),colormap(hsv(maxcolorbar))
%colormap(jet),colormap(jet(maxcolorbar))
caxis([0.5 12.5])
set(b,'ytick',[1:maxcolorbar])
set(b,'yticklabel',[namemonth(1:maxcolorbar)])
set(b,'fontsize',20)
set(b,'ydir','reverse')

title(titrisk,'fontsize',20)
xlabel(tit0,'fontsize',19,'color',micolor)
ylabel(ytit,'fontsize',19,'color',micolor)
hold on,plot(playa_lon,playa_lat,'k+','markersize',num0,'linewidth',num)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%-----Bubble plot: plot monthmaxprob AND maxprob at the same time!!
sibubble=0;
if sibubble,

clear ind,ind=find(isnan(mat_monthmaxprob)==0);
%ind=ind(1:2:end);
    scax=[-2.5 -2.5  -2.5]';
    scay=[31 32 33]';
    scar=[0.1*0.1 0.5*0.5 1]'*100;
    scac=[nan nan nan]';
    
x = boxlon(ind); %x,y posiciones
y = boxlat(ind);
r = mat_maxprob(ind);
r=r.*r*100; %Size of marker
c = mat_monthmaxprob(ind); %colores

%x=[x;scax];
%y=[y;scay];
%r=[r;scar];
%c=[c;scac];

figure,
h=scatter(x, y, r, c, 'filled', 'MarkerEdgeColor', 'k');
hold on
scatter(scax, scay, scar, scac, 'MarkerEdgeColor', 'k');
medcoast,b=colorbar;

colormap(hsv),colormap(hsv(maxcolorbar))
%colormap(jet),colormap(jet(maxcolorbar))
caxis([0.5 12.5])
%cl=colormap(hsv);

set(b,'ytick',[1:maxcolorbar])
set(b,'yticklabel',[namemonth(1:maxcolorbar)])
set(b,'fontsize',20)

title(titprob,'fontsize',20)
xlabel(tit0,'fontsize',19,'color',micolor)
ylabel(ytit,'fontsize',19,'color',micolor)
hold on,plot(playa_lon,playa_lat,'k+','markersize',num0,'linewidth',num)
%{}
text(scax(1)-2,scay(3)+1,'MAX Probability:','fontsize',15)
text(scax(1)+1,scay(1),'0.1','fontsize',15)
text(scax(2)+1,scay(2),'0.5','fontsize',15)
text(scax(3)+1,scay(3),'1.0','fontsize',15)

%----


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-----Bubble plot: plot monthmaxrisk AND maxrisk at the same time!!
clear ind,ind=find(isnan(mat_monthmaxrisk)==0);
%ind=ind(1:2:end);
    scax=[-2.5 -2.5  -2.5]';
    scay=[31 32 33]';
    scar=[0.33*0.33 .66*.66 1]'*100;
    scac=[nan nan nan]';
    
x = boxlon(ind); %x,y posiciones
y = boxlat(ind);
r = mat_maxrisk(ind)/10; %Cuidado, esto es para pintar bien en escala
r=r.*r*100; %Size of marker
c = mat_monthmaxrisk(ind); %colores

%x=[x;scax];
%y=[y;scay];
%r=[r;scar];
%c=[c;scac];

figure,
h=scatter(x, y, r, c, 'filled', 'MarkerEdgeColor', 'k');
hold on
scatter(scax, scay, scar, scac, 'MarkerEdgeColor', 'b');
medcoast,b=colorbar;

colormap(hsv),colormap(hsv(maxcolorbar))
%colormap(jet),colormap(jet(maxcolorbar))
caxis([0.5 12.5])
%cl=colormap(hsv);

set(b,'ytick',[1:maxcolorbar])
set(b,'yticklabel',[namemonth(1:maxcolorbar)])
set(b,'fontsize',20)

title(titrisk,'fontsize',20)
xlabel(tit0,'fontsize',19,'color',micolor)
ylabel(ytit,'fontsize',19,'color',micolor)
hold on,plot(playa_lon,playa_lat,'k+','markersize',num0,'linewidth',num)
%{}
text(scax(1)-2,scay(3)+1,'MAX Risk:','fontsize',14,'color','b')
text(scax(1)+1,scay(1),'Low','fontsize',14,'color','b')
text(scax(2)+1,scay(2),'Medium','fontsize',14,'color','b')
text(scax(3)+1,scay(3),'High','fontsize',14,'color','b')

end %if sibubble


%figure(3),phys_pose
figure(1),phys_pose
%figure(4),phys_posd
figure(2),phys_posd


clear miroi_lon,miroi_lon=[minlonreg,maxlonreg,maxlonreg,minlonreg,minlonreg];
clear miroi_lat,miroi_lat=[minlatreg,minlatreg,maxlatreg,maxlatreg,minlatreg];
for k=1:2
figure(k) 

plot(miroi_lon,miroi_lat,'color',micolor,'linewidth',2)
text(minlonreg,maxlatreg+maxlatreg/100,'Region of Interest','fontsize',20,'color',micolor)
end

%----
siprint=0;
if siprint
for k=1:12
  figure(k),
  %eval(['print -djpeg PhysaliaSIMfig',int2str(k)])
  eval(['print -dpng PhysSardegnaallyears',int2str(k)])
 end %for k 
 end %if siprint



