function varargout = DM(varargin)
% DM M-file for DM.fig
%      DM, by itself, creates a new DM or raises the existing
%      singleton*.
%
%      H = DM returns the handle to a new DM or the handle to
%      the existing singleton*.
%
%      DM('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DM.M with the given input arguments.
%
%      DM('Property','Value',...) creates a new DM or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before DM_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to DM_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 DM

% Last Modified by GUIDE v2.5 09-Feb-2012 11:34:07

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @DM_OpeningFcn, ...
                   'gui_OutputFcn',  @DM_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(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

%Add path info
path = strrep(which('DM'),'DM.m','');
addpath([path filesep 'XBeach_Toolbox_Code'])



% --- Executes just before DM is made visible.
function DM_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 DM (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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



% This creates the 'background' axes
ha = axes('units','normalized', ...
'position',[0 0 1 1]);

% Move the background axes to the bottom
uistack(ha,'bottom');

% Load in a background image and display it using the correct colors
% The image used below, is in the Image Processing Toolbox. If you do not have %access to this toolbox, you can use another image file instead.
dune_image = imread('Background1.jpg');
image(dune_image);

% Turn the handlevisibility off so that we don't inadvertently plot into the axes again
% Also, make the axes invisible
set(ha,'handlevisibility','off', ...
'visible','off')

%Hide axes on startup
set(handles.dune,'Visible','off')
set(handles.nodune,'Visible','off')
set(handles.profile,'Visible','off')
set(handles.changeprofile,'Visible','off')



% --- Outputs from this function are returned to the command line.
function varargout = DM_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;



% --- Executes on button press in loaddata.
function loaddata_Callback(hObject, eventdata, handles)
% hObject    handle to loaddata (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

DM_load_data(handles)



% --- Executes on button press in pushbutton2.
%function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in drawduneline.
function drawduneline_Callback(hObject, eventdata, handles)
% hObject    handle to drawduneline (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

DM_drawduneline(handles)

% --- Executes on button press in deleteduneline.
function deleteduneline_Callback(hObject, eventdata, handles)
% hObject    handle to deleteduneline (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

DM_deleteduneline(handles)

% --- Executes on button press in calculatedune.
function calculatedune_Callback(hObject, eventdata, handles)
% hObject    handle to calculatedune (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data
DM_startdigging
DM_plot_dune(handles)
if isnumeric(data.duneline)
    crest_line = data.duneline;
else
    crest_line = getPosition(data.duneline);
end
crest_line = interp1(crest_line(:,2),crest_line(:,1),data.y(:,1)); %Interp crest line 2 grid
crest_line_y = data.y(~isnan(crest_line),1);
Index = interp1(data.y(:,1),1:length(data.y(:,1)),(max(crest_line_y)+min(crest_line_y))/2,'nearest');
DM_plot_profile(handles,Index)


% --- Executes on button press in showcontours.
function showcontours_Callback(hObject, eventdata, handles)
% hObject    handle to showcontours (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

DM_showcontours(hObject)

% --- Executes on button press in showBWD.
function showBWD_Callback(hObject, eventdata, handles)
% hObject    handle to showBWD (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of showBWD

DM_showBWD(hObject,handles)


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

% Hints: get(hObject,'String') returns contents of duneheight as text
%        str2double(get(hObject,'String')) returns contents of duneheight as a double

DM_duneheight(hObject)


% --- Executes during object creation, after setting all editdunedimensions.
function duneheight_CreateFcn(hObject, eventdata, handles)
% hObject    handle to duneheight (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of dunewidth as text
%        str2double(get(hObject,'String')) returns contents of dunewidth as a double

DM_dunewidth(hObject);

% --- Executes during object creation, after setting all editdunedimensions.
function dunewidth_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dunewidth (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on slider movement.
function changeprofile_Callback(hObject, eventdata, handles)
% hObject    handle to changeprofile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

DM_changeprofile(hObject,handles)


% --- Executes during object creation, after setting all editdunedimensions.
function changeprofile_CreateFcn(hObject, eventdata, handles)
% hObject    handle to changeprofile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



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

% Hints: get(hObject,'String') returns contents of startdepth as text
%        str2double(get(hObject,'String')) returns contents of startdepth as a double


% --- Executes during object creation, after setting all editdunedimensions.
function startdepth_CreateFcn(hObject, eventdata, handles)
% hObject    handle to startdepth (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in savegrid.
function savegrid_Callback(hObject, eventdata, handles)
% hObject    handle to savegrid (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

DM_savegrid


% --- Executes when figure1 is resized.
function figure1_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


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


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


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


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

DM_loadduneline(handles)

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


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


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


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


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

DM_displayinformation


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

DM_aboutdunemaker


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

DM_editdunedimensions(handles)

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


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

DM_change_profilelimits(handles)

% --- Executes on button press in calculatedune_menu.
function calculatedune_menu_Callback(hObject, eventdata, handles)
% hObject    handle to calculatedune_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global data
DM_startdigging
DM_plot_dune(handles)
crest_line = data.duneline;
crest_line = interp1(crest_line(:,2),crest_line(:,1),data.y(:,1)); %Interp crest line 2 grid
crest_line_y = data.y(~isnan(crest_line),1);
Index = interp1(data.y(:,1),1:length(data.y(:,1)),(max(crest_line_y)+min(crest_line_y))/2,'nearest');
DM_plot_profile(handles,Index)
