subr imagelist_save_cb
 ty,'imagelist_save_cb called'
 ;get and open the save file
 name = xmtextfieldgetstring($imagelistsavefname)
 if filesize(name) gt 0 then {
    sq = sprintf('File %s already exists\nAre you sure you want to overwrite it?',name)
   xmprompt,sq,'N','prompt_cb',1,$f7,'',200,200
   xtloop, 1
   ;we get here after the prompt_cb lets us (it sets !motif = 0)
   !motif = 1
   if reply_was_not_yes() then return
 }
 lun = get_lun()
 if lun lt 0 then { errormess,'no available LUN for file write!' return }
 stat = openw(lun, name)
 if stat ne 1 then {
 	errormess,sprintf('can''t write to file: %s\ncheck name and permissions\n',name)
 	close, lun
	return }

 i = $list_selected - 1
 type = $image_list(0,i)
 nim = $image_list(1,i)
 if (type eq 1) then {
  ;TRACE raw hourly list require a filename and a # in the file
  selected_tops = eval(sprintf('$image_list_tops%d',i))
  selected_image_files = eval(sprintf('$image_list_files%d',i))
  selected_image_nums = eval(sprintf('$image_list_nums%d',i))
  nfiles = num_elem(selected_image_files)
  nselected = num_elem(selected_image_nums)
  nf = 0
  k = 0
  current_top = -1
  for i= 0, nim - 1 do {
  if i gt current_top then {
   ;need a new file
   fname = selected_image_files(nf)
   ;;ty,'new file = ', fname
   current_top = selected_tops(nf)
   fname = selected_image_files(nf)
   nf += 1
  }
  j = selected_image_nums(i)
  ;now have file name in fname and image # in j
  printf,lun,sprintf('%s %d', fname, j)
  }
 }
 ;need to do something with types 5 and 6
 if (type eq 2 or type eq 5 or type eq 6 or type eq 7) then {
  ;these just require a file name
  for j= 0, nim - 1 do {
  printf,lun, subsc(eval(sprintf('$image_list_files%d',i)), j)
  }
 }
 close, lun
 endsubr
 ;=======================================================================
