Avatar of mgjust
mgjust

asked on 

Python script error - Assertion error

Hello,
I am trying to use the code I got from here:

https://www.experts-exchange.com/questions/23068228/Modify-Python-Script-for-more-automation.html

I have changed directory locations and the lines:

expTemplate = 'CON(ISNULL(%s), %s, %s * -1)'

to

expTemplate = '(%s, + (%s * 10))'

and

  numSuffix = LnumDir[-2:]   # last 2 chars
            idSuffix = LnumDir[-4:]    # last 4 chars

to

  numSuffix = LnumDir[-3:]   # last 3 chars
            idSuffix = LnumDir[-5:]    # last 5 chars

I get the following error:

C:\Documents and Settings\u4cncmgj>c:\python24\python "C:\Documents and Settings
\u4cncmgj\My Documents\code\to_create_redone_binLC16_with3digits.py
Traceback (most recent call last):
  File "C:\Documents and Settings\u4cncmgj\My Documents\code\to_create_redone_bi
nLC16_with3digits.py", line 51, in ?
    for landcover, donut, idSuffix in allLandcoverDonutAndIdSuffix(gisworkDir):
  File "C:\Documents and Settings\u4cncmgj\My Documents\code\to_create_redone_bi
nLC16_with3digits.py", line 40, in allLandcoverDonutAndIdSuffix
    assert os.path.isdir(DnumDir)
AssertionError

Here is the code I used:

# Multi_Output_Map_Algebra_sample.py
# Description:
#   Runs an expression built with the Map Algebra language.
# Requirements: None
 
import arcgisscripting
import glob
import os
 
gisworkDir = 'C:\GISWORK\Hibernacula\EUA_805\TOCREATE2016'
 
def log(s):
    f = open("C:/tester.txt","w")
    f.write(s)
    f.close()
 
 
def allLandcoverDonutAndIdSuffix(gisworkDir):
    '''Generates all tuples (landcover, donut, idSuffix) from the gisworkDir.'''
    ##log("1\n")
    # Prepare the path to the donuts.
    donutDir = os.path.join(gisworkDir, 'Donut')
    assert os.path.isdir(donutDir)
    ##log("2\n")
    # Iterate through sequence of top-level landcover subdirectories.
    mask = os.path.normpath(os.path.join(gisworkDir, 'landcover*'))
    ##log(mask)            
    for LDir in glob.glob(mask):      # full paths to LandcoverA, LandcoverB, etc.
        subdir = os.path.basename(LDir)
        ##log("3\n")
        # Iterate through all numbered landcover subdirs inside.
        mask2 = os.path.join(LDir, '*')
        for LnumDir in glob.glob(mask2):
            # Separate the counter part, the idSuffix and construct
            # the related donut numbered directory name.
            numSuffix = LnumDir[-3:]   # last 3 chars
            idSuffix = LnumDir[-5:]    # last 5 chars
            DnumDir = os.path.normpath(
                         os.path.join(donutDir, 'DONUT_' + numSuffix))
            assert os.path.isdir(DnumDir)  
            ##log("4")
            # Generate another output tuple.
            yield (LnumDir, DnumDir, idSuffix)
 
 
# Create the Geoprocessor object
gp = arcgisscripting.create()
expTemplate = '(%s, + (%s * 10))'
log("\n===== Output below: ====\n")
 
for landcover, donut, idSuffix in allLandcoverDonutAndIdSuffix(gisworkDir):
    # Construct the expression and the output directory name.
    outDir = os.path.join(gisworkDir, 'landcoverprep', 'Results', idSuffix)
    log("\n" + outDir)
    InExpression = expTemplate % (donut, landcover)
    log("  " + InExpression)
 
    # Check out Spatial Analyst extension license
    gp.CheckOutExtension("Spatial")
 
    # Process: MapAlgebra
    gp.SingleOutputMapAlgebra_sa(InExpression, outDir)
 
    log("Msg: " + gp.GetMessages())



Please advise,
Thanks,
MJ



# Multi_Output_Map_Algebra_sample.py
# Description:
#   Runs an expression built with the Map Algebra language.
# Requirements: None
 
import arcgisscripting
import glob
import os
 
