;---------------------------------------------------------------------------
; Document name: CMDLOG2UDP.PRO
; Created by:    Dietmar Germerott, MPAE Lindau, May 9, 2001
;
; Last Modified: Fri Sep  7 11:07:21 2018 (germerott.nospam@mps.mpg.de)
;---------------------------------------------------------------------------
;
PRO cmdlog2udp, file_spec, errmsg=errmsg, outfile=outfile,debug_mode=debug_mode
;+
; PROJECT:
;       SOHO - SUMER
;
; NAME:
;       CMDLOG2UDP
;
; PURPOSE: 
;       Search the SUMOP1 Log files for LOAD and CLEAR UDP commands.
;
; CATEGORY:
;       
; 
; EXPLANATION:
;       
; SYNTAX: 
;       cmdlog2udp, file_spec
;
; EXAMPLES:
;       cmdlog2udp, '..../log*/rtg.*'
; INPUTS:
;       file_spec - file specification for the log files
;
; OPTIONAL INPUTS: 
;       None.
;
; OUTPUTS:
;       File containing the load and clearing of an UDP slot.
;
; OPTIONAL OUTPUTS:
;       None.
;
; KEYWORDS: 
;       errmsg - return processing status.
;       outfile - optional filename for the output file.
;       debug_mode - be more verbose during processing.
;
; COMMON:
;       None.
;
; RESTRICTIONS: 
;       None.
;
; SIDE EFFECTS:
;       None.
;
; HISTORY:
;       Version 1, May 9, 2001, Dietmar Germerott, MPAE Lindau. Written
;       Version 1.1, April 25, 2003, Dietmar Germerott, MPAE Lindau
;         correctet processing of linefeed in Logfile
;       Version 2, December 20, 2010, Dietmar Germerott, MPS
;         Added check for correct slot number and range.
;       Version 3, July 5, 2011, Dietmar Germerott, MPS
;         Make text processing case insensitive, because SUMOP1
;         accepts also mixed case command.
;         Added selection for NRT (file) load_UDP... pattern.
;       Version 4, September 7, 2018, Dietmar Germerott, MPS
;         Corrected case for search pattern UDP Number: to UDP NUMBER
;         (all uppercase!)
;
; CONTACT:
;       Dietmar Germerott, MPS Lindau (germerott.nospam(at)linmpi.mpg.de)
;-
;
   ON_ERROR, 2
   
   errmsg = '' & tmpmsg=''
   IF N_ELEMENTS(file_spec) EQ 0 THEN BEGIN
      PRINT, 'CMDLOG2UDP -- Syntax error.'
      PRINT, '   Usage: CMDLOG2UDP, file_spec [,errmsg=errmsg]'
      PRINT, ' '
      tmpmsg = ' Syntax error'
      GOTO, handle_error
   ENDIF
   
   IF N_ELEMENTS(outfile) EQ 0 THEN outfile = 'cmdudplist.txt'
   IF KEYWORD_SET(debug_mode) THEN debug = 1 ELSE debug = 0
   
   all_log = FINDFILE(file_spec, count=fnum)
   
   IF fnum LE 0 THEN BEGIN
      PRINT,' No files found for FILESPEC: ',file_spec
      tmpmsg = ' No files found'
   ENDIF
   
   IF debug THEN PRINT,fnum,' files to process'
   
   s_pattern1 = 'LOAD_UDP'
   s_pattern2 = 'CLEAR_UDP'
   
   OPENW, uout, outfile, /GET_LUN
   
   FOR n=0,fnum-1 DO BEGIN
      
      checkflag = 0
      
      break_file, all_log(n), dv, dd, fnam, fext
      IF STRMID(fext,1,1) EQ '9' THEN ddate = '19'+STRMID(fext,1,6) ELSE $
         ddate = '20'+STRMID(fext, 1, 6)
      odate = STRMID(ddate,0,4)+'-'+STRMID(ddate,4,2)+'-'+STRMID(ddate,6,2)
      
      IF debug THEN PRINT,n,' ',all_log(n)
      
                                ; search for pattern
      intxt = ''
      OPENR,uin,/GET_LUN,all_log(n)
      WHILE (NOT(EOF(uin)) AND (checkflag EQ 0)) DO BEGIN 
         READF,uin,intxt
