Search code examples
pythonformulaoperand

Unsupported operand type(s) in formula


I m having problems getting python 2.7 to do this formula. Its purpose is to convert Landsat 7 DN values to reflectance values. b1 references a band1 TIFF image.

L_B1 = float(((LMax_B1 - LMin_B1) / (QCALMax_B1 - QCALMin_B1)) * (b1 - QCALMin_B1) + LMin_B1)

The error message I m getting is:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode'

It appears to not like - / * . Ive imported math.

Any help would be appreciated.


Solution

  • To me it looks like you are operating on strings, that is either one of your variables like LMax_B1 etc. is a unicode string, or probably all of them are. You have to convert them to floating point numbers (or whatever they are) first, i.e. change:

    LMax_B1 - LMin_B1
    

    to

    float(LMax_B1) - float(LMin_B1)
    

    Do so likewise for all other variables. By the way, you don't need to import math for the operators + - * /. They are always available and work as expected if you operate on numbers. Math is required for more advanced functions like sin etc.