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

% Last Modified by GUIDE v2.5 24-Apr-2016 16:38:57

% Begin initialization code - DO NOT EDIT
if nargin == 0  % LAUNCH GUI
%    close all
    fig = openfig(mfilename,'reuse');

    % Generate a structure of handles to pass to callbacks, and store it.
    handles = guihandles(fig);
    guidata(fig, handles);

    if nargout > 0
        varargout{1} = fig;
    end
elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK

    try
        if (nargout)
            [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
        else
            feval(varargin{:}); % FEVAL switchyard
        end
    catch
        disp(lasterr);
    end

end
end
% End initialization code - DO NOT EDIT



% --------------------------------------------------------------------
function varargout = pushbutton1_Callback(h, eventdata, handles, varargin)
format long
% Default input file
def_input_file = get(handles.edit12,'string');  % default input data filename plus path
% Specify input file
[filename1, pathname1]=uigetfile({'*.*','All files (*.*)'},'Import data [must conform to correct format]',def_input_file);
% opens dialog in present working directory (pwd)
myfile1=fullfile(pathname1, filename1);
handles.filename1 = filename1;  % filename only
handles.myfile1 = myfile1;  % filename plus path
set(handles.edit11,'string',[handles.filename1])  % filename only
guidata(gcbo,handles) % store the changes
set(handles.edit12,'string',[handles.myfile1])  % filename including path
guidata(gcbo,handles) % store the changes

M= csvread(get(handles.edit12,'string'));
dat_t = M(:,1);  % time, units?
dat_1 = M(:,2);  % Stokes vector S1
dat_2 = M(:,3);  % Stokes vector S2
dat_3 = M(:,4);  % Stokes vector S3
dat_4 = M(:,9);  % DOP

set(handles.edit6,'string',0)
guidata(gcbo,handles)
set(handles.edit7,'string',[dat_t(length(dat_t))])
guidata(gcbo,handles)
set(handles.text9,'string',0)
guidata(gcbo,handles)
set(handles.text10,'string',[dat_t(length(dat_t))])
guidata(gcbo,handles)

x=dat_1;
y=dat_2;
z=dat_3;
%Stokes
figure('Position',[0 0 750 750]);
ax1=subplot(4,1,1);
ax2=subplot(4,1,2);
ax3=subplot(4,1,3);
ax4=subplot(4,1,4);
plot(ax1,dat_t,x)
title(ax1,'Stokes parameter S1')
xlabel(ax1,'t [s]')
plot(ax2,dat_t,y)
title(ax2,'Stokes parameter S2')
xlabel(ax2,'t [s]')
plot(ax3,dat_t,z)
title(ax3,'Stokes parameter S3')
xlabel(ax3,'t [s]')
plot(ax4,dat_t,dat_4)
title(ax4,'DOP')
xlabel(ax4,'t [s]')

end


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
input_filename = get(handles.edit11,'string');  % input data filename
input_file = get(handles.edit12,'string');  % input data filename plus path

% load data from file
M = csvread(input_file);
% Extract data from file
dat_t = M(:,1);  % time, units?
dat_1 = M(:,2);  % Stokes vector S1
dat_2 = M(:,3);  % Stokes vector S2
dat_3 = M(:,4);  % Stokes vector S3
dat_4 = M(:,9);  % DOP

%Stokes
%figure
%ax1=subplot(4,1,1);
%ax2=subplot(4,1,2);
%ax3=subplot(4,1,3);
%ax4=subplot(4,1,4);
%plot(ax1,dat_t,x)
%title(ax1,'Stokes paramater S1')
%plot(ax2,dat_t,y)
%title(ax2,'Stokes paramater S2')
%plot(ax3,dat_t,z)
%title(ax3,'Stokes paramater S3')
%plot(ax4,dat_t,dat_4)
%title(ax4,'DOP')

%Poincare
krok = str2double(get(handles.edit3,'string'));
pauza = str2double(get(handles.edit4,'string'));
if (str2double(get(handles.edit6,'string'))>dat_t(length(dat_t)))
    prvni_cas=dat_t(length(dat_t));
else
    prvni_cas=str2double(get(handles.edit6,'string'));
end

if (str2double(get(handles.edit7,'string'))>dat_t(length(dat_t)))
    posledni_cas=dat_t(length(dat_t));
else
    posledni_cas=str2double(get(handles.edit7,'string'));
end


if (prvni_cas<posledni_cas)
scrsz = get(0, 'ScreenSize');
figure('Position', [1 1 scrsz(3)-100 scrsz(4)-100]); 
bx1=subplot(4,2,[1,3,5,7]);
[X,Y,Z] = sphere(20);
X = X;
Y = Y;
Z = Z;
Hs = mesh(X,Y,Z,'facecolor','white','edgecolor',[0.5 0.5 0.5]);  % set grid facecolor to white
caxis([1.0 1.01]);  % set grid to appear like all one color
alpha(0.70);  % set opacity of sphere to 70%
axis equal;  % make the three axes equal so the ellipsoid looks like a sphere
%set(gcf,'Renderer','opengl');
set(gcf,'Renderer','Zbuffer');
hold on;
% Draw x- and y- and z-axes
Hx = plot3(bx1,[-1.5 1.5], [0 0], [0 0],'k-');
set(Hx,'linewidth',2,'linestyle','-','color','k');
ht_x = text(1.75,0,0,'+S_1','fontweight','bold','fontsize',12,'fontname','arial');
Hy = plot3(bx1,[0 0], [-1.5 1.5], [0 0],'k-');
set(Hy,'linewidth',2,'linestyle','-','color','k');
ht_y = text(0.1,1.6,0,'+S_2','fontweight','bold','fontsize',12,'fontname','arial');
Hz = plot3(bx1,[0 0], [0 0], [-1.5 1.5],'k-');
set(Hz,'linewidth',2,'linestyle','-','color','k');
ht_z = text(-0.05,0,1.35,'+S_3','fontweight','bold','fontsize',12,'fontname','arial');
%ht_lcp = text(-0.05,0.0,1.1,'RCP','fontweight','bold','fontsize',12,'fontname','arial','color','k');
% Draw a bold circle about the equator (2*epsilon = 0)
x_e = (-1:.01:1);
for i = 1:length(x_e)
z_e(i) = 0;
y_e_p(i) = +sqrt(1 - x_e(i)^2);
y_e_n(i) = -sqrt(1 - x_e(i)^2);
end
He = plot3(bx1,x_e,y_e_p,z_e,'k-',x_e,y_e_n,z_e,'k-');
set(He,'linewidth',2,'color','k');
% Draw a bold circle about the prime meridian (2*theta = 0, 180)
y_pm = (-1:.01:1);
for i = 1:length(x_e)
x_pm(i) = 0;
z_pm_p(i) = +sqrt(1 - y_pm(i)^2);
z_pm_n(i) = -sqrt(1 - y_pm(i)^2);
end
Hpm = plot3(bx1,x_pm,y_pm,z_pm_p,'k-',x_pm,y_pm,z_pm_n,'k-');
set(Hpm,'linewidth',2,'color','k');
%najití prvního a posledního vzorku na zobrazení

    for k=1:length(dat_t)
        if (abs(dat_t(k)-prvni_cas)<1)
            zac=k;
        end
        if (abs(dat_t(k)-posledni_cas)<1)
            konec=k;
        end
    end
    l=1;
    for k=zac:konec
        tpp(l)=dat_t(k);
        xpp(l)=dat_1(k);
        ypp(l)=dat_2(k);
        zpp(l)=dat_3(k);
        dop(l)=dat_4(k);
        l=l+1;
    end
    by1=subplot(4,2,2);
    plot(by1,tpp,xpp,'Color', [0.1 0.1 0.1])
    title(by1,'Stokes parameter S1')
    xlabel(by1,'t [s]')
    hold on;
    by2=subplot(4,2,4);
    plot(by2,tpp,ypp,'Color', [0.1 0.1 0.1])
    title(by2,'Stokes parameter S2')
    xlabel(by2,'t [s]')
    hold on;
    by3=subplot(4,2,6);
    plot(by3,tpp,zpp,'Color', [0.1 0.1 0.1])
    title(by3,'Stokes parameter S3')
    xlabel(by3,'t [s]')
    hold on;
    by4=subplot(4,2,8);
    plot(by4,tpp,dop,'Color', [0.1 0.1 0.1])
    title(by4,'DOP')
    xlabel(by4,'t [s]')
    hold on;
    i=1;
    for k=zac:konec
        xp(i)=dat_1(k);
        yp(i)=dat_2(k);
        zp(i)=dat_3(k);
        tp(i)=dat_t(k);
        dopp(i)=dat_4(k);
        if i==1
            plot3(bx1,xp,yp,zp,'bx','MarkerSize',10)
            plot(by1,tp,xp,'b')
            plot(by2,tp,yp,'b')
            plot(by3,tp,zp,'b')
            plot(by4,tp,dopp,'b')
        end
        if (get(handles.checkbox1,'value')==get(handles.checkbox1,'Max'))
           if i>1
                pause(dat_t(k+1)-dat_t(k))
                v1 = [xp(i-1),yp(i-1),zp(i-1)];
                r = norm(v1);
                v2 = [xp(i),yp(i),zp(i)];
                v3 = cross(cross(v1,v2),v1);
                v3 = r*v3/norm(v3); 
                t = linspace(0,atan2(norm(cross(v1,v2)),dot(v1,v2)),1000); 
                vx = v1(1)*cos(t)+v3(1)*sin(t);
                vy = v1(2)*cos(t)+v3(2)*sin(t);
                vz = v1(3)*cos(t)+v3(3)*sin(t);
                plot3(bx1,vx,vy,vz,'b','LineWidth',2)
                plot3(bx1,xp,yp,zp,'bx','MarkerSize',10)
                plot(by1,tp,xp,'-xb','MarkerSize',10)
                plot(by2,tp,yp,'-xb','MarkerSize',10)
                plot(by3,tp,zp,'-xb','MarkerSize',10)
                plot(by4,tp,dopp,'-xb','MarkerSize',10)
           end
        else
            if i>1
                v1 = [xp(i-1),yp(i-1),zp(i-1)];
                r = norm(v1);
                v2 = [xp(i),yp(i),zp(i)];
                v3 = cross(cross(v1,v2),v1);
                v3 = r*v3/norm(v3); 
                t = linspace(0,atan2(norm(cross(v1,v2)),dot(v1,v2)),1000); 
                vx = v1(1)*cos(t)+v3(1)*sin(t);
                vy = v1(2)*cos(t)+v3(2)*sin(t);
                vz = v1(3)*cos(t)+v3(3)*sin(t);
                plot3(bx1,vx,vy,vz,'b','LineWidth',2)
                plot3(bx1,xp,yp,zp,'bx','MarkerSize',10)
                plot(by1,tp,xp,'-xb','MarkerSize',10)
                plot(by2,tp,yp,'-xb','MarkerSize',10)
                plot(by3,tp,zp,'-xb','MarkerSize',10)
                plot(by4,tp,dopp,'-xb','MarkerSize',10)
            end
            if (mod(i,krok)==0)
                pause(pauza/1000)
            end
        end
        i=i+1;
    end
    
else
    msgbox('Počáteční čas je větší nebo roven koncovému času', 'Chyba')
end
end
