function error_code = Interpolate5

error_code = 0;

% Algorithm

% kf_mat vom T10024_40 .ini_datei auslesen = imut_refernz_Feld
x_num_pixel = 27;
y_num_pixel = 27;

fid = fopen('C:\Users\brico\Desktop\testrefgui\Referenzfeld_T10024_40.ini', 'rt');
txt = '';
while ~strcmp(txt, '[Referenzwerte]')
    txt = fgetl(fid);
end
kf_mat = fscanf(fid, '%f = %f', inf);
fclose(fid);
kf_mat = kf_mat(2:2:end);
kf_mat = reshape(kf_mat, x_num_pixel, y_num_pixel);
kf_mat = flipud(kf_mat);

%vom starcheck text_datei abgelesene x und y_array_in_mm, in index_koordinatensystem(xI, yI) umwandeln
fid2 = fopen('C:\Users\brico\Desktop\testkoordgui\Array_T10032.ini');
txt = '';
while ~strcmp(txt, ';Kanal-Nr. = Status(Aktiv/Inaktiv);A/D-Kanal(1-4);IF-KanalNr;X_0;Y_0;K-Faktor;"Bemerkung"')
txt = fgetl(fid2);
end
B = textscan(fid2, '%f = %s%f%f%f%f%f%s', 'delimiter', ';', 'headerlines', 1);
fclose(fid2);
x_array_in_mm = B{5};                                                           
y_array_in_mm = B{6};
property      = B{2};
AD_kanal      = B{3};
IF_kanal      = B{4};
Bemerkung     = B{8};
x_dist_in_mm = 10;
y_dist_in_mm = 10;
num_pos = length(x_array_in_mm);
result =  -ones(num_pos, 1);

% umwandlung alle kordinatensystem(in mm) in index koordinatensystem
T_PB = [0 1 140; 1 0 140; 0 0 1];
T_IP = [1/x_dist_in_mm 0 0; 0 1/y_dist_in_mm 0; 0 0 1];

%xI = (y_array_in_mm + 140)./ x_dist_in_mm; 
%yI = (x_array_in_mm + 140)./ y_dist_in_mm; 

%billinear interpolation : hier sollten alle komponete von xI und yI
%interpoliert werden
for row_id = 1:num_pos
    x_I = T_IP * T_PB * [x_array_in_mm(row_id); y_array_in_mm(row_id); 1];
    cur_x = x_I(1);
    cur_y = x_I(2);
    if cur_x >= 0.5 && cur_x < x_num_pixel+0.5 && cur_y > 0.5 && cur_y < y_num_pixel+0.5 
        x0 = floor(cur_x);
        y0 = floor(cur_y);
        r = cur_x - x0;
        s = cur_y - y0;
        x01 = x0 + 1;
        y01 = y0 + 1;
        if x0 == 0
            x0 = 1;
        elseif x01 ==  x_num_pixel + 1
            x01 = x_num_pixel;
        endif
        if y0 == 0
            y0 = 1;
        elseif y0 + 1 == y_num_pixel+1
            y01 = y_num_pixel;
        endif

          if strcmp(property(row_id), "Aktiv") == 1
             result(row_id, 1) = (1 - r) * (1 - s) * kf_mat(x0, y0) + ...
                                 (1 - r) *      s  * kf_mat(x0, y01) + ...
                                      r  * (1 - s) * kf_mat(x01, y0) + ...
                                      r  *      s  * kf_mat(x01, y01);
           else 
                    result(row_id) = -2;
           endif
     endif
 endfor
 
result(5)
%Ergebnisse in einer text datei schreiben
% pfad_ref = 'G:\Daten\KuE\cvi5prue\pruef\monitor\Co60-1\ArrayCal\Referenzfeld\Referenzfeld_T10024_40.ini';
% pfad_KS  = 'G:\Daten\KuE\cvi5prue\pruef\monitor\Co60-1\ArrayCal\Eigenschaften\Array_T10032.ini';
% fid1 = fopen('result.dat', 'wt');
% fprintf(fid1, ';**********************************************************************\r\n');
% fprintf(fid1, '; InterpolResultTemplate.txt\r\n\n');
% fprintf(fid1, '; erstellt: 			26.11.2012/Ge\r\n\n');
% fprintf(fid1, ';**********************************************************************\r\n\n');
% fprintf(fid1, '[Info]\r\n');
% fprintf(fid1, 'ErstelldatumRef = 16.01.2012\r\n');
% fprintf(fid1, 'PfadReferenz = %s\r\n', pfad_ref);
% fprintf(fid1, 'PfadKoordinaten = %s\r\n', pfad_KS);
% fprintf(fid1, 'Array_Typen = T10032, T10043\r\n');
% fprintf(fid1, 'Pruefplatz = Co60-1\r\n'); 
% fprintf(fid1, 'ProgrammName = ArrayInterpol.exe\r\n');
% fprintf(fid1, 'ProgrammVersion = 1.00\r\n\n\n');
% fprintf(fid1, '[Kanal_Info]\r\n');
% fprintf(fid1, ';Kanal-Nr. = Status(Aktiv/Inaktiv);A/D-Kanal(1-4);IF-KanalNr;X_0;Y_0;K-Faktor;"Bemerkung"\r\n\n');
% for row_id = 1:num_pos
      % fprintf(fid1, '%d = %s;%d;%d;%3f;%3f;%3f;%s\r\n',...
                  % row_id-1, property{row_id}, AD_kanal(row_id), IF_kanal(row_id), x_array_in_mm(row_id), y_array_in_mm(row_id), result(row_id), Bemerkung{row_id});
 % end 
% fclose(fid1); 

% % ergebnisse Darstellen
% 
% xI = (y_array_in_mm + 140)./ x_dist_in_mm; 
% yI = (x_array_in_mm + 140)./ y_dist_in_mm; 
% 
% figure(1); clf; hold on;
% plot3(x_array_in_mm, y_array_in_mm, result, 'xb');
% plot3(xI, yI, result, 'or');
% grid on;
% xlabel('x_array_in_mm');
% ylabel('Y_array_in_mm');
% zlabel('result');
% 
% title('Interpolate');



