function varargout = modisDisplay(varargin)
% MODISDISPLAY M-file for modisDisplay.fig
%      MODISDISPLAY, by itself, creates a new MODISDISPLAY or raises the existing
%      singleton*.
%
%      H = MODISDISPLAY returns the handle to a new MODISDISPLAY or the handle to
%      the existing singleton*.
%
%      MODISDISPLAY('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MODISDISPLAY.M with the given input arguments.
%
%      MODISDISPLAY('Property','Value',...) creates a new MODISDISPLAY or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before modisDisplay_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to modisDisplay_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help modisDisplay

% Last Modified by GUIDE v2.5 14-Sep-2005 16:46:32

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @modisDisplay_OpeningFcn, ...
                   'gui_OutputFcn',  @modisDisplay_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before modisDisplay is made visible.
function modisDisplay_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to modisDisplay (see VARARGIN)

% Get image data from collocation.fig
if(nargin > 3)
    for index = 1:2:(nargin-3),
        if nargin-3==index break, end
        switch lower(varargin{index})
         case 'airs'
           airs = varargin{index+1};
           handles.airs.filename = airs.filename;
           handles.airs.index = airs.index;
           handles.airs.lat = airs.lat;
           handles.airs.lon = airs.lon;
           clear airs;
         case 'modis'
           handles.modis = varargin{index+1};
         case 'colloc'
           handles.colloc = varargin{index+1};
        end
    end
end

handles.machineFormat = 'ieee-be';
handles.modis.band = 0;
handles.modis.x_band = 0;
handles.modis.y_band = 0;
handles.modis.refine = [];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%Commented out  classification mask as a default
%%%This elimnates the need for jliclass9_new.dat file
%%%to exist.  This should rather be an option.  New default
%%%will be MODIS Band 31 (~11um)  JS 10/24/2005
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Load MODIS classification mask
%handles.modis.select_name = 'Classification Mask';
%handles = load_data(handles);

% Get selected MODIS elements (radiance measurement)
%handles = select_modis(handles);

% Plot classification mask
%handles.modis.select_name = 'Classification Mask';
%handles = plotModisImage(handles);

handles.modis.select_name = 'MODIS Level1b';
handles.modis.band = 31;
handles = load_modis1b(handles);
% Get selected MODIS elements (radiance measurement)
handles = select_modis(handles);
handles = plotModisImage(handles);

% Choose default command line output for modisDisplay
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes modisDisplay wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = modisDisplay_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --------------------------------------------------------------------
function plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------
function select_menu_Callback(hObject, eventdata, handles)
% hObject    handle to select_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[lat, lon] = inputm(1, handles.modis_axes);
airs_index = searchAirsElement(handles.airs.lat, handles.airs.lon, lat, lon);
if airs_index < 0
  errordlg('Not nearby AIRS Element', 'Try again');
  return
end
handles.airs.oval_index = airs_index;
clear airs_index;
airsDisplay('airs', handles.airs, 'modis', handles.modis, 'colloc', handles.colloc);


% --------------------------------------------------------------------
function Save_menu_Callback(hObject, eventdata, handles)
% hObject    handle to Save_mane (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

save_image(handles.modis_figure)


% --------------------------------------------------------------------
function refine_menu_Callback(hObject, eventdata, handles)
% hObject    handle to refine_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if isempty(handles.modis.refine)
  handles = select_modis_domain_refine(handles);
end

% Get selected MODIS elements's data
handles = select_modis_refine(handles);
handles = plotModisImageRefine(handles);

% Update handles structure
guidata(hObject, handles);

% --------------------------------------------------------------------
function modis1b_plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to modis1b_plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function diff_modis1b_plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to modis1b_plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function cmask_plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to cmask_plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

var = get(hObject, 'Label');
handles.modis.select_name = var;
handles = load_data(handles);
% Get selected MODIS elements
handles = select_modis(handles);
handles = plotModisImage(handles);

% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------
function cphase_plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to cphase_plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

var = get(hObject, 'Label');
handles.modis.select_name = var;
handles = load_data(handles);
% Get selected MODIS elements 
handles = select_modis(handles);
handles = plotModisImage(handles);

% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------
function tpw_plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to tpw_plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

var = get(hObject, 'Label');
handles.modis.select_name = var;
handles = load_data(handles);
% Get selected MODIS elements
handles = select_modis(handles);
handles = plotModisImage(handles);

% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------
function modis_cmask_plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to modis_cmask_plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

var = get(hObject, 'Label');
handles.modis.select_name = var;
handles = load_data(handles);
% Get selected MODIS elements
handles = select_modis(handles);
handles = plotModisImage(handles);

% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------
function modis_ctp_plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to modis_ctp_plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

var = get(hObject, 'Label');
handles.modis.select_name = var;
handles = load_data(handles);
% Get selected MODIS elements
handles = select_modis(handles);
handles = plotModisImage(handles);

% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------
function water_path_plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to cmask_plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

var = get(hObject, 'Label');
handles.modis.select_name = var;
handles = load_data(handles);
% Get selected MODIS elements 
handles = select_modis(handles);
handles = plotModisImage(handles);

% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------
function particle_radius_plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to cphase_plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

var = get(hObject, 'Label');
handles.modis.select_name = var;
handles = load_data(handles);
% Get selected MODIS elements 
handles = select_modis(handles);
handles = plotModisImage(handles);

% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------
function cloud_thickness_plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to tpw_plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

var = get(hObject, 'Label');
handles.modis.select_name = var;
handles = load_data(handles);
% Get selected MODIS elements 
handles = select_modis(handles);
handles = plotModisImage(handles);

% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------
function cloud_emissivity_plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to modis_cmask_plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

var = get(hObject, 'Label');
handles.modis.select_name = var;
handles = load_data(handles);
% Get selected MODIS elements 
handles = select_modis(handles);
handles = plotModisImage(handles);

% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------
function layered_cmask_plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to modis_ctp_plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

var = get(hObject, 'Label');
handles.modis.select_name = var;
handles = load_data(handles);
% Get selected MODIS elements 
handles = select_modis(handles);
handles = plotModisImage(handles);

% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------
function layered_cphase_plot_menu_Callback(hObject, eventdata, handles)
% hObject    handle to modis_ctp_plot_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

var = get(hObject, 'Label');
handles.modis.select_name = var;
handles = load_data(handles);
% Get selected MODIS elements
handles = select_modis(handles);
handles = plotModisImage(handles);

% Update handles structure
guidata(hObject, handles);
