function varargout = Agora_GUI_main(varargin)

%     Copyright 2015
%     Ozkan Sertlek,Phd Student((2011-2015),University of Leiden,The Netherlands
%
%
%     This script calls calculation and plotting functions of airgun signature
%
%     LICENCE
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.see <http://www.gnu.org/licenses/>.

%     AGORA is a research algorithm which calculates the airgun array 
%     signatures to use in the sound mapping tools for the environmental 
%     risk assessment. The motivation of this algorithm is based on a PhD 
%     project (at Institute of Biology, University of Leiden) which aims to
%     model the soundscape of the Dutch North Sea. This project is funded 
%     by ZKO programme of NWO. The mathematical basement of the algorithm 
%     is described by "H.Ö.Sertlek and M.A. Ainslie, AGORA: Airgun source 
%     signature model: its application for the Dutch seismic surveys, 
%     Conference Proceedings of UAC 2015, Crete, Greece". 

%     The developers of AGORA accepts no responsibility or liability 
%     whatsoever with regard to the results of AGORA if they are used in 
%     any commercial or scientific projects.It is a research algorithm and 
%     still needs more advanced approach to increase its accuracy for 
%     entire frequency band. 
%
%     If you have any feedback or questions, please contact agora.model@gmail.com.  


gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Agora_GUI_main_OpeningFcn, ...
                   'gui_OutputFcn',  @Agora_GUI_main_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

% --- Executes just before Agora_GUI_main is made visible.
function Agora_GUI_main_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
axes(handles.axes_logo);
rgb = imread('agora_logo_notional.png');
whitebg('black')
image(rgb); 
axis off;          % Remove axis ticks and numbers
axis image ;
set(gcf,'Toolbar','none','Menubar','none');

%   hFig = gcf;
%   hAxes = gca;
%   hAxes = findall(hFig,'type','axes');
%   hText = findall(hAxes,'type','text');
%   hUIControls = findall(gcf,'type','uicontrol');
%   set([hAxes; hText; hUIControls],'units','normalized','fontunits','pixels');
%  ScrSize = get(0,'ScreenSize');
%  set(gcf,'Units','pixels','Position',ScrSize);

def_env=load('environment.ain');
handles.def_env=def_env;
handles.cw=def_env(4);
handles.rhow=def_env(3);
% Update handles structure
 
handles.SLplot.el_angle=0;
handles.SLplot.az_angle=0;
handles.AirgunArraySignature=[];
handles.SLplot.f_max=1000;
handles.directivity='H';
handles.ghost_check=0;
handles.item=[];
d = dir('output\');
isub = [d(:).isdir]; % returns logical vector
nameFolds = {d(isub).name};
nameFolds(ismember(nameFolds,{'.','..'})) = [];
set(handles.load_listbox,'String',nameFolds);
handles.AirgunArraySignature.gun_id=1;
handles.AirgunArraySignature.p_notional=[];
set(handles.main_layout,'Pointer','arrow');
guidata(hObject, handles);
%  ScrSize = get(0,'ScreenSize');
%  set(gcf,'Units','pixels','Position',ScrSize);

function varargout = Agora_GUI_main_OutputFcn(hObject, eventdata, handles) 
'update'
    varargout{1} = handles.output;

% --- Executes on button press in configfile.
function configfile_Callback(hObject, eventdata, handles)
    % opens the config file
    config_panel
    disp('Config file is loaded...')

% --- Executes on button press in sign_calc.
function sign_calc_Callback(hObject, eventdata, handles)
    % starts the simulation
  %  set(handles.main_layout,'Pointer','watch');
    [handles.AirgunArraySignature]= agora_kernel(handles.ArrayGeo); %only this function will be converted into C++
  % set(handles.main_layout,'Pointer','arrow');
    guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function axes_ArrayGeo_CreateFcn(hObject, eventdata, handles)

function dip_angle_edit_Callback(hObject, eventdata, handles)
    %gets el angle 
    el_angle= str2double(get(hObject, 'String'));
    if isnan(el_angle)
        set(hObject, 'String', 0);
        errordlg('Input must be a number','Error');
    end
    handles.SLplot.el_angle = el_angle*pi/180;
    guidata(hObject,handles)

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

function az_angle_edit_Callback(hObject, eventdata, handles)
    %gets az angle
    az_angle= str2double(get(hObject, 'String'));
    if isnan(az_angle)
        set(hObject, 'String', 0);
        errordlg('Input must be a number','Error');
    end
handles.SLplot.az_angle = az_angle*pi/180;
guidata(hObject,handles)

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

% --- Executes on button press in plot_esl_button.
function plot_esl_button_Callback(hObject, eventdata, handles)
    if isempty(handles.AirgunArraySignature.p_notional)
        h = errordlg('No airgun array signature is calculated or loaded')
    else
        P= calc_directivity_AGORA_gui(handles.AirgunArraySignature.F0i,handles.SLplot.az_angle,handles.SLplot.el_angle,handles.ArrayGeo,handles.AirgunArraySignature.P_out);
        Ee=2*P.*conj(P) ./(handles.rhow*handles.cw);
        ESL_f=10*log10((10^12).*Ee.*(handles.rhow*handles.cw));
        f_max=handles.SLplot.f_max;
        if f_max > handles.AirgunArraySignature.F0i(end);
            f_max=handles.AirgunArraySignature.F0i(end);
            errordlg(['The maximum frequency should be less than ' num2str(f_max) ' Hz'],'Error');
        end
        id_max=find(handles.AirgunArraySignature.F0i<=f_max);
        axes(handles.axes_ESL);
        cla;          %set(gca,'XTick',[],'YTick',[],'XLabel',[],'YLabel',[],'Title',[]); % clear axes labels 
        hold on;
        plot(handles.AirgunArraySignature.F0i(id_max),ESL_f(id_max),'y','LineWidth',2);
        xlabel('Frequency (Hz)', 'FontSize', 12);
        ylabel({'Energy Dipole Source'; 'Spectral Density Level'; '[dB re 1 \mu Pa^2 m^2 s / Hz ]'}, 'FontSize', 12)
        ESL_f(isnan(ESL_f))=0; % for plotting
        ESL_f(isinf(ESL_f))=0; % for plotting
        y_max=max((10*ceil((ESL_f./10))));
        grid on
        axis([0 ceil(handles.AirgunArraySignature.F0i(id_max(end))) y_max(1)-150 y_max(end)]);
        set(gca,'XTick',10*round(0.1*linspace(0,ceil(handles.AirgunArraySignature.F0i(id_max(end))),11)),'YTick',[(y_max(1)-150):50:y_max(end)]); % clear axes labels 
        title(['Azimuth=' num2str(handles.SLplot.az_angle*180/pi) '^o , Dip=' num2str(handles.SLplot.el_angle*180/pi) '^o'],'FontSize', 12 )
    end
    
% --- Executes on selection change in directivity_type.
function directivity_type_Callback(hObject, eventdata, handles)
    popup_sel_index = get(handles.directivity_type, 'Value');
    switch popup_sel_index
        case 1
           handles.directivity='V';
        case 2
           handles.directivity='H';
    end
guidata(hObject,handles)

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

function directivityplot_button_Callback(hObject, eventdata, handles)
    if isempty(handles.AirgunArraySignature.p_notional)
        h = errordlg('No airgun array signature is calculated or loaded')
    else
      %  set(gcf,'Pointer','watch');
        if handles.directivity=='V'
            hwa=waitbar(0,'plotting...')
            phi=  linspace(-pi/2,pi/2,181); %elevation
            F0=handles.AirgunArraySignature.F0i;
            Pff_ver(1:length(F0),1:length(phi))=0;                
            for pp=1:length(phi)
                RT=(pp-1)/(length(phi));
                waitbar( RT,hwa,[ num2str(round(100*RT)) ' % is being calculated...'])
                Pff_ver(:,pp)= calc_directivity_AGORA_gui(handles.AirgunArraySignature.F0i,handles.SLplot.az_angle,phi(pp),handles.ArrayGeo,handles.AirgunArraySignature.P_out);
            end
            Eef=Pff_ver.*conj(Pff_ver) ./(handles.rhow*handles.cw);
            SLf=10*log10(abs((10^12).*Eef.*(handles.rhow*handles.cw)));

            f_ind=handles.SLplot.f_max;  % max frequency index
            F0(f_ind)
            % plotting ESL_f with its directivity
            figure('units','normalized','outerposition',[0 0 1 1])
            polarplot3d(SLf(1:f_ind,:),'plottype','surfcn','radialrange',F0(1:f_ind),'InterpMethod','nearest','AxisLocation','off','PolarDirection','cw','AngularRange',[0 pi]);
            caxis([100 max(SLf(:))]);colorbar('SouthOutside');grid on; colormap hot;
            axis([0 F0(f_ind) -F0(f_ind) F0(f_ind)])
            view(90,90); set(gca, 'YAxisLocation', 'right'); set(gca,'xticklabel',[]);
            text(0.8*f_ind,0.65*f_ind, ['Vertical Directivity'],'FontSize',12);
            text(0.85*f_ind,0.65*f_ind, ['azimuth angle=' num2str(handles.SLplot.az_angle*180/pi) ' deg'],'FontSize',12);
            text(0.9*f_ind,0.65*f_ind, ['Radial Axis is frequency'],'FontSize',12);
            axis equal tight
            close(hwa)
            saveas(gcf,'directivity_ver.jpg') 
        elseif handles.directivity=='H'
            hwa=waitbar(0,'plotting...')
            theta=  linspace(0,2*pi,361); %elevation
            F0=handles.AirgunArraySignature.F0i;
            Pff_ver(1:length(F0),1:length(theta))=0;
            for tt=1:length(theta)
                RT=(tt-1)/(length(theta));
                waitbar( RT,hwa,[ num2str(round(100*RT)) ' % plotting...'])
                Pff_hor(:,tt)= calc_directivity_AGORA_gui(handles.AirgunArraySignature.F0i,theta(tt),handles.SLplot.el_angle,handles.ArrayGeo,handles.AirgunArraySignature.P_out);
            end
            Eef=Pff_hor.*conj(Pff_hor) ./(handles.rhow*handles.cw);
            SLf=10*log10(abs((10^12).*Eef.*(handles.rhow*handles.cw)));
            f_ind=handles.SLplot.f_max;  % max frequency index
            SLf(isinf(SLf(:)))=0;
            figure('units','normalized','outerposition',[0 0 1 1])
            polarplot3d(SLf(1:f_ind,:),'plottype','surfcn','radialrange',F0(1:f_ind),'InterpMethod','nearest','AxisLocation','off','PolarDirection','cw','AngularRange',[0 2*pi]);
            caxis([max(SLf(:))-100 max(SLf(:))]);colorbar('SouthOutside');
            colormap hot; grid on; 
            axis([0 F0(f_ind) -F0(f_ind) F0(f_ind)]);     
            set(gca, 'YAxisLocation', 'right');    set(gca,'xticklabel',[]);
            title (['Horizontal Directivity, dip angle=' num2str(handles.SLplot.el_angle*180/pi) ' deg, Radial Axis is frequency'],'FontSize',12); 
            axis equal tight
            close(hwa)
            saveas(gcf,'directivity_hor.jpg') 
        end
    end
  % set(handles.main_layout,'Pointer','arrow');

function open_sign_Callback(hObject, eventdata, handles)

% --- Executes on button press in reset_button.
function reset_button_Callback(hObject, eventdata, handles)
choice = questdlg('Are you sure to delete all simulation data?', ...
	'reset?', ...
	'Yes,delete all','only clear the figures','No','No');
% Handle response
switch choice
    case 'Yes,delete all'
    %  axes(handles.axes_ArrayGeo); cla
     % axes(handles.axes_ESL); cla
      close all
      clear all
      disp('All date is deleted...')
      Agora_GUI_main
      disp('AGORA restarted...')
    case 'only clear the figures'
      axes(handles.axes_ArrayGeo); cla;
        set(gca,'XLabel',[],'YLabel',[],'Title',[]); % clear axes labels 
        colorbar off
      axes(handles.axes_ESL); cla
        set(gca,'XLabel',[],'YLabel',[],'Title',[]); % clear axes labels 
        colorbar off
      axes(handles.axes_timeplot); cla
         set(gca,'XTick',[],'YTick',[]); % clear axes labels 
         set(gca,'XTick',[],'YTick',[],'XLabel',[],'YLabel',[],'Title',[]); % clear axes labels 
         colorbar off 
         rgb = imread('agora_logo_notional.png');whitebg('black');image(rgb);
    case 'No'
        disp('Nothing is deleted...')
end

% --------------------------------------------------------------------
function Open_menu_Callback(hObject, eventdata, handles)

function open_conf_menu_Callback(hObject, eventdata, handles)
    config_panel
    disp('Config file is loaded...')

function array_geo_menu_Callback(hObject, eventdata, handles)
    ArrayGeo=array_open_plot(handles,1);
    handles.ArrayGeo=ArrayGeo;
    guidata(hObject, handles);

function help_menu_Callback(hObject, eventdata, handles)
    h = msgbox([{'The motivation of this algorithm is based on a PhD project of H.O.Sertlek which aims to model the soundscape of the Dutch North Sea. This project is funded by ZKO programme of NWO in the Netherlands. The mathematical background of the algorithm is described by "H.Ö.Sertlek and M.A. Ainslie, AGORA: Airgun source signature model: its application for the Dutch seismic surveys, Conference Proceedings of UAC 2015, Crete, Greece". The description of interface is explained in the user guide.'}; {' '}; {  'AGORA is a research algorithm. The developers would be appreciated if you could please send any feedback or suggestion to agora.model@gmail.com.However,the developers of AGORA accepts no responsibility or liability whatsoever with regard to the wrong results of AGORA if they are used in any commercial or scientific projects'}]) 

% --------------------------------------------------------------------
function calc_sin_menu_Callback(hObject, eventdata, handles)
% simulation starts
    [handles.AirgunArraySignature]=agora_kernel(handles.ArrayGeo); %only this function will be converted into C++
    guidata(hObject, handles);

% --- Executes when main_layout is resized.
function main_layout_SizeChangedFcn(hObject, eventdata, handles)

function open_sgin_menu_Callback(hObject, eventdata, handles)
    if isempty(handles.item)
         errordlg('please select a signature file from the list')
    else
         filename=handles.item
         handles.time_not=csvread(['output/' filename '/time_notational.aas']);
         handles.time_ghost=csvread(['output/' filename '/time_ghost.aas']);
         handles.freq_not=csvread(['output/' filename '/freq_not.aas']);
         handles.array_geo=csvread(['output/' filename '/array_geo.aas']);
         N=length(handles.array_geo(1,:));
         handles.AirgunArraySignature.Time_Ax=handles.time_not(1,:);
         handles.AirgunArraySignature.F0i=handles.freq_not(1,:);
         handles.AirgunArraySignature.p_notional=handles.time_not(2:end,:);
         handles.AirgunArraySignature.p_bi_ghost=handles.time_ghost(2:end,:);
         handles.AirgunArraySignature.P_out=handles.freq_not(2:end,:);
         handles.ArrayGeo.x=handles.array_geo(1,:);
         handles.ArrayGeo.y=handles.array_geo(2,:);
         handles.ArrayGeo.zr=handles.array_geo(3,:);
         handles.ArrayGeo.zg=handles.array_geo(4,:);
         handles.ArrayGeo.Vg=handles.array_geo(5,:);
         handles.ArrayGeo.Pg=handles.array_geo(6,:);
         handles.ArrayGeo.N=N;
         axes(handles.axes_ArrayGeo); 
         cla(handles.axes_ArrayGeo)
         array_open_plot(handles,0);
    end
    guidata(hObject, handles);

function reset_menu_Callback(hObject, eventdata, handles)
    choice = questdlg('Are you sure to delete all simulation data?', ...
	'Reset ?', ...
	'Yes,delete all','only clear the figures','No','No');
    switch choice
        case 'Yes,delete all'
          close all
          clear all
          disp('All data is deleted...')
          Agora_GUI_main
          disp('AGORA restarted...')

        case 'only clear the figures'
          axes(handles.axes_ArrayGeo); cla;
             set(gca,'XTick',[],'YTick',[],'XLabel',[],'YLabel',[],'Title',[]); % clear axes labels 
            colorbar off
          axes(handles.axes_ESL); cla
             set(gca,'XTick',[],'YTick',[]); % clear axes labels 
             set(gca,'XTick',[],'YTick',[],'XLabel',[],'YLabel',[],'Title',[]); % clear axes labels 
            colorbar off
           axes(handles.axes_timeplot); cla
             set(gca,'XTick',[],'YTick',[]); % clear axes labels 
             set(gca,'XTick',[],'YTick',[],'XLabel',[],'YLabel',[],'Title',[]); % clear axes labels 
            colorbar off  
            rgb = imread('agora_logo_notional.png');whitebg('black');image(rgb);
        case 'No'
            disp('Nothing is deleted...')
    end

function gun_id_edit_Callback(hObject, eventdata, handles)
    gun_id= str2double(get(hObject, 'String'));
    if isnan(gun_id)
        set(hObject, 'String', 0);
        errordlg('Input must be a number','Error');
    end
    handles.AirgunArraySignature.gun_id = gun_id;
    guidata(hObject, handles);

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

% --- Executes on button press in plot_time_signature.
function plot_time_signature_Callback(hObject, eventdata, handles)
    if handles.AirgunArraySignature.gun_id> handles.ArrayGeo.N
    errordlg('This gun id is not available.Please enter a number less than the total numbers of airguns','Error');
    end
    if handles.ghost_check==1
        p_time=handles.AirgunArraySignature.p_bi_ghost(handles.AirgunArraySignature.gun_id,:);
    else
        p_time=handles.AirgunArraySignature.p_notional(handles.AirgunArraySignature.gun_id,:);
    end
    axes(handles.axes_timeplot);
    plot(handles.AirgunArraySignature.Time_Ax,p_time);
    xlabel('Time (ms)', 'FontSize', 12)
    ylabel('Signature (Pa.m)', 'FontSize', 12)
    axis([-50 max(handles.AirgunArraySignature.Time_Ax) -max(1.2*abs(p_time)) max(1.2*abs(p_time))])
    title ([num2str(handles.AirgunArraySignature.gun_id) '. gun signature'], 'FontSize', 14)
    grid on;
    set(gca,'XTick',ceil(linspace(-50,max(handles.AirgunArraySignature.Time_Ax),10)),'YTick',1e5.*ceil(linspace(-max(1.2*abs(p_time./1e5)), max(1.2*abs(p_time./1e5)),5))); % clear axes labels 
    set(gca,'fontsize',10)
     
% --- Executes on button press in ghost_on_off.
function ghost_on_off_Callback(hObject, eventdata, handles)
    if (get(hObject,'Value') == get(hObject,'Max'))
        display('Selected');
        handles.ghost_check=1
    else
        display('Not selected');
        handles.ghost_check=0;
    end
    guidata(hObject, handles);

function f_max_edit_Callback(hObject, eventdata, handles)
    f_max= str2double(get(hObject, 'String'));
    if isnan(f_max)
        set(hObject, 'String', 0);
        errordlg('Input must be a number','Error');
    end
    handles.SLplot.f_max = f_max;
    guidata(hObject, handles);

function f_max_edit_CreateFcn(hObject, eventdata, handles)
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end

function calc_array_sign_button_Callback(hObject, eventdata, handles)
   % set(handles.main_layout,'Pointer','watch');
    [handles.AirgunArraySignature]= agora_kernel(handles.ArrayGeo); %only this function will be converted into C++
   % set(handles.main_layout,'Pointer','arrow');
    guidata(hObject, handles);

function pushbutton_parameters_Callback(hObject, eventdata, handles)
    config_panel
    disp('Config file is loaded...')

% --- Executes on button press in pushbutton_ArrayGeo.
function pushbutton_ArrayGeo_Callback(hObject, eventdata, handles)
    ArrayGeo=array_open_plot(handles,1);
    handles.ArrayGeo=ArrayGeo;
    guidata(hObject, handles);

    % --- Executes on button press in save_time_sign_button.
function save_time_sign_button_Callback(hObject, eventdata, handles)
    %[filename,PathName,FilterIndex] = uiputfile({  '*.aas','AGORA Airgun Signature File (*.aas)'})
      if isempty(handles.AirgunArraySignature.p_notional)
          errordlg('Please load array geometry(*.ago) file and run the simulation')
      else
    filename= char (inputdlg('Please Enter the name of signature data folder' ));
    if isempty(filename)
          errordlg('Please write the filename to save the simulation results')
    else
       % set(handles.main_layout,'Pointer','watch');
        filename=['agora_' filename];
        N=handles.ArrayGeo.N;
        p_unperturbed=handles.AirgunArraySignature.p_unperturbed;
        Time_Ax=handles.AirgunArraySignature.Time_Ax;
        p_notional=handles.AirgunArraySignature.p_notional;
        p_bi_ghost=handles.AirgunArraySignature.p_bi_ghost;
        F0i=handles.AirgunArraySignature.F0i;
        filename_array_geo=handles.ArrayGeo.filename;
        PathName_array_geo=handles.ArrayGeo.PathName; 
        aa=0;
        time_not=[Time_Ax;p_notional;];
        time_ghost=[Time_Ax;p_bi_ghost;];
        freq_not=[F0i;handles.AirgunArraySignature.P_out;];
        array_geo=[handles.ArrayGeo.x;handles.ArrayGeo.y;handles.ArrayGeo.zr;handles.ArrayGeo.zg;handles.ArrayGeo.Vg;handles.ArrayGeo.Pg];

        mkdir(['output\', filename])
        csvwrite(['output/' filename '/time_notational_unperturbed.aas'],p_unperturbed);
        csvwrite(['output/' filename '/time_notational.aas'],time_not);
        csvwrite(['output/' filename '/time_ghost.aas'],time_ghost);
        csvwrite(['output/' filename '/freq_not.aas'],freq_not);
        csvwrite(['output/' filename '/array_geo.aas'],array_geo);
        msgbox(['airgun signature files are saved in' 'output/' filename ' folder' ])
        d = dir('output\');
        isub = [d(:).isdir]; %# returns logical vector
        nameFolds = {d(isub).name};
        nameFolds(ismember(nameFolds,{'.','..'})) = [];
        set(handles.load_listbox,'String',nameFolds);

        ArrayGeo = getframe(handles.axes_ArrayGeo);
        ImageArrayGeo = frame2im(ArrayGeo);
        imwrite(ImageArrayGeo, ['output/' filename '/array_geo.jpg'])
        set(handles.main_layout,'Pointer','arrow');
    end
      end
      
    % --- Executes on selection change in load_listbox.
function load_listbox_Callback(hObject, eventdata, handles)

    index_selected = get(hObject,'Value');
    list = get(hObject,'String');
    item_selected = list{index_selected}; 
    handles.item =  item_selected;
    guidata(hObject, handles);

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

% ---load the signatures
function pushbutton_loadsignature_Callback(hObject, eventdata, handles)
    if isempty(handles.item)
        errordlg('please select a signature file from the list')
    else
         filename=handles.item
         
         handles.p_unperturbed=csvread(['output/' filename '/time_notational_unperturbed.aas']);
         handles.time_not=csvread(['output/' filename '/time_notational.aas']);
         handles.time_ghost=csvread(['output/' filename '/time_ghost.aas']);
         handles.freq_not=csvread(['output/' filename '/freq_not.aas']);
         handles.array_geo=csvread(['output/' filename '/array_geo.aas']);
         N=length(handles.array_geo(1,:));
         handles.AirgunArraySignature.Time_Ax=handles.time_not(1,:);
         handles.AirgunArraySignature.F0i=handles.freq_not(1,:);
         
         handles.AirgunArraySignature.p_unperturbed=handles.p_unperturbed(2:end,:);
         handles.AirgunArraySignature.p_notional=handles.time_not(2:end,:);
         handles.AirgunArraySignature.p_bi_ghost=handles.time_ghost(2:end,:);
         handles.AirgunArraySignature.P_out=handles.freq_not(2:end,:);
         handles.ArrayGeo.x=handles.array_geo(1,:);
         handles.ArrayGeo.y=handles.array_geo(2,:);
         handles.ArrayGeo.zr=handles.array_geo(3,:);
         handles.ArrayGeo.zg=handles.array_geo(4,:);
         handles.ArrayGeo.Vg=handles.array_geo(5,:);
         handles.ArrayGeo.Pg=handles.array_geo(6,:);
         handles.ArrayGeo.N=N;
         axes(handles.axes_ArrayGeo); 
         cla(handles.axes_ArrayGeo)
         array_open_plot(handles,0);
    end
    guidata(hObject, handles);

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