;---------------------------------------------------------------------------
; Document name: GET_RAWDATA.PRO
; Created by:    Dietmar Germerott, MPAE Lindau, April 7, 2002
;
; Last Modified: Sun Jun 20 14:12:53 2010 (GERMEROTT@sumop1.nascom.nasa.gov)
;---------------------------------------------------------------------------
;
PRO get_rawdata, rawpath_=rawpath_, out_path=out_path, once_=once_, $
                 lastagain_=lastagain_, nohkhall=nohkall,nointer=nointer, $
		 filespec_=filespec_
;+
; PROJECT:
;       SOHO - SUMER
;
; NAME:
;       GET_RAWDATA
;
; PURPOSE: 
;       Collect SUMER Raw Telemetry and convert to IDL Restore files
;
; CATEGORY:
;       Telemetry Conversion
; 
; EXPLANATION:
;       
; SYNTAX: 
;       get_rawdata
;
; EXAMPLES:
;       
; INPUTS:
;       None.
;
; OPTIONAL INPUTS: 
;       None.
;
; OUTPUTS:
;       None.
;
; OPTIONAL OUTPUTS:
;       None.
;
; KEYWORDS: 
;       rawpath_ - where to find Raw Tlemetry (def -> SUMER_RAW)
;       out_path - Root Directory, where to put Restore files
;       once_    - Run collection only once and stop then otherwise endless
;                  loop until 'Q' is hit on keyboard
;      lastagain_- process also the last processed file again to get in SYNC
;      nohkhall  - switch of Science HK saving
;      nointer   - do not read keyboard because run from batch
;
; COMMON:
;       None.
;
; RESTRICTIONS: 
;       None.
;
; SIDE EFFECTS:
;       None.
;
; HISTORY:
;       Version 1, April 7, 2002, Dietmar Germerott, MPAE Lindau. Written
;
; CONTACT:
;       Dietmar Germerott, MPAE Lindau (germerott@linmpi.mpg.de)
;-
;
   ON_ERROR, 2
   
   IF KEYWORD_SET(rawpath_) THEN rawpath = rawpath_ ELSE rawpath = 'sumer_raw'
    
   cont_flag = 1
   now_flag = SYSTIME(1)-200.
   firstrun = 1
   
   IF KEYWORD_SET(filespec_) THEN fspec = filespec_ ELSE fspec = 'sc1*.*'
   WHILE cont_flag DO BEGIN
      
      IF ((SYSTIME(1)-now_flag) GE 150) THEN BEGIN ;wait 2 1/2 Minutes to close next TM
         now_flag = SYSTIME(1)
         fx = FINDFILE(concat_dir(rawpath, fspec), count=raw_fnum)
         last_file = concat_dir('cd_image_loc', 'lastprocfile.txt')
         flast = FINDFILE(last_file, count=lfnum)
         
         IF lfnum GT 0 THEN BEGIN
            
            instr = ''
            OPENR, uin, flast(0), /GET_LUN
            READF, uin, instr
            FREE_LUN, uin
            
            IF firstrun THEN BEGIN
               IF KEYWORD_SET(lastagain_) THEN lfc = 0 ELSE lfc = 1
            ENDIF ELSE BEGIN
               lfc = 1
            ENDELSE
            
            IF instr EQ 'NOFILE' THEN BEGIN
               cc = 0
               cnt = 1
            ENDIF ELSE BEGIN
               
               cc = WHERE(STRPOS(fx, instr) GE 0, cnt)
            ENDELSE
            
            IF ((cnt GT 0) AND(N_ELEMENTS(fx) GE cc(0) ))THEN BEGIN
               fx = fx(cc(0)+lfc:*)
               raw2bin, fx, /SAVE, out_path=out_path, nohkall=nohkall
               
            ENDIF
            
         ENDIF
      ENDIF
      
      IF KEYWORD_SET(once_) THEN cont_flag = 0 ;stop it after one run
      ;HELP, fx
      IF KEYWORD_SET(nointer) THEN BEGIN
         cont_flag = 0 ;end here if run from batch
                       ; and skipp Keyboard reading
      ENDIF ELSE BEGIN
        inp = GET_KBRD(0)
       
        CASE STRUPCASE(inp) OF 
           'Q':cont_flag = 0
           'W':WAIT,10
           'H':HELP,fx
           'D':debug_mode = 1
           'S':stop
           ELSE:
        ENDCASE
      endelse
   ENDWHILE
   
END


;---------------------------------------------------------------------------
; End of 'GET_RAWDATA.PRO'.
;---------------------------------------------------------------------------
