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

% Last Modified by GUIDE v2.5 24-Apr-2018 17:53:38

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

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


set(handles.pushbutton_rf,'Enable','off');


%% options_off(hObject,eventdata,handles);



% Update handles structure
guidata(hObject, handles);

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


function options_off(hObject,eventdata,handles)

set(handles.uipanel_save,'Visible','on');


% --- Outputs from this function are returned to the command line.
function varargout = rainflow_NASGRO_LB_output_batch_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


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


num_eng=get(handles.listbox_numerical_engine,'Value');

k=get(handles.listbox_method,'Value');
 
if(k==1)
  FS=get(handles.edit_input_array,'String');
  THM=evalin('base',FS);   
else
  THM=getappdata(0,'THM');
end

THM=fix_size(THM);

disp(' ref 2 ');
sz=size(THM);

if(sz(2)==1)
    y=THM(:,1);
else    
    y=THM(:,2);
end

iu=get(handles.listbox_input_unit,'Value');
ou=get(handles.listbox_output_unit,'Value');


if(iu==1)
    YS='Stress (psi)';
end
if(iu==2)
    YS='Stress (ksi)';
end
if(iu==3)
    YS='Stress (Pa)';
end
    
Pa_per_psi=6891.2;
psi_per_Pa=1/Pa_per_psi;

if(iu==1) % psi 
    if(ou==2) % ksi
        y=y/1000;
    end
    if(ou==3) % Pa
        y=y*Pa_per_psi;
    end    
end    
if(iu==2) % ksi
    if(ou==1) % psi
        y=y*1000;        
    end
    if(ou==3) % Pa
        y=y*1000*Pa_per_psi; 
    end      
end        
if(iu==3) % Pa
    if(ou==1) % psi
        y=y*psi_per_Pa;
    end
    if(ou==2) % ksi
        y=y*psi_per_Pa/1000;
    end    
end    


    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

ndf=1;

if(num_eng==1)
    [range_cycles,amean,amax,amin,BIG]=vibrationdata_rainflow_mean_max_min_function(THM,YS,ndf);    
 
    
    amp_cycles=[range_cycles(:,1)/2 range_cycles(:,2)];
    amean=fix_size(amean);
    amp_mean_cycles=[range_cycles(:,1)/2 amean range_cycles(:,2)];
 
    amax=fix_size(amax);
    amin=fix_size(amin);
    
    range_cycles_max_min=[range_cycles(:,1) range_cycles(:,2) amax amin ];

  
else
%
    disp(' '); 
    disp(' Calculating... ');
    disp(' '); 
    
    dchoice=-1.; % needs to be double
%
    exponent=1;
% 
    [L,C,AverageAmp,MaxAmp,MinMean,AverageMean,MaxMean,MinValley,MaxPeak,D,ac1,ac2,amean,amax,amin,cL]...
                                         =rainflow_mean_max_min_mex(y,dchoice,exponent);
%
    sz=size(ac1);
    if(sz(2)>sz(1))
        ac1=ac1';
        ac2=ac2';
    end
%
    ncL=int64(cL);
%
    amp_cycles=[ ac1(1:ncL) ac2(1:ncL) ];
    amean=fix_size(amean);
    amin=fix_size(amin);
    amax=fix_size(amax);    

%      

    amp_mean_cycles=[ ac1(1:ncL) amean(1:ncL) ac2(1:ncL) ];
    range_cycles=[2*amp_cycles(:,1) amp_cycles(:,2)];
    range_cycles_max_min=[2*amp_cycles(:,1) amp_cycles(:,2) amax(1:ncL) amin(1:ncL) ];    

    clear ac1;
    clear ac2;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%
        figure(1);
%
        sz=size(THM);
        
        if(sz(2)==2)
            plot(THM(:,1),THM(:,2));
            xlabel('Time (sec)');
        else
            plot(THM(:,1));            
        end    

%        
        ylabel(YS)
        grid on;
%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

setappdata(0,'amp_cycles',amp_cycles); 
setappdata(0,'amp_mean_cycles',amp_mean_cycles); 
setappdata(0,'range_cycles',range_cycles);
setappdata(0,'range_cycles_max_min',range_cycles_max_min);  

set(handles.uipanel_save,'Visible','on');

assignin('base', 'range_cycles', range_cycles);

sz=size(range_cycles);

if(sz(1)<=14)
    
   range_cycles=sortrows(range_cycles,1);
   range_cycles=flipud(range_cycles);

   disp(' ');
   disp('     Range  Cycles ');
    
   for i=1:sz(1) 
        out1=sprintf(' %8.4g   %g',range_cycles(i,1),range_cycles(i,2));
        disp(out1);
   end
   
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

header=get(handles.edit_header,'String');
 
