function productData = readProductsFromMod03(filename, keywords)
%
% Read MODIS level03 collocation file
%
% This function is used to output data from a MODIS03 Collocation file
%  to be used in the AIRS/MODIS collocation routine
% Kevin Baggett (based on code by Suzanne Wetzel-Seamann)
% Mon Dec 16 22:29:43 GMT 2002
% filename must be full path of MOD03 MODIS .hdf file
%
% keywords can be 
%  'scanStartTime'
%  'latitude'
%  'longitude'
%  'satHeight'
%  'scanAngle'

% Open the file and open the sds dataset
 sd_id = hdfsd( 'start', filename, 'read' );
 if (sd_id < 0)
    error(['MODIS03 HDF file is NOT found: ' filename]);
 end
 
 if ismember('scanStartTime',keywords)
    index = hdfsd('nametoindex',sd_id,'EV start time'); 
    sds_id = hdfsd('select',sd_id, index);
    [name,rank,dimsizes,data_type,nattrs,status] = hdfsd('getinfo', sds_id);
    [data,status] = hdfsd('readdata',sds_id,0,1,dimsizes) ;
    status = hdfsd('endaccess',sds_id);
    if (status < 0)
       warning(['HDF file' filename ' dataset scanStartTime was not closed.']);
    end
    productData.Time = data;
 end

 if ismember('latitude',keywords)
    index = hdfsd('nametoindex',sd_id,'Latitude'); 
    sds_id = hdfsd('select',sd_id, index);
    [name,rank,dimsizes,data_type,nattrs,status] = hdfsd('getinfo', sds_id);
    [data,status] = hdfsd('readdata',sds_id,[0 ;0],[1;1],dimsizes) ;
    status = hdfsd('endaccess',sds_id);
    if (status < 0)
       warning(['HDF file' filename ' dataset Latitude was not closed.']);
    end
%   productData.Latitude = double(rot90(flipud(data)));
    productData.Latitude = double(data);
 end

 if ismember('longitude',keywords)
    index = hdfsd('nametoindex',sd_id,'Longitude'); 
    sds_id = hdfsd('select',sd_id, index);
    [name,rank,dimsizes,data_type,nattrs,status] = hdfsd('getinfo', sds_id);
    [data,status] = hdfsd('readdata',sds_id,[0 ;0],[1;1],dimsizes) ;
    status = hdfsd('endaccess',sds_id);
    if (status < 0)
       warning(['HDF file' filename ' dataset Longitude was not closed.']);
    end
%   productData.Longitude = double(rot90(flipud(data)));
    productData.Longitude = double(data);
 end

 if ismember('satHeight',keywords)
    index = hdfsd('nametoindex',sd_id,'Range');
    sds_id = hdfsd('select',sd_id, index);
    [name,rank,dimsizes,data_type,nattrs,status] = hdfsd('getinfo', sds_id);
    [data,status] = hdfsd('readdata',sds_id,[0;0],[1;1],dimsizes) ;
    status = hdfsd('endaccess',sds_id);
    if (status < 0)
       warning(['HDF file' filename ' dataset satellite height was not closed.']);
    end
    %% Range is in meters, needs to be doubled and multipled by 25
%   productData.satHeight = double(rot90(flipud(data)))*25;
    productData.satHeight = double(data)*25;
    % Line 677 is approximately the satellite height at nadir
    % productData.satHeight=productData.satHeight(:,677);
 end 

 if ismember('scanAngle',keywords)
    index = hdfsd('nametoindex',sd_id,'SensorZenith');
    sds_id = hdfsd('select',sd_id, index);
    [name,rank,dimsizes,data_type,nattrs,status] = hdfsd('getinfo', sds_id);
    [data,status] = hdfsd('readdata',sds_id,[0 ;0],[1;1],dimsizes) ;
    status = hdfsd('endaccess',sds_id);
    if (status < 0)
       warning(['HDF file' filename ' Dataset scan angle was not closed.']);
    end
    %% Scan angle is SensorZenith doubled * 0.01
%   productData.satZenith = double(rot90(flipud(data)))*0.01;
    productData.satZenith = double(data)*0.01;
end 

% Close the file 
 status = hdfsd('end',sd_id);
 if (status < 0)
    warning(['HDF file' filename ' was not closed.']);
 end
