%% Time Range Amplitude plot
function [logA,time,range,map]=trampl(nc,txpol,xpix,ypix);

if nargin<3, xpix=300; end
if nargin<4, ypix=100; end

% averaging to match window size
nsweep=size(nc{'adc_data'},1);
xavg=max(1,ceil(nsweep/xpix));
range=nc{'range'}(:);
nrange=length(range); 

% process radar returns
A=zeros(nrange,floor(nsweep/xavg));
ampl=zeros(xavg,floor(nsweep/xavg)); lenAmpl=prod(size(ampl));
for r=1:nrange,
  [I,Q,mnIQ,sdIQ]=ipixload(nc,txpol,r,'ohgr');
  absiq=abs(I+j*Q)*sqrt(prod(sdIQ));
  ampl(:)=absiq(1:lenAmpl);
  A(r,:)=mean(ampl,1);
end
PRF=nc{'PRF'}(1);
time=(1:size(A,2))/PRF*xavg;
range=nc{'range'}(:);

% range averaging
while size(A,1)>ypix,
  A=0.5*(A(1:2:end-1,:)+A(2:2:end,:));
  range=0.5*(range(1:2:end-1)+range(2:2:end));
end

% indexed color log plot
logA=log(A);
mn=min(min(logA)); mx=max(max(logA));
logA=(logA-mn)*63/(mx-mn);

if nargout<1,
  % display image
  image(time,range,logA); set(gca,'ydir','normal');
else
  % prepare for imwrite
  time=[0 nsweep]/PRF;
  range=nc{'range'}([1 end]);
  range(2)=range(2)+(range(2)-range(1))/(nrange-1);
  map=jet;
end

