yy=[];dta=10;
x1=load('c:\data\cwf\Dropbox\DATA\dynamo\files\DYNAMO_2011_PSD_flux_5min_leg1.txt');%read file with 10min 
jdy=x1(:,1);[jdav1,y1,yvar1]=timave_2_nonan(jdy,dta,x1);
x2=load('c:\data\cwf\Dropbox\DATA\dynamo\files\DYNAMO_2011_PSD_flux_5min_leg2.txt');%read file with 10min 
jdy=x2(:,1);[jdav2,y2,yvar1]=timave_2_nonan(jdy,dta,x2);
x3=load('c:\data\cwf\Dropbox\DATA\dynamo\files\DYNAMO_2011_PSD_flux_5min_leg3.txt');%read file with 10min 
jdy=x3(:,1);[jdav3,y3,yvar1]=timave_2_nonan(jdy,dta,x3);
x4=load('c:\data\cwf\Dropbox\DATA\dynamo\files\DYNAMO_2011_PSD_flux_5min_leg4.txt');%read file with 10min 
jdy=x4(:,1);ii=find(jdy>200);[jdav4,y4,yvar1]=timave_2_nonan(jdy(ii),dta,x4(ii,:));
yy=[y1' y2' y3' y4']';

ii=find(~isnan(yy(:,2)));x=yy(ii,:);
jdy=x(:,1);%julian day at beginning of time average
U=x(:,2);%true wind,PSD sonic (m/s)
dir=x(:,3);%true wind direction, PSD sonic (deg)
tsnk=x(:,4);%sea snake temperature,PSD, 0.05 m depth (C)
tsg=x(:,5);%tsg water temperature, 5 m depth, (C)
sal=x(:,6);%tsg salinity (psu)
ta=x(:,7);%air temperature, PSD (C)
qse=x(:,8);%sea surface specific humidity,from snake (g/kg)
qa=x(:,9);%air specific humidity, PSD (g/kg)
rs=x(:,10)*1.01;%downward solar flux, PSD units (W/m^2)
rl=x(:,11);%downward IR flux,  PSD units (W/m^2)
org1=x(:,12);%rainrate, PSD STI optical rain gauge, uncorrected (mm/hr)
ushp=x(:,13);%doppler log, SCS (m/s)
head=x(:,14);%ship heading, deg clockwise rel north, SCS laser ring gyro (deg)
urel=x(:,15);%relative wind speed, PSD (m/s)
reldir=x(:,16);%relative wind direction (from),clockwise rel ship's bow, PSD sonic (deg)
lat=x(:,17);%latitude, deg  (SCS pcode)
lon=x(:,18);%longitude, deg (SCS pcode)
zts=x(:,19);%depth for bulk flux Ts reference, =0.05 when snake is used
sig_u=x(:,20);%std dev of ship speed, m/s (>.2 indicates maneuver during half hour average)
taub=x(:,21);%bulk wind stress along mean wind, (N/m^2)
hsb=x(:,22);%bulk sensible heat flux, (W/m^2)
hlb=x(:,23);%bulk latent heat flux, W/m^2 (includes Webb et al. correction)
hrain=x(:,24);%rain heat flux, as per Gosnell et al 1995, JGR, 18437-18442, (W/m^2)
ta_im=x(:,25);%ship IMET air temp @21 m (degree C)
qa_im=x(:,26);%ship IMET air specific humidity (g/kg)
s_ship=x(:,27);%ship speed over ground (m/s)
dir_ship=x(:,28);%ship gyro heading (degrees)
psp_ship=x(:,29);%ship IMET pyranometer (solar flux, W/m^2)
pir_ship=x(:,30);%ship IMET pirgeometer (IR flux, W/m^2)
barpress=x(:,31);%IMET barometric pressure (hPa)
RH=x(:,32);%PSD RHvais, %
rh_ship=relhum5([ta_im qa_im barpress]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  clean up PSD data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ii=find(tsg>35 | tsg<25 );tsg(ii)=tsg(ii-1);
ii=find(tsnk>35 | tsnk<25 | abs(tsnk-tsg)>5 | isnan(tsnk));tsnk(ii)=tsg(ii);
ii=find(jdy>274);tsnk(ii(1):ii(1)+11)=tsg(ii(1):ii(1)+11);
ii=find(jdy>311);tsnk(ii(1):ii(1)+40)=tsg(ii(1):ii(1)+40);
ii=find(jdy>350);tsnk(ii(1):ii(1)+49)=tsg(ii(1):ii(1)+49);
ii=find(jdy>248 & jdy<256 & abs(tsnk-tsg)>1 );tsnk(ii)=tsg(ii);
ii=find(rl<390);rl(ii)=400;
qa=qa./.96;
RH=RH/.95;ii=find(jdy>243.5 & jdy<245.4 | RH>120);RH(ii)=rh_ship(ii);

%%%%%Recompute Fluxes
rh=RH;
P=barpress;
ts=tsnk;
zu_etl=17.5;zt_etl=15.5;zq_etl=15.5;zi=600;
%%%%%%%%%%%%   set heights for profiles
zp=[15.5 30.5 74.4 99.4 150 200];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

ii=1;
zref_u=zp(ii);zref_t=zref_u;zref_q=zref_u;
A=[];A=coare35vn_cool_refa(U,zu_etl,ta,zt_etl,rh,zq_etl,P,tsnk,rs,rl,lat,zi,org1,NaN,NaN,zref_u,zref_t,zref_q);
%A=[usr tau hsb hlb hbb hsbb hlwebb tsr qsr zou zot zoq Cd Ch Ce  L zet ug dter dqer tkt Urf Trf Qrf RHrf UrfN Rnl Le rhoa UN U10 U10N Cdn_10 Chn_10 Cen_10 RF Qs Evap];
%   1   2   3   4   5   6    7      8   9    10  11  12 13 14 15  16 17 18  19  20  21  22  23   24   25 26  27  28  29  30    31     32     33   34 35  36
usb=A(:,1);
taub=A(:,2);
hsb=A(:,3);
hlb=A(:,4);
usr=A(:,1);
tsr=A(:,8);
qsr=A(:,9);
zou=A(:,10);
zot=A(:,11);
zoq=A(:,12);
L=A(:,16);
Cd=A(:,13);
Ce=A(:,14);
ug=A(:,18);
Gs=sqrt(U.^2+ug.^2)./U;
U10n=usr./.4./sqrt(Gs).*log(10./zou);
DT10n=-tsr./.4.*log(10./zot);
Dq10n=-qsr./.4.*log(10./zoq);
%%%%%%%%%%%%%%%%%%%%%  save profile data
uref(:,ii)=A(:,22);%wind speed
Tref(:,ii)=A(:,23);%air temp
qref(:,ii)=A(:,24);%air spec humid
RHref(:,ii)=A(:,25);%air RH

for ii=2:length(zp)
    
zref_u=zp(ii);zref_t=zref_u;zref_q=zref_u;
A=[];A=coare35vn_cool_refa(U,zu_etl,ta,zt_etl,rh,zq_etl,P,tsnk,rs,rl,lat,zi,org1,NaN,NaN,zref_u,zref_t,zref_q);
%A=[usr tau hsb hlb hbb hsbb hlwebb tsr qsr zou zot zoq Cd Ch Ce  L zet ug dter dqer tkt Urf Trf Qrf RHrf UrfN Rnl Le rhoa UN U10 U10N Cdn_10 Chn_10 Cen_10 RF Qs Evap];
%   1   2   3   4   5   6    7      8   9    10  11  12 13 14 15  16 17 18  19  20  21  22  23   24   25 26  27  28  29  30    31     32     33   34 35  36
%%%%%%%%%%%%%%%%%%%%%  save profile data
uref(:,ii)=A(:,22);
Tref(:,ii)=A(:,23);
qref(:,ii)=A(:,24);
RHref(:,ii)=A(:,25);
end;


k=100;figure;semilogy(uref(k,:),zp);xlabel('Wind Speed (m/s)');ylabel('height (m)');
figure;plot(jdy,uref(:,4)-U);xlabel('Year Day (2010)');ylabel('U99-U17 (m/s)');
