%%%%%%%%%%%%%%   GEt satellite data from Feb 12

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Select day in Feb  07, 08, 11, 12, 13, 14,
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  15, 18, 19, 21, 22, 24
prtit=0;
graphformat='.jpg';  %select graphics format files
graphdevice='-djpeg'; %select graphic device


%xdir1=dir(['C:\data\cwf\Dropbox\DOC\CLIMATE\CalWater2\aircraft\dynamo_axbt_data\mat']);
%


way_raw_images='E:\DATA\ATOMIC\P3\axbt\figs\';
way_raw_files='E:\DATA\ATOMIC\P3\axbt\data\';

zdir=dir([way_raw_files '*.mat']);
jdxhc=[];lathhc=[];lonhhc=[];tahhc=[];zhhc=[];fall_tc=[];
m=7;
for i=1:m
    load([way_raw_files zdir(i).name]);
    [m,n]=size(tahh);
    tahhx=tahh;
        for j=1:n
          ii=find(tahh(:,j)<20 & zhh(:,j)<100);tahh(ii,j)=NaN;tahhx(ii,j)=NaN;%find bad near surface values
          kk=find(zhh(:,j)>200);%[j tahh(kk(1),j)];
          if ~isempty(kk) & tahh(kk(1),j)>25;%slide up profile if missing depths
              jj=find(~isnan(tahh(:,j)));
              [j jj(1)]
              nn=1:7000-jj(1)+1;mm=jj(1):7000;
              tahhx(nn,j)=tahh(mm,j);
              zhh(:,j)=zhh(:,j+1);
          end;
          %figure;plot(tahh(:,j),zhh(:,j),'.');
        end;
  
    figure;plot(nanmeanx(tahh'),nanmeanx(zhh'),'.');xlabel('T (C)');ylabel('Depth (m)');title(['ATOMIC AXBT Mean T profile Day ' zdir(i).name(9:11)]);ylim([0 300]);grid; 
    if prtit; print(graphdevice,[way_raw_images 'ATOMIC_AXBT_Tprof_' zdir(i).name(9:11) graphformat]); end;
    
    jdxhc=[jdxhc jdxh];
    lathhc=[lathhc lathh];
    lonhhc=[lonhhc lonhh];
    tahhc=[tahhc tahh];
    zhhc=[zhhc zhh];
    fall_tc=[fall_tc fall_t]; 
        
end;

%ydir=dir(['Z:\CALWATER_2015\P3\P3*']);

ii=find(~isnan(tahhc(3,:)));y=tahhc(3,ii)';
x=[ones(size(y)) lathhc(ii)' lonhhc(ii)'];
a=x\y;
xx=x*a;

figure;
subplot(2,1,1);plot(lathhc,tahhc(3,:),'o');xlabel('Latitude (deg)');ylabel('SST (C)');ylim([25 28]);grid;
title('ATOMIC AXBT SST');
subplot(2,1,2);plot(lonhhc,tahhc(3,:),'o');xlabel('Longitude (deg)');ylabel('SST (C)');ylim([25 28]);grid;
ts=tahhc(3,:);ii=find(tahhc(3,:)<25.5);ts(ii)=NaN;
figure;scatter(lonhhc,lathhc,20,ts);colorbar;xlabel('Longitude (Deg)');ylabel('Latitude(Deg)');title(['ATOMIC AXBT SST ']); 
t200=[];
for i=1:length(jdxhc)
    k=find(zhhc(:,i)>200);
    if ~isempty(k)
        t200(i)=tahhc(k(1),i);
    else
        t200(i)=NaN;
    end;
end;
    
figure;scatter(lonhhc,lathhc,20,t200);colorbar;xlabel('Longitude (Deg)');ylabel('Latitude(Deg)');title(['ATOMIC AXBT T200 ']); 

%save([way_raw_files 'ATOMIC_AXBT_data_all.mat'], 'jdxhc','lathhc','lonhhc','tahhc','zhhc','fall_tc');
%save([way_raw_files 'ATOMIC_SST_data.mat'], 'jdxhc','ts','lathhc','lonhhc');

% %%%%%%%%  write as a csv file
 mk=length(jdxhc);
 P3data1=[];
 P3data1=zeros(4,mk);
 P3data1(1,:)=jdxhc;
 P3data1(2,:)=ts;
 P3data1(3,:)=lathhc;
 P3data1(4,:)=lonhhc;
 P3data1=P3data1';
 csvwrite([way_raw_files 'ATOMIC_SST_data.dat'], P3data1);

