
clear all
close all

load ('C:\Documents and Settings\Emmanuel\Desktop\PhilEx\Data_2008\ShipData_2009\ph2_met')
load ('C:\Documents and Settings\Emmanuel\Desktop\PhilEx\Data_2008\Pre-Cruise\Bathymetry\coast_xy_b.txt')
coast_xy=coast_xy_b;
%%%%%%%%%%%%%%%% San Bernardino SB outside %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load ./Datasets_2009/SanBernardino_a_ACS_apcp

I=find(acs_cp.time>733821.3292 & acs_cp.time<733822.0417);

time=acs_cp.time(I);
lat = interp1(met.time,met.lat, acs_cp.time(I), 'nearest');
lon = interp1(met.time,met.lon, acs_cp.time(I), 'nearest');
T = interp1(met.time,met.sst1, acs_cp.time(I), 'nearest'); %in-line temperature
S = interp1(met.time,met.tsal, acs_cp.time(I), 'nearest'); %in-line Salinity

ap=acs_ap.mean(I,:);
ap_wl=acs_ap.wavel(:);
cp=acs_cp.mean(I,:);
cp_wl=acs_cp.wavel(:);
chl=(ap(:,60)-ap(:,54)*43.5/71.1)/0.014;

opts = optimset('fminsearch');
opts = optimset(opts,'MaxIter',4000);
opts = optimset(opts,'MaxFunEvals',2000); % usually 100*number of params
opts = optimset(opts,'TolFun',1e-9);
opts = optimset('LevenbergMarquardt','on');