gisworkDir = 'c:\Giswork'
 
def log(s):
    f = open("C:/tester.txt","w")
    f.write(s)
    f.close()
 
 
def allLandcoverDonutAndIdSuffix(gisworkDir):
    '''Generates all tuples (landcover, donut, idSuffix) from the gisworkDir.'''
    ##log("1\n")
    # Prepare the path to the donuts.
    donutDir = os.path.join(gisworkDir, 'landcoverprep', 'Donut')
    assert os.path.isdir(donutDir)
    ##log("2\n")
    # Iterate through sequence of top-level landcover subdirectories.
    mask = os.path.normpath(os.path.join(gisworkDir, 'Landcoverprep\landcover*'))
    ##log(mask)             
    for LDir in glob.glob(mask):      # full paths to LandcoverA, LandcoverB, etc.
        subdir = os.path.basename(LDir)
        ##log("3\n")
        # Iterate through all numbered landcover subdirs inside.
        mask2 = os.path.join(LDir, '*')
        for LnumDir in glob.glob(mask2):
            # Separate the counter part, the idSuffix and construct 
            # the related donut numbered directory name.
            numSuffix = LnumDir[-2:]   # last 2 chars
            idSuffix = LnumDir[-4:]    # last 4 chars
            DnumDir = os.path.normpath(
                         os.path.join(donutDir, 'DONUT_' + numSuffix))
            assert os.path.isdir(DnumDir)  
            ##log("4")
            # Generate another output tuple.
            yield (LnumDir, DnumDir, idSuffix)
 
 
# Create the Geoprocessor object
gp = arcgisscripting.create()
expTemplate = 'CON(ISNULL(%s), %s, %s * -1)'
log("\n===== Output below: ====\n")
 
for landcover, donut, idSuffix in allLandcoverDonutAndIdSuffix(gisworkDir):
    # Construct the expression and the output directory name.
    outDir = os.path.join(gisworkDir, 'landcoverprep', 'Results', idSuffix)
    log("\n" + outDir)
    InExpression = expTemplate % (donut, landcover, landcover)
    log("  " + InExpression)
 
    # Check out Spatial Analyst extension license
    gp.CheckOutExtension("Spatial")
 
    # Process: MapAlgebra
    gp.SingleOutputMapAlgebra_sa(InExpression, outDir)
 
    log("Msg: " + gp.GetMessages())

Open in new window

Python

Avatar of undefined
Last Comment
mgjust
Avatar of mish33
mish33
Flag of United States of America image

There is no 'Donut' dir in C:\GISWORK\Hibernacula\EUA_805\TOCREATE2016
which the script expect to be.
Avatar of mgjust
mgjust

ASKER

mish33:

Thanks, However:

There are directories named:

Donut
LandcoverI
LandcoverL

Do the sub directories name matter as long as the have the three digit number at the end?
Any other possible problems?

Thanks,
MJ
Avatar of mish33
mish33
Flag of United States of America image

Donut should be exactly Donut, not Donut001 not Donut.001 etc.

Line
gisworkDir = 'C:\GISWORK\Hibernacula\EUA_805\TOCREATE2016'

is wrong, it should be either
gisworkDir = r'C:\GISWORK\Hibernacula\EUA_805\TOCREATE2016'
or
gisworkDir = 'C:\\GISWORK\\Hibernacula\\EUA_805\\TOCREATE2016'

which mean the same.
Avatar of mgjust
mgjust

ASKER

Hello,
My files are as you suggest and I am now getting the same error.

Please advise,
MJ
Avatar of mish33
mish33
Flag of United States of America image

