function [idt]=read_83p(fname)

if(nargin == 0)
  %     error('Must supply a file name on the command line.')
  fname = input('.83p filename for conversion?  ','s');
end
%reprates = zeros(1000,1);
lfn = length(fname);
fn_ext = fname(lfn-2:lfn);
fnwo   = fname(1:lfn-4);
if((fn_ext ~= '83p') & (fn_ext ~= '83P'))
  error('Trouble with file name, try again with .83p extension?')
  %     fname = [fname '.83p'];
end
fid  = fopen(fname,'r','b');
if(fid < 0) 
  error('Trouble with file name, try again with .83p extension?')
end

%fnameout = input('Name of file for text?','s');


firsthdr = fread(fid,256,'uchar');
%[Nbeams,roll, mounttilt,GPSlats,GPSlons, GPSS, GPSh, reprate] = parseheader2(firsthdr);

%k[Nbeams, SectorSize,StartAngle, AngleInc,mounttilt, repr] = parseh(hdr)
%
%keyboard
[Nbeams, GPSs,SectorSize,StartAngle, AngleInc,mounttilt, repr, rangeres, ...
 timeVec, dateStr] = parseh(firsthdr)

time  = timeVec(1)*3600+timeVec(2)*60+timeVec(3);
hrs0  = timeVec(1);
time0 = time;
dateStr0 = dateStr;


%GPSlatstr(1,1:14) = GPSlats';
%GPSlonstr(1,1:14) = GPSlons';
%GPSspeed(1) = GPSS;
%GPShead(1) = GPSh;
reprates(1) = repr;
a83pdata(:,1) = fread(fid,Nbeams,'uint16');
if(firsthdr(118) == 1)
  a83pAmp(:,1) = fread(fid,Nbeams,'uint16');
end
i = 2;
count = 1;
%keyboard
Nbeamsold = Nbeams;
avect = [0:(Nbeams-1)];

theta(:,1) = StartAngle+AngleInc*avect' + mounttilt;
tic
while(count ~= 0)
  
  [hdr,ct] = fread(fid,256,'uchar');
  %     keyboard
  if(ct < 256) break; end
  %    [Nbeams, Rollangle, mounttilt, GPSlats, GPSlons, GPSs, GPSh, reprate] = parseheader2(hdr);
  %    slen = length(GPSlats)
  %    decpts = find(GPSlats == '.')
  %    if(~isempty(decpts))
  %    GPSlatstr(i, 1:14) = GPSlats';
  %    GPSlonstr(i, 1:14) = GPSlons';
  %    GPSspeed(i) = GPSs;
  %    GPShead(i) = GPSh;
  [Nbeams, GPSs,SectorSize,StartAngle, AngleInc,mounttilt, repr, rangeres, ...
  timeVec, dateStr] = parseh(hdr);
  GPSspeed(i) = GPSs;
  reprates(i) = repr;
  %    disp(Nbeams)
  if(Nbeams ~= Nbeamsold)
    disp('Number of beams changes mid-file')
    disp('Closing output file')
    fclose (fid)
    break
  end
  a83pdata(:,i) = fread(fid,Nbeams,'uint16');
  if(hdr(118) == 1)
    a83pAmp(:,i) = fread(fid,Nbeams,'uint16');
  end
  avect = [0:(Nbeams-1)];
  theta(:,i) = StartAngle+AngleInc*avect' + mounttilt;
  time(i,1) = timeVec(1)*3600+timeVec(2)*60+timeVec(3);
  %
  % Assume missions are < 24 hours.
  if( time(i,1) < time0 ) 
      time(i,1) = time(i,1) + time0;
  end
  i = i+1;
  Nbeamsold = Nbeams;
end
toc
fclose(fid);


%theta = -60.0:(120/(Nbeams-1)):60.0;   % note lazy coding here
%theta = angles + mounttilt;
a83pmeters = a83pdata.*rangeres./1000.0;
zerospeeds = find(GPSspeed == 0);
%GPSlatall = strvcat(char(GPSlatstr));
%GPSlatdeg = str2num(GPSlatall(:,1:3));
%GPSlatmin = str2num(GPSlatall(:,5:12));
%GPSlonall = strvcat(char(GPSlonstr));
%GPSlondeg = str2num(GPSlonall(:,1:3));
%GPSlonmin = str2num(GPSlonall(:,5:12));

if(firsthdr(118) == 1)
  a83pAmp    = a83pAmp';
  idt.data = [ time-time0 a83pmeters' a83pAmp' ];
