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

% Last Modified by GUIDE v2.5 22-Jun-2005 20:03:10

global day_thresh night_thresh current_tank FTPusername FTPpassword

FTPusername = 'efish';
FTPpassword = 'XXX';  % enter correct password in place of XXX






% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @setlevels_OpeningFcn, ...
                   'gui_OutputFcn',  @setlevels_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 setlevels is made visible.
function setlevels_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 setlevels (see VARARGIN)
global day_thresh night_thresh
day_thresh = zeros(12);
night_thresh = zeros(12);

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = setlevels_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 during object creation, after setting all properties.
function tankpopupmenu_CreateFcn(hObject, eventdata, handles)
global current_tank day_thresh night_thresh full_filename
	varstrings = {'Tank 1' 'Tank 2' 'Tank 3' 'Tank 4' 'Tank 5' 'Tank 6' 'Tank 7' 'Tank 8'...
                   'Tank 9' 'Tank 10' 'Tank 11' 'Tank 12'};
	set(hObject, 'String', varstrings);
	current_tank = 1;
	if ispc
        set(hObject,'BackgroundColor','white');
	else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
	end


% --- Executes on selection change in tankpopupmenu.
function tankpopupmenu_Callback(hObject, eventdata, handles)
global current_tank day_thresh night_thresh
    current_tank = get(hObject, 'Value');
	displayvalues(handles);
% Hints: contents = get(hObject,'String') returns tankpopupmenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from tankpopupmenu


% --- Executes on button press in pushbutton_dayup.
function pushbutton_dayup_Callback(hObject, eventdata, handles)
global current_tank day_thresh night_thresh
    if (day_thresh(current_tank) < 2.0)
        day_thresh(current_tank) = day_thresh(current_tank) + .005;
		displayvalues(handles);
    end

% --- Executes on button press in pushbutton_daydown.
function pushbutton_daydown_Callback(hObject, eventdata, handles)
global current_tank day_thresh night_thresh
    if (day_thresh(current_tank) > .010)
		day_thresh(current_tank) = day_thresh(current_tank) - .005;
		displayvalues(handles);
    end
    
% --- Executes on button press in pushbutton_nightup.
function pushbutton_nightup_Callback(hObject, eventdata, handles)
	global current_tank day_thresh night_thresh
    if (night_thresh(current_tank) < 2.0)
		night_thresh(current_tank) = night_thresh(current_tank) + .005;
		displayvalues(handles);
    end

% --- Executes on button press in pushbutton_nightdown.
function pushbutton_nightdown_Callback(hObject, eventdata, handles)
	global current_tank day_thresh night_thresh
    if (night_thresh(current_tank) > .010)
		night_thresh(current_tank) = night_thresh(current_tank) - .005;
		displayvalues(handles);
    end

% --- Executes on button press in pushbutton_loadvalues.
function pushbutton_loadvalues_Callback(hObject, eventdata, handles)
global current_tank day_thresh night_thresh tankdata full_filename FTPusername FTPpassword
        try
            f = ftp('solix.fiu.edu',FTPusername,FTPpassword);      % create FTP connection
            cd(f, 'www');
            cd(f, 'EODmachine');
            mget(f, 'thresholds.mat');   % load thresholds into local directory
            mget(f, 'quickdata.mat');   % load amplitude data for last hour contains tanks() structure
            load('thresholds.mat');      % load local copy of thresholds.mat
            load('quickdata.mat');
            delete('thresholds.mat');
            close(f);                                       % close the FTP connection
        catch
            errordlg('Could not load thresholds from solix.fiu.edu');
        end;
        set(handles.DownloadTime, 'String', datestr(now));
       displayvalues(handles);
    


   
% ------------- displays current values in GUI ----------------------
function displayvalues(handles)
global current_tank day_thresh night_thresh tankdata
	daystr = num2str(day_thresh(current_tank), '%4.3f');
	nightstr = num2str(night_thresh(current_tank), '%4.3f');
	set(handles.edit_day, 'String', daystr);
	set(handles.edit_night, 'String', nightstr);
    
    hour = datenum(0,0,0,2,1,0);
    minute = datenum(0,0,0,0,1,0);
    
    set(handles.TankLabel, 'String', tankdata(current_tank).name);
    axes(handles.axes1);
    cla;
    if (length(tankdata(current_tank).times > 0))   % if we have data to work with
        plot(tankdata(current_tank).times,tankdata(current_tank).amps, '.r'); 
        axes(handles.axes1); xlim([tankdata(current_tank).last_time-hour tankdata(current_tank).last_time + minute]);
        axes(handles.axes1); ylim([0 max(tankdata(current_tank).amps)*1.25]);
        datetick('x', 'keeplimits');
        ylabel('Amplitude');
        xlabel('Time');
    else
        axes(handles.axes1); xlim([0 2]);
        axes(handles.axes1); ylim([0 2]);
        text(0.7, 1, 'No Data To Display');
        ylabel('Amplitude');
        xlabel('Time');
       
    end;

    


% --- Executes during object creation, after setting all properties.
function edit_day_CreateFcn(hObject, eventdata, handles)
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_day_Callback(hObject, eventdata, handles)
% hObject    handle to edit_day (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 edit_day as text
%        str2double(get(hObject,'String')) returns contents of edit_day as a double
global current_tank day_thresh night_thresh
	day_value = str2num(get(hObject, 'String'));
	if (day_value > .04) & (day_value < 2.0) 
           day_thresh(current_tank) = day_value;
           displayvalues(handles);
      else
           errordlg('Day threshold out of range');
           displayvalues(handles);
    end


% --- Executes during object creation, after setting all properties.
function edit_night_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_night (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 edit_night_Callback(hObject, eventdata, handles)
% hObject    handle to edit_night (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 edit_night as text
%        str2double(get(hObject,'String')) returns contents of edit_night as a double
global current_tank day_thresh night_thresh
	night_value = str2num(get(hObject, 'String'));
	if (night_value > .04) & (night_value <= 2.5) 
           night_thresh(current_tank) = night_value;
           displayvalues(handles);
      else
           errordlg('Night trheshold out of range');
           displayvalues(handles);
    end


% --- Executes on button press in pushbutton_upload.
function pushbutton_upload_Callback(hObject, eventdata, handles)
global day_thresh night_thresh current_tank FTPusername FTPpassword
remote_change_flag = 1;
       save('thresholds.mat', 'day_thresh', 'night_thresh', 'remote_change_flag')
       try
            f = ftp('solix.fiu.edu',FTPusername,FTPpassword);      % create FTP connection
            cd(f, 'www');
            cd(f, 'EODmachine');
            mput(f, 'thresholds.mat');   % upload new threshold file to web server
            close(f);                                       % close the FTP connection
            msgbox('Thresholds uploaded successfully');
       catch
            errordlg('Error uploading thresholds');
       end