;+
; NAME:
;     WAPCAL
; PURPOSE:
;     Supervises insertion of calibration data from Reference Calibration
;     AMPHIT files, created by WAMPHIT, into AMPCAL and PHZCAL records.
; CATEGORY:
;     OVSA APC CALIBRATION ANALYSIS
; CALLING SEQUENCE:
;     wapcal
; INPUTS:
;     None
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
;     apcal_add_2mant and apcal_add_27mant OR apcal_add_2m_rl and apcal_add_27m_rl
; OUTPUTS:
; COMMENTS:
; SIDE EFFECTS:
;     updates existing ampcal and phzcal .rec files
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 20-Jan-2014 by Dale E. Gary
;     09-Feb-2014  DG
;        Fixed crash when user cancels "get settings" file dialog
;     15-Feb-2014  DG
;        Add rec file templates to use
;-

PRO wapcalMain_event, event

  base = Event.handler
  stash = WIDGET_INFO(base, /CHILD)
  WIDGET_CONTROL, stash, GET_UVALUE=state, /NO_COPY
  
  WIDGET_CONTROL,Event.Id,GET_UVALUE=Ev
  
  CASE Ev OF
  
    'Amf12rli': BEGIN                      ;Event for AMF 12 RLI text
      WIDGET_CONTROL,Event.Id,GET_VALUE=amf12rli
      state.amf12rli = amf12rli[0]
    END
    'Amf12rliButton': BEGIN
      CATCH,err_stat
      if (err_stat eq 0) then begin
        file = dialog_pickfile(path=state.amf12rli,get_path=amf12rli)
        CATCH,/CANCEL
        state.amf12rli = file[0]
        WIDGET_CONTROL,state.Amf12rliID,SET_VALUE=file[0]
      endif else begin
        junk = widget_message(!ERR_STRING,/ERROR)
      endelse
    END
    
    'Amf124': BEGIN                      ;Event for AMF 124 text
      WIDGET_CONTROL,Event.Id,GET_VALUE=amf124
      state.amf124 = amf124[0]
    END
    'Amf124Button': BEGIN
      CATCH,err_stat
      if (err_stat eq 0) then begin
        file = dialog_pickfile(path=state.amf124,get_path=amf124)
        CATCH,/CANCEL
        state.amf124 = file[0]
        WIDGET_CONTROL,state.Amf124ID,SET_VALUE=file[0]
      endif else begin
        junk = widget_message(!ERR_STRING,/ERROR)
      endelse
    END

    'Amf125': BEGIN                      ;Event for AMF 125 text
      WIDGET_CONTROL,Event.Id,GET_VALUE=amf125
      state.amf125 = amf125[0]
    END
    'Amf125Button': BEGIN
      CATCH,err_stat
      if (err_stat eq 0) then begin
        file = dialog_pickfile(path=state.amf125,get_path=amf125)
        CATCH,/CANCEL
        state.amf125 = file[0]
        WIDGET_CONTROL,state.Amf125ID,SET_VALUE=file[0]
      endif else begin
        junk = widget_message(!ERR_STRING,/ERROR)
      endelse
    END
    
    'Amf126': BEGIN                      ;Event for AMF 126 text
      WIDGET_CONTROL,Event.Id,GET_VALUE=amf126
      state.amf126 = amf126[0]
    END
    'Amf126Button': BEGIN
      CATCH,err_stat
      if (err_stat eq 0) then begin
        file = dialog_pickfile(path=state.amf126,get_path=amf126)
        CATCH,/CANCEL
        state.amf126 = file[0]
        WIDGET_CONTROL,state.Amf126ID,SET_VALUE=file[0]
      endif else begin
        junk = widget_message(!ERR_STRING,/ERROR)
      endelse
    END
    
    'Amf127': BEGIN                      ;Event for AMF 127 text
      WIDGET_CONTROL,Event.Id,GET_VALUE=amf127
      state.amf127 = amf127[0]
    END
    'Amf127Button': BEGIN
      CATCH,err_stat
      if (err_stat eq 0) then begin
        file = dialog_pickfile(path=state.amf127,get_path=amf127)
        CATCH,/CANCEL
        state.amf127 = file[0]
        WIDGET_CONTROL,state.Amf127ID,SET_VALUE=file[0]
      endif else begin
        junk = widget_message(!ERR_STRING,/ERROR)
      endelse
    END
    
    'Amf128': BEGIN                      ;Event for AMF 128 text
      WIDGET_CONTROL,Event.Id,GET_VALUE=amf128
      state.amf128 = amf128[0]
    END
    'Amf128Button': BEGIN
      CATCH,err_stat
      if (err_stat eq 0) then begin
        file = dialog_pickfile(path=state.amf128,get_path=amf128)
        CATCH,/CANCEL
        state.amf128 = file[0]
        WIDGET_CONTROL,state.Amf128ID,SET_VALUE=file[0]
      endif else begin
        junk = widget_message(!ERR_STRING,/ERROR)
      endelse
    END
    
    'Ampcal': BEGIN                      ;Event for AMPCAL text
      WIDGET_CONTROL,Event.Id,GET_VALUE=ampcal
      state.ampcal = ampcal[0]
    END
    'AmpcalButton': BEGIN
      CATCH,err_stat
      if (err_stat eq 0) then begin
        file = dialog_pickfile(path=state.ampcal,get_path=ampcal)
        CATCH,/CANCEL
        state.ampcal = file[0]
        WIDGET_CONTROL,state.AmpcalID,SET_VALUE=file[0]
      endif else begin
        junk = widget_message(!ERR_STRING,/ERROR)
      endelse
    END
    
    'Phzcal': BEGIN                      ;Event for PHZCAL text
      WIDGET_CONTROL,Event.Id,GET_VALUE=phzcal
      state.phzcal = phzcal[0]
    END
    'PhzcalButton': BEGIN
      CATCH,err_stat
      if (err_stat eq 0) then begin
        file = dialog_pickfile(path=state.phzcal,get_path=phzcal)
        CATCH,/CANCEL
        state.phzcal = file[0]
        WIDGET_CONTROL,state.PhzcalID,SET_VALUE=file[0]
      endif else begin
        junk = widget_message(!ERR_STRING,/ERROR)
      endelse
    END

    'Ampcaltmp': BEGIN                      ;Event for AMPCAL text
      WIDGET_CONTROL,Event.Id,GET_VALUE=ampcaltmp
      state.ampcal = ampcaltmp[0]
    END
    'AmpcaltmpButton': BEGIN
      CATCH,err_stat
      if (err_stat eq 0) then begin
        file = dialog_pickfile(path=state.ampcaltmp,get_path=ampcaltmp)
        CATCH,/CANCEL
        state.ampcaltmp = file[0]
        WIDGET_CONTROL,state.AmpcaltmpID,SET_VALUE=file[0]
      endif else begin
        junk = widget_message(!ERR_STRING,/ERROR)
      endelse
    END
    
    'Phzcaltmp': BEGIN                      ;Event for PHZCAL text
      WIDGET_CONTROL,Event.Id,GET_VALUE=phzcaltmp
      state.phzcaltmp = phzcaltmp[0]
    END
    'PhzcaltmpButton': BEGIN
      CATCH,err_stat
      if (err_stat eq 0) then begin
        file = dialog_pickfile(path=state.phzcaltmp,get_path=phzcaltmp)
        CATCH,/CANCEL
        state.phzcaltmp = file[0]
        WIDGET_CONTROL,state.PhzcaltmpID,SET_VALUE=file[0]
      endif else begin
        junk = widget_message(!ERR_STRING,/ERROR)
      endelse
    END
    
    'BGROUP43': BEGIN                   ; A button was pressed...
      CASE Event.Value OF
      0: BEGIN                          ; Get Settings button
         file = dialog_pickfile(filter='*.inp',/read)
         if file ne '' then begin
            str = ''
            openr,lun,/get_lun,file
            readf,lun,str & state.amf12rli = str
            readf,lun,str & state.amf124 = str
            readf,lun,str & state.amf125 = str
            readf,lun,str & state.amf126 = str
            readf,lun,str & state.amf127 = str
            readf,lun,str & state.amf128 = str
            readf,lun,str & state.ampcal = str
            readf,lun,str & state.phzcal = str
            readf,lun,str & state.ampcaltmp = str
            readf,lun,str & state.phzcaltmp = str
            state.inpfile = file
            WIDGET_CONTROL,state.amf12rliid,SET_VALUE=state.amf12rli
            WIDGET_CONTROL,state.amf124id,SET_VALUE=state.amf124
            WIDGET_CONTROL,state.amf125id,SET_VALUE=state.amf125
            WIDGET_CONTROL,state.amf126id,SET_VALUE=state.amf126
            WIDGET_CONTROL,state.amf127id,SET_VALUE=state.amf127
            WIDGET_CONTROL,state.amf128id,SET_VALUE=state.amf128
            WIDGET_CONTROL,state.ampcalid,SET_VALUE=state.ampcal
            WIDGET_CONTROL,state.phzcalid,SET_VALUE=state.phzcal
            WIDGET_CONTROL,state.ampcaltmpid,SET_VALUE=state.ampcaltmp
            WIDGET_CONTROL,state.phzcaltmpid,SET_VALUE=state.phzcaltmp
            WIDGET_CONTROL,state.inpid, $
              SET_VALUE='Files from '+state.inpfile
            close,lun
            free_lun,lun
         endif
      END
        1: BEGIN                          ; Save Settings button
          file = dialog_pickfile(filter='*.inp',/write)
          openw,lun,/get_lun,file
          printf,lun,state.amf12rli,format='(a)'
          printf,lun,state.amf124,format='(a)'
          printf,lun,state.amf125,format='(a)'
          printf,lun,state.amf126,format='(a)'
          printf,lun,state.amf127,format='(a)'
          printf,lun,state.amf128,format='(a)'
          printf,lun,state.ampcal,format='(a)'
          printf,lun,state.phzcal,format='(a)'
          printf,lun,state.ampcaltmp,format='(a)'
          printf,lun,state.phzcaltmp,format='(a)'
          state.inpfile = file
          WIDGET_CONTROL,state.inpid, $
            SET_VALUE='Files from '+state.inpfile
          close,lun
          free_lun,lun
        END
        2: BEGIN                          ; RESET to Defaults button
          state.amf12rli = 'None'
          state.amf124 = 'None'
          state.amf125 = 'None'
          state.amf126 = 'None'
          state.amf127 = 'None'
          state.amf128 = 'None'
          state.ampcal = 'None'
          state.phzcal = 'None'
          state.ampcaltmp = 'None'
          state.phzcaltmp = 'None'
          state.inpfile = 'empty.inp'
          WIDGET_CONTROL,state.amf12rliid,SET_VALUE=state.amf12rli
          WIDGET_CONTROL,state.amf124id,SET_VALUE=state.amf124
          WIDGET_CONTROL,state.amf125id,SET_VALUE=state.amf125
          WIDGET_CONTROL,state.amf126id,SET_VALUE=state.amf126
          WIDGET_CONTROL,state.amf127id,SET_VALUE=state.amf127
          WIDGET_CONTROL,state.amf128id,SET_VALUE=state.amf128
          WIDGET_CONTROL,state.ampcalid,SET_VALUE=state.ampcal
          WIDGET_CONTROL,state.phzcalid,SET_VALUE=state.phzcal
          WIDGET_CONTROL,state.ampcaltmpid,SET_VALUE=state.ampcaltmp
          WIDGET_CONTROL,state.phzcaltmpid,SET_VALUE=state.phzcaltmp
          WIDGET_CONTROL,state.inpid, $
            SET_VALUE='Files from '+state.inpfile
        END
        3: BEGIN                            ; Set AMF Files button
        
          ; Set AMF button pressed, so allow user to select multiple
          ; files and set the system variables accordingly
          filelist = dialog_pickfile(path=!defaults.datadir,filter='*.amf',/multi,/read,$
            title='Select all .AMF files you wish to include.')
          if (filelist[0] ne '') then begin
            filelist = filelist[sort(filelist)]
            print,filelist
            for i = 0, n_elements(filelist)-1 do begin
               break_file,filelist[i],disk,dir,stem,ext
               print,stem
               if strpos(stem,'124') ne -1 then begin 
                  state.amf124 = filelist[i]
                  WIDGET_CONTROL,state.amf124id,SET_VALUE=state.amf124
               endif else if strpos(stem,'125') ne -1 then begin
                 state.amf125 = filelist[i]
                 WIDGET_CONTROL,state.amf125id,SET_VALUE=state.amf125
               endif else if strpos(stem,'126') ne -1 then begin
                 state.amf126 = filelist[i]
                 WIDGET_CONTROL,state.amf126id,SET_VALUE=state.amf126
               endif else if strpos(stem,'127') ne -1 then begin
                 state.amf127 = filelist[i]
                 WIDGET_CONTROL,state.amf127id,SET_VALUE=state.amf127
               endif else if strpos(stem,'128') ne -1 then begin
                 state.amf128 = filelist[i]
                 WIDGET_CONTROL,state.amf128id,SET_VALUE=state.amf128
               endif else if strpos(stem,'12rli') ne -1 then begin
                 state.amf12rli = filelist[i]
                 WIDGET_CONTROL,state.amf12rliid,SET_VALUE=state.amf12rli
               endif
            endfor
          endif
          
        END
        4: BEGIN                            ; Set REC Files button
        
        ; Set REC button pressed, so allow user to select multiple
        ; files and set the system variables accordingly
        filelist = dialog_pickfile(path=!defaults.datadir,filter='*.rec',/multi,/read,$
          title='Select all .REC files you wish to include.')
        if (filelist[0] ne '') then begin
          filelist = filelist[sort(filelist)]
          for i = 0, n_elements(filelist)-1 do begin
            break_file,filelist[i],disk,dir,stem,ext
            if strpos(stem,'ampcal_template') ne -1 then begin
               state.ampcaltmp = filelist[i]
               WIDGET_CONTROL,state.ampcaltmpid,SET_VALUE=state.ampcaltmp
            endif else if strpos(stem,'phzcal_template') ne -1 then begin
               state.phzcaltmp = filelist[i]
               WIDGET_CONTROL,state.phzcaltmpid,SET_VALUE=state.phzcaltmp
            endif else if strpos(stem,'ampcal') ne -1 then begin
               state.ampcal = filelist[i]
               WIDGET_CONTROL,state.ampcalid,SET_VALUE=state.ampcal
            endif else if strpos(stem,'phzcal') ne -1 then begin
               state.phzcal = filelist[i]
               WIDGET_CONTROL,state.phzcalid,SET_VALUE=state.phzcal
            endif
          endfor
        endif
        END
        5: BEGIN                            ; GO button
        
          ; Now actually do the analysis
          ; First do quality checks on existence of files
          if not file_test(state.ampcaltmp) then begin
             print,'WAPCAL Error: Ampcal Template file ',state.ampcaltmp,' does not exist'
             goto,WRITE_STATE
          endif
          if not file_test(state.phzcaltmp) then begin
             print,'WAPCAL Error: Phzcal Template file ',state.phzcaltmp,' does not exist'
             goto,WRITE_STATE
          endif
          if state.amf12rli eq 'None' then begin
             ; If the amf12rli file does not exist, this is the indication that we
             ; are dealing with R, L data (after 2004.231).
             filelist = []
             file_delete,'out12.sav',/allow_nonexistent
             if state.amf124 ne 'None' then begin
                if not file_exist(state.amf124) then begin
                   print,'WAPCAL Warning: 124 amf file ',state.amf124,' does not exist. Skipping...'
                endif else begin
                   apcal_add_2m_rl,filename=state.amf124,out12='out12.sav',ampfile=state.ampcaltmp,phzfile=state.phzcaltmp
                   filelist = [filelist,state.amf124]
                endelse
             endif
             if state.amf125 ne 'None' then begin
                if not file_exist(state.amf125) then begin
                   print,'WAPCAL Warning: 125 amf file ',state.amf125,' does not exist. Skipping...'
                endif else begin
                   apcal_add_2m_rl,filename=state.amf125,out12='out12.sav',ampfile=state.ampcaltmp,phzfile=state.phzcaltmp
                   filelist = [filelist,state.amf125]
                endelse
             endif
             if state.amf126 ne 'None' then begin
                if not file_exist(state.amf126) then begin
                   print,'WAPCAL Warning: 126 amf file ',state.amf126,' does not exist. Skipping...'
                endif else begin
                   apcal_add_2m_rl,filename=state.amf126,out12='out12.sav',ampfile=state.ampcaltmp,phzfile=state.phzcaltmp
                   filelist = [filelist,state.amf126]
                endelse
             endif
             if state.amf127 ne 'None' then begin
                if not file_exist(state.amf127) then begin
                   print,'WAPCAL Warning: 127 amf file ',state.amf127,' does not exist. Skipping...'
                endif else begin
                   apcal_add_2m_rl,filename=state.amf127,out12='out12.sav',ampfile=state.ampcaltmp,phzfile=state.phzcaltmp
                   filelist = [filelist,state.amf127]
                endelse
             endif
             if state.amf128 ne 'None' then begin
                if not file_exist(state.amf128) then begin
                   print,'WAPCAL Warning: 128 amf file ',state.amf128,' does not exist. Skipping...'
                endif else begin
                   apcal_add_2m_rl,filename=state.amf128,out12='out12.sav',ampfile=state.ampcaltmp,phzfile=state.phzcaltmp
                   filelist = [filelist,state.amf128]
                endelse
             endif
             if n_elements(filelist) eq 0 then begin
                print,'WAPCAL Error: No files to process'
                goto,WRITE_STATE
             endif
             apcal_add_27m_rl,filelist=filelist,savfile='out12.sav',ampfile=state.ampcaltmp,phzfile=state.phzcaltmp
             ; Copy completed template files to output files
             file_copy,state.ampcaltmp,state.ampcal
             file_copy,state.phzcaltmp,state.phzcal
             goto,WRITE_STATE
          endif
          ; If we reach here, then this must be RLI data from before 2004.231.  First process 2m antenna files
          file_delete,'out12.sav',/allow_nonexistent
          if state.amf124 ne 'None' then begin
             if not file_exist(state.amf124) then begin
                print,'WAPCAL Warning: 124 amf file ',state.amf124,' does not exist. Skipping...'
             endif else begin
                apcal_add_2mant,filename=state.amf124,out12='out12.sav',ampfile=state.ampcaltmp,phzfile=state.phzcaltmp
             endelse
          endif
          if state.amf125 ne 'None' then begin
             if not file_exist(state.amf125) then begin
                print,'WAPCAL Warning: 125 amf file ',state.amf125,' does not exist. Skipping...'
             endif else begin
                apcal_add_2mant,filename=state.amf125,out12='out12.sav',ampfile=state.ampcaltmp,phzfile=state.phzcaltmp
             endelse
          endif
          if state.amf126 ne 'None' then begin
             if not file_exist(state.amf126) then begin
                print,'WAPCAL Warning: 126 amf file ',state.amf126,' does not exist. Skipping...'
             endif else begin
                apcal_add_2mant,filename=state.amf126,out12='out12.sav',ampfile=state.ampcaltmp,phzfile=state.phzcaltmp
             endelse
          endif
          if state.amf127 ne 'None' then begin
             if not file_exist(state.amf127) then begin
                print,'WAPCAL Warning: 127 amf file ',state.amf127,' does not exist. Skipping...'
             endif else begin
                apcal_add_2mant,filename=state.amf127,out12='out12.sav',ampfile=state.ampcaltmp,phzfile=state.phzcaltmp
             endelse
          endif
          if state.amf128 ne 'None' then begin
             if not file_exist(state.amf128) then begin
                print,'WAPCAL Warning: 128 amf file ',state.amf128,' does not exist. Skipping...'
             endif else begin
                apcal_add_2mant,filename=state.amf128,out12='out12.sav',ampfile=state.ampcaltmp,phzfile=state.phzcaltmp
             endelse
          endif
          if not file_test(state.amf12rli) then begin
             print,'WAPCAL Error: RLI amf file ',state.amf12rli,' do not exist.  Skipping...'
          endif else begin
             apcal_add_27mant,filename=state.amf12rli,savfile='out12.sav',ampfile=state.ampcaltmp,phzfile=state.phzcaltmp
          endelse
          file_copy,state.ampcaltmp,state.ampcal
          file_copy,state.phzcaltmp,state.phzcal
          goto,WRITE_STATE
          
        END
        6: BEGIN                             ; Exit button
        
          ; Exit button pressed, so exit.
          
          WIDGET_CONTROL,Event.Top,/DESTROY
          return
          
        END
        ELSE: Message,'Unknown button pressed'
      ENDCASE
    END
  ENDCASE
  
  WRITE_STATE:
  WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY
  
