0

I have two hdf files, hdf4 and hdf5. In both cases, I am able to load the files in python using respective libraries (pyhdf and h5py). But inside the files, I can only read the data fields' name, size, and other parameters. The datasets are coming in tuples. I am unable to extract the actual data. Below is the code I am using. I was previously using MATLAB where both worked fine. Any help will be appreciated.

import os
import h5py as hp
import numpy as np
from pyhdf.SD import SD, SDC  

path=r'C:\Users\Sky\Desktop'
os.chdir(path)

f1=hp.File('TPR7_uw1_00539.19980101.011127_SAS.hdf')
ds1=f1.keys()

f2=SD('era5_2020_4_17_18.00_23.88_91.24.HDF')
ds2=f2.datasets()
T=ds2["T"] # Temperature

dataset output: dataset output

M Z
  • 4,571
  • 2
  • 13
  • 27
Sky
  • 9
  • 3
  • 1
    What is the output of your code? You haven't showed us how it fails. – Tim Roberts Apr 29 '23 at 04:45
  • Check the attached image. It shows the field names but not data – Sky Apr 29 '23 at 04:47
  • In matlab i can view the data inside the file but here only the field names not the actual data – Sky Apr 29 '23 at 04:49
  • You still have not show how it fails. Where do you get a "none " or "KeyError" or something that you do not expect ? – Itération 122442 Apr 29 '23 at 05:04
  • The image seems to show both keys and values. What is it SUPPOSED to look like? – Tim Roberts Apr 29 '23 at 06:50
  • There is the issue. Only the key names are showing not the values associated with it. Each parameter is an array of length 28. The length of the array is showing not the elements of the array. Am I able to make problem clear?? – Sky Apr 29 '23 at 12:19
  • Not sure about HDF4 without looking it up but h5py datasets act like proxies for numpy arrays. If you want to read the data, use the pattern `file["dataset"][:]` or whatever slice you want, e.g. `file["dataset"][start:end]` – Homer512 Apr 29 '23 at 14:56
  • Thank you @Homer512. Now I can read the content inside the file. – Sky Apr 30 '23 at 18:18

0 Answers0