#!/usr/bin/octave -qf
output_precision(10)
#vypocet sumu pozadia
load "pozadie.dat"
pozadie = pozadie(:,3);
p = mean(pozadie)

#######################################
#hodnoty ktore sa menia pre ine hviezdy
#datovy subor( uprava velkosti a matice M)

# bllac
load "bl.dat"

#velkost stvorca
velkost = sqrt(size(bl(:,1),1));
n = velkost**2;

#matica s odpocitanym sumom
M = [bl(:,1) bl(:,2)  bl(:,3) - p];

#tazisko bllac z gaie
x0 = 9.77;
y0 = 9.04;
#katalogova magnituda objektu
mag = 13.60;
#######################################

#definovane premenne
sigmax2 = 0;
sigmay2 = 0;
sigmax = 0;
sigmay = 0;
sigma = 0;
suma = 0;
sumag = 0;
#prepocet toku v danom filtri R
d = 3e8*140e-9/((666e-9)**2);
#f0 pre R filter
f0 = 3.08e-26;

#vypocet matice w(i,j)
c = M(:,3);
l=1;
for i=1:velkost
for j=1:velkost
w(i,j)=c(l);
l=l+1;
endfor
endfor

#sigma
suma = sum(c)
for i=1:velkost
for j=1:velkost
sigmax2=sigmax2+(w(i,j))*(i-x0)**2;
sigmay2=sigmay2+(w(i,j))*(j-y0)**2;
endfor
endfor

sigmax=sqrt(sigmax2/suma);
sigmay=sqrt(sigmay2/suma);
sigma=(sigmax+sigmay)/2

#vypocet matice g(i,j)
for i=1:velkost
for j=1:velkost
g(i,j)=exp(-((i-x0)**2+(j-y0)**2)/(2*sigma**2));
endfor
endfor

#sucet nasobkov matic w a g
sumaSg=0;
for i=1:velkost
for j=1:velkost
sumaSg=sumaSg+w(i,j)*g(i,j);
endfor
endfor

#sucet countov s odcitanym pozadim
sumaS=0;
sumaS = suma;


#sucet nasobkov zloziek g so sebou
sumagna2=0;
for i=1:velkost
for j=1:velkost
sumagna2=sumagna2+g(i,j)**2;
endfor
endfor

#sucet zloziek matice g
sumag=sum(sum(g));

#vypocet parametrov 
G0=(n*sumaSg-sumaS*sumag)/(n*sumagna2-sumag**2)
Bp=(sumaS-G0*sumag)/n

#celkovy tok
Fp = 2*pi*G0*sigma**2

#tok na jednotkovu plochu za 1 sec
T = 60;
S = pi*0.31**2;
C = Fp/(T*S)

#hustota toku
f = f0*10**(-0.4*mag)

#hustota pre filter
F = d*f

#priemer pomeru medzi inštrumentalnymi a kalibracnymi tokmi
r=F/C


