subr fdmgapplyselect_cb
 ;adapted from the TRACE selector
 ;actually apply the selection and load a list
 ;widget (or append to one) with the results
 ;start up the selected list widget if not already done

 fdmg_selected_list_wq
 
 ;2/28/2002 - also a minimum delta time
 s = xmtextfieldgetstring($fdmg_select_time_txt(8))
 decode_time_str, s, h3, m3, s3
 min_time_bwt = s3 + 60.*m3 + 3600.*h3
 ;need to erase previous? (if any)
 if $fdmg_select_add_flag eq 0 then {
   fdmgselect_eraseall_cb
   $fdmg_select_count = 0
   $fdmg_times = 0
   $fdmg_xc = 0
   $fdmg_yc = 0
   $fdmg_pixsc = 0
   last_tai = 0		;used by the minimum delta t check
  }
 
 $fdmgselect_stop = 0
 change_button_label, $fdmgapplyselect_but, 'STOP', 'red'
 
 ;these data are organized with a directory per month, so the first step
 ;is to figure out the first and last directory to search
 
 time_span, $fdmg_select_time_txt, t1, t2, t1r, t2r
 if t1 gt t2 then { errormess, 'start time is\nbefore end time\ntry again'
 	return }
 tt = t1r
 y1 = fix(xmtextfieldgetstring($fdmg_select_time_txt(0)))
 mon1 = fix(xmtextfieldgetstring($fdmg_select_time_txt(1)))
 ;for MDI, we expect nothing before 1996
 y1 = y1 > 1996

 y2 = fix(xmtextfieldgetstring($fdmg_select_time_txt(4)))
 mon2 = fix(xmtextfieldgetstring($fdmg_select_time_txt(5)))
 if y2 lt 1996 then { errormess, 'no MDI FDMG''s\nbefore 1996\ntry again'
 	return }
 ;we also need the dom, h, and minute
 dhm1 = 10000*fix(xmtextfieldgetstring($fdmg_select_time_txt(2)))
 s = xmtextfieldgetstring($fdmg_select_time_txt(3))
 decode_time_str, s, h, m, sec
 dhm1 = dhm1 + 100*h + m
 dhm2 = 10000*fix(xmtextfieldgetstring($fdmg_select_time_txt(6)))
 s = xmtextfieldgetstring($fdmg_select_time_txt(7))
 decode_time_str, s, h, m, sec
 dhm2 = dhm2 + 100*h + m
 ;construct and loop through all possible directories and then check
 ;each file in a choosen directory
 ym = y1 * 100 + mon1	;normally a 6 digit #
 mm = mon1
 yy = y1
 ym2 = y2 * 100 + mon2
 while (ym le ym2) {
  ;allow a stop from the button
  xtloop, 2
  if $fdmgselect_stop then break	;stop, but keep what we have
  files = getfiles($fdmg_base + sprintf('m%0.6d',ym), 500)
  if isstrarr(files) then {
    files = sort(files)
    ;make sure no strays in here of course
    nf = num_elem(files)
    ind = indgen(lonarr(nf))
    for i=0,nf-1 do { if strpos(files(i), 'fdmg') lt 0 then ind(i) = -1 }
    ind = sieve(ind, ind ge 0)
    files = files(ind)  nf = num_elem(files)
    ;L.S.'s file names will sort in chronological order and the time can
    ;be derived from the name (nice work Louie, it makes it easier)
      ind = indgen(lonarr(nf))
      for i=0,nf-1 do {
	;we assume that the file name has the time in a particular format
	tai = tai_from_file_name(files(i), 0)
	;if we are using a minimum time between choices, need to do more work
	ty, date_from_tai(tai)
	if tai lt t1 or tai gt t2 then ind(i) = -1 else {
         if tai lt (last_tai + min_time_bwt) then ind(i) = -1 else last_tai = tai
	}
	}
      }
      ind = sieve(ind, ind ge 0)
      files = files(ind)  nf = num_elem(files)
  if nf gt 0 then {
   ;add these files to our list
   if $fdmg_select_count then {
    ;append to them
    $fdmg_names = [$fdmg_names, strarr(nf)]
    $fdmg_times = [$fdmg_times, dblarr(nf)]
    $fdmg_xc = [$fdmg_xc, fltarr(nf)]
    $fdmg_yc = [$fdmg_yc, fltarr(nf)]
    $fdmg_pixsc = [$fdmg_pixsc, fltarr(nf)]
   } else {
    ;init the times, positions, and scale
    $fdmg_names = strarr(nf)
    $fdmg_times = dblarr(nf)
    $fdmg_xc = fltarr(nf)
    $fdmg_yc = fltarr(nf)
    $fdmg_pixsc = fltarr(nf)
   }
   for i=0,nf-1 do {
   xmlistadditem, $fdmgselectedwidget, files(i)
   ;also get the times, positions, and scales
   j = i + $fdmg_select_count
   ;and try to get times and such
   iq = data_type(files(i), header, params)
   $fdmg_names(j) = files(i)
   ;only try for times, etc if a FITS file, could extend to others?
   if iq eq 1 then {
    fits_time_decode, header
    $fdmg_times(j) = $data_time_tai
    $fdmg_xc(j) = $data_xcen
    $fdmg_yc(j) = $data_ycen
    $fdmg_pixsc(j) = $data_xscale
   }
   }
   $fdmg_select_count += nf	
   xmsetlabel, $fdmgselect_lab1, sprintf('%d files', $fdmg_select_count)
   } 
  mm = mm + 1   if (mm gt 12) then { mm = 1   yy = yy + 1}
  ym = yy * 100 + mm
 }
 change_button_label, $fdmgapplyselect_but, 'apply selection', 'darkgreen'
 xmsetlabel, $fdmgselect_lab1, sprintf('%d files', $fdmg_select_count)
 $fdmgselect_stop = 1

 ;don't use if no items selected
 if $fdmg_select_count le 0 then {
  errormess,'Sorry, nothing found'
  return }
 ;now we can put it in the list of lists
 add_selection_to_listoflists, 4
 endsubr
;---------------------------------------------------------------
