%**************************************************************************
%--------------------------------------------------------------------------
%Matlab m_map MODIS chlorophyll mapping code
%L. McKinna
%James Cook University
%April, 2010
%--------------------------------------------------------------------------
%
% 1.) Let's make sure the memory is clear and we have no other files or figures
%open.
clear all; close all; fclose all;
%
%--
% 2.)Allocate your L2 file name
%L2_file = '**some**MODIS**file**here.L2';
%
%--
% 3.) Read in the products you require using the hdfread function
productCHL = hdfread(L2_file,'chl_gsm01');  
%you may have to change this depending on what CHL algorithm you used. I
%have assumed the gsm01 for this example
LON = hdfread(L2_file,'longitude');
LAT = hdfread(L2_file,'latitude');
%
%--
%4.) Turn data into double presision from 16bit data.
CHL = double(productCHL);
%
%Set negative values of CHL to zero.
CHL(CHL(:) < 0) = 0; 
%
%--
%5.) Remove redundant variables (free up some memory!)
clear productCHL
clear longitude
clear latitude
%
%--
% 6.) Let's create a map projection with your desired limits.  For this
% example we'll use a latitude range of 32.25 - 34.25 N and longitude
%range of 130.75 - 132.75 E with a mercator projection.
%
North = 34.25; 
South = 32.25;
East = 132.75; 
West = 132.25;
%
m_proj('mercator','lon',[West  East],'lat',[South North]);
%
%--
% 7.) Now we map our L2 product (in this case Chl) using m_pcolor function.
m_pcolor(LON,LAT,CHL); 
shading flat;
%
%--
% 8.) Now draw the coastline in black and land as grey using m_gshhs_h
% (high resolution coast).  Also add a grid using m_grid funtion
m_gshhs_h('patch',[.5 .5 .5],'edgecolor','k');
m_grid('color','k')
%
%--
% 9.) Set the range of CHL values you want displayed (example 0-5 ug/L).
caxis([0 5]);
%
%--
% 10.) Add the fancy border to the plot.
m_grid('box','fancy','tickdir','in');
%
%--
% 11.) Apply the seadas color map
chl_cmap = dlmread('chl_colormap.dat');
colormap(chl_cmap);
%
%--
% 12.) Add a colorbar to the plot 
cbar1 = colorbar('location','EastOutside');
set(get(cbar1 ,'ylabel'),'String', 'mg m^{-3}','Rotation',90);
%
%--
% 13.) Save the matlab figure as a .png file, I have assumed that the L2
% file has 14 characters before the file extension ie. Ayyyydddhhmmss.L2
save_filename = [L2_file(1:14),'_CHL.png'];
saveas(gcf,save_filename,'png');
close all;
fclose all;
%--------------------------------------------------------------------------
%                             ALL DONE!
%--------------------------------------------------------------------------
