% clear all;fclose all;hhh=9;read_Riegl_wave_VOCALS
%%DEW 10/19/2008
disp('read_altimeter')

close all
clear

ddds = input('Input yearday  ','s');
ddd = str2num(ddds);
read_parameters_VOCALS   % DEW 10092008
prtit = 0; % DEW 10092008

fclose('all');

jd=num2str(ddd);

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


for jam=0:6:23,					    %cycle thru 24 hourly stats files
    clear stx;
    temp=[];stx=[];

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

    e=[way_raw_data_flux, 'Day',jd,'\lsr0' year(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,
        e
        %%%dew
        dmp=fgets(flist);			%read 1st header line
        YY = str2num(dmp(7:10));
        MM = str2num(dmp(1:2));
        DD = str2num(dmp(4:5));
        hh = str2num(dmp(12:13));
        ict = 0;
        while feof(flist)==0,
            %for p = 1:3
            dtmp=fgetl(flist);
            d1 = strrep(dtmp, 'r',' ');
            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 = str2num(tim(:,1:2));
                ss = str2num(tim(:,3:8))/1000;
                %calculate year day
                yd=datenum(YY,MM,DD,hh,0,0)-datenum(YY-1,12,31);
                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(['JD ' year]);ylabel('Wave ht non motion corrected  (m)')
        title(['Wave ht ', cruise, year]);
        ylim([ 0 25]);
        xlim([minx maxx]);
        print_buffer = [way_raw_images_flux 'WaveHt_' num2str(ddd),'_',hr, '.png'];
        print('-dpng ', print_buffer);
        %print('-djpeg90 ',[way_raw_images_flux 'WaveHt_' num2str(ddd),'_',hr, '.jpg']);

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

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



        figure(4);
        clf
        plot(st1(1:3000,2),'.');
        xlabel('pts');ylabel('Wave ht non motion corrected  (m)')
        title(['Wave ht ', cruise, year]);
        ylim([ 0 25]);
        %xlim([minx maxx]);
        print_buffer = [way_raw_images_flux 'WaveHt_' num2str(ddd),'_',hr, 'a.png'];
        print('-dpng ', print_buffer);
        %print('-djpeg90 ',[way_raw_images_flux 'WaveHt_' num2str(ddd),'_',hr, '.jpg']);

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

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

    end % test if file exists
end % hour loop


