% function [I,Q,meanIQ,stdIQ,inbal]=ipixload(nc,pol,rangebin,mode); 
%
% Loads I and Q data from IPIX radar cdf file.
% Inputs:
%   nc       - pointer to netCDF file
%   pol      - Transmit/receive polarization ('vv','hh','vh', or 'hv')
%   rangebin - Range bin(s) to load
%   mode     - Pre-processing mode:
%              'raw' does not apply any corrections to the data;
%              'auto' applies automatic corrections, assumes that radar
%                 does not look at still objects, such as land;
%              'dartmouth' first removes land, using knowledge of the geometry
%                 of the Dartmouth site. Then same as 'auto'.
%
% Outputs:
%   I,Q      - Pre-processed in-phase and quadrature component of data
%   meanIQ   - Mean of I and Q used in pre-processing
%   stdIQ    - Standard deviation of I and Q used in pre-processing
%   inbal    - Phase inbalance [degrees] used in pre-processing
%
% Rembrandt Bakker, McMaster University, 2001-2002
%

function [I,Q,meanIQ,stdIQ,inbal]=ipixload(nc,pol,rangebin,mode); 

%% check inputs
nrange=length(nc{'range'}(:));
if rangebin<1 | rangebin>nrange,     
  disp(['Warning: rangebin ' num2str(rangebin) ...
        ' not found in file ' nc.NetCDF_file_name(1:end-1)]);
  return;
end

%% in some cdf files, the unsigned flag is not set correctly %%
adc_data=unsigned(nc{'adc_data'},1);

H_txpol     = 1;
V_txpol     = 2;
Like_adc_I  = nc{'adc_like_I'}(1)+1;
Like_adc_Q  = nc{'adc_like_Q'}(1)+1;
Cross_adc_I = nc{'adc_cross_I'}(1)+1;
Cross_adc_Q = nc{'adc_cross_Q'}(1)+1;

%% extract correct polarization from cdffile %%
pol=lower(pol);
if length(size(adc_data))==3,
  % read global attribute TX_polarization
  txpol=nc.TX_polarization(1);
  if pol(1)~=lower(txpol),
    fname=strrep(nc.NetCDF_file_name(:),'\0','');
    disp(['Warning: file ' fname ' does not contain ''' ...
          txpol(1) ''' transmit polarization.']);
  end
  switch pol,
  case {'hh','vv'}, xiq=adc_data(:,rangebin,[Like_adc_I Like_adc_Q]);
  case {'hv','vh'}, xiq=adc_data(:,rangebin,[Cross_adc_I Cross_adc_Q]);
  end
  I=permute(xiq(:,:,1),[1 2 3]); 
  Q=permute(xiq(:,:,2),[1 2 3]);
else 
  switch pol,
  case 'hh', xiq=adc_data(:,H_txpol,rangebin,[Like_adc_I Like_adc_Q]);
  case 'hv', xiq=adc_data(:,H_txpol,rangebin,[Cross_adc_I Cross_adc_Q]);
  case 'vv', xiq=adc_data(:,V_txpol,rangebin,[Like_adc_I Like_adc_Q]);
  case 'vh', xiq=adc_data(:,V_txpol,rangebin,[Cross_adc_I Cross_adc_Q]);
  end
  I=permute(xiq(:,1,:,1),[1 3 4 2]); 
  Q=permute(xiq(:,1,:,2),[1 3 4 2]); 
end

%% apply corrections to I and Q data %%
if nargin<3, mode='auto'; end
switch mode,
case 'raw',
  meanIQ=[0 0]; stdIQ=[1 1]; inbal=0;  
case 'auto',
  % Pre-processing
  meanIQ=mean([I(:) Q(:)]); stdIQ=std([I(:) Q(:)],1);
  I=(I-meanIQ(1))/stdIQ(1);
  Q=(Q-meanIQ(2))/stdIQ(2);
  sin_inbal=mean(I(:).*Q(:));
  inbal=asin(sin_inbal)*180/pi;
  I=(I-Q*sin_inbal)/sqrt(1-sin_inbal^2);
case 'dartmouth',
  % Define rectangular patches of land in Dartmouth campaign.
  % Format: [azmStart azmEnd  rangeStart rangeEnd]
  landcoord=[
      0  70     0  600;
    305 360     0  600;  
     30  55     0 8000; 
    210 305     0 4700;
    320 325  2200 2700; 
  ];
  % Exclude land from data used to estimate pre-processing parameters
  azm=mod(ipixazm(nc),360);
  range=nc{'range'}(rangebin);
  nbin=length(rangebin);
  ok=ones(size(I));
  for i=1:size(landcoord,1),
    for r=1:nbin,
      if range(r)>=landcoord(i,3) & range(r)<=landcoord(i,4),
        ok(find(azm>=landcoord(i,1) & azm<=landcoord(i,2)),r)=0;
      end
    end
  end
  ok=find(ok);
  if length(ok)<100,
    disp(['Warning: not enough sweeps for land-free pre-processing.']);
    ok=ones(size(I));
  end
  % Pre-processing
  meanIQ=mean([I(ok) Q(ok)]); stdIQ=std([I(ok) Q(ok)],1);
  I=(I-meanIQ(1))/stdIQ(1);
  Q=(Q-meanIQ(2))/stdIQ(2);
  sin_inbal=mean(I(ok).*Q(ok));
  inbal=asin(sin_inbal)*180/pi;
  I=(I-Q*sin_inbal)/sqrt(1-sin_inbal^2);
end
