
PRO CHOOSE_IONEQ, IZ, ION, TEMP_ALL, IONEQ, USER_SELECT=USER_SELECT, $
             IONEQ_NAME=IONEQ_NAME

;+
; PROJECT:  CHIANTI
;
;       CHIANTI is an atomic database package for the calculation of
;       astrophysical emission line spectra.  It is a collaborative project
;       involving Ken Dere (Naval Research Laboratory, Washington DC), 
;       Brunella Monsignori-Fossi and Enrico Landi (Arcetri Observatory, 
;       Florence), and Helen Mason and Peter Young (DAMTP, Cambridge Univ.).
;
;
; NAME: CHOOSE_IONEQ
;       
; PURPOSE:
;
;	To load up a set of ionisation balance calculations.
;
; CATEGORY:
;
;       Atomic data analysis
;
; EXPLANATION:
;
;	Due to problems with the existence of the arnaud_raymond.ioneq file, 
;	I've decided to delegate the job of loading in the ion balance data 
;	to this routine. This will also be convenient if new ionisation 
;	balance data becomes available in the future.
;
; INPUTS:
;
;	IZ:	The atomic number of the ion
;	ION:	The spectroscopic number of the ion (e.g., 12 = XII)
;	
; OUTPUTS:
;
;	TEMP_ALL:	Vector containing temperatures
;	IONEQ:		3D Array containing ion fractions
;
; OPTIONAL OUTPUTS:
;
;	FILENAME:	The name of the ion balance file used, e.g.,
;			'arnaud_rothenflug.ioneq'
;
; KEYWORDS:
;
;	USER_SELECT:	Allows the user selection of a .ioneq file via 
;			a widget
;
; CALLS:
;
;	READ_IONEQ
;
; RESTRICTIONS:
;
;	Contains a direct reference to !xuvtop.
;
; HISTORY:
;
;	Ver.1, PRY 5-Sep-98.
;	Ver.2, PRY 19-Feb-99, corrected bug
;	Ver.3, PRY 6-Mar-99, modified to take account of change in the 
;		read_ioneq procedure
;	Ver.4, PRY 13-Oct-99, corrected bug
;       Ver.5, PRY 6-Nov-00, changed default ion balance file to 
;               mazzotta_etal.ioneq
;
; CONTACT:
;
;	Peter Young, CfA, pyoung@cfa.harvard.edu
;-


IF N_PARAMS() LT 4 THEN BEGIN
  PRINT,' Use: IDL> choose_ioneq, iz, ion, temp, ioneq, $'
  PRINT,'                               [ ioneq_name=ioneq_name, $'
  PRINT,'                                 /user_select ]'
  RETURN
ENDIF

IF N_ELEMENTS(ioneq_name) NE 0 THEN BEGIN
  dir=add_subdir(!xuvtop,'ioneq')
  full_name=dir+'/'+ioneq_name
  read_ioneq,full_name,temp_all,ioneq,ref
  return
ENDIF


; The following uses the arnaud_rothenflug.ioneq file in all cases except 
; when count is 2 (implying arnaud_raymond.ioneq exists) and iz is 26 (iron)
;
IF KEYWORD_SET(user_select) THEN BEGIN
  dir=ADD_SUBDIR(!xuvtop,'ioneq')
  dir=EXPAND_PATH(dir)
  default='/arnaud_rothenflug_lmf.ioneq'
  ioneq_name=dialog_pickfile(file=dir+default,path=dir,filter='*.ioneq', $
                           title='Select Ion Balance File')
  IF ioneq_name EQ '' THEN ioneq_name=dir+default
  read_ioneq,ioneq_name,temp_all,ioneq,ref
  ioneq_name=str_sep(ioneq_name,'/')
  nn=N_ELEMENTS(ioneq_name)
  ioneq_name=ioneq_name(nn-1)
ENDIF ELSE BEGIN
  result=findfile(expand_path(!xuvtop)+'/ioneq/*.ioneq',count=count)
  ion_balance='mazzotta_etal.ioneq'
  read_ioneq,!xuvtop+'/ioneq/'+ion_balance,temp_all,ioneq,ref
  ioneq_name=ion_balance
ENDELSE

END