else
  idt.data = [ time-time0 a83pmeters'];
  a83pAmp = 0;
end

idt.startDate = dateStr0;
idt.endDate   = dateStr;
idt.startTime = time0;
%
% Beams begin in column 2.
idt.BeamRange1 = 2;
idt.dataSource = fname;


if( 0 )

GPSspeed(zerospeeds) = 3.0;    %  default to 3 Knots
speedmeters = GPSspeed.*0.5144; % about 2 kt to 1 meter/sec 
if(speedmeters ==0)
  speedmeters = 3.0;
end

repratesec = reprates.*0.001;
ddists = speedmeters.*repratesec;
dists = cumsum(ddists);

cumtime = cumsum(repratesec);
%Inxlat = find(diff(GPSlatmin));
%if(~isempty(Inxlat))
%     GPSlatspl = spline(cumtime(Inxlat),GPSlatmin(Inxlat), cumtime);
%else
%     GPSlatspl = 0;
%end

%Inxlon = find(diff(GPSlonmin));
%if(~isempty(Inxlon))

%     GPSlonspl = spline(cumtime(Inxlon),GPSlonmin(Inxlon), cumtime);
%else
%     GPSlonspl = 0;
%end

%keyboard

%GPSlatmeters = GPSlatspl.*111200/60;
%GPSlonmeters = GPSlonspl.*(111200/60).*cos(str2num(GPSlatall(:,1:3)))';


outname = [fnwo '.XYZ'];

%disp('Format of output file is tab delimited ascii X, Y, Z, then newline')
disp('X is across track, Y is along track, and Z is positive upwards')
disp('Bad profile points default to Z = 0, but are left in the output file.')
disp('NOTE: if GPS speed is unavailable, speed defaults to 3 Knots')
%fidout = fopen(outname,'w');
theta_cart = 90 -theta;


  %keyboard
  for(k = 1:(i-1))         % (i-1) is the number of pings
    X = a83pmeters(:,k).*(cos(theta_cart(:,k)*pi/180));
    Z = a83pmeters(:,k).*(sin(theta_cart(:,k)*pi/180));%
    %     Yi = GPSlatmeters(k) + dists(1,k);
    Yi = speedmeters(k)*cumtime(k);
    Y = Yi.*ones(1,Nbeams);
    %     XGPS = GPSlonmeters(k) + X.*sin((270-GPShead(k))*pi/180);
    %     YGPS = GPSlatmeters(k) + X.*cos((270-GPShead(k))*pi/180);
    
    
    for(j = 1:Nbeams)
      ct = fprintf(fidout,'%12.8f \t %12.8f \t %12.8f \n', X(j),Y(j),Z(j));
    end
    
    %     ct = fprintf(fidout,'\n');
  end
end
%fclose(fidout)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%function [Nbeams, Rollangle, mounttilt, GPSlatstr, GPSlonstr, GPSsspeed, GPSheading, repr] = parseheader2(hdr)
function [Nbeams,GPSsspeed, SectorSize,StartAngle, AngleInc,mounttilt, ...
          repr,rangeres, timeVec, datestring] = parseh(hdr)
if(hdr(1:3)' ~= '83P')
  error('Trouble with file header')
end
totalbytes = hdr(5)*256 + hdr(6);
totalbeams = (totalbytes - 256)/2;

datestring = hdr(9:20);
timestring = hdr(21:29);
hdthstring = hdr(30:33);

timeVec = sscanf(char(timestring'),'%2d:%2d:%2d');

GPSlatstr  = hdr(34:47);
GPSlonstr  = hdr(48:61);
GPSsspeed  = hdr(62)/10; % knots

GPSheading = (hdr(63)*256 + hdr(64))/10; % compass degrees

Pitchangle = (((bitand(hdr(65),127))*256 + hdr(66))-900)/10;
if(bitget(hdr(65),8) == 0) Pitchangle = 0; end

Rollangle  = (((bitand(hdr(67),127))*256 + hdr(68))-900)/10;
if(bitget(hdr(67),8) == 0) Rollangle  = 0; end

Headangle  = (((bitand(hdr(69),127))*256 + hdr(70))-900)/10;
if(bitget(hdr(69),8) == 0) Headangle  = 0; end

Nbeams = hdr(71)*256 + hdr(72);

SampPerBeam = hdr(73)*256 + hdr(74);

SectorSize  = hdr(75)*256 + hdr(76);  % in degrees

StartAngle  = ((hdr(77)*256 + hdr(78))-180.0)/100.0; 

AngleInc    = hdr(79)/100; % in degrees

Rangesetting = hdr(80)*256 + hdr(81);

Frequency   = hdr(82)*256 + hdr(83); % in kHz

soundspd    = (hdr(84)*256 + hdr(85))./10;
if(soundspd>6000) soundspd = 1500.0;
end 
rangeres = (hdr(86)*256 + hdr(87));   % in mm.
%rangeres
%pulselen
mounttilt  = hdr(90)*256 + hdr(91) -180;
repr     = hdr(92)*256 + hdr(93);
% ping no

return





