<div dir="ltr"><div>Hi,<br><br></div>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 <i><b><span style="color:rgb(255,0,0)"> for L7Dir in L7Dirs:<br>TypeError: 'NoneType' object is not iterable</span></b></i><br><br>#!/usr/bin/env python<br><br># PURPOSE<br># This script processes LANDSAT 7 ETM+ images<br># 1 - unzip *.gz files<br># 2 - import files in GRASS GIS Location of your choice (r.in.gdal)<br># 3 - DN to Top of Atmosphere reflectance (i.landsat.toar)<br># 4 - TOA reflectance to Surface reflectance (i.atcorr)<br># 5 - NDVI (<a href="http://i.vi">i.vi</a>), Albedo (i.albedo), Emissivity (i.emissivit<br><br># USER HAS TO SET THOSE<br># QUIET REPORTS<br>QIET = True<br># OVERWRITE EXISTING FILES<br>OVR = False<br># Define Landsat 7 sensor for i.landsat.toar<br>LSENSOR = "tm7"<br><br># Setup the path to the Landsat 7 Directories<br>rsdatapath = "~/rawData/L7Dir"<br># set L7 Metadata wildcards<br>wldc_mtl = "*_MTL.txt"<br># Visibility distance [Km]<br>vis = 18<br><br># DEM input to atmospheric correction<br>inDEM = rsdatapath + "/dem_30m/Kenya_SRTM30meters.tif"<br><br>import glob<br>import os<br>import subprocess<br>import sys<br><br># path to the GRASS GIS launch script<br># MS Windows<br># grass7bin_win = r'C:\OSGeo4W\bin\grass72svn.bat'<br># uncomment when using standalone WinGRASS installer<br># grass7bin_win = r'C:\Program Files (x86)\GRASS GIS 7.2.0\grass72.bat'<br># Linux<br>grass7bin_lin = 'grass72'<br># Mac OS X<br># grass7bin_mac = '/Applications/GRASS/GRASS-7.2.app/'<br><br># DATA<br># define GRASS DATABASE<br># add your path to grassdata (GRASS GIS database) directory<br>gisdb = os.path.join(os.path.expanduser("~"), "rawData")<br># the following path is the default path on MS Windows<br># gisdb = os.path.join(os.path.expanduser("~"), "Documents/grassdata")<br><br># specify (existing) location and mapset<br>location = "L7_Kenya"<br>mapset = "hempire"<br><br># SOFTWARE<br>if sys.platform.startswith('linux'):<br> # we assume that the GRASS GIS start script is available and in the PATH<br> # query GRASS 7 itself for its GISBASE<br> grass7bin = grass7bin_lin<br>else:<br> raise OSError('Platform not configured.')<br><br># query GRASS 7 itself for its GISBASE<br>startcmd = [grass7bin, '--config', 'path']<br><br>p = subprocess.Popen(startcmd, shell=False,<br> stdout=subprocess.PIPE, stderr=subprocess.PIPE)<br>out, err = p.communicate()<br>if p.returncode != 0:<br> print >> sys.stderr, "ERROR: Cannot find GRASS GIS 7 start script (%s)" % startcmd<br> sys.exit(-1)<br>gisbase = out.strip('\n\r')<br><br># Set GISBASE environment variable<br>os.environ['GISBASE'] = gisbase<br># the following not needed with trunk<br>os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'extrabin')<br># add path to GRASS addons<br>home = os.path.expanduser("~")<br>os.environ['PATH'] += os.pathsep + os.path.join(home, '.grass7', 'addons', 'scripts')<br><br># define GRASS-Python environment<br>gpydir = os.path.join(gisbase, "etc", "python")<br>sys.path.append(gpydir)<br><br># DATA<br># Set GISDBASE environment variable<br>os.environ['GISDBASE'] = gisdb<br><br># import GRASS Python bindings (see also pygrass)<br>import grass.script.setup as gsetup<br><br>gsetup.init(gisbase, gisdb, location, mapset)<br>from grass.pygrass.modules.shortcuts import raster as r<br><br><br># Needed for floor()<br><br><br># env = os.environ.copy()<br># env['GRASS_MESSAGE_FORMAT'] = 'gui'<br># Function to get a list of L7 Directories in the rsdatapath<br>def fn(path):<br> for top, dirs, files in os.walk(path):<br> return [os.path.join(top, dir) for dir in dirs]<br><br><br># START PROCESS<br># PART 0: PRE-PROCESSING STUFF ###<br># import DEM for atmospheric correction<br># r.in.gdal(input=inDEM,output="dem",overwrite=OVR)<br># r.mapcalc(expression="dem=25",overwrite=OVR)<br># create a visibility map<br># r.mapcalc(expression="vis=18", overwrite=OVR)<br># Find the central location of the Landsat file from metadata<br>metadata = []<br>fileList = []<br>L7Dirs = fn(rsdatapath)<br>for L7Dir in L7Dirs:<br> # Ungzip all of your Landsat7 images in all your directories<br> # print "Ungzip Landsat files in\t",L7Dir<br> # p=os.system("gzip -d -q "+L7Dir+"/*.gz")<br> # Using pthreads on multi-core machines<br> # p=os.system("pigz -d "+L7Dir+"/*.gz")<br> # Wait ten seconds for gzip to create the tif images<br> # time.sleep(10)<br> print "Import in GRASS GIS"<br><br clear="all"><div><div><div><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><br><br>Kind Regards,<div><font size="4" face="comic sans ms, sans-serif"><b><span style="color:rgb(0,0,0)"><span style="font-family:arial,helvetica,sans-serif">Joseph Kariuki</span></span><br></b></font></div><div><br></div><div><span style="color:rgb(111,168,220)"><u><b>Geospatial Engineer</b></u></span><span style="color:rgb(153,153,153)"><b> | </b></span><u><span style="color:rgb(106,168,79)"><b>GIS / Web Developer</b></span></u><b> </b></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
</div></div></div>