6

I have a multi-band HDF file that I want to convert to a single GeoTiff file, using free tools, preferably gdal. I'm trying to use gdal_translate, but I either get back multiple output .tif files, or I get "subdatasets" errors.

When I use the -b flag to specify bands:

gdal_translate -b 1 -b 2 -of GTiff input_image.hdf output_image.tif

I get "subdatasets" errors: "Input file contains subdatasets. Please, select one of them for reading."

And when I use the sds flag:

gdal_translate -sds -of GTiff input_file.hdf output_file.tif

I get back separate .tif files, one for each band in the input file.

How can I convert an HDF file into a single GeoTiff file using gdal?

2 Answers 2

2

I also couldn't find a way to do it in one step... but using -sds like you did, followed by a gdal_merge.py -separate should work https://gdal.org/programs/gdal_merge.html#cmdoption-gdaladdo-separate

https://gdal.org/programs/gdal_translate.html#cmdoption-gdal-translate-sds

So this should work for you:

gdal_translate -sds input_file.hdf tmp_outs.tif

gdal_merge.py -separate -o final_output.tif tmp_outs*tif

rm tmp_outs*tif

if your HDF5 subdatasets are different datatypes, specify which output type you want with gdal_merge.py -ot Float32 ...

Just to reference here what those options mean:

-sds

Copy all subdatasets of this file to individual output files. Use with formats like HDF that have subdatasets.



-separate

Place each input file into a separate band.
1
  • 1
    This worked for me!
    – Dieblitzen
    Commented Mar 12, 2021 at 12:20
1

If you want to extract one band (subdataset) from your HDF file, than you need to specify the SUBDATASET name as part of the gdal_translate command. For instance, in the example described here, it is suggested to do the following:

gdal_translate HDF4_SDS:UNKNOWN:"AMOD021KM20061229043102.hdf":0 test_b0.tif

gdal_translate HDF4_SDS:UNKNOWN:"AMOD021KM20061229043102.hdf":1 test_b1.tif

You will need to update HDF4_SDS:UNKNOWN:"AMOD021KM20061229043102.hdf to reflect your own dataset, and you can retrieve this information using gdalinfo.

Also, the documentation on the GDAL website relating to the HDF format provides a lot of useful information and examples that should further assist you.

1
  • 2
    Thanks for responding. I am trying to convert all of the bands/subdatasets from HDF format to GeoTiff. Commented Dec 6, 2016 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.