FileName = uiputfile;

% csvwrite(FileName,columnname);

fid = fopen(FileName,'wt');
fprintf(fid, '%s \n',header);

sz=size(range_cycles);

for j=1:sz(1)
%
    nc=range_cycles_max_min(j,2);
     p=range_cycles_max_min(j,4);
     q=range_cycles_max_min(j,3);
       
     fprintf(fid,'%4.1f, %8.4f, %8.4f, %8.4f, %8.4f, %8.4f, %8.4f, %8.4f, %8.4f \n',...
                                                                nc,p,q,p,q,p,q,p,q);                                         
end
%

fclose(fid);

msgbox('Save Complete'); 


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

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

n=get(handles.listbox_output_type,'Value');

if(n==1)
    data=getappdata(0,'amp_cycles'); 
end
if(n==2)
    data=getappdata(0,'amp_mean_cycles');     
end
if(n==3)
    data=getappdata(0,'range_cycles');  
end
if(n==4)
    data=getappdata(0,'range_cycles_max_min');  
end

output_name=get(handles.edit_output_array,'String');
assignin('base', output_name, data);

h = msgbox('Save Complete'); 



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

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


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

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


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

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

options_off(hObject,eventdata,handles);

set(handles.edit_input_array,'String',' ');

set(handles.text_input_array_name,'Visible','on');
set(handles.edit_input_array,'Visible','on');


if(n==1)
   set(handles.edit_input_array,'enable','on') 
else
    
   set(handles.text_input_array_name,'Visible','off');
   set(handles.edit_input_array,'Visible','off');    
   set(handles.edit_input_array,'enable','off')
   
   [filename, pathname] = uigetfile('*.*');
   filename = fullfile(pathname, filename);
   fid = fopen(filename,'r');
   THM = fscanf(fid,'%g %g',[2 inf]);
   THM=THM';
    
   setappdata(0,'THM',THM);
end



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

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


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

BIG=getappdata(0,'BIG');
columnname=getappdata(0,'columnname');

FileName = uiputfile;

% csvwrite(FileName,columnname);

fid = fopen(FileName,'wt');
fprintf(fid, '%s,',columnname{:});
fprintf(fid, '\n');
fclose(fid);

dlmwrite (FileName, BIG, '-append');

h = msgbox('Save Complete'); 




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

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


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

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


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

b=str2num(get(handles.edit_exponent,'String'));

range_cycles=getappdata(0,'range_cycles');  

D=0;

sz=size(range_cycles);

for i=1:sz(1)
    D=D+range_cycles(i,2)*( 0.5*range_cycles(i,1) )^b;
end    

string=sprintf('%8.4g',D);

set(handles.edit_damage_result,'String',string);


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

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


