disp('timatch_5_sjohnson_05')
fclose('all');

clear ztsg zt zsnk zo y x w vv vrsp uu usr un5 un1 un ue5 ue1 ue ts_tsg ts_snk ts tn tm_s tm_ot tm_n tm_in tcol taub ta_im ta_etl ta t sogy sogx
clear sogm5 sog sody5 sody sodx5 sodx sm skip_it sig_sp shp_spd shp_hed sal_tsg s3 s1 rh_im rh_etl rf relsp reldir rdcon qs qcol qa_im qa_etl qa psp_im psp1 psp prt_it prt_f pir1 pir org odec5 ode npz
clear np n minn m lrgsm lrgs5 lrgs lrgm5 lrgcm lrgc5 lrgc lrg lon lm lat k jl jk jj jg jf jdx jd jazz jazy5 jazy1 jazy jazx5 jazx1 jazx jaz5 jaz1 jaz jax jaq jadson jadscs jadmns jad5 
clear jad0 j imuy imux imum5 imu imdm5 imd ii i hsb hr hnet hm hlb g frac flist3 flist2 flist f e dt dir1 d cogm5 cog c by bx ay ax ans V U Sm Rns
clear Rnl Lm Dt

%*********************
prt_f=1;    %set to one to print data to file
bangit=0;   %set to zero variables
%*********************

skip_it=0;
if skip_it==0
%jaz=zeros(15,1440);
%jazx=zeros(6,1440);
%jazy=zeros(9,1440);

jd=num2str(ddd);
if ddd<100
   jd=['0' num2str(ddd)];
end;
if ddd<10
   jd=['00' num2str(ddd)];
end;

f=['d:\data\proc_files\proc_son' jd  '.txt'];
flist=fopen(f,'r');
jazx=fscanf(flist,'%g %g %g %g %g %g',[6,inf]);

%e=['d:\data\proc_files\proc_scs' jd  '.txt'];
%flist2=fopen(e,'r');
%jaz=fscanf(flist2,'%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g',[15,inf]);

g=['d:\data\proc_files\proc_pc' jd  '.txt'];
flist3=fopen(g,'r');
jazy=fscanf(flist3,'%g %g %g %g %g %g %g %g %g %g %g',[11,inf]);

end;

zsnk=ones(1,288);ztsg=ones(1,288);

%**********   not needed for revelle data  
tm_s=[360.00    366];		%time entering S lat. from Jasmine
tm_n=[300  300];

[ax,bx]=size(jazx);			%jazx
%[c,d]=size(jaz);				%jaz
[ay,by]=size(jazy);
tn=60*24;														%number of minutes in a day
n=1:tn;
jad0=str2num(jd)+(n-1)/24/60;jad0(tn)=jad0(tn)+1e-5;		%	don't ask
jadson=jazx(1,1:bx);
%jadscs=jaz(1,1:d);
jadmns=jazy(1,1:by);

%for ii=2:length(jadscs);
%   if jadscs(ii)<0,jadscs(ii)=jadscs(ii-1)+.000001;end;
%end;

%%%%%%%%**********   sonic loop
clear jazx1;
%fill in beginning empty spots 
jk=1;
while jad0(jk)<jadson(1),
   jazx1(:,jk)=jazx(:,1);
   jk=jk+1;
end;

for i=jk:tn;
   t=find(jadson>jad0(i));
   if ~isempty(t),
      j=t(1);
   else
      j=i;
   end;
   dt=jadson(j)-jadson(j-1);
   Dt=jad0(i)-jadson(j-1);
   jazx1(:,i)=jazx(:,j-1)*(1-Dt/dt)+jazx(:,j)*Dt/dt;
   if j==bx;
      break;
   end;
end;

jk=tn;
while jad0(jk)>=jadson(bx),
   jazx1(:,jk)=jazx(:,bx);
   jk=jk-1;
end;

