disp('read_shp_day')
fclose('all');

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

ship='RHB';
if ship =='RHB'
   total_num_fields=17;
   imu_field_num=16;
   imd_field_num=17;
elseif ship =='KAI'
   total_num_fields=19;
   imu_field_num=15;
   imd_field_num=16;
end;

plotit=1;
prtit=1;
tzilch=0;
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(total_num_fields,1800);
st2=zeros(total_num_fields,4500);
for jam=0:23,						%cycle thru 24 hourly stats files
   if jam<10,
      hr=['0'num2str(jam)];
   else
      hr=num2str(jam);
   end;
   e=['f:\day',jd,'\P6_' jd hr];
   disp(['Reading SCS file from hour ',num2str(jam)])
   flist=fopen(e,'r');
   if flist>0,   					%if the file exists, 
      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
         if ship=='RHB'
            st1=fscanf(flist,'%g,%g,%g,%10g%*c,%10g%*c,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g',[total_num_fields,inf]);
         elseif ship=='KAI'
            st1=fscanf(flist,'%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g',[total_num_fields,inf]);
         end;
         [nr1,nl1]=size(st1);
         if nr1==total_num_fields
      	    st2(:,jax:jax+nl1-1)=st1;
      	    jax=jax+nl1;
            if backchk==0,
                tzilch=jam;
                backchk=1;
            end;
	     else
   	       x=fgets(flist);
	     end;
      end;
   end;
end;

cog=st2(6,:);
sog=st2(7,:);
lrg=st2(14,:);
imu=st2(imu_field_num,:);
imd=st2(imd_field_num,:);

jk=ceil((jax-1)/30);%1-minute
st3=zeros(total_num_fields,jk);
rdcon=pi/180;
st2(6,:)=sog.*cos(cog*rdcon);
st2(7,:)=sog.*sin(cog*rdcon);
st2(imu_field_num,:)=imu.*cos(imd*rdcon);
st2(imd_field_num,:)=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)')';
   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(imu_field_num,:);
imuy=st3(imd_field_num,:);
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,:);
%
if ship=='RHB' odec=st3(15,:); end;
%jad=str2num(num2str(jd))+(tzilch+(st3(1,:)-st2(1,1))/60/60)/24;
np=jk;

if prtit==1;
  prt_epi_scs_etl;
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=1100;
   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 (2000)');ylabel('Temperature (C)');title('SCS Tair=blue;    Tsea=green');axis([j_st j_en 20 26]);
   if ship=='RHB',
      figure;plot(jad(1:np),sogm(1:np),jad(1:np),odec(1:np));xlabel('Jday (2000)');ylabel('SCS Ship Speed (m/s)');title('GPS=blue;    odec=green'); axis([j_st j_en 0 10]);
   elseif ship=='KAI'
      figure;plot(jad(1:np),sogm(1:np));xlabel('Jday (2000)');ylabel('SCS Ship Speed (m/s)');title('GPS=blue');
   end;
   figure;plot(jad(1:np),imsol(1:np));xlabel('Jday (2000)');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 (2000)');ylabel('Precipitation  (mm)');title('SCS Imet Rain Gauge');
   figure;plot(jad(1:np),cogm(1:np),jad(1:np),lrgm(1:np));xlabel('Jday (2000)');ylabel('SCS Ship Direction (deg)');axis([j_st j_en 0 360]);title('GPS Cog=blue;    Laser Ring Gyro=green');
end; 

fclose('all');

%clear x vul uul tzilch tss tsg sul st3 st2 st1 sogy sogx sogm sog sig_sb
%clear rdcon prtit prt_it plotit odec nr1 npz np nl1 lrgsm lrgs lrgm lrgcm lrgc lrg lon lat
%clear jl jk jj jd jaz jaxx jax jam jade jad j_st j_en j imuy imux imum imu imta imsol imrh imrain imdm imd
%clear hul hr hll gpstim g flist f eps_w e dmp2 dmp cogm cog blk backchk ans
%clear Tul Tll Rtul Rtll Rsul Rsll Rlul Rlll
%clear total_num_fields imu_field_num imd_field_num

