% read_wave
% Reads Riegl 20Hz laser range meter for wave height. (slow)
% VOCALS 2008 :: 2008-10-19 :: Dan Wolfe
%                     10-30 :: Simon de Szoeke
disp(['read_wave' cruise ' ' yyyy])

if ~exist('ddd','var')
    ls([way_raw_data_flux 'day*'])
    ddd=input('Input yearday: ');
end

% Set in calling program:
% read_parameters   % DEW 10092008
% prtit = 0; % DEW 10092008

fclose('all');
jd=sprintf('%03i',ddd);

for jam=0:6:23,					    %cycle thru 24 hourly stats files
    clear stx;
    temp=[];stx=[];
    hr=sprintf('%02i',jam);
    e=[way_raw_data_flux, 'Day',jd,'\lsr0' yyyy(3:4) jd hr '_raw.txt'];
    %10/10/2008 02:00 LD90-3100VHS-FLP (SN: 12)
    %0000080 r8.051;a49;q27
    %0000189 r8.110;a47;q13
    %0000283 r8.148;a48;q46
    %0000392 r8.088;a58;q44
    %  Time  range(m) amp  qc
    % amplitude of signal 0-255 255 is strongest
    % q is quality of measurements 0-100 % of valid shots
    disp(['Reading laser file from hour ',hr]);
    flist=fopen(e,'r');
    if flist>0,					    %if the file exists,
        %disp(['Reading laser wave file:  ' e]);
        dmp=fgets(flist);			%read 1st header line
        YY=str2double(dmp(7:10));
        MM=str2double(dmp(1:2));
        DD=str2double(dmp(4:5));
        hh=str2double(dmp(12:13));
        ict=0;
        while feof(flist)==0,
            %for p = 1:3
            dtmp=fgetl(flist);
            d1 = strrep(dtmp, 'r',' '); % replace characters with spaces
            d2 = strrep(d1, 'a',' ');
            d3 = strrep(d2, 'q',' ');
            d4 = strrep(d3, ';', '');
            if length(dtmp) > 13 %%check fro an empty line  ie  1357337 r;a;q
                ict=ict+1;
                tim=dtmp(1:8);
                mm=str2double(tim(:,1:2));
                ss=str2double(tim(:,3:8))/1000;
                %calculate year day
                yd=datenum(YY,MM,DD,hh,0,0)-datenum(YY,1,0);
                jdn(ict,1)=yd+(((mm+(ss/60))/60))/24;
                st1(ict,1)=jdn(ict,1);
                data1=d4(8:length(d4));
                C=textscan(data1,'%f%f%f','CollectOutput',1);
                tmp=C{:};
                st1(ict,2)=tmp(1,1);  %  range
                st1(ict,3)=tmp(1,2);  %  amp
                st1(ict,4)=tmp(1,3);  %  qc
            end
        end
    end  % end file test

    if flist>0 % file exists
        maxx=max(jdn);
        minx=min(jdn);

        figure(1);
        clf
        plot(jdn,st1(:,2),'.');
        xlabel(['yearday ' yyyy]);ylabel('Wave height non motion corrected  (m)')
        title(['Wave height ', cruise, year]);
        ylim([ 0 25]);
        xlim([minx maxx]);
        if prtit; print('-dpng ', [way_raw_images_flux 'WaveHt_' num2str(ddd),'_',hr, '.png']); end

        figure(2);
        clf
        plot(jdn,st1(:,3),'.');
        xlabel(['yearday ' ]);ylabel('Amp (0-255')
        title(['Laser Wave Amplitude ', cruise, year]);
        ylim([0 255]);
        xlim([minx maxx]);
        if prtit; print('-dpng ', [way_raw_images_flux 'WaveAmp_' num2str(ddd),'_',hr, '.png']); end

        figure(3);
        clf
        plot(jdn,st1(:,4),'.');
        xlabel(['yearday ' yyyy]);ylabel('Laser QC  (%)')
        title(['Laser Wave QC ', cruise, year]);
        ylim([0 100])
        xlim([minx maxx]);
        if prtit; print('-dpng ', [way_raw_images_flux 'WaveQC_' num2str(ddd),'_',hr, '.png']); end

        figure(4);
        clf
        plot(st1(1:3000,2),'.');
        xlabel('20 Hz samples');ylabel('Wave height non motion corrected  (m)')
        title(['Wave height ', cruise, year]);
        ylim([ 0 25]);
        %xlim([minx maxx]);
        if prtit; print('-dpng ', [way_raw_images_flux 'WaveHt_' num2str(ddd),'_',hr, 'a.png']); end

        figure(5);
        clf
        plot(st1(1:3000,3),'.');
        xlabel('20 Hz samples');ylabel('Amp (0-255')
        title(['Laser Wave Amplitude ', cruise, year]);
        ylim([0 255]);
        %xlim([minx maxx]);
        if prtit; print('-dpng ', [way_raw_images_flux 'WaveAmp_' num2str(ddd),'_',hr, 'a.png']); end

        figure(6);
        clf
        plot(st1(1:3000,4),'.');
        xlabel('20 Hz samples');ylabel('Laser QC  (%)')
        title(['Laser Wave QC ', cruise, year]);
        ylim([0 100])
        %xlim([minx maxx]);
        if prtit; print('-dpng ', [way_raw_images_flux 'WaveQC_' num2str(ddd),'_',hr, 'a.png']); end

    end % test if file exists
end % hour loop