END


pro wapcal, infile, group = Group

   ; Set variables with defaults
   amf12rli = 'None'
   amf124   = 'None'
   amf125   = 'None'
   amf126   = 'None'
   amf127   = 'None'
   amf128   = 'None'
   ampcal   = 'None'
   phzcal   = 'None'
   ampcaltmp= 'None'
   phzcaltmp= 'None'
   inpfile  = 'None'
   
   ; Read preferences from infile, or use defaults if an error occurs
   
   CATCH,err_stat
   if (n_elements(infile) eq 0) then err_stat=1   ; Force defaults if no infile
   if (err_stat eq 0) then begin
     openr,lun,/get_lun,infile
     readf,lun,amf12rli
     readf,lun,amf124
     readf,lun,amf125
     readf,lun,amf126
     readf,lun,amf127
     readf,lun,amf128
     readf,lun,ampcal
     readf,lun,phzcal
     readf,lun,ampcaltmp
     readf,lun,phzcaltmp
     inpfile = infile
     close,lun
     free_lun,lun
     CATCH,/CANCEL
   endif else begin
     CATCH,/CANCEL
   endelse
   
   IF N_ELEMENTS(Group) EQ 0 THEN GROUP=0
   
   junk   = { CW_PDMENU_S, flags:0, name:'' }
   
   font = !DEFAULTS.FONT
   
   WAPCalMain = WIDGET_BASE(GROUP_LEADER=Group, $
     COLUMN=1, $
     MAP=1, $
     TITLE='WAPCal', $
     UVALUE='WAPCal')
     
   BASE2 = WIDGET_BASE(WAPCalMain, $
     COLUMN=1, $
     MAP=1, $
     TITLE='ExtraBase', $
     UVALUE='BASE2')
     
   BASE4 = WIDGET_BASE(BASE2, $
     COLUMN=1, $
     FRAME=1, $
     MAP=1, $
     TITLE='DirBase', $
     UVALUE='BASE4')
     
   IF (inpfile EQ 'None') THEN vstr = ' from Defaults' $
   ELSE vstr = ' from '+inpfile
   LABEL5 = WIDGET_LABEL( BASE4, $
     UVALUE='LABEL5', $
     VALUE='Files'+vstr+'                              ')
     
   Amf12rliBase = WIDGET_BASE(BASE4, /ROW)
   Label = WIDGET_LABEL(Amf12rliBase, FONT=font, /ALIGN_RIGHT,XSIZE=150, VALUE='12 RLI')
   Amf12rliID = WIDGET_TEXT(Amf12rliBase,VALUE=amf12rli, /EDITABLE, UVALUE='Amf12rli', XSIZE=100, YSIZE=1)
   Amf12rliButton = WIDGET_BUTTON( Amf12rliBase, UVALUE='Amf12rliButton', VALUE=' Browse ')
    
   Amf124Base = WIDGET_BASE(BASE4, /ROW)
   Label = WIDGET_LABEL(Amf124Base, FONT=font, /ALIGN_RIGHT,XSIZE=150, VALUE='124')
   Amf124ID = WIDGET_TEXT(Amf124Base,VALUE=amf124, /EDITABLE, UVALUE='Amf124', XSIZE=100, YSIZE=1)
   Amf124Button = WIDGET_BUTTON( Amf124Base, UVALUE='Amf124Button', VALUE=' Browse ')
     
   Amf125Base = WIDGET_BASE(BASE4, /ROW)
   Label = WIDGET_LABEL(Amf125Base, FONT=font, /ALIGN_RIGHT,XSIZE=150, VALUE='125')
   Amf125ID = WIDGET_TEXT(Amf125Base,VALUE=amf125, /EDITABLE, UVALUE='Amf125', XSIZE=100, YSIZE=1)
   Amf125Button = WIDGET_BUTTON( Amf125Base, UVALUE='Amf125Button', VALUE=' Browse ')
   
   Amf126Base = WIDGET_BASE(BASE4, /ROW)
   Label = WIDGET_LABEL(Amf126Base, FONT=font, /ALIGN_RIGHT,XSIZE=150, VALUE='126')
   Amf126ID = WIDGET_TEXT(Amf126Base,VALUE=amf126, /EDITABLE, UVALUE='Amf126', XSIZE=100, YSIZE=1)
   Amf126Button = WIDGET_BUTTON( Amf126Base, UVALUE='Amf126Button', VALUE=' Browse ')
   
   Amf127Base = WIDGET_BASE(BASE4, /ROW)
   Label = WIDGET_LABEL(Amf127Base, FONT=font, /ALIGN_RIGHT,XSIZE=150, VALUE='127')
   Amf127ID = WIDGET_TEXT(Amf127Base,VALUE=amf127, /EDITABLE, UVALUE='Amf127', XSIZE=100, YSIZE=1)
   Amf127Button = WIDGET_BUTTON( Amf127Base, UVALUE='Amf127Button', VALUE=' Browse ')
   
   Amf128Base = WIDGET_BASE(BASE4, /ROW)
   Label = WIDGET_LABEL(Amf128Base, FONT=font, /ALIGN_RIGHT,XSIZE=150, VALUE='128')
   Amf128ID = WIDGET_TEXT(Amf128Base,VALUE=amf128, /EDITABLE, UVALUE='Amf128', XSIZE=100, YSIZE=1)
   Amf128Button = WIDGET_BUTTON( Amf128Base, UVALUE='Amf128Button', VALUE=' Browse ')
   
   AmpcalBase = WIDGET_BASE(BASE4, /ROW)
   Label = WIDGET_LABEL(AmpcalBase, FONT=font, /ALIGN_RIGHT,XSIZE=150, VALUE='Ampcal')
   AmpcalID = WIDGET_TEXT(AmpcalBase,VALUE=ampcal, /EDITABLE, UVALUE='Ampcal', XSIZE=100, YSIZE=1)
   AmpcalButton = WIDGET_BUTTON( AmpcalBase, UVALUE='AmpcalButton', VALUE=' Browse ')
   
   PhzcalBase = WIDGET_BASE(BASE4, /ROW)
   Label = WIDGET_LABEL(PhzcalBase, FONT=font, /ALIGN_RIGHT,XSIZE=150, VALUE='Phzcal')
   PhzcalID = WIDGET_TEXT(PhzcalBase,VALUE=ampcal, /EDITABLE, UVALUE='Phzcal', XSIZE=100, YSIZE=1)
   PhzcalButton = WIDGET_BUTTON( PhzcalBase, UVALUE='PhzcalButton', VALUE=' Browse ')
   
   AmpcaltmpBase = WIDGET_BASE(BASE4, /ROW)
   Label = WIDGET_LABEL(AmpcaltmpBase, FONT=font, /ALIGN_RIGHT,XSIZE=150, VALUE='Ampcal Template')
   AmpcaltmpID = WIDGET_TEXT(AmpcaltmpBase,VALUE=ampcaltmp, /EDITABLE, UVALUE='Ampcaltmp', XSIZE=100, YSIZE=1)
   AmpcaltmpButton = WIDGET_BUTTON( AmpcaltmpBase, UVALUE='AmpcaltmpButton', VALUE=' Browse ')
   
   PhzcaltmpBase = WIDGET_BASE(BASE4, /ROW)
   Label = WIDGET_LABEL(PhzcaltmpBase, FONT=font, /ALIGN_RIGHT,XSIZE=150, VALUE='Phzcal Template')
   PhzcaltmpID = WIDGET_TEXT(PhzcaltmpBase,VALUE=ampcal, /EDITABLE, UVALUE='Phzcaltmp', XSIZE=100, YSIZE=1)
   PhzcaltmpButton = WIDGET_BUTTON( PhzcaltmpBase, UVALUE='PhzcaltmpButton', VALUE=' Browse ')

   BASE42 = WIDGET_BASE(BASE2, $
     ROW=1, $
     MAP=1, $
     TITLE='ButtonBase', $
     UVALUE='BASE42')
     
   Btns2106 = [ $
     ' Get Settings', $
     ' Save Settings', $
     ' RESET to Defaults', $
     ' Set AMF Files ', $
     ' Set REC Files ', $
     ' GO ',     $
     ' Exit' ]
   BGROUP43 = CW_BGROUP( BASE42, Btns2106, $
     ROW=1, $
     FRAME=1, $
     UVALUE='BGROUP43')
     
     
   WIDGET_CONTROL, WAPCalMain, /REALIZE
   
   state = {amf12rli:amf12rli,amf124:amf124,amf125:amf125,amf126:amf126,$
            amf127:amf127,amf128:amf128,ampcal:ampcal,phzcal:phzcal,ampcaltmp:ampcaltmp,phzcaltmp:phzcaltmp,$
            inpfile:inpfile,Amf12rliID:Amf12rliID,Amf124ID:Amf124ID,$
            Amf125ID:Amf125ID,Amf126ID:Amf126ID,Amf127ID:Amf127ID,Amf128ID:Amf128ID,$
            AmpcalID:AmpcalID,PhzcalID:PhzcalID,AmpcaltmpID:AmpcaltmpID,PhzcaltmpID:PhzcaltmpID,inpid:LABEL5}
     
   WIDGET_CONTROL, WIDGET_INFO(WAPCalMain, /CHILD), SET_UVALUE=state, /NO_COPY
   
   XMANAGER, 'WAPCalMain', WAPCalMain, /NO_BLOCK
   return
end
