%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Changing to the data directory
cd ('OMI_Daily_2007');  % Change directories to where data is located
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Finding the number of files in the directory
OMIdir = dir('OMI-Aura_L3-OMTO3d*.he5');
num_months = length(OMIdir);
OMI_waiting = waitbar(0,'Please wait, working on a lot of OMI files.');
%
n = 1;
while n <= length(OMIdir)
    name = getfield(OMIdir(n),'name');
    disp(['working on: ', name])
    %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    % Getting out the year and month information from the filename
    % File name format:  OMI-Aura_L3-OMTO3d_2007m1231_v003-2012m0405t203652
    %
    yearyear=name(20:23);      % Takes out the year 'YYYY' which exists 10-7 spaces from the first M
    monthmonth=name(25:26);    % Takes out the year 'YYYY' which exists 10-7 spaces from the first M
    dayday=name(27:28);        % Takes out the year 'YYYY' which exists 10-7 spaces from the first M
    %
    %year=str2num(yearyear);   %  turns the string (text) year into a number value
    %
    OMI_AI = 'OMI_AI_';  % Making a Text String
    spacer = '_';
    AI_filename_save_string = strcat(OMI_AI, yearyear, spacer, monthmonth, spacer, dayday); % stringing Name and Year
    %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    % Extracting the optical depth information
    %    
    %AI_data = hdf5read(name,'UVAerosolIndex');
    
    % Open the HDF5 File.
    FILE_NAME = name;
    file_id = H5F.open (FILE_NAME, 'H5F_ACC_RDONLY', 'H5P_DEFAULT');
    % Open the dataset.
    DATAFIELD_NAME = '/HDFEOS/GRIDS/OMI Column Amount O3/Data Fields/UVAerosolIndex';
    data_id = H5D.open (file_id, DATAFIELD_NAME);

    % Get dataspace. 
    data_space = H5D.get_space (data_id);
    [data_numdims data_dims data_maxdims]= H5S.get_simple_extent_dims (data_space);
    data_dims=fliplr(data_dims');

    % Read the dataset.
    OMI_data=H5D.read (data_id,'H5T_NATIVE_DOUBLE', 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT');

    % Transpose the data to match the map projection.
    OMI_data=OMI_data';

    % Release resources.
    H5S.close (data_space)

    % Read the units.
    ATTRIBUTE = 'Units';
    attr_id = H5A.open_name (data_id, ATTRIBUTE);
    units = H5A.read(attr_id, 'H5ML_DEFAULT');

    % Read the fill value.
    ATTRIBUTE = '_FillValue';
    attr_id = H5A.open_name (data_id, ATTRIBUTE);
    fillvalue=H5A.read (attr_id, 'H5T_NATIVE_DOUBLE');

    % Read the missing value.
    ATTRIBUTE = 'MissingValue';
    attr_id = H5A.open_name (data_id, ATTRIBUTE);
    missingvalue=H5A.read (attr_id, 'H5T_NATIVE_DOUBLE');

    % Read title attribute.
    ATTRIBUTE = 'Title';
    attr_id = H5A.open_name (data_id, ATTRIBUTE);
    long_name=H5A.read (attr_id, 'H5ML_DEFAULT');


    % Close and release resources.
    H5A.close (attr_id)
    H5D.close (data_id);
    H5F.close (file_id);
    
%

OMI_9999 = find(OMI_data < 0);
OMI_data(OMI_9999) = NaN;
dlmwrite([AI_filename_save_string,'.csv'], OMI_data,'delimiter', ',', 'precision', 15);
%      
waitbar(n/length(OMIdir));
    n = n+1;
end
close (OMI_waiting)

    % 