function Info=AK_MDS_node_info(node,shot,k_debug,shot_time)
% Get information about node in MDS
% Call: Info=AK_MDS_node_info(node,shot,k_debug)
%       node - MDS node name, for example '\RESULTS::CXRS_001:COMMENT'
%       shot - TCV shot number (default [], opened shot)
%       k_debug - debug key (default false)

s=sprintf('%s',mfilename);
Info.OK  = 1;
Info.msg = 'OK';

if nargin < 1, node=[]; end
if nargin < 2, shot=[]; end
if nargin < 3, k_debug = false; end
if nargin < 4, shot_time=[]; end
if ~isempty(shot),
    Info.shot=mdsopen(shot);
    if isempty(Info.shot), 
      Info.msg=sprintf('ERROR in %s - MDS Open shot #%d - probably not exist',s,shot); disp(Info.msg);
      Info.OK=-100; mdsclose; return
    elseif (shot~=Info.shot)&&(shot~=0), 
      Info.msg=sprintf('ERROR in %s - MDS Open shot #%d <> data.shot #%d',s,shot,Info.shot); disp(Info.msg);
      Info.OK=-101; mdsclose; return
    end
else
    [Info.shot,stat]=mdsvalue('$SHOT');
    if ~mod(stat,2),
      Info.msg=sprintf('ERROR in %s - MDS shot is nor opened',s); disp(Info.msg);
      Info.OK=-102; mdsclose; return
    elseif isempty(Info.shot);
      Info.msg=sprintf('ERROR in %s - MDS shot is unknown',s); disp(Info.msg);
      Info.OK=-103; mdsclose; return
    end    
end

if isempty(shot_time),
    % Info.shot_time = mdsvalue('shot_date(nint($1))',Info.shot);
    [Info.shot_time,stat] = mdsdata(['tcv_shot_time(',int2str(Info.shot),')']);
else
    Info.shot_time=shot_time; stat=1;
end
chk_tim=true;
if ~mod(stat,2),
    [shot_time,stat] = mdsvalue('shot_date(nint($1))',Info.shot);
    lf= findstr(shot_time,sprintf('\n'));
    if ~isempty(lf), shot_time=shot_time(1:lf(1)-1); end
    Info.shot_time = shot_time; 
    chk_tim=false;
end

if (k_debug~=0), disp(sprintf('          ----- TCV shot %d cycle start date and time: %s -----',Info.shot,Info.shot_time));  end 
Info.shot_time_vec=datevec(Info.shot_time,'dd-mmm-yyyy HH:MM:SS.FFF'); timk=[10000 100 1 0.01 0.0001 0.000001];
Info.shot_run= sum(Info.shot_time_vec.*timk);

if ~isempty(node),
    Info.node=node;
    tdis=sprintf('getnci("\\%s","FULLPATH")'            ,node);  [node_fullpath,stat_fullpath] =mdsdata(tdis);
    tdis=sprintf('owner_ascii("\\%s")'                  ,node);  [node_ownerid, stat_ownerid ] =mdsdata(tdis);
    tdis=sprintf('times(getnci("\\%s","TIME_INSERTED"))',node);  [node_time,    stat_time    ] =mdsdata(tdis);
    tdis=sprintf('uid_gid("\\%s")'                      ,node);  [node_ownerID, stat_ownerID ] =mdsdata(tdis);
    if ~mod(stat_fullpath,2), node_fullpath = 'unknown'; Info.OK  = -201; end
    if ~mod(stat_ownerid ,2), node_ownerid  = 0        ; Info.OK  = -202; end
    if ~mod(stat_time    ,2), node_time     = 'unknown'; Info.OK  = -203; end
    if ~mod(stat_ownerID ,2), node_ownerID  = []       ; Info.OK  = -204; end
    Info.node_fullpath= deblank(node_fullpath);
    Info.node_ownerid = node_ownerid;
    Info.node_time    = node_time;
    Info.node_ownerID = node_ownerID;
    %if ~isempty(node_ownerID),
    %    cmd=sprintf('getent passwd %d  | sed -e ''s/\\:.*//''',node_ownerID(1));  [stat,owner] = system(cmd);
    %    if (stat~=0), owner=[]; end
    %    cmd=sprintf('getent group  %d  | sed -e ''s/\\:.*//''',node_ownerID(2));  [stat,group] = system(cmd);
    %    if (stat~=0), group=[]; end
    %    if isempty(group), group=sprintf('%d',node_ownerID(2)); end
    %    if isempty(owner), owner=sprintf('%d',node_ownerID(1)); end
    %    if ~isempty(findstr(group,'invalid option')), group='unknown'; end
    %    if ~isempty(findstr(owner,'invalid option')), owner='unknown'; end
    %    node_owner=sprintf('[%s,%s]',deblank(group),deblank(owner));
    %else
    %    node_owner=sprintf('[unknown,unknown]');
    %end
    Info.node_owner   = sprintf(' user %s',node_ownerid);
    if (k_debug~=0), disp(sprintf('Node %s (%s) created %s by %s',Info.node,Info.node_fullpath,Info.node_time,Info.node_owner)); end
    if mod(stat_time    ,2),
        Info.node_time_vec=datevec(Info.node_time,'dd-mmm-yyyy HH:MM:SS.FFF'); timk=[10000 100 1 0.01 0.0001 0.000001];
        Info.node_run= sum(Info.node_time_vec.*timk);        
    else
        Info.node_time_vec=[1970 1 1 0 0 0]; timk=[10000 100 1 0.01 0.0001 0.000001];
        Info.node_run= sum(Info.node_time_vec.*timk);                
    end
    if (Info.node_run-Info.shot_run)<-0.005,
        if (Info.OK==1)&&chk_tim, 
            Info.OK=-301;
            Info.msg=sprintf('WARNING node %s is craeated before shot #%d created',Info.node,Info.shot); 
            if (k_debug~=0), disp(Info.msg); end
        end
    end
    
end

if ~isempty(shot), mdsclose; end

return