function [data,size1,size2,size3,numbeams]=read_header(fid1,datafile_name)

frewind(fid1);
fseek(fid1,0,'eof');
filesz=ftell(fid1);
frewind(fid1);

data.sampleRate = 1.5625e6;
data.filename = char(datafile_name);
c= 3e8;
chDisable = hex2dec('ffffffff');
maxDwells=5000;
flashlite=0;

% Read in header data t identify size of beam datga size
size1=0;
size2=0;
size3=0;

data.messageVersion = fread(fid1,1,'uint32');
size1=size1+4;
data.messageId = fread(fid1,1,'uint32');
size1=size1+4;
data.messageSize = fread(fid1,1,'uint32');
size1=size1+4;
data.scanId = fread(fid1,1,'uint32');
size1=size1+4;
data.timestamp = fread(fid1,1,'uint64');
size1=size1+8;
if data.messageVersion >= 4       % v4 of the CPI message includes a GPS status flag
    data.gpsSync = fread(fid1,1,'uint32');
    size1=size1+4;
else
    data.gpsSync = -999;
end

data.waveformId = fread(fid1,1,'uint32');
size1=size1+4;
data.transceiverType = fread(fid1,1,'uint32');

size1=size1+4;
data.numBeamPosInScan = fread(fid1,1,'uint32');
size1=size1+4;
data.beamPositionId = fread(fid1,1,'uint32');
data.numPulses = fread(fid1,1,'uint32');
size2=size2+4;
% Transmitter position structure
data.latitude = fread(fid1,1,'double');
size2=size2+8;
data.longitude = fread(fid1,1,'double');
size2=size2+8;
data.pitch = fread(fid1,1,'double');
size2=size2+8;
data.roll = fread(fid1,1,'double');
size2=size2+8;
data.yaw = fread(fid1,1,'double');
size2=size2+8;

if data.messageVersion >= 5       % v5 of the CPI message includes positioner bearing & tilt
    data.bearing = fread(fid1,1,'double');
    size2=size2+8;
    data.tilt = fread(fid1,1,'double');
    size2=size2+8;
else
    data.bearing = -999;
    data.tilt = -999;
end

% Steer position structure
data.azCh0 = fread(fid1,1,'double');
data.elCh0 = fread(fid1,1,'double');


if flashlite == 1,
    data.azCh0 = data.yaw;
    data.elCh0 = data.pitch;
end
    % Recieve channel strcuture (currently a max of 4 in version 3 and up of CPI)
    data.idCh0 = fread(fid1,1,'uint32');
    size3=size3+4;
    data.ch0numSamples = fread(fid1,1,'uint32');
    size3=size3+4;
    data.idCh1 = fread(fid1,1,'uint32');
    size3=size3+4;
    data.ch1numSamples = fread(fid1,1,'uint32');
    size3=size3+4;
    data.idCh2 = fread(fid1,1,'uint32');
    size3=size3+4;
    data.ch2numSamples = fread(fid1,1,'uint32');
    size3=size3+4;
    data.idCh3 = fread(fid1,1,'uint32');
    size3=size3+4;
    data.ch3numSamples = fread(fid1,1,'uint32');
    size3=size3+4;
    
    numpulses=data.numPulses;
    ch0samples=data.ch0numSamples;
    ch1samples=data.ch1numSamples;
    ch2samples=data.ch2numSamples;
    ch3samples=data.ch3numSamples;
    if data.messageVersion >=3
        beambytes=64+17*4+4*numpulses*(ch0samples+ch1samples+ch2samples+ch3samples);
    elseif data.messageVersion>=4
        beambytes=beambytes+4;
    elseif data.messageVersion>=5
        beambytes=beambytes+16;
    end
    data.beambytes=beambytes;

    numbeams=ceil(filesz/data.beambytes/2);
return
end