% read carbon waveglider (CWG) zrecord summary log]
function[] = read_obin(logpath)
    global serr;
    serr = 2;
    fid = -1;
    errmsg='';
    status=0;

    try
    fprintf(serr,"loading %s...\n",logpath);
    [fid,errmsg] = fopen(logpath,'r');
    if fid >= 0 
        fprintf(serr,"fopen OK fid[%d] %s\n",fid,errmsg);
        status = load_records(fid);
        if ~(status == 0)
         fprintf(serr,"load_records returned [%d]\n",status);           
        end
        fclose(fid);
    else
        fprintf(serr,"fopen failed [%s]\n",errmsg);
    end
    catch OE
        fprintf(serr,"open failed [%s]\n",OE.identifier);
    end
end

% load records
function status = load_records(fd)
    global serr;
    sync_err = 0;
    record_count = 1;
    load_err=0;
    status=0;
    lerr_count=0;
    serr_count=0;

    if fd >= 0 
        while feof(fd)==0
        sync_err=-1;
        ofs=0;
        [sync_err,ofs] = find_sync(fd);
        if sync_err==0 
            fseek(fd,ofs,-1);
            % load next record
            [record,load_err] = load_record(fd);
            if load_err == 0
%            fprintf(serr,"read_log co2[%g]\n",record(1).data(1).fields(1));
                % add record to array and increment count
                record_arr(record_count)=record;
                record_count=record_count+1;
            else
                status=-1;
                lerr_count=lerr_count+1;
            end
        else
			if sync_err==-2
%				fprintf("end of file\n");
				status=0;
            else
				status=-1;
            	serr_count=serr_count+1;
		end
    end
%         fprintf(serr,"read_log sync_err[%d] ofs[%d] load_err[%d] records[%u]\n",sync_err,ofs,load_err,record_count);
        end
    end
    if ~( status==0 )
        fprintf(serr,"read_records errors load_errors[%d] sync_errors[%d]\n",lerr_count,serr_count);
    end
    assignin('base','records',record_arr);
end

% load one record
function [logrec,err] = load_record(fd)
    global serr;
    logrec=struct('header',{},'data',{});
    header=struct('sync',[],'timestamp',[],'id',[],'seq_n',[],'cycle_id',[],'meas_n',[],'ens_n',[],'ens_len',[],'period_s',[]);
    err=0;

    % read header
    try
        header(1).sync=fread(fd,1,'uint32',0,'l');
        header(1).timestamp=fread(fd,1,'uint64',0,'l');
        header(1).id=fread(fd,1,'uint16',0,'l');
        header(1).seq_n=fread(fd,1,'uint32',0,'l');
        header(1).cycle_id=fread(fd,1,'uint32',0,'l');
        header(1).meas_n=fread(fd,1,'uint16',0,'l');
        header(1).ens_n=fread(fd,1,'uint16',0,'l');
		header(1).ens_len=fread(fd,1,'uint16',0,'l');
        header(1).period_s=fread(fd,1,'float',0,'l');
	% read data
        meas=struct('fields',{});
        for j=1:13
            meas(1).fields(j)=fread(fd,1,'float',0,'l');
        end
        % add measurement to array
        meas_arr(1)=meas;
        fprintf(serr,"load_record sync[%08lX] ts[%lu] id[%04hX] seq_n[%lu] cycle_id[%lu] meas_n[%hu] ens_n[%hu] ens_len[%hu]  period_s[%f]\n",header(1).sync,header(1).timestamp,header(1).id,header(1).seq_n,header(1).cycle_id,header(1).meas_n,header(1).ens_n,header(1).ens_len,header(1).period_s);
    catch HE
        fprintf(serr,"header read error %s\n",HE.identifier);
        err=-1;
    end

    if err==0
        % populate struct
        logrec(1).header=header;
        logrec(1).data=meas_arr;
    end

%     fprintf("data end ofs[%d]\n",ftell(fd));
end

% find record sync pattern
function [r_err,r_ofs] = find_sync(fd)
     global serr;
     r_err=-1;
     r_ofs=-1;
     sync_valid=int32(0);
     sync_byte=uint8(0);
    if (fd>0) 
%         fprintf(2,"find_sync ENTER fd[%d] ofs[%ld]\n",fd,ftell(fd));
        while feof(fd)==0 && sync_valid==0
            sync_start = ftell(fd);
            sync_byte=uint8(0);
            [sync_byte,count]=fread(fd,1,'uint8');
            if count==1 && sync_byte==uint8(234)
               sync_byte=fread(fd,1,'uint8');
               if count==1 && sync_byte==uint8(117)
                   sync_byte=fread(fd,1,'uint8');
                   if count==1 && sync_byte==uint8(42)  
                       sync_byte=fread(fd,1,'uint8');
                       if count==1 && sync_byte==uint8(192) 
                           sync_valid=1;
                           break;
                       else
%                             fprintf(serr,"err[3] read %02X\n",sync_byte);
                       end
                   else
%                         fprintf(serr,"err[2] read %02X\n",sync_byte);
                   end
               else
%                     fprintf(serr,"err[1] read %02X\n",sync_byte);
               end
            else
%                 fprintf(serr,"err[0] read %02X\n",sync_byte);
            end
        end

        if sync_valid == 1
            r_ofs = sync_start;
            r_err = 0;
        elseif feof(fd)==1
            r_err = -2;
        elseif ferror(fd)==1
            r_err = -3;
        else
            r_err = -4;
        end
    else
        fprintf(serr,"invalid arg\n");   
    end
%     fprintf(serr,"find_sync EXIT r_ofs[%ld] r_err[%d]\n",r_ofs,r_err);

end

