Extreme heat and air pollution kill!

Tag: MODIS data processing in MATLAB

How to process and visualize MODIS data in MATLAB

In an earlier post, I talked about how to download MODIS data in NetCDF format. Now I will tell you how to process the downloaded data in MATLAB.

Note that there can be be data outages on certain days so you might see less data than expected. For example, MODIS Aqua data for the year 2008 will have only 364 days of data although 366 (2008 is a leap year) is expected. Check the day stamp in the file name to know exactly which day is missing. I will be processing the 2008 data below. Further, I have noticed that on certain days (2-3 days in a year), the filenames are extra long with some added information during data processing in Giovanni. If you see such unusual names, you have to manually rename the files to make the names consistent with other files.

Read data

  1. Copy all the files in your MATLAB workspace.
  2. Since there are so many daily files, we need to read all the files using a loop. So follow the following steps:
file_names = dir ('*.nc'); % extract filenames and associated information
num_files = length(file_names); %num_files in this case is 364

deep_aod = zeros (360, 180, num_files); % 180 and 360 are latitude and longitude grids in each MODIS data
for m = 1:num_files;
deep_aod (:, :, m) = ncread(file_names(m).name, 'Deep_Blue_Aerosol_Optical_Depth_550_Land_Mean');
end;

The above code will create a 3-d workspace variable named deep_aod (360*180*364) in which 360, 180, and 364 represents longitude, latitude, and days of year 2008, respectively.

Calculate average for plotting

% change the dimensions of latitude and longitude to make it more intuitive (we want to see latitude in the rows and longitudes in the columns)

deep_aod_arng = permute(deep_aod, [2 1 3]); 

%bring the time dimension to the front for easier processing
deep_aod_arng_time = permute(deep_aod_arng, [3 1 2]); 

% convert the 3-d data to 2-d data so that we can apply vector operations in MATLAB, the resulting rows represent each day and columns represent each grid points in the entire globe.
deep_aod_arng_points = reshape(deep_aod_arng_time, [364 64800]); 

Upon examining the data files we see that the days 107 and 309 days are missing. So lets insert two arrays in the respective place so that the date represents complete year and it is easier for further processing. 

%create a dummy row of NaN to be inserted

dummy = NaN (1, 64800); 

%insert the dummy row at the respective rows

deep_aod_arng_points_complete = [deep_aod_arng_points (1:106, :);dummy;deep_aod_arng_points (107:308, :);dummy;deep_aod_arng_points (309:end, :)];

Now create a timetable using this data; it will make it very easy for further calculations, for example, for calculating monthly mean.

%first create a datetime array for 2008

t1 = datetime (2008, 01, 01);
t2 = datetime (2008, 12, 31);
t = (t1:days:t2)';

% now create the timetable

deep_aod_2008_timetable = timetable(t, deep_aod_arng_points_complete);

%now lets calculate the monthly mean AOD values using this timetable

aod_monmean_2008 = retime (deep_aod_2008_timetable, 'monthly', 'mean'); %

Plot the data

% To plot the data, we need to read the latitude and longitude data from any one MODIS file:

MYD_lat = ncread('MYD08_D3.A2008001.061.2018031095350.hdf.nc', 'YDim');
MYD_lon = ncread('MYD08_D3.A2008001.061.2018031095350.hdf.nc', 'XDim');

% create a 2-d grid of latitude and longitude to be used for plotting

lat_mat = repmat(MYD_lat, 1, 360);
lon_mat = repmat(MYD_lon', 180, 1);

Now plot data with the below script using geoshow.

h1 = figure('position', [50 50 1200 600]); 
axesm mercator
p = worldmap([-90 90],[-180 180]);
%borders('countries', 'Color', 'black', 'linewidth', 0.75) 
gridm('on');
setm (p, 'mlabellocation', 40); % for reference_longitude
setm (p, 'plabellocation', 20); % for reference_latitude
geoshow('landareas.shp', 'linewidth', 1.2, 'facecolor', 'none')
h2 = geoshow (lat_mat, lon_mat, reshape(aod_monmean_2008.deep_aod_arng_points_complete(1, :), [180 360]), 'displaytype', 'surface');
caxis([0, 1]);        
title('MODIS AOD Jan 2008');
colormap jet; brighten(0.5);
h3 = colorbar;
set(h3,'fontsize', 12);

You should get a plot like below:

Note: always verify the accuracy of the plotted data from your intuition or secondary sources. For example, in this case, I know that bodele in central Africa is one of the most active dust source region so it should have high AOD which I can see in this plot.

Thank you!

How to download MODIS aerosol data in NetCDF format?

MODIS data can be downloaded from EarthData portal easily. Remember you have to register and login to download the data. Unfortunately, the data there are available in HDF format like in most other portals. But there is one portal from where you can download processed MODIS data in NetCDF format which can be easily handled in MATLAB and other programs. The name of that portal is Giovanni. Let me explain how you can download MODIS data in NetCDF format from this website:

Example of data visualization from Giovanni.
  1. MODIS data are available in daily/monthly formats from both Terra (MOD*) and Aqua (MYD*) platforms. I will be downloading MODIS Aqua daily (MYD08_D3) data here. Go to the Giovanni website and enter the term MYD08_D3 in the search area. Be sure to login first; the same credentials from EarthData can be used. Enter the required date range. You can download data for the entire globe since data files are not that big. Let the plot be ‘Time Average Map’. After the search, select the desired variable, here I use “Aerosol Optical Depth 550 nm (Deep Blue, Land-only) (MYD08_D3 v6.1)”. Then click ‘plot data’.
  2. In the upper left corner, click ‘lineage’, and go to the heading ‘Data File Staging’. Then click “Download list of all URLs in step” which will download the link of all input files (*.nc) in a text file. If you examine the data links inside the text file, you will see that the input files are in NetCDF format, not in HDF format.
  3. Rename the text file to modis_data_list.txt and copy this file to your unix/linux workspace. Change the permission of this file by: chmod 777 modis_data_list.txt.
  4. Now download the data listed in the modis_data_list.txt by using the command wget: wget –content-disposition -i modis_data_list.txt
  5. The above step will download all the data requested in NetCDF format.

In the next post, I will explain how to process and visualize the downloaded data in MATLAB.

Powered by WordPress & Theme by Anders Norén