%--------------------------------------------------------------------------
%
% load_response_wsig_points.m
%
% 8/10/2007, MERS Lab, Brigham Young University
%
% Reader for the tabularized QuikSCAT slice spatial response function. For
% use in conjunction with interpolate_slice_response.m . 
%
% Inputs:
% save_az: the azimuth index of the desired response (1-36) respresenting
%   azimuth angles spaced every 10 degrees starting with 0
% save_pos: the orbit time index of the desired response (1-36) spaced
%   according to a table found in interpolate_slice_response.m
% polarization: 1 = Vpol, 0 = Hpol;
%
%
% Outputs:
% m,n : response function array dimensions
% lat,lon : (m x n) arrays containing the lat/lon perturbations of the response function
% resp :(m x n 8) array containing the response function for each of the 8 slices
% m1, n1 : indice pairs of the slice centers in the response of lat/lon arrays
%           e.g. lat(m1,n1) gives the latitudes of the slice centers
% sig_m, sig_n : indice pairs of the significant vectors used in correct
%           interpolation of the slice response
%
%
%--------------------------------------------------------------------------


function [ m, n, lat, lon, resp, m1, n1, sig_m, sig_n] =  load_response_wsig_points(save_az, save_pos, polarization);
    

% paths to local copy of the tabularized response files
if(polarization == 1)
    inFile='slice.outer.backup';
else
    inFile='slice.inner.backup';
end


fid=fopen(inFile);
sig_m = zeros(3,1);
sig_n = zeros(3,1);
m=0;n=0;temp=0;lat=0;lon=0;resp=0;m1=0;n1=0;
mas_m=0;mas_n=0;
% Number of positions & number of azimuth angles
temp=fread(fid,1,'int'); temp=fread(fid,temp/4,'int'); Npos=temp(1);Nazimuth=temp(2); temp=fread(fid,1,'int');


for pos=1:Npos
for az=1:Nazimuth
  
  % dimensions of the response function
  temp=fread(fid,1,'int'); temp=fread(fid,temp/4,'int'); m=temp(1);n=temp(2); temp=fread(fid,1,'int');
  
  % latitude of the response function points
  temp=fread(fid,1,'int'); 
  temp=fread(fid,temp/4,'float'); 
  if(pos == save_pos && az == save_az)
    lat=reshape(temp,[m n]); 
  end
  temp=fread(fid,1,'int');
  
  % longitude of the response function points
  temp=fread(fid,1,'int'); 
  temp=fread(fid,temp/4,'float');
  if(pos == save_pos && az == save_az)
      lon=reshape(temp,[m n]); 
  end
  temp=fread(fid,1,'int');
  
  % the response function
  temp=fread(fid,1,'int');
  temp=fread(fid,temp/4,'float');
  if(pos == save_pos && az == save_az)
       resp=reshape(temp,[m n 8]); 
  end
  temp=fread(fid,1,'int');
  
  % significant points
  temp=fread(fid,1,'int'); temp=fread(fid,temp/4,'int');
  if(pos == save_pos && az == save_az)
      sig_m(1)=temp(1); sig_n(1)=temp(2); sig_m(2)=temp(3); sig_n(2)=temp(4); sig_m(3)=temp(5); sig_n(3)=temp(6); 
  end
  temp=fread(fid,1,'int');
  
  % centers of slices
  temp=fread(fid,1,'int'); temp=fread(fid,temp/4,'int');
  if(pos == save_pos && az == save_az)
    m1=temp([1,3,5,7,9,11,13,15]); n1=temp([2,4,6,8,10,12,14,16]);
  end
  temp=fread(fid,1,'int');


end
end
temp=fclose(fid);