%guess for paramters (amplitude at 532 and slope)
x0=[0.2, 1];
I1=find(cp_wl>450)
%minimization routine
I=find(isfinite(cp(:,20)));
for i=1:length(I)
    x1 = fminsearch(@least_squares_cp,x0,opts,cp(I(i),I1)',cp_wl(I1));
    gamma(i)=x1(2);
    cp_amp(i)=x1(1);
end
save SB_outside.mat cp_amp gamma chl cp_amp ap ap_wl cp cp_wl time lat lon T S
subplot(2,2,1)
plot(coast_xy(:,1),coast_xy(:,2), 'k')
hold on
scatter(lon,lat,3,chl)
axis([min(lon)-0.1 max(lon)+0.1 min(lat)-0.1 max(lat)+0.1])
title('chlorophyll [\mug/l]')
hold off
colorbar

subplot(2,2,2)
plot(coast_xy(:,1),coast_xy(:,2), 'k')
hold on
scatter(lon(I),lat(I),3,cp_amp)
axis([min(lon)-0.1 max(lon)+0.1 min(lat)-0.1 max(lat)+0.1])
title('c_p(650) [m^{-1}]')
hold off
colorbar

subplot(2,2,3)
plot(coast_xy(:,1),coast_xy(:,2), 'k')
hold on
scatter(lon(I),lat(I),3,gamma)
axis([min(lon)-0.1 max(lon)+0.1 min(lat)-0.1 max(lat)+0.1])
title('PSD-slope \gamma')
hold off
colorbar

subplot(2,2,4)
plot(coast_xy(:,1),coast_xy(:,2), 'k')
hold on
scatter(lon,lat,3,T)
axis([min(lon)-0.1 max(lon)+0.1 min(lat)-0.1 max(lat)+0.1])
title('Temperature [C]')
hold off
colorbar

%%%%%%%%%%%%%%%% San Bernardino SB inside %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all

load ('C:\Documents and Settings\Emmanuel\Desktop\PhilEx\Data_2008\ShipData_2009\ph2_met')
load ('C:\Documents and Settings\Emmanuel\Desktop\PhilEx\Data_2008\Pre-Cruise\Bathymetry\coast_xy_b.txt')
coast_xy=coast_xy_b;load ./Datasets_2009/SanBernardino_a_ACS_apcp
I=find(acs_cp.time>733822.4306 & acs_cp.time<733824.0417);

time=acs_cp.time(I);
lat = interp1(met.time,met.lat, acs_cp.time(I), 'nearest');
lon = interp1(met.time,met.lon, acs_cp.time(I), 'nearest');
T = interp1(met.time,met.sst1, acs_cp.time(I), 'nearest'); %in-line temperature
S = interp1(met.time,met.tsal, acs_cp.time(I), 'nearest'); %in-line Salinity

ap=acs_ap.mean(I,:);
ap_wl=acs_ap.wavel(:);
cp=acs_cp.mean(I,:);
cp_wl=acs_cp.wavel(:);
chl=(ap(:,60)-ap(:,54)*43.5/71.1)/0.014;

load ./Datasets_2009/SanBernardino_b_ACS_apcp
I=find(acs_cp.time>733822.4306 & acs_cp.time<733824.0417);

lat2 = interp1(met.time,met.lat, acs_cp.time(I), 'nearest');
lon2 = interp1(met.time,met.lon, acs_cp.time(I), 'nearest');
T2 = interp1(met.time,met.sst1, acs_cp.time(I), 'nearest'); %in-line temperature
S2 = interp1(met.time,met.tsal, acs_cp.time(I), 'nearest'); %in-line Salinity

ap2=acs_ap.mean(I,:);
cp2=acs_cp.mean(I,:);
chl2=(ap2(:,60)-ap2(:,54)*43.5/71.1)/0.014;

lat=[lat; lat2];
lon=[lon; lon2];
T=[T; T2];
S=[S; S2];
ap=[ap;ap2];
cp=[cp;cp2];
chl=[chl;chl2];

opts = optimset('fminsearch');
opts = optimset(opts,'MaxIter',4000);
opts = optimset(opts,'MaxFunEvals',2000); % usually 100*number of params
opts = optimset(opts,'TolFun',1e-9);
opts = optimset('LevenbergMarquardt','on');

%guess for paramters (amplitude at 532 and slope)
x0=[0.2, 1];
I1=find(cp_wl>450)
%minimization routine
I=find(isfinite(cp(:,20)));
for i=1:length(I)
    x1 = fminsearch(@least_squares_cp,x0,opts,cp(I(i),I1)',cp_wl(I1));
    gamma(i)=x1(2);
    cp_amp(i)=x1(1);
end
save SB_inside.mat cp_amp gamma chl cp_amp ap ap_wl cp cp_wl time lat lon T S
subplot(2,2,1)
plot(coast_xy(:,1),coast_xy(:,2), 'k')
hold on
scatter(lon,lat,3,chl)
axis([min(lon)-0.1 max(lon)+0.1 min(lat)-0.1 max(lat)+0.1])
title('chlorophyll [\mug/l]')
hold off
colorbar

subplot(2,2,2)
plot(coast_xy(:,1),coast_xy(:,2), 'k')
hold on
scatter(lon(I),lat(I),3,cp_amp)
axis([min(lon)-0.1 max(lon)+0.1 min(lat)-0.1 max(lat)+0.1])
title('c_p(650) [m^{-1}]')
hold off
colorbar

subplot(2,2,3)
plot(coast_xy(:,1),coast_xy(:,2), 'k')
hold on
scatter(lon(I),lat(I),3,gamma)
axis([min(lon)-0.1 max(lon)+0.1 min(lat)-0.1 max(lat)+0.1])
title('PSD-slope \gamma')
hold off
colorbar

subplot(2,2,4)
plot(coast_xy(:,1),coast_xy(:,2), 'k')
hold on
scatter(lon,lat,3,T)
axis([min(lon)-0.1 max(lon)+0.1 min(lat)-0.1 max(lat)+0.1])
title('Temperature [C]')
hold off
colorbar

%%%%%%%%%%%%%%%% San Bernardino SB West %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all

load ('C:\Documents and Settings\Emmanuel\Desktop\PhilEx\Data_2008\ShipData_2009\ph2_met')
load ('C:\Documents and Settings\Emmanuel\Desktop\PhilEx\Data_2008\Pre-Cruise\Bathymetry\coast_xy_b.txt')
coast_xy=coast_xy_b;load ./Datasets_2009/SanBernardino_D_ACS_apcp

I=find(acs_cp.time>733824.1382 & acs_cp.time<733826.1042);

time=acs_cp.time(I);
lat = interp1(met.time,met.lat, acs_cp.time(I), 'nearest');
lon = interp1(met.time,met.lon, acs_cp.time(I), 'nearest');
T = interp1(met.time,met.sst1, acs_cp.time(I), 'nearest'); %in-line temperature
S = interp1(met.time,met.tsal, acs_cp.time(I), 'nearest'); %in-line Salinity

ap=acs_ap.mean(I,:);
ap_wl=acs_ap.wavel(:);
cp=acs_cp.mean(I,:);
cp_wl=acs_cp.wavel(:);
chl=(ap(:,60)-ap(:,54)*43.5/71.1)/0.014;

opts = optimset('fminsearch');
opts = optimset(opts,'MaxIter',4000);
opts = optimset(opts,'MaxFunEvals',2000); % usually 100*number of params
opts = optimset(opts,'TolFun',1e-9);
opts = optimset('LevenbergMarquardt','on');

%guess for paramters (amplitude at 532 and slope)
x0=[0.2, 1];
I1=find(cp_wl>450)
%minimization routine
I=find(isfinite(cp(:,20)));
for i=1:length(I)
    x1 = fminsearch(@least_squares_cp,x0,opts,cp(I(i),I1)',cp_wl(I1));
    gamma(i)=x1(2);
    cp_amp(i)=x1(1);
end
save SB_west.mat cp_amp gamma chl cp_amp ap ap_wl cp cp_wl time lat lon T S
subplot(2,2,1)
plot(coast_xy(:,1),coast_xy(:,2), 'k')
hold on
scatter(lon,lat,3,chl)
axis([min(lon)-0.1 max(lon)+0.1 min(lat)-0.1 max(lat)+0.1])
title('chlorophyll [\mug/l]')
hold off
colorbar

subplot(2,2,2)
plot(coast_xy(:,1),coast_xy(:,2), 'k')
hold on
scatter(lon(I),lat(I),3,cp_amp)
axis([min(lon)-0.1 max(lon)+0.1 min(lat)-0.1 max(lat)+0.1])
title('c_p(650) [m^{-1}]')
hold off
colorbar

subplot(2,2,3)
plot(coast_xy(:,1),coast_xy(:,2), 'k')
hold on
scatter(lon(I),lat(I),3,gamma)
axis([min(lon)-0.1 max(lon)+0.1 min(lat)-0.1 max(lat)+0.1])
title('PSD-slope \gamma')
hold off
colorbar

subplot(2,2,4)
plot(coast_xy(:,1),coast_xy(:,2), 'k')
hold on
scatter(lon,lat,3,T)
axis([min(lon)-0.1 max(lon)+0.1 min(lat)-0.1 max(lat)+0.1])
title('Temperature [C]')
hold off
colorbar