disp('read_shp_day')
fclose('all');
clear all;
jd=input('julian day, ddd  ','s');
plotit=input('plot plots? 0=n, 1=y  ');
prtit=input('append master data file? 0=n, 1=y  ');
hr='00';
g=0.0098;							%adiabatic lapse rate
eps_w=0.97;							%emissivity of water
sig_sb=5.67e-8;					%Stefan Boltzmann constant
blk=1;								%10-min index 
jax=1;
jaxx=1;
backchk=0;
st1=zeros(17,1800);
st2=zeros(17,4500);
for jam=0:23,						%cycle thru 24 hourly stats files
   if jam<10,
      hr=['0'num2str(jam)];
   else
      hr=num2str(jam);
   end;
   e=['d:\hp_data\day',jd,'\P6_' jd hr];
   disp(e)
   flist=fopen(e,'r');
   if flist>0,   					%if the file exists, 
      if backchk==0,
         tzilch=jam;
         backchk=1;
      end;
      dmp=fgets(flist);			%read header lines and skip blanks
      dmp2=fgets(flist);		%read header lines and skip blanks
	   while feof(flist)==0,	%go until you run out of 10-min sections
         st1=fscanf(flist,'%g,%g,%g,%g%*c,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,',[17,inf]);
         [nr1,nl1]=size(st1);
			if nr1==17
      		st2(:,jax:jax+nl1-1)=st1;
      		jax=jax+nl1;
			else
   			x=fgets(flist);
			end;
      end;
   end;
end;

cog=st2(6,:);
sog=st2(7,:);
lrg=st2(14,:);
imu=st2(16,:);
imd=st2(17,:);
jk=ceil(jax/30);
st3=zeros(17,jk);
rdcon=pi/180;
st2(6,:)=sog.*cos(cog*rdcon);
st2(7,:)=sog.*sin(cog*rdcon);
st2(16,:)=imu.*cos(imd*rdcon);
st2(17,:)=imu.*sin(imd*rdcon);
lrgc=cos(lrg*rdcon);
lrgs=sin(lrg*rdcon);
jade=str2num(num2str(jd))+(tzilch+(st2(1,:)-st2(1,1))/60/60)/24;

for j=1:jk
   jl=30*(j-1)+1;jj=min(jax-1,30*j);
%   st3(:,j)=median(st2(:,jl:jj)')';
   st3(:,j)=median(st2(:,jl:jj)')';
   jad(j)=jade(jl);
   lrgcm(j)=median(lrgc(jl:jj));
   lrgsm(j)=median(lrgs(jl:jj));
end;

sogx=st3(6,:);
sogy=st3(7,:);
imux=st3(16,:);
imuy=st3(17,:);
sogm=sqrt(sogx.*sogx+sogy.*sogy)*.514;				%1-min sog
cogm=mod(atan2(sogy,sogx)/rdcon+360,360);
imum=sqrt(imux.*imux+imuy.*imuy)*.514;				%1-min imet speed, m/s
imdm=mod(atan2(imuy,imux)/rdcon+360,360);
lrgm=mod(atan2(lrgsm,lrgcm)/rdcon+360,360);		%laser ring gyro

gpstim=st3(3,:);
lat=st3(4,:);
lon=st3(5,:);
%
tsg=st3(8,:);
tss=st3(9,:);
imrh=st3(10,:);
imta=st3(11,:);
imsol=st3(12,:);
imrain=st3(13,:);
%
odec=st3(15,:);
jad=str2num(num2str(jd))+(tzilch+(st3(1,:)-st2(1,1))/60/60)/24;
np=jk;

if prtit==1;
  prt_jas_scs;
end;

if plotit==1;
%  set some plot limits
   Tll=-25;
   hll=70;
   uul=10;
   vul=10;
   Tul=-0;
   hul=100;
   sul=12;
   Rlul=30;
   Rlll=-100;
   Rtul=300;
   Rtll=125;
   Rsul=1000;
   Rsll=0;

   j_st=jad(1);
   j_en=jad(np);if j_en<0,j_en=jad(np-1);end;
   figure;plot(jad(1:np),imta(1:np),jad(1:np),tsg(1:np));xlabel('Jday (1999)');ylabel('Temperature (C)');Title('SCS Tair=blu; Tsea=grn');
   figure;plot(jad(1:np),sogm(1:np),jad(1:np),odec(1:np));xlabel('Jday (1999)');ylabel('SCS Ship Speed (m/s)');Title('GPS=blu; odec=grn'); 
   figure;plot(jad(1:np),imsol(1:np));xlabel('Jday (1999)');ylabel('Psp_imet (W/m^2)');Title('SCS Downward Solar Flux: Imet'); axis([j_st j_en Rsll Rsul]);
   figure;plot(jad(1:np),imrain(1:np));xlabel('Jday (1999)');ylabel('Precipitation  (mm)');Title('SCS Imet Rain Gauge');
   figure;plot(jad(1:np),cogm(1:np),jad(1:np),lrgm(1:np));xlabel('Jday (1999)');ylabel('SCS Ship Direction (deg)');axis([j_st j_en 0 360]);Title('GPS Cog=blue; Laser Ring Gyro=green');
end; 

if prtit==0,
   ti15=st2(3,:);
   hr15=floor(st2(3,:)/10000);
   mn15=floor(st2(3,:)/100)-hr15*100;
   sc15=round(st2(3,:)-hr15*10000-mn15*100);
   yr15=(1:length(sc15)).*0+1999;
   mo15=(1:length(sc15)).*0+6;
   da15=(1:length(sc15)).*0+15;               %day
   sogx15=st2(6,:);
   sogy15=st2(7,:);
   sogm15=sqrt(sogx15.*sogx15+sogy15.*sogy15).*0.514;
   cogm15=mod(atan2(sogy15,sogx15)/rdcon+360,360);
   lat15=st2(4,:);
   lon15=st2(5,:);
   lrg15=st2(14,:);
   lrgc15=cos(lrg15*rdcon);
   lrgs15=sin(lrg15*rdcon);
   lrgm15=mod(atan2(lrgs15,lrgc15)/rdcon+360,360);
   for ii=1:length(sogm15);
      if abs(sogm15(ii))>15,
         sogm15(ii)=0;
      end;
      if lrgm15(ii)==0,
         lrgm15(ii)=lrgm15(ii-1);
      end;
      if lat15(ii)==0,
         lat15(ii)=lat15(ii-1);
      end;
      if lon15(ii)==0,
         lon15(ii)=lon15(ii-1);
      end;
   end;
   course=(1:length(cogm15)).*0;
   ringyro=(1:length(cogm15)).*0;
   n=datenum(yr15,mo15,da15,hr15,mn15,sc15);
   bfin=datestr(n);
   flneme=['e:\wind\temm',num2str(jd),'.prn'];
   frep=fopen(flneme,'w');
   disp('writing')
   for ii=1:length(bfin),
      if cogm15(ii)>=100,
         course=num2str(cogm15(ii),'%7.4f');
      else
         course=['0',num2str(cogm15(ii),'%7.4f')];
      end
      if lrgm15(ii)>=100,
         ringyro=num2str(lrgm15(ii),'%7.4f');
      else
         ringyro=['0',num2str(lrgm15(ii),'%7.4f')];
      end
      fprintf(frep,'%s',['06/',num2str(str2num(jd)-151),'/1999-',bfin(ii,13:20),',',course,',',num2str(sogm15(ii),'%6.4f'),',-',num2str(lat15(ii),'%8.4f'),',',num2str(lon15(ii),'%8.4f'),',',ringyro]);
      fprintf(frep,'\n');
   end;
end;
fclose('all');