% --- Executes during object creation, after setting all properties.
function edit_damage_result_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_damage_result (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 key press with focus on edit_input_array and none of its controls.
function edit_input_array_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to edit_input_array (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)

options_off(hObject,eventdata,handles);


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

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


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

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


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

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


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

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


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

handles.s=vibrationdata_miners;    
 
set(handles.s,'Visible','on'); 


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

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


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

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


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

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


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

   disp('  ');
   disp(' * * * * *');
   disp('  ');

   [filename, pathname] = uigetfile('*.*');
   filename = fullfile(pathname, filename);
 
   setappdata(0,'pathname',pathname);
   
   try
      fid = fopen(filename,'r');
   catch
      warndlg('File not opened'); 
      return; 
   end
   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
   
   disp('  ');   
 
   file_array = textscan(fid,'%s','Delimiter','\n');
   fclose(fid);
    
    
    kv=cellfun(@length,file_array);
 
    out1=sprintf(' %d number of lines',kv);
    disp(out1);    
    
    
   setappdata(0,'file_array',file_array);
   setappdata(0,'kv',kv);
    

   max_amp=zeros(kv,1);
   min_amp=zeros(kv,1);
   abs_amp=zeros(kv,1);
   
   for i=1:kv
       
        filename=file_array{1}{i};
 
        try
            fff = fullfile(pathname, filename);
            fid = fopen(fff,'r');
            disp(filename);
        catch
            warndlg('File not opened'); 
            return; 
        end    
   
       
        try
        
            THM = fscanf(fid,'%g %g',[2 inf]);
            THM=THM';       
        
            max_amp(i)=max(THM(:,2));
            min_amp(i)=min(THM(:,2)); 
            abs_amp(i)=max(abs(THM(:,2)));
            
        catch
            
            warndlg(' Read error.  File must have two columns');
            return;
            
        end
        
        fclose(fid);
            
   end

   amax=max(max_amp);
   amin=min(min_amp);
   aabs=max(abs_amp);
   
   setappdata(0,'amax',amax);
   setappdata(0,'amin',amin);   
   setappdata(0,'aabs',aabs);
   

   out1=sprintf('\n  max=%8.4g   min=%8.4g   max abs=%8.4g \n',amax,amin,aabs);
   disp(out1);
   
   msgbox('Read complete');
   
   disp('Read complete');
   
   set(handles.pushbutton_rf,'Enable','on');


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

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


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

threshold=str2num(get(handles.edit_threshold,'String'));

num_eng=get(handles.listbox_numerical_engine,'Value');

pathname=getappdata(0,'pathname');
file_array=getappdata(0,'file_array');
kv=getappdata(0,'kv');
amax=getappdata(0,'amax');
amin=getappdata(0,'amin');   
aabs=getappdata(0,'aabs');

ns=get(handles.listbox_scaling,'Value');

scale=1;


if(ns==1)
    scale=aabs;
end
if(ns==2)
    scale=amax;
end
if(ns==3)
    scale=amin;
end

%%%


for i=1:kv
       
        filename=file_array{1}{i};
 
        try
            fff = fullfile(pathname, filename);
            fid = fopen(fff,'r');
            disp(filename);
        catch
            warndlg('File not opened'); 
            return; 
        end    
          
        try
        
            THM = fscanf(fid,'%g %g',[2 inf]);
            THM=THM'; 
            y=THM(:,2);
        
        catch
            
            warndlg(' Read error.  File must have two columns');
            return;
            
        end
        
        fclose(fid);
        
%%%%%%%%%%%%%%%%%        

        if(num_eng==1)
            [range_cycles,amean,amax,amin,BIG]=vibrationdata_rainflow_mean_max_min_function_np(THM);    
 
    
            amp_cycles=[range_cycles(:,1)/2 range_cycles(:,2)];
            amean=fix_size(amean);
            amp_mean_cycles=[range_cycles(:,1)/2 amean range_cycles(:,2)];
 
            amax=fix_size(amax);
            amin=fix_size(amin);
    
            range_cycles_max_min=[range_cycles(:,1) range_cycles(:,2) amax amin ];

        else
%
            disp(' '); 
            disp(' Calculating... ');
            disp(' '); 
    
            dchoice=-1.; % needs to be double
%
            exponent=1;
% 
            [L,C,AverageAmp,MaxAmp,MinMean,AverageMean,MaxMean,MinValley,MaxPeak,D,ac1,ac2,amean,amax,amin,cL]...
                                         =rainflow_mean_max_min_mex(y,dchoice,exponent);
%
            sz=size(ac1);
            if(sz(2)>sz(1))
                ac1=ac1';
                ac2=ac2';
            end
%
            ncL=int64(cL);
%
            amp_cycles=[ ac1(1:ncL) ac2(1:ncL) ];
%%            amean=fix_size(amean);
            amin=fix_size(amin);
            amax=fix_size(amax);    

%      
%%%            amp_mean_cycles=[ ac1(1:ncL) amean(1:ncL) ac2(1:ncL) ];
            range_cycles=[2*amp_cycles(:,1) amp_cycles(:,2)];
            range_cycles_max_min=[2*amp_cycles(:,1) amp_cycles(:,2) amax(1:ncL) amin(1:ncL) ];    

            clear ac1;
            clear ac2;
%
        end

        % csvwrite(FileName,columnname);

        filename_out=sprintf('%s.lb',filename);
        filename_out=strrep(filename_out,'.dat','');
        filename_out=strrep(filename_out,'.txt','');  
        
        sss{i,:}=filename_out;
        
        disp(filename_out);
        
        ffvv = fullfile(pathname, filename_out);
        
        header=sprintf('%s  scale=%8.4g',filename,scale);
        
        fid = fopen(ffvv,'wt');
        fprintf(fid, '%s \n',header);

        sz=size(range_cycles);

        for j=1:sz(1)
%
            nc=range_cycles_max_min(j,2);
            p=range_cycles_max_min(j,4);
            q=range_cycles_max_min(j,3);
     
            if(abs(p-q)>threshold)
         
                p=p/scale;
                q=q/scale;
       
                fprintf(fid,'%4.1f, %8.4f, %8.4f, %8.4f, %8.4f, %8.4f, %8.4f, %8.4f, %8.4f \n',...
                                                                nc,p,q,p,q,p,q,p,q);                                                                                                          
            end                                                       
        end
%
        fclose(fid);

end

disp(' ');
disp('Output files: ');

for i=1:kv
    disp(sss{i,:});
end

disp(' ');
out1=sprintf(' scale=%8.4g',scale);
disp(out1);

msgbox('Calculation Complete');



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

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


% --- Executes during object creation, after setting all properties.
function edit_threshold_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_threshold (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
