;+------------------------------------------------------------
; NAME:
;       OpenMDSshot
;
; PURPOSE:
;       Open MDSplus shot
;
; CATEGORY:
;	MDSplus
;
; CALLING SEQUENCE:
;        status = OpenMDSshot( MDStree, shot_num, SERVER=server, /QUIET )
;
; INPUTS:
;	MDStree - e.g. 'operations'				in
;       shot_num - shot number					in
;
; KEYWORD PARAMETERS:
;    Optional Inputs:
;	QUIET - just passed to MDS routines
;	SERVER - MDS server (default is europa.pppl.gov:8501 for NSTX)
;	JUSTCONNECT - if set, will just connect to MDSplus
;			(can be used before calling LastMDSshot()).
;
; OUTPUTS:
;	Status - MDS status
;
; EXAMPLE:
;	MDStree = 'operations'
;	shot_num = 100193
;	status = OpenMDSshot( MDStree, shot_num, /QUIET )
;	IF status THEN y=MDSGetSig('\IP1')
;  or,
;   sts = OpenMDSshot( 'EFIT01', 95334, server='omega.gat.com')
;   IF sts THEN PRINT, 'Opened GA shot successfully'
;
; COMMON BLOCKS:
;
; NOTES:
;	This can be called repeatedly and will not re-connect or
;	re-open a shot if it is already open.
;
;	MDS Connections need Multinet at this time.
;
; MODIFICATION HISTORY:
;  13-Sep-01 Don't avoid re-opening files, in case MDSopen was called directly.
;  24-Aug-00 Changed the default server from Unix to europa.pppl.gov:8501
;  13-Apr-00 Changed the default server from Unix to kees.pppl.gov:8501
;  22-Oct-99 Took out check for at GA, because added 0.3 seconds
;  04-Aug-99 fix for server= ' ' 
;  17-Mar-99 Have the default server from Unix be birch.pppl.gov:8501
;  12-Mar-99 refer to first argument as a tree. Make a keyword for server
;  Written by Bill Davis, 2/99
;-------------------------------------------------------------
FUNCTION OpenMDSshot, MDStree, shot_num, SERVER=server, QUIET=quiet, $
		JUSTCONNECT=JustConnect, REALQUIET=realQuiet

COMMON OpenMDSshot_Local, lastServer, lastTree, lastShot

debug = GETENV('DEBUG_IDL')            ; get a logical/environmental variable

IF debug EQ 9 THEN t0=SYSTIME(1)

;;;IF N_ELEMENTS( lastServer ) LE 0 THEN lastServer = ""

; Only open the Tree once.  Only Open a shot the first time accessed

IF N_PARAMS(0) GE 2 THEN BEGIN
   IF debug THEN PRINT,'  >>OpenMDSshot called with MDStree, shot_num=', $
                     MDStree, shot_num
ENDIF

IF debug THEN QUIET=0

IF N_ELEMENTS( shot_num ) EQ 0 THEN BEGIN
    shot_num = 100193
    IF NOT KEYWORD_SET( QUIET) THEN PRINT,  $
          ' (OpenMDSshot defaulting to shot 100193)'
ENDIF

arrayInfo = SIZE( MDStree )
IF arrayInfo(0) GT 0 THEN MDStree = MDStree(0)

IF debug EQ 9 THEN PRINT, '1:', SYSTIME(1)-t0

; Connect to a remote MDS server, if necessary

   ; determine if connection is needed:

needToConnect = 0
IF N_ELEMENTS( server ) GT 0 THEN BEGIN  ; server specified, so see if need to connect
   needToConnect = 1
   IF NWORDS( server ) LE 0 THEN BEGIN
      needToConnect = 0
      ; but you may already be connected:
   ENDIF ELSE IF N_ELEMENTS( lastServer ) GT 0 THEN BEGIN
       IF STRUPCASE(server) EQ STRUPCASE(lastServer) THEN needToConnect = 0
   ENDIF ELSE BEGIN	; if on VMS with no MultiNet, won't work
       IF (!VERSION.OS_FAMILY EQ 'vms') THEN BEGIN
	      ; on VMS, MultiNet must exist
	   testMN = getenv('multinet_vms_shareable')
	   IF NWORDS( testMN ) EQ 0 THEN BEGIN	 ; No MultiNet on this machine
	      ; one last chance: if on Birch, won't need to connect to PPPL server
              SPAWN,' write sys$output f$getsyi("nodename")',nodeName
	         ; if on KEES, hope you are trying to connect locally
	      IF nodeName(0) EQ 'BIRCH' THEN BEGIN
	         needToConnect = 0
	      ENDIF ELSE BEGIN
	          PRINT,' '
	          PRINT,' *** Need MultiNet for MDS connection - try Birch'
	          PRINT,' '
		  return, 0
	      ENDELSE
	   ENDIF
       ENDIF
   ENDELSE