All backslashes in pathes should be either
(a) escaped with \ (like 'c:\\path')
or (b) made raw with r'' (like r'c:\path')
or (c) converted to forward slashes (like 'c:/path)

Also you access Donut_NNNN !?
Do you still that assert statement?

What is your dir structure?
Avatar of mgjust
mgjust

ASKER

mish33:
Hello,
1. All backslashes in paths have been converted to forward slashes.

2. You said "Also you access Donut_NNNN!? Do you still assert that last statement?" I am unsure of what this means, but yes I am still trying to use Donut.

3. Directory structure:

C:\Giswork\landcoverprep\Donut
C:\Giswork\landcoverprep\LandcoverI
C:\Giswork\landcoverprep\Results

Donut has folders named "donut_001 - donut_407, donut_431, donut_435, donut_440, donut_447, donut_448

LandcoverI has the same numbering (i.e. LandcoverI_001 - 407, 431, 435, 440, 447)

Please advise,
MJ
Avatar of mish33
mish33
Flag of United States of America image

ok, please post latest code as snippet (updates to use last 3 chars for number) and the error (I guess line # is different - if not and it's the same as in the begining, fine)

Your numbered code has right workdir but [-2:]. Your inline code has different workdir but [-3:]!?
Avatar of mgjust
mgjust

ASKER

mish33"

With the snippet below I get the following error:

C:\Documents and Settings\user>c:\python24\python "c:\new2.py"
Traceback (most recent call last):
  File "c:\new2.py", line 51, in ?
    for landcover, donut, idSuffix in allLandcoverDonutAndIdSuffix(gisworkDir):
  File "c:\new2.py", line 40, in allLandcoverDonutAndIdSuffix
    assert os.path.isdir(DnumDir)
AssertionError

All Landcover and Donut directories have three digits (donut_xxx & landcover*_xxx) and will not be mixed with the old directories with only two appended digits.  How come changing the -2, -4 to :

numSuffix = LnumDir[-3:]   # last 3 chars
idSuffix = LnumDir[-5:]    # last 3 chars

did not solve the problem?


Thanks,
MJ
# Multi_Output_Map_Algebra_sample.py
# Description:
#   Runs an expression built with the Map Algebra language.
# Requirements: None
 
import arcgisscripting
import glob
import os
 
gisworkDir = 'c:\Giswork'
 
def log(s):
    f = open("C:/tester.txt","w")
    f.write(s)
    f.close()
 
 
def allLandcoverDonutAndIdSuffix(gisworkDir):
    '''Generates all tuples (landcover, donut, idSuffix) from the gisworkDir.'''
    ##log("1\n")
    # Prepare the path to the donuts.
    donutDir = os.path.join(gisworkDir, 'landcoverprepnew', 'Donut')
    assert os.path.isdir(donutDir)
    ##log("2\n")
    # Iterate through sequence of top-level landcover subdirectories.
    mask = os.path.normpath(os.path.join(gisworkDir, 'Landcoverprepnew\landcover*'))
    ##log(mask)             
    for LDir in glob.glob(mask):      # full paths to LandcoverA, LandcoverB, etc.
        subdir = os.path.basename(LDir)
        ##log("3\n")
        # Iterate through all numbered landcover subdirs inside.
        mask2 = os.path.join(LDir, '*')
        for LnumDir in glob.glob(mask2):
            # Separate the counter part, the idSuffix and construct 
            # the related donut numbered directory name.
            numSuffix = LnumDir[-3:]   # last 3 chars
            idSuffix = LnumDir[-5:]    # last 5 chars
            DnumDir = os.path.normpath(
                         os.path.join(donutDir, 'DONUT_' + numSuffix))
            assert os.path.isdir(DnumDir)  
            ##log("4/n")
            # Generate another output tuple.
            yield (LnumDir, DnumDir, idSuffix)
 
 
# Create the Geoprocessor object
gp = arcgisscripting.create()
expTemplate = '(%s, + (%s * 10))'
log("\n===== Output below: ====\n")
 
for landcover, donut, idSuffix in allLandcoverDonutAndIdSuffix(gisworkDir):
    # Construct the expression and the output directory name.
    outDir = os.path.join(gisworkDir, 'landcoverprepnew', 'Results', idSuffix)
    log("\n" + outDir)
    InExpression = expTemplate % (donut, landcover)
    log("  " + InExpression)
 
    # Check out Spatial Analyst extension license
    gp.CheckOutExtension("Spatial")
 
    # Process: MapAlgebra
    gp.SingleOutputMapAlgebra_sa(InExpression, outDir)
 
    log("Msg: " + gp.GetMessages())

Open in new window

SOLUTION
Avatar of mish33
mish33
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of mgjust
mgjust

ASKER

mish33:
I completed your suggestions as seen in the code snippet below. I got this error message:

C:\Documents and Settings\u4cncmgj>c:\python24\python "C:\Documents and Settings
\u4cncmgj\Desktop\mish_donut.py"
C:\GISWORK\Hibernacula\EUA_805\TOCREATE2016\LandcoverI\I_001.aux C:\GISWORK\Hibe
rnacula\EUA_805\TOCREATE2016\Donut\DONUT_aux
Traceback (most recent call last):
  File "C:\Documents and Settings\u4cncmgj\Desktop\mish_donut.py", line 52, in ?

    for landcover, donut, idSuffix in allLandcoverDonutAndIdSuffix(gisworkDir):
  File "C:\Documents and Settings\u4cncmgj\Desktop\mish_donut.py", line 41, in a
llLandcoverDonutAndIdSuffix
    assert os.path.isdir(DnumDir), "Dir %s not found" % DnumDir
AssertionError: Dir C:\GISWORK\Hibernacula\EUA_805\TOCREATE2016\Donut\DONUT_aux
not found

The log test_mish outputted:

===== Output below: ====


Please advise,
Thank you kindly,
MJ
# Multi_Output_Map_Algebra_sample.py
# Description:
#   Runs an expression built with the Map Algebra language.
# Requirements: None
 
import arcgisscripting
import glob
import os
 
gisworkDir = 'C:/GISWORK/Hibernacula/EUA_805/TOCREATE2016'
 
def log(s):
    f = open("C:/test_mish.txt","w")
    f.write(s)
    f.close()
 
 
def allLandcoverDonutAndIdSuffix(gisworkDir):
    '''Generates all tuples (landcover, donut, idSuffix) from the gisworkDir.'''
    ##log("1\n")
    # Prepare the path to the donuts.
    donutDir = os.path.join(gisworkDir, 'Donut')
    assert os.path.isdir(donutDir)
    ##log("2\n")
    # Iterate through sequence of top-level landcover subdirectories.
    mask = os.path.normpath(os.path.join(gisworkDir, 'landcover*'))
    ##log(mask)             
    for LDir in glob.glob(mask):      # full paths to LandcoverA, LandcoverB, etc.
        subdir = os.path.basename(LDir)
        ##log("3\n")
        # Iterate through all numbered landcover subdirs inside.
        mask2 = os.path.join(LDir, '*')
        for LnumDir in glob.glob(mask2):
            # Separate the counter part, the idSuffix and construct 
            # the related donut numbered directory name.
            numSuffix = LnumDir[-3:]   # last 3 chars
            idSuffix = LnumDir[-5:]    # last 5 chars
            DnumDir = os.path.normpath(
                         os.path.join(donutDir, 'DONUT_' + numSuffix))
            print LnumDir, DnumDir
            assert os.path.isdir(DnumDir), "Dir %s not found" % DnumDir  
            ##log("4/n")
            # Generate another output tuple.
            yield (LnumDir, DnumDir, idSuffix)
 
 
# Create the Geoprocessor object
gp = arcgisscripting.create()
expTemplate = '(%s, + (%s * 10))'
log("\n===== Output below: ====\n")
 
for landcover, donut, idSuffix in allLandcoverDonutAndIdSuffix(gisworkDir):
    # Construct the expression and the output directory name.
    outDir = os.path.join(gisworkDir, 'Results', idSuffix)
    log("\n" + outDir)
    InExpression = expTemplate % (donut, landcover)
    log("  " + InExpression)
 
    # Check out Spatial Analyst extension license
    gp.CheckOutExtension("Spatial")
 
    # Process: MapAlgebra
    gp.SingleOutputMapAlgebra_sa(InExpression, outDir)
 
    log("Msg: " + gp.GetMessages())

Open in new window

Avatar of mish33
mish33
Flag of United States of America image

As you see in the error: Donut_aux not found

What do you want to do in that case?
Avatar of mgjust
mgjust

ASKER

I would like to continue without it.
ASKER CERTIFIED SOLUTION
Avatar of mish33
mish33
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of mgjust
mgjust

ASKER

Thanks.
Python
Python

Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in other languages. Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive set of standard libraries, including NumPy, SciPy, Django, PyQuery, and PyLibrary.

6K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo