;+
;NAME:
; thm_ui_wavelet
;PURPOSE:
; wavelet transform of the input tplot variables, fist calls split_vec
; to split up any multi-component vectors.
;CALLING SEQUENCE:
; thm_ui_wavelet, vnames, new_names, polar=polar,_extra=_extra
;INPUT:
; vnames = the tplot variable names to transform
;OUTPUT:
; new_names = new variable names generated by the process
;KEYWORDS:
; polar = if set, passes the /polar keyword into the split_vec
;         procedure, new variable names for 3-d data will be polar,
;         and not '_x','_y','_z'
; gui_id = the main GUI widget ID, for messages
; messw_id = the dproc widget id, for messages
;HISTORY:
; 12-mar-2007, jmm, jimm@ssl.berkeley.edu
; 5-jun-2007, jmm, no longer handles history
;
;$LastChangedBy$
;$LastChangedDate$
;$LastChangedRevision$
;$URL$
;-
Pro thm_ui_wavelet, vnames, new_names, trange, polar = polar, $
                    gui_id = gui_id, messw_id = messw_id, $
                    _extra = _extra

  If(is_string(vnames) Eq 0) Then Begin
    message, /info, 'No Active variable name is set'
    If(keyword_set(gui_id)) Then $
      thm_ui_update_progress, gui_id, 'THM_UI_WAVELET: No Active variable name set'
    If(keyword_set(messw_id)) Then $
      widget_control, messw_id, set_val = 'THM_UI_WAVELET: No Active variable name set'
    new_names = ''
    return
  Endif
  n =  n_elements(vnames)
  tn0 = tnames()                ;we'll need to know this
  new_names = ''
  For j = 0, n-1 Do Begin
    tn1 = tnames() ;and we'll need to keep track of these for each point
    get_data, vnames[j], data = data ;first call split_vec, if necessary
    If(is_struct(data)) Then Begin 
      ndj = n_elements(data.y[0, *])
      If(ndj Eq 3) Then Begin
        split_vec, vnames[j], polar = polar, names_out = vn_j
      Endif Else If(ndj Gt 1) Then Begin
        split_vec, vnames[j], names_out = vn_j, $
          suffix = '_'+strcompress(string(indgen(ndj)), /remove_all)
      Endif Else vn_j = vnames[j]
    Endif Else vn_j = ''
;Do the transform
    If(is_string(vn_j)) Then Begin
      nvnj = n_elements(vn_j)
      For k = 0, nvnj-1 Do Begin
;test the data first
        get_data, vn_j[k], data = d
        If(is_struct(d)) Then Begin
          ok = where(d.x Gt trange[0] And d.x Le trange[1], nok)
          If(nok Gt 500000l) Then Begin
            message, /info, 'Warning: '+strcompress(string(nok))+' May be too many data points'
            If(keyword_set(gui_id)) Then $
              thm_ui_update_progress, gui_id, 'THM_UI_WAVELET: Warning: '+strcompress(string(nok))+' May be too many data points'
            If(keyword_set(messw_id)) Then $
              widget_control, messw_id, set_val = 'THM_UI_WAVELET: Warning: '+strcompress(string(nok))+' May be too many data points'
          Endif Else Begin
            If(keyword_set(gui_id)) Then $
              thm_ui_update_progress, gui_id, 'THM_UI_WAVELET: Processing: '+vn_j[k]
            If(keyword_set(messw_id)) Then $
              widget_control, messw_id, set_val = 'THM_UI_WAVELET: Processing: '+vn_j[k]
          Endelse
          wav_data, vn_j[k], trange = trange
          new_names = [new_names, vn_j[k]+'_wv_pow']
        Endif Else Begin
          If(keyword_set(gui_id)) Then $
            thm_ui_update_progress, gui_id, 'THM_UI_WAVELET: No data for: '+vn_j[k]
          If(keyword_set(messw_id)) Then $
            widget_control, messw_id, set_val = 'THM_UI_WAVELET: No Data for: '+vn_j[k]
          wav_data, vn_j[k], trange = trange
        Endelse
      Endfor
    Endif
  Endfor
;What are the new names?
  If(n_elements(new_names) Gt 1) Then new_names = new_names[1:*]
  Return
End