79134112

Date: 2024-10-28 15:46:56
Score: 0.5
Natty:
Report link
# open the HDF5 file
with h5py.File(hdf5_path, 'r+') as hdf:  # 'r+' mode allows overwrite existing file
    # open the NetCDF file
    ncdf_file = Dataset(self.CDF_path, 'r')
    # create a new group for NetCDF data within HDF5 file
    ncdf_group = hdf.create_group(f'Advanced_ASTRA/Astra_output')

    # loop over each variable in the NetCDF file
    for var in ncdf_file.variables:
        # create a new group for each variable
        var_group = ncdf_group.create_group(var)
        # save data, units, and long_name as datasets within this group
        var_group.create_dataset('data', data=ncdf_file.variables[var][:])
        var_group.create_dataset('units', data=str(ncdf_file.variables[var].units))
        var_group.create_dataset('long_name', data=str(ncdf_file.variables[var].long_name))

    ncdf_file.close()
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Niandra Lades