[GRASS-dev] Optimizing a script

antonio.rocha at deimos.com.pt antonio.rocha at deimos.com.pt
Fri Oct 29 19:06:45 EDT 2010


Greetings

I have created a script to detect clouds/shadows (i.landsat.acca-  
Great work Jorge), grow of these pixels and then identify an area that  
is common to all bands (7).And at the end, turn these pixels into  
NULL. Above this paragraph I placed a part of this code. My problem is  
that, for regular satellite images  it's a very slow process. How can  
I optimize it? any suggestion?
Thanks

def cloud_detection(output_t,output_thermal_t,landsatsensor):
     #Create temporary map for cloud and shadows
     tempC= "myscript.tmpcloud"
     #Create temporary map for valid areas
     tempV= "myscript.tmpvalid"
     #Create temporary file
     tempB= "myscript.tmpB"

#Due to a shift between it will be necessary to identify a valid area  
for all bands
     try:
         grass.mapcalc("$tempV= if(($B1>=0 && $B7>=0 && $B3>=0 &&  
$B4>=0 && $B5>=0 && $B7>=0 &&  
$B6>=0),1,null())",tempV=tempV,B1=output_t[0],B2=output_t[1],B3=output_t[2],B4=output_t[3],B5=output_t[4],B7=output_t[4],B6=output_thermal_t)
     except:
         grass.fatal(_("ERROR"))

#Apply i.landsat.acca module according to landsat sensor type
     if landsatsensor == '5TM':
        p=grass.run_command("i.landsat.acca", band_prefix="B",  
output=tempC, flags="52s")
     elif landsatsensor == '7ETM':
        p=grass.run_command("i.landsat.acca", band_prefix="B",  
output=tempC,flags="2s")
	#Apply r.grow to detected clouds and shadows
     p=grass.run_command("r.grow", input=tempC, output=tempC,  
overwrite=True, radius=3, old=1, new=1, metric="euclidean")

     p=grass.run_command("r.null", map=tempC, null=0)
     p=grass.run_command("r.null", map=tempV, null=0)

	#Cross bands (output_t) with r.grow ou*tput and Valid area
     for i in output_t:
         try:
             grass.mapcalc("$output= $input",output=tempB, input=i)
         except:
             grass.fatal(_("ERROR"))
         try:
             grass.mapcalc("$output= if(($valid==1 &&  
$cloud!=1),$input,null())",output=i, input=tempB,  
valid=tempV,cloud=tempC)
         except:
             grass.fatal(_("ERROR"))

	#Eliminate temporary raster


More information about the grass-dev mailing list