function [TF,TB,TM,Tint,tint,tint_shift,Tiso,tiso] = isoTint(Tex,tex,T_step,TFS,TFF,TBS,TBF,TMS,TMF,Tcrit)
%isotint splits each cooling cycle into isothermal temperature intervals
%with a maximum temperature difference of T_step inbetween the temperature
%intervals

% the temperature interval is considered between the peak temperature and
% the minimum temperature of the cooling cycle

Tpeak = max(Tex);
Tmin = min(Tex);
%% split cycle into isothermal temperature intervals
% T critical only appears when there is a heating cycle in between two
% cooling cycles. Tcrit is either the temperature at which fA=fAeq or the
% end temperature of the transformation
% if Tcrit is empty, there was no heat cycle considered before the cooling
% cycle
if isempty(Tcrit) == 1
    if Tpeak > TFS
        TF = linspace(TFS,TFF,ceil((TFS-TFF)/T_step)+1);
    elseif Tpeak< TFS
        TF = linspace(Tpeak,TFF,ceil((Tpeak-TFF)/T_step)+1);
    end
    
    % if Tcrit contains a temperature, the temperature interval starts where
    % the previous transformation stopped at fA=fAeq or AC1
elseif isempty(Tcrit) == 0
    TF = linspace(Tcrit,TFF,ceil((Tcrit-TFF)/T_step)+1);
end

if Tmin < TBF
    TB = linspace(TBS,TBF,ceil((TBS-TBF)/T_step)+1);
    TM = linspace(TMS,Tmin,ceil((TMS-Tmin)/T_step)+1);
    
elseif Tmin > TBF % in case of heavy heat accumulation
    TB = linspace(TBS,Tmin,ceil((TBS-Tmin)/T_step)+1);
end

if TBF == TMS && TFF == TBS
    disp('TBF == TMS && TFF == TBS')
    Tint = [TF,TB(2:end),TM(2:end)];            % Temperature vector that contains all interpolated temperatures that are considered when estimating the MS
elseif TBF ~= TMS && TFF == TBS
    disp('TBF ~= TMS && TFF == TBS')
    Tint = [TF,TB(2:end),TM];
elseif TBF ~= TMS && TFF ~= TBS
    disp('TBF ~= TMS && TFF ~= TBS')
    Tint = [TF,TB,TM];
elseif TBF == TMS && TFF ~= TBS
    disp('TBF == TMS && TFF ~= TBS')
    Tint = [TF,TB,TM(2:end)];
end
tint = interp1(Tex,tex,Tint,'linear');      % Time vector that contains all interpolated times
dt = abs(diff(tint));                       % dt of interpolated time vector
tint_shift = [tint(1),tint(2:end)-0.5*dt];  % Time vector, starting with t(0) and than shifted with -0.5*dt to get the isothermal temperature intervals in which the temperature is evaluated in the middle of the time interval
dt_iso = abs(diff(tint_shift));             % dt of the shifted time vector

%% plot the isothermal temperature intervals
% create vectors to plot the isothermal timesteps
for i = 1:(length(Tint)-1)
    Tiso(2*i-1)  = Tint(i);
    Tiso(2*i)    = Tint(i);
    tiso(2*i-1)  = tint_shift(i);
    tiso(2*i)    = tint_shift(i+1);
    if i == (length(Tint)-1)
        i = i+1;
        Tiso(2*i-1)  = Tint(i);
        tiso(2*i-1)  = tint_shift(i);
    end
end
end

