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

% Last Modified by GUIDE v2.5 22-Jul-2010 10:56:22

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @DMSP_Plot_OpeningFcn, ...
                   'gui_OutputFcn',  @DMSP_Plot_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 DMSP_Plot is made visible.
function DMSP_Plot_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 DMSP_Plot (see VARARGIN)

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

global data_plot map_color stateRB stateCoord hbStatusBar ...
       FS FS1 LW NR MS1 pChoice options
   
%Set the default settings for checkboxes and their associated flags
set(handles.qlCB, 'Value', 1);
handles.data_user.quickloadflag = 1;

set(handles.vswCB, 'Value', 1);
handles.data_user.vswflag = 1;




locChoices = {'GLat' 'GLon' 'MLAT110' 'MLT' 'MLON110' 'GLon110' 'GLat110' 'Poynting Flux'};

%short list 
cartChoices ={'Vy','Vz','VxVyVz','dBx','dBy','BxBy',...
                      'IEFlx','EEFlx','EFlx',...
                      'IEFlx_Ch','EEFlx_Ch','Sz','SzM' ...
                      'IMF-Bx','IMF-By','IMF-Bz','IMF-B','IMF-dB','IMF-Bxyz', ...
                      'V_sw','Vxyz_sw','Psw','Kp','KL E-field','Hp','Hp_ion','Pe','Pi','AE','AL','AU','DST','eqb','hour'...
                      };

%full list
%{ 
cartChoices ={'Vx','Vy','Vz','VxVyVz','dBx','dBy','BxBy',...
                      'IEFlx','EEFlx','EFlx',...
                      'IEFlx_Ch','EEFlx_Ch','Sz','SzM','EFlx/Sz', ...
                      'IMF-Bx','IMF-By','IMF-Bz','IMF-B','IMF-dB','IMF-Bxyz', ...
                      'atan(Bz/By)','Vx_sw','Vy_sw','Vz_sw','V_sw','Vxyz_sw',...
                      'Psw'};
%}

polarChoices = cartChoices;


%Set the maximum and minimum number of graphs for each

set(handles.locList, 'Max', 6); set(handles.locList, 'Min',1);
set(handles.cartList, 'Max', 10); set(handles.cartList, 'Min',1);
set(handles.polarList, 'Max', 4); set(handles.polarList, 'Min',1);



locDefault = {'GLat' 'GLon' 'MLAT110' 'MLT'};
cartDefault = {'Vy' 'BxBy' 'Vz' 'EFlx' 'Sz' 'IMF-Bxyz' 'V_sw' 'Psw'};
polarDefault = {'EEFlx' 'IEFlx' 'Sz' 'Vy'};

for i = 1:length(locChoices)
    for j = 1:length(locDefault)
        if strcmp(locDefault{j},locChoices{i})
            values(j) = i;
            locChoices{i} = strcat('*',locChoices{i},'*');
        end
    end
end

values = sort(values);
set(handles.locList, 'UserData', values);

for i = 1:length(cartChoices)
    for j = 1:length(cartDefault)
        if strcmp(cartDefault{j},cartChoices{i})
            values(j) = i;
            cartChoices{i} = strcat('*',cartChoices{i},'*');
        end
    end
end

values = sort(values);
set(handles.cartList, 'UserData', values);

for i = 1:length(polarChoices)
    for j = 1:length(polarDefault)
        if strcmp(polarDefault{j},polarChoices{i})
            values(j) = i;
            polarChoices{i} = strcat('*',polarChoices{i},'*');
        end
    end
end

values = sort(values);
set(handles.polarList, 'UserData', values);
%Set the list contents

set(handles.locList, 'String', locChoices);
set(handles.cartList, 'String', cartChoices);
set(handles.polarList, 'String', polarChoices);



% Update handles structure
guidata(hObject, handles);

% UIWAIT makes DMSP_Plot wait for user response (see UIRESUME)
% uiwait(handles.SSJ);
function refreshMenus(handles,idm,ssj,omni,poy,Sz)
%This function adds or removes menu items dependant on which data files 
%are present, the arguments other than handles are 1 or 0 integers which 
%mark a data set as present or not for the most recent read
locChoices = {};
cartChoices = {};

if idm
locChoices = {'GLat' 'GLon'};   
cartChoices ={'idm_Vy','idm_Vz'};   

end

if ssj
locChoices = [locChoices {'MLAT110' 'MLT' 'MLON110' 'GLon110' 'GLat110'}];

cartChoices = [cartChoices, {'IEFlx','EEFlx','EFlx','IEFlx_Ch','EEFlx_Ch'}];
end

if Sz

cartChoices = [cartChoices, {'Sz','SzM'}];

end

if omni
   

cartChoices = [cartChoices, {'IMF-Bx','IMF-By','IMF-Bz','IMF-B','IMF-dB','IMF-Bxyz', ...
                      'V_sw','Vxyz_sw','Psw','Kp','KL E-field','Hp','Hp_ion',...
                      'Pe','Pi','AE','AL','AU','DST','eqb','hour'}];
end

if poy
    locChoices = [locChoices, {'GLat' 'GLon'}];
    locChoices = [locChoices, {'Poynting Flux'}];
    
end


%full list
%{ 
cartChoices ={'Vx','Vy','Vz','VxVyVz','dBx','dBy','BxBy',...
                      'IEFlx','EEFlx','EFlx',...
                      'IEFlx_Ch','EEFlx_Ch','Sz','SzM','EFlx/Sz', ...
                      'IMF-Bx','IMF-By','IMF-Bz','IMF-B','IMF-dB','IMF-Bxyz', ...
                      'atan(Bz/By)','Vx_sw','Vy_sw','Vz_sw','V_sw','Vxyz_sw',...
                      'Psw'};
%}

polarChoices = cartChoices;


%Set the maximum and minimum number of graphs for each

set(handles.locList, 'Max', 6); set(handles.locList, 'Min',1);
set(handles.cartList, 'Max', 10); set(handles.cartList, 'Min',1);
set(handles.polarList, 'Max', 4); set(handles.polarList, 'Min',1);

%set defaults
locDefault = {'GLat' 'GLon' 'MLAT110' 'MLT'};

for i = 1:length(locChoices)
    for j = 1:length(locDefault)
        if strcmp(locDefault{j},locChoices{i})
            values(j) = i;
            locChoices{i} = strcat('*',locChoices{i},'*');
        end
    end
end

values = sort(values);
set(handles.locList, 'UserData', values);
set(handles.cartList, 'UserData', []);
set(handles.polarList, 'UserData', []);

%{
for i = 1:length(cartChoices)
    for j = 1:length(cartDefault)
        if strcmp(cartDefault{j},cartChoices{i})
            values(j) = i;
            cartChoices{i} = strcat('*',cartChoices{i},'*');
        end
    end
end

values = sort(values);
set(handles.cartList, 'UserData', values);

for i = 1:length(polarChoices)
    for j = 1:length(polarDefault)
        if strcmp(polarDefault{j},polarChoices{i})
            values(j) = i;
            polarChoices{i} = strcat('*',polarChoices{i},'*');
        end
    end
end

values = sort(values);
set(handles.polarList, 'UserData', values);
%}

%Set the list contents

set(handles.locList, 'String', locChoices);
set(handles.cartList, 'String', cartChoices);
set(handles.polarList, 'String', polarChoices);

