; docformat = 'rst'

;+
; $Id: nrl_restgen.pro 3888 2014-06-17 14:09:04Z mariska $
;-

;+
; Restore an arbitrary number of IDL variables from an HDF 5 file
; written using using nrl_savegen.pro. Warning, variable length
; strings are not saved and restored correctly.
;
; :Parmmeters:
;
; :Keywords:
;   file:
;     Input file name (defaults to save.h5).
;   inquire:
;      If set, display summary of file and return.
;   text:
;      Retrieve text section from file.
;
; :Author:
;   John Mariska, GMU, jmariska@gmu.edu, jtmariska@gmail.com
;
; :History:
;   (2014-Jun-13) JTM: first working test version.
;-
pro nrl_restgen, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, $
                 p12, p13, p14, p15, $
                 file=file, text=text, inquire=inquire

    compile_opt strictarr

    if n_elements(file) eq 0 then file = 'save.h5'
    if ~file_test(file) then begin
        message, /info, 'File ' + file + ' not found.'
        return
    endif

    ; Retrieve data
    struct = h5_parse(file, /read_data)
    names = tag_names(struct.nrl_savegen._data)

    ; There will always be text but only pull it if user asks.
    intext = struct.nrl_text._data
    intext = strsplit(intext, 10b, /extract)
    if n_elements(text) ne 0 or arg_present(text) then begin
        text = intext
    endif

    ; Info on contents
    if n_elements(inquire) ne 0 then begin
        print
        print, '--- Contents summary of NRL generic file: ' $
               + file + ' ---'
        print
        print, '... Text Section ...'
        print, intext
        print
        print, '... Data Section ...'
        foreach name, names do print, name
        return
    endif
        
    ntags = n_tags(struct.nrl_savegen._data)
    nargs = n_params()
    np = max(ntags, nargs)
    params = 'p' + strtrim(sindgen(np), 2)
    for i=0, n_tags(struct.nrl_savegen._data)-1 do begin
        execstr = params[i] + ' = struct.nrl_savegen._data.(' + $
                  strtrim(i, 2) + ')'
        status = execute(execstr)
    endfor

end
