Kartoza - Mass reprojection of h5 Files using Gdal and Bash Tricks
I recently got hold of h5 files (Hierarchical Data Format (HDF)) and I tried to load them into QGIS and they were drawing in the wrong places.This was because they were not georeferenced.
I recently got hold of h5 files (Hierarchical Data Format (HDF)) and I tried to load them into QGIS and they were drawing in the wrong places.This was because they were not georeferenced. I set out to georeference them using GDAL. Since I was dealing with many h5 files I searched for an automated way to georeference them and could not find one complete solution hence I decided to do it myself with the help of Tim Sutton.
The following script is the one I used to automate georeferencing h5 files in Ubuntu 16.04.
#!/usr/bin/env bashFILENAME=$1if [ -n "$1" ]thenFILENAME=$1fiif [[ "$FILENAME" == *h5 ]];thenLIST="chla cyanobacteria cyanobacteria_high_risk flag_cloud vegetation"for args in $LIST; dogdal_translate -a_srs "EPSG:4326" -mask 1 -of VRT HDF5:"${FILENAME}"://bands/${args} ${args}.vrtgdal_translate -a_srs "EPSG:4326" -mask 1 -of VRT HDF5:"${FILENAME}"://bands/longitude lon.vrtgdal_translate -a_srs "EPSG:4326" -mask 1 -of VRT HDF5:"${FILENAME}"://bands/latitude lat.vrtLINES=`cat ${args}.vrt | wc -l`BOTTOMLINES=`echo "$LINES-2" | bc`head -2 ${args}.vrt > ${args}_referenced.vrtecho "<metadata domain="GEOLOCATION"><mdi key="X_DATASET">lon.vrt</mdi><mdi key="X_BAND">1</mdi><mdi key="Y_DATASET">lat.vrt</mdi><mdi key="Y_BAND">1</mdi><mdi key="PIXEL_OFFSET">0</mdi><mdi key="LINE_OFFSET">0</mdi><mdi key="PIXEL_STEP">1</mdi><mdi key="LINE_STEP">1</mdi></metadata>">> ${args}_referenced.vrttail -${BOTTOMLINES} ${args}.vrt >> ${args}_referenced.vrtgdalwarp -dstalpha -geoloc -t_srs EPSG:4326 ${args}_referenced.vrt ${FILENAME%.*}_${args}.tifdoneelse:fifind . -name "*.vrt" -type f -delete
I copied the script and saved it as reproject_h5.sh and made it executable in bash. I then ran the script as../reproject_h5.sh MER_FSG.h5
You can also run the script by looping through a text file that contains filenames for the h5 rasters.
No comments yet. Login to start a new discussion Start a new discussion