function productData = readProductsFromMod07(filename, keywords)

% function productData = readProductsFromMod07(filename, keywords)
%
% filename must be full path of MOD07 MODIS .hdf file
%
% keywords can be 
%  'Scan_Start_time'
%  'Latitude'
%  'Longitude'
%  'Water_Vapor'  (total precipitable water vapor, in mm)
%  'Ozone' (total ozone, dobson units)
%  'Surface_Pressure' (surface -- not mean sea level -- pressure, in hPa)
%  'Sensor_Zenith'


% Open the file again and open the sds dataset
 sd_id = hdfsd( 'start', filename, 'read' );
 if (sd_id < 0)
    error(['HDF file' filename ' was not opened.']);
 end
 
 if ismember('Scan_Start_Time',keywords)
    index = hdfsd('nametoindex',sd_id,'Scan_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 ;0],[1;1],dimsizes) ;
    status = hdfsd('endaccess',sds_id);
    if (status < 0)
       warning(['HDF file' filename ' dataset Scan_Start_Time was not closed.']);
    end
    productData.Scan_Start_Time = double(rot90(flipud(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)));
 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)));
 end

 if ismember('Water_Vapor',keywords)
    index = hdfsd('nametoindex',sd_id,'Water_Vapor');
    sds_id = hdfsd('select',sd_id, index);
    [name,rank,dimsizes,data_type,nattrs,status] = hdfsd('getinfo', sds_id);
%     npixels_across =  dimsizes(1);
% 	npixels_along = dimsizes(2); 
% 	
% 	start = [0; 0]; 
% 	count = [1 ; 1];
% 	edge = [npixels_across ;  npixels_along];
% 	
% 	
%      if (nargin == 3)
%         start(1) = min( [max([area(1) 0]) (npixels_across - 1)] );
%         start(2) = min( [max([area(2) 0]) (npixels_along - 1)] );
%         edge(1) = min( [max([area(3) 0]) (npixels_across - start(1))] );
%         edge(2) = min( [max([area(4) 0]) (npixels_along - start(2))] );
%     end
    [data,status] = hdfsd('readdata',sds_id,[0;0],[1;1],dimsizes) ;
    status = hdfsd('endaccess',sds_id);
    if (status < 0)
       warning(['HDF file' filename ' dataset Water_Vapor was not closed.']);
    end
    %% MODIS TPW is in cm and scaled 0.001, convert to mm
    productData.Water_Vapor = double(rot90(flipud(data)));
    neg = find(productData.Water_Vapor < 0);
    productData.Water_Vapor = (productData.Water_Vapor*0.001)*10;
    productData.Water_Vapor(neg) = NaN;
 end 

 if ismember('Ozone',keywords)
    index = hdfsd('nametoindex',sd_id,'Total_Ozone');
    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 Total_Ozone was not closed.']);
    end
    %% MODIS Ozone is in Dobson and scaled 0.1
    productData.Ozone = double(rot90(flipud(data)));
    neg = find(productData.Ozone < 0);
    productData.Ozone =productData.Ozone*0.1;
    productData.Ozone(neg) = NaN;
 end 

 if ismember('Surface_Pressure',keywords)
    index = hdfsd('nametoindex',sd_id,'Surface_Pressure');
    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 Surface_Pressure was not closed.']);
    end
    %% MODIS PSFC is in hPa and scaled 0.1
    productData.Surface_Pressure = double(rot90(flipud(data))); 
    neg = find(productData.Surface_Pressure < 0);
    productData.Surface_Pressure =productData.Surface_Pressure*0.1;
    productData.Surface_Pressure(neg) = NaN;
 end 

 if ismember('Sensor_Zenith',keywords)
    index = hdfsd('nametoindex',sd_id,'Sensor_Zenith');
    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 Sensor_Zenith was not closed.']);
    end
    %% MODIS Sensor Zenith is in degrees and scaled 0.01
    productData.Sensor_Zenith = double(rot90(flipud(data))); 
    neg = find(productData.Sensor_Zenith < 0);
    productData.Sensor_Zenith =productData.Sensor_Zenith*0.01;
    productData.Sensor_Zenith(neg) = NaN;
 end

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

