function [shoto,status] = mdsopen(server, shot)
% function to open remote server server
% eg : mdsopen('tcv_shot',12345);
% defaults to mdsopen('tcvdata.epfl.ch::tcv_shot',shot);
% To open a remote server only, use mdsopen ( no arguments )
% If first parameter is not a string, will open 'tcv_shot' tree
%
% Basil P. DUVAL, Oct 1998

if(nargin < 1);server='';shot='none';
elseif(nargin< 2 && ~ischar(server));shot=server;server='tcv_shot';
elseif(nargin< 2 && ischar(server));shot='none';
elseif(nargin >2 || ~ischar(server) || ischar(shot));error('Incorrect arguments to mdsopen');
end
if isnumeric(shot), shot = double(shot);end

[ST,I] = dbstack;
sout='';
for(i=2:length(ST));sout=[sout,sprintf('-%s#%d',ST(i).file,ST(i).line)];end
disp(sprintf('MDSOpen(''%s'',%d)%s',server,shot,sout));

% open the shot

status = mdsipmex(1,server,shot);

%status  = mdsipmex('MDSLIB->MDS$OPEN($,int($))',server,shot);
% return the open shot number if successful
if(rem(status,2) == 1 && nargout > 0 && ~strcmp(shot,'none'))
  shoto = mdsipmex(2,'$SHOT');
else
  if(nargout > 0);shoto=[];end
end
