classdef SCDclass_mdsparnumeric < SCDclass_mdspar
    % General purpose numeric loader MDS+ parameter class
    
    properties
    end
    
   
    methods
        
        function obj=SCDclass_mdsparnumeric(srctdimodel, destparam, varargin)   
            obj@SCDclass_mdspar();
            % Constructor parser customization definitions here
            obj=obj.parseconstructor(srctdimodel, destparam, varargin);
            % Constructor parser customization results here
            obj.classname=class(obj);            
            obj.marteclassname='MDSParameter';
        end

        function actualizedata(obj, shot) 
              obj=obj.preactualize(shot);
              if ~obj.actualizable
                  if obj.loadpermissivity
                      warning('SCDDScore:mdsparnumeric','%s: parameters not actualizable (preactualize fails), actualization skipped!',obj.modelparam);
                  else
                      error('SCDDScore:mdsparnumeric','%s: parameters not actualizable (preactualize fails), actualization skipped!',obj.modelparam);
                  end
                  return;
              end
     
              %%%% Actualization checks %%%%
              boolcast=false;
              %%% dimensions
              sourcedim=size(obj.value);
              targetdim=Simulink.data.evalinGlobal(obj.modelname,sprintf('size(%s)',obj.assignvar));
              actchk=true;
              if numel(sourcedim)~=numel(targetdim)
                  sourcedim
                  targetdim
                  if obj.loadpermissivity
                    warning('SCDDScore:mdsparnumeric','%s: number of dimensions not matching, actualization skipped!',obj.modelparam); 
                  else
                    error('SCDDScore:mdsparnumeric','%s: number of dimensions not matching, actualization skipped!',obj.modelparam); 
                  end

                  actchk=false;
              end
              %%% handle vector transposition 
              if numel(sourcedim)==2 && ~isscalar(obj.value)
                 if (sourcedim(1)==1 && targetdim(2)==1) || (sourcedim(2)==1 && targetdim(1)==1)
                    % we've a vector to transpose
                    obj.value=obj.value';
                    sourcedim=size(obj.value);
                    % TODO: in the new implementation, use obj.value for
                    % actualization and not an evaluated command ?
                    obj.assignstring=sprintf('%s=%s'';',obj.assignvar,obj.getcommand);
                 end
              end
              if actchk && ~any(sourcedim == targetdim)
                  sourcedim
                  targetdim
                  if obj.loadpermissivity
                    warning('SCDDScore:mdsparnumeric','%s: dimensions not matching, actualization skipped!',obj.modelparam); 
                  else
                    error('SCDDScore:mdsparnumeric','%s: dimensions not matching, actualization skipped!',obj.modelparam); 
                  end
                  actchk=false;
              end  
              %%%% type
              sourceclass=class(obj.value);
              targetclass=Simulink.data.evalinGlobal(obj.modelname,sprintf('class(%s)',obj.assignvar));
              %%% handle cast to logical
              if strcmp(targetclass,'logical')
                if ~strcmp(sourceclass,'uint8')
                   sourceclass  
                   if obj.loadpermissivity
                     warning('SCDDScore:mdsparnumeric','%s: source type must be %s for logical parameters',obj.modelparam,obj.logicalmdstype); 
                   else
                     error('SCDDScore:mdsparnumeric','%s: source type must be %s for logical parameters',obj.modelparam,obj.logicalmdstype); 
                   end
                   actchk=false;
                end
                if actchk
                    sourceclass='logical';
                    boolcast=true;
                end
              end  
              %%% type check
              if actchk && ~strcmp(sourceclass,targetclass)
                  sourceclass
                  targetclass
                  if obj.loadpermissivity
                    warning('SCDDScore:mdsparnumeric','%s: Data types not matching, actualization skipped!',obj.modelparam); 
                  else
                    error('SCDDScore:mdsparnumeric','%s: Data types not matching, actualization skipped!',obj.modelparam); 
                  end
                  actchk=false;
              end
              
              % MARTe2 checks matrix orientation, but MDSplus - MATLBA
              % interface do not provide in information (matrix
              % orientation) to do this. Or, probably, I misunderstood and
              % matrix orientation is always preserved
              
              if boolcast
                obj.caststring=sprintf('%s=logical(%s);',obj.assignvar,obj.assignvar);
              else
                obj.caststring=sprintf('%s=%s;',obj.assignvar,obj.assignvar);
              end
              if actchk
                obj.postactualize(shot);
              end
        end
                
        function [obj, value] = getdata(obj, shot)
              [obj,value]=getdata@SCDclass_mdspar(obj,shot);
        end
       
        function printinfo(obj)
            printinfo@SCDclass_mdspar(obj);
        end
         
        function entrystring = genMARTe2entry(obj, shot)
            entrystring=obj.genMARTe2entrypreamble(shot);
            entrystring=[entrystring ' }'];
        end

        function out = casttomds(obj, in)
            out = in;
        end
    end
end

