function [z,Ts,TimeUnit,dataname] = iddsrc(CB,z,T0)
% Mask initialization code for IDDATA source block in SLIDENT library.

% Copyright 2000-2023 The MathWorks, Inc.

hCB = get_param(CB,'handle');

if ~isa(z,'iddata') || ~strcmpi(z.Domain,'time') || ~isreal(z)
   DAStudio.slerror('Ident:simulink:iddataCheck1',hCB)
end

Ts = z.Ts;
if iscell(Ts)
   DAStudio.slerror('Ident:simulink:iddataCheck2',hCB)
end
TimeUnit = z.TimeUnit;
if isempty(TimeUnit)
   TimeUnit = 'seconds';
elseif ~strcmp(TimeUnit,'seconds')
   Ts = tunitconv(TimeUnit,'seconds')*Ts;
end

if ~isequal(T0,-1) && ~idpack.isNonnegRealScalar(T0)
   DAStudio.slerror('Ident:simulink:iddataCheck3',hCB)
end

if T0==-1
   % Set start time to match that of the Simulink model (g3124316)
   T0str = get_param(bdroot(CB),'StartTime');
   try
      T0 = slResolve(T0str,CB); %localEvaluate(maskStr{1},bdroot(CB));
   catch %#ok<CTCH>
      T0 = 0;
   end
end
z.TStart = T0;

porty = find_system(CB,'MatchFilter',@Simulink.match.allVariants, ...
   'LookUnderMasks', 'all', 'FollowLinks', 'on', 'Name', 'y');
portu = find_system(CB,'MatchFilter',@Simulink.match.allVariants, ...
   'LookUnderMasks', 'all', 'FollowLinks', 'on', 'Name', 'u');
[~,ny,nu]=size(z);
if nu == 0 && ~isempty(portu)
   delete_line(CB,'FromWorkspace/1','u/1');
   delete_block(portu{1})
   delete_block([CB,'/FromWorkspace'])
end
if nu>0 && isempty(portu)
   if ~isempty(porty) % This is only the get the right numbering of the ports
      delete_line(CB,'FromWorkspace1/1','y/1');
      delete_block(porty{1})
      delete_block([CB,'/FromWorkspace1'])
      porty={};
   end
   add_block('simulink/Sinks/Out1',[CB,'/u'])
   set_param([CB,'/u'],'Position','[360    43   390    57]')
   add_block('simulink/Sources/From Workspace',[CB,'/FromWorkspace']);
   %set_param(blk,'OutputAfterFinalValue','Setting to zero')
   set_param([CB,'/FromWorkspace'],'Position','[50    31   285    69]')
   set_param([CB,'/FromWorkspace'],'VariableName',...
      '[iddataobj.SamplingInstants, iddataobj.InputData]',...
      'OutputAfterFinalValue','Setting to zero')
   add_line(CB,'FromWorkspace/1','u/1');
end

if ny == 0 && ~isempty(porty)
   delete_line(CB,'FromWorkspace1/1','y/1');
   delete_block(porty{1})
   delete_block([CB,'/FromWorkspace1'])
end
if ny>0 && isempty(porty)
   add_block('simulink/Sinks/Out1',[CB,'/y'])
   set_param([CB,'/y'],'Position','[355   128   385   142]')
   add_block('simulink/Sources/From Workspace',[CB,'/FromWorkspace1']);
   set_param([CB,'/FromWorkspace1'],'Position','[50   116   285   154]')
   set_param([CB,'/FromWorkspace1'],'VariableName',...
      '[iddataobj.SamplingInstants, iddataobj.OutputData]',...
      'OutputAfterFinalValue','Setting to zero')
   add_line(CB,'FromWorkspace1/1','y/1');
end

%{
% Set interpolation behavior: always 'foh'
blku = find_system(CB,'LookUnderMasks','all','FollowLinks','on','Name','FromWorkspace');
%blky = find_system(CB,'LookUnderMasks','all','FollowLinks','on','Name','FromWorkspace1');

if ~isempty(blku)
    if strncmpi(inters,'z',1)
        set_param(blku{1},'Interpolate','off');
    else
        set_param(blku{1},'Interpolate','on');
    end
end
%}

% Set the mask display property
MaskVal = get_param(CB,'MaskValues');
dataname = MaskVal{1};
if (ny == 0) && (nu == 0)
   maskdisplay = 'disp(dataname)';
elseif (ny == 0) && (nu ~= 0)
   maskdisplay = ['disp(dataname)',newline,'port_label(''output'',1,''Input'')'];
elseif (ny ~= 0) && (nu == 0)
   maskdisplay = ['disp(dataname)',newline,'port_label(''output'',1,''Output'')'];
else
   maskdisplay = ['disp(dataname)',newline,'port_label(''output'',1,''Input'')',...
      newline,'port_label(''output'',2,''Output'')'];
end

set_param(CB,'MaskDisplay',maskdisplay);


