Select to view content in your preferred language

Combing Multiple Models

4165
16
Jump to solution
08-28-2013 08:24 AM
CraigCarsley
Frequent Contributor
I'm starting to get a feel for Model Builder, but I can't figure this out:

I've got 9 geoprocessing models that are all part of the same essential process.  I want to have one model, or script, that will run each one of them consecutively.  I tried doing everything in a single model, but I got red exes on some of the tools because I am overwriting some datasets.  At the same time, it could be useful to keep each step separated anyway.

So, does anyone know of anyway to automate running 9 models consecutively?

Thanks!
0 Kudos
1 Solution

Accepted Solutions
RussellBrennan
Esri Contributor
Hi Mike,
What I think is the easiest way to do this is to call each of your models from a script. If all of your models are in a single toolbox this is really easy. This is a much cleaner way to execute the custom functions without having to write any??? complex python.

Write a python script that looks like this (sorry for the lame names in the example):
import arcpy  #Import the toolbox containing the models. arcpy.ImportToolbox(r"c:\temp\toolboxname.tbx", "toolboxAlias")  #Run each model as though it were a tool arcpy.ModelName_toolboxAlias() arcpy.Model2Name_toolboxAlias() arcpy.Model3Name_toolboxAlias()


In the example above I have a toolbox named 'toolboxname' that stores all of my models. In this toolbox I have three models named ModelName, Model2Name, Model3Name. In the example I have also chosen an alias for my tools. The alias is 'toolboxAlias', the alias is just used to differentiate your tools from other system tools so there are not name collisions. This is similar to some core tools that have a _management or _analysis alias. Each of the models do not take any input parameters hence the (). If one or many inputs were required for these models you would put them between the () brackets (ex: arcpy.ModelName_toolboxAlias('c:\temp\test.gdb\featureclass'))

You can then run this very simple script to call your models in sequence.

A (slightly) better example might look like this:
import arcpy  #Import the toolbox containing the models. arcpy.ImportToolbox(r"c:\temp\my_analysis.tbx", "models")  #Run each model as though it were a tool arcpy.CustomBufferIntersect_models() arcpy.CreateNewFeatures_models() arcpy.MakeSurface_models()

View solution in original post

16 Replies
MichaelVolz
Esteemed Contributor
I would export your models out to python scripts and then call them consecutively from a bat file that is called from a scheduled task.

I can provide a sample bat file if you need it.
0 Kudos
CraigCarsley
Frequent Contributor
Thanks Michael,

I have been trying to figure out how to do just that.  I'm not much of a python whiz.
Would you, please, provide an example?

Thanks again
0 Kudos
MichaelVolz
Esteemed Contributor
@echo off

echo Start time: %time% > update_parcel.log

echo ------------  Update time: %time%
echo ------------  06 Truncating parcel feature class  -----------------------
echo ------------  06 Truncating parcel feature class  ----------------------- >> update_parcel.log
06_truncate_parcels.py >> update_parcel.log

echo  ' ' >> update_parcel.log
echo Update time: %time% >> update_parcel.log
echo  ' ' >> update_parcel.log

echo ------------  Update time: %time%
echo ------------  07 Appending parcel feature class  -----------------------
echo ------------  07 Appending parcel feature class  ----------------------- >> update_parcel.log
07_append_parcels.py >> update_parcel.log

echo  ' ' >> update_parcel.log
echo Update time: %time% >> update_parcel.log
echo  ' ' >> update_parcel.log

@echo on
0 Kudos
CraigCarsley
Frequent Contributor
I must be less apt for this than I thought.. I can't make any sense out of that.  Any chance you could explain a little more?
0 Kudos
MichaelVolz
Esteemed Contributor
Craig:

The echo statements allow for messages to be written to the log file (for understanding how processes are working), as well as to the DOS prompt.

I am able to call just the python script name because I am running on a 64 bit server with only the 32 bit version of python installed (c:\Python26\ArcGIS10.0\python.exe) and the PYTHONPATH environmental variable set to c:\Python26\ArcGIS10.0.  [If you are running on a server with ArcGIS Server also installed with background processing enabled this gets trickier as you will also have a 64 bit version of python installed on the machine.]

>>update_parcel.log after the call to the python script allows me to use print statements in the python script that get written out to the log file.

I use the %time% statements to write out important times in the process to the log file which helps to diagnose problems and determine how long processes run so I can optimize times when scripts are run, especially when order of the scripts is important and I need to work around database maintenance times.
0 Kudos
CraigCarsley
Frequent Contributor
That helps, thanks!
0 Kudos
RussellBrennan
Esri Contributor
Hi Mike,
What I think is the easiest way to do this is to call each of your models from a script. If all of your models are in a single toolbox this is really easy. This is a much cleaner way to execute the custom functions without having to write any??? complex python.

Write a python script that looks like this (sorry for the lame names in the example):
import arcpy  #Import the toolbox containing the models. arcpy.ImportToolbox(r"c:\temp\toolboxname.tbx", "toolboxAlias")  #Run each model as though it were a tool arcpy.ModelName_toolboxAlias() arcpy.Model2Name_toolboxAlias() arcpy.Model3Name_toolboxAlias()


In the example above I have a toolbox named 'toolboxname' that stores all of my models. In this toolbox I have three models named ModelName, Model2Name, Model3Name. In the example I have also chosen an alias for my tools. The alias is 'toolboxAlias', the alias is just used to differentiate your tools from other system tools so there are not name collisions. This is similar to some core tools that have a _management or _analysis alias. Each of the models do not take any input parameters hence the (). If one or many inputs were required for these models you would put them between the () brackets (ex: arcpy.ModelName_toolboxAlias('c:\temp\test.gdb\featureclass'))

You can then run this very simple script to call your models in sequence.

A (slightly) better example might look like this:
import arcpy  #Import the toolbox containing the models. arcpy.ImportToolbox(r"c:\temp\my_analysis.tbx", "models")  #Run each model as though it were a tool arcpy.CustomBufferIntersect_models() arcpy.CreateNewFeatures_models() arcpy.MakeSurface_models()
GavinMcDade
Regular Contributor
Hey guys,

I work with Mike and the models/scripts in question are actually mine. Our main reason for exporting our models to Python scripts and running them as such is mainly because of the ability to augment the scripts to include Try/Except error-handling, etc. That said, I wouldn't mind implementing your script below, Russel - albeit, to call the Python scripts instead of the models themselves. Does that function the same way as calling a model?

Gavin
0 Kudos
RussellBrennan
Esri Contributor
Hi Gavin,

When you import your toolbox using arcpy.ImportToolbox the models within your toolbox function just like any other tool. Essentially each model becomes a tool. You will then have the ability to use try/except blocks catch any failures. You could also parse messages returned from 'arcpy.getmessages()' if there are specific messages you are interested in investigating.


import arcpy#Import the toolbox containing the models.arcpy.ImportToolbox(r"c:\temp\my_analysis.tbx", "models")#Run each model as though it were a tool#use try/except for finding major failures.#use getmessages to find more specific info.try:    arcpy.CustomBufferIntersect_models()    msg = arcpy.GetMessages()    if msg.lower().find('error 000375'):        print "Do something!"    else:        print "That's not working, do something else!"    arcpy.CreateNewFeatures_models()    arcpy.MakeSurface_models()except:    print 'Oh no! Something went wrong'


When calling the model from a script you do not get granularity at the tool level you just get the entire model. This is what we usually recommend for people who want to do some sort of automated process but do not want to write much Python. The problem with the scripts exported from models is that they are very messy and hard to read. Also you can end up with a lot of redundant code.

Hope this helps.
0 Kudos