;---------------------------------------------------------------------------
;  Make it case insensitive
;---------------------------------------------------------------------------
         intxt=strupcase(intxt)
         IF (STRPOS(intxt,s_pattern1) GE 0) OR (STRPOS(intxt,s_pattern2) GE 0) THEN $
            checkflag = 1
      ENDWHILE
      
      FREE_LUN,uin
      
      
      IF checkflag THEN BEGIN 
         logtxt = rd_ascii(all_log(n))
         logtxt=strupcase(logtxt);s.a.
         IF debug THEN PRINT, ' Read ', N_ELEMENTS(logtxt), ' Lines' 
         
         plines = WHERE((STRPOS(logtxt, s_pattern1) GE 0) OR (STRPOS(logtxt, s_pattern2) GE 0), cnt)
         
         IF cnt GT 0 THEN BEGIN 
            FOR k=0, cnt-1 DO BEGIN
               intxt = logtxt(plines(k))
               ftmp=0
               CASE 1 OF 
                  (STRPOS(intxt(0), s_pattern1) GE 0):BEGIN
                     wtxt = STRCOMPRESS(STRTRIM(intxt(0), 2))
                     IF debug THEN PRINT, wtxt
                     stxt = str_sep(wtxt, ' ')
                     
                                ;look for continue lines
                     pattloc = WHERE(STRPOS(stxt, s_pattern1) EQ 0)
                     ;PRINT,pattloc
                     
                     IF pattloc(0) EQ N_ELEMENTS(stxt)-1 THEN BEGIN 
                        tmptxt = logtxt(plines(k):plines(k)+2)
                        wtxt = STRCOMPRESS(STRTRIM(tmptxt(1), 2))
                        tmp1 = str_sep(wtxt, ' ')
                        wtxt = STRCOMPRESS(STRTRIM(tmptxt(2), 2))
                        tmp2 = str_sep(wtxt, ' ')
                        stxt = [stxt, tmp1(2), tmp2(2)]
                     ENDIF
                     
                                ;look for "UDP Number: NN" in the next line
                     IF pattloc(0) EQ N_ELEMENTS(stxt)-2 THEN BEGIN
                        tmptxt = logtxt(plines(k)+1)
                        IF STRPOS(tmptxt,'UDP NUMBER:') GE 0 THEN BEGIN
                           wtxt = str_sep(STRCOMPRESS(STRTRIM(tmptxt,2)),' ')
                           IF N_ELEMENTS(wtxt) EQ 3 THEN stxt = [stxt,wtxt(2)]
                        ENDIF
                     ENDIF
                     
                     pattloc = WHERE(STRPOS(stxt, s_pattern1) EQ 0)
                     ;PRINT,pattloc,N_ELEMENTS(stxt),stxt(4:*)
                     
                     IF pattloc(0) EQ N_ELEMENTS (stxt)-3 THEN BEGIN  
                       IF ((stxt(N_ELEMENTS(stxt)-4 ) EQ 'NRT') AND (stxt(0) NE 'UDP')) THEN BEGIN
                         ftmp=fix(stxt(N_ELEMENTS(stxt)-1)) ;test correct range (1-16)
                         IF ((ftmp GT 0) AND (ftmp LT 17)) THEN BEGIN
                           PRINTF, uout, format='(a10,x,a8,x,a2,x,a)', odate, STRMID(stxt(0), 0, 8), $
                                   stxt(N_ELEMENTS(stxt)-1), strlowcase(stxt(N_ELEMENTS(stxt)-2))
                           IF debug THEN PRINT, format='(a10,x,a8,x,a2,x,a)', odate, STRMID(stxt(0), 0, 8), $
                                                stxt(N_ELEMENTS(stxt)-1), stxt(N_ELEMENTS(stxt)-2)
                         ENDIF
                       ENDIF

                        ;find possible lines with >>> NRT (file) LOAD_UDP.... <<<
                       IF ((stxt(N_ELEMENTS(stxt)-5 ) EQ 'NRT') AND (stxt(N_ELEMENTS(stxt)-4 ) EQ '(FILE)')) THEN BEGIN
                         ftmp=fix(stxt(N_ELEMENTS(stxt)-1)) ;test correct range (1-16)
                         IF ((ftmp GT 0) AND (ftmp LT 17)) THEN BEGIN
                           PRINTF, uout, format='(a10,x,a8,x,a2,x,a)', odate, STRMID(stxt(0), 0, 8), $
                                   stxt(N_ELEMENTS(stxt)-1), strlowcase(stxt(N_ELEMENTS(stxt)-2))
                           IF debug THEN PRINT, format='(a10,x,a8,x,a2,x,a)', odate, STRMID(stxt(0), 0, 8), $
                                                stxt(N_ELEMENTS(stxt)-1), stxt(N_ELEMENTS(stxt)-2)
                         ENDIF
                       ENDIF

                     ENDIF   
                  END
                  
                  (STRPOS(intxt(0), s_pattern2) GE 0):BEGIN
                     wtxt = STRCOMPRESS(STRTRIM(intxt(0), 2))
                     IF debug THEN PRINT, wtxt
                     stxt = str_sep(wtxt, ' ')
                     
                                ;look for continue line
                     IF N_ELEMENTS(stxt) EQ 4 THEN BEGIN 
                        tmptxt = logtxt(plines(k):plines(k)+2)
                        wtxt = STRCOMPRESS(STRTRIM(tmptxt(1), 2))
                        tmp1 = str_sep(wtxt, ' ')
                        stxt = [stxt, tmp1(2)]
                     ENDIF
                     
                     IF N_ELEMENTS(stxt) GE 5 THEN BEGIN
                        IF (stxt(2) EQ 'NRT') THEN BEGIN
                          ftmp=fix(stxt(4));test for correct range (1-16)
                          IF ((ftmp GT 0) AND (ftmp LT 17)) THEN BEGIN 
                            PRINTF, uout, format='(a10,x,a8,x,a2,x,a)', odate, STRMID(stxt(0), 0, 8), $
                                    stxt(4), 'xxxxslot_clearxxxx'
                            IF debug THEN PRINT, format='(a10,x,a8,x,a2,x,a)', odate, STRMID(stxt(0), 0, 8), $
                                                 stxt(4), 'xxxxslot_clearxxxx'
                          ENDIF
                        ENDIF
                        IF (stxt(3) EQ 'PTD') THEN BEGIN
                          ftmp=fix(stxt(6));test for correct range (1-16)
                          IF ((ftmp GT 0) AND (ftmp LT 17)) THEN BEGIN 
                            PRINTF, uout, format='(a10,x,a8,x,a2,x,a)', odate, STRMID(stxt(0), 0, 8), $
                                    stxt(6), 'xxxxslot_clearxxxx'
                            IF debug THEN PRINT, format='(a10,x,a8,x,a2,x,a)', odate, STRMID(stxt(0), 0, 8), $
                                                 stxt(6), 'xxxxslot_clearxxxx'
                          ENDIF
                          
                        ENDIF

                     ENDIF
                  END
                  
                  ELSE:
                  
               ENDCASE
               lasttxt = intxt(0)
            ENDFOR
            
         ENDIF 
      ENDIF
      
   ENDFOR
   
   ; Read additional listed udps from hand made list
;   missing_file = FINDFILE('data3:[log]missing_udp.txt',count=fcnt)
   missing_file = FILE_SEARCH('/data1/data/sumer/tmroot/log','missing_udp.txt',count=fcnt)
   IF fcnt GT 0 THEN BEGIN
      print,'Processing missing UDPs'
      OPENR,muin,missing_file(0),/get_lun
      instr = ''
      WHILE NOT(EOF(muin)) DO BEGIN
         READF,muin,instr
         IF debug THEN PRINT,instr
         PRINTF,uout,strlowcase(instr)
      ENDWHILE
      FREE_LUN,muin
   ENDIF
   
   FREE_LUN,uout
   
   cmd = 'sort -u '+outfile+' > '+outfile+'_srt'
   if debug then print,' CMD = ',cmd
   SPAWN,cmd
   RETURN
handle_error:
   
   errmsg = tmpmsg
END


;---------------------------------------------------------------------------
; End of 'CMDLOG2UDP.PRO'.
;---------------------------------------------------------------------------
