disp('read_altimeter')
%in Matlab, read all days and hours, compute significant wave height
% (non-motion corrected) with the following lines:
% for ddd=292;for hhh=12;
% ZZZ=[];;read_altimeter;end;end;
%figure;plot((0:length(ZZZ)-1)./24+147,ZZZ);

fclose('all');

cruisename = 'AMMA07';

jd=num2str(ddd);

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

if hhh < 10,
   hr=['0' num2str(hhh)];
else
   hr=num2str(hhh);
end;

wave=[];timer=[];st0=[];

e=['F:\AMMA_2007\RHB\flux\raw\DAY',jd,'\P3_' jd hr];
disp(['Reading altimeter file from hour ',num2str(hhh)]);
flist=fopen(e,'r');
   
if flist>0,					%if the file exists, 
   dmp=fgets(flist);		%read header lines and skip blanks
   while feof(flist)==0,
      st0=fgets(flist);
      if length(st0) > 20 & ~isempty(str2num(st0(17:21))),
         timer=[timer str2num(st0(1:14))];
         wave=[wave str2num(st0(17:21))];
      else
         st0=[];
      end;
   end;
end;

timer=timer./1000-timer(1)./1000;
mwave=median(wave);
wave=wave-mwave;

Owave=detrend(wave);
med_Owave=median(Owave);
sort_Owave=sort(Owave);
jk=floor(0.5*length(sort_Owave));
jkp=floor(.667*length(sort_Owave));
jkm=floor(.333*length(sort_Owave));
sig_Owave=0.5*(sort_Owave(jkp)-sort_Owave(jkm));
Otimer=timer;

if abs(Owave(1)-med_Owave) > 7*sig_Owave,
   Owave(1)=med_Owave;
end;
for kkk=2:length(Owave);
   if abs(Owave(kkk)-med_Owave) > 7*sig_Owave,
      Owave(kkk)=Owave(kkk-1);
      Otimer(kkk)=Otimer(kkk-1)+0.5;
   end;
end;

[Swave,Fwave]=psd2(detrend(wave),length(wave),2.);
[Swaves,Fwaves]=specsmoo(Swave,2.);
figure;loglog(Fwaves,Fwaves.*Swaves);xlabel('Frequency (Hz)');ylabel('f*Sw(f)  (m^2)')
sig_wave_ht=2.83.*sqrt(var(wave));
sig_Owave_ht=2.83.*sqrt(var(Owave));
title(['Sig W Ht ',num2str(sig_wave_ht),'  ',num2str(sig_Owave_ht),'   JD=', jd,' Hr=', hr])
disp(['Non-compensated significant wave height = ',num2str(sig_wave_ht)])
print_buffer = ['f:\AMMA_2007\RHB\flux\Raw_Images\', cruisename, jd '_wavespc.jpg'];
print('-djpeg90 ', print_buffer);

figure;plot(timer,wave,Otimer,Owave);
title(['Sig W Ht ',num2str(sig_wave_ht),'  ',num2str(sig_Owave_ht),'   JD=', jd,' Hr=', hr])
print_buffer = ['f:\AMMA_2007\RHB\flux\Raw_Images\', cruisename, jd '_wave.jpg'];
print('-djpeg90 ', print_buffer);


      if length(st0) > 20 & ~isempty(str2num(st0(17:21))),
    display(['File not found or empty    ',e])
end

ZZZ=[ZZZ sig_Owave_ht];

%figure;plot((0:length(ZZZ)-1)./24+188,ZZZ)