pythonhdf5netcdf

Concatenating netcdf and hdf5 files


I have two data files in two formats, one is netcdf, the other is hdf5. I wish to combine them and export the combined data as hdf5. My current solution includes converting each to a python dictionary and them combining the dictionaries and converting the combined dictionary to hdf5. Is there a better solution for this?


Solution

  • # 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()