; docformat = 'rst'
;+
; Write a variable to a file or group.
;
; :Examples:
; For example, the following creates an HDF5 file::
;
; IDL> filename = 'test.h5'
; IDL> fid = h5f_create(filename)
; IDL> mg_h5_writevariable, fid, 1.0, 'scalar'
; IDL> mg_h5_writevariable, fid, findgen(10), 'array'
; IDL> group = h5g_create(fid, 'group')
; IDL> mg_h5_writevariable, group, 1.0, 'another_scalar'
; IDL> mg_h5_writevariable, group, findgen(10), 'another_array'
; IDL> h5g_close, group
; IDL> h5f_close, fid
;
; To browse the results::
;
; IDL> ok = h5_browser(filename)
;
; :Params:
; id : in, required, type=long
; HDF5 identifier of file or group to write variable into
; data : in, required, type=any
; IDL variable to write
; name : in, required, type=string
; name of variable in HDF5 file
;-
pro mg_h5_writevariable, id, data, name
compile_opt strictarr
; get the HDF5 type from the IDL variable
datatype_id = h5t_idl_create(data)
; scalars and arrays are created differently
if (size(data, /n_dimensions) eq 0L) then begin
dataspace_id = h5s_create_scalar()
endif else begin
dataspace_id = h5s_create_simple(size(data, /dimensions))
endelse
dataset_id = h5d_create(id, name, datatype_id, dataspace_id)
h5d_write, dataset_id, data
h5d_close, dataset_id
h5s_close, dataspace_id
h5t_close, datatype_id
end