[GRASS-user] Landsat Processing in PyGrass

Hi,

I am new to pygrass, I got the landsat processing script from osgeo pygrass wiki page. I am stuck here at looping through landsat directories that I have already created through unzipping lansat .gz archives. Kindly assist, code is a shown below, the error being raised is for L7Dir in L7Dirs:
TypeError: ‘NoneType’ object is not iterable

#!/usr/bin/env python

PURPOSE

This script processes LANDSAT 7 ETM+ images

1 - unzip *.gz files

2 - import files in GRASS GIS Location of your choice (r.in.gdal)

3 - DN to Top of Atmosphere reflectance (i.landsat.toar)

4 - TOA reflectance to Surface reflectance (i.atcorr)

5 - NDVI (i.vi), Albedo (i.albedo), Emissivity (i.emissivit

USER HAS TO SET THOSE

QUIET REPORTS

QIET = True

OVERWRITE EXISTING FILES

OVR = False

Define Landsat 7 sensor for i.landsat.toar

LSENSOR = “tm7”

Setup the path to the Landsat 7 Directories

rsdatapath = “~/rawData/L7Dir”

set L7 Metadata wildcards

wldc_mtl = “*_MTL.txt”

Visibility distance [Km]

vis = 18

DEM input to atmospheric correction

inDEM = rsdatapath + “/dem_30m/Kenya_SRTM30meters.tif”

import glob
import os
import subprocess
import sys

path to the GRASS GIS launch script

MS Windows

grass7bin_win = r’C:\OSGeo4W\bin\grass72svn.bat’

uncomment when using standalone WinGRASS installer

grass7bin_win = r’C:\Program Files (x86)\GRASS GIS 7.2.0\grass72.bat’

Linux

grass7bin_lin = ‘grass72’

Mac OS X

grass7bin_mac = ‘/Applications/GRASS/GRASS-7.2.app/’

DATA

define GRASS DATABASE

add your path to grassdata (GRASS GIS database) directory

gisdb = os.path.join(os.path.expanduser(“~”), “rawData”)

the following path is the default path on MS Windows

gisdb = os.path.join(os.path.expanduser(“~”), “Documents/grassdata”)

specify (existing) location and mapset

location = “L7_Kenya”
mapset = “hempire”

SOFTWARE

if sys.platform.startswith(‘linux’):

we assume that the GRASS GIS start script is available and in the PATH

query GRASS 7 itself for its GISBASE

grass7bin = grass7bin_lin
else:
raise OSError(‘Platform not configured.’)

query GRASS 7 itself for its GISBASE

startcmd = [grass7bin, ‘–config’, ‘path’]

p = subprocess.Popen(startcmd, shell=False,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
print >> sys.stderr, “ERROR: Cannot find GRASS GIS 7 start script (%s)” % startcmd
sys.exit(-1)
gisbase = out.strip(‘\n\r’)

Set GISBASE environment variable

os.environ[‘GISBASE’] = gisbase

the following not needed with trunk

os.environ[‘PATH’] += os.pathsep + os.path.join(gisbase, ‘extrabin’)

add path to GRASS addons

home = os.path.expanduser(“~”)
os.environ[‘PATH’] += os.pathsep + os.path.join(home, ‘.grass7’, ‘addons’, ‘scripts’)

define GRASS-Python environment

gpydir = os.path.join(gisbase, “etc”, “python”)
sys.path.append(gpydir)

DATA

Set GISDBASE environment variable

os.environ[‘GISDBASE’] = gisdb

import GRASS Python bindings (see also pygrass)

import grass.script.setup as gsetup

gsetup.init(gisbase, gisdb, location, mapset)
from grass.pygrass.modules.shortcuts import raster as r

Needed for floor()

env = os.environ.copy()

env[‘GRASS_MESSAGE_FORMAT’] = ‘gui’

Function to get a list of L7 Directories in the rsdatapath

def fn(path):
for top, dirs, files in os.walk(path):
return [os.path.join(top, dir) for dir in dirs]

START PROCESS

PART 0: PRE-PROCESSING STUFF

import DEM for atmospheric correction

r.in.gdal(input=inDEM,output=“dem”,overwrite=OVR)

r.mapcalc(expression=“dem=25”,overwrite=OVR)

create a visibility map

r.mapcalc(expression=“vis=18”, overwrite=OVR)

Find the central location of the Landsat file from metadata

metadata =
fileList =
L7Dirs = fn(rsdatapath)
for L7Dir in L7Dirs:

Ungzip all of your Landsat7 images in all your directories

print “Ungzip Landsat files in\t”,L7Dir

p=os.system(“gzip -d -q “+L7Dir+”/*.gz”)

Using pthreads on multi-core machines

p=os.system(“pigz -d “+L7Dir+”/*.gz”)

Wait ten seconds for gzip to create the tif images

time.sleep(10)

print “Import in GRASS GIS”

···

Kind Regards,
Joseph Kariuki

Geospatial Engineer | GIS / Web Developer