1

I am trying to make a Landsat geoprocessing script for calculating vegetation indices using Grass modules and Python. I am fairly new at using Python for Grass and I have managed to call and create Grass sessions (outside Grass), importing Landsat rasters into Grass and run basic modules like querying resters, set region and read raster statistics etc.

But whenever I am trying to run some complex modules like i.landsat.toar, mapcalc and r.reclass, I am getting the following errors.

My code looks like this, the first part works fine:

import os
import sys
import subprocess
# This part works fine
grass7bin = r'C:\GIS\GRASS GIS 7.4.0\grass74.bat'
startcmd = [grass7bin, '--config', 'path']
try:
    p = subprocess.Popen(startcmd, shell=False,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = p.communicate()
except OSError as error:
    sys.exit("ERROR: Cannot find GRASS GIS start script"
             " {cmd}: {error}".format(cmd=startcmd[0], error=error))
if p.returncode != 0:
    sys.exit("ERROR: Issues running GRASS GIS start script"
             " {cmd}: {error}"
             .format(cmd=' '.join(startcmd), error=err))
gisbase = out.strip(os.linesep)

os.environ['GISBASE'] = gisbase
grass_pydir = os.path.join(gisbase, "etc", "python")
sys.path.append(grass_pydir)

import grass.script as gscript
import grass.script.setup as gsetup
import grass.script.core as gcore    

gcore.create_location(r'D:\MyLocation\subfolder\subfolder', 'MC_LS_Proc', epsg=32650, datum='wgs84')
    # Getting the environmental variables
    gisdb = os.path.join(r"D:\MyLocation\subfolder\subfolder")
    location = "MC_LS_Proc"
    mapset = "PERMANENT"
    rcfile = gsetup.init(gisbase, gisdb, location, mapset)

The routine checks runs fine:

# Routine Checks
print gscript.read_command('g.gisenv')
print gscript.read_command('g.proj', flags='p')
print gscript.read_command('g.region', flags='p')
# Changing mapset
gscript.run_command('g.mapset', mapset='Gr_LC08_L1TP_112076_20170526_20170615_01_T1', location='MC_LS_Proc')
gscript.read_command('g.mapset', flags='p')

# Set computational region
gscript.raster_info('B1')
gscript.run_command('g.region', raster='B1')
print gscript.read_command('g.region', flags='p')

The problems start when I try running these commands: Please note that these commands run all good when I do it in the GUI in GRASS. I have many scenes to process, so am very keen to automate it in Python.

gscript.run_command('i.landsat.toar', input='B', output='R.', metfile='D:\26_18_BHP_RVMP\02_subfolder\01_subfolder\03_sub\location\RAW\LC08_L1TP_112076_20170526_20170615_01_T1_MTL.txt', sensor='oli8', method='dos4')
 # And
 gscript.run_command('r.reclass', overwrite=True, input='MC_dmsavi_1718_norm@Gr_LC08_L1TP_112076_20170526_20170615_01_T1', output='MC_dmsavi_May1718_recl', rules='D:\2456_18_BHP_RVMP\07_Code_Backup\Class_Rule_Dmsavi_MC_May1718_7class.txt')

Everytimne similiar error I am receiving is like this:

    gscript.run_command('r.reclass', overwrite=True, input='MC_dmsavi_1718_norm@Gr_LC08_L1TP_112076_20170526_20170615_01_T1', output='MC_dmsavi_May1718_recl', rules='D:\2456_18_BHP_RVMP\07_Code_Backup\Class_Rule_Dmsavi_MC_May1718_7class.txt')
    Traceback (most recent call last):

      File "<ipython-input-145-16743a2687da>", line 1, in <module>
    gscript.run_command('r.reclass', overwrite=True, input='MC_dmsavi_1718_norm@Gr_LC08_L1TP_112076_20170526_20170615_01_T1', output='MC_dmsavi_May1718_recl', rules='D:\2456_18_BHP_RVMP\07_Code_Backup\Class_Rule_Dmsavi_MC_May1718_7class.txt')

     File "C:\GIS\GRASS GIS 7.4.0\etc\python\grass\script\core.py", line 421, in run_command
    return handle_errors(returncode, returncode, args, kwargs)

  File "C:\GIS\GRASS GIS 7.4.0\etc\python\grass\script\core.py", line 332, in handle_errors
    returncode=returncode)

    CalledModuleError: Module run None ['r.reclass', '--o', 

 'rules=D:\xa56_18_BHP_RVMP\x07_Code_Backup\\Class_Rule_Dmsavi_MC_May1718_7class .txt', 'input=MC_dmsavi_1718_norm@Gr_LC08_L1TP_112076_20170526_20170615_01_T1', 'output=MC_dmsavi_May1718_recl'] ended with error
    process ended with non-zero return code 1. See errors in the (error) output.

    Traceback (most recent call last):

     File "<ipython-input-145-16743a2687da>", line 1, in <module>
        gscript.run_command('r.reclass', overwrite=True, input='MC_dmsavi_1718_norm@Gr_LC08_L1TP_112076_20170526_20170615_01_T1', output='MC_dmsavi_May1718_recl', rules='D:\2456_18_BHP_RVMP\07_Code_Backup\Class_Rule_Dmsavi_MC_May1718_7class.txt')

      File "C:\GIS\GRASS GIS 7.4.0\etc\python\grass\script\core.py", line 421, in run_command
        return handle_errors(returncode, returncode, args, kwargs)

      File "C:\GIS\GRASS GIS 7.4.0\etc\python\grass\script\core.py", line 332, in handle_errors
        returncode=returncode)

    CalledModuleError: Module run None ['r.reclass', '--o', 'rules=D:\xa56_18_BHP_RVMP\x07_Code_Backup\\Class_Rule_Dmsavi_MC_May1718_7class.txt', 'input=MC_dmsavi_1718_norm@Gr_LC08_L1TP_112076_20170526_20170615_01_T1', 'output=MC_dmsavi_May1718_recl'] ended with error
    Process ended with non-zero return code 1. See errors in the (error) output.

1 Answer 1

1

It looks like the path and file name for your rules file is getting mangled.

Here's what you put into the command:

rules='D:\2456_18_BHP_RVMP\07_Code_BackupClass_Rule_Dmsavi_MC_May1718_7class.txt'

Whereas this is what python sees:

rules=D:\xa56_18_BHP_RVMP\x07_Code_Backup\\Class_Rule_Dmsavi_MC_May1718_7class

Two suggestions: Can you change the paths and filenames so that they always begin with a letter, not a number? And more importantly, I would build the whole rules parameter properly in python using os.path.join(). Something like:

rules_file = "Class_Rule_Dmsavi_MC_May1718_7class.txt" rules_path = os.path.join("D:/", "2456_18_BHP_RVMP", "07_Code_Backup", rules_file) gscript.run_command('r.reclass', overwrite=True, input='MC_dmsavi_1718_norm@Gr_LC08_L1TP_112076_20170526_20170615_01_T1', output='MC_dmsavi_May1718_recl', rules=rules_path)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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