;+
; NAME:
;	SMEI_SEQUENCE__MODEL_MENU
;
;
; PURPOSE:
;	Select a background model set and subtract it.
;
;
; CATEGORY:
;	SMEI_SEQUENCE
;
;
; CALLING SEQUENCE:
;	seqref -> model_menu[, group = group]
;
;
; KEYWORD PARAMETERS:
;	group	long	A possible group leader for the menu
;
;
; SIDE EFFECTS:
;	A background is subtracted from the sequence.
;
;
; RESTRICTIONS:
;	Can only be applied to uncompacted sequences.
;
;
; MODIFICATION HISTORY:
;	Original: 7/5/04; SJT
;	Add ability to change model directory: 23/11/04; SJT
;-

pro make_model_list, dir, names, descs, projection = projection

models = file_search(dir+'*_dates.lis', count = nmodels)
if nmodels eq 0 then begin
    smei_msg, /alert, ['Could not find any models to subtract', $
                       'Check that they are correcly installed', $
                       'in '+dir]
    return
endif
names = strarr(nmodels+1)
descs = strarr(nmodels+1)
indat = intarr(5)
type = ''

for j = 0, nmodels-1 do begin
    names[j+1] = file_basename(models[j])
;strmid(models[j], strlen(dir), $
                 ;          strlen(models[j])-strlen(dir)-10)
    pu = strpos(names[j+1], '_', /reverse_search)
    names[j+1] = strmid(names[j+1], 0, pu)
    openr, ilu, /get, models[j]
    readf, ilu, indat, type
    free_lun, ilu
    descs[j+1] = names[j+1]+ $
                    string(indat[1:4], format = "(' M=',I2,' " + $
                           "S=',I2,' *=',I2,' G=',I2)")+type
endfor

case projection of
    0: locs = where(strpos(descs, 'Aitoff') ne -1, nv)
    1: locs = where(strpos(descs, 'Fisheye') ne -1, nv) 
    2: locs = where(strpos(descs, 'Rectangular') ne -1, nv)
endcase

if nv eq 0 then begin
    smei_msg, /alert, ['Could not find any models to subtract', $
                       'for this projection']
    return
endif
descs = descs[[0, locs]]
names = names[[0, locs]]

end

pro ss_model_event, event
sbase = widget_info(event.top, /child)
widget_control, sbase, get_uvalue = state
widget_control, event.id, get_uvalue = mnu

case mnu of
    "QUIT": begin
        if event.value eq 'DO' then (state.original) ->  $
          subtract_model, (*state.models)[state.modno], path = $
          state.modpath $
        else if event.value eq 'RESTORE' then (state.original) ->  $
          subtract_model, /restore
        widget_control, event.top, /destroy
        ptr_free, state.models
        return
    end
    "PATH": begin
        mod_dir = event.value
        if strlen(mod_dir) eq 0 or not file_test(mod_dir, /dir) then $
          return

        if strpos(mod_dir, '/', /reverse_search) ne strlen(mod_dir)-1 $
          then mod_dir += '/'

        make_model_list, mod_dir, modnames, moddescs, projection = $
                         state.projection

        widget_control, state.modid, set_value = moddescs, $
                        set_droplist_select = 0
        state.modpath = mod_dir
        state.modno = 0
        *state.models = modnames

    end
    "PICK": begin
        mod_dir = dialog_pickfile(/directory, $
                                  /must_exist, $
                                  title = 'Select model directory', $
                                  dialog_parent = base)
        if strlen(mod_dir) eq 0 or not file_test(mod_dir, /dir) then $
          return

        if strpos(mod_dir, '/', /reverse_search) ne strlen(mod_dir)-1 $
          then mod_dir += '/'

        make_model_list, mod_dir, modnames, moddescs, projection = $
                         state.projection

        widget_control, state.modid, set_value = moddescs, $
                        set_droplist_select = 0
        widget_control, state.pathid, set_value = mod_dir
        state.modpath = mod_dir
        state.modno = 0
        *state.models = modnames

    end
    "DEFAULT": begin
        bg_base = getenv('SMEI_BG_BASE')
        if bg_base eq '' then bg_base = getenv("SSWDB")+'/smei'
        if (state.original -> get_pipe_level() gt 1) then $
          mod_dir = bg_base+'/Background_models_2/' $
        else $
          mod_dir = bg_base+'/Background_models/'

        make_model_list, mod_dir, modnames, moddescs, projection = $
                         state.projection

        widget_control, state.modid, set_value = moddescs, $
                        set_droplist_select = 0
        widget_control, state.pathid, set_value = mod_dir
        state.modpath = mod_dir
        state.modno = 0
        *state.models = modnames

    end

    'MODEL': state.modno = event.index
endcase

widget_control, state.doid, sensitive = state.modno ne 0
widget_control, sbase, set_uvalue = state

end

pro smei_sequence::model_menu, group = group


bg_base = getenv('SMEI_BG_BASE')
if bg_base eq '' then bg_base = getenv("SSWDB")+'/smei'
if (self -> get_pipe_level() gt 1) then $
  mod_dir = bg_base+'/Background_models_2/' $
else $
  mod_dir = bg_base+'/Background_models/'

projection = self -> get_projection()

make_model_list, mod_dir, modnames, moddescs, projection = projection

base = widget_base(title = 'Background subtraction', $
                   /column, $
                   group = group)

label = widget_label(base, $
                     value = 'Background Subtraction')

jb = widget_base(base, $
                 /column, $
                 /frame)

pathid = cw_ffield(jb, $
                 /text, $
                 xsize = 40, $
                 label = "Models directory:", $
                 value = mod_dir, $
                 uvalue = 'PATH', $
                 /box)
jbb = widget_base(jb, $
                  /row)
junk = widget_button(jbb, $
                     value = 'Select...', $
                     uvalue = 'PICK')
junk = widget_button(jbb, $
                     value = 'Default path', $
                     uvalue = 'DEFAULT')

modid = widget_droplist(base, $
                       value = moddescs, $
                       uvalue = 'MODEL', $
                       title = 'Select background model')

junk = cw_bgroup(base, $
                 /row, $
                 ['Apply', 'Cancel', 'Restore Original'], $
                 button_uvalue = ['DO', 'DONT', 'RESTORE'], $
                 ids = bids, $
                 uvalue = 'QUIT')

state = {ss_model_state, $
         original: self, $
         modid: modid, $
         pathid: pathid, $
         doid: bids[0], $
         modno: 0, $
         modpath: '', $
         models: ptr_new(modnames), $
         projection: projection}

widget_control, label, set_uvalue = state
widget_control, base, /real

xmanager, "ss_model", base, /no_block

end