%%%%%%%%*********  means loop
clear jazy1;
%fill in beginning empty spots 
jk=1;
while jad0(jk)<jadmns(1),
   jazy1(:,jk)=jazy(:,1);
   jk=jk+1;
end;

%for i=jk:tn;
%   t=find(jadmns>jad0(i));
%   if length(t)>0,
%      j=t(1);
%   else
%      j=2;
%   end;
%   dt=jadmns(j)-jadmns(j-1);Dt=jad0(i)-jadmns(j-1);
%   jazy1(:,i)=jazy(:,j-1)*(1-Dt/dt)+jazy(:,j)*Dt/dt;
%   if j==by;break;end;
%end;
%jk=tn;
%while jad0(jk)>=jadmns(by),
%   jazy1(:,jk)=jazy(:,by);
%   jk=jk-1;
%end;

%%%%%%%%%%%%%****************   ship data loop
%first setup to interpolate fricking angles

cog=gpshead;
sog=gpsspeed*.514;  %Convert from knots to m/s
lrg=Heading;
%imu=jaz(12,:);
%imd=jaz(13,:);

iic=find(sog<0 | sog>10);cog(iic)=NaN;sog(iic)=NaN;lrg(iic)=NaN;
%iid=find(imu<0 | imu>50);imu(iid)=NaN;imd(iid)=NaN;

rdcon=pi/180;
jaz(4,:)=sog.*cos(cog*rdcon);			%sogx
jaz(5,:)=sog.*sin(cog*rdcon);			%sogy
%jaz(12,:)=imu.*cos(imd*rdcon);		%imux
%jaz(13,:)=imu.*sin(imd*rdcon);		%imuy

lrgc=cos(Heading*rdcon);
lrgs=sin(Heading*rdcon);
lrgcm=cos(Heading*rdcon);
lrgsm=sin(Heading*rdcon);

clear jaz1;
												%fill in beginning empty spots 
jk=1;
%while jad0(jk)<jadson(1),
%   jaz1(:,jk)=jaz(:,1);
%   lrgcm(jk)=lrgc(1);
%   lrgsm(jk)=lrgs(1);
%   jk=jk+1;
%end;

%for i=jk:tn;
%   t=find(jadson>jad0(i));
%   if~isempty(t)
%      j=t(1);
%      dt=jadson(j)-jadson(j-1);Dt=jad0(i)-jadson(j-1);
%      jaz1(:,i)=jaz(:,j-1)*(1-Dt/dt)+jaz(:,j)*Dt/dt;
%      lrgcm(i)=lrgc(j-1)*(1-Dt/dt)+lrgc(j)*Dt/dt;
%      lrgsm(i)=lrgs(j-1)*(1-Dt/dt)+lrgs(j)*Dt/dt;
%   else
%      break;
%   end;
%end;

jk=tn;
jazy1=jazy;
%while jk > 0 & jad0(jk)>=jadson(bx),
%   jaz1(:,jk)=jaz(:,d);
%   lrgcm(jk)=lrgc(bx);
%   lrgsm(jk)=lrgs(bx);
%   jk=jk-1;
%end;

%************************   do 5 minute averages
jax=1440;
jk=288;
if ship=='RHB'
   ode=gpsspeed;
elseif (ship=='KAI' | ship=='REV')	%The Ka'imimoana doesn't have an ODEC.  Use SOG.
%   [dim1 dim2]=size(jaz1);
%   for i=1:dim2, ode(i)=sqrt(jaz1(5,i)*jaz1(5,i)+jaz1(6,i)*jaz1(6,i)); end;
   clear dim1 dim2 i
