function SI2con_2DFLASHlinear_roi(data1, info, bw, skip);
% test conversion to a value linear w.r.t. concentration on a ROI
% input args:
%   data1, info - input data
%   bw - mask of the testing roi
%   skip - how many frames to skip




%% precontrast sequences - extract imagesc for estimation of M0 and E10

seqs = length(data1);   % number of precontrast seqs.
slices = info.sections;

fas = 1:90; % flip angle axis

frames = length(data1{info.acq.kM0Range(1)});
X = size(data1{info.acq.kM0Range(1)}{1},2);
Y = size(data1{info.acq.kM0Range(1)}{1},1);

% ROI
numpixels = sum(sum(bw));

si = zeros(1,50);
figure(10);
subplot(111)
hold off;
plot(fas(1),si(1));
hold on;

%img = cell(seqs,frames);   % sequence , frame
img = zeros(Y,X,seqs,frames);   % sequence , frame
SI =  zeros(seqs,frames);

% for each precontrast sequence:
ind = 1;
for seq = info.acq.kM0Range(1):info.acq.kM0Range(2) %1:seqs
    
    % for each frame:
    for frame=1:frames
        img(:,:,ind,frame)=double(data1{seq}{frame});
        im = double(data1{seq}{frame});
        fa = info.acq.FA(seq);
        
        si = sum(sum(double(bw).* im)) / numpixels;
        SI(seq,frame)=si;
        %figure(2);
%         subplot(122)
%         imagesc(im + im.*double(bw)*1); colormap(gray);
        %title(sprintf([precontrast_seq_mat{seq} '   frame: %d'],slice,frame));
%         subplot(121);
        figure(10);
        switch frame
            case 1
                plot(fa,si,'.');
            case 2
                plot(fa,si,'*');
            case 3
                plot(fa,si,'x');
            otherwise
                plot(fa,si,'o');
        end
        
    end %for frame=1:frames
    
    ind = ind + 1;
end %for seq = 1:seqs




%% curve fitting to estimate E10 and M0 for ROI:

% optimization:

FAs = double(repmat(info.acq.FA(1:seqs)', [1,frames-skip]))/180*pi;
SIs = SI(:,skip+1:end);

xx = SIs ./ tan(FAs);
yy = SIs ./ sin(FAs);
p = polyfit(xx,yy,1);
T10 = -info.acq.TR(1) / log(p(1));

figure(11)
plot(xx, yy, 'o', xx, p(2)+p(1)*xx,'g-');
title(sprintf('T10: %f ms', T10))



end
