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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Select day in Feb  07, 08, 11, 12, 13, 14,
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  15, 18, 19, 21, 22, 24
prtit=1;

%xdir1=dir(['C:\data\cwf\Dropbox\DOC\CLIMATE\CalWater2\aircraft\dynamo_dropsonde_data\dynamo_dropsonde_data\']);
 ddds=input('Input yearday ddd ','s')

way_raw_images=['E:\DATA\ATOMIC\P3\sonde\'  ddds '\Raw_images\'];
way_raw_files=['E:\DATA\ATOMIC\P3\sonde\' ddds '\data\'];


i=1;tim=[];chx=0;
%ydir=dir(['E:\DATA\ATOMIC\P3\sonde\']);
jm=1+2;
xdir=dir(['E:\DATA\ATOMIC\P3\sonde\' ddds '\frd\*.frd']);
zh=[];rhah=[];Uh=[];tah=[];lath=[];lonh=[];Ph=[];qah=[];jdx=[];
zhh=[];rhahh=[];Uhh=[];tahh=[];lathh=[];lonhh=[];Phh=[];qahh=[];jdxh=[];

for kkk=1:length(xdir);
    fnm=xdir(kkk).name;
    position=findstr(fnm,'frd')-4;
    if ~isempty(position)
        if (fnm(position-4:position-2)=='Fix') | (fnm(position-4:position-2)=='FIX')
            chx=-4;
        else
            chx=0;
        end;
        tim(i,1)=str2num(fnm(position-16+chx:position-13+chx));%year
%         if length(fnm)>27;position=position-4;end;
        tim(i,2)=str2num(fnm(position-12+chx:position-11+chx));%month
        tim(i,3)=str2num(fnm(position-10+chx:position-9+chx));%day
        tim(i,4)=str2num(fnm(position-7+chx:position-2+chx));
        dname(i,1:13)=fnm(position-16+chx:position-4+chx);
        dindex(i)=kkk;
        i=i+1;
    end;
end;
jk=i-1
%zh=ones(100,52)*NaN;;tah=ones(100,52)*NaN;;lath=ones(1,52)*NaN;lonh=ones(1,52)*NaN;
zh=ones(4000,470)*NaN;;Uh=ones(4000,470)*NaN;rhah=ones(4000,470)*NaN;tah=ones(4000,470)*NaN;;lath=ones(1,470)*NaN;lonh=ones(1,470)*NaN;Ph=ones(4000,470)*NaN;;qah=ones(4000,470)*NaN;udirh=ones(4000,470)*NaN;

%rhahh=[];Uhh=[];zhh=[];tahh=[];lathh=[];lonhh=[];qahh=[];Phh=[];jdxh=[];
%sonde=load(['c:\data\cwf\Dropbox\DOC\CLIMATE\CalWater2\aircraft\G4_2014\sondedata\Summary_Data_dZ_10_feb' num2str(dtt) '_nohd.txt']);
%sonde=load(['c:\data\cwf\Dropbox\DOC\CLIMATE\CalWater2\aircraft\G4_2014\sondedata\Summary_Data_dZ_0_feb' num2str(dtt) '_nohd.txt']);
mmm=NaN;
for j=1:jk
sondex=['E:\DATA\ATOMIC\P3\sonde\' ddds '\frd\' xdir(dindex(j)).name ];
[sonde] =  fast_textread(sondex, 19, 21, 0);%dropsonde_avaps2struct(sondex);
i=j;

%IX	     t_s	P_mb	T_C	   RH	    Z_m   	WD	  WS_m_s	U_m_s	V_m_s	  NS	WZ_m_s	        ZW_m	  FP	FT	 FH	   FW	LAT_N	LON_E
%  1       2     3       4       5        6       7      8       9       10       11        12          13        14    15   16   17    18    19

yr=tim(i,1);
mo=tim(i,2);
dy=tim(i,3);
hr=floor(tim(i,4)/1e4);
mn=floor((tim(i,4)/1e2-hr*1e2));
jdy=datenum(yr,mo,dy,hr,mn,0)-datenum(2010,12,31);
dtt=dy;

% dtt=tim(i);
% yr=axbtdate(1:4);
% mo=axbtdate(6:7);
% dy=axbtdate(9:10);
% hr=axbtdate(12:13);
% mn=axbtdate(15:16);
% jdy=datenum(str2num(yr),str2num(mo),str2num(dy),str2num(hr),str2num(mn),0)-datenum(2010,12,31);

% dtt=tim(i,4);mo=tim(i,2);
% hr=floor(tim(i,2)/1e4);
% mn=floor((tim(i,2)/1e2-hr*1e2));
jdy=datenum(2014,mo,(dtt),hr,mn,0)-datenum(2013,12,31);

Pp=sonde.var3;
zp=sonde.var6;
ii=find(Pp>0 & zp>1);
P=Pp(ii);
tsec=sonde.var2(ii);
ta=sonde.var4(ii);jj=find(ta==-999);ta(jj)=NaN;
rh=sonde.var5(ii);jj=find(rh==-999);rh(jj)=NaN;
z=sonde.var6(ii);
udir=sonde.var7(ii);jj=find(udir==-999);udir(jj)=NaN;
U=sonde.var8(ii);jj=find(U==-999);U(jj)=NaN;
Un=sonde.var9(ii);
Ue=sonde.var10(ii);
NS=sonde.var11(ii);
W=sonde.var12(ii);
lat=sonde.var18(ii);jj=find(lat==-999);lat(jj)=NaN;
lon=sonde.var19(ii);jj=find(lon==-999);lon(jj)=NaN;
jdx(:,i)=jdy;

A=[];B=[];
if ii>0
qa=qair_p3([ta P rh/100]);
rh_ice=qa./qice([ta,P])*100;
rha=rh;ii=find(ta<0);rha(ii)=rh_ice(ii);
ii=length(z);
A(1:ii,1)=z;
A(1:ii,2)=U;
A(1:ii,3)=rha;
A(1:ii,4)=ta;
A(1:ii,5)=lat;
A(1:ii,6)=lon;
A(1:ii,7)=qa;
A(1:ii,8)=P;
A(1:ii,9)=udir;

%
    B=sortrows(A,1);
    [ii ij]=size(B);
    zh(1:ii,i)=B(:,1);
    Uh(1:ii,i)=B(:,2);
    rhah(1:ii,i)=B(:,3);
    tah(1:ii,i)=B(:,4);
    xj=min(20,ii);
    lath(i)=nanmedianx(B(1:xj,5));
    lonh(i)=nanmedianx(B(1:xj,6));
    qah(1:ii,i)=B(:,7);
    Ph(1:ii,i)=B(:,8);
    udirh(1:ii,i)=B(:,9);
    figure;plot(ta,z);ylabel('Depth (m)');xlabel('T (C)');title(['ATOMIC P3 Drop# ' num2str(i) ' ' num2str(mo) '/' num2str(dy) ' '  num2str(hr)]);%ylim([0 3]);
    if prtit;print('-djpeg90 ',[way_raw_images 'T Profile' dname(i,:) ' .jpg']);close all;end;
end;
end;

jk=length(jdx);
rhahh=[rhah(1:4000,1:jk)];
zhh=[ zh(1:4000,1:jk)];
Uhh=[ Uh(1:4000,1:jk)];
tahh=[ tah(1:4000,1:jk)];
lathh=[lath(1:jk)];
lonhh=[lonh(1:jk)];
Phh=[Ph(1:4000,1:jk)];
qahh=[qah(1:4000,1:jk)];
udirhh=[udirh(1:4000,1:jk)];
jdxh=[jdx(1:jk)];

if prtit;
        save([way_raw_files 'ATOMIC_P3_sonde_' ddds '.mat'], 'jdxh', 'zhh','Uhh','rhahh','tahh','lathh','lonhh','Phh','qahh','udirhh');
        %save([way_raw_files 'CALWATER2_P3_sonde_' ydir(jm).name '.txt'], '-ascii','-tabs','jdxh', 'zhh','Uhh','rhahh','tahh','lathh','lonhh','Phh','qahh');
end;


Nx=[];zhmn=[];qamn=[];Umn=[];rhmn=[];Pmn=[];tamn=[];
Nt=[];zhmntp=[];qamntp=[];Umntp=[];rhmntp=[];Pmntp=[];tamntp=[];
latline=14.0;
latline=-56;
for i=1:4000;
    ik=find(~isnan(rhahh(i,:)) & lonhh>=latline);
    Nx(i)=length(ik);
    if Nx(i)>2;
        zhmn(i)=nanmedian(zhh(i,ik));
        qamn(i)=nanmeanx(qahh(i,ik));
        Umn(i)=nanmeanx(Uhh(i,ik));
        tamn(i)=nanmeanx(tahh(i,ik));
        rhmn(i)=nanmeanx(rhahh(i,ik));
        Pmn(i)=nanmeanx(Phh(i,ik));                
    end;
    ik=find(~isnan(rhahh(i,:)) & lonhh<latline);
    Ny(i)=length(ik);
    if Ny(i)>2;
        zhmntp(i)=nanmedian(zhh(i,ik));
        qamntp(i)=nanmeanx(qahh(i,ik));
        Umntp(i)=nanmeanx(Uhh(i,ik));
        tamntp(i)=nanmeanx(tahh(i,ik));
        rhmntp(i)=nanmeanx(rhahh(i,ik));
        Pmntp(i)=nanmeanx(Phh(i,ik));                
    end;
   
    rhmx(i)=max(rhahh(i,:));

end;%figure;plot(rhmx(1:length(zhmn)),zhmn,'.')
close all;
figure;plot(rhmn(1:length(zhmn)),zhmn,'.',rhmntp(1:length(zhmntp)),zhmntp,'o',rhmx(1:length(zhmn)),zhmn,'o');legend(['Lon> ' num2str(latline)],['Lon<', num2str(latline)],'Max');xlabel('RH (%)');ylabel('Alt (m)');title(['Mean RH ATOMIC P3 Day ' ddds]); 
if prtit;print('-djpeg90 ',[way_raw_images 'Mean RH Profile' ddds '.jpg']);end;

%filecat([way_raw_files 'CALWATER2_P3_sonde_*.mat'],[way_raw_files 'data\CALWATER2_P3_sonde_all.mat']);
% 
% xdir2=dir([way_raw_files '*.mat']);xdir2.name
% for i=1:5
%     fnm=xdir2(i).namelen
%     B=load([way_raw_files fnm]);
%end;
% 
