
clc;clear all;close all;

%EXAMPLE CODE FOR MAPPING AMSR-E PARAMETER RETRIEVALS======================
%  This example routine loads and maps AMSR-E geophysical parameter data
%
%Requires function: mkgrid_global.m
%
% The user should change the path variable 'MyDir' to their specific system
% location.  The user should also check that the paths structure to ancilary
% and data files (below) are correct for their specific system location.
%
% Author: Lucas A. Jones
% Organization: NTSG, College of Forestry and Conservation
%               University of Montana
% Email: lucas@ntsg.umt.edu
% Date: January 21, 2010
%--------------------------------------------------------------------------

%Clear the workspace variables, clear the command window, and close all
%open figures
clc;clear all;close all;


%Modify these lines to choose your directory, year, date, overpass, parameter
%that you want to view, the associated multiplication factor for the
%parameter, the parameter's expected bounds (for color scaling) and
%and whether you'd like to see maps or images.

MyDir = '.\data';    %Type the path to your data
year = '2010';          %Type the year
date = '123';           %Type the date (pad with zeros to three digits: e.g. '006')
pass = 'D';             %Type the overpass: 'A' or 'D'
param = 'flag';           %Type the parameter
factor = 1e-0;          %Type the multiplier associated with the factor
bnds = [0 255];       %Type the lower and upper bounds of the parameter
maps = 'yes';           %Enter 'yes' to seem maps and 'no' to see images

%Load the data to view
fid = fopen([MyDir '\' year '\AMSRU_Mland_' year date pass '.' param],'r');x=fread(fid,209091,'int8');fclose(fid);
x(x<0.0) = NaN; x = x.*factor; data = x;

%Load latitude and longitude data (only used for mapping, not for producing
%images
fid = fopen([ '.\anci\MLLATLSB'],'r');late=fread(fid,[1383 586],'int32');fclose(fid);
fid = fopen([ '.\anci\MLLONLSB'],'r');lone=fread(fid,[1383 586],'int32');fclose(fid);

late = late'.*1e-5;
lone = lone'.*1e-5;

%Make the data into EASE grids for viewing
datagrid = mkgrid_global(data);


if strcmp(maps,'yes')==1 %Requires the MATLAB Mapping Toolbox
    
    load coast
    %Plot a map of the data
    figure
    axesm eqdcylin
    surfm(late,lone,datagrid);
    set(gca,'CLim',bnds)
    plotm(lat,long,'k')
    colormap(jet)
    colorbar('southoutside')
    title([param ' ' year date pass],'fontsize',14)
    setm(gca,'origin',[0 0 0],'frame','on','grid','on', 'maplatlimit',[-65 85])
    set(gcf,'Position',[38   152   755   357])
    
else
    
    %Plot an image of the data
    figure
    imagesc(datagrid,bnds)
    colormap(jet)
    colorbar
    title([param ' ' year date pass],'fontsize',14)
    set(gcf,'Position',[38   152   755   357])
end
