I have been using gdal_calc.py like this:

os.system("""gdal_calc.py -a DSM.tif --a_band=1 -b DTM.tif --b_band=1 --calc="a-b" --outfile=CHM.tif""")

But now it tells me that I am missing required arguments, which I have in my code.

gdal_calc.py: error: the following arguments are required: --calc, --outfile

Unsure how to fix an error, I don't actually have.

1 Answer 1

  1. In my gdal_calc.py version I have to use uppercase letters (..help gdal_calc.py ..-A filename input gdal raster file, you can use any letter (A-Z)).

  2. A call direct of the command line from bash works without the quotes:

    $ gdal_calc.py -A DSM.tif --A_band=1 -B DTM.tif --B_band=1 --calc=A-B --outfile=CHM.tif
    0 .. 10 .. 20 .. 30 .. 40 .. 50 .. 60 .. 70 .. 80 .. 90 .. 100 - Done
  1. The same way with ipython but with correct CLI parameter.
    $ ipython
    python 2.7.17 (default, Feb 27 2021, 15:10:58) 
    Type "copyright", "credits" or "license" for more information.
    IPython 5.5.0 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.
    In [1]: import os
    In [2]: os.system("""gdal_calc.py -A DSM.tif --A_band=1 -B DTM.tif --B_band=1 --calc="A-B" --outfile=CHM.tif""")
    0 .. 10 .. 20 .. 30 .. 40 .. 50 .. 60 .. 70 .. 80 .. 90 .. 100 - Done
    Out[2]: 0

Help gdal_cal.py

    $ gdal_calc.py --help
    Usage: gdal_calc.py --calc=expression --outfile=out_filename [-A filename]
                        [--A_band=n] [-B...-Z filename] [other_options]
      -h, --help            show this help message and exit
      --calc=expression     calculation in gdalnumeric syntax using +-/* or any
                            numpy array functions (i.e. log10())
      -A filename           input gdal raster file, you can use any letter (A-Z)
      --A_band=n            number of raster band for file A (default 1)
      --outfile=filename    output file to generate or fill
      --NoDataValue=value   output nodata value (default datatype specific value)
      --type=datatype       output datatype, must be one of ['Int32', 'Int16',
                            'Float64', 'UInt16', 'Byte', 'UInt32', 'Float32']
      --format=gdal_format  GDAL format for output file (default 'GTiff')
      --creation-option=option, --co=option
                            Passes a creation option to the output format driver.
                            Multiple options may be listed. See format specific
                            documentation for legal creation options for each
      --allBands=[A-Z]      process all bands of given raster (A-Z)
      --overwrite           overwrite output file if it already exists
      --debug               print debugging information
      --quiet               suppress progress messages
  • Thanks, yes I did try to use capital letters as well and directly within the command line without quotes. and it still gives me the same error. saying I am missing arguments
    – B-C B.
    Dec 10, 2021 at 18:07
  • Which gdal version do you use?
    – huckfinn
    Dec 10, 2021 at 18:20
  • Strange when I search for parts of your error message grep 'are required' /usr/bin/gdal_calc.py, I get no hit.
    – huckfinn
    Dec 10, 2021 at 18:26
  • yes, I even tried using the GUI in QGIS and get similar, yet different error: 'gdal_calc' is not recognized as an internal or external command, operable program or batch file.. I am using the latest version of gdal 3.4.0. But I also tried with older version 3.3.3
    – B-C B.
    Dec 10, 2021 at 18:32
  • Can you try from bash grep 'No ' $(which gdal_calc.py) and present the output?
    – huckfinn
    Dec 10, 2021 at 18:45

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.