ENDIF ELSE BEGIN	; server not specified
   Server = ""
      ; You don't need to connect when on VMS at PPPL (currently)
      ;  or at GA (because it is done in the MDSOPEN call)
;;;   IF STRPOS( STRUPCASE(domainName()), 'GAT' ) GE 0 THEN BEGIN
;;;      needToConnect = 0
;;;   ENDIF ELSE BEGIN
	 ; you may already be connected:
      IF N_ELEMENTS( lastServer ) GT 0 THEN BEGIN
	  needToConnect = 0
      ENDIF ELSE BEGIN
	  IF (!VERSION.OS_FAMILY NE 'vms') THEN BEGIN
	      needToConnect = 1
	      server = 'europa.pppl.gov:8501'	; (8501 may not be needed later)
	  ENDIF
      ENDELSE
ENDELSE

IF debug EQ 9 THEN PRINT, '2:', SYSTIME(1)-t0
IF debug EQ 9 THEN PRINT," needToConnect=", needToConnect

IF needToConnect THEN BEGIN
   IF NWORDS(server) EQ 0 THEN BEGIN	; server not specified, so take default
      IF STRPOS( STRUPCASE(domainName()), 'PPPL' ) GE 0 THEN server='europa.pppl.gov:8501'
   ENDIF
   MDSCONNECT, server, STATUS=status, /quiet
   IF NOT KEYWORD_SET( QUIET ) THEN PRINT,'MDSCONNECT with server=', server
      ; perhaps Europa is down, but Kees is up:
   if not status then MDSCONNECT, 'kees:8501', STATUS=status, /quiet
   if not status then return, status
   IF NOT STATUS THEN BEGIN
       PRINT,' >>> Connection failed; status = ', status
       RETURN, status
   ENDIF
   lastServer = server
ENDIF 

IF KEYWORD_SET( JustConnect ) THEN RETURN, 1

IF debug EQ 9 THEN PRINT, '3:', SYSTIME(1)-t0

; Open the MDS shot tree, if not already open

needToOpen = 1
;;;IF (N_ELEMENTS( lastShot ) GT 0) THEN BEGIN
;;;   IF (lastShot EQ shot_num) AND (lastTree EQ MDStree) THEN needToOpen = 0 ;;; $
                              ; (MDS system takes care of closing files)
                                  ;;;ELSE MDSCLOSE, lastTree, lastShot ; (mds 
;;;ENDIF

If NOT needToOpen THEN BEGIN
   RETURN, 1	; already open
ENDIF ELSE BEGIN
   MDSOPEN, MDStree, shot_num, STATUS=stat,quiet=1	; need quiet=1 so doesn't quit
   IF NOT stat THEN BEGIN
      if NOT KEYWORD_SET( realQuiet)  THEN $
      PRINT,' *** Could Not Open MDS Tree ', MDStree, ' for shot ', $
                  STRTRIM(shot_num, 2)
      RETURN, 0
   ENDIF ELSE BEGIN
      ;;;IF NOT KEYWORD_SET( QUIET ) THEN PRINT,'MDSOPEN called successfully'
      IF N_ELEMENTS( QUIET ) GT 0 AND $
         NOT KEYWORD_SET( realQuiet)  THEN PRINT,'MDSOPEN called successfully'
      lastShot = shot_num
      lastTree = MDStree
      RETURN, 1
   ENDELSE
ENDELSE 

IF debug EQ 9 THEN PRINT, '4:', SYSTIME(1)-t0

END