How does gdal_merge.py want input bands of a multiband tiff pathed via the --optfile option?

I am merging a large list of .tifs via a subprocess.call in python:

gdal_merge.py -o "target" --optfile "fileIn"

where "fileIn" is a list of input files. But playing around I can't seem to find how to reference example.tif Band_1, for example. So even more simply, how do you call a specific band of a multiband geotiff in gdal?

  • Your question is a bit unclear. Would you like to extract n-th band or specify the order of the input files? – matej Sep 28 '16 at 22:22
  • thanks. i'm trying to merge many bands of the same type into one mosaic, and all the files have the same structure. so file1.tif has 1000 bands, and i have 100 .tifs. So i want to merge file1.tif/band_1 with all other band_1s from the other 100 files, and then do that for every band. but at its simplest, i can't figure out how, in gdal_merge, to path file1.tf/band_1! – Jason Sep 28 '16 at 22:31
  • Just a comment, because I'd have to check, but it seems that you will have to break the 100 tifs into separate bands and then merge them back. – matej Sep 28 '16 at 22:51

You can't tell gdal_merge.py to process a specific band from multiband rasters as it doesn't support a -b band argument or path/to/file.tif/band_num syntax.

I would use gdalbuildvrt then gdal_translate:

gdalbuildvrt -b 1 -input_file_list "file_list.txt" "target.vrt"
gdal_translate "target.vrt" "target.tif"

Or without an intermediate VRT file:

gdalbuildvrt -b 1 -input_file_list "file_list.txt" /vsistdout/ | gdal_translate /vsistdin/ "target.tif"
  • thanks very much for the explanation of gdal_merge.py and the alternative solution. I didn't know the gdalbuildvrt would mosaic files spatially. Awesome. – Jason Sep 29 '16 at 14:04
  • ok, sort of awesome. seems to be taking much longer than my previous routine that used gdal_merge.py. Any idea if the gdal_merge.py routine would be able to take a NetCDF file with a similar Band_n format? – Jason Sep 30 '16 at 19:47
  • Weird, I've always found gdalbuildvrt + gdal_translate to be much faster than gdal_merge.py. Did you try the 1st method without the intermediate vrt? Don't know, depends on whether the netcdf file uses subdatasets gdal.org/frmt_netcdf.html – user2856 Sep 30 '16 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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