Modify output name / counter in python script

Hello,
The script below executes fine (Its from the ESRI user forums).  But I would like to modify the output naming convention.  The script goes through two directories and takes all of the "fc" files and performs an operation on all of the "grid" files. It does this by the taking the first fc and performing the operation on all of the grids, and then moves on to the second fc and all of the grids, and so forth and so on.

This following line is the line that creates the output(Generically, function(input 1, input 2, output)):

 gp.ExtractByMask_sa(GridFolder + "/" + grid, fc, OutFolder + "/" + grid + str(gridcount) + str(fccount))
       

this creates output named like this

grida11
gridb21
gridc31
...
grida12
gridb22
gridc32

I would like the output to look like this:
grida_001
gridb_001
gridc_001
...
grida_002
gridb_002
gridc_002
...
grida_435
gridb_435
gridc_435

 or output = OutFolder + "/" + grid + xxx
where xxx = a list of numbers (see attached)

Can this be done? If so, how?  Annotated code is appreciated.

Thank you kindly,
JE


# Batch Clip Multiple Grids with multiple feature classes.py
# Author
#  Jeff Lee 
#
# Date September 17, 2009
#
#
#Purpose
#  This script clips multiple grids using multiple feature classes. 
#  
#
#Properties (right-click on the tool and specify the following)
#General
#  Name   ClipGrid
#  Label  Batch clip multiple grids with multiple feature classes
#  Desc   This script clips multiple grids using multiple feature classes. 
#
#
#Source script Batch Clip Grid.py
#Parameter list
#                                           Parameter Properties
#            Display Name           Data type                   Type      Direction  MultiValue Dependency
#  argv[1]   Workspace              Workspace                   Required    Input      No            No  
#  argv[2]   Grid  Folder           Folder                      Required    Input      No            No
#  argv[3]   Output Folder          Folder                      Required    Input      No            No
#-----------------------------------------------------------------------------------------------------------------------------
# 
import sys, string, os # Import common system modules

try:
  #for 9.2 and above
  import arcgisscripting
  gp = arcgisscripting.create(9.3)
  gp.AddMessage("\n" + "Using ArcMap 9.2 or above with arcgisscripting..." + "\n")
except:
  #for 9.0/9.1
  import win32com.client
  gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
  gp.AddMessage("\n" + "Using ArcMap 9.0/9.1 or above with win32com.client.Dispatch..." + "\n")
#
# Check for the necessary product

Licensed = gp.ProductInfo()
gp.addmessage("You are using the following product " + str(Licensed))

try: 
    gp.SetProduct(str(Licensed))
    gp.addmessage("Successfully set the product type to " + str(Licensed))
except:
    gp.addmessage("Could not find an available ArcGIS License")

# Load required toolboxes...
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")

# Check out Spatial Analyst License
gp.CheckOutExtension("spatial")

gp.Workspace = sys.argv[1]
Workspace = str(gp.Workspace)
Workspace = string.replace(Workspace, "\\", "/")

GridFolder = sys.argv[2]
GridFolder = str(GridFolder)
GridFolder = string.replace(GridFolder, "\\", "/")

OutFolder = sys.argv[3]
OutFolder = str(OutFolder)
OutFolder = string.replace(OutFolder, "\\", "/")

#Generate a list of the grids in the grid folder
#All ESRI grids should have an associated .aux file in the grid folder

GridList = []

# FileList = os.listdir(GridFolder)

for file in os.listdir(GridFolder):
    # Get only the grids
    if (file.endswith(".aux")):
        basename, extension = file.split('.')
        GridList.append(basename)

fccount = 0


#Get a list of feature classes in the input folder
fcs = gp.ListFeatureClasses() 

for fc in fcs:
  
  #Open an output files to write the feature class names that could not clip the grid
  outputfile1 = open(OutFolder + "/" + "Unclipped_grids.txt", "w")

  try:

    gp.addmessage("processing feature  " + str(fc) + "  Number  " + str(fccount))
    fccount += 1

    try:

      # Iterate through the grid list
      gridcount = 1
      for grid in GridList:
        # Clip the grid with polygon feature class
        gp.ExtractByMask_sa(GridFolder + "/" + grid, fc, OutFolder + "/" + grid + str(gridcount) + str(fccount))
        gp.addmessage(str(grid) + " clipped sucessfully by " + str(fc))
        gridcount += 1

    except:

      outputfile1.write(str(fc) + '\n')
      gp.addmessage("Could not clip " + str(grid) + " with " + str(fc))
            
      # Move to the next feature class
        
  except:
    
    #If an error occurred while running the script, then print the messages
    print gp.GetMessages()
    # closes output file after processing
    outputfile1.close()
    del GridList
    gp.CheckInExtension("spatial")

Open in new window

justearthAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

justearthAuthor Commented:
Number list below.

{001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
217
315
372
373
374
375
376
419
424
426
431
435}

Open in new window

0
HonorGodSoftware EngineerCommented:
You're going to have to append the source of the ExtractByMask_sa method (at least).
0
justearthAuthor Commented:
HonorGod:
>You're going to have to append the source of the ExtractByMask_sa method  (at least).

Could you please explain this a little more?

Thanks,
JE
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

mish33Commented:
Replace 89 with
for fc_index, fc in enumerate(fcs):

Replace 105 with
        gp.ExtractByMask_sa(GridFolder + "/" + grid, fc, OutFolder + "/" + grid + ("_%03d" % numbers[fc_index]))

Add before 75
numbers = [1, 2, 3, # and so on ending with 435
                  ]

Each fc output will be named with corresponding number taken by fc_index.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
justearthAuthor Commented:
mish33:

Thanks - it works great.  Could you please explain in words what these updates mean / are doing?

Cheers,
JE
0
mish33Commented:
1st change - iterate over fc with index, ie. if we have fcs=['a', 'b']
we get  pairs (0,'a'), (1,'b')

2nd - generate output filename from number you supplied using fc index to get number corresponding to the fc.
0
justearthAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.