;+
; NAME:
;     THM_LSP_CHECKARG_TVAR (PROCEDURE)
;
; PURPOSE:
;     A utility to check whether the given argument is a valid tplot variable. A
;     tplot variable is specified by a tplot name such as 'tha_efp'
;
; CALLING SEQUENCE:
;     thm_lsp_checkarg_tvar, tvar, error, errmsg
;
; ARGUMENTS:
;     tvar: (INPUT, REQUIRED) The argument to be checked. It must be a scalar or
;           1-element array.
;     error: (OUTPUT, REQUIRED) The error code returned to the caller. If tvar
;           is a valid tplot variable, error=0. If not, error = 1. 
;     errmsg: (OUTPUT, REQUIRED) The error message returned to the caller. If
;           error = 0, errmsg=''. Otherwise a string containing error info is
;           returned.
;
; KEYWORDS:
;     None.
;
; EXAMPLES:
;     thm_lsp_checkarg_tvar, tvar, error, errmsg
;     if error ne 0 then print, errmsg
;
; HISTORY:
;     2010-03-06: Created by JBT, CU/LASP.
;
;-

pro thm_lsp_checkarg_tvar, tvar, error, errmsg

if n_elements(tvar) eq 0 then begin
   message, 'THM_LSP_CHECKARG_TVAR: ' + $
      'No argument is provided.'
endif

if not arg_present(error) then begin
   message, 'THM_LSP_CHECKARG_TVAR: ' + $
      'The ERROR and the ERRMSG arguments are not supplied.'
endif

if not arg_present(errmsg) then begin
   message, 'THM_LSP_CHECKARG_TVAR: ' + $
      'The ERRMSG argument is not supplied.'
endif

; Check type.
tmp = size(tvar, /type)
if tmp ne 7 then begin
   error = 1
   errmsg = 'The tvar to be checked is not a string.'
   return
endif

; Check element numbers.
tmp = n_elements(tvar)
if tmp ne 1 then begin
   error = 1
   errmsg = 'The tvar to be checked has more than one element.'
   return
endif

; Check if tvar is on the memory.
tmp = tnames(tvar)
if strlen(tmp) eq 0 then begin
   error = 1
   errmsg = 'The tvar to be checked is not in the memory.'
   return
endif

error = 0
errmsg = ''

end