;+
; PROJECT:
;   XRAY
; NAME:
;   XR_RD_ABUNDANCE
;
; PURPOSE:
;   This returns the abundances written in the xray_abun_file.genx
;   The abundances are taken from CHIANTI and MEWE.  The source filenames are:
;   cosmic sun_coronal sun_coronal_ext sun_hybrid sun_hybrid_ext sun_photospheric mewe_cosmic mewe_solar
;   The first six come fron Chianti, the last two from Mewe.  They are:
;   cosmic sun_coronal sun_coronal_ext sun_hybrid sun_hybrid_ext sun_photospheric mewe_cosmic mewe_solar
;	These abundances are used with CHIANTI_KEV.  MEWE_KEV can only use the two mewe sourced
;	abundance distributions unless using a heavily modified rel_abun structure for all of the elements.
;
; CATEGORY:
;
;
; CALLING SEQUENCE:
;   abundance = xr_rd_abundance( [AB_TYPE,] [/coronal,] [/photospheric,] [/mewe,][/cosmic,]
;     [/hybrid,][/ext])
;
; CALLS:
;   none
;
; INPUTS:
;      AB_TYPE - optional, give it one of the eight filenames (string)
;      From Chianti
;      1. cosmic
;      2. sun_coronal - default abundance
;      3. sun_coronal_ext
;      4. sun_hybrid
;      5. sun_hybrid_ext
;      6. sun_photospheric
;      7. mewe_cosmic
;      8. mewe_solar - default for mewe_kev
;
; OPTIONAL INPUTS:
;   none
;
; OUTPUTS:
;       Returns array of 50 abundance levels for first 50 elements.  Also sets values
;       in common block chianti_kev_abundance.
;
; OPTIONAL OUTPUTS:
;   none
;
; KEYWORDS:
;     VERBOSE - If set, display the XR_AB_TYPE used.
;     CORONAL - USE ONE OF CHIANTI CORONAL ABUNDANCES
;     HYBRID - SELECT ONE OF TWO HYBRID CHIANTI ABUNDANCES
;     EXT - SELECT ONE OF THE TWO EXTENDED CHIANTI ABUNDANCES
;     PHOTOSPHERIC - SELECT THE CHIANTI PHOTOSPHERIC ABUNDANCE
;     COSMIC - SELECT ONE OF THE TWO COSMIC ABUNDANCES
;     MEWE - SELECT ONE OF THE TWO MEWE ABUNDANCES
;     MEWE HAS PRECEDENCE, THEN COSMIC, THEN CORONAL, THEN HYBRID
;      EXT MUST BE USED IN COMBINATION WITH CORONAL OR HYBRID
;     NOSAVE - If set, don't set values in common block
; COMMON BLOCKS:
;   none
;
; SIDE EFFECTS:
;   none
;
; RESTRICTIONS:
;   none
;
; PROCEDURE:
;   none
;
; MODIFICATION HISTORY:
;   Version 1, richard.schwartz@gsfc.nasa.gov, 8-FEB-2005
;	16-feb-2006, richard.schwartz@gsfc.nasa.gov, added sun_photospheric to case statement
;	24-apr-2006, richard.schwartz@gsfc.nasa.gov,
;		Changed CHIANTI_AB_FILE to XR_AB_FILE - this is the file containing the various types
;		of abundance distributions, some coming from MEWE_SPEC(KEV), and others coming
;		from the CHIANTI package. The default abundance distribution is sun_coronal from CHIANTI
;   5-May-2006, kim.tolbert@gsfc.nasa.gov.  Added nosave keyword.
;
;
;-
function xr_rd_abundance, ab_type,  $
    coronal=coronal, $
    photospheric=photospheric, $
    hybrid=hybrid,$
    ext=ext, $
    mewe=mewe, $
    cosmic=cosmic,$
    reload=reload, $
    verbose=verbose, $
    nosave=nosave
common chianti_kev_abundance, abundance, ab_file_sav, rel_abun_com, ab_type_sav, ab_sav
;Do we need to read the XR_AB_FILE
XR_AB_FILE = getenv('XR_AB_FILE')
test = exist( ab_file_sav) ? ab_file_sav eq xr_ab_file : 0
if test eq 0 or keyword_set(RELOAD) then begin
	xray_chianti = concat_dir('SSWDB_XRAY','chianti')
	ab_file = (loc_file(path=xray_chianti, XR_AB_FILE ,count=nabfile))[0]
	if nabfile ge 1 then file = AB_FILE else begin
	    xray_chianti = concat_dir('SSWDB_XRAY','chianti')
	    xr_ab_file   = 'xray_abun_file.genx'
	    default, ab_file_sav, ''
	    test = ab_file_sav eq xr_ab_file
	    if test eq 0 or keyword_set(RELOAD) then $
	    	file = loc_file( path=[curdir(),xray_chianti], xr_ab_file, count=nabfile)
	 endelse
	if nabfile ge 1 then begin
		restgen, ab, file=file[0]
		ab_sav = ab
		ab_file_sav = xr_ab_file
    endif else if test eq 0 then message,/continue, 'Invalid XRAY abundance filename '+ xr_ab_file
endif
def_ab_type = getenv('XR_AB_TYPE')
default, ab_type, def_ab_type
select = where( strlowcase(ab_type) eq ab_sav.filnam, count)
if count eq 0 then begin
	if ab_type ne '' then message,/continue,'Invalid abundance distribution type'
	;No selection yet
	photospheric = keyword_set( photospheric )
	ext = keyword_set( ext )
	hybrid = keyword_set( hybrid )
	coronal = keyword_set( coronal )
	mewe = keyword_set( mewe)
	cosmic = keyword_set( cosmic )
	default, ext, 0
	default, hybrid, 0
	default, photospheric, 0
	default, mewe, 0
	default, cosmic, 0
	coronal = 1 - photospheric
	case 1 of
	    mewe: ab_type = cosmic ?'mewe_cosmic' : 'mewe_solar'
	    cosmic: ab_type = 'cosmic'
	    coronal: ab_type = ext ? 'sun_coronal_ext' : 'sun_coronal'
	    hybrid: ab_type = ext ? 'sun_hybrid_ext'  : 'sun_hybrid'
	    photospheric: ab_type = 'sun_photospheric'
	    else: ab_type = 'sun_coronal
	    endcase
	select = where( ab_type eq ab_sav.filnam, count)
endif
if count then begin
    if keyword_set(verbose) then print, ab_type
    abund= ab_sav[select[0]].abund
	; replace values in common block, unless nosave is set
    if not keyword_set(nosave) then begin
       ab_type_sav = ab_type
       abundance = abund
    endif
endif
out = fcheck(abund,0)
return, out
end