function varargout = AddDatasetD3DDepth(varargin)
% ADDDATASETD3DDEPTH M-file for AddDatasetD3DDepth.fig
%      ADDDATASETD3DDEPTH, by itself, creates a new ADDDATASETD3DDEPTH or raises the existing
%      singleton*.
%
%      H = ADDDATASETD3DDEPTH returns the handle to a new ADDDATASETD3DDEPTH or the handle to
%      the existing singleton*.
%
%      ADDDATASETD3DDEPTH('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ADDDATASETD3DDEPTH.M with the given input arguments.
%
%      ADDDATASETD3DDEPTH('Property','Value',...) creates a new ADDDATASETD3DDEPTH or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before AddDatasetD3DDepth_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to AddDatasetD3DDepth_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 AddDatasetD3DDepth
 
% Last Modified by GUIDE v2.5 04-Dec-2007 14:00:07
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @AddDatasetD3DDepth_OpeningFcn, ...
                   'gui_OutputFcn',  @AddDatasetD3DDepth_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 AddDatasetD3DDepth is made visible.
function AddDatasetD3DDepth_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 AddDatasetD3DDepth (see VARARGIN)
 
% Choose default command line output for AddDatasetD3DDepth
 
handles.output=hObject;
 
handles.DataProperties=varargin{2};
handles.PathName=varargin{4};
handles.FileName=varargin{6};
handles.NrAvailableDatasets=varargin{8};
 
PutInCentre(hObject);
 
set(handles.EditDatasetName,'String',handles.FileName);
 
set(handles.TextPolyFile,    'Enable','off','BackGroundColor',[0.831 0.816 0.784]);
 
 
% Update handles structure
guidata(hObject, handles);
 
% UIWAIT makes AddDatasetD3DDepth wait for user response (see UIRESUME)
uiwait(handles.figure1);
 
 
% --- Outputs from this function are returned to the command line.
function varargout = AddDatasetD3DDepth_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;
varargout{2} = handles.second_output;
 
close(handles.figure1)
 
 
% --- Executes on button press in CancelDataset.
function CancelDataset_Callback(hObject, eventdata, handles)
% hObject    handle to CancelDataset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
handles.output=handles.DataProperties;
handles.second_output=handles.NrAvailableDatasets;
 
guidata(hObject, handles);
 
uiresume;
 
 
% --- Executes on button press in AddDataset.
function AddDataset_Callback(hObject, eventdata, handles)
% hObject    handle to AddDataset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
name=get(handles.EditDatasetName,'String');
 
if NewDatasetName(handles.DataProperties,handles.NrAvailableDatasets,name)
 
    a=get(handles.TextPolyFile,'String');
 
    if ~isempty(a)
 
        handles.NrAvailableDatasets=handles.NrAvailableDatasets+1;
 
        nr=handles.NrAvailableDatasets;
 
        handles.DataProperties(nr).Name=get(handles.EditDatasetName,'String');
 
        handles.DataProperties(nr).FileType='D3DDepth';
 
        handles.DataProperties(nr).Parameter='Depth';
        handles.DataProperties(nr).Type='2DScalar';
        handles.DataProperties(nr).TC='c';
 
        handles.DataProperties(nr).DateTime=0;
 
        handles.DataProperties(nr).Block=1;
 
        handles.DataProperties(nr).Column=1;
 
        handles.DataProperties(nr).PathName=handles.PathName;
        handles.DataProperties(nr).FileName=handles.FileName;
        handles.DataProperties(nr).CombinedDataset=0;
 
        handles.DataProperties(nr).GridFile=handles.GridFile;

        wb = waitbox('Reading data file ...');pause(0.1);
        grd=wlgrid('read',[handles.GridFile]);
        z=wldep('read',[handles.PathName handles.FileName],grd);
        close(wb);

        handles.DataProperties(nr).x=grd.X;
        handles.DataProperties(nr).y=grd.Y;
        handles.DataProperties(nr).z=z(1:end-1,1:end-1);
        handles.DataProperties(nr).zz=z;
        
        handles.DataProperties(nr).z(handles.DataProperties(nr).z==-999.0)=NaN;
        handles.DataProperties(nr).z(handles.DataProperties(nr).z==999.999)=NaN;
        handles.DataProperties(nr).zz(handles.DataProperties(nr).zz==-999.0)=NaN;
        handles.DataProperties(nr).zz(handles.DataProperties(nr).zz==999.999)=NaN;
        
        handles.DataProperties(nr).GrdFile=handles.GridFile;
        
        handles.output=handles.DataProperties;
        handles.second_output=handles.NrAvailableDatasets;
 
        uiresume;
 
    else
        txt='First select polyline file!';
        mp_giveWarning('WarningText',txt);
    end
 
else
    txt='Dataset name already exists!';
    mp_giveWarning('WarningText',txt);
end
 
guidata(hObject, handles);
 
function EditDatasetName_Callback(hObject, eventdata, handles)
% hObject    handle to EditDatasetName (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 EditDatasetName as text
%        str2double(get(hObject,'String')) returns contents of EditDatasetName as a double
 
handles.Name=get(hObject,'String');
 
guidata(hObject, handles);
 
 
% --- Executes during object creation, after setting all properties.
function EditDatasetName_CreateFcn(hObject, eventdata, handles)
% hObject    handle to EditDatasetName (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
 
 
% function handles=RefreshDatasetName(handles);
%
% i=get(handles.SelectParameter,'Value');
% str=get(handles.SelectParameter,'String');
%
% j=get(handles.SelectBlock,'Value');
%
%
% set(handles.EditDatasetName,'String',[str{i} ' - Block ' num2str(j)]);
%
 
 
 
 
 
 
% --- Executes on button press in PushSelectGridFile.
function PushSelectGridFile_Callback(hObject, eventdata, handles)
% hObject    handle to PushSelectGridFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
filterspec=       {'*.grd','Delft3D grid file (*.grd)'};
[filename, pathname, filterindex] = uigetfile(filterspec);
 
handles.GridFile=[pathname filename];
 
set(handles.TextPolyFile,'String',handles.GridFile);
 
guidata(hObject, handles);
 
 
function TextPolyFile_Callback(hObject, eventdata, handles)
% hObject    handle to TextPolyFile (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 TextPolyFile as text
%        str2double(get(hObject,'String')) returns contents of TextPolyFile as a double
 
 
% --- Executes during object creation, after setting all properties.
function TextPolyFile_CreateFcn(hObject, eventdata, handles)
% hObject    handle to TextPolyFile (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
 
 