% --- Outputs from this function are returned to the command line.
function varargout = DMSP_Plot_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 MM_EditText1_Callback(hObject, eventdata, handles)
% hObject    handle to MM_EditText1 (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 MM_EditText1 as text
%        str2double(get(hObject,'String')) returns contents of MM_EditText1 as a double


% --- Executes during object creation, after setting all properties.
function MM_EditText1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MM_EditText1 (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 DD_EditText1_Callback(hObject, eventdata, handles)
% hObject    handle to DD_EditText1 (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 DD_EditText1 as text
%        str2double(get(hObject,'String')) returns contents of DD_EditText1 as a double


% --- Executes during object creation, after setting all properties.
function DD_EditText1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to DD_EditText1 (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 YY_EditText1_Callback(hObject, eventdata, handles)
% hObject    handle to YY_EditText1 (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 YY_EditText1 as text
%        str2double(get(hObject,'String')) returns contents of YY_EditText1 as a double


% --- Executes during object creation, after setting all properties.
function YY_EditText1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to YY_EditText1 (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 HH_EditText1_Callback(hObject, eventdata, handles)
% hObject    handle to HH_EditText1 (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 HH_EditText1 as text
%        str2double(get(hObject,'String')) returns contents of HH_EditText1 as a double


% --- Executes during object creation, after setting all properties.
function HH_EditText1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to HH_EditText1 (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 Min_EditText1_Callback(hObject, eventdata, handles)
% hObject    handle to Min_EditText1 (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 Min_EditText1 as text
%        str2double(get(hObject,'String')) returns contents of Min_EditText1 as a double


% --- Executes during object creation, after setting all properties.
function Min_EditText1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Min_EditText1 (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 SS_EditText1_Callback(hObject, eventdata, handles)
% hObject    handle to SS_EditText1 (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 SS_EditText1 as text
%        str2double(get(hObject,'String')) returns contents of SS_EditText1 as a double


% --- Executes during object creation, after setting all properties.
function SS_EditText1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to SS_EditText1 (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 MM_EditText2_Callback(hObject, eventdata, handles)
% hObject    handle to MM_EditText2 (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 MM_EditText2 as text
%        str2double(get(hObject,'String')) returns contents of MM_EditText2 as a double


% --- Executes during object creation, after setting all properties.
function MM_EditText2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MM_EditText2 (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 DD_EditText2_Callback(hObject, eventdata, handles)
% hObject    handle to DD_EditText2 (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 DD_EditText2 as text
%        str2double(get(hObject,'String')) returns contents of DD_EditText2 as a double


% --- Executes during object creation, after setting all properties.
function DD_EditText2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to DD_EditText2 (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 YY_EditText2_Callback(hObject, eventdata, handles)
% hObject    handle to YY_EditText2 (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 YY_EditText2 as text
%        str2double(get(hObject,'String')) returns contents of YY_EditText2 as a double


% --- Executes during object creation, after setting all properties.
function YY_EditText2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to YY_EditText2 (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 during object creation, after setting all properties.
function HH_EditText2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to HH_EditText2 (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 Min_EditText2_Callback(hObject, eventdata, handles)
% hObject    handle to Min_EditText2 (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 Min_EditText2 as text
%        str2double(get(hObject,'String')) returns contents of Min_EditText2 as a double


% --- Executes during object creation, after setting all properties.
function Min_EditText2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Min_EditText2 (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 SS_EditText2_Callback(hObject, eventdata, handles)
% hObject    handle to SS_EditText2 (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 SS_EditText2 as text
%        str2double(get(hObject,'String')) returns contents of SS_EditText2 as a double


% --- Executes during object creation, after setting all properties.
function SS_EditText2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to SS_EditText2 (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 Slide_EditText_Callback(hObject, eventdata, handles)
% hObject    handle to Slide_EditText (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 Slide_EditText as text
%        str2double(get(hObject,'String')) returns contents of Slide_EditText as a double


% --- Executes during object creation, after setting all properties.
function Slide_EditText_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Slide_EditText (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 DMSP_EditText_Callback(hObject, eventdata, handles)
% hObject    handle to DMSP_EditText (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 DMSP_EditText as text
%        str2double(get(hObject,'String')) returns contents of DMSP_EditText as a double


% --- Executes during object creation, after setting all properties.
function DMSP_EditText_CreateFcn(hObject, eventdata, handles)
% hObject    handle to DMSP_EditText (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 StepClbk(source, eventdata, datevec, handles)
global data_plot map_color stateRB stateCoord hbStatusBar hbDataBar ...
       FS FS1 LW NR MS1 pChoice

a = get(handles.YY_EditText1,'String');  varStart(1) = str2num(a);
a = get(handles.MM_EditText1,'String');  varStart(2) = str2num(a);
a = get(handles.DD_EditText1,'String');  varStart(3) = str2num(a);
a = get(handles.HH_EditText1,'String');  varStart(4) = str2num(a);
a = get(handles.Min_EditText1,'String'); varStart(5) = str2num(a);
a = get(handles.SS_EditText1,'String');  varStart(6) = str2num(a);

a = get(handles.YY_EditText2,'String');  varEnd(1) = str2num(a);
a = get(handles.MM_EditText2,'String');  varEnd(2) = str2num(a);
a = get(handles.DD_EditText2,'String');  varEnd(3) = str2num(a);
a = get(handles.HH_EditText2,'String');  varEnd(4) = str2num(a);
a = get(handles.Min_EditText2,'String'); varEnd(5) = str2num(a);
a = get(handles.SS_EditText2,'String');  varEnd(6) = str2num(a);

varStart(4) = varStart(4) + datevec;
varEnd(4) = varEnd(4) + datevec;

if (varStart(4) < 0 || varEnd(4) > 23)
    errordlg('Check the time selection',...
			 'Check the time selection','modal')
    return
end

start_time = datestr(varStart);
strDate = datestr([varStart(1) varStart(2) varStart(3) 0 0 0]);
refDate = datestr([varStart(1) 1 1 0 0 0]);
numDate_Start = datenum(strDate)-datenum(refDate)+1;

end_time = datestr(varEnd);
strDate = datestr([varEnd(1) varEnd(2) varEnd(3) 0 0 0]);
refDate = datestr([varEnd(1) 1 1 0 0 0]);
numDate_End = datenum(strDate)-datenum(refDate)+1;

startDay = handles.data_user.startDay;

%This code converts the start time into 1 second, 4 second, 1 minute and 1
%hour interval index values. The (length of day in desired unit)*k seems to have something to
%do with the time stepping code I removed

k = numDate_Start-startDay;
iStart = varStart(4)*3600+varStart(5)*60+varStart(6)+86400*k+1; %86400 seconds in a day 
iStart1 = varStart(4)*60+varStart(5)+60*24*k+1; 
iStart2 = ceil(varStart(4)*15+varStart(5)/4+15*24*k+1); 
iStart3 = varStart(4)+1; 

k = numDate_End-startDay;
iEnd = varEnd(4)*3600+varEnd(5)*60+varEnd(6)+86400*k+1;
iEnd1 = varEnd(4)*60+varEnd(5)+60*24*k+1;
iEnd2 = floor(varEnd(4)*15+varEnd(5)/4+15*24*k+1);
iEnd3 = varEnd(4)+1;


if iStart >= iEnd
	errordlg('Check the time selection',...
			 'Check the time selection','modal')
    return
end

SatNum = get(handles.DMSP_EditText,'String');
strSat = sprintf('DMSP: F%s',SatNum);
strTime = sprintf('Generated on  %s',datestr(clock));
numDate = numDate_End;
strDate = [start_time,'   -   ',end_time];
strDate1 = [start_time,' (',num2str(numDate_Start),')','  -  ',end_time,...
                       ' (',num2str(numDate_End),')'];
                   
% global variables, initial values
%---------------------------------
FS = 9; FS1 = 8; LW = 1; NR = 0.035; MS1 = 8;stateRB = 'North'; stateCoord = 'Geom';

data_plot.iStart = iStart; data_plot.iStart1 = iStart1; data_plot.iStart2 = iStart2; 
data_plot.iStart3 = iStart3;
data_plot.iEnd = iEnd; data_plot.iEnd1 = iEnd1; data_plot.iEnd2 = iEnd2;
data_plot.iEnd3 = iEnd3;
data_plot.SatNum = SatNum; data_plot.strSat = strSat; data_plot.strTime = strTime;
data_plot.numDate_Start = numDate_Start; data_plot.numDate_End = numDate_End;
data_plot.start_time = start_time; data_plot.end_time = end_time;
data_plot.strDate = strDate; data_plot.strDate1 = strDate1;

data_plot.pos.count = length(get(handles.cartList,'UserData')); 
data_plot.pos.width = 0.55; data_plot.pos.spacing = 0.026;
data_plot.pos.top = 0.062; data_plot.pos.bottom = 0.02; data_plot.pos.left = 0.06;
data_plot.pos.height = (1-data_plot.pos.bottom-data_plot.pos.top- ...
        (data_plot.pos.count-1)*data_plot.pos.spacing)/data_plot.pos.count;
    
len = length(iStart:iEnd);
if len > 60*60*2
    pChoice = 3;
else
    pChoice = 1;
end

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

global data_plot map_color stateRB stateCoord hbStatusBar hbDataBar ...
       FS FS1 LW NR MS1 pChoice 
   
a = get(handles.YY_EditText1,'String');  varStart(1) = str2num(a);
a = get(handles.MM_EditText1,'String');  varStart(2) = str2num(a);
a = get(handles.DD_EditText1,'String');  varStart(3) = str2num(a);
a = get(handles.HH_EditText1,'String');  varStart(4) = str2num(a);
a = get(handles.Min_EditText1,'String'); varStart(5) = str2num(a);
a = get(handles.SS_EditText1,'String');  varStart(6) = str2num(a);

a = get(handles.YY_EditText2,'String');  varEnd(1) = str2num(a);
a = get(handles.MM_EditText2,'String');  varEnd(2) = str2num(a);
a = get(handles.DD_EditText2,'String');  varEnd(3) = str2num(a);
a = get(handles.HH_EditText2,'String');  varEnd(4) = str2num(a);
a = get(handles.Min_EditText2,'String'); varEnd(5) = str2num(a);
a = get(handles.SS_EditText2,'String');  varEnd(6) = str2num(a);

start_time = datestr(varStart);
strDate = datestr([varStart(1) varStart(2) varStart(3) 0 0 0]);
refDate = datestr([varStart(1) 1 1 0 0 0]);
numDate_Start = datenum(strDate)-datenum(refDate)+1;

end_time = datestr(varEnd);
strDate = datestr([varEnd(1) varEnd(2) varEnd(3) 0 0 0]);
refDate = datestr([varEnd(1) 1 1 0 0 0]);
numDate_End = datenum(strDate)-datenum(refDate)+1;

startDay = handles.data_user.startDay;

%This code converts the start time into 1 second, 4 second, 1 minute and 1
%hour interval index values. The (length of day in desired unit)*k seems to have something to
%do with the time stepping code I removed

k = numDate_Start-startDay;
iStart = varStart(4)*3600+varStart(5)*60+varStart(6)+86400*k+1; %86400 seconds in a day 
iStart1 = varStart(4)*60+varStart(5)+60*24*k+1; 
iStart2 = ceil(varStart(4)*15+varStart(5)/4+15*24*k+1); 
iStart3 = floor(varStart(4)+24*k+1); 

k = numDate_End-startDay;
iEnd = varEnd(4)*3600+varEnd(5)*60+varEnd(6)+86400*k+1;
iEnd1 = varEnd(4)*60+varEnd(5)+60*24*k+1;
iEnd2 = floor(varEnd(4)*15+varEnd(5)/4+15*24*k+1);
iEnd3 = floor(varEnd(4)+24*k+1);


if iStart >= iEnd
	errordlg('Check the time selection',...
			 'Check the time selection','modal')
    return
end

SatNum = get(handles.DMSP_EditText,'String');
strSat = sprintf('DMSP: F%s',SatNum);
strTime = sprintf('Generated on  %s',datestr(clock));
numDate = numDate_End;
strDate = [start_time,'   -   ',end_time];
strDate1 = [start_time,' (',num2str(numDate_Start),')','  -  ',end_time,...
                       ' (',num2str(numDate_End),')'];
                   
% global variables, initial values
%---------------------------------
FS = 9; FS1 = 8; LW = 1; NR = 0.035; MS1 = 8;stateRB = 'North'; stateCoord = 'Geom';

data_plot.iStart = iStart; data_plot.iStart1 = iStart1; data_plot.iStart2 = iStart2; 
data_plot.iStart3 = iStart3;
data_plot.iEnd = iEnd; data_plot.iEnd1 = iEnd1; data_plot.iEnd2 = iEnd2;
data_plot.iEnd3 = iEnd3;
data_plot.SatNum = SatNum; data_plot.strSat = strSat; data_plot.strTime = strTime;
data_plot.numDate_Start = numDate_Start; data_plot.numDate_End = numDate_End;
data_plot.start_time = start_time; data_plot.end_time = end_time;
data_plot.strDate = strDate; data_plot.strDate1 = strDate1;

data_plot.pos.count = length(get(handles.cartList,'UserData')); 
data_plot.pos.width = 0.55; data_plot.pos.spacing = 0.026;
data_plot.pos.top = 0.062; data_plot.pos.bottom = 0.02; data_plot.pos.left = 0.06;
data_plot.pos.height = (1-data_plot.pos.bottom-data_plot.pos.top- ...
        (data_plot.pos.count-1)*data_plot.pos.spacing)/data_plot.pos.count;
    
len = length(iStart:iEnd);
if len > 60*60*2
    pChoice = 3;
else
    pChoice = 1;
end
    
data_plot.default.list_plot = {'Vy' 'BxBy' 'Vz' 'EFlx' 'Sz' 'IMF-Bxyz' 'V_sw' 'Psw'};
data_plot.default.list_polar = {'EEFlx' 'IEFlx' 'Sz' 'Vy'};
data_plot.default.list_global = {'Asc./Des.'};
data_plot.default.list_global_Sz = {'Asc./Des.'};

data_plot.default.plot_Factor = ones(10,1);
data_plot.default.polar_Factor = ones(4,1);
data_plot.default.polar_plot_type = ones(4,1)*pChoice;
data_plot.default.global_Factor = 1;
data_plot.default.global_Factor_Sz = 1;
data_plot.default.global_Factor_loc = 1;


% data_plot.list_plot = data_plot.default.list_plot;
% data_plot.list_polar = data_plot.default.list_polar;
% data_plot.list_global = data_plot.default.list_global;
% 
% data_plot.plot_Factor = data_plot.default.plot_Factor ;
% data_plot.polar_Factor = data_plot.default.polar_Factor;
% data_plot.polar_plot_type = data_plot.default.polar_plot_type;
% data_plot.global_Factor = data_plot.default.global_Factor;

% generate the plotting figure
%-----------------------------
PP = [0 0.05 1 0.895];
hf = figure('Units','normalized','Position',PP,'Name','DMSP_Plotter','NumberTitle','off');            
            % 'Toolbar','none','Menubar','none');
col_lim = 56; map_color1 = jet;
map_color = map_color1(1:col_lim,:);
        
hp_plot = uipanel('Parent',hf,'Units','normalized','BackgroundColor','white',...
                  'Position',[.01 .06 .25 .93]);
hp1_plot = uipanel('Parent',hf,'Units','normalized','BackgroundColor','white',...
                  'Position',[.265 .06 .725 .93]);
hp_control = uipanel('Parent',hf,'Units','normalized','Position',[.01 .001 .98 .055]);
hbStatusBar = uicontrol('Parent',hp_control,'Units','normalized','Style','text',...
                  'BackgroundColor','white','Position',[0.83 0.2 0.16 0.6]);
set(hf,'KeyPressFcn',{@hf_KeyPressFcn,hbStatusBar})

hbDataBar = uicontrol('Parent',hp_control,'Units','normalized','Style','text',...
                  'BackgroundColor','white','Position',[0.551 0.2 0.16 0.6],'FontSize',12);
              

hbg = uibuttongroup('Parent',hp_control,'Units','normalized','Position',[0.005 0.08 0.15 0.9], ...
                'Title','Select Hemisphere','TitlePosition','centertop','FontSize',6,'SelectionChangeFcn',@selcbk);
hb0 = uicontrol('Parent',hbg,'Style','Radio','String','North', ...
                'Units','normalized','Position',[0.03 0.27 0.3 0.6], ...
                'ToolTipString','Select Northern Hemisphere');
hb1 = uicontrol('Parent',hbg,'Style','Radio','String','South',...
                'Units','normalized','Position',[0.35 0.27 0.3 0.6],...
                'ToolTipString','Select Southern Hemisphere');
hb2 = uicontrol('Parent',hbg,'Style','Radio','String','N & S',...
                'Units','normalized','Position',[0.67 0.27 0.3 0.6], ...
                'ToolTipString','Select Both Hemispheres');
set(hbg,'SelectedObject',hb0);


hbgn = uibuttongroup('Parent',hp_control,'Units','normalized','Position',[0.16 0.08 0.092 0.9],...
            'Title','Select Coordinates','TitlePosition','centertop','FontSize',6,'SelectionChangeFcn',@selcbkn);
hbn0 = uicontrol('Parent',hbgn,'Style','Radio','String','Geom',...
            'Units','normalized','Position',[0.04 0.27 0.45 0.6],...
            'ToolTipString','Select Geom Coordinates');
hbn1 = uicontrol('Parent',hbgn,'Style','Radio','String','Geog',...
            'Units','normalized','Position',[0.52 0.27 0.45 0.6],...
            'ToolTipString','Select Geog Coordinates');
set(hbgn,'SelectedObject',hbn0);

%{
List_items = {'8','7','6','5','4'};
lbText = uicontrol('Parent',hp_control, 'Style','text','Units','normalized',...
         'Position',[0.265 0.4 0.0315 0.5],'HorizontalAlignment','left',...
         'FontSize',8,'String','Nr. Plots','ToolTipString',...
         'Select the nr. of plots for the middle column');
tl = uicontrol('Parent',hp_control,'Style','popupmenu','String',List_items,...
         'Units','normalized','Position',[0.265 0 0.0315 0.6],...
         'Callback',@list_callback,'ToolTipString', ...
         'Select the nr. of plots for the middle column');
%}

%lmk - this button is unnessecary, but I am leaving it because the callback 
%      already works, so I am just going to 'press' the button with a hardcoded event 
%      at the end of the Display button callback
hbPlotD = uicontrol('Parent',hp_control,'String','Plot','Units','normalized',...
         'Position',[0.309 0.15 0.055 0.7],'Callback',{@plotD_all_data,hbStatusBar,hp_plot, ...
         hp1_plot,handles},'ToolTipString','Default Plots');
%{
hbPlotS = uicontrol('Parent',hp_control,'String','PlotS','Units','normalized',...
         'Position',[0.367 0.15 0.055 0.7],'Callback',{@plotS_all_data,hbStatusBar,hp_plot, ...
         hp1_plot,handles},'ToolTipString','Selected Plots');
%}
hbChEFlx = uicontrol('Parent',hp_control,'String','SSJ_Plots','Units','normalized',...
         'Position',[0.367 0.15 0.055 0.7],'Callback',{@EFlx_Plots,hbStatusBar,handles},...
         'ToolTipString','SSJ Channel Polar Plots');
     

hbSave = uicontrol('Parent',hp_control,'String','Save (s)','Units','normalized',...
         'Position',[0.435 0.15 0.055 0.7],'Callback',{@save_figure,hbStatusBar},...
         'ToolTipString','Save figure');
hbClose = uicontrol('Parent',hp_control,'String','Close (q)','Units','normalized',...
         'Position',[0.493 0.15 0.055 0.7],'Callback','closereq',...
         'ToolTipString','Close figure');
hbStepBack = uicontrol('Parent',hp_control,'String','<','Units','normalized',...
         'Position',[0.72 0.15 0.055 0.7],'Callback',{@StepClbk,-1,handles},...
         'ToolTipString','Step back 1 hour');
hbStepForward = uicontrol('Parent',hp_control,'String','>','Units','normalized',...
         'Position',[0.77 0.15 0.055 0.7],'Callback',{@StepClbk,1,handles},...
         'ToolTipString','Step forward 1 hour');     
     
set(hbStatusBar,'String','Press Plot button to display the data');
set(hbDataBar,'String','Extra info will appear here');

guidata(hObject, handles); %update the handles

%------------
% save figure
%------------
function hf_KeyPressFcn(source,eventdata,hbStatusBar)

keycode = double(get(source,'CurrentCharacter'));

if ~isempty(keycode)
switch keycode
    case double('s')
        set(hbStatusBar,'String','Saving the figure...');
        pause(2)
  
        [filename,pathname] = uiputfile('*.tif','Save As');
        if filename ~= 0
            FullPath = [pathname '/' filename];
            set(gcf,'PaperPositionMode','auto','InvertHardcopy','off')
            print('-dtiff','-zbuffer','-r0',FullPath)
        end

        set(hbStatusBar,'String','Done saving the figure...');
        pause(2)
    case double('q')
        closereq                  
end
end

%------------
% save figure
%------------
function save_figure(source,eventdata,hbStatusBar)

set(hbStatusBar,'String','Saving the figure...');
pause(2)
  
[filename,pathname] = uiputfile('*.tif','Save As');
if filename ~= 0
    FullPath = [pathname '/' filename];
    set(gcf,'PaperPositionMode','auto','InvertHardcopy','off')
    print('-dtiff','-zbuffer','-r0',FullPath)
end

set(hbStatusBar,'String','Done saving the figure...');
pause(2)    



%-------------------
% plot the EFlx data
%-------------------
function EFlx_Plots(source,eventdata,hbStatusBar,handles)
global data_plot map_color stateRB stateCoord strCoord hax FS ...
       IEFlxCh EEFlxCh x y Slope pChoice Energy strTitle strDate1

if length(handles.data_user.dataP) == 0
	errordlg('Read the data!!!',...
			 'Read the data!!!','modal')
    return
end

set(hbStatusBar,'String','Plotting the SSJ channel data');
pause(2)

iStart = data_plot.iStart; 
iEnd = data_plot.iEnd;
strDate1 = data_plot.strDate1;

data1P = handles.data_user.dataP(iStart:iEnd,:);
data1_egeo = handles.data_user.data_egeo(iStart:iEnd,:);
data1_ieflx = handles.data_user.data_ieflx(iStart:iEnd,:);
data1_eeflx = handles.data_user.data_eeflx(iStart:iEnd,:);

k = find(data1P == 0); data1P(k) = NaN;
k = find(abs(data1_egeo) < 10^(-30)); data1_egeo(k) = NaN;
k = find(abs(data1_ieflx) < 10^(-30)); data1_ieflx(k) = NaN;
k = find(abs(data1_eeflx) < 10^(-30)); data1_eeflx(k) = NaN;

MLAT = data1_egeo(:,14); MLT = data1_egeo(:,13);
k = find(~isnan(data1P(:,5)));
if length(k) ~= 0
    MLAT = data1P(:,5); MLT = data1P(:,6);
end

GLAT = data1P(:,7); GLON = data1P(:,8);
k = find(~isnan(data1_egeo(:,7)));
if length(k) ~= 0
    GLAT = data1_egeo(:,7); GLON = data1_egeo(:,8);
end
clear data1_egeo


clear data1P data1_egeo 

% select Northern/Southern or Both Hemispheres
%---------------------------------------------
kk = []; strTitle = [];
switch stateRB
    case 'North'
        % execute this code when North is selected        
        if strcmp(stateCoord,'Geom')
            kk = find(MLAT < 45.0);            
        elseif strcmp(stateCoord,'Geog')
            kk = find(GLAT < 45.0);            
        end        
        strTitle = '    NORTHERN  HEMISPHERE    ';         
        
    case 'South'
        % execute this code when South is selected
        if strcmp(stateCoord,'Geom')
            kk = find(MLAT > -45.0);            
        elseif strcmp(stateCoord,'Geog')
            kk = find(GLAT > -45.0);
        end
        strTitle = '    SOUTHERN  HEMISPHERE    ';
        
    case 'N & S'
        % execute this code when North & South is selected
        if strcmp(stateCoord,'Geom')
            kk = find(MLAT < 45.0 & MLAT > -45.0);
        elseif strcmp(stateCoord,'Geog')
            kk = find(GLAT < 45.0 & GLAT > -45.0);           
        end        
        strTitle = '    NORTHERN  AND  SOUTHERN  HEMISPHERES    ';
end

EEFlxCh = data1_eeflx(:,7:25); EEFlxCh(kk,:) = NaN;
IEFlxCh = data1_ieflx(:,7:25); IEFlxCh(kk,:) = NaN;
MLAT(kk) = NaN; MLT(kk) = NaN; GLAT(kk) = NaN; GLON(kk) = NaN;
clear data1_eeflx data1_ieflx

if strcmp(stateCoord,'Geom')
    strCoord = '(Geom)';    
    th =(MLT*15-90)*pi/180; rho = 90-abs(MLAT);                   
elseif strcmp(stateCoord,'Geog')
    strCoord = '(Geog)';   
    th = (GLON-90)*pi/180; rho = 90-abs(GLAT);    
end

% calculate the polar coordinates and the slope
[x,y] = pol2cart(th,rho);
Slope = determine_slope(x,y);
clear GLAT GLON MLAT MLT th rho

Energy = [30.0 20.4 13.9 9.45 6.46 4.40 3.00 2.04 1.39 0.949 0.646 0.440... 
          0.300 0.204 0.139 0.095 0.065 0.044 0.030 0.0];     

% generate the plotting figure
PP = [0 0.05 0.85 0.8];
hfs = figure('Units','normalized','Position',PP,'Name','SSJ_Plotter', ...
             'NumberTitle','off','Color','white');
set(hfs,'ButtonDownFcn',{@mouse_click_EFlx_plots,hbStatusBar});

% generate the axes
columns = 5; left = 0.01; right = 0.02; spacingH = 0.01; 
width = (1-left-right-(columns-1)*spacingH)/columns;
rows = 4; top = 0.08; bottom = 0.04; spacingV = 0.05;
height = (1-top-bottom-(rows-1)*spacingV)/rows; 
for i = 1:rows
    for j = 1:columns        
        k = (i-1)*columns+j;       
        if k < 20
            valLeft = left+(j-1)*(width+spacingH);
            valBottom = bottom+(rows-i)*(height+spacingV);
            hax(i,j) = axes('Units','normalized','Position',[valLeft valBottom width height], ...
                            'FontSize',FS,'Visible','off');
        end
    end
end

strCB = 'EEFlx [#/(cm^2 s sr eV)]';
EFlx_polar_all(EEFlxCh,x,y,Slope,pChoice,map_color,strCB,strCoord)

set(hbStatusBar,'String','Done plotting the SSJ channel data');
pause(2)


%-------------------------------------
% select parameter - callback function
%-------------------------------------
function mouse_click_EFlx_plots(source,eventdata,hbStatusBar)
global txp txep
if strcmp(get(gcf,'SelectionType'),'alt')   
    ptl = get(gcf,'CurrentPoint');
    xl = ptl(1,1); yl = ptl(1,2);
    txp = uicontrol(gcf,'Style', 'text','String','Plot Type',...
                   'Units','normalized','Position',[xl yl+0.058 0.05 0.025],...
                   'BackgroundColor','white','HorizontalAlignment','left'); 
    txep = uicontrol(gcf,'Style', 'edit','String','1',...
                   'Units','normalized','Position',[xl+0.05 yl+0.061 0.029 0.025],...
                   'BackgroundColor','white','HorizontalAlignment','left');
    List_items = {'None','IEFlx','EEFlx'};    
    tl = uicontrol(gcf,'Style', 'listbox','String',List_items,...
                   'Units','normalized','Position',[xl yl 0.08 0.06],'Visible','on',...
                   'BackgroundColor','white');
    set(tl,'Callback',{@list_callback_EFlx_plots,hbStatusBar});
end


function list_callback_EFlx_plots(source,eventdata,hbStatusBar)
global IEFlxCh EEFlxCh x y Slope map_color strCoord txp txep

if strcmp(get(gcf,'SelectionType'),'open')
    file_list = get(source,'String');
    index_selected = get(source,'Value');
    param = file_list{index_selected};    
    pC = str2num(get(txep,'String'));
    delete(source)
    delete(txp)
    delete(txep)    
    
    set(hbStatusBar,'String','Plotting the SSJ channel data');
    pause(2)
    
    switch param
        case 'None'
        case 'IEFlx'
            strCB = 'IEFlx [#/(cm^2 s sr eV)]';
            EFlx_polar_all(IEFlxCh,x,y,Slope,pC,map_color,strCB,strCoord)
        case 'EEFlx'
            strCB = 'EEFlx [#/(cm^2 s sr eV)]';
            EFlx_polar_all(EEFlxCh,x,y,Slope,pC,map_color,strCB,strCoord)
    end 
    
    set(hbStatusBar,'String','Done plotting the SSJ channel data');
    pause(2)
end


%---------------------
% generate polar plots
%---------------------
function EFlx_polar_all(EFlxCh,x,y,Slope,pC,map_color,strCB,strCoord)
global Energy hax strTitle strDate1 FS FS1

[rows,columns] = size(hax);
for i = 1:rows
    for j = 1:columns
        k = (i-1)*columns+j;
        if k < 20               
            axes(hax(i,j)); cla(gca,'reset');
            strEFlx = [num2str(Energy(k)),' - ',num2str(Energy(k+1)),' ','keV'];
            polar_EFlx(EFlxCh(:,k),x,y,Slope,pC,map_color,strEFlx);
            pause(1)
        end
    end
end

% include the title
axes(hax(1,3))
if strcmp(strCB(1:5),'EEFlx')
    strTitle_all = [strTitle,' - ','Electron Energy Flux [#/(cm^2 s sr eV)] - (background removed)',' - ',strCoord];
elseif strcmp(strCB(1:5),'IEFlx')
    strTitle_all = [strTitle,' - ','Ion Energy Flux [#/(cm^2 s sr eV)] - (background removed)',' - ',strCoord];
end
text(-160,87,strTitle_all,'FontSize',FS+1)
text(-100,75,strDate1,'FontSize',FS)

pause(1)

% add colorbar
if pC ~= 2  
    axes(hax(4,4))
    text(65,42,strCB,'FontSize',7);
    
    bX(1) = 3; bX(2) = 6; bX(3) = 9;
    len_color = length(map_color);
    xMax = max(bX); xMin = min(bX);
    bY = len_color/(xMax-xMin)*(bX-xMin)+1;
    
    colormap(map_color) 
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)+0.003 P(2)+0.04 0.006 0.6*P(4)],...
             'yTick',bY,'yTickLabels','');   
    
    % colorbar('Position',[0.785 0.08 0.005 0.1],'yTick',bY,'yTickLabels','');
    text(76,-27,'10^3','FontSize',FS1); 
    text(76,4,'10^6','FontSize',FS1);
    text(76,33,'10^9','FontSize',FS1);
    
end
    

%--------------------------------
% generate individual polar plots
%--------------------------------
function polar_EFlx(EFlx,x,y,Slope,pChoice,map_color,strEFlx)
global FS LW NR MS1

color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5];
len_color = length(map_color);

draw_polar() 
text(15,57,strEFlx,'FontSize',FS)         

bX(1) = 3; bX(2) = 6; bX(3) = 9;
xMin = min(bX); xMax = max(bX);
inc_color = (xMax-xMin)/len_color;
EFlx = log10(EFlx);

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
set(gca,'yLim',[-51 51]);

xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(EFlx));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = EFlx(k)*2;
    
    % determine the color
    j = 1;
    while EFlx(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1);    
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);        
        
    if pChoice == 1       
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
             'Color',col);    
    elseif pChoice == 3
        plot([x1,x2],[y1,y2],'LineWidth',MS1,'LineStyle','-','Color',col);
    end           
end
if pChoice == 2    
    plot(xm,ym,'.b','MarkerSize',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off

                
%--------------------
% determine the slope
%--------------------
function Slope = determine_slope(x,y)
Slope = nan(size(x)); dx = diff(x); dy = diff(y);
Slope(2:length(x)) = dy./dx; Slope(1) = Slope(2);
kk = find(~isnan(Slope));
avgSlope = mean(abs(Slope(kk)));
k = find(abs(Slope)>1.6*avgSlope); Slope(k) = NaN;
k = find(isnan(Slope)); 
for i = 1:length(k)
    ll = k(i);
    j = 1;
    while j < 6 && isnan(Slope(ll))
        if ll > j && ll < length(Slope)-j
            kk = find(~isnan(Slope(ll-j:ll+j)));
            if length(kk) ~= 0 
                Slope(ll) = mean(Slope(kk));
            end
        elseif ll < j && ll < length(Slope)-j
            kk = find(~isnan(Slope(ll:ll+j)));
            if length(kk) ~= 0 
                Slope(ll) = mean(Slope(kk));
            end
        elseif ll > j && ll > length(Slope)-j
            kk = find(~isnan(Slope(ll-j:ll)));
            if length(kk) ~= 0 
                Slope(ll) = mean(Slope(kk));
            end
        end
        j = j+1;
    end
end 


%-------------------
% callback functions
%-------------------
function selcbk(source,eventdata)
global stateRB
stateRB = get(eventdata.NewValue,'String');
         
         
function selcbkn(source,eventdata)
global stateCoord
stateCoord = get(eventdata.NewValue,'String');
      

function list_callback(source,eventdata)
global data_plot

switch get(source,'Value')   
    case 1                
        count = 10; width = 0.55; spacing = 0.026; top = 0.062; bottom = 0.02; 
        left = 0.06; height = (1-bottom-top-(count-1)*spacing)/count;
    case 2
        count = 9; width = 0.55; spacing = 0.026; top = 0.062; bottom = 0.02; 
        left = 0.06; height = (1-bottom-top-(count-1)*spacing)/count;
    case 3        
        count = 8; width = 0.55; spacing = 0.026; top = 0.062; bottom = 0.02; 
        left = 0.06; height = (1-bottom-top-(count-1)*spacing)/count;
    case 4
        count = 7; width = 0.55; spacing = 0.026; top = 0.062; bottom = 0.02; 
        left = 0.06; height = (1-bottom-top-(count-1)*spacing)/count;
    case 5
        count = 6; width = 0.55; spacing = 0.03; top = 0.065; bottom = 0.025; 
        left = 0.06; height = (1-bottom-top-(count-1)*spacing)/count;
    case 6
        count = 5; width = 0.55; spacing = 0.032; top = 0.065; bottom = 0.025; 
        left = 0.06; height = (1-bottom-top-(count-1)*spacing)/count;
    case 7
        count = 4; width = 0.55; spacing = 0.06; top = 0.08; bottom = 0.04; 
        left = 0.06; height = (1-bottom-top-(count-1)*spacing)/count;    
end

data_plot.pos.count = count; data_plot.pos.width = width; 
data_plot.pos.spacing = spacing; data_plot.pos.top = top; 
data_plot.pos.bottom = bottom; data_plot.pos.left = left;
data_plot.pos.height = height;


%---------------
% update_plots()
%---------------
function update_plots(handles)
global data_plot FS FS1 LW 

% get the data
%-------------
read_data(handles)

list_plot = data_plot.list_plot;
list_polar = data_plot.list_polar;
list_global = data_plot.list_global;
list_global_Sz = data_plot.list_global_Sz;

plot_Factor = data_plot.plot_Factor;
polar_Factor = data_plot.polar_Factor;
polar_plot_type = data_plot.polar_plot_type;
global_Factor = data_plot.global_Factor;
global_Factor_Sz = data_plot.global_Factor_Sz;

t  = data_plot.t;  len  = length(t);  inc  = len/6;
t1 = data_plot.t1; len1 = length(t1); inc1 = len1/6;
t2 = data_plot.t2; len2 = length(t2); inc2 = len2/6;

Slope = data_plot.Slope; SlopeD = data_plot.SlopeD; 
Slope_g = data_plot.Slope_g; Slope_g_Sz = data_plot.Slope_g_Sz;
strArrow = data_plot.strArrow; strArrow1 = data_plot.strArrow1;
x = data_plot.x; y = data_plot.y; xD = data_plot.xD; yD = data_plot.yD;
latD = data_plot.latD; str6 = data_plot.str6; str0 = data_plot.str0;

% plot the position data
%-----------------------

% plot MLT 
%---------
axes(data_plot.hl(1))
strVar = plot_MLT(data_plot.MLT_P,data_plot.MLT_C,data_plot.strDate,data_plot.strSat,t,LW,FS,FS1,str0);
pause(1)

% plot MLAT110 
%-------------
axes(data_plot.hl(2))
strVar = plot_MLAT110(data_plot.MLAT110_P,data_plot.MLAT110_C,t,LW,FS,str0);
pause(1)

% plot Glon
%----------
axes(data_plot.hl(3))
strVar = plot_GLON(data_plot.Glon_P,data_plot.Glon_C,t,LW,FS,str0);
pause(1)

% plot Glat
%----------
axes(data_plot.hl(4))
strVar = plot_GLAT(data_plot.Glat_P,data_plot.Glat_C,t,LW,FS,str6);
pause(1)

% global Sz plots
%----------------
axes(data_plot.hls)
param = list_global_Sz{1};
Factor = global_Factor_Sz(1);
select_Sz_global(param,Factor);
pause(1)

% global Nd plots
%----------------
axes(data_plot.hln)
param = list_global{1};
Factor = global_Factor(1);
select_Nd_global(param,Factor);
pause(1)

% include the title
%------------------
strTitle_all = [data_plot.strTitle,data_plot.strCoord];
axes(data_plot.hT(1))
title(strTitle_all,'Color','black');

axes(data_plot.hT(2))
title(data_plot.strDate1,'Color','black');
pause(1)

% create plots for hp1_plot
%--------------------------
for i = 1:data_plot.pos.count-1    
    axes(data_plot.ha(i))
    param = list_plot{i};
    Factor = plot_Factor(i);
    select_plot(str0,param,Factor);
    pause(1)    
end
i = i+1;
axes(data_plot.ha(i))
param = list_plot{i};
Factor = plot_Factor(i);         
select_plot(str6,param,Factor);
pause(1)

% create the polar plots
%-----------------------
for i = 1:data_plot.pos.pcount    
    axes(data_plot.hap(i))
    param = list_polar{i};
    Factor = polar_Factor(i);
    plot_type = polar_plot_type(i);
    select_polar(param,Factor,plot_type);
    pause(1)
end


%-------------------
% read_data(handles)
%-------------------
function read_data(handles)
global data_plot stateRB stateCoord pChoice hbDataBar

iStart  = data_plot.iStart; %minute
iStart1 = data_plot.iStart1; %hourly
iStart2 = data_plot.iStart2;
iStart3 = data_plot.iStart3;
iEnd  = data_plot.iEnd;
iEnd1 = data_plot.iEnd1;
iEnd2 = data_plot.iEnd2;
iEnd3 = data_plot.iEnd3;

len = length(iStart:iEnd);
if len > 60*60*2
    pChoice = 3;
else
    pChoice = 1;
end

data1P = handles.data_user.dataP(iStart:iEnd,:);
data1M = handles.data_user.dataM(iStart:iEnd,:);
vecTime1 = handles.data_user.vecTime(iStart:iEnd,:);
data1_ssj = handles.data_user.data_ssj(iStart:iEnd,:);
data1_OMNI = handles.data_user.data_OMNI(iStart3:iEnd3,:);
data1_idm = handles.data_user.data_idm(iStart:iEnd,:);

EV = handles.data_user.data_EV(iStart2:iEnd2,:);

k = find(data1M == 0); data1M(k) = NaN;
k = find(data1P < -30000); data1P(k) = NaN; 
k = find(EV == 0); EV(k) = NaN; 
k = find(abs(data1_ssj) < 10^(-30)); data1_ssj(k) = NaN;
k = find(data1_OMNI == -32767); data1_OMNI(k) = NaN; 
k = find(data1_OMNI == -999); data1_OMNI(k) = NaN;
k = find(data1_idm == 999.0000); data1_idm(k) = NaN;

len  = length(iStart:iEnd);   data_plot.t  = 1:len;  inc = len/6;
len1 = length(iStart1:iEnd1); data_plot.t1 = 1:len1; inc1 = len1/6;
len2 = length(iStart2:iEnd2); data_plot.t2 = 1:len2; inc2 = len2/6;
len3 = length(iStart3:iEnd3); data_plot.t3 = .5:1:len3-.5; inc3 = len3/6;

x01 = vecTime1(1:inc:len,1); x02 = vecTime1(1:inc:len,2);
if length(x01) == 6
    x01 = [x01; vecTime1(len,1)]; x02 = [x02; vecTime1(len,2)];
end

str6 = {}; str0 = '';
for i = 1:7
    str6 = [str6,{sprintf('%02d:%02d',x01(i),x02(i))}];        
end
k = strcmp(str6,'NaN:NaN'); kkk = find(k==1);
if length(kkk) ~= 0
    str6(kkk) = {'  NaN'};
end
data_plot.str0 = str0;
data_plot.str6 = str6;
clear str0 str6 x01 x02 

refDate = datestr([vecTime1(1,5) 1 1 0 0 0]);
startX = datenum(refDate)-1+vecTime1(1,4)+ ...
                 (vecTime1(1,1)*3600+ ...
                  vecTime1(1,2)*60+ ...
                  vecTime1(1,3))/3600/24;
start_time = datestr(startX);              
refDate = datestr([vecTime1(len,5) 1 1 0 0 0]);
endX = datenum(refDate)-1+vecTime1(len,4)+ ...
               (vecTime1(len,1)*3600+ ...
                vecTime1(len,2)*60+ ...
                vecTime1(len,3))/3600/24;
end_time = datestr(endX);            
strDate = [start_time,'   -   ',end_time];
strDate1 = [start_time,' (',num2str(vecTime1(1,4)),')','  -  ',end_time,...
                       ' (',num2str(vecTime1(len,4)),')'];
                   
data_plot.strDate1 = strDate1;
data_plot.strDate = strDate;

sT = datestr(startX,31);
set(handles.YY_EditText1,'String',sT(1:4));
set(handles.MM_EditText1,'String',sT(6:7));
set(handles.DD_EditText1,'String',sT(9:10));
set(handles.HH_EditText1,'String',sT(12:13));
set(handles.Min_EditText1,'String',sT(15:16));
set(handles.SS_EditText1,'String',sT(18:19));

eT = datestr(endX,31);
set(handles.YY_EditText2,'String',eT(1:4));
set(handles.MM_EditText2,'String',eT(6:7));
set(handles.DD_EditText2,'String',eT(9:10));
set(handles.HH_EditText2,'String',eT(12:13));
set(handles.Min_EditText2,'String',eT(15:16));
set(handles.SS_EditText2,'String',eT(18:19));
clear vecTime1
pause(1)

% select Northern/Southern or Both Hemispheres
%---------------------------------------------
% MLAT = data1P(:,5); MLT = data1P(:,6);  
% GLAT = data1_egeo(:,7); GLON = data1_egeo(:,8); 

MLAT = data1_ssj(:,8); MLT = data1_ssj(:,10); %I am using 110 km data
k = find(~isnan(data1P(:,5))); %check if geo data exists for poynting
if length(k) ~= 0
    MLAT = data1P(:,5); MLT = data1P(:,6);
end

GLAT = data1P(:,7); GLON = data1P(:,8);
k = find(~isnan(data1_ssj(:,6)));
if length(k) ~= 0
    GLAT = data1_ssj(:,6); GLON = data1_ssj(:,7);
end

%{
MLATD = data1C(:,4); MLTD = data1C(:,6); 
GLATD = data1C(:,1); GLOND = data1C(:,2);
%} 

kk = []; kk1 = []; kkD = []; strTitle = [];
SzM = nan(length(data1M),1);
switch stateRB
    case 'North'
        % execute this code when North is selected
        kk = find(MLAT < 0.0);        
        if strcmp(stateCoord,'Geom')
            kk1 = find(MLAT < 45.0);
            %kkD = find(MLATD < 45.0); 
            data_plot.strArrow  = '\uparrow';
            data_plot.strArrow1 = '';
        elseif strcmp(stateCoord,'Geog')
            kk1 = find(GLAT < 45.0);
            %kkD = find(GLATD < 45.0); 
            data_plot.strArrow  = ''; 
            data_plot.strArrow1 = '';
        end
        SzM = data1M(:,5); SzM(kk1) = NaN;        
        data_plot.strTitle = '    NORTHERN  HEMISPHERE    ';         
        
    case 'South'
        % execute this code when South is selected
        kk = find(MLAT >= 0.0);
        if strcmp(stateCoord,'Geom')
            kk1 = find(MLAT > -45.0);
            %kkD = find(MLATD > -45.0);  
            data_plot.strArrow  = '';
            data_plot.strArrow1 = '\downarrow';
        elseif strcmp(stateCoord,'Geog')
            kk1 = find(GLAT > -45.0);
            %kkD = find(GLATD > -45.0);  
            data_plot.strArrow  = ''; 
            data_plot.strArrow1 = '';
        end        
        SzM = -data1M(:,5); SzM(kk1) = NaN;        
        data_plot.strTitle = '    SOUTHERN  HEMISPHERE    ';        
        
    case 'N & S'
        % execute this code when North & South is selected         
        kk = find(MLAT >= 0.0 & MLAT <= 0.0); 
        if strcmp(stateCoord,'Geom')
            kk1 = find(MLAT < 45.0 & MLAT > -45.0);
            %kkD = find(MLATD < 45.0 & MLATD > -45.0);
            k1 = find(MLAT >= 45.0); k2 = find(MLAT <= -45.0);
            data_plot.strArrow  = '\uparrow';
            data_plot.strArrow1 = '\downarrow';
        elseif strcmp(stateCoord,'Geog')
            kk1 = find(GLAT < 45.0 & GLAT > -45.0);
            %kkD = find(GLATD < 45.0 & GLATD > -45.0);
            k1 = find(GLAT >= 45.0); k2 = find(GLAT <= -45.0);  
            data_plot.strArrow  = ''; 
            data_plot.strArrow1 = '';
        end                                
        SzM(k1) = data1M(k1,5); SzM(k2) = -data1M(k2,5); SzM(kk1) = NaN;                      
        data_plot.strTitle = '    NORTHERN  AND  SOUTHERN  HEMISPHERES    ';        
end

MLAT(kk1) = NaN; MLT(kk1) = NaN; GLAT(kk1) = NaN; GLON(kk1) = NaN;
%MLATD(kkD) = NaN; MLTD(kkD) = NaN; GLATD(kkD) = NaN; GLOND(kkD) = NaN;


% MLAT = data1_egeo(:,14); MLT = data1_egeo(:,13);
% k = find(~isnan(data1P(:,5)));
% if length(k) ~= 0
%     MLAT = data1P(:,5); MLT = data1P(:,6);
% end
% 
% GLAT = data1P(:,7); GLON = data1P(:,8);
% k = find(~isnan(data1_egeo(:,7)));
% if length(k) ~= 0
%     GLAT = data1_egeo(:,7); GLON = data1_egeo(:,8);
% end
% 
% MLATD = data1C(:,4); MLTD = data1C(:,6); 
% GLATD = data1C(:,1); GLOND = data1C(:,2);

mlat = data1_ssj(:,8); 
mlt = data1_ssj(:,10);
k = find(~isnan(data1P(:,5)));
if length(k) ~= 0
    mlat = data1P(:,5); 
    mlt = data1P(:,6);
end

glat = data1P(:,7); 
glon = data1P(:,8);
k = find(~isnan(data1_ssj(:,5)));
if length(k) ~= 0
    glat = data1_ssj(:,3); 
    glon = data1_ssj(:,4);
end

data_plot.MLT_P = mlt; 
data_plot.MLAT110_P = mlat;
data_plot.Glon_P = glon; 
data_plot.Glat_P = glat;
data_plot.MLON110 = data1_ssj(:,9);
data_plot.Glat110 = data1_ssj(:,6);
data_plot.Glon110 = data1_ssj(:,7);

if strcmp(stateCoord,'Geom')    
    data_plot.strCoord = '(Geom)';
    data_plot.lat = mlat;
    %data_plot.latD = data1C(:,4);
    data_plot.strYlabel = 'Geomagnetic Latitude (deg.)';    
    th =(MLT*15-90)*pi/180; rho = 90-abs(MLAT);               
    %thD =(MLTD*15-90)*pi/180; rhoD = 90-abs(MLATD);    
elseif strcmp(stateCoord,'Geog')
    data_plot.strCoord = '(Geog)';
    data_plot.lat = glat;
    %data_plot.latD = data1C(:,1);
    data_plot.strYlabel = 'Geographic Latitude (deg.)';
    th = (GLON-90)*pi/180; rho = 90-abs(GLAT);     
    %thD = (GLOND-90)*pi/180; rhoD = 90-abs(GLATD); 
end
clear data1_egeo mlt mlat glon glat

% calculate the polar coordinates
[data_plot.x,data_plot.y] = pol2cart(th,rho);
%[data_plot.xD,data_plot.yD] = pol2cart(thD,rhoD); 
clear th rho thD rhoD
pause(1)

data_plot.Slope = determine_slope(data_plot.x,data_plot.y);
data_plot.Slope_g_Sz = determine_slope(data_plot.t',data_plot.lat);

%{
data_plot.SlopeD = nan(size(data_plot.xD)); 
k = find(~isnan(data_plot.xD)&~isnan(data_plot.yD));
if length(k) > 2
    for i = 2:length(k)-1        
        data_plot.SlopeD(k(i)) = (data_plot.yD(k(i+1))-data_plot.yD(k(i-1)))/...
                                 (data_plot.xD(k(i+1))-data_plot.xD(k(i-1)));
    end
    data_plot.SlopeD(k(1)) = data_plot.SlopeD(k(2)); 
    data_plot.SlopeD(k(length(k))) = data_plot.SlopeD(k(length(k)-1));
end
pause(1)

data_plot.Slope_g = nan(size(data_plot.latD)); 
k = find(~isnan(data_plot.latD));
for i = 2:length(k)-1
    data_plot.Slope_g(k(i)) = (data_plot.latD(k(i+1))-data_plot.latD(k(i-1)))/...
                              (data_plot.t(k(i+1))-data_plot.t(k(i-1)));
end
data_plot.Slope_g(k(1)) = data_plot.Slope_g(k(2)); 
data_plot.Slope_g(k(length(k))) = data_plot.Slope_g(k(length(k)-1));
%}

% SSJ particle fluxes
data_plot.Flx = [data1_ssj(:,11) , data1_ssj(:,14)]; data_plot.Flx(kk1,:)= NaN;
data_plot.EEFlxCh = data1_ssj(:,11); data_plot.EEFlxCh(kk1,:) = NaN; 
data_plot.IEFlxCh = data1_ssj(:,14); data_plot.IEFlxCh(kk1,:) = NaN; 
pause(1)

% IDM data
%all quantities are taken for the midpoint of the bin

data_plot.idm_Vy = data1_idm(:,2); %km/s 
idm_Vy = data1_idm(:,2); save('vy.mat','idm_Vy');
data_plot.idm_Vz = data1_idm(:,3); %km/s
data_plot.idm_sVy = data1_idm(:,4); %stdev Vy
data_plot.idm_sVz = data1_idm(:,5); %stdev Vz
data_plot.idm_pot = data1_idm(:,7); %average electrostatic potential in kV
%data_plot.idm_mlat110 = data1_idm(:,8); %corrected magnetic latitude at 110 km trace along field line
%data_plot.idm_mlatssp = data1_idm(:,9); %corrected mag lat at sub satellite point 
%data_plot.idm_mlt = data1_idm(:,10); %magentic local time
%data_plot.idm_invlat = data1_idm(:,11); %invariant lat
%data_plot.idm_lat = data1_idm(:,12); %geographic lat
%data_plot.idm_lon = data1_idm(:,13); %geographic lon

% DMSP data
data_plot.Vxyz = data1P(:,10:12); data_plot.Vxyz(kk1,:) = NaN;
data_plot.Bxy = data1P(:,13:14); data_plot.Bxy(kk1,:) = NaN;
data_plot.Exyz = data1P(:,17:2:21); data_plot.Exyz(kk1,:) = NaN;
data_plot.Exyz_flag = data1P(:,18:2:22); data_plot.Exyz_flag(kk1,:) = NaN;
data_plot.Poy = [data1P(:,23) SzM data1P(:,24)]; data_plot.Poy(kk1,:) = NaN; 
data_plot.Poy(:,4) = data1P(:,23);

% SW data
data_plot.B_OMNI = data1_OMNI(:,13:15); 
data_plot.V_OMNI = data1_OMNI(:,19);
data_plot.Omni_solarflux107 = data1_OMNI(:,6); %Single Daily Value
data_plot.Omni_Kp = data1_OMNI(:,4);
data_plot.RHOSW_OMNI = data1_OMNI(:,18);
data_plot.PSW_OMNI = data1_OMNI(:,21);
data_plot.Omni_Kan_Lee = data1_OMNI(:,24);
data_plot.Omni_HpSmean = data1_OMNI(:,33);
data_plot.Omni_HpNmean = data1_OMNI(:,45);
data_plot.Omni_SH_Hp_ion_mean = data1_OMNI(:,39);
data_plot.Omni_NH_Hp_ion_mean = data1_OMNI(:,51);
data_plot.Omni_Pemean = data1_OMNI(:,57);
data_plot.Omni_Pimean = data1_OMNI(:,63);
data_plot.Omni_AEmean = data1_OMNI(:,75);
data_plot.Omni_ALmean = data1_OMNI(:,78); 
data_plot.Omni_AUmean = data1_OMNI(:,81);
data_plot.Omni_DST = data1_OMNI(:,86);
data_plot.Omni_mti_S = data1_OMNI(:,91);
data_plot.Omni_mti_N = data1_OMNI(:,97);
data_plot.Omni_eqb_mean = data1_OMNI(:,109);
data_plot.Omni_VSWStruct = data1_OMNI(:,111); %Single Daily Value

% Read out the solar wind structure
%----------------------------------
if (handles.data_user.vswflag == 1 && ~isempty(data_plot.Omni_VSWStruct))
    switch data_plot.Omni_VSWStruct(1)
        case 0
            datastr = '0, unclear';
        case 1
            datastr = '1, transient';
        case 2 
            datastr = '2, HSS (fast)';
        case 3
            datastr = '3, slow';
    end
    set(hbDataBar, 'String',strcat('Vsw structure flag=',datastr));
end
%add rest of OMNI stuff here
data_plot.Omni_hour = data1_OMNI(:,3)./100+.5;

data_plot.EV = EV;
clear data1_OMNI
pause(1)


function findCRB(Vy,method)
%---------------
% plotD_all_data
%---------------
switch method
    case 'constant'
    k = find(~isnan(Vy))    
end
        


function plotD_all_data(source,eventdata,hbStatusBar,hp_plot,hp1_plot,handles)
global data_plot FS FS1 LW hbDataBar

if length(handles.data_user.dataP) == 0
	errordlg('Read the data!!!',...
			 'Read the data!!!','modal')
    return
end

set(hbStatusBar,'String','Plotting the data...');
pause(2)

% use the default lists for plotting
%-----------------------------------

%aquire list of plots and number of plots for location data
ind = get(handles.locList,'UserData');
data_plot.loc_length = length(ind);


list_loc = get(handles.locList, 'String');
list_loc = strrep(list_loc(ind), '*', '');
data_plot.list_loc = list_loc;

%get the list contents desired indices
plot_list = get(handles.cartList, 'String');
polar_list = get(handles.polarList, 'String');
i1 = get(handles.cartList, 'UserData');
i2 = get(handles.polarList, 'UserData');

%remove the asterix used to mark a selected plot
plot_list(i1) = strrep(plot_list(i1), '*', ''); 
polar_list(i2) = strrep(polar_list(i2), '*', '');

data_plot.list_plot = plot_list(i1); %must use regular paren instead of curly here
data_plot.list_polar = polar_list(i2);
data_plot.list_global = data_plot.default.list_global;
data_plot.list_global_Sz = data_plot.default.list_global_Sz;

data_plot.plot_Factor = data_plot.default.plot_Factor ;
data_plot.polar_Factor = data_plot.default.polar_Factor;
data_plot.polar_plot_type = data_plot.default.polar_plot_type;
data_plot.global_Factor = data_plot.default.global_Factor;
data_plot.global_Factor_Sz = data_plot.default.global_Factor_Sz;

h.list_plot = data_plot.list_plot;
h.list_polar = data_plot.list_polar;
h.list_loc = data_plot.list_loc;
h.list_global = data_plot.default.list_global;
h.list_global_Sz = data_plot.default.list_global_Sz;

h.plot_Factor = data_plot.default.plot_Factor;
h.polar_Factor = data_plot.default.polar_Factor;
h.polar_plot_type = data_plot.default.polar_plot_type;
h.global_Factor = data_plot.default.global_Factor;
h.global_Factor_Sz = data_plot.default.global_Factor_Sz;
h.global_Factor_loc = data_plot.default.global_Factor_loc;

% plot the data
%--------------
plot_data(hp_plot,hp1_plot,handles,h)

set(hbStatusBar,'String','Done plotting the data');
pause(2)


%----------
% plot_data
%----------
function plot_data(hp_plot,hp1_plot,handles,h)
global data_plot FS FS1 LW

% clear the plotting panels
%--------------------------
hC  = get(hp_plot,'Children');
hC1 = get(hp1_plot,'Children');
if ~isempty(hC)
    delete(hC)
end
if ~isempty(hC1)
    delete(hC1)
end
pause(1)

% get the data
%-------------
read_data(handles)

t  = data_plot.t;  len  = length(t);  inc  = len/6;
t1 = data_plot.t1; len1 = length(t1); inc1 = len1/6;
t2 = data_plot.t2; len2 = length(t2); inc2 = len2/6;
t3 = data_plot.t3; len3 = length(t3); inc3 = len3/6;
Slope = data_plot.Slope; %SlopeD = data_plot.SlopeD; 
%Slope_g = data_plot.Slope_g; 
Slope_g_Sz = data_plot.Slope_g_Sz;
strArrow = data_plot.strArrow; strArrow1 = data_plot.strArrow1;
x = data_plot.x; y = data_plot.y; %xD = data_plot.xD; yD = data_plot.yD;
%latD = data_plot.latD; 
str6 = data_plot.str6; str0 = data_plot.str0;


% plot the position data
%-----------------------
lcount = data_plot.loc_length; 

lwidth = 0.77; ltop = 0.06; lbottom = 0.1; lspacing = 0.01;
lleft = 0.15; lH = (1-lbottom-ltop-(lcount-1)*lspacing)/lcount;

for i = 1:lcount-1
   
    lval = 1-ltop-(i)*lH-(i-1)*lspacing;
    data_plot.hls(i) = axes('Parent',hp_plot,'Position',...
        [lleft lval lwidth lH],'FontSize',FS1,'xTick',[],'Visible','on','Units','Normalized'); 
    param = h.list_loc{i};
    if (strcmp(param,'Poynting Flux'))
        param = h.list_global_Sz{1};
        Factor = h.global_Factor_Sz(1);
        select_Sz_global(param,Factor,str0);
    else
        param = h.list_loc{i};
        Factor = h.global_Factor_loc;
        select_loc(param,Factor,str0);
    end
    pause(1)
end
i = i+1;
lval = 1-ltop-(i)*lH-(i-1)*lspacing;
data_plot.hls(i) = axes('Parent',hp_plot,'Position',...
        [lleft lval lwidth lH],'FontSize',FS1,'Units','Normalized');
        param = h.list_loc{i};
    if (strcmp(param,'Poynting Flux'))
        param = h.list_global_Sz{1};
        Factor = h.global_Factor_Sz(1);
        select_Sz_global(param,Factor,str6);
    else
        Factor = h.global_Factor_loc;
        select_loc(param,Factor,str6);
    end
    
%{
% plot MLT 
%---------
lval = 1-ltop-1*lH-0*lspacing; pause(0)
data_plot.hl(1) = axes('Parent',hp_plot,'Units','normalized','Position',[lleft lval lwidth lH], ...
             'FontSize',FS1,'xTick',[]);
strVar = plot_MLT(data_plot.MLT_P,data_plot.strDate,data_plot.strSat,t,LW,FS,FS1,str0);
pause(1)

% plot MLAT110 
%-------------
lval = 1-ltop-2*lH-1*lspacing; pause(0)
data_plot.hl(2) = axes('Parent',hp_plot,'Units','normalized','Position',[lleft lval lwidth lH], ...
             'FontSize',FS1,'xTick',[]);
strVar = plot_MLAT110(data_plot.MLAT110_P,t,LW,FS,str0);
pause(1)

% plot Glon
%----------
lval = 1-ltop-3*lH-2*lspacing; pause(0)
data_plot.hl(3) = axes('Parent',hp_plot,'Units','normalized','Position',[lleft lval lwidth lH], ...
             'FontSize',FS1,'xTick',[]);
strVar = plot_GLON(data_plot.Glon_P,t,LW,FS,str0);
pause(1)

% plot Glat
%----------
lval = 1-ltop-4*lH-3*lspacing; pause(0)
data_plot.hl(4) = axes('Parent',hp_plot,'Units','normalized','Position',[lleft lval lwidth lH], ...
             'FontSize',FS1,'xTick',[]);
strVar = plot_GLAT(data_plot.Glat_P,t,LW,FS,str6);
pause(1)
%}

%{
% global Sz plots
%----------------
lcount = 4; lwidth = 0.77; ltop = 0.08; lbottom = 0.06; lspacing = 0.1;
lleft = 0.15; lH = (1-lbottom-ltop-(lcount-1)*lspacing)/lcount; 

lval = 1-ltop-(lcount-1)*lH-(lcount-2)*lspacing;
data_plot.hls = axes('Parent',hp_plot,'Position',[lleft lval lwidth lH]);
param = h.list_global_Sz{1};
Factor = h.global_Factor_Sz(1);
select_Sz_global(param,Factor);
pause(1)
%}

% include the title
%------------------
strTitle_all = [data_plot.strTitle,data_plot.strCoord];
data_plot.hT(1) = axes('Parent',hp1_plot,'Units','normalized','Position', ...
            [data_plot.pos.left 0.9665 data_plot.pos.width 0.0005],...
            'FontSize',FS,'xTick',[],'yTick',[],'Visible','on', ...
            'xColor','white','yColor','white','zColor','white');
title(strTitle_all,'Color','black');

data_plot.hT(2) = axes('Parent',hp1_plot,'Units','normalized','Position', ...
           [data_plot.pos.left 0.952 data_plot.pos.width 0.0005],...
           'FontSize',FS1,'xTick',[],'yTick',[],'Visible','on', ...
           'xColor','white','yColor','white','zColor','white');
title(data_plot.strDate1,'Color','black');
pause(1)

% create axes and plots for hp1_plot
%-----------------------------------
for i = 1:data_plot.pos.count-1
    val = 1-data_plot.pos.top-i*data_plot.pos.height-(i-1)*data_plot.pos.spacing;
    data_plot.ha(i) = axes('Parent',hp1_plot,'Units','normalized','Position', ...
                 [data_plot.pos.left val data_plot.pos.width data_plot.pos.height], ...
                 'FontSize',FS1,'xTick',[],'Visible','on');
    if data_plot.pos.count < 7
        set(data_plot.ha(i),'FontSize',FS)
    end
    param = h.list_plot{i};
    Factor = h.plot_Factor(i);
    select_plot(str0,param,Factor);
    pause(1)    
end
i = i+1;
val = 1-data_plot.pos.top-i*data_plot.pos.height-(i-1)*data_plot.pos.spacing;
data_plot.ha(i) = axes('Parent',hp1_plot,'Units','normalized','Position', ...
                 [data_plot.pos.left val data_plot.pos.width data_plot.pos.height], ...
                 'FontSize',FS1,'xTick',[],'Visible','on');
param = h.list_plot{i};
Factor = h.plot_Factor(i);         
select_plot(str6,param,Factor);
pause(1)

% create axes and plots for the polar plots
%------------------------------------------
pcount = 4; pwidth = 0.30; pleft = 0.65; ptop = 0.07; pbottom = 0.03;
pspacing = 0.04; pheight = (1-pbottom-ptop-(pcount-1)*pspacing)/pcount;
data_plot.pos.pcount = pcount; 

for i = 1:pcount
    val = 1-ptop-i*pheight-(i-1)*pspacing;
    data_plot.hap(i) = axes('Parent',hp1_plot,'Units','normalized','Position',[pleft val pwidth pheight],...
                  'FontSize',FS1,'xTick',[],'Visible','off'); 
    param = h.list_polar{i};
    Factor = h.polar_Factor(i);
    plot_type = h.polar_plot_type(i);
    select_polar(param,Factor,plot_type);
    pause(1)
end

% set(data_plot.incS,'Enable','on');
% set(data_plot.decS,'Enable','on');
% set(data_plot.ddecB,'Enable','on');
% set(data_plot.dincB,'Enable','on');
% set(data_plot.incE,'Enable','on');
% set(data_plot.decE,'Enable','on');


%-------------------
% plotting functions
%-------------------

%{
function select_Nd_global(param,Factor)
global data_plot

str6 = data_plot.str6;
latD = data_plot.latD;
Slope_g = data_plot.Slope_g;
t = data_plot.t;

Den_both = data_plot.Nd(:,1)*10^11;
Den_asc = Den_both; k = find(Slope_g<0); Den_asc(k) = NaN;
Den_des = Den_both; k = find(Slope_g>0); Den_des(k) = NaN;
bX = [0 0.5 1 1.5];

cla(gca,'reset');
switch param
    case 'Asc.'
        strVar = global_Nd(Den_asc,t,latD,Slope_g,param,str6,bX*Factor);
    case 'Des.'
        strVar = global_Nd(Den_des,t,latD,Slope_g,param,str6,bX*Factor);
    case 'Asc./Des.'
        strVar = global_Nd(Den_both,t,latD,Slope_g,param,str6,bX*Factor);
end 
set(gca,'ButtonDownFcn',{@mouse_click_global,strVar});
%}

function select_Sz_global(param,Factor,str0)
global data_plot

lat = data_plot.lat;
Slope = data_plot.Slope_g_Sz;
t = data_plot.t;

Sz_both = data_plot.Poy(:,4);
Sz_asc = Sz_both; k = find(Slope<0); Sz_asc(k) = NaN;
Sz_des = Sz_both; k = find(Slope>0); Sz_des(k) = NaN;
bX = [0 5 10 15 20];

cla(gca,'reset');
switch param
    case 'Asc.'
        strVar = global_Sz(Sz_asc,t,lat,Slope,param,str0,bX*Factor);
    case 'Des.'
        strVar = global_Sz(Sz_des,t,lat,Slope,param,str0,bX*Factor);
    case 'Asc./Des.'
        strVar = global_Sz(Sz_both,t,lat,Slope,param,str0,bX*Factor);
end 
set(gca,'ButtonDownFcn',{@mouse_click_global_Sz,strVar});

function select_loc(param,Factor,str0)
global data_plot LW FS FS1

lat = data_plot.lat;
t = data_plot.t;

cla(gca,'reset');
switch param
    case 'MLT'
    strVar = plot_MLT(data_plot.MLT_P,t,LW,FS,FS1,str0);
    
    case 'MLAT110'
    strVar = plot_MLAT110(data_plot.MLAT110_P,t,LW,FS,str0);
    
    case 'GLon'
    strVar = plot_GLON(data_plot.Glon_P,t,LW,FS,str0);

    case 'GLat'
    strVar = plot_GLAT(data_plot.Glat_P,t,LW,FS,str0);
    
    case 'GLon110'
    strVar = plot_GLON110(data_plot.Glon110,t,LW,FS,str0);

    case 'GLat110'
    strVar = plot_GLAT110(data_plot.Glat110,t,LW,FS,str0);
    
    case 'MLON110'
    strVar = plot_MLON110(data_plot.MLON110,t,LW,FS,str0);
    
end 
set(gca,'ButtonDownFcn',{@mouse_click_loc,strVar});

function select_plot(strX,param,FactorP)
global data_plot LW FS1
 
t = data_plot.t; t1 = data_plot.t1; t2 = data_plot.t2; t3 = data_plot.t3;
OMNI_B = sqrt(data_plot.B_OMNI(:,1).^2+data_plot.B_OMNI(:,2).^2+data_plot.B_OMNI(:,3).^2);
OMNI_V = (data_plot.V_OMNI(:,1));

cla(gca,'reset');
switch param
    case 'Vx'            
        bX = [-3 -2 -1 0 1 2 3];
        strVar = plot_Vx(data_plot.Vxyz(:,1),t,LW,FS1,strX,bX*FactorP);         
    case 'Vy'
        bX = [-3 -2 -1 0 1 2 3];
        strVar = plot_Vy(data_plot.Vxyz(:,2),t,LW,FS1,strX,bX*FactorP); 
    case 'idm_Vy'
        bX = [-3 -2 -1 0 1 2 3];
        strVar = plot_idm_Vy(data_plot.idm_Vy,t,LW,FS1,strX,bX*FactorP);
    case 'Vz'            
        bX = [-3 -2 -1 0 1 2 3];
        strVar = plot_Vz(data_plot.Vxyz(:,3),t,LW,FS1,strX,bX*FactorP);
    case 'idm_Vz'            
        bX = [-3 -2 -1 0 1 2 3];
        strVar = plot_idm_Vz(data_plot.idm_Vz,t,LW,FS1,strX,bX*FactorP);
    case 'VxVyVz'
        bX = [-3 -2 -1 0 1 2 3];
        strVar = plot_V(data_plot.Vxyz,t,LW,FS1,strX,bX*FactorP);
    case 'dBx'
        bX = [-2 -1 0 1 2];
        strVar = plot_Bx(data_plot.Bxy(:,1),t,LW,FS1,strX,bX*FactorP);
    case 'dBy'
        bX = [-2 -1 0 1 2];
        strVar = plot_By(data_plot.Bxy(:,2),t,LW,FS1,strX,bX*FactorP);
    case 'BxBy'
        bX = [-2 -1 0 1 2];
        strVar = plot_B(data_plot.Bxy,t,LW,FS1,strX,bX*FactorP);
    case 'Ex'            
        bX = [-100 -50 0 50 100];
        strVar = plot_Ex(data_plot.Exyz(:,1),t,LW,FS1,strX,bX*FactorP);
    case 'Ey'
        bX = [-100 -50 0 50 100];
        strVar = plot_Ey(data_plot.Exyz(:,2),t,LW,FS1,strX,bX*FactorP);
    case 'Ez'
        bX = [-10 -5 0 5 10];
        strVar = plot_Ez(data_plot.Exyz(:,3),t,LW,FS1,strX,bX*FactorP);
    case 'ExEyEz'
        bX = [-10 -5 0 5 10];
        strVar = plot_E(data_plot.Exyz,t,LW,FS1,strX,bX*FactorP);
    case 'IEFlx'
        bX = [10^(-2) 10^(-1) 1 10^1 10^2];
        strVar = plot_IEFlx(data_plot.Flx(:,1),t,LW,FS1,strX,bX*FactorP);
    case 'EEFlx'
        bX = [10^(-2) 10^(-1) 1 10^1 10^2];
        strVar = plot_EEFlx(data_plot.Flx(:,2),t,LW,FS1,strX,bX*FactorP);
    case 'EFlx'
        bX = [10^(-2) 10^(-1) 1 10^1 10^2];
        strVar = plot_Flx(data_plot.Flx,t,LW,FS1,strX,bX*FactorP);
    case 'IEFlx_Ch'
        strVar = plot_IEFlx_Ch(data_plot.IEFlxCh,t,LW,FS1,strX);
    case 'EEFlx_Ch'
        strVar = plot_EEFlx_Ch(data_plot.EEFlxCh,t,LW,FS1,strX);
    case 'Sz'
        bX = [-40 -30 -20 -10 0 10];
        strVar = plot_Poy(data_plot.Poy,t,LW,FS1,strX,bX*FactorP);
    case 'SzM'
        bX = [-40 -30 -20 -10 0 10];
        strVar = plot_SzM(data_plot.Poy,t,LW,FS1,strX,bX*FactorP);
    case 'EFlx/Sz'
        bX = [10^(-6) 10^(-3) 10^0 10^3 10^6];
        strVar = plot_Ratio(data_plot.Ra,t,LW,FS1,strX,bX*FactorP);
    case 'Nd'          
        bX = [0 5 10 15 20]*10^(-12);
        strVar = plot_Nd(data_plot.Nd,t,LW,FS1,strX,bX*FactorP);
    case 'IEF'
        bX = [-10 -5 0 5 10];
        strVar = plot_IEF(data_plot.NpTpPswBetaIEF_OMNI(:,4),t3,LW,FS1,strX,bX*FactorP);
    case 'IMF-Bx'
        bX = [-30 -15 0 15 30];
        strVar = plot_IMF_Bx(data_plot.B_OMNI(:,1),t3,LW,FS1,strX,bX*FactorP);
    case 'IMF-By'
        bX = [-30 -15 0 15 30];
        strVar = plot_IMF_By(data_plot.B_OMNI(:,2),t3,LW,FS1,strX,bX*FactorP);
    case 'IMF-Bz'
        bX = [-30 -15 0 15 30];
        strVar = plot_IMF_Bz(data_plot.B_OMNI(:,3),t3,LW,FS1,strX,bX*FactorP);
    case 'IMF-B'
        bX = [-30 -15 0 15 30];
        strVar = plot_IMF_B(OMNI_B,t3,LW,FS1,strX,bX*FactorP);
    case 'IMF-dB'
        bX = [-4 -2 0 2 4];
        strVar = plot_IMF_dB(data_plot.B_OMNI(:,4),t3,LW,FS1,strX,bX*FactorP);
    case 'IMF-Bxyz'
        bX = [-20 -10 0 10 20];
        strVar = plot_IMF_Bxyz(data_plot.B_OMNI,t3,LW,FS1,strX,bX*FactorP);
    case 'atan(Bz/By)'
        strVar = plot_atan(data_plot.B_OMNI,t3,LW,FS1,strX);
    case 'hour'
        bX = [0 5 10 15 20 25];
        strVar = plot_hour(data_plot.Omni_hour,t3,LW,FS1,strX,bX*FactorP);
    case 'Vx_sw'
        bX = [-1000 -800 -600 -400 -200 0];
        strVar = plot_Vx_sw(data_plot.V_OMNI(:,1),t3,LW,FS1,strX,bX*FactorP);
    case 'Vy_sw'
        bX = [-100 -50 0 50 100];
        strVar = plot_Vy_sw(data_plot.V_OMNI(:,2),t3,LW,FS1,strX,bX*FactorP);
    case 'Vz_sw'
        bX = [-100 -50 0 50 100];
        strVar = plot_Vz_sw(data_plot.V_OMNI(:,3),t3,LW,FS1,strX,bX*FactorP);
    case 'V_sw'
        bX = [0 200 400 600 800 1000];
        strVar = plot_V_sw(OMNI_V,t3,LW,FS1,strX,bX*FactorP);
    case 'Vxyz_sw'
        bX = [-100 -50 0 50 100];
        strVar = plot_Vxyz_sw(data_plot.V_OMNI,t3,LW,FS1,strX,bX*FactorP);
    case 'Psw'
        bX = [0 5 10 15 20];
        strVar = plot_Psw(data_plot.PSW_OMNI,t3,LW,FS1,strX,bX*FactorP);
    case 'AE'
        bX = [0 750 1500];
        strVar = plot_AE(data_plot.Omni_AEmean,t3,LW,FS1,strX,bX*FactorP);
    case 'KL E-field'
        bX = [0 5 10 15];
        strVar = plot_Kan_Lee(data_plot.Omni_Kan_Lee, t3, LW, FS1, strX, bX*FactorP);
    case 'Kp'
        bX = [0 2 4 6 8 10];
        strVar = plot_Kp(data_plot.Omni_Kp, t3, LW, FS1, strX, bX*FactorP);
    case 'Hp'
        bX = [0 50 100 150 200];
        strVar = plot_Hp([data_plot.Omni_HpNmean, data_plot.Omni_HpSmean], t3, ...
            LW, FS1, strX, bX*FactorP);
    case 'Hp_ion'
        bX = [0 20 40 60];
        strVar = plot_Hp_ion([data_plot.Omni_NH_Hp_ion_mean, data_plot.Omni_SH_Hp_ion_mean], ...
            t3, LW, FS1, strX, bX*FactorP);
    case 'Pe'   
        bX = [0 50 100 150 200 250];
        strVar = plot_Pe(data_plot.Omni_Pemean, t3, LW, FS1, strX, bX*FactorP);
    case 'Pi'    
        bX = [0 20 40 60 80];
        strVar = plot_Pi(data_plot.Omni_Pimean, t3, LW, FS1, strX, bX*FactorP);
    case 'AL'
        bX = [0 750 1500];
        strVar = plot_AL(data_plot.Omni_ALmean,t3,LW,FS1,strX,bX*FactorP);
    case 'AU'
        bX = [0 200 400 600];
        strVar = plot_AU(data_plot.Omni_AUmean,t3,LW,FS1,strX,bX*FactorP);
    case 'DST'
        bX = [-100 -50 0 50 100];
        strVar = plot_DST(data_plot.Omni_DST,t3,LW,FS1,strX,bX*FactorP);
    case 'eqb'
        bX = [40 50 60 70];
        strVar = plot_eqb(data_plot.Omni_eqb_mean,t3,LW,FS1,strX,bX*FactorP);
    case 'EV'
        bX = [0 2 4];
        strVar = plot_EV(data_plot.EV,t2,LW,FS1,strX,bX*FactorP);   
  
end
set(gca,'ButtonDownFcn',{@mouse_click_plot,strVar});


function select_polar(param,Factor,plot_type)
global data_plot map_color LW FS1 

pChoice = plot_type;
strArrow = data_plot.strArrow;
strArrow1 = data_plot.strArrow1;
x = data_plot.x; y = data_plot.y;
%xD = data_plot.xD; yD = data_plot.yD;
Slope = data_plot.Slope; %SlopeD = data_plot.SlopeD;
        
cla(gca,'reset');
switch param
    case 'Vx'
        bX = [-3 -2 -1 0 1 2 3]; % Vx
        strVar = display_polar_Vx(data_plot.Vxyz(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'Vy'
        bX = [-3 -2 -1 0 1 2 3]; % Vy
        strVar = display_polar_Vy(data_plot.Vxyz(:,2),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'Vz'
        bX = [-3 -2 -1 0 1 2 3]; % Vz
        strVar = display_polar_Vz(data_plot.Vxyz(:,3),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'dBx'
        bX = [-2 -1 0 1 2]; % Bx
        strVar = display_polar_Bx(data_plot.Bxy(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'dBy'
        bX = [-2 -1 0 1 2]; % By
        strVar = display_polar_By(data_plot.Bxy(:,2),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'Ex'
        bX = [-10 -5 0 5 10]; % Ex
        strVar = display_polar_Ex(data_plot.Exyz(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'Ey'
        bX = [-10 -5 0 5 10]; % Ey
         strVar = display_polar_Ey(data_plot.Exyz(:,2),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'Ez'
        bX = [-10 -5 0 5 10]; % Ez
        strVar = display_polar_Ez(data_plot.Exyz(:,3),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'IEFlx' 
        bX = [0 0.5 1 1.5 2]; % IEFlx
        strVar = display_polar_IEFlx(data_plot.Flx(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
   case 'EEFlx'           
        bX = [0 0.2 0.4 0.6 0.8 1]; % EEFlx
        strVar = display_polar_EEFlx(data_plot.Flx(:,2),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'EFlx'         
        bX = [0 2 4 6 8 10 ]; % EFlx
        strVar = display_polar_EFlx(data_plot.EFlx,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'Sz'
        bX = [0 5 10 15 20]; % Sz
        strVar = display_polar_Sz( data_plot.Poy(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'SzM'            
        bX = [0 5 10 15 20]; % SzM
        strVar = display_polar_SzM( data_plot.Poy(:,2),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'EFlx/Sz'
        bX = [-3 -2 -1 0 1 2 3]; % Ratio
        strVar = display_polar_Ratio(data_plot.Ra(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'Nd'            
        bX = [0 0.5 1 1.5]; % Nd
        strVar = display_polar_Nd(data_plot.Nd(:,2)*10^11,map_color,xD,yD,SlopeD,pChoice,bX*Factor);
    %{
    case 'AMIE-Sz'
        bX = [0 5 10 15 20]; %Sz
        strVar = display_polar_AMIE_Sz(data_plot.AMIE(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'AMIE-Phi'
        bX = [-100 -50 0 50 100];
        strVar = display_polar_AMIE_P(data_plot.AMIE(:,2),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'AMIE-Ex'
        bX = [-10 -5 0 5 10]; % Ex
        strVar = display_polar_AMIE_Ex(data_plot.AMIE(:,3),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);
    case 'AMIE-Ey'
        bX = [-10 -5 0 5 10]; % Ey
        strVar = display_polar_AMIE_Ey(data_plot.AMIE(:,4),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX*Factor);                
    %}
end
set(gca,'Visible','on','yLim',[-51 51],'xColor','white','yColor','white');
set(gca,'ButtonDownFcn',{@mouse_click_polar,strVar});
   

%Ephemeris plots 

function strVar = plot_GLAT(Glat_P,t,LW,FS,str0)
len = length(t); inc = len/6;
if len > 2*60*60
    plot(t,Glat_P,'.b','MarkerSize',0.5); 
else
    plot(t,Glat_P,'.b','MarkerSize',0.5); 
end
ylabel('Glat (deg.)','FontSize',FS)
grid on; axis tight
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
strVar = 'GLat';


function strVar = plot_GLON(Glon_P,t,LW,FS,str0)
len = length(t); inc = len/6;
if len > 2*60*60
    plot(t,Glon_P,'.b','MarkerSize',0.5); 
else
    plot(t,Glon_P,'.b','MarkerSize',0.5);  
end
ylabel('Glon (deg.)','FontSize',FS)
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
grid on; axis tight
strVar = 'GLon';


function strVar = plot_GLAT110(Glat_P,t,LW,FS,str0)
len = length(t); inc = len/6;
if len > 2*60*60
    plot(t,Glat_P,'.b','MarkerSize',0.5); 
else
    plot(t,Glat_P,'.b','MarkerSize',0.5); 
end
ylabel('Glat110 (deg.)','FontSize',FS)
grid on; axis tight
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
strVar = 'GLat110';


function strVar = plot_GLON110(Glon_P,t,LW,FS,str0)
len = length(t); inc = len/6;
if len > 2*60*60
    plot(t,Glon_P,'.b','MarkerSize',0.5); 
else
    plot(t,Glon_P,'.b','MarkerSize',0.5);  
end
ylabel('Glon110 (deg.)','FontSize',FS)
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
grid on; axis tight
strVar = 'GLon110';


function strVar = plot_MLAT110(MLAT110_P,t,LW,FS,str0)
len = length(t); inc = len/6;
if len > 2*60*60
    plot(t,MLAT110_P,'.b','MarkerSize',0.5); 
else
    plot(t,MLAT110_P,'.b','MarkerSize',0.5); 
end
ylabel('MLAT110(deg.)','FontSize',FS)
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
grid on; axis tight
strVar = 'MLAT110';


function strVar = plot_MLON110(MLON110_P,t,LW,FS,str0)
len = length(t); inc = len/6;
if len > 2*60*60
    plot(t,MLON110_P,'.b','MarkerSize',0.5); 
else
    plot(t,MLON110_P,'.b','MarkerSize',0.5); 
end
ylabel('MLON110(deg.)','FontSize',FS)
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
grid on; axis tight
strVar = 'MLON110';


function strVar = plot_MLT(MLT,t,LW,FS,FS1,str0)
len = length(t); inc = len/6;
if len > 2*60*60
    plot(t,MLT,'.b','MarkerSize',0.5); 
else
    plot(t,MLT,'.b','MarkerSize',0.5); 
end
ylabel('MLT(deg.)','FontSize',FS)
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
grid on; axis tight
strVar = 'MLT';


function strVar = global_Sz(Sz,t,lat,Slope,strAD,str6,bX)
global data_plot map_color FS FS1 MS1
NR = 0.5; Sz = -Sz; 
colormap(map_color); len_color = length(map_color); 
len = length(t); inc = len/6; colorPlot = [.5 .5 .5];
xMax = max(bX); xMin = min(bX);
bY = len_color/(xMax-xMin)*(bX-xMin)+1; 
inc_color = (xMax-xMin)/len_color;
m = Slope;
plot(t,lat,'.','MarkerSize',0.5,'Color',colorPlot);hold on

% xx = [t;t];
% yy = [lat';lat'];
% zz = 0*xx;
% cc = [Sz';Sz'];
% surface(xx,yy,zz,cc,'facecolor','none','edgecolor','flat','linewidth',MS1)
% caxis([xMin xMax])

kC = find(~isnan(Sz)&~isnan(m));
if length(kC)>0
    for i = 1:length(kC)
        k = kC(i);
        j = 1;
        while Sz(k) > xMin+j*inc_color
            j = j+1;
        end    
        if (j > len_color)
            col = map_color(len_color,:);
        else
            col = map_color(j,:);
        end        
        
        x1 = t(k) - NR/sqrt(m(k)^2+1);
        y1 = lat(k) - NR*m(k)/sqrt(m(k)^2+1);    
        x2 = t(k) + NR/sqrt(m(k)^2+1);
        y2 = lat(k) + NR*m(k)/sqrt(m(k)^2+1);    
        
        plot([x1,x2],[y1,y2],'LineStyle','-','Color',col,'LineWidth',MS1);
        % plot([t(k),t(kC(i+1))],[latD(k),latD(kC(i+1))],'LineStyle','-',...
        %    'Color',col,'LineWidth',MS1);
    end
end
hold off    
ylabel(data_plot.strYlabel,'FontSize',FS);
set(gca,'yLim',[-90 90],'yTick',[-90 -60 -30 0 30 60 90],'FontSize',FS1)
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str6,'FontSize',FS1)
ht = title('');
PP = get(ht,'Position');
% title(['Thermospheric Neutral Density (x10^1^1  kg/m^3)',' - ',strAD], ...
%        'FontSize',FS+1,'Position',[PP(1) PP(2)+90/20 PP(3)])   
P = get(gca,'Position');
title('Poynting Flux  (mW/m^2)', ...
       'FontSize',FS+1,'Position',[PP(1) PP(2)-270 PP(3)],'Units','Normalized')   
grid on; box on    
P = get(gca,'Position');
xL = [P(1) P(2)-0.045 P(3) 0.008];
colorbar('horizontal','Position',xL,'xTick',bY,'xTickLabel',bX,'FontSize',FS1)
strVar = strAD;


function strVar = plot_IEFlx_Ch(IEFlxCh,t,LW,FS1,str0)
global map_color count
En = [30.0 20.4 13.9 9.45 6.46 4.40 3.00 2.04 1.39 0.949 0.646 0.440... 
      0.300 0.204 0.139 0.095 0.065 0.044 0.030 0.025]*10^3;
len = length(t); inc = len/6;
len_color = length(map_color); 
bX = [3 4  5 6 7 8 9];
xMax = max(bX); xMin = min(bX);
bY = len_color/(xMax-xMin)*(bX-xMin)+1;
inc_color = (xMax-xMin)/len_color;
EFlx = log10(IEFlxCh);
semilogy(t(1),EFlx(1,1),'.w','MarkerSize',2); hold on
[kR,kC] = find(~isnan(EFlx));
if length(kR)>0
    for i = 1:length(kR)                         
        % determine the color
        m = kR(i); n = kC(i);
        j = 1;
        while EFlx(m,n) > xMin+j*inc_color
            j = j+1;
        end    
        if (j > len_color)
            col = map_color(len_color,:);
        else
            col = map_color(j,:);
        end
   
        % plot   
         semilogy([t(m), t(m)],[En(n),En(n+1)],'LineStyle','-', ...
                  'Color',col);    
    end   
end
hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[25 30*10^3],'yTick',[10^2 10^3 10^4])
if count >= 8    
    FS2 = 6;
else    
    FS2 = 6.5;
end
ylabel('Energy (eV)','FontSize',FS2)
grid off; box on
% add colorbar
%-------------   
colormap(map_color)
P = get(gca,'Position');
colorbar('Position',[P(1)+P(3)+0.01 P(2) 0.008 P(4)],'yTick',bY(1:3:7), ...
         'yTickLabel',bX(1:3:7),'FontSize',FS2);           
lh = legend('log_1_0(Ion Energy Flux)  [#/(cm^2 s sr eV)]');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal')
step1 = 0.1;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.025;
set(lh,'Position',PP)
strVar = 'IEFlx_Ch';


function strVar = plot_EEFlx_Ch(EEFlxCh,t,LW,FS1,str0)
global map_color count

En = [30.0 20.4 13.9 9.45 6.46 4.40 3.00 2.04 1.39 0.949 0.646 0.440... 
      0.300 0.204 0.139 0.095 0.065 0.044 0.030 0.025]*10^3;
len = length(t); inc = len/6;
len_color = length(map_color); 
bX = [3 4  5 6 7 8 9];           
xMax = max(bX); xMin = min(bX);
bY = len_color/(xMax-xMin)*(bX-xMin)+1; 
inc_color = (xMax-xMin)/len_color;
EFlx = log10(EEFlxCh);
semilogy(t(1),EFlx(1,1),'.w','MarkerSize',2); hold on
[kR,kC] = find(~isnan(EFlx));
if length(kR)>0
    for i = 1:length(kR)                         
        % determine the color
        m = kR(i); n = kC(i);
        j = 1;
        while EFlx(m,n) > xMin+j*inc_color
            j = j+1;
        end    
        if (j > len_color)
            col = map_color(len_color,:);
        else
            col = map_color(j,:);
        end
   
        % plot   
         semilogy([t(m), t(m)],[En(n),En(n+1)],'LineStyle','-', ...
                  'Color',col);    
    end   
end
hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[25 30*10^3],'yTick',[10^2 10^3 10^4])
if count >= 8    
    FS2 = 6;    
else    
    FS2 = 6.5;
end
ylabel('Energy (eV)','FontSize',FS2)
grid off; box on
% add colorbar
%-------------   
colormap(map_color)
P = get(gca,'Position');
colorbar('Position',[P(1)+P(3)+0.01 P(2) 0.008 P(4)],'yTick',bY(1:3:7),...
         'yTickLabel',bX(1:3:7),'FontSize',FS2)
lh = legend('log_1_0(Electon Energy Flux)  [#/(cm^2 s sr eV)]');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal')
step1 = 0.1;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.025;
set(lh,'Position',PP)
strVar = 'EEFlx_Ch';


function strVar = plot_V(Vxyz,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
Vx = Vxyz(:,1); Vy = Vxyz(:,2); Vz = Vxyz(:,3);
plot(t(1),Vx(1)/1000,'.b','MarkerSize',2); hold on 
plot(t(1),Vy(1)/1000,'.r','MarkerSize',2)
plot(t(1),Vz(1)/1000,'.k','MarkerSize',2);
plot(t,Vx/1000,'-b','LineWidth',LW);  
plot(t,Vy/1000,'-r','LineWidth',LW)
plot(t,Vz/1000,'-k','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
% lh = legend('Horizontal In-Track Ion Drift',...
%             'Horizontal-Transverse Ion Drift',...
%             'Vertical Ion Drift x 10^3  (m/s)');
lh = legend('Vx','Vy','Vz  x 10^3  (m/s)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal')
step = 0.05; step1 = 0.035;
step = 0.12; step1 = 0.08;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
set(ll(4),'MarkerSize',10)
set(ll(7),'MarkerSize',10)
PP = get(ll(6),'Position');
PP(1) = PP(1)+step;
set(ll(6),'Position',PP)
PP = get(ll(9),'Position');
PP(1) = PP(1)+2*step;
set(ll(9),'Position',PP)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(ll(4),'xData');
set(ll(4),'xData',PP+step1+step)
PP = get(ll(7),'xData');
set(ll(7),'xData',PP+step1+2*step)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'VxVyVz';


function strVar = plot_Vx(Vx,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
plot(t(1),Vx(1)/1000,'.b','MarkerSize',2); hold on
plot(t,Vx/1000,'-b','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
% lh = legend('Horizontal In-Track Ion Drift x 10^3  (m/s)');
lh = legend('Vx  x 10^3  (m/s)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal')
step1 = 0.08;
step1 = 0.15;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Vx';


function strVar = plot_Vy(Vy,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
plot(t(1),Vy(1)/1000,'.r','MarkerSize',2); hold on
plot(t,Vy/1000,'-r','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
% lh = legend('Horizontal-Transverse Ion Drift x 10^3  (m/s)');
lh = legend('Vy  x 10^3  (m/s)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal')
step1 = 0.08;
step1 = 0.15;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Vy';


function strVar = plot_idm_Vy(Vy,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
plot(t(1),Vy(1),'.r','MarkerSize',2); hold on
plot(t,Vy,'-r','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
% lh = legend('Horizontal-Transverse Ion Drift x 10^3  (m/s)');
lh = legend('Vy  x 10^3  (m/s)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal')
step1 = 0.08;
step1 = 0.15;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'idm_Vy';


function strVar = plot_Vz(Vz,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
plot(t(1),Vz(1)/1000,'.k','MarkerSize',2); hold on
plot(t,Vz/1000,'-k','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
% lh = legend('Vertical Ion Drift x 10^3  (m/s)');
lh = legend('Vz  x 10^3  (m/s)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal')
step1 = 0.08;
step1 = 0.15;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Vz';


function strVar = plot_idm_Vz(Vz,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
plot(t(1),Vz(1),'.k','MarkerSize',2); hold on
plot(t,Vz,'-k','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
% lh = legend('Vertical Ion Drift x 10^3  (m/s)');
lh = legend('Vz  x 10^3  (m/s)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal')
step1 = 0.08;
step1 = 0.15;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'idm_Vz';


function strVar = plot_E(Exyz,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
Ex = Exyz(:,1)/10; Ey = Exyz(:,2)/10; Ez = Exyz(:,3);
plot(t(1),Ex(1),'.b','MarkerSize',2); hold on 
plot(t(1),Ey(1),'.r','MarkerSize',2)
plot(t(1),Ez(1),'.k','MarkerSize',2);
plot(t,Ex,'-b','LineWidth',LW);  
plot(t,Ey,'-r','LineWidth',LW)
plot(t,Ez,'-k','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('Ex/10','Ey/10','Ez  (mV/m)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal')
step = 0.13; step1 = 0.08;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
set(ll(4),'MarkerSize',10)
set(ll(7),'MarkerSize',10)
PP = get(ll(6),'Position');
PP(1) = PP(1)+step;
set(ll(6),'Position',PP)
PP = get(ll(9),'Position');
PP(1) = PP(1)+2*step;
set(ll(9),'Position',PP)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(ll(4),'xData');
set(ll(4),'xData',PP+step1+step)
PP = get(ll(7),'xData');
set(ll(7),'xData',PP+step1+2*step)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'ExEyEz';


function strVar = plot_Ex(Ex,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
plot(t(1),Ex(1),'.b','MarkerSize',2); hold on
plot(t,Ex,'-b','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)  
grid on; box off
lh = legend('Ex  (mV/m)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal')
step1 = 0.18;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Ex';


function strVar = plot_Ey(Ey,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
plot(t(1),Ey(1),'.r','MarkerSize',2); hold on
plot(t,Ey,'-r','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('Ey  (mV/m)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal')
step1 = 0.18;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Ey';


function strVar = plot_Ez(Ez,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
plot(t(1),Ez(1),'.k','MarkerSize',2); hold on
plot(t,Ez,'-k','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('Ez  (mV/m)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal')
step1 = 0.18;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Ez';


function strVar = plot_B(Bxy,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
Bx = Bxy(:,1); By = Bxy(:,2);
plot(t(1),Bx(1)/1000,'.b','MarkerSize',2); hold on
plot(t(1),By(1)/1000,'.r','MarkerSize',2); 
plot(t,Bx/1000,'-b','LineWidth',LW); 
plot(t,By/1000,'-r','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
% lh = legend('Horizontal In-Track Magnetic Filed Perturbation',...
%              'Horizontal-Transverse Magnetic Filed Perturbation  x 10^3  (nT)');
lh = legend('\DeltaBx','\DeltaBy  x 10^3  (nT)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
% step = 0.05; step1 = 0.035;
step = 0.15; step1 = 0.09;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
set(ll(4),'MarkerSize',10)
PP = get(ll(6),'Position');
PP(1) = PP(1)+step;
set(ll(6),'Position',PP)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(ll(4),'xData');
set(ll(4),'xData',PP+step1+step)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'BxBy';


function strVar = plot_Bx(Bx,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
plot(t(1),Bx(1)/1000,'.b','MarkerSize',2); hold on
plot(t,Bx/1000,'-b','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
% lh = legend('Horizontal In-Track Magnetic Filed Perturbation x 10^3  (nT)');
lh = legend('\DeltaBx  x 10^3  (nT)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.14;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'dBx';


function strVar = plot_By(By,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
plot(t(1),By(1)/1000,'.r','MarkerSize',2); hold on
plot(t,By/1000,'-r','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
% lh = legend('Horizontal-Transverse Magnetic Filed Perturbation x 10^3  (nT)');
lh = legend('\DeltaBy  x 10^3  (nT)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.14;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'dBy';


function strVar = plot_Flx(Flx,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
IEFlx = Flx(:,1); EEFlx = Flx(:,2);
semilogy(t(1),EEFlx(1),'.k','MarkerSize',2); hold on
semilogy(t(1),IEFlx(1),'.r','MarkerSize',2); 
semilogy(t,EEFlx,'-k','LineWidth',LW);  
semilogy(t,IEFlx,'-r','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
box on
lh = legend('EEFlx','IEFlx  (mW/m^2)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step = 0.15; step1 = 0.1;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
set(ll(4),'MarkerSize',10)
PP = get(ll(6),'Position');
PP(1) = PP(1)+step;
set(ll(6),'Position',PP)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(ll(4),'xData');
set(ll(4),'xData',PP+step1+step)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'EFlx';


function strVar = plot_EEFlx(EEFlx,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
semilogy(t(1),abs(EEFlx(1)),'.k','MarkerSize',2); hold on 
semilogy(t,abs(EEFlx),'-k','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
box on
lh = legend('EEFlx  (mW/m^2)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.15;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'EEFlx';


function strVar = plot_IEFlx(IEFlx,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
semilogy(t(1),abs(IEFlx(1)),'.r','MarkerSize',2); hold on
semilogy(t,abs(IEFlx),'-r','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
box on
lh = legend('IEFlx  (mW/m^2)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.15;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'IEFlx';


function strVar = plot_Poy(Poy,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
Sz = Poy(:,1); SzM = Poy(:,2); Sz_ind = Poy(:,3);
k = find(Sz_ind==3); Sz1 = nan(size(Sz)); Sz1(k) = Sz(k);
plot(t(1),Sz1(1),'.k','MarkerSize',2); hold on
plot(t(1),Sz(1),'.r','MarkerSize',2);
% plot(t(1),SzM(1),'.c','MarkerSize',4); hold off
plot(t,Sz,'-r','LineWidth',LW);
plot(t,Sz1,'-k','LineWidth',LW); hold off
%plot(t,SzM,'-c','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
% lh = legend('Sz (2 comp.)','Sz (1 comp.)','Sz - model  (mW/m^2)');
lh = legend('Sz (2 comp.)','Sz (1 comp.)  (mW/m^2)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
% step = 0.12; step1 = 0.06;
% ll = get(lh,'Children');
% set(ll(1),'MarkerSize',10)
% set(ll(4),'MarkerSize',10)
% set(ll(7),'MarkerSize',10)
% PP1 = get(ll(6),'Position');
% PP1(1) = PP1(1)+step;
% set(ll(6),'Position',PP1)
% PP1 = get(ll(9),'Position');
% PP1(1) = PP1(1)+2*step;
% set(ll(9),'Position',PP1)
% PP1 = get(ll(1),'xData');
% set(ll(1),'xData',PP1+step1)
% PP1 = get(ll(4),'xData');
% set(ll(4),'xData',PP1+step1+step)
% PP1 = get(ll(7),'xData');
% set(ll(7),'xData',PP1+step1+2*step)
step = 0.12; step1 = 0.08;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
set(ll(4),'MarkerSize',10)
PP = get(ll(6),'Position');
PP(1) = PP(1)+step;
set(ll(6),'Position',PP)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(ll(4),'xData');
set(ll(4),'xData',PP+step1+step)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Sz';


function plotD_all_data(source,eventdata,hbStatusBar,hp_plot,hp1_plot,handles)
global data_plot FS FS1 LW hbDataBar

if length(handles.data_user.dataP) == 0
	errordlg('Read the data!!!',...
			 'Read the data!!!','modal')
    return
end

set(hbStatusBar,'String','Plotting the data...');
pause(2)

% use the default lists for plotting
%-----------------------------------

%aquire list of plots and number of plots for location data
ind = get(handles.locList,'UserData');
data_plot.loc_length = length(ind);


list_loc = get(handles.locList, 'String');
list_loc = strrep(list_loc(ind), '*', '');
data_plot.list_loc = list_loc;

%get the list contents desired indices
plot_list = get(handles.cartList, 'String');
polar_list = get(handles.polarList, 'String');
i1 = get(handles.cartList, 'UserData');
i2 = get(handles.polarList, 'UserData');

%remove the asterix used to mark a selected plot
plot_list(i1) = strrep(plot_list(i1), '*', ''); 
polar_list(i2) = strrep(polar_list(i2), '*', '');

data_plot.list_plot = plot_list(i1); %must use regular paren instead of curly here
data_plot.list_polar = polar_list(i2);
data_plot.list_global = data_plot.default.list_global;
data_plot.list_global_Sz = data_plot.default.list_global_Sz;

data_plot.plot_Factor = data_plot.default.plot_Factor ;
data_plot.polar_Factor = data_plot.default.polar_Factor;
data_plot.polar_plot_type = data_plot.default.polar_plot_type;
data_plot.global_Factor = data_plot.default.global_Factor;
data_plot.global_Factor_Sz = data_plot.default.global_Factor_Sz;

h.list_plot = data_plot.list_plot;
h.list_polar = data_plot.list_polar;
h.list_loc = data_plot.list_loc;
h.list_global = data_plot.default.list_global;
h.list_global_Sz = data_plot.default.list_global_Sz;

h.plot_Factor = data_plot.default.plot_Factor;
h.polar_Factor = data_plot.default.polar_Factor;
h.polar_plot_type = data_plot.default.polar_plot_type;
h.global_Factor = data_plot.default.global_Factor;
h.global_Factor_Sz = data_plot.default.global_Factor_Sz;
h.global_Factor_loc = data_plot.default.global_Factor_loc;

% Read out the solar wind structure
%----------------------------------
if (handles.data_user.vswflag == 1 && ~isempty(data_plot.Omni_VSWStruct))
    switch data_plot.Omni_VSWStruct(1)
        case 1
            datastr = '1, unclear';
        case 2
            datastr = '2, transient';
        case 3 
            datastr = '3, HSS (fast)';
        case 4
            datastr = '4, slow';
    end
    set(hbDataBar, 'String',strcat('Vsw structure flag=',datastr));
end

% plot the data
%--------------
plot_data(hp_plot,hp1_plot,handles,h)

set(hbStatusBar,'String','Done plotting the data');
pause(2)


function strVar = plot_SzM(Poy,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
SzM = Poy(:,2); 
plot(t(1),SzM(1),'.k','MarkerSize',4); hold on
plot(t,SzM,'-k','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('SzM  (mW/m^2)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.15;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'SzM';


function strVar = plot_Ratio(Ra,t,LW,FS1,str0,bX)
len = length(t); inc = len/6;
Ratio = Ra(:,1); Sz_ind = Ra(:,2);
k = find(Sz_ind==3); Ratio1 = nan(size(Ratio)); Ratio1(k) = Ratio(k);
semilogy(t(1),abs(Ratio(1)),'.r','MarkerSize',2); hold on
semilogy(t(1),abs(Ratio1(1)),'.k','MarkerSize',2);
semilogy(t,abs(Ratio),'-r','LineWidth',LW);
semilogy(t,abs(Ratio1),'-k','LineWidth',LW); hold off
set(gca,'xLim',[0 len],'xTick',[0:inc:len],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('EFlx/Sz (1 comp.)','EFlx/Sz (2 comp.)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step = 0.12; step1 = 0.08;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
set(ll(4),'MarkerSize',10)
PP = get(ll(6),'Position');
PP(1) = PP(1)+step;
set(ll(6),'Position',PP)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(ll(4),'xData');
set(ll(4),'xData',PP+step1+step)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Ratio';

%OMNI plots

function strVar = plot_hour(OMNI_H,t1,LW,FS1,str0,bX) 
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_H(1),'.k','MarkerSize',2); hold on
plot(t1,OMNI_H,'-ok','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('Timestamp(hrs)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.16;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'hour'; 

function strVar = plot_IEF(OMNI_IEF,t1,LW,FS1,str0,bX)  
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_IEF(1),'.k','MarkerSize',2); hold on
plot(t1,OMNI_IEF,'-ko','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('IEF  (mV/m)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.17;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'IEF'; 


function strVar = plot_IMF_Bx(OMNI_Bx,t1,LW,FS1,str0,bX) 
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_Bx(1),'.b','MarkerSize',2); hold on
plot(t1,OMNI_Bx,'-bo','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('IMF-Bx  (nT)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.16;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'IMF-Bx'; 


function strVar = plot_IMF_By(OMNI_By,t1,LW,FS1,str0,bX) 
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_By(1),'.r','MarkerSize',2); hold on
plot(t1,OMNI_By,'-ro','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('IMF-By  (nT)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.16;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'IMF-By'; 


function strVar = plot_IMF_Bz(OMNI_Bz,t1,LW,FS1,str0,bX) 
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_Bz(1),'.ko','MarkerSize',2); hold on
plot(t1,OMNI_Bz,'-k','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('IMF-Bz  (nT)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.16;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'IMF-Bz'; 


function strVar = plot_IMF_B(OMNI_B,t1,LW,FS1,str0,bX) 
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_B(1),'.k','MarkerSize',2); hold on
plot(t1,OMNI_B,'-ko','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('IMF-B  (nT)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.16;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'IMF-B'; 


function strVar = plot_IMF_dB(OMNI_dB,t1,LW,FS1,str0,bX) 
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_dB(1),'.k','MarkerSize',2); hold on
plot(t1,OMNI_dB,'-ko','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('IMF-dB  (nT)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.16;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'IMF-dB'; 


function strVar = plot_IMF_Bxyz(B_OMNI,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
OMNI_Bx = B_OMNI(:,1); OMNI_By = B_OMNI(:,2); OMNI_Bz = B_OMNI(:,3); 
plot(t1(1),OMNI_Bx(1),'.b','MarkerSize',2); hold on
plot(t1(1),OMNI_By(1),'.r','MarkerSize',2);
plot(t1(1),OMNI_Bz(1),'.k','MarkerSize',2);
plot(t1,OMNI_Bx,'-bo','LineWidth',LW); 
plot(t1,OMNI_By,'-ro','LineWidth',LW);
plot(t1,OMNI_Bz,'-ko','LineWidth',LW);
 hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('IMF-Bx','IMF-By','IMF-Bz  (nT)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step = 0.12; step1 = 0.07;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
set(ll(4),'MarkerSize',10)
set(ll(7),'MarkerSize',10)
PP = get(ll(6),'Position');
PP(1) = PP(1)+step;
set(ll(6),'Position',PP)
PP = get(ll(9),'Position');
PP(1) = PP(1)+2*step;
set(ll(9),'Position',PP)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(ll(4),'xData');
set(ll(4),'xData',PP+step1+step)
PP = get(ll(7),'xData');
set(ll(7),'xData',PP+step1+2*step)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'IMF-Bxyz';


function strVar = plot_atan(B_OMNI,t1,LW,FS1,str0)
len1 = length(t1); inc1 = len1/6;
By = B_OMNI(:,1); Bz = B_OMNI(:,2); clock_angle = atan2(Bz,By);
k = find(clock_angle<0); clock_angle(k) = clock_angle(k)+2*pi;
plot(t1(1),clock_angle(1),'.k','MarkerSize',2); hold on 
plot(t1,clock_angle,'-k','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0,'fontname','Helvetica')
set(gca,'yLim',[0 2*pi],'yTick',[0 pi 2*pi],'yTickLabel',{'0','p','2p'},'fontname','symbol')
grid on; box off
lh = legend('atan(IMF-Bz/IMF-By)  (rad.)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal','fontname','Helvetica');
step1 = 0.11;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'atan(Bz/By)'; 


function strVar = plot_Vx_sw(OMNI_Vx,t1,LW,FS1,str0,bX) 
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_Vx(1),'.b','MarkerSize',2); hold on
plot(t1,OMNI_Vx,'-bo','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('Vx(sw)  (km/s)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.16;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Vx_sw'; 


function strVar = plot_Vy_sw(OMNI_Vy,t1,LW,FS1,str0,bX) 
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_Vy(1),'.r','MarkerSize',2); hold on
plot(t1,OMNI_Vy,'-ro','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('Vy(sw)  (km/s)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.16;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Vy_sw'; 


function strVar = plot_Vz_sw(OMNI_Vz,t1,LW,FS1,str0,bX) 
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_Vz(1),'.k','MarkerSize',2); hold on
plot(t1,OMNI_Vz,'-ko','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('Vz(sw)  (km/s)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.16;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Vz_sw'; 


function strVar = plot_V_sw(OMNI_V,t1,LW,FS1,str0,bX) 
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_V(1),'.k','MarkerSize',2); hold on
plot(t1,OMNI_V,'-ko','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('V(sw)  (km/s)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.16;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'V_sw'; 


function strVar = plot_Vxyz_sw(V_OMNI,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
OMNI_Vsw = V_OMNI(:,1)/10;
plot(t1(1),OMNI_Vsw(1),'.b','MarkerSize',2); hold on

plot(t1,OMNI_Vsw,'-bo','LineWidth',LW); 

set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('Vsw)/10','(km/s)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step = 0.12; step1 = 0.07;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(6),'Position');
PP(1) = PP(1)+step;
set(ll(6),'Position',PP)
PP = get(ll(9),'Position');
PP(1) = PP(1)+2*step;
set(ll(9),'Position',PP)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(ll(4),'xData');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Vxyz_sw';


function strVar = plot_Np(OMNI_Np,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_Np(1),'.r','MarkerSize',2); hold on
plot(t1,OMNI_Np,'-ro','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('Np  (1/cm^3)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.17;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Np';


function strVar = plot_Tp(OMNI_Tp,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_Tp(1),'.k','MarkerSize',2); hold on
plot(t1,OMNI_Tp,'-ko','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('Tp  (K)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.22;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.022;
set(lh,'Position',PP)
strVar = 'Tp';


function strVar = plot_Psw(OMNI_Psw,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_Psw(1),'.k','MarkerSize',2); hold on
plot(t1,OMNI_Psw,'-ko','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX) 
grid on; box off
lh = legend('Psw  (nPa)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.18;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.022;
set(lh,'Position',PP)
strVar = 'Psw';


function strVar = plot_AE(OMNI_AE,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
OMNI_AE = OMNI_AE(:,1);
plot(t1(1),OMNI_AE(1),'.k','MarkerSize',2); hold on 
plot(t1,OMNI_AE,'-ko','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('AE  (nT)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.22;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'AE';

function strVar = plot_Kp(OMNI_Kp,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
OMNI_Kp = OMNI_Kp(:,1);
plot(t1(1),OMNI_Kp(1),'.k','MarkerSize',2); hold on 
plot(t1,OMNI_Kp,'-ro','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('Kp');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.22;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Kp';

function strVar = plot_Kan_Lee(OMNI_KL,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_KL(1),'.b','MarkerSize',2); hold on 
plot(t1,OMNI_KL,'-bo','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('K-L E-field  (mV/m)');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.22;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Kan_Lee';

function strVar = plot_Hp(OMNI_HP,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
OMNI_HPN = OMNI_HP(:,1);
OMNI_HPS = OMNI_HP(:,2);
plot(t1(1),OMNI_HPN(1),'.b','MarkerSize',2); hold on 
plot(t1,OMNI_HPN,'-bo','LineWidth',LW,'MarkerSize',5); 
plot(t1(1),OMNI_HPS(1),'.r','MarkerSize',2); 
plot(t1,OMNI_HPS,'-ro','LineWidth',LW,'MarkerSize',5); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('','Mean Hp(N)','','Mean Hp(S)' );
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
%{
step = 0.2; step1 = 0.14;
ll = get(lh,'Children');
set(ll(2),'MarkerSize',4)
set(ll(5),'MarkerSize',4)
PP = get(ll(6),'Position');
PP(2) = PP(2)+step;
set(ll(6),'Position',PP)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(ll(5),'xData');
set(ll(5),'xData',PP+step1+step)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
%}
strVar = 'Hp';

function strVar = plot_Hp_ion(OMNI_HP,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
OMNI_HPN = OMNI_HP(:,1);
OMNI_HPS = OMNI_HP(:,2);
plot(t1(1),OMNI_HPN(1),'.b','MarkerSize',2); hold on 
plot(t1,OMNI_HPN,'-bo','LineWidth',LW); 
plot(t1(1),OMNI_HPS(1),'.r','MarkerSize',2); 
plot(t1,OMNI_HPS,'-ro','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('Mean ion Hp (northern)' , 'Mean ion Hp (southern)' );
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step = 0.2; step1 = 0.14;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
set(ll(4),'MarkerSize',10)
PP = get(ll(6),'Position');
PP(1) = PP(1)+step;
set(ll(6),'Position',PP)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(ll(4),'xData');
set(ll(4),'xData',PP+step1+step)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Hp';

function strVar = plot_Pe(OMNI_PE,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_PE(1),'.b','MarkerSize',2); hold on 
plot(t1,OMNI_PE,'-bo','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('Mean Pe');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.22;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Pe';

function strVar = plot_Pi(OMNI_PI,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_PI(1),'.b','MarkerSize',2); hold on 
plot(t1,OMNI_PI,'-bo','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('Mean Pi');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.22;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'Pi';

function strVar = plot_AL(OMNI_AL,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_AL(1),'.b','MarkerSize',2); hold on 
plot(t1,OMNI_AL,'-bo','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('AL');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.22;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'AL';

function strVar = plot_AU(OMNI_AU,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_AU(1),'.b','MarkerSize',2); hold on 
plot(t1,OMNI_AU,'-bo','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('Mean AU');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.22;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'AU';

function strVar = plot_DST(OMNI_DST,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_DST(1),'.b','MarkerSize',2); hold on 
plot(t1,OMNI_DST,'-bo','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('DST');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.22;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'DST';

function strVar = plot_eqb(OMNI_EQB,t1,LW,FS1,str0,bX)
len1 = length(t1); inc1 = len1/6;
plot(t1(1),OMNI_EQB(1),'.b','MarkerSize',2); hold on 
plot(t1,OMNI_EQB,'-bo','LineWidth',LW); hold off
set(gca,'xLim',[0 len1],'xTick',[0:inc1:len1],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('Mean Eqb');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step1 = 0.22;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'AE';

function strVar = plot_EV(EV,t2,LW,FS1,str0,bX)
len2 = length(t2); inc2 = len2/6;
EV6 = EV(:,1); EV10 = EV(:,2);
plot(t2(1),EV6(1),'.k','MarkerSize',2); hold on 
plot(t2(1),EV10(1),'.r','MarkerSize',2);
plot(t2,EV6,'-k','LineWidth',LW);
plot(t2,EV10,'-r','LineWidth',LW); hold off
set(gca,'xLim',[0 len2],'xTick',[0:inc2:len2],'xTickLabel',str0)
set(gca,'yLim',[min(bX) max(bX)],'yTick',bX)
grid on; box off
lh = legend('EV6','EV10');
set(lh,'FontSize',FS1,'Box','off','Orientation','horizontal');
step = 0.2; step1 = 0.14;
ll = get(lh,'Children');
set(ll(1),'MarkerSize',10)
set(ll(4),'MarkerSize',10)
PP = get(ll(6),'Position');
PP(1) = PP(1)+step;
set(ll(6),'Position',PP)
PP = get(ll(1),'xData');
set(ll(1),'xData',PP+step1)
PP = get(ll(4),'xData');
set(ll(4),'xData',PP+step1+step)
PP = get(lh,'Position');
PP(2) = PP(2)+0.024;
set(lh,'Position',PP)
strVar = 'EV';

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

close all 
clear
closereq

%Function which checks if a particular date number represents the last day
%of a month
function yesno = lastDayCheck(year, daynum)
if ((mod(year,4) == 0)) %leap year
    switch daynum 
        case {31,60,91,121,152,182,213,244,274,305,335,366} 
            yesno = 1;
        otherwise
            yesno = 0;
    end
else %non-leap year
    switch daynum 
        case{31,59,90,120,151,181,212,243,273,304,334,365}
            yesno = 1;
        otherwise
            yesno = 0;
    end
end

%Function which run IDL program cenergy.pro and outputs the name of the
%ascii file of all of the SSJ data for a particular day
function fileStr = createCenergyText(dateVec,sat)
global options
%options members: 
%omni_daily_path ssj_daily_path omni_source_path cenergy_path script_path 
%temp_path ssj_binary_path

    year = dateVec(1); yearS = int2str(year);
    month = dateVec(2); monthS = sprintf('%2.2d',month);
    day = dateVec(3); dayS = sprintf('%2.2d',day);
    daynumber = dayno([year,month,day,0,0,0]);
    satS = int2str(sat);
    
    %Directories
    temp_path = options.temp_path;
    ssj_daily_path = options.ssj_daily_path;
    script_path = options.script_path;
    log_path = options.log_path;
    
    %Files
    
    fileStr = strcat('cenergy_all_f',satS,'_',yearS,monthS,dayS,'.txt');
    logStr = strcat(log_path,'cenergy_f',satS,'_',yearS,monthS,dayS,'_full.log');
    
    lastdayswitch=lastDayCheck(year, daynumber);
    
    cmd = sprintf('%sdocenergy.sh %i %i %i %i %i %i config.txt',script_path, ...
        year, month, day, daynumber, sat, lastdayswitch);
    
    disp(cmd);
    exitstat = system(cmd)
    if (exitstat ~= 0)
        outstr = strcat('shell script exited with non-zero status check',logStr);
        disp(outstr)
    end
       
% --- Simple functions used to convert between date and array index
function out = calculateIndex(startDate, endDate, firstDate, stepPerD)
%lmk- startDate and endDate must have format [YYYY,MM,DD, 0, 0, 0] (standard matlab
%vector date). This only works with entire days. Returns
%[Startindex,endindex]. firstDate is earliest date in database.
%All use vector date format except stepPerD (steps per day) which must be integer.

sY = startDate(1);
eY = endDate(1);
fY = firstDate(1);

startD = dayno(startDate); endD = dayno(endDate); firstD = dayno(firstDate);

startI = 1; %start from firstDate at index 1
endI = stepPerD;

corr = 0; %leap year correction


    for y = fY:sY  
        if ((mod(y,4) == 0)), corr = 1; else corr = 0; end
        
        if (fY == sY)
           startI = startI + (startD - firstD)*stepPerD;
        elseif (y == sY)
           startI = startI + startD*stepPerD;          
        elseif (y == fY)
           startI = startI + ((365+corr)-firstD)*stepPerD;
        else
           startI = startI + (365+corr)*stepPerD;
        end
    end
    
    for y = fY:eY  
        if ((mod(y,4) == 0)), corr = 1; else corr = 0; end
        
        if (eY == fY)
            endI = endI + (endD - firstD)*stepPerD;
        elseif (y == eY)
            endI = endI + endD*stepPerD;          
        elseif (y == fY)
            endI = endI + ((365+corr)-firstD)*stepPerD;
        else
           endI = endI + (365+corr)*stepPerD;
        end
    end
    
    out = [startI, endI];
    
function out = dayno(datevec)
%lmk - Expects date vector format [YYYY MM DD 0 0 0]
dateNum = datenum(datevec)-datenum([datevec(1) 1 1 0 0 0]) + 1;
out = dateNum;

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

global options
%options members: 
%omni_daily_path ssj_daily_path omni_source_path cenergy_path script_path 
%temp_path ssj_binary_path idm_source_path

read_Options('./DMSP_Plot_Options.txt');

%Reading multiple days at a time is partially broken right now, mainly because cenergy.pro can 
%only deal with one day at a time. To fix add loops around cenergy.pro and OMNI reads -lmk


%OMNI solar wind data path
idm_source_path = options.idm_source_path;
omni_source_path = options.omni_source_path;
%cenergy.pro output file location
ssj_daily_path = options.ssj_daily_path;
%OMNI 'Quick-Load' data path (this is a .mat file generated for a single
%day using OMNILoad utility)
omni_daily_path = options.omni_daily_path;

%flag to indicate to refreshMenus which data are present
idm_flag = 0; ssj_flag = 0; omni_flag = 0; poy_flag = 0; Sz_flag = 0;

warning off
a = get(handles.YY_EditText1,'String');  varStart(1) = str2num(a);
a = get(handles.MM_EditText1,'String');  varStart(2) = str2num(a);
a = get(handles.DD_EditText1,'String');  varStart(3) = str2num(a);

a = get(handles.YY_EditText2,'String');  varEnd(1) = str2num(a);
a = get(handles.MM_EditText2,'String');  varEnd(2) = str2num(a);
a = get(handles.DD_EditText2,'String');  varEnd(3) = str2num(a);



vecDateStart = [varStart(1) varStart(2) varStart(3) 0 0 0]; 
vecDateEnd = [varEnd(1) varEnd(2) varEnd(3) 0 0 0];


strDate = datestr(vecDateStart);
refDate = datestr([varStart(1) 1 1 0 0 0]);
numDate_Start = datenum(strDate)-datenum(refDate)+1;
strDate = datestr(vecDateEnd);
refDate = datestr([varEnd(1) 1 1 0 0 0]);
numDate_End = datenum(strDate)-datenum(refDate)+1;
startDay = numDate_Start;
handles.data_user.startDay = startDay;

SatNum = get(handles.DMSP_EditText,'String');
strSat = sprintf('DMSP: F%s',SatNum);
sat = str2num(SatNum);
strTime = sprintf('Generated on  %s',datestr(clock));

if numDate_Start > numDate_End
	errordlg('Check the time selection',...
			 'Incorrect Selection','modal')
    return
end
%lmk- Appears to aquire an entire day(s) data regardless of your time
%interval 


%read the IDM 4s data from text files
%-------------------------------------
   
firstDateIDM = vecDateStart;
index = calculateIndex(vecDateStart, vecDateEnd, firstDateIDM, 24*60*60);
i_Start = index(1);
i_End = index(2);

year = vecDateStart(1); yearS = int2str(year);
month = vecDateStart(2); monthS = sprintf('%2.2d',month);
day = vecDateStart(3); dayS = sprintf('%2.2d',day);
    
if (year <= 2002)
   
    year2S = yearS(3:4);
    daynum = dayno(vecDateStart); daynumS = sprintf('%3.3d',daynum);
    satS = sprintf('%2.2d',sat);

    idm_file = strcat(yearS,'/',year2S,monthS,'/l',satS,year2S,daynumS,'a.txt');

    set(handles.Msg_EditText,'String','Reading IDM file...');
    
    
    
    data = read_idm(strcat(idm_source_path,idm_file));

    handles.data_user.data_idm = data(i_Start:i_End,:);

    idm_flag = 1;
    
    clear data;
    
else
    data = NaN(86400,13);
    handles.data_user.data_idm = data(i_Start:i_End,:);
    set(handles.Msg_EditText,'String','IDM data not present for this interval...');
    pause(2);
    clear data;

end

% read the OMNI data
%-------------------
firstDate = [1972 1 1 0 0 0]; %first date in ONMI data 
set(handles.Msg_EditText,'String','Reading the OMNI data...');
pause(2)
quickLoad = get(handles.qlCB,'Value'); %Consider changing to a handles reference

% calculate the start and end indices for 1 hr OMNI data
index = calculateIndex(vecDateStart, vecDateEnd, firstDate, 24);
i_Start = index(1);
i_End = index(2);

OMNI_SL_File = strcat('omni_1hr_',yearS,monthS,dayS,'.mat');

if (quickLoad == 1)
load(strcat(omni_daily_path,OMNI_SL_File));
handles.data_user.data_OMNI = data(i_Start:i_End,:);
else
data = read_Omni(omni_source_path); %load OMNI_hourly_1972_2010 (111 variables)
handles.data_user.data_OMNI = nan(i_End-i_Start+1,111);
handles.data_user.data_OMNI = data(i_Start:i_End,:);
end

% bae: write out first time for OMNI solar wind data (12 variables)
data(1,:)
omni_flag = 1;
clear data



% read the EV values
%-------------------
if ( (sat == 15) && (2004 <= year <= 2005) )
    
    set(handles.Msg_EditText,'String','Reading the EV values...');
    pause(2)

    if varStart(1) < 2005
        i_Start = (numDate_Start-1)*24*15+1;
    else
        i_Start = 24*15*366+(numDate_Start-1)*24*15+1;
    end

    if varEnd(1) < 2005
        i_End = numDate_End*24*15;
    else
        i_End = 24*15*366+numDate_End*24*15;
    end

    %load EV_2004_2005
    load /home/anghel/NADIR/EV_2004_2005.mat
    handles.data_user.data_EV = nan((numDate_End-numDate_Start+1)*24*15,2);
    handles.data_user.data_EV = data(i_Start:i_End,:);
else
    handles.data_user.data_EV = nan((numDate_End-numDate_Start+1)*24*15,2);
    set(handles.Msg_EditText,'String','No EV data available for this sat/time...');
    pause(2);

end
% read the DMSP SSJ and Ephemeris data (cenergy.pro output)
%----------------------------------------------------------

firstDateSSJ = vecDateStart;

%Calculate the indices that correspond to the time frame 
index = calculateIndex(vecDateStart, vecDateEnd, firstDateSSJ, 24*60*60);
i_Start = index(1);
i_End = index(2);

quickLoad = get(handles.qlCB2,'Value');

if(~quickLoad) %Run cenergy.pro and generate the ASCII file
    set(handles.Msg_EditText,'String','Running cenergy.pro...');
    pause(2)

    SSJ_File = createCenergyText([varStart(1),varStart(2),varStart(3),0,0,0], str2num(SatNum));

    set(handles.Msg_EditText,'String','Reading DMSP ASCII file...');
    pause(2)
    
    data = read_Cenergy(strcat(ssj_daily_path,SSJ_File));
else %Assume the ASCII file already exists and read from that
    
    year = varStart(1); yearS = int2str(year);
    month = varStart(2); monthS = sprintf('%2.2d',month);
    day = varStart(3); dayS = sprintf('%2.2d',day);
    
    SSJ_File = strcat('cenergy_all_f',SatNum,'_',yearS,monthS,dayS,'.txt');
    disp(SSJ_File);
    data = read_Cenergy(strcat(ssj_daily_path, SSJ_File));
end

handles.data_user.data_ssj = data(i_Start:i_End,:);

ssj_flag = 1;

clear data;

% m = 14; n = 20;
% data_egeo = nan((numDate_End-numDate_Start+1)*60*60*24,m);
% for i = numDate_Start:numDate_End    
%     k = i-numDate_Start;
% %   filename = ['EGEO/egeo_F',SatNum,'_',num2str(varEnd(1)),'_',num2str(i),'.dat'];    
%     filename = ['/home/anghel/NADIR/EGEO/egeo_F',SatNum,'_',num2str(varEnd(1)),'_',num2str(i),'.dat'];
%     data_egeo(k*86400+1:(k+1)*86400,:) = read_file(filename,m,n);        
% end
% handles.data_user.data_ssj = data_egeo;
% clear data_egeo
% 
% set(handles.Msg_EditText,'String','Reading the IEFlx data...');
% pause(2)
% 
% m = 26; n = 14;
% data_ieflx = nan((numDate_End-numDate_Start+1)*60*60*24,m);
% for i = numDate_Start:numDate_End    
%     k = i-numDate_Start;
% %   filename = ['IEFLX/ieflx_F',SatNum,'_',num2str(varEnd(1)),'_',num2str(i),'.dat'];    
%     filename = ['/home/anghel/NADIR/IEFLX/ieflx_F',SatNum,'_',num2str(varEnd(1)),'_',num2str(i),'.dat'];
%     data_ieflx(k*86400+1:(k+1)*86400,:) = read_file(filename,m,n);
% end
% handles.data_user.data_ieflx = data_ieflx;
% clear data_ieflx
% 
% set(handles.Msg_EditText,'String','Reading the EEFlx data...');
% pause(2)
% 
% m = 26; n = 14;
% data_eeflx = nan((numDate_End-numDate_Start+1)*60*60*24,m);
% for i = numDate_Start:numDate_End    
%     k = i-numDate_Start;
% %   filename = ['EEFLX/eeflx_F',SatNum,'_',num2str(varEnd(1)),'_',num2str(i),'.dat'];
%     filename = ['/home/anghel/NADIR/EEFLX/eeflx_F',SatNum,'_',num2str(varEnd(1)),'_',num2str(i),'.dat'];    
%     data_eeflx(k*86400+1:(k+1)*86400,:) = read_file(filename,26,14);
% end
% handles.data_user.data_eeflx = data_eeflx;
% clear data_eeflx;

if ( (sat == 15) && (2004 <= year <= 2005) )
    
    % read the Poynting flux data
    %----------------------------
    set(handles.Msg_EditText,'String','Reading the Poynting flux data...');
    pause(2)
    poy_flag = 1;
    
    m = 37;
    dataP = nan((numDate_End-numDate_Start+1)*60*60*24,m);
    for i = numDate_Start:numDate_End        
        k = i-numDate_Start;
    %   filename = ['Poy/',num2str(varEnd(1)),'_',num2str(i),'_','Sz.txt'];
    %   filename = ['/home/anghel/NADIR/Poy/',num2str(varEnd(1)),'_',num2str(i),'_','Sz.txt'];
       filename = ['/net/aim/d/dmsp/dmsp_poynting/Sz_',num2str(varEnd(1)),'/',num2str(varEnd(1)),'_',num2str(i),'_','Sz.txt'];
    % bae: write out daynumber, year, and Poynting flux filename
    i
    varEnd(1)
    filename
        dataP(k*86400+1:(k+1)*86400,:) = read_Poynting(filename);
    end
    handles.data_user.dataP = dataP;
    clear dataP

    % read Sz model data
    %-------------------
    set(handles.Msg_EditText,'String','Reading the Sz model data ...');
    Sz_flag = 1;
    pause(2)

    m = 5;
    dataM = nan((numDate_End-numDate_Start+1)*60*60*24,m);
    for i = numDate_Start:numDate_End
        k = i-numDate_Start;    
    %   filename = ['Poy_Model/',num2str(varEnd(1)),'_',num2str(i),'_','SzM.txt'];
        filename = ['/home/anghel/NADIR/Poy_Model/',num2str(varEnd(1)),'_',num2str(i),'_','SzM.txt'];
        dataM(k*86400+1:60:(k+1)*86400,:) = read_Model(filename);
    end
    handles.data_user.dataM = dataM;
    clear dataM
else
    dataP = nan((numDate_End-numDate_Start+1)*60*60*24,37);
    handles.data_user.dataP = dataP;
    dataM = nan((numDate_End-numDate_Start+1)*60*60*24,5);
    handles.data_user.dataM = dataM;
    set(handles.Msg_EditText,'String','No Poynting/Sz data available for this sat/time...');
    clear dataM dataP;

end
    
%lmk - This block of code makes arrays that index the time window between
%numDate_Start and numDate_End in various ways. There is one array element
%per second in each array, so they all have the same length

k = numDate_End-numDate_Start;
d = numDate_Start:numDate_End;
doy = reshape(repmat(d,60*60*24,1),(k+1)*60*60*24,1)';
year = varStart(1)*ones(1,(k+1)*60*60*24);
vTime = [0:(k+1)*60*60*24-1];
vTime = mod(vTime,60*60*24);
hours = floor(vTime/3600);
mins = floor((vTime-3600*hours)/60);
secs = floor(vTime-3600*hours-60*mins);
vecTime(:,1) = hours';
vecTime(:,2) = mins';
vecTime(:,3) = secs';
vecTime(:,4) = doy';
vecTime(:,5) = year';
handles.data_user.vecTime = vecTime;
clear hours mins secs doy year vecTime

set(handles.Msg_EditText,'String','Refreshing menus based on data availability ...');
refreshMenus(handles,idm_flag,ssj_flag,omni_flag,poy_flag,Sz_flag);
pause(2)

set(handles.Msg_EditText,'String','Done reading the data');
pause(2)

guidata(hObject, handles); %updates the handles

% --- Executes on button press in generateButton.
function generateButton_Callback(hObject, eventdata, handles)
% hObject    handle to generateButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global options
%options members: 
%omni_daily_path ssj_daily_path omni_source_path cenergy_path script_path 
%temp_path ssj_binary_path

read_Options('DMSP_Plot_Options.txt');

%OMNI solar wind data path
omni_source_path = options.omni_source_path;
omni_daily_path = options.omni_daily_path;

%this callback shares a lot of code with the Read button callback

a = get(handles.YY_EditText1,'String');  varStart(1) = str2num(a);
a = get(handles.MM_EditText1,'String');  varStart(2) = str2num(a);
a = get(handles.DD_EditText1,'String');  varStart(3) = str2num(a);

a = get(handles.YY_EditText2,'String');  varEnd(1) = str2num(a);
a = get(handles.MM_EditText2,'String');  varEnd(2) = str2num(a);
a = get(handles.DD_EditText2,'String');  varEnd(3) = str2num(a);



vecDateStart = [varStart(1) varStart(2) varStart(3) 0 0 0]; 
vecDateEnd = [varEnd(1) varEnd(2) varEnd(3) 0 0 0];


strDate = datestr(vecDateStart);
refDate = datestr([varStart(1) 1 1 0 0 0]);
numDate_Start = datenum(strDate)-datenum(refDate)+1;
strDate = datestr(vecDateEnd);
refDate = datestr([varEnd(1) 1 1 0 0 0]);
numDate_End = datenum(strDate)-datenum(refDate)+1;
startDay = numDate_Start;
handles.data_user.startDay = startDay;

SatNum = get(handles.DMSP_EditText,'String');

if numDate_Start > numDate_End
	errordlg('Check the time selection',...
			 'Incorrect Selection','modal')
    return
end

% generate daily OMNI data
%-------------------------
firstDate = [1972 1 1 0 0 0]; %first date in ONMI data 
set(handles.Msg_EditText,'String','Reading the OMNI data...Generating files');
pause(2)

% calculate the start and end indices for 1 hr OMNI data

rawdata = read_Omni(omni_source_path); %load OMNI_hourly_1972_2010 (111 variables)
[m,n] = size(rawdata);


for i = datenum(vecDateStart):datenum(vecDateEnd)  
thisDay = datevec(i); 

index = calculateIndex(thisDay, thisDay, firstDate, 24);
i_Start = index(1);
i_End = index(2);

data = zeros(m,n);

data(i_Start:i_End,:) = rawdata(i_Start:i_End,:);

data = sparse(data);

year = thisDay(1); yearS = int2str(year);
month = thisDay(2); monthS = sprintf('%2.2d',month);
day = thisDay(3); dayS = sprintf('%2.2d',day);

omni_daily_file = strcat('omni_1hr_',yearS,monthS,dayS,'.mat');

save(strcat(omni_daily_path,omni_daily_file), 'data');
msg=strcat('File generated: ',omni_daily_file);
set(handles.Msg_EditText,'String',msg);
pause(2)
end

% bae: write out first time for OMNI solar wind data (12 variables)
% data(1,:)

clear data

% generate the DMSP SSJ and Ephemeris data (cenergy.pro output)
%----------------------------------------------------------

%Run cenergy.pro and generate the ASCII file(s)
    set(handles.Msg_EditText,'String','Running cenergy.pro...');
    pause(2)
for i = datenum(vecDateStart):datenum(vecDateEnd)  
    thisDay = datevec(i);    
    year = thisDay(1);
    month = thisDay(2); 
    day = thisDay(3); 
    SSJ_File = createCenergyText([year, month, day, 0, 0, 0], str2num(SatNum));
    msg=strcat('File generated: ',SSJ_File);
    set(handles.Msg_EditText,'String',msg);
    pause(2)
end

set(handles.Msg_EditText,'String','Done generating files!');
clear data;

 
function data = read_Model(filename)
data = NaN(60*24,5);
fid = fopen(filename,'rt'); 
if (fid ~= -1)
    tline = fgets(fid);
    tline = fgets(fid);
    while ~feof(fid)    
        tline = fgets(fid);
        a = sscanf(tline,'%f');
        H = a(3); M = a(4);
        k = H*60+M+1;
        data(k,1) = a(6);  % dBphi
        data(k,2) = a(7);  % dBthe
        data(k,3) = a(8);  % Ephi
        data(k,4) = a(9);  % Ethe     
        data(k,5) = a(10); % Sz       
    end    
    fclose(fid);
end      


function data = read_Poynting(filename)
data = NaN(86400,37);
fid = fopen(filename,'rt');
if (fid ~= -1)
    for i = 1:40
        tline = fgets(fid); 
    end        
    tline = fgets(fid);            
    j = 1;
    while ~feof(fid)
        tline = fgets(fid);
        a = sscanf(tline,'%f');
        data(j,:) = a;
        j = j+1;
    end    
    fclose(fid);
end

function data = read_idm(filename)
data = NaN(86400,13);
fid = fopen(filename,'rt');
if (fid ~= -1)
        tline = fgets(fid); %clear 1 line header
        i=1;
        last=1;
        while ~feof(fid)
            tline = fgets(fid);
            a = sscanf(tline,'%f'); 
            if (length(a) == 13)
                if (last ~= a(1)-4) %missing data check; a(1) is time column
                    warn = sprintf('idm output file inconsistant between UT secs: %i and %i', last,a(1));
                    disp(warn);
                    last = a(1);
                    i=a(1); 
                    
                else
                    last = a(1);
                    i=a(1);
                    
                end
                data(i,:) = a; %ith row of data
            else
                disp(tline);
            end
                
                
        end
end
fclose(fid);

function data = read_file(filename,m,n)
%m is number of columns to output, n is number of lines to dump in
%bitbucket before beginning real read
data = NaN(86400,m);
fid = fopen(filename,'rt');
if (fid ~= -1)
    for i = 1:n
        tline = fgets(fid); 
    end                
    j = 1;
    while ~feof(fid)
        tline = fgets(fid);
        a = sscanf(tline,'%f');
        data(j,:) = a; %jth row of data
        j = j+1;
    end    
    fclose(fid);
end

function data = read_Cenergy(filename)
%there are 53300 lines in a single day of DMSP SSJ4 data output from
%cenergy.pro
data = NaN(86400,16);
fid = fopen(filename,'rt');
if (fid ~= -1)
        for i = 1:2
            tline = fgets(fid); 
        end          
        i = 1;
        j = 1;
        h = 2;
        while ~feof(fid)
            tline = fgets(fid);
            a = sscanf(tline,'%f');
            if (length(a) == 16) 
                if (i ~= (a(2)+1)) %missing data check; a(2) is time column
                    warn = sprintf('cenergy.pro output file is missing data beginning at line %d', j+h);
                    disp(warn);
                    i = a(2)+1;
                end
            data(i,:) = a; %jth row of data
            j = j+1;
            i = i+1;
            else 
            tline = fgets(fid); %clear header     
            h = h+1;
            end
        end
    fclose(fid);
end
return

function data = read_Omni(filename)
%Has 111 columns
%Data at:
%/hao/polar1/emery/aurora_i/xuw/combined_output/eln_tot_72_10_combnx7_hourl
%y
data = NaN(335000,111);
fid = fopen(filename,'rt');
p=0;
if (fid ~= -1)               
        j = 1;
        disp('OMNI read percent completed:');
        while ~feof(fid)
            tline = fgets(fid);
            a = sscanf(tline,'%f');
            data(j,:) = a; %jth row of data
            j = j+1;
            if (mod(j,3346*5) == 0)
                p=p+5;
                out = sprintf('%i percent...Line: %i',p,j);
                disp(out);
            end
            if ( p == 100 )
                fclose(fid);
                return
            end
        end
end
fclose(fid);

return

function data = read_Options(filename)
global options 
o = options;
%options members: 
%omni_daily_path ssj_daily_path omni_source_path cenergy_path script_path 
%temp_path ssj_binary_path log_path
   FID = fopen(filename,'rt');
   if (FID ~= -1)
   t = ~feof(FID);
      while t
        tline = fgetl(FID);
        %disp(tline)
        if (~isempty(tline))
            if ~strcmp(tline(1),'%')
                a = textscan(tline, '%s %s');
                param = char(a{1}); %disp(param);
                value = char(a{2}); %disp(value);
                switch param
                    case 'omni_source_path'
                    o.omni_source_path = value;
                    case 'omni_daily_path'
                    o.omni_daily_path = value;
                    case 'ssj_daily_path'
                    o.ssj_daily_path = value;
                    case 'ssj_binary_path'
                    o.ssj_binary_path = value;
                    case 'cenergy_path'
                    o.cenergy_path = value;
                    case 'script_path'
                    o.script_path = value;
                    case 'temp_path'
                    o.temp_path = value;
                    case 'log_path'
                    o.log_path = value;
                    case 'idm_source_path'
                    o.idm_source_path = value;
                    otherwise 
                    errordlg('Error: invalid parameter read from options file','I`m sorry Dave, I can`t do that');                                     
                end
            end
        end
      t = ~feof(FID);
      end
      fclose(FID);
    options = o;
   else
       errordlg('Error: options file not found', 'I`m sorry Dave, I can`t do that.');
   end
   return
   
    

function Msg_EditText_Callback(hObject, eventdata, handles)
% hObject    handle to Msg_EditText (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 Msg_EditText as text
%        str2double(get(hObject,'String')) returns contents of Msg_EditText as a double


% --- Executes during object creation, after setting all properties.
function Msg_EditText_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Msg_EditText (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 Save_pushbutton.
function Save_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to Save_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% global dataA dataC dataM dataP data_ieflx data_eeflx ...
%        data_OMNI indices_OMNI data_EV startDay 

a = get(handles.YY_EditText1,'String');  varStart(1) = str2num(a);
a = get(handles.MM_EditText1,'String');  varStart(2) = str2num(a);
a = get(handles.DD_EditText1,'String');  varStart(3) = str2num(a);
a = get(handles.HH_EditText1,'String');  varStart(4) = str2num(a);
a = get(handles.Min_EditText1,'String'); varStart(5) = str2num(a);
a = get(handles.SS_EditText1,'String');  varStart(6) = str2num(a);

start_time = datestr(varStart);
strDate = datestr([varStart(1) varStart(2) varStart(3) 0 0 0]);
refDate = datestr([varStart(1) 1 1 0 0 0]);
numDate_Start = datenum(strDate)-datenum(refDate)+1;
startDay = handles.data_user.startDay;
k = numDate_Start-startDay;
iStart = varStart(4)*3600+varStart(5)*60+varStart(6)+86400*k+1;
iStart1 = varStart(4)*60+varStart(5)+60*24*k+1; 
iStart2 = ceil(varStart(4)*15+varStart(5)/4+15*24*k+1); 

a = get(handles.YY_EditText2,'String');  varEnd(1) = str2num(a);
a = get(handles.MM_EditText2,'String');  varEnd(2) = str2num(a);
a = get(handles.DD_EditText2,'String');  varEnd(3) = str2num(a);
a = get(handles.HH_EditText2,'String');  varEnd(4) = str2num(a);
a = get(handles.Min_EditText2,'String'); varEnd(5) = str2num(a);
a = get(handles.SS_EditText2,'String');  varEnd(6) = str2num(a);

end_time = datestr(varEnd);
strDate = datestr([varEnd(1) varEnd(2) varEnd(3) 0 0 0]);
refDate = datestr([varEnd(1) 1 1 0 0 0]);
numDate_End = datenum(strDate)-datenum(refDate)+1;
k = numDate_End-startDay;
iEnd = varEnd(4)*3600+varEnd(5)*60+varEnd(6)+86400*k+1;
iEnd1 = varEnd(4)*60+varEnd(5)+60*24*k+1;
iEnd2 = floor(varEnd(4)*15+varEnd(5)/4+15*24*k+1);

set(handles.Msg_EditText,'String','Saving the selected parameters...');
pause(2)

% get the list of all the entries in the listbox
list_entries = get(handles.Select_listbox,'String');

% get the indices of the selected items
index_selected = get(handles.Select_listbox,'Value');
     
if length(index_selected) == 0
	errordlg('Select parameters',...
			 'Incorrect Selection','modal')
    return
end

SatNum = get(handles.DMSP_EditText,'String');
strMM  = get(handles.MM_EditText1,'String');
strDD  = get(handles.DD_EditText1,'String');
strYY  = get(handles.YY_EditText1,'String');       
strFilename = ['Output/DMSP_',SatNum,'_',num2str(numDate_End),'_',strYY,'.dat'];
strFilename_xls = ['Output/DMSP_',SatNum,'_',num2str(numDate_End),'_',strYY,'.xls'];

vecTime1 = handles.data_user.vecTime(iStart:iEnd,1:3);
kk = find(isnan(vecTime1)); vecTime1(kk) = 0;
data1P = [handles.data_user.dataP(iStart:iEnd,1:4) ...
          vecTime1 ...
          handles.data_user.dataP(iStart:iEnd,5:14) ...
          handles.data_user.dataP(iStart:iEnd,17) ...
          handles.data_user.dataP(iStart:iEnd,19) ...
          handles.data_user.dataP(iStart:iEnd,21) ...
          handles.data_user.dataP(iStart:iEnd,23:24)];

data1M = handles.data_user.dataM(iStart1:iEnd1,5);
data2M = nan(length(data1P),1);
data2M(1:60:60*length(data1M)) = data1M;

data1_ieflx = handles.data_user.data_ieflx(iStart:iEnd,26);
data1_eeflx = handles.data_user.data_eeflx(iStart:iEnd,26);

data1_OMNI = handles.data_user.data_OMNI(iStart1:iEnd1,:);
data2_OMNI = nan(length(data1P),12);
data2_OMNI(1:60:60*length(data1_OMNI),:) = data1_OMNI;

indices1_OMNI = handles.data_user.indices_OMNI(iStart1:iEnd1,:);
indices2_OMNI = nan(length(data1P),8);
indices2_OMNI(1:60:60*length(indices1_OMNI),:) = indices1_OMNI;

data1_EV = handles.data_user.data_EV(iStart2:iEnd2,:);
data2_EV = nan(length(data1P),2);
data2_EV(1:4*60:4*60*length(data1_EV),:) = data1_EV;

data1A = handles.data_user.dataA(iStart:iEnd,6:9);
data1C = handles.data_user.dataC(iStart:iEnd,7);

param = {'SAT' 'YEAR' 'DOY' 'XUT' 'UT(H)' 'UT(M)' 'UT(S)' 'MLAT' 'MLT' 'GLAT' 'GLONG' 'ALTIT' ...
         'Vx' 'Vy' 'Vz' 'dBx' 'dBy' 'Ex' 'Ey'  'Ez' 'Sz' ...
         'SzFlag' 'SzM' 'IEFlx_bgs' 'EEFlx_bgs' 'Nd' 'IMF-Bx' 'IMF-By' ...
         'IMF-Bz' 'IMF-dB' 'Vx_sw' 'Vy_sw' 'Vz_sw' 'Np' 'Tp' 'Psw' 'IEF'...
         'Beta' 'AE' 'AU' 'AL' 'SymD' 'SymH' 'AsyD' 'AsyH' 'PCN' 'EV6' 'EV10' ...
         'AMIE-Sz' 'AMIE-P' 'AMIE-Ex' 'AMIE-Ey'};
     
dataS = [data1P data2M data1_ieflx data1_eeflx data1C data2_OMNI ...
         indices2_OMNI data2_EV data1A];
k = find(abs(dataS(:,24)) < 10^(-30)); dataS(k,24) = NaN;
k = find(abs(dataS(:,25)) < 10^(-30)); dataS(k,25) = NaN;
k = find(abs(dataS(:,26)) < 10^(-30)); dataS(k,26) = NaN;
     
formatS = {'%2d' '%4d' '%3d' '%5d' '%02d' '%02d' '%02d' '%6.2f' '%7.4f' '%6.2f' '%6.2f' '%7.3f' ...
           '%10.2f' '%10.2f' '%10.2f' '%10.2f' '%10.2f' '%10.2f' '%10.2f' '%10.2f' '%10.2f' ...
           '%1d' '%10.2f' '%15.5e' '%15.5e' '%15.5e' '%10.2f' '%10.2f' ...
           '%10.2f' '%10.2f' '%10.2f' '%10.2f' '%10.2f' '%8.4f' '%10.2f' '%8.4f' '%10.4f' ...
           '%10.4f' '%6d' '%6d' '%6d' '%6d' '%6d' '%6d' '%6d' '%7.2f' '%7.2f' '%7.2f' ...
           '%10.5f' '%10.5f' '%15.5e' '%15.5e'};
    
if get(handles.Text_radiobutton,'Value')
    fid = fopen(strFilename,'wt');
    
    fprintf(fid,'DMSP F%d Calculated Poynting Flux and Ion/Electron energy flux\n',str2num(SatNum));
    fprintf(fid,'USAFA Dept. of Physics - Data processed on %s\n\n',datestr(clock));
    
    for i = 1:length(index_selected)
        fprintf(fid,'%s  (%s)\n',list_entries{index_selected(i)},formatS{index_selected(i)});
    end    
    fprintf(fid,'\n');
            
    for i = 1:length(index_selected)
        fprintf(fid,'%s\t',param{index_selected(i)});
    end   
    fprintf(fid,'\n');
    
    format_data = '';
    for i = 1:length(index_selected)
        format_data = [format_data,'   ',formatS{index_selected(i)}]; 
    end    
    format_data = [format_data,'\n'];    
    
    for i = 1:length(dataS)
        fprintf(fid,format_data,dataS(i,index_selected));
    end
    fclose(fid);
    
    % dlmwrite(strFilename,dataS(:,index_selected),'-append','delimiter','\t','newline','pc');
    
elseif get(handles.Excel_radiobutton,'Value')
    dataSS = cat(1,param(index_selected),num2cell(dataS(:,index_selected)));    
    
    if length(dataSS) > 65537
        errordlg('Data too large',...
        'Incorrect Selection','modal')
        return
    end
    
    xlswrite(strFilename_xls,dataSS);    
end 

guidata(hObject,handles)

set(handles.Msg_EditText,'String','Done saving the data');
pause(2)


% --- Executes on selection change in Select_listbox.
function Select_listbox_Callback(hObject, eventdata, handles)
% hObject    handle to Select_listbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns Select_listbox contents as cell array
%        contents{get(hObject,'Value')} returns selected item from Select_listbox


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

% Hint: listbox 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 Radio_uipanel_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to Radio_uipanel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


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


% --- Executes during object deletion, before destroying properties.
function Display_pushbutton_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to Display_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


function HH_EditText2_Callback(hObject, eventdata, handles)
% hObject    handle to HH_EditText2 (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 HH_EditText2 as text
%        str2double(get(hObject,'String')) returns contents of HH_EditText2 as a double


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

% get the time
%-------------
var_Time = str2num(get(handles.Slide_EditText,'String'));
NR_min = floor(var_Time/60);
NR_sec = var_Time - 60*NR_min;

% decrease start time
%--------------------
varTime(1) = str2num(get(handles.YY_EditText1,'String'));
varTime(2) = str2num(get(handles.MM_EditText1,'String')); 
varTime(3) = str2num(get(handles.DD_EditText1,'String'));  
varTime(4) = str2num(get(handles.HH_EditText1,'String'));
varTime(5) = str2num(get(handles.Min_EditText1,'String'));
varTime(6) = str2num(get(handles.SS_EditText1,'String'));

varTime_chg = decrease_time(varTime,NR_min,NR_sec);

set(handles.YY_EditText1,'String',num2str(varTime_chg(1)))
set(handles.MM_EditText1,'String',num2str(varTime_chg(2)))
set(handles.DD_EditText1,'String',num2str(varTime_chg(3)))
set(handles.HH_EditText1,'String',num2str(varTime_chg(4)))
set(handles.Min_EditText1,'String',num2str(varTime_chg(5)))
set(handles.SS_EditText1,'String',num2str(varTime_chg(6)))

% decrease end time
%------------------
varTime(1) = str2num(get(handles.YY_EditText2,'String'));
varTime(2) = str2num(get(handles.MM_EditText2,'String')); 
varTime(3) = str2num(get(handles.DD_EditText2,'String'));  
varTime(4) = str2num(get(handles.HH_EditText2,'String'));
varTime(5) = str2num(get(handles.Min_EditText2,'String'));
varTime(6) = str2num(get(handles.SS_EditText2,'String'));

varTime_chg = decrease_time(varTime,NR_min,NR_sec);

set(handles.YY_EditText2,'String',num2str(varTime_chg(1)))
set(handles.MM_EditText2,'String',num2str(varTime_chg(2)))
set(handles.DD_EditText2,'String',num2str(varTime_chg(3)))
set(handles.HH_EditText2,'String',num2str(varTime_chg(4)))
set(handles.Min_EditText2,'String',num2str(varTime_chg(5)))
set(handles.SS_EditText2,'String',num2str(varTime_chg(6)))

guidata(hObject, handles); %updates the handles


function varTime_chg = decrease_time(varTime,NR_min,NR_sec)
YY  =  varTime(1); MM   =  varTime(2); DD  =  varTime(3);
HH  =  varTime(4); MMin =  varTime(5); SS  =  varTime(6);

if SS >= NR_sec
    SS = SS - NR_sec;
    if MMin >= NR_min
        MMin = MMin - NR_min;
    else
        MMin = 60 + MMin - NR_min;
        if HH >= 1
            HH = HH - 1;
        else
            HH = 23;
            DD = DD - 1;
        end
    end
else
    SS = SS + 60 - NR_sec;
    if MMin >= NR_min + 1
        MMin = MMin - NR_min - 1;
    else
        MMin = MMin + 60 - NR_min - 1;
        if HH >= 1
            HH = HH - 1;
        else
            HH = 23;            
            DD = DD - 1;
        end
    end
end

varTime_chg(1) = YY; varTime_chg(2) = MM;   varTime_chg(3) = DD;
varTime_chg(4) = HH; varTime_chg(5) = MMin; varTime_chg(6) = SS;


%--------------------
% display polar plots
%--------------------
function strVar = display_polar_IEFlx(IEFlx,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX)
global FS FS1 LW NR MS1
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5];
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;  
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'Ion Energy Flux','FontSize',FS+1)         

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color;
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(62,33,'IEFlx','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);    
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(IEFlx));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = IEFlx(k)/6;
    
    % determine the color
    j = 1;
    while IEFlx(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1); 
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    
    if pChoice == 1 
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
                'Color',col);        
    elseif pChoice == 3
        plot([x1,x2],[y1,y2],'LineStyle','-','Color',col,'LineWidth',MS1);        
    end
end
if pChoice == 2    
    plot(xm,ym,'-b','LineWidth',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'IEFlx';


function strVar = display_polar_EEFlx(EEFlx,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX)
global FS FS1 LW NR MS1
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5];
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;  
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'Electon Energy Flux','FontSize',FS+1)         

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color;
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(62,33,'EEFlx','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(EEFlx));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = EEFlx(k)/6;
    
    % determine the color
    j = 1;
    while EEFlx(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1);
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    
    if pChoice == 1  
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
                 'Color',col);                
    elseif pChoice == 3
        plot([x1,x2],[y1,y2],'LineStyle','-','Color',col,'LineWidth',MS1);        
    end        
end
if pChoice == 2    
    plot(xm,ym,'-b','LineWidth',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'EEFlx';


function strVar = display_polar_EFlx(EFlx,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX)
global FS FS1 LW NR MS1
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5];
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;  
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'Particle Energy Flux','FontSize',FS+1)         

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color;
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(62,33,'EFlx','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(EFlx));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = EFlx(k)/6;
    
    % determine the color
    j = 1;
    while EFlx(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1); 
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    if pChoice == 1     
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
                 'Color',col);        
    elseif pChoice == 3
        plot([x1 x2],[y1 y2],'LineStyle','-','Color',col,'LineWidth',MS1);        
    end
end
if pChoice == 2   
    plot(xm,ym,'-b','LineWidth',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'EFlx';


function strVar = display_polar_Sz(Sz,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX)
global FS FS1 LW NR MS1
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5]; 
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;        
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'Poynting Flux','FontSize',FS+1)  

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color; 
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(63,33,'-Sz','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(Sz));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = -Sz(k)/6;
    
    % determine the color
    j = 1;
    while -Sz(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1); 
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    if pChoice == 1  
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
                 'Color',col);        
    elseif pChoice == 3
        plot([x1 x2],[y1 y2],'LineStyle','-','Color',col,'LineWidth',MS1);          
    end
end
if pChoice == 2    
    plot(xm,ym,'-b','LineWidth',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'Sz';


function strVar = display_polar_SzM(SzM,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX)
global FS FS1 LW MS1
NR = 1.6;
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5];
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;       
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'Poynting Flux Model','FontSize',FS+1)

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color;
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(63,33,'-Sz','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(SzM));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = -SzM(k)/6;
    
    % determine the color
    j = 1;
    while -SzM(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1); 
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    if pChoice == 1       
        plot([x(k) 1.2*xm(k)],[y(k) 1.2*ym(k)],'Linewidth',LW+2,'LineStyle','-',...
             'Color',col);            
    elseif pChoice == 3
        plot([x1 x2],[y1 y2],'LineStyle','-','Color',col,'LineWidth',MS1);                     
        % plot([x(k),x(kC(i+1))],[y(k),y(kC(i+1))],'LineStyle','-',...
        %     'Color',col,'LineWidth',MS1);
    end
end
if pChoice == 2    
    plot(xm,ym,'.b','MarkerSize',5);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'SzM';


function strVar = display_polar_Vx(Vx,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX) 
global FS FS1 LW NR MS1
Vx = Vx/1000;
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5]; 
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;        
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'Vx','FontSize',FS+1)  

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color; 
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(64,33,'Vx  x10^3','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(Vx));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = Vx(k)*10;
    
    % determine the color
    j = 1;
    while Vx(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1); 
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    if pChoice == 1        
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
             'Color',col);       
    elseif pChoice == 3                    
        plot([x1 x2],[y1 y2],'LineStyle','-','Color',col,'LineWidth',MS1);     
        % plot([x1,x2],[y1,y2],'LineStyle','-','Color',col,'LineWidth',MS1);
        % plot([x(k),x(kC(i+1))],[y(k),y(kC(i+1))],'LineStyle','-',...
        %     'Color',col,'LineWidth',MS1);
    end
end
if pChoice == 2  
    plot(xm,ym,'-b','LineWidth',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'Vx';


function strVar = display_polar_Vy(Vy,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX) 
global FS FS1 LW NR MS1
Vy = Vy/1000;
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5]; 
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;     
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'Vy','FontSize',FS+1)  

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color;
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(64,33,'Vy  x10^3','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(Vy));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = Vy(k)*10;
    
    % determine the color
    j = 1;
    while Vy(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1);
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    if pChoice == 1       
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
             'Color',col);    
    elseif pChoice == 3        
        plot([x1 x2],[y1 y2],'LineStyle','-','Color',col,'LineWidth',MS1);           
        % plot([x1,x2],[y1,y2],'LineStyle','-','Color',col,'LineWidth',MS1);
        % plot([x(k),x(kC(i+1))],[y(k),y(kC(i+1))],'LineStyle','-',...
        %     'Color',col,'LineWidth',MS1);
    end
end
if pChoice == 2    
    plot(xm,ym,'-b','LineWidth',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'Vy';


function strVar = display_polar_Vz(Vz,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX) 
global FS FS1 LW NR MS1
Vz = Vz/1000;
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5]; 
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;        
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'Vz','FontSize',FS+1)  

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color;
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(64,33,'Vz  x10^3','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(Vz));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = Vz(k)*10;
    
    % determine the color
    j = 1;
    while Vz(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1);
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    if pChoice == 1       
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
             'Color',col);    
    elseif pChoice == 3                
        plot([x1 x2],[y1 y2],'LineStyle','-','Color',col,'LineWidth',MS1);          
        % plot([x1,x2],[y1,y2],'LineStyle','-','Color',col,'LineWidth',MS1);
        % plot([x(k),x(kC(i+1))],[y(k),y(kC(i+1))],'LineStyle','-',...
        %     'Color',col,'LineWidth',MS1);
    end
end
if pChoice == 2    
    plot(xm,ym,'-b','LineWidth',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'Vz';


function strVar = display_polar_Ex(Ex,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX)
global FS FS1 LW NR MS1
Ex = Ex/10;
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5]; 
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;        
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'Ex','FontSize',FS+1)  

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color; 
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(64,33,'Ex  x10','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(Ex));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = Ex(k)*2.3;
    
    % determine the color
    j = 1;
    while Ex(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1); 
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    if pChoice == 1       
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
             'Color',col);    
    elseif pChoice == 3        
        plot([x1 x2],[y1 y2],'LineStyle','-','Color',col,'LineWidth',MS1);            
        % plot([x1,x2],[y1,y2],'LineStyle','-','Color',col,'LineWidth',MS1);
        % plot([x(k),x(kC(i+1))],[y(k),y(kC(i+1))],'LineStyle','-',...
        %     'Color',col,'LineWidth',MS1);
    end
end
if pChoice == 2  
    plot(xm,ym,'-b','LineWidth',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'Ex';


function strVar = display_polar_Ey(Ey,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX) 
global FS FS1 LW NR MS1 
Ey = Ey/10;
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5]; 
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;     
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'Ey','FontSize',FS+1)  

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color;
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(64,33,'Ey  x10','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(Ey));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = Ey(k)*2.3;
    
    % determine the color
    j = 1;
    while Ey(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1);
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    if pChoice == 1       
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
             'Color',col);    
    elseif pChoice == 3
        plot([x1 x2],[y1 y2],'LineStyle','-','Color',col,'LineWidth',MS1);           
        % plot([x1,x2],[y1,y2],'LineStyle','-','Color',col,'LineWidth',MS1);
        % plot([x(k),x(kC(i+1))],[y(k),y(kC(i+1))],'LineStyle','-',...
        %     'Color',col,'LineWidth',MS1);
    end
end
if pChoice == 2    
    plot(xm,ym,'-b','LineWidth',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'Ey';


function strVar = display_polar_Ez(Ez,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX) 
global FS FS1 LW NR MS1
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5]; 
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;        
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'Ez','FontSize',FS+1)  

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color;
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(64,33,'Ez','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(Ez));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = Ez(k)*2.3;
    
    % determine the color
    j = 1;
    while Ez(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1);
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    if pChoice == 1       
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
             'Color',col);    
    elseif pChoice == 3
        plot([x1 x2],[y1 y2],'LineStyle','-','Color',col,'LineWidth',MS1);            
        % plot([x1,x2],[y1,y2],'LineStyle','-','Color',col,'LineWidth',MS1);
        % plot([x(k),x(kC(i+1))],[y(k),y(kC(i+1))],'LineStyle','-',...
        %     'Color',col,'LineWidth',MS1);
    end
end
if pChoice == 2    
    plot(xm,ym,'-b','LineWidth',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'Ez';


function strVar = display_polar_Bx(Bx,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX) 
global FS FS1 LW NR MS1
Bx = Bx/1000;
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5]; 
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;        
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'\DeltaBx','FontSize',FS+1)  

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color;
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(63,33,'\DeltaBx  x10^3','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(Bx));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = Bx(k)*20;
    
    % determine the color
    j = 1;
    while Bx(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1); 
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    if pChoice == 1       
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
             'Color',col);    
    elseif pChoice == 3
        plot([x1 x2],[y1 y2],'LineStyle','-','Color',col,'LineWidth',MS1);
        % plot([x1,x2],[y1,y2],'LineStyle','-','Color',col,'LineWidth',MS1);
        % plot([x(k),x(kC(i+1))],[y(k),y(kC(i+1))],'LineStyle','-',...
        %     'Color',col,'LineWidth',MS1);
    end
end
if pChoice == 2    
    plot(xm,ym,'-b','LineWidth',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'dBx';


function strVar = display_polar_By(By,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX) 
global FS FS1 LW NR MS1
By = By/1000;
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5]; 
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;        
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'\DeltaBy','FontSize',FS+1)  

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color;
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(63,33,'\DeltaBy  x10^3','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(By));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    len = By(k)*20;
    
    % determine the color
    j = 1;
    while By(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1); 
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    if pChoice == 1       
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
             'Color',col);    
    elseif pChoice == 3
        plot([x1 x2],[y1 y2],'LineStyle','-','Color',col,'LineWidth',MS1);   
        % plot([x1,x2],[y1,y2],'LineStyle','-','Color',col,'LineWidth',MS1);
        % plot([x(k),x(kC(i+1))],[y(k),y(kC(i+1))],'LineStyle','-',...
        %     'Color',col,'LineWidth',MS1);
    end
end
if pChoice == 2   
    plot(xm,ym,'-b','LineWidth',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'dBy';


function strVar = display_polar_Ratio(Ratio,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX) 
global FS FS1 LW NR MS1
Ratio = log10(Ratio);
color_grid = [0.65 0.65 0.65]; color_edge = [0.7 0.7 0.7];
color_patch = [.9 .9 .9]; colorPlot = [.5 .5 .5]; 
len_color = length(map_color);

draw_polar() 
m = -60; n = -30; m1 = -47; n1 = -47;        
text(m,n,strArrow,'Rotation',-45,'FontSize',16,'Color',colorPlot)
text(m1,n1,strArrow1,'Rotation',-45,'FontSize',14,'Color',colorPlot)
text(15,57,'log_1_0(EFlx/Sz)','FontSize',FS+1)  

xMin = min(bX); xMax = max(bX); inc_color = (xMax-xMin)/len_color;
bY = len_color/(xMax-xMin)*(bX-xMin)+1;

if pChoice ~=2 
    text(63,33,'log_1_0(EFlx/Sz)','FontSize',FS1)
    colormap(map_color)
    P = get(gca,'Position');
    colorbar('Position',[P(1)+P(3)-0.03 P(2)+0.05 0.0082 0.5*P(4)],...
             'yTick',bY,'yTickLabel',bX,'FontSize',FS1);
end

hold on
plot(x,y,'LineWidth',1,'Color',colorPlot)
xm = nan(size(x)); ym = nan(size(y)); m = Slope;
kC = find(~isnan(m) & ~isnan(Ratio));
if length(kC) > 0
for i = 1:length(kC)
    k = kC(i);
    if Ratio(k) > 0
        len = Ratio(k)*8;
    else
        len = Ratio(k)*2; 
    end
    
    % determine the color
    j = 1;
    while Ratio(k) > xMin+j*inc_color
        j = j+1;
    end    
    if (j > len_color)
        col = map_color(len_color,:);
    else
        col = map_color(j,:);
    end
    
    % plot
    minv = -1/m(k);        
    xm(k) = x(k) - len/sqrt(minv^2+1);
    ym(k) = y(k) - len*minv/sqrt(minv^2+1); 
    x1 = x(k) - NR/sqrt(m(k)^2+1);
    y1 = y(k) - NR*m(k)/sqrt(m(k)^2+1);    
    x2 = x(k) + NR/sqrt(m(k)^2+1);
    y2 = y(k) + NR*m(k)/sqrt(m(k)^2+1);
    if pChoice == 1       
        plot([x(k) xm(k)],[y(k) ym(k)],'Linewidth',LW,'LineStyle','-',...
             'Color',col);    
    elseif pChoice == 3
        plot([x1 x2],[y1 y2],'LineStyle','-','Color',col,'LineWidth',MS1);   
        % plot([x1,x2],[y1,y2],'LineStyle','-','Color',col,'LineWidth',MS1);
        % plot([x(k),x(kC(i+1))],[y(k),y(kC(i+1))],'LineStyle','-',...
        %     'Color',col,'LineWidth',MS1);
    end
end
if pChoice == 2   
    plot(xm,ym,'-b','LineWidth',1);
end
end
kP = find(~isnan(x) & ~isnan(y));
if length(kP) ~= 0
    plot(x(kP(1)),y(kP(1)),'.m','MarkerSize',20)
end
hold off
strVar = 'EFlx/Sz';


%-------------------
% callback functions
%-------------------
function mouse_click_plot(source,eventdata,strVar)
global data_plot LW FS FS1 hbStatusBar ttxP teP

param = get(gcf,'SelectionType');
        
switch param        
    case 'alt'
        ptl = get(gcf,'CurrentPoint');
        xl = ptl(1,1); yl = ptl(1,2);
        ttxP = uicontrol(gcf,'Style', 'text','String','Factor',...
                       'Units','normalized','Position',[xl yl+0.06 0.04 0.02],...
                       'BackgroundColor','white','HorizontalAlignment','left'); 
        teP = uicontrol(gcf,'Style', 'edit','String','1',...
                       'Units','normalized','Position',[xl+0.04 yl+0.061 0.029 0.02], ...    
                       'BackgroundColor','white','HorizontalAlignment','left');                   
        List_items = {'None','Vx','Vy','Vz','VxVyVz','dBx','dBy','BxBy',...
                      'Ex','Ey','Ez','ExEyEz','IEFlx','EEFlx','EFlx',...
                      'IEFlx_Ch','EEFlx_Ch','Sz','SzM','EFlx/Sz','Nd','IEF', ...
                      'IMF-Bx','IMF-By','IMF-Bz','IMF-B','IMF-dB','IMF-Bxyz', ...
                      'atan(Bz/By)','Vx_sw','Vy_sw','Vz_sw','V_sw','Vxyz_sw',...
                      'Np','Tp','Psw','Beta','Sym','Asy','PCN','AE','EV','AMIE-Ex',...
                      'AMIE-Ey','AMIE-Exy','AMIE-Phi','AMIE-Sz'};
        tl = uicontrol(gcf,'Style', 'listbox','String',List_items,...
                       'Units','normalized','Position',[xl yl 0.07 0.06],'Visible','on',...
                       'BackgroundColor','white');
        set(tl,'CallBack',@list_callback_plot);

    case 'open'   
        set(hbStatusBar,'String','Plotting the data...');
        pause(2)
        
        % generate a plotting figure
        PP = [0 0.05 0.9 0.5];        
        hfs = figure('Units','normalized','Position',PP,'Name','Plotter',...
                     'NumberTitle','off','Color','white');

        % generate the axes
        top = 0.15; bottom = 0.1; height = 1-top-bottom;
        left = 0.05; right = 0.05; width = 1-left-right;
        hax = axes('Position',[left bottom width height], ...
                   'FontSize',FS,'Visible','on');  
        str6 = data_plot.str6;
        strX = str6;
 
t = data_plot.t; t1 = data_plot.t1; t2 = data_plot.t2; t3 = data_plot.t3;
OMNI_B = sqrt(data_plot.B_OMNI(:,1).^2+data_plot.B_OMNI(:,2).^2+data_plot.B_OMNI(:,3).^2);
OMNI_V = (data_plot.V_OMNI(:,1));

cla(gca,'reset');
switch param
    case 'Vx'            
        bX = [-3 -2 -1 0 1 2 3];
        strVar = plot_Vx(data_plot.Vxyz(:,1),t,LW,FS1,strX,bX*FactorP);         
    case 'Vy'
        bX = [-3 -2 -1 0 1 2 3];
        strVar = plot_Vy(data_plot.Vxyz(:,2),t,LW,FS1,strX,bX*FactorP);            
    case 'Vz'            
        bX = [-3 -2 -1 0 1 2 3];
        strVar = plot_Vz(data_plot.Vxyz(:,3),t,LW,FS1,strX,bX*FactorP);
    case 'VxVyVz'
        bX = [-3 -2 -1 0 1 2 3];
        strVar = plot_V(data_plot.Vxyz,t,LW,FS1,strX,bX*FactorP);
    case 'dBx'
        bX = [-2 -1 0 1 2];
        strVar = plot_Bx(data_plot.Bxy(:,1),t,LW,FS1,strX,bX*FactorP);
    case 'dBy'
        bX = [-2 -1 0 1 2];
        strVar = plot_By(data_plot.Bxy(:,2),t,LW,FS1,strX,bX*FactorP);
    case 'BxBy'
        bX = [-2 -1 0 1 2];
        strVar = plot_B(data_plot.Bxy,t,LW,FS1,strX,bX*FactorP);
    case 'Ex'            
        bX = [-100 -50 0 50 100];
        strVar = plot_Ex(data_plot.Exyz(:,1),t,LW,FS1,strX,bX*FactorP);
    case 'Ey'
        bX = [-100 -50 0 50 100];
        strVar = plot_Ey(data_plot.Exyz(:,2),t,LW,FS1,strX,bX*FactorP);
    case 'Ez'
        bX = [-10 -5 0 5 10];
        strVar = plot_Ez(data_plot.Exyz(:,3),t,LW,FS1,strX,bX*FactorP);
    case 'ExEyEz'
        bX = [-10 -5 0 5 10];
        strVar = plot_E(data_plot.Exyz,t,LW,FS1,strX,bX*FactorP);
    case 'IEFlx'
        bX = [10^(-2) 10^(-1) 1 10^1 10^2];
        strVar = plot_IEFlx(data_plot.Flx(:,1),t,LW,FS1,strX,bX*FactorP);
    case 'EEFlx'
        bX = [10^(-2) 10^(-1) 1 10^1 10^2];
        strVar = plot_EEFlx(data_plot.Flx(:,2),t,LW,FS1,strX,bX*FactorP);
    case 'EFlx'
        bX = [10^(-2) 10^(-1) 1 10^1 10^2];
        strVar = plot_Flx(data_plot.Flx,t,LW,FS1,strX,bX*FactorP);
    case 'IEFlx_Ch'
        strVar = plot_IEFlx_Ch(data_plot.IEFlxCh,t,LW,FS1,strX);
    case 'EEFlx_Ch'
        strVar = plot_EEFlx_Ch(data_plot.EEFlxCh,t,LW,FS1,strX);
    case 'Sz'
        bX = [-40 -30 -20 -10 0 10];
        strVar = plot_Poy(data_plot.Poy,t,LW,FS1,strX,bX*FactorP);
    case 'SzM'
        bX = [-40 -30 -20 -10 0 10];
        strVar = plot_SzM(data_plot.Poy,t,LW,FS1,strX,bX*FactorP);
    case 'EFlx/Sz'
        bX = [10^(-6) 10^(-3) 10^0 10^3 10^6];
        strVar = plot_Ratio(data_plot.Ra,t,LW,FS1,strX,bX*FactorP);
    case 'Nd'          
        bX = [0 5 10 15 20]*10^(-12);
        strVar = plot_Nd(data_plot.Nd,t,LW,FS1,strX,bX*FactorP);
    case 'IEF'
        bX = [-10 -5 0 5 10];
        strVar = plot_IEF(data_plot.NpTpPswBetaIEF_OMNI(:,4),t3,LW,FS1,strX,bX*FactorP);
    case 'IMF-Bx'
        bX = [-40 -20 0 20 40];
        strVar = plot_IMF_Bx(data_plot.B_OMNI(:,1),t3,LW,FS1,strX,bX*FactorP);
    case 'IMF-By'
        bX = [-40 -20 0 20 40];
        strVar = plot_IMF_By(data_plot.B_OMNI(:,2),t3,LW,FS1,strX,bX*FactorP);
    case 'IMF-Bz'
        bX = [-40 -20 0 20 40];
        strVar = plot_IMF_Bz(data_plot.B_OMNI(:,3),t3,LW,FS1,strX,bX*FactorP);
    case 'IMF-B'
        bX = [-40 -20 0 20 40];
        strVar = plot_IMF_B(OMNI_B,t3,LW,FS1,strX,bX*FactorP);
    case 'IMF-dB'
        bX = [-4 -2 0 2 4];
        strVar = plot_IMF_dB(data_plot.B_OMNI(:,4),t3,LW,FS1,strX,bX*FactorP);
    case 'IMF-Bxyz'
        bX = [-40 -20 0 20 40];
        strVar = plot_IMF_Bxyz(data_plot.B_OMNI,t3,LW,FS1,strX,bX*FactorP);
    case 'atan(Bz/By)'
        strVar = plot_atan(data_plot.B_OMNI,t3,LW,FS1,strX);
    case 'Vx_sw'
        bX = [-1000 -800 -600 -400 -200 0];
        strVar = plot_Vx_sw(data_plot.V_OMNI(:,1),t3,LW,FS1,strX,bX*FactorP);
    case 'Vy_sw'
        bX = [-100 -50 0 50 100];
        strVar = plot_Vy_sw(data_plot.V_OMNI(:,2),t3,LW,FS1,strX,bX*FactorP);
    case 'Vz_sw'
        bX = [-100 -50 0 50 100];
        strVar = plot_Vz_sw(data_plot.V_OMNI(:,3),t3,LW,FS1,strX,bX*FactorP);
    case 'V_sw'
        bX = [0 200 400 600 800 1000];
        strVar = plot_V_sw(OMNI_V,t3,LW,FS1,strX,bX*FactorP);
    case 'Vxyz_sw'
        bX = [-100 -50 0 50 100];
        strVar = plot_Vxyz_sw(data_plot.V_OMNI,t3,LW,FS1,strX,bX*FactorP);
    case 'Psw'
        bX = [0 5 10 15 20];
        strVar = plot_Psw(data_plot.PSW_OMNI,t3,LW,FS1,strX,bX*FactorP);
    case 'AE'
        bX = [0 750 1500];
        strVar = plot_AE(data_plot.Omni_AEmean,t3,LW,FS1,strX,bX*FactorP);
    case 'KL E-field'
        bX = [0 5 10 15];
        strVar = plot_Kan_Lee(data_plot.Omni_Kan_Lee, t3, LW, FS1, strX, bX*FactorP);
    case 'Kp'
        bX = [0 2 4 6 8 10];
        strVar = plot_Kp(data_plot.Omni_Kp, t3, LW, FS1, strX, bX*FactorP);
    case 'Hp'
        bX = [0 50 100 150 200];
        strVar = plot_Hp([data_plot.Omni_HpNmean, data_plot.Omni_HpSmean], t3, ...
            LW, FS1, strX, bX*FactorP);
    case 'Hp_ion'
        bX = [0 20 40 60];
        strVar = plot_Hp_ion([data_plot.Omni_NH_Hp_ion_mean, data_plot.Omni_SH_Hp_ion_mean], ...
            t3, LW, FS1, strX, bX*FactorP);
    case 'Pe'   
        bX = [0 50 100 150 200 250];
        strVar = plot_Pe(data_plot.Omni_Pemean, t3, LW, FS1, strX, bX*FactorP);
    case 'Pi'    
        bX = [0 20 40 60 80];
        strVar = plot_Pi(data_plot.Omni_Pimean, t3, LW, FS1, strX, bX*FactorP);
    case 'AL'
        bX = [0 750 1500];
        strVar = plot_AL(data_plot.Omni_ALmean,t3,LW,FS1,strX,bX*FactorP);
    case 'AU'
        bX = [0 200 400 600];
        strVar = plot_AU(data_plot.Omni_AUmean,t3,LW,FS1,strX,bX*FactorP);
    case 'DST'
        bX = [-100 -50 0 50 100];
        strVar = plot_DST(data_plot.Omni_DST,t3,LW,FS1,strX,bX*FactorP);
    case 'eqb'
        bX = [40 50 60 70];
        strVar = plot_eqb(data_plot.Omni_eqb_mean,t3,LW,FS1,strX,bX*FactorP);
    case 'EV'
        bX = [0 2 4];
        strVar = plot_EV(data_plot.EV,t2,LW,FS1,strX,bX*FactorP);   
  
end
        set(gca,'ButtonDownFcn',{@mouse_click_plot,strVar});
        set(hbStatusBar,'String','Done plotting the data...');
        pause(2)
end


function list_callback_plot(source,eventdata)
global data_plot LW FS1 hbStatusBar teP ttxP
   
if strcmp(get(gcf,'SelectionType'),'open')
    set(hbStatusBar,'String','Plotting the data...');
    pause(2)

    file_list = get(source,'String');
    index_selected = get(source,'Value');
    param = file_list{index_selected};
    FactorP = str2num(get(teP,'String'));
    delete(source)
    delete(ttxP)
    delete(teP)
    
    str0 = data_plot.str0;
    str6 = data_plot.str6;
    if strcmp(get(gca,'xTickLabel'),'')
        strX = str0;
    else
        strX = str6;
    end
    
    t = data_plot.t; t1 = data_plot.t1; t2 = data_plot.t2; 
    OMNI_B = sqrt(data_plot.B_OMNI(:,1).^2+data_plot.B_OMNI(:,2).^2+data_plot.B_OMNI(:,3).^2);
    OMNI_V = sqrt(data_plot.V_OMNI(:,1).^2+data_plot.V_OMNI(:,2).^2+data_plot.V_OMNI(:,3).^2);                       
    
    if ~strcmp(param,'None')
        cla(gca,'reset')
    end
    
    switch param        
        case 'Vx'            
            bX = [-3 -2 -1 0 1 2 3];
            strVar = plot_Vx(data_plot.Vxyz(:,1),t,LW,FS1,strX,bX*FactorP);
        case 'Vy'
            bX = [-3 -2 -1 0 1 2 3];
            strVar = plot_Vy(data_plot.Vxyz(:,2),t,LW,FS1,strX,bX*FactorP);
        case 'idm_Vy'
            bX = [-3 -2 -1 0 1 2 3];
            strVar = plot_idm_Vy(data_plot.idm_Vy,t2,LW,FS1,strX,bX*FactorP);
        case 'idm_Vz'            
            bX = [-3 -2 -1 0 1 2 3];
            strVar = plot_idm_Vz(data_plot.idm_Vz,t2,LW,FS1,strX,bX*FactorP);
        case 'Vz'
            bX = [-3 -2 -1 0 1 2 3];
            strVar = plot_Vz(data_plot.Vxyz(:,3),t,LW,FS1,strX,bX*FactorP);
        case 'VxVyVz'
            bX = [-3 -2 -1 0 1 2 3];
            strVar = plot_V(data_plot.Vxyz,t,LW,FS1,strX,bX*FactorP);
        case 'dBx'
            bX = [-2 -1 0 1 2];
            strVar = plot_Bx(data_plot.Bxy(:,1),t,LW,FS1,strX,bX*FactorP);
        case 'dBy'
            bX = [-2 -1 0 1 2];
            strVar = plot_By(data_plot.Bxy(:,2),t,LW,FS1,strX,bX*FactorP);
        case 'BxBy'
            bX = [-2 -1 0 1 2];
            strVar = plot_B(data_plot.Bxy,t,LW,FS1,strX,bX*FactorP);
        case 'Ex'
            bX = [-100 -50 0 50 100];
            strVar = plot_Ex(data_plot.Exyz(:,1),t,LW,FS1,strX,bX*FactorP);
        case 'Ey'
            bX = [-100 -50 0 50 100];
            strVar = plot_Ey(data_plot.Exyz(:,2),t,LW,FS1,strX,bX*FactorP);
        case 'Ez'
            bX = [-10 -5 0 5 10];
            strVar = plot_Ez(data_plot.Exyz(:,3),t,LW,FS1,strX,bX*FactorP);
        case 'ExEyEz'
            bX = [-10 -5 0 5 10];
            strVar = plot_E(data_plot.Exyz,t,LW,FS1,strX,bX*FactorP);
        case 'IEFlx'
            bX = [10^(-2) 10^(-1) 1 10^1 10^2];
            strVar = plot_IEFlx(data_plot.Flx(:,1),t,LW,FS1,strX,bX*FactorP);
        case 'EEFlx'
            bX = [10^(-2) 10^(-1) 1 10^1 10^2];
            strVar = plot_EEFlx(data_plot.Flx(:,2),t,LW,FS1,strX,bX*FactorP);
        case 'EFlx'
            bX = [10^(-2) 10^(-1) 1 10^1 10^2];
            strVar = plot_Flx(data_plot.Flx,t,LW,FS1,strX,bX*FactorP);
        case 'IEFlx_Ch'
            strVar = plot_IEFlx_Ch(data_plot.IEFlxCh,t,LW,FS1,strX);
        case 'EEFlx_Ch'
            strVar = plot_EEFlx_Ch(data_plot.EEFlxCh,t,LW,FS1,strX);            
        case 'Sz'
            bX = [-40 -30 -20 -10 0 10];
            strVar = plot_Poy(data_plot.Poy,t,LW,FS1,strX,bX*FactorP);          
        case 'SzM'                       
            bX = [-40 -30 -20 -10 0 10];
            strVar = plot_SzM(data_plot.Poy,t,LW,FS1,strX,bX*FactorP);            
        case 'EFlx/Sz'           
            bX = [10^(-6) 10^(-3) 10^0 10^3 10^6];
            strVar = plot_Ratio(data_plot.Ra,t,LW,FS1,strX,bX*FactorP);            
        case 'Nd'             
            bX = [0 5 10 15 20]*10^(-12);
            strVar = plot_Nd(data_plot.Nd,t,LW,FS1,strX,bX*FactorP);            
        case 'IEF'            
            bX = [-10 -5 0 5 10];
            strVar = plot_IEF(data_plot.NpTpPswBetaIEF_OMNI(:,4),t1,LW,FS1,strX,bX*FactorP);            
        case 'IMF-Bx'            
            bX = [-40 -20 0 20 40];
            strVar = plot_IMF_Bx(data_plot.B_OMNI(:,1),t1,LW,FS1,strX,bX*FactorP);            
        case 'IMF-By'            
            bX = [-40 -20 0 20 40];
            strVar = plot_IMF_By(data_plot.B_OMNI(:,2),t1,LW,FS1,strX,bX*FactorP);            
        case 'IMF-Bz'            
            bX = [-40 -20 0 20 40];
            strVar = plot_IMF_Bz(data_plot.B_OMNI(:,3),t1,LW,FS1,strX,bX*FactorP);           
        case 'IMF-B'            
            bX = [-40 -20 0 20 40];
            strVar = plot_IMF_B(OMNI_B,t1,LW,FS1,strX,bX*FactorP);           
        case 'IMF-dB'            
            bX = [-4 -2 0 2 4];
            strVar = plot_IMF_dB(data_plot.B_OMNI(:,4),t1,LW,FS1,strX,bX*FactorP);            
        case 'IMF-Bxyz'            
            bX = [-40 -20 0 20 40];
            strVar = plot_IMF_Bxyz(data_plot.B_OMNI,t1,LW,FS1,strX,bX*FactorP);           
        case 'atan(Bz/By)'            
            strVar = plot_atan(data_plot.B_OMNI,t1,LW,FS1,strX);            
        case 'Vx_sw'            
            bX = [-1000 -800 -600 -400 -200 0];
            strVar = plot_Vx_sw(data_plot.V_OMNI(:,1),t1,LW,FS1,strX,bX*FactorP);            
        case 'Vy_sw'            
            bX = [-100 -50 0 50 100];
            strVar = plot_Vy_sw(data_plot.V_OMNI(:,2),t1,LW,FS1,strX,bX*FactorP);            
        case 'Vz_sw'
            bX = [-100 -50 0 50 100];
            strVar = plot_Vz_sw(data_plot.V_OMNI(:,3),t1,LW,FS1,strX,bX*FactorP);            
        case 'V_sw'           
            bX = [0 200 400 600 800 1000];
            strVar = plot_V_sw(OMNI_V,t1,LW,FS1,strX,bX*FactorP);            
        case 'Vxyz_sw'            
            bX = [-100 -50 0 50 100];
            strVar = plot_Vxyz_sw(data_plot.V_OMNI,t1,LW,FS1,strX,bX*FactorP);            
        case 'Np'            
            bX = [0 10 20 30 40];
            strVar = plot_Np(data_plot.NpTpPswBetaIEF_OMNI(:,1),t1,LW,FS1,strX,bX*FactorP);           
        case 'Tp'            
            bX = [0 2 4]*10^5;
            strVar = plot_Tp(data_plot.NpTpPswBetaIEF_OMNI(:,2),t1,LW,FS1,strX,bX*FactorP);           
        case 'Psw'            
            bX = [0 5 10 15 20];
            strVar = plot_Psw(data_plot.NpTpPswBetaIEF_OMNI(:,3),t1,LW,FS1,strX,bX*FactorP);            
        case 'Beta'            
            bX = [0 20 40];
            strVar = plot_Beta(data_plot.NpTpPswBetaIEF_OMNI(:,5),t1,LW,FS1,strX,bX*FactorP);            
        case 'Sym'           
            bX = [-150 -100 -50 0 50];
            strVar = plot_Sym(data_plot.OMNI_Sym,t1,LW,FS1,strX,bX*FactorP);
        case 'Asy'           
            bX = [0 50 100 150 200];
            strVar = plot_Asy(data_plot.OMNI_Asy,t1,LW,FS1,strX,bX*FactorP);
        case 'PCN'            
            bX = [-10 -5 0 5 10];
            strVar = plot_PCN(data_plot.OMNI_PCN,t1,LW,FS1,strX,bX*FactorP);            
        case 'AE'            
            bX = [0 500 1000];
            strVar = plot_AE(data_plot.OMNI_AE,t1,LW,FS1,strX,bX*FactorP);            
        case 'EV'            
            bX = [0 2 4];
            strVar = plot_EV(data_plot.EV,t2,LW,FS1,strX,bX*FactorP);                            
        case 'AMIE-Sz'            
            bX = [-40 -30 -20 -10 0 10];
            strVar = plot_AMIE_Sz(data_plot.AMIE(:,1),t,LW,FS1,strX,bX*FactorP);            
        case 'AMIE-Phi'           
            bX = [-100 -50 0 50 100];
            strVar = plot_AMIE_P(data_plot.AMIE(:,2),t,LW,FS1,strX,bX*FactorP);            
        case 'AMIE-Ex'            
            bX = [-100 -50 0 50 100];
            strVar = plot_AMIE_Ex(data_plot.AMIE(:,3),t,LW,FS1,strX,bX*FactorP);            
        case 'AMIE-Ey'           
            bX = [-100 -50 0 50 100];
            strVar = plot_AMIE_Ey(data_plot.AMIE(:,4),t,LW,FS1,strX,bX*FactorP);            
        case 'AMIE-Exy'            
            bX = [-100 -50 0 50 100];
            strVar = plot_AMIE_E(data_plot.AMIE,t,LW,FS1,strX,bX*FactorP);            
    end
    
    if ~strcmp(param,'None')
        set(gca,'ButtonDownFcn',{@mouse_click_plot,strVar});
        k = find(data_plot.ha == gca);
        if ~isempty(k)
            data_plot.list_plot(k) = {strVar};
            data_plot.plot_Factor(k) = FactorP;
        end
    end      
    
    set(hbStatusBar,'String','Done plotting the data...');
    pause(2)
end


function mouse_click_polar(source,eventdata,strVar)
global data_plot hbStatusBar map_color pChoice FS tx txe ttx te
   
param = get(gcf,'SelectionType');
        
switch param        
    case 'alt' 
        ptl = get(gcf,'CurrentPoint');
        xl = ptl(1,1); yl = ptl(1,2);
        tx = uicontrol(gcf,'Style', 'text','String','Plot Type',...
                       'Units','normalized','Position',[xl yl+0.08 0.04 0.02],...
                       'BackgroundColor','white','HorizontalAlignment','left'); 
        txe = uicontrol(gcf,'Style', 'edit','String','1',...
                       'Units','normalized','Position',[xl+0.04 yl+0.083 0.029 0.02],...
                       'BackgroundColor','white','HorizontalAlignment','left');
        ttx = uicontrol(gcf,'Style', 'text','String','Factor',...
                       'Units','normalized','Position',[xl yl+0.06 0.04 0.02],...
                       'BackgroundColor','white','HorizontalAlignment','left'); 
        te = uicontrol(gcf,'Style', 'edit','String','1',...
                       'Units','normalized','Position',[xl+0.04 yl+0.061 0.029 0.02],...
                       'BackgroundColor','white','HorizontalAlignment','left');
        List_items = {'None','Vx','Vy','Vz','dBx','dBy','Ex','Ey','Ez', ...
                      'IEFlx','EEFlx','EFlx','Sz','SzM','EFlx/Sz','Nd', ...
                      'AMIE-Ex','AMIE-Ey','AMIE-Phi','AMIE-Sz'};
        tl = uicontrol(gcf,'Style', 'listbox','String',List_items,...
                       'Units','normalized','Position',[xl yl 0.07 0.06],'Visible','on',...
                       'BackgroundColor','white');
        set(tl,'Callback',@list_callback_polar);   
    
    case 'open'
        set(hbStatusBar,'String','Plotting the data...');
        pause(2)                
        
        % generate a plotting figure
        PP = [0 0.05 0.6 0.5];
        hfs = figure('Units','normalized','Position',PP,'Name','Plotter','NumberTitle','off', ...
                     'Color','white'); 

        % generate the axes
        pheight = 0.1950; pwidth = 0.3000; k = pheight/pwidth;
        top = 0.1; bottom = 0.1; height = 1-top-bottom;         
        left = 0.01; right = 0.01; width = 1-left-right;   
        hax = axes('Position',[left bottom width height], ...
                   'FontSize',FS,'Visible','on');
         
        strArrow = data_plot.strArrow;
        strArrow1 = data_plot.strArrow1;
        x = data_plot.x; y = data_plot.y;
        xD = data_plot.xD; yD = data_plot.yD;
        Slope = data_plot.Slope;
        SlopeD = data_plot.SlopeD;
        
        cla(gca,'reset'); bx = [];                         
        
        switch strVar
        case 'Vx'
            bX = [-3 -2 -1 0 1 2 3]; % Vx
            strVar = display_polar_Vx(data_plot.Vxyz(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'Vy'
            bX = [-3 -2 -1 0 1 2 3]; % Vy
            strVar = display_polar_Vy(data_plot.Vxyz(:,2),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'Vz'
            bX = [-3 -2 -1 0 1 2 3]; % Vz
            strVar = display_polar_Vz(data_plot.Vxyz(:,3),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'dBx'
            bX = [-2 -1 0 1 2]; % Bx
            strVar = display_polar_Bx(data_plot.Bxy(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'dBy'
            bX = [-2 -1 0 1 2]; % By
            strVar = display_polar_By(data_plot.Bxy(:,2),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'Ex'
            bX = [-10 -5 0 5 10]; % Ex
            strVar = display_polar_Ex(data_plot.Exyz(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'Ey'
            bX = [-10 -5 0 5 10]; % Ey
            strVar = display_polar_Ey(data_plot.Exyz(:,2),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'Ez'
            bX = [-10 -5 0 5 10]; % Ez
            strVar = display_polar_Ez(data_plot.Exyz(:,3),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'IEFlx'
            bX = [0 5 10 15 20]; % IEFlx
            strVar = display_polar_IEFlx(data_plot.Flx(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'EEFlx'
            bX = [0 5 10 15 20]; % EEFlx
            strVar = display_polar_EEFlx(data_plot.Flx(:,2),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'EFlx'
            bX = [0 5 10 15 20]; % EFlx
            strVar = display_polar_EFlx(data_plot.EFlx,map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'Sz'
            bX = [0 5 10 15 20]; %Sz
            strVar = display_polar_Sz(data_plot.Poy(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'SzM'
            bX = [0 5 10 15 20]; %SzM
            strVar = display_polar_SzM(data_plot.Poy(:,2),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'EFlx/Sz'
            bX = [-3 -2 -1 0 1 2 3]; % Ratio
            strVar = display_polar_Ratio(data_plot.Ra(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'Nd'
            bX = [0 0.5 1 1.5]; % Nd
            strVar = display_polar_Nd(data_plot.Nd(:,2)*10^11,map_color,xD,yD,SlopeD,pChoice,bX);            
        case 'AMIE-Sz'
            bX = [0 5 10 15 20]; %Sz
            strVar = display_polar_AMIE_Sz(data_plot.AMIE(:,1),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'AMIE-Phi'
            bX = [-100 -50 0 50 100];
            strVar = display_polar_AMIE_P(data_plot.AMIE(:,2),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'AMIE-Ex'
            bX = [-10 -5 0 5 10]; % Ex
            strVar = display_polar_AMIE_Ex(data_plot.AMIE(:,3),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        case 'AMIE-Ey'
            bX = [-10 -5 0 5 10]; % Ey
            strVar = display_polar_AMIE_Ey(data_plot.AMIE(:,4),map_color,strArrow,strArrow1,x,y,Slope,pChoice,bX);            
        end 
        set(gca,'Visible','on','yLim',[-51 51],'xColor','white','yColor','white');
        set(gca,'ButtonDownFcn',{@mouse_click_polar,strVar});
        
        set(hbStatusBar,'String','Done plotting the data...');
        pause(2)
end


function list_callback_polar(source,eventdata)
global data_plot hbStatusBar map_color tx txe ttx te

if strcmp(get(gcf,'SelectionType'),'open')
    set(hbStatusBar,'String','Plotting the data...');
    pause(2)

    file_list = get(source,'String');
    index_selected = get(source,'Value');
    param = file_list{index_selected};        
    Factor = str2num(get(te,'String'));  
    pC = str2num(get(txe,'String'));
    delete(source)
    delete(tx)
    delete(txe)
    delete(ttx)
    delete(te)
    bX = [];
    
    strArrow = data_plot.strArrow;
    strArrow1 = data_plot.strArrow1;
    x = data_plot.x; y = data_plot.y;
    xD = data_plot.xD; yD = data_plot.yD;
    Slope = data_plot.Slope;
    SlopeD = data_plot.SlopeD;    
       
    if ~strcmp(param,'None')
        cla(gca,'reset');
    end
    switch param        
        case 'Vx'   
            bX = [-3 -2 -1 0 1 2 3]; % Vx
            strVar = display_polar_Vx(data_plot.Vxyz(:,1),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'Vy'
            bX = [-3 -2 -1 0 1 2 3]; % Vy
            strVar = display_polar_Vy(data_plot.Vxyz(:,2),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'Vz'
            bX = [-3 -2 -1 0 1 2 3]; % Vz
            strVar = display_polar_Vz(data_plot.Vxyz(:,3),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'dBx' 
            bX = [-2 -1 0 1 2]; % Bx
            strVar = display_polar_Bx(data_plot.Bxy(:,1),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'dBy'
            bX = [-2 -1 0 1 2]; % By
            strVar = display_polar_By(data_plot.Bxy(:,2),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);            
        case 'Ex'             
            bX = [-10 -5 0 5 10]; % Ex
            strVar = display_polar_Ex(data_plot.Exyz(:,1),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'Ey'
            bX = [-10 -5 0 5 10]; % Ey
            strVar = display_polar_Ey(data_plot.Exyz(:,2),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'Ez'
            bX = [-10 -5 0 5 10]; % Ez
            strVar = display_polar_Ez(data_plot.Exyz(:,3),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'IEFlx'
            bX = [0 5 10 15 20]; % IEFlx
            strVar = display_polar_IEFlx(data_plot.Flx(:,1),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'EEFlx'                     
            bX = [0 5 10 15 20]; % EEFlx
            strVar = display_polar_EEFlx(data_plot.Flx(:,2),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'EFlx'                     
            bX = [0 5 10 15 20]; % EFlx
            strVar = display_polar_EFlx(data_plot.EFlx,map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'Sz'           
            bX = [0 5 10 15 20]; % Sz
            strVar = display_polar_Sz( data_plot.Poy(:,1),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'SzM'            
            bX = [0 5 10 15 20]; % SzM
            strVar = display_polar_SzM( data_plot.Poy(:,2),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'EFlx/Sz'
            bX = [-3 -2 -1 0 1 2 3]; % Ratio
            strVar = display_polar_Ratio(data_plot.Ra(:,1),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'Nd'            
            bX = [0 0.5 1 1.5]; % Nd
            strVar = display_polar_Nd(data_plot.Nd(:,2)*10^11,map_color,xD,yD,SlopeD,pC,bX*Factor);
        case 'AMIE-Sz'
            bX = [0 5 10 15 20]; %Sz
            strVar = display_polar_AMIE_Sz(data_plot.AMIE(:,1),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'AMIE-Phi'           
            bX = [-100 -50 0 50 100];
            strVar = display_polar_AMIE_P(data_plot.AMIE(:,2),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'AMIE-Ex'
            bX = [-10 -5 0 5 10]; % Ex
            strVar = display_polar_AMIE_Ex(data_plot.AMIE(:,3),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);
        case 'AMIE-Ey'            
            bX = [-10 -5 0 5 10]; % Ey
            strVar = display_polar_AMIE_Ey(data_plot.AMIE(:,4),map_color,strArrow,strArrow1,x,y,Slope,pC,bX*Factor);               
    end
    
    if ~strcmp(param,'None')
        set(gca,'Visible','on','yLim',[-51 51],'xColor','white','yColor','white');
        set(gca,'ButtonDownFcn',{@mouse_click_polar,strVar});
        k = find(data_plot.hap == gca);
        if ~isempty(k)
            data_plot.list_polar(k) = {strVar};
            data_plot.polar_Factor(k) = Factor;
            data_plot.polar_plot_type(k) = pC;
        end
    end
    
%     if gca == hap(4)
%         text(20,-60,data_user.strTime(1:25))
%     end        
    
    set(hbStatusBar,'String','Done plotting the data...');
    pause(2)
end


function mouse_click_global(source,eventdata,strVar)
global data_plot hbStatusBar teD ttxD FS

param = get(gcf,'SelectionType');        
        
switch param
    case 'alt'   
        ptl = get(gcf,'CurrentPoint');
        xl = ptl(1,1); yl = ptl(1,2);
        ttxD = uicontrol(gcf,'Style', 'text','String','Factor',...
                  'Units','normalized','Position',[xl yl+0.06 0.04 0.02],...
                  'BackgroundColor','white','HorizontalAlignment','left'); 
        teD = uicontrol(gcf,'Style', 'edit','String','1',...
                  'Units','normalized','Position',[xl+0.04 yl+0.061 0.029 0.02],...
                  'BackgroundColor','white','HorizontalAlignment','left');
        List_items = {'None','Asc.','Des.','Asc./Des.'};
        tl = uicontrol(gcf,'Style', 'listbox','String',List_items,...
                  'Units','normalized','Position',[xl yl 0.07 0.06],'Visible','on',...
                  'BackgroundColor','white');
        set(tl,'Callback',@list_callback_global); 

    case 'open'
        set(hbStatusBar,'String','Plotting the data...');
        pause(2)
        
        % generate a plotting figure
        PP = [0 0.05 0.6 0.4];
        hfs = figure('Units','normalized','Position',PP,'Name','Plotter',...
                     'NumberTitle','off','Color','white');

        % generate the axes
        top = 0.15; bottom = 0.15; height = 1-top-bottom;
        left = 0.1; right = 0.05; width = 1-left-right;
        hax = axes('Position',[left bottom width height], ...
                   'FontSize',FS,'Visible','on');
        
        str6 = data_plot.str6;        
        latD = data_plot.latD;        
        Slope_g = data_plot.Slope_g;
        t = data_plot.t;
        
        Den_both = data_plot.Nd(:,1)*10^11;
        Den_asc = Den_both; k = find(Slope_g<0); Den_asc(k) = NaN;
        Den_des = Den_both; k = find(Slope_g>0); Den_des(k) = NaN;
        bX = [0 0.5 1 1.5];
        
        cla(gca,'reset');
        switch strVar
            case 'Asc.'            
                strVar = global_Nd(Den_asc,t,latD,Slope_g,strVar,str6,bX);
            case 'Des.'
                strVar = global_Nd(Den_des,t,latD,Slope_g,strVar,str6,bX);
            case 'Asc./Des.'
                strVar = global_Nd(Den_both,t,latD,Slope_g,strVar,str6,bX);
        end        
        set(gca,'ButtonDownFcn',{@mouse_click_global,strVar});
        
        set(hbStatusBar,'String','Done plotting the data...');
        pause(2)  
end


function list_callback_global(source,eventdata)
global data_plot hbStatusBar teD ttxD

if strcmp(get(gcf,'SelectionType'),'open')
    set(hbStatusBar,'String','Plotting the data...');
    pause(2)

    file_list = get(source,'String');
    index_selected = get(source,'Value');
    param = file_list{index_selected};        
    Factor = str2num(get(teD,'String'));    
    delete(source)
    delete(ttxD)
    delete(teD)
     
    str6 = data_plot.str6;
    latD = data_plot.latD;
    Slope_g = data_plot.Slope_g;
    t = data_plot.t;
    
    Den_both = data_plot.Nd(:,1)*10^11;
    Den_asc = Den_both; k = find(Slope_g<0); Den_asc(k) = NaN;
    Den_des = Den_both; k = find(Slope_g>0); Den_des(k) = NaN;
    bX = [0 0.5 1 1.5];
       
    if ~strcmp(param,'None')
        cla(gca,'reset');        
    end
    
    switch param        
        case 'Asc.'
            strVar = global_Nd(Den_asc,t,latD,Slope_g,param,str6,bX*Factor);
        case 'Des.'
            strVar = global_Nd(Den_des,t,latD,Slope_g,param,str6,bX*Factor);
        case 'Asc./Des.'
            strVar = global_Nd(Den_both,t,latD,Slope_g,param,str6,bX*Factor);
    end 
    
    if ~strcmp(param,'None')
        set(gca,'ButtonDownFcn',{@mouse_click_global,strVar});
        data_plot.list_global(1) = {strVar};
        data_plot.global_Factor(1) = Factor;        
    end
    
    set(hbStatusBar,'String','Done plotting the data...');
    pause(2)
end


function mouse_click_global_Sz(source,eventdata,strVar)
global data_plot hbStatusBar teS ttxS FS

param = get(gcf,'SelectionType');        
        
switch param
    case 'alt'   
        ptl = get(gcf,'CurrentPoint');
        xl = ptl(1,1); yl = ptl(1,2);
        ttxS = uicontrol(gcf,'Style', 'text','String','Factor',...
                  'Units','normalized','Position',[xl yl+0.06 0.04 0.02],...
                  'BackgroundColor','white','HorizontalAlignment','left'); 
        teS = uicontrol(gcf,'Style', 'edit','String','1',...
                  'Units','normalized','Position',[xl+0.04 yl+0.061 0.029 0.02],...
                  'BackgroundColor','white','HorizontalAlignment','left');
        List_items = {'None','Asc.','Des.','Asc./Des.'};
        tl = uicontrol(gcf,'Style', 'listbox','String',List_items,...
                  'Units','normalized','Position',[xl yl 0.07 0.06],'Visible','on',...
                  'BackgroundColor','white');
        set(tl,'Callback',@list_callback_global_Sz); 

    case 'open'
        set(hbStatusBar,'String','Plotting the data...');
        pause(2)
        
        % generate a plotting figure
        PP = [0 0.05 0.6 0.4];
        hfs = figure('Units','normalized','Position',PP,'Name','Plotter',...
                     'NumberTitle','off','Color','white');

        % generate the axes
        top = 0.15; bottom = 0.15; height = 1-top-bottom;
        left = 0.1; right = 0.05; width = 1-left-right;
        hax = axes('Position',[left bottom width height], ...
                   'FontSize',FS,'Visible','on');
        
        str6 = data_plot.str6;
        lat = data_plot.lat;
        Slope = data_plot.Slope_g_Sz;
        t = data_plot.t;
        
        Sz_both = data_plot.Poy(:,4);
        Sz_asc = Sz_both; k = find(Slope<0); Sz_asc(k) = NaN;
        Sz_des = Sz_both; k = find(Slope>0); Sz_des(k) = NaN;
        bX = [0 5 10 15 20];
        
        cla(gca,'reset');
        switch strVar
            case 'Asc.'            
                strVar = global_Nd(Sz_asc,t,lat,Slope,strVar,str6,bX);
            case 'Des.'
                strVar = global_Nd(Sz_des,t,lat,Slope,strVar,str6,bX);
            case 'Asc./Des.'
                strVar = global_Nd(Sz_both,t,lat,Slope,strVar,str6,bX);
        end        
        set(gca,'ButtonDownFcn',{@mouse_click_global_Sz,strVar});
        
        set(hbStatusBar,'String','Done plotting the data...');
        pause(2)  
end

function mouse_click_loc(source,eventdata,strVar)
global data_plot hbStatusBar teS ttxS FS

param = get(gcf,'SelectionType');        
        
switch param
    case 'alt'   
        ptl = get(gcf,'CurrentPoint');
        xl = ptl(1,1); yl = ptl(1,2);
        ttxS = uicontrol(gcf,'Style', 'text','String','Factor',...
                  'Units','normalized','Position',[xl yl+0.06 0.04 0.02],...
                  'BackgroundColor','white','HorizontalAlignment','left'); 
        teS = uicontrol(gcf,'Style', 'edit','String','1',...
                  'Units','normalized','Position',[xl+0.04 yl+0.061 0.029 0.02],...
                  'BackgroundColor','white','HorizontalAlignment','left');
        List_items = {'None','Asc.','Des.','Asc./Des.'};
        tl = uicontrol(gcf,'Style', 'listbox','String',List_items,...
                  'Units','normalized','Position',[xl yl 0.07 0.06],'Visible','on',...
                  'BackgroundColor','white');
        set(tl,'Callback',@list_callback_global_Sz); 

    case 'open'
        set(hbStatusBar,'String','Plotting the data...');
        pause(2)
        
        % generate a plotting figure
        PP = [0 0.05 0.6 0.4];
        hfs = figure('Units','normalized','Position',PP,'Name','Plotter',...
                     'NumberTitle','off','Color','white');

        % generate the axes
        top = 0.15; bottom = 0.15; height = 1-top-bottom;
        left = 0.1; right = 0.05; width = 1-left-right;
        hax = axes('Position',[left bottom width height], ...
                   'FontSize',FS,'Visible','on');
        
        str6 = data_plot.str6;
        t = data_plot.t;

        cla(gca,'reset');
        switch strVar
            case 'MLT'
            strVar = plot_MLT(data_plot.MLT_P,data_plot.strDate,data_plot.strSat,t,LW,FS,FS1,str6);
    
            case 'MLAT110'
            strVar = plot_MLAT110(data_plot.MLAT110_P,t,LW,FS,str6);
    
            case 'GLon'
            strVar = plot_GLON(data_plot.Glon_P,t,LW,FS,str6);

            case 'GLat'
            strVar = plot_GLAT(data_plot.Glat_P,t,LW,FS,str6);
    
            case 'GLon110'
            strVar = plot_GLON110(data_plot.Glon110,t,LW,FS,str6);

            case 'GLat110'
            strVar = plot_GLAT110(data_plot.Glat110,t,LW,FS,str6);
    
            case 'MLON110'
            strVar = plot_MLON110(data_plot.MLON110,t,LW,FS,str6);
    
        end     
        set(gca,'ButtonDownFcn',{@mouse_click_loc,strVar});
        
        set(hbStatusBar,'String','Done plotting the data...');
        pause(2)  
end

function list_callback_global_Sz(source,eventdata)
global data_plot hbStatusBar teS ttxS

if strcmp(get(gcf,'SelectionType'),'open')
    set(hbStatusBar,'String','Plotting the data...');
    pause(2)

    file_list = get(source,'String');
    index_selected = get(source,'Value');
    param = file_list{index_selected};        
    Factor = str2num(get(teS,'String'));           
     
    str6 = data_plot.str6;
    lat = data_plot.lat;
    Slope = data_plot.Slope_g_Sz;
    t = data_plot.t;
    
    Sz_both = data_plot.Poy(:,4);
    Sz_asc = Sz_both; k = find(Slope<0); Sz_asc(k) = NaN;
    Sz_des = Sz_both; k = find(Slope>0); Sz_des(k) = NaN;
    bX = [0 5 10 15 20];
       
    if ~strcmp(param,'None')
        cla(gca,'reset');        
    end
        
    switch param        
        case 'Asc.'
            strVar = global_Sz(Sz_asc,t,lat,Slope,param,str6,bX*Factor);            
        case 'Des.'            
            strVar = global_Sz(Sz_des,t,lat,Slope,param,str6,bX*Factor);            
        case 'Asc./Des.'                                    
            strVar = global_Sz(Sz_both,t,lat,Slope,param,str6,bX*Factor);            
    end     
    
    if ~strcmp(param,'None')
        set(gca,'ButtonDownFcn',{@mouse_click_global_Sz,strVar});
        data_plot.list_global_Sz(1) = {strVar};
        data_plot.global_Factor_Sz(1) = Factor;        
    end
    
    delete(source)
    delete(ttxS)
    delete(teS)
    set(hbStatusBar,'String','Done plotting the data...');
    pause(2)
end


%-----------
% draw polar
%-----------
function draw_polar()
global FS FS1
color_grid = [0.75 0.75 0.75];
phi = linspace(0,2*pi,10000); 

plot(50*cos(phi), 50*sin(phi),'LineStyle','-','Color',color_grid,...
     'LineWidth',1);
axis equal; hold on; axis off
plot(40*cos(phi), 40*sin(phi),':','Color',color_grid);
plot(30*cos(phi), 30*sin(phi),':','Color',color_grid);
plot(20*cos(phi), 20*sin(phi),':','Color',color_grid);
plot(10*cos(phi), 10*sin(phi),':','Color',color_grid);

r_text = 55; th_text = 3*pi/2;
[x,y] = pol2cart(th_text,r_text);
text(x-1,y-0.5,'0','FontSize',FS,'Color','r')

r_text = 55; th_text = 7*pi/4;
[x,y] = pol2cart(th_text,r_text);
text(x-1,y,'3','FontSize',FS,'Color','r')

r_text = 55; th_text = 0*pi/2;
[x,y] = pol2cart(th_text,r_text);
text(x-2,y,'6','FontSize',FS,'Color','r')

r_text = 55; th_text = pi/4;
[x,y] = pol2cart(th_text,r_text);
text(x-1,y,'9','FontSize',FS,'Color','r')

r_text = 55; th_text = 1*pi/2;
[x,y] = pol2cart(th_text,r_text);
text(x-4,y-0.2,'12','FontSize',FS,'Color','r')

r_text = 55; th_text = 3*pi/4;
[x,y] = pol2cart(th_text,r_text);
text(x-5,y,'15','FontSize',FS,'Color','r')

r_text = 55; th_text = 2*pi/2;
[x,y] = pol2cart(th_text,r_text);
text(x-5,y,'18','FontSize',FS,'Color','r')

r_text = 55; th_text = 5*pi/4;
[x,y] = pol2cart(th_text,r_text);
text(x-5,y,'21','FontSize',FS,'Color','r')

% line([0 0],[-50 50],'Color',[0.7 0.7 0.7],'LineWidth',1.5); 
% line([-50 50],[0 0],'Color',[0.7 0.7 0.7],'LineWidth',1.5)
for i = 1:24
    th = (i-1)*pi/12;
    r_min = 10; r_max = 50;
    line([r_min*cos(th) r_max*cos(th)],[r_min*sin(th) r_max*sin(th)],...
         'Color',color_grid,'LineStyle',':')
end

text( 6,-5,'80^o','FontSize',FS1,'Color','r');
text(16,-5,'70^o','FontSize',FS1,'Color','r');
text(26,-5,'60^o','FontSize',FS1,'Color','r');
text(36,-5,'50^o','FontSize',FS1,'Color','r');
    

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


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


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


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


% --- Executes during object deletion, before destroying properties.
function All_uipanel_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to All_uipanel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------


%-------------------------
% New GUI Components (lmk)
%-------------------------

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


% --- Executes when All_uipanel is resized.
function All_uipanel_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to All_uipanel (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 saveButton.
function saveButton_Callback(hObject, eventdata, handles)
% hObject    handle to saveButton (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 qlCB.
function qlCB_Callback(hObject, eventdata, handles)
% hObject    handle to qlCB (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 qlCB

% --- Executes on button press in baselineLinearCB.
function baselineLinearCB_Callback(hObject, eventdata, handles)
% hObject    handle to baselineLinearCB (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 baselineLinearCB


% --- Executes on selection change in locList.
function locList_Callback(hObject, eventdata, handles)
% hObject    handle to locList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns locList contents as cell array
%        contents{get(hObject,'Value')} returns selected item from locList
contents = get(hObject,'String');
selected = get(hObject,'UserData'); %list of selected indices is here
selind = get(hObject,'Value');
selstr = contents{selind};

flag = 0;
for i = 1:length(selected)
    if(strcmp(contents{selected(i)}, selstr)), flag = 1; end
end
if(flag) %deselect
   p = find(selected == selind);
   selected(p) = -1;
   selected = sort(selected);
   selected = selected(2:end);
   contents{selind} = strrep(contents{selind}, '*', '');
else %select
   selected(length(selected)+1) = selind;
   selected = sort(selected);
   contents{selind} = strcat('*',contents{selind},'*');
end
display(selected);
set(hObject,'String',contents);
set(hObject,'UserData',selected);
guidata(hObject, handles); %update handles
  
% --- Executes during object creation, after setting all properties.
function locList_CreateFcn(hObject, eventdata, handles)
% hObject    handle to locList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox 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 selection change in polarList.
function polarList_Callback(hObject, eventdata, handles)
% hObject    handle to polarList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns polarList contents as cell array
%        contents{get(hObject,'Value')} returns selected item from polarList
contents = get(hObject,'String');
selected = get(hObject,'UserData'); %list of selected indices is here
selind = get(hObject,'Value');
selstr = contents{selind};

flag = 0;
for i = 1:length(selected)
    if(strcmp(contents{selected(i)}, selstr)), flag = 1; end
end
if(flag) %deselect
   p = find(selected == selind);
   selected(p) = -1;
   selected = sort(selected);
   selected = selected(2:end);
   contents{selind} = strrep(contents{selind}, '*', '');
else %select
   selected(length(selected)+1) = selind;
   selected = sort(selected);
   contents{selind} = strcat('*',contents{selind},'*');
end
display(selected);
set(hObject,'String',contents);
set(hObject,'UserData',selected);
guidata(hObject, handles); %update handles

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

% Hint: listbox 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 selection change in cartList.
function cartList_Callback(hObject, eventdata, handles)
% hObject    handle to cartList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns cartList contents as cell array
%        contents{get(hObject,'Value')} returns selected item from cartList
contents = get(hObject,'String');
selected = get(hObject,'UserData'); %list of selected indices is here
selind = get(hObject,'Value');
selstr = contents{selind};

flag = 0;
for i = 1:length(selected)
    if(strcmp(contents{selected(i)}, selstr)), flag = 1; end
end
if(flag) %deselect
   p = find(selected == selind);
   selected(p) = -1;
   selected = sort(selected);
   selected = selected(2:end);
   contents{selind} = strrep(contents{selind}, '*', '');
else %select
   selected(length(selected)+1) = selind;
   selected = sort(selected);
   contents{selind} = strcat('*',contents{selind},'*');
end
display(selected);
set(hObject,'String',contents);
set(hObject,'UserData',selected);
guidata(hObject, handles); %update handles

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

% Hint: listbox 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 vswCB.
function vswCB_Callback(hObject, eventdata, handles)
% hObject    handle to vswCB (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 vswCB

tog = get(hObject,'Value');
if(tog), handles.data_user.vswflag = 1; 
else handles.data_user.vswflag = 0;    
end

% --- Executes on button press in qlCB2.
function qlCB2_Callback(hObject, eventdata, handles)
% hObject    handle to qlCB2 (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 qlCB2


