function [data] = read_collocation(filename)

% Read collocation information into structure array
% from the file produced by '../fortran/inovalxx.f'
% Each cell element is an AIRS oval. It includes, AIRS element index,
% and latitude/longitude, and the number of elements of MODIS for each
% AIRS oval, latitude, longitude and weight of each MODIS element.
%

fid = fopen(filename);
if fid == -1
  disp(['AIRS/MODIS collocation file is NOT found: ' filename])
  return
end

n = 0;
while 1
  val = struct('apixel', 0, 'alat', 0, 'alon', 0, 'mpixels', 0, ...
               'index', [], 'lat', [], 'lon', [], 'weight', []);
  n = n + 1;
  % Read airs grid index, latitude and longitude
  [tmp, count] = fscanf(fid, '%d %g %g', 3);
  if count <= 0
    break
  end
  val.apixel = tmp(1);
  val.alat = tmp(2);
  val.alon = tmp(3);

  % Read Modis the number of MODIS pixels in one AIRS pixel oval
  [nmodis] = fscanf(fid, '%d', 1);

  % Read each of MODIS grid index, latitude, longitude and weight
  val.index = zeros(1, nmodis);
  val.lat = zeros(1, nmodis);
  val.lon = zeros(1, nmodis);
  val.weight = zeros(1, nmodis);
  for m = 1:nmodis
    [tmp] = fscanf(fid, '%d %g %g %g %g', 5);
    val.index(m) = tmp(1);
    val.lat(m) = tmp(2);
    val.lon(m) = tmp(3);
    val.weight(m) = tmp(4);
    val.distance(m) = tmp(5);
  end
  val.mpixels = nmodis;
  data(n) = val;
end

fclose(fid);
