clear all
close all
%read and plot TSG
load('/Volumes/Boss_SSD/Perseverance/prod/Perseverance_InLine_TSG_20260120_20260302_Product_v20260303.mat');


%compute density:
%input Fchl and Turbidity
cd '/Volumes/Boss_SSD/Perseverance/raw/FLNTU999'
B=dir('FLNTU999_2026012*.csv');
C=dir('FLNTU999_2026013*.csv');
D=dir('FLNTU999_202602*.csv');
E=dir('FLNTU999_202603*.csv');
B=[B;C;D;E];
tt=[];
Turb=[];
Fchl=[];
Lon=[];
Lat=[];
for i=1:length(B)
    A=rd_FLNTU(B(i).name);
    Fchl=[Fchl;A.FLNTU_Chla];
    Turb=[Turb;A.FLNTU_Turb];
    Lon=[Lon;A.lon];
    Lat=[Lat;A.lat];
    tt=[tt;datenum(char(A.dt))];
end

load('/Volumes/Boss_SSD/Perseverance/prod/Perseverance_InLine_ACS_20260120_20260302_Products_v20260303.mat');
 
%% plot Chl map 
 figure
 clf
mresol=0; 
 m_proj('Stereographic','longitude',160,'latitude',-60,'radius',20);
 sondes=[-5000 -3000 -2000 -1000 -500 -250 -0];

 I=find(acs_prod.Chl_lineheight<0.01);
 acs_prod.Chl_lineheight(I)=NaN;
 minChl=prctile(log10(acs_prod.Chl_lineheight),1);
 maxChl=prctile(log10(acs_prod.Chl_lineheight),99);


 if mresol== 0  % low map and low bathy resolution
     m_coast('patch',[.7 .7 .7],'edgecolor','k');
     m_elev('contour',sondes);
 elseif mresol== 1  % low map and high bathy resolution
     m_coast('patch',[.7 .7 .7],'edgecolor','k');
     m_tbase('contour',sondes,'linecolor','k');
 elseif mresol== 2  % high map and high bathy resolution
     m_usercoast('gumby','patch',[.7 .7 .7],'edgecolor','k');
     m_tbase('contour',sondes,'linecolor','k');
 end
 m_grid
 [lontsg,lattsg]=m_ll2xy(acs_prod.lon,acs_prod.lat);
 hold on
 scatter (lontsg,lattsg,8,log10(acs_prod.Chl_lineheight))
 hold on
 caxis([minChl maxChl])
 xlabel('lon');
 ylabel('lat');
 title(sprintf(' Log_{10}{Chlorophyl} [mg m^{-3}]'));
 colorbar
 grid on
  print('chlorophyll.png','-dpng')
 
 %% plot F_Chl map 
 figure(9)
 clf

 I=find(acs_prod.Chl_lineheight<0.01);
 acs_prod.Chl_lineheight(I)=NaN;
 minChl=prctile(log10(Fchl),1);
 maxChl=prctile(log10(Fchl),99);


 if mresol== 0  % low map and low bathy resolution
     m_coast('patch',[.7 .7 .7],'edgecolor','k');
     m_elev('contour',sondes);
 elseif mresol== 1  % low map and high bathy resolution
     m_coast('patch',[.7 .7 .7],'edgecolor','k');
     m_tbase('contour',sondes,'linecolor','k');
 elseif mresol== 2  % high map and high bathy resolution
     m_usercoast('gumby','patch',[.7 .7 .7],'edgecolor','k');
     m_tbase('contour',sondes,'linecolor','k');
 end
 m_grid
 [lontsg,lattsg]=m_ll2xy(Lon,Lat);
 hold on
 scatter (lontsg,lattsg,8,log10(Fchl))
 hold on
 caxis([minChl maxChl])
 xlabel('lon');
 ylabel('lat');
 title(sprintf(' Log_{10}{F-Chlorophyl} [mg m^{-3}]'));
 colorbar
 grid on
  print('F_{chl}.png','-dpng')




%compute SZA along-track
%delta=23.45*sind(360/365*(yeardat-81));



 %compare ship fluorescence with AC-S
figure
 [C,IA,IC] = unique(tt);
 Fchl_FB=interp1(tt(IA),Fchl(IA),datenum(acs_prod.dt),'linear');
 figure(11)
 loglog(Fchl_FB,acs_prod.Chl_lineheight,'.')
 xlabel('Fchl')
 ylabel('chl_{lh}')



 load('/Volumes/Boss_SSD/Perseverance/raw/Radiance_data/PERSEVERANCE_RADIOMETERS_20260223_0633.mat')
rad_interp=interp1(rad.time,rad.SMP21_SW_Flux,datenum(acs_prod.dt),'linear');

rad_interp_smooth=NaN*ones(length(rad_interp),1);
for i=1+5:length(rad_interp)-5
    rad_interp_smooth(i)=max(rad_interp(i-5:i+5));
end

plot(rad_interp_smooth,Fchl_FB,'.');

Fchl_smooth=NaN*ones(length(rad_interp),1);
chl_smooth=NaN*ones(length(rad_interp),1);
for i=1+5:length(rad_interp)-5
    Fchl_smooth(i)=min(Fchl_FB(i-5:i+5));
    chl_smooth(i)=min(acs_prod.Chl_lineheight(i-5:i+5))
end
 



 








