Extracting bands from hdf4_eos to tif

Here is a quick and dirty bash script I wrote to extract all bands from a modis image and save each to a tiff file. The saved bands are named after the sub layer in the original hdf4 file. The script actually generates a second script called batch.sh so you can peek in there to see the undelying gdal_translate commands that are generated.

One day I need to get around to letting QGIS natively support HDF – maybe a good topic for those attending the OSGEO hackfest in NY?


#!/bin/bash
#Set field separator to linefeeds rather than spaces
echo "#!/bin/bash" > batch.sh
chmod +x batch.sh
IFS=$'\n'
for FILE in *.hdf
do
  echo $FILE
  LAYERS=$(gdalinfo $FILE | grep SUBDATASET | grep NAME  | sed 's/[A-Z0-9\_]*=//g')
  for LAYER in $LAYERS
  do
    LAYER=$(echo $LAYER | sed 's/  //g')
    NEWFILE=$(echo $LAYER | \
    sed 's/  //g' | sed 's/"//g' | sed 's/ //g'| sed 's/:/./g' ).tif
    CMD="gdal_translate -of GTiff '${LAYER}' $NEWFILE"
    echo $CMD >> batch.sh
  done
done

exec ./batch.sh

5 Comments

[...] from: Linfiniti Geo Blog » Blog Archive » Extracting bands from hdf4_eos … Share and [...]

Barry RowlingsonNovember 26th, 2009 at 7:14 pm

This is about the state my bash scripts get to when I decide to rewrite them in python!

I should probably point out that this script itself shouldn’t be called batch.sh…

adminNovember 27th, 2009 at 12:46 am

For quick & dirty stuff I still find bash quicker & dirtier than my noobie python skills permit….and yeah don’t call the above script ‘batch.sh’ otherwise you will have a very odd day :-)

Frank WarmerdamNovember 27th, 2009 at 6:12 am

Tim,

Nice job, but the -sds switch to gdal_translate will create an output file for each subdataset in the input file and is likely an easier solution depending on how you want to name the output files.

Best regards,

[...] Извлечение каналов из hdf4_eos в tif. [...]

Leave a comment

You must be logged in to post a comment.