;+
; Project     :	BepiColombo
;
; Name        :	LOAD_SUNSPICE_BC_MPO
;
; Purpose     :	Load the BepiColombo SPICE ephemerides
;
; Category    :	BepiColombo, SUNSPICE, Orbit
;
; Explanation : Loads the BepiColombo ephemeris files in SPICE format.  Also
;               calls LOAD_SUNSPICE_GEN to load the generic kernels.
;
; Syntax      :	LOAD_SUNSPICE_BC_MPO
;
; Inputs      :	None.
;
; Opt. Inputs :	None.
;
; Outputs     :	None.
;
; Opt. Outputs:	None.
;
; Keywords    : RELOAD = If set, then unload the current ephemeris files, and
;                        redetermine which kernels to load.  The default is to
;                        not reload already loaded kernels.
;
;               VERBOSE= If set, then print a message for each file loaded.
;
;               ERRMSG = If defined and passed, then any error messages will be
;                        returned to the user in this parameter rather than
;                        depending on the MESSAGE routine in IDL.  If no errors
;                        are encountered, then a null string is returned.  In
;                        order to use this feature, ERRMSG must be defined
;                        first, e.g.
;
;                               ERRMSG = ''
;                               LOAD_SUNSPICE_BC_MPO, ERRMSG=ERRMSG
;                               IF ERRMSG NE '' THEN ...
;
; Calls       :	TEST_SUNSPICE_DLM, LOAD_SUNSPICE, LIST_SUNSPICE_KERNELS, MATCH,
;               CSPICE_FURNSH
;
; Common      :	BC_MPO_SUNSPICE contains the names of the loaded files.
;
; Env. Vars.  : None.
;
; Restrictions:	This procedure works in conjunction with the Icy/CSPICE
;               package, which is implemented as an IDL Dynamically Loadable
;               Module (DLM).  The Icy source code can be downloaded from
;
;                       ftp://naif.jpl.nasa.gov/pub/naif/toolkit/IDL
;
;               Because this uses dynamic frames, it requires Icy/CSPICE
;               version N0058 or higher.
;
; Side effects:	None.
;
; Prev. Hist. :	Based on LOAD_SUNSPICE_SDO
;
; History     :	Version 1, 01-Sep-2021, William Thompson, GSFC
;
; Contact     :	WTHOMPSON
;-
;
pro load_sunspice_bc_mpo, reload=reload, verbose=verbose, errmsg=errmsg
common bc_mpo_sunspice, rtnframe, ephem
on_error, 2
;
;  Make sure that the SPICE/Icy DLM is available.
;
if not test_sunspice_dlm() then begin
    message = 'SPICE/Icy DLM not available'
    goto, handle_error
endif
;
;  If the /RELOAD keyword wasn't passed, then check to see if the kernels have
;  already been loaded.
;
n_kernels = n_elements(ephem)
if (not keyword_set(reload)) and (n_kernels gt 0) then return
;
;  Start by unloading any ephemerides which were previously loaded, and then
;  loading the generic kernels.
;
unload_sunspice_bc_mpo, verbose=verbose
message = ''
load_sunspice_gen, verbose=verbose, errmsg=message
if message ne '' then goto, handle_error
;
;  Load the RTN frame.
;
bc_mpo_spice = concat_dir(getenv('SSW'), 'packages/sunspice/bc_mpo/data')
bc_mpo_spice_text = bc_mpo_spice
if !version.os_family eq 'Windows' then $
  bc_mpo_spice_text = concat_dir(bc_mpo_spice_text, 'dos')
;
rtnframe = concat_dir(bc_mpo_spice_text, 'bc_mpo_rtn.tf')
if not file_exist(rtnframe) then begin
    message = 'Unable to find heliospheric frame file'
    goto, handle_error
endif
if keyword_set(verbose) then print, 'Loaded ' + rtnframe
cspice_furnsh, rtnframe
;
;  Load the ephemeris file.
;
ephem = concat_dir(bc_mpo_spice, 'bc_mpo_fcp_latest.bsp')
if not file_exist(ephem) then begin
    message = 'No BC_MPO ephemeris files found'
    goto, handle_error
endif
if keyword_set(verbose) then print, 'Loaded ' + ephem
cspice_furnsh, ephem
;
return
;
;  Error handling point.
;
handle_error:
if n_elements(errmsg) eq 0 then message, message else $
  errmsg = 'LOAD_SUNSPICE_BC_MPO: ' + message
;
end