end;
sodx=sog.*lrgcm;
sody=sog.*lrgsm;
%sodx=odecgx.*lrgcm;  %Brown only
%sody=odecy.*lrgsm;   %Brown only
uu=jazx1(2,:)/.95;				%.95 is empirical distortion factor
vv=jazx1(3,:)/1.15;				%1.15 is other empirical distortion factor
un=-uu.*lrgcm-vv.*lrgsm;
ue=-uu.*lrgsm-vv.*lrgcm;
clear jaz5 jazy5 jazx5 lrgc5 lrgs5 sodx5 sody5 un5 ue5 vrsp jad5;
for j=1:jk
   jl=5*(j-1)+1;jj=min(jax,5*j);
   jazx5(:,j)=mean(jazx1(:,jl:jj)')';
   jazy5(:,j)=mean(jazy1(:,jl:jj)')';
%   jaz5(:,j)=mean(jaz1(:,jl:jj)')';
   lrgc5(j)=mean(lrgcm(jl:jj)')';
   lrgs5(j)=mean(lrgsm(jl:jj)')';
   sodx5(j)=mean(sodx(jl:jj)')';
   sody5(j)=mean(sody(jl:jj)')';
   un5(j)=mean(un(jl:jj)')';
   ue5(j)=mean(ue(jl:jj)')';
%   vrsp(j)=var(jaz1(5,jl:jj))+var(jaz1(6,jl:jj));
   jad5(j)=jad0(jl);
end;

%*********************  then uncomponentize angles

U=jazx5(2,:);										
V=jazx5(3,:);										
W=jazx5(4,:);
reldir=mod(atan2(-V,U)/rdcon+180+360,360);		%signs convert jeff coord to conventional
relsp=sqrt(U.*U+V.*V);

for iii=1:5:length(sodx);
   sogx=median(sodx(iii:iii+4));
   sogy=median(sody(iii:iii+4));
end;

imux=U;%imet true wind
imuy=V;%imet true wind

sogm5=sqrt(sogx.*sogx+sogy.*sogy);					%1-min sog
cogm5=mod(atan2(sogy,sogx)/rdcon+360,360);
imum5=sqrt(imux.*imux+imuy.*imuy);					%1-min imet speed, m/s
imdm5=mod(atan2(imuy,imux)/rdcon+360,360);
lrgm5=mod(atan2(lrgs5,lrgc5)/rdcon+360,360);			%laser ring gyro
odec5=sqrt(sodx5.*sodx5+sody5.*sody5);

%**********  do some true wind calcs
un1=un5+sogx;
ue1=ue5+sogy;     %!!!!!!!!!!!!!!!
s1=sqrt(un1.*un1+ue1.*ue1);
s3=sqrt(un1.*un1+ue1.*ue1+W.*W);
s_shp=sqrt(imux.*imux+imuy.*imuy);

dir1=mod(atan2(-ue1,-un1)*180/pi+360,360);%- sign converts to to from
dir_shp=mod(atan2(imuy,imux)*180/pi+360,360);
%****************   sea temps
ts_snk=jazy5(5,:)+.0;%correct tsnake
ts_tsg=ts_snk;
sal_tsg=ts_snk.*0;
ts=ts_tsg-0.36;zt=ztsg*5;%correct bias in tsg
iis=find(ts_tsg<10);ts_tsg(iis)=NaN;

ts=ts_snk;							%water temperature if snake is in
zt=zsnk*.05;						%depth of water temp measurement
qs=qsea(ts);

%%%%%************   air temps
ta_etl=jazy5(7,:)-0.05;%est correction for etl
ta_im=ta_etl;
iit=find(ta_im<10);ta_im(iit)=NaN;  
ta=ta_etl;
iita=find(isreal(ta_im) & ta_im-ta_etl>.25);ta(iita)=ta_im(iita);
%***************    relative hums
rh_etl=jazy5(8,:)+1;%est correct
rh_im=rh_etl;
iih=find(rh_im<0);rh_im(iih)=NaN;
qa_etl=qair([ta_etl' rh_etl'])';
qa_im=qair([ta_etl' rh_etl'])';
qa=qa_etl;
qa(iita)=qa_im(iita);

%**************   solar fluxes
psp1=jazy5(3,:);
psp_im=psp1;
psp=psp1;

%**************  IR fluxes
pir=jazy5(2,:);

%**************************
org=jazy5(9,:);
lat=gpslat;
lon=gpslon;
jjj=0;

for iii=1:5:length(lat);
    jjj=jjj+1;
    lat1(jjj)=median(lat(iii:iii+4));
    lon1(jjj)=median(lon(iii:iii+4));
end;
lat=lat1;lon=lon1;

[n,m]=size(tm_s);
jg=[];
for k=1:m
   jf=find(jad5>tm_s(k) & jad5<tm_n(k));					%find when south lat
   jg=[jg jf];
end;
lat(jg)=-lat(jg);

shp_spd=odec5;
shp_hed=lrgm5;

sig_sp=sqrt(1);

prt_f=0;
prt_f=1;
np=jk;

for k=1:jk
   %   u    0  ts  ta      qs     qa   rs      rl   rain     zi  p   zu    zt   zq
   x=[s1(k) 0 ts(k) ta(k) qs(k) qa(k) psp(k) pir(k) org(k) 600 1010 18.5 17.5 17.5 lat(k) 1 0 1 1];

   y=cor26_5(x);
   hsb(k)=y(1);
   hlb(k)=y(2);
   taub(k)=y(3);
   tcol(k)=y(11);
   qcol(k)=y(12);
   rf(k)=y(14);
   usr(k)=y(8);
   zo(k)=y(4);
end;
Rns=[];Rnl=[];hnet=[];
Rns=psp*.955;
Rnl=.97*(pir-5.67e-8*(ts_snk+273.15).^4);
hnet=Rns+Rnl-hsb-hlb-rf;
iihm=find(~isnan(hnet));
hm=mean(hnet(iihm));sm=mean(hsb(iihm));lm=mean(hlb(iihm));Sm=mean(Rns);Lm=mean(Rnl);
figure;plot(jad5,hnet);xlabel('Julian Day (2005)');ylabel('Net Heat Flux (W/m^2)');title(['Heat Fluxes:  Net= ',num2str(hm),'; H_l_a_t= ',num2str(lm),'; H_s_e_n_s= ',num2str(sm),'; R_s_h_o_r_t= ',num2str(Sm),'; R_l_o_n_g= ',num2str(Lm)]);

figure;plot(jad5,s1);xlabel('Julian Day (2005)');ylabel('True Wind Speed (m/s)');title('Relative to Earth=blue;   Relative to Ocean=green');
figure;plot(jad5,hsb, jad5, hlb,jad5, Rnl,jad5,rf,'.');xlabel('Julian Day (2005)');ylabel('Heat Flux Components (W/m^2)');title('H_s_e_n_s=blue;   H_l_a_t= green;   Rnet_l_o_n_g=red;   Rain=dots');

np=jk;
if prt_f==1;
  prt_altair_flux_5_04;
end;

clear ztsg zt zsnk zo y x w vv vrsp uu usr un5 un1 un ue5 ue1 ue ts_tsg ts_snk ts tn tm_s tm_ot tm_n tm_in tcol taub ta_im ta_etl ta t sogy sogx
clear sogm5 sog sody5 sody sodx5 sodx sm skip_it sig_sp shp_spd shp_hed sal_tsg s3 s1 rh_im rh_etl rf relsp reldir rdcon qs qcol qa_im qa_etl qa psp_im psp1 psp prt_it prt_f pir1 pir org odec5 ode npz
clear np n minn m lrgsm lrgs5 lrgs lrgm5 lrgcm lrgc5 lrgc lrg lon lm lat k jl jk jj jg jf jdx jd jazz jazy5 jazy1 jazy jazx5 jazx1 jazx jaz5 jaz1 jaz jax jaq jadson jadscs jadmns jad5 
clear jad0 j imuy imux imum5 imu imdm5 imd ii i hsb hr hnet hm hlb g frac flist3 flist2 flist f e dt dir1 d cogm5 cog c by bx ay ax ans V U Sm Rns
clear Rnl Lm Dt
