;+-------------------------------------------------------------
; NAME:
;       MDSnodeChanges
; PURPOSE:
;       Print times when the last change was made to an MDSplus node.
; CATEGORY:
;       MDSplus, Tree Status, dates
; CALLING SEQUENCE:
;       MDSnodeChanges, node, shot1=shot1, shot2=shot2, skip=skip, tree=tree
; INPUTS:
;       node = MDSplus node name, e.g. \OPERATIONS::TOP.RAWDATA:LM_H908_01
; KEYWORDS:
;       shot1 = starting shot number to process
;       shot2 = last shot number to process (else shot1+400)
;       skip = skip factor
;       tree = MDSplus tree (defaults to nstx -- 
;		will be faster if specific tree specified)
;	BEFORE - show only nodes written before this date (defaults to '1-jan-1971'), 
;		so can see what never got written.
;	AFTER - show only nodes written after this date (defaults to '1-jan-1971'),
;		so can see only nodes that HAVE been written.
;	OUTFILE - a file name to contain a script related all shots for which
;		  a valid node was found in MDSplus
;	PREFIX - string to prefix the shot # in the script. 
;		DEFAULT is '/bin/rm -R -f '
;	VERBOSE - if set, will tell you what shots could not be opened.
; EXAMPLE:
;	IDL> tree='microwave'
;	IDL> node='\top.electron_den:line_density'
;	IDL> MDSnodeChanges, node, shot1=101300, shot2=101330, tree=tree
;
;	IDL> MDSnodeChanges, '\cameras2::FastSoftXray:frames',shot1=124459, $
;				shot2=125000, /after
;
;    to produce a script to delete shot directories that have been put in MDSplus:
;	IDL> MDSnodeChanges, node, shot1=shot1, shot2=shot2, /after, outfile='shots.txt'
;
; COMMON BLOCKS:
; NOTES:
;	see MDStreeChanges for who last changed a tree.
;	Note that "OWNER" returns 0 from getnci call.
;	This runs quickly.
; LIMITATIONS:
; MODIFICATION HISTORY:
;	30-Apr-2008 added outfile and prefix keywords, so directories can be
;		    deleted after verifying they are in MDSplus
;	09-Jul-2007 added AFTER keyword, so you can see what nodes have been written
;	30-Apr-2007 add BEFORE keyword, so you can see what nodes never got written
;	26-Apr-2007 "Who" stopped working (returning numberical value)
;	09-May-2006 Allow for numerical or '*' for who.
;       20-Dec-99 Written for Rajesh Maingi.
;--------------------------------------------------------------
PRO MDSnodeChanges, node, shot1=shot1, shot2=shot2, skip=skip, tree=tree, $
                    before=before, after=after, $
		    outfile=outfile, prefix=prefix, $
		    server=server, debug=debug, verbose=verbose

if n_params() ne 1 then begin
   print, 'USEAGE: IDL> MDSnodeChanges, node, shot1=shot1, shot2=shot2,tree=tree'
   return
endif

if N_ELEMENTS(debug) EQ 0 THEN debug = 0
if N_ELEMENTS(verbose) EQ 0 THEN verbose = 0
if debug then verbose = 1

if N_ELEMENTS(shot2) EQ 0 THEN begin
   if N_ELEMENTS(shot1) EQ 0 then shot2=lastshot() else shot2=shot1+400
endif

if N_ELEMENTS(shot1) EQ 0 THEN shot1 = shot2-400

IF N_ELEMENTS(skip) EQ 0 THEN skip = 1

IF  N_ELEMENTS(node) EQ 0 THEN node= '\wf::ip'

IF N_ELEMENTS(tree) EQ 0 THEN BEGIN
   dum = leafname( node, tree=tree )
ENDIF
if nwords( tree ) eq 0 then tree = 'NSTX'

if keyword_set( before ) then begin
   if isnumber( before ) then before = '1-jan-1971'
endif
if nwords( before ) gt 0 then jdBefore = date2jd( before )

if keyword_set( after ) then begin
   if isnumber( after ) then after = '1-jan-1971'
endif
if nwords( after ) gt 0 then jdAfter = date2jd( after )

if nwords(outfile) gt 0 then begin
   openw, lun, outfile, /get_lun
      ; will produce a file of scripts for deleting these files
   if nwords( prefix ) eq 0 then prefix = '/bin/rm -R -f '	
endif

print, ' '
print, 'Latest changes from shot ' + STRTRIM(shot1,2) + ' to ' + $
        STRTRIM(shot2,2) + ' for the ' + STRTRIM(tree,2) +    $
	' tree'
IF skip GT 1 THEN PRINT, '    (skip factor=' + STRTRIM(skip,2) + ')'

print, ' '
;;;print, '   Shot       Who    Date        Time     Node'
;;;print, '   ----       ---    ----        ----     ----'
print, '   Shot       Date	  Time     Node'
print, '   ----       ----	  ----     ----'

for shot=shot1,shot2,skip do begin

;;;   status = openMDSshot(tree, shot, /REALQUIET)
   mdsopen, tree, shot, /quiet, status=status

   IF NOT status THEN BEGIN
      if keyword_set( verbose ) then begin
	 BEEP
	 if verbose then begin
	    print, ' '
	    print, 'Could not open tree ' + STRTRIM(tree,2) + $
        	   ' for shot ', shot
	 endif
      endif
   ENDIF ELSE BEGIN

      nid = mdsvalue('_nid=getnci('+node+',"NID_NUMBER")', status=status )
      if not status then begin
	 BEEP
	 if verbose then begin
	    print, ' '
	    print, 'Could not find node ' + STRTRIM(node,2) + $
        	   ' for shot ', shot
	 endif
      endif else begin
	 quadWord = mdsvalue('_ti=getnci(_nid,"TIME_INSERTED")')
	 Date_time = mdsvalue('date_time( _ti )')
	 WORDARRAY, Date_time, StrArray
	 if StrArray[0] ne '*' then begin      
;;;      who = mdsvalue('_w=getnci(_nid,"OWNER")')
;;;      who = strtrim( who, 2 )
;;;      help,who
	    time = STRMID(StrArray[1],0,STRPOS(StrArray[1],'.'))
;;;     print, shot,  who, StrArray[0], time, node, $
;;;   		 FORMAT='(I8, A10, A12, A10, 2x, A)'
            doThisOne = 0
            if n_elements( jdBefore ) gt 0 AND  n_elements( jdAfter ) gt 0 then begin
	       jd = date2jd( StrArray[0])
	       if jd lt jdBefore and jd gt jdAfter then doThisOne = 1
	    endif else if nwords( before ) gt 0 then begin
	       jd = date2jd( StrArray[0])
	       if jd lt jdBefore then doThisOne = 1
	    endif else if nwords( after ) gt 0 then begin
	       jd = date2jd( StrArray[0])
	       if jd gt jdAfter then doThisOne = 1
	    endif else begin	; no date criter
	       doThisOne = 1
	    endelse 
	    if doThisOne then begin
	       print, shot, StrArray[0], time, node, $
        	     FORMAT='(I8, A12, A10, 2x, A)'
               if nwords(outfile) gt 0 then begin
		  printf, lun, prefix, shot
	       endif
            endif
	 endif
      endelse
   ENDELSE
   
ENDFOR
if nwords(outfile) gt 0 then begin
   close, lun
   free_lun, lun
endif
END