Name output using input base name (minus extension)

188
10
08-15-2011 09:28 AM
deleted-user-XuRyPg9pv1OY
New Contributor III
Hi All,

In modelbuilder, I need to use the base name from an input file to name an output later in the process.  For example, I have NetCDF files named like A12_G01_X00_ABCABC10.nc.  I want to clip the features and name the output using the same name, minus the ".nc" extension.

I can easily use the input filename as an inline variable using %InputFile%.shp, but that produces A12_G01_X00_ABCABC10.nc.shp, which contains an extra period and therefore creates an illegal character.

Is it possible to use an inline variable to drop the extension ".nc" and replace it with ".shp".

Thank you!

Matt Ruch
GIS Analyst
Brown & Caldwell
0 Kudos
10 Replies
deleted-user-XuRyPg9pv1OY
New Contributor III
I should mention that I am using 9.3.1.
0 Kudos
DaleHoneycutt
Regular Contributor
Use the Calculate Value tool.

Assuming you have a model variable named "Input Features" and its contents is "E:\Data\Iowa\IowaCo.shp".

Add the Calculate Value tool to your model.

For the Expression parameter, enter:
base("%Input Features%")

(the name of the variable)

In the Code Block parameter: enter:
def base(filename):
  import os
  y = os.path.basename(filename)
  return y[:y.rfind(".")]


In the Data Type parameter, set it to String.

The output of Calculate Value ("output_value") by default will contain (when model is run) "IowaCo".  You can then use %output_value% to name your file.
0 Kudos
deleted-user-XuRyPg9pv1OY
New Contributor III
Worked great! Thank you, Dale.

Matt Ruch
0 Kudos
deleted-user-XuRyPg9pv1OY
New Contributor III
Is it possible to use os.path.split in the Calculate Values tool to extract parts from the base name, where the underscore is the separator for the parts?  I will need to use those individual parts to calculate field attributes. Using the example Dale provided above, I would want to use the "Iowa" part of "IowaCo" and calculate a field called [Name] = "Iowa".

Thank you,

Matt Ruch
0 Kudos
DaleHoneycutt
Regular Contributor
Check out this blog on Calculate Field.  It shows how to use .split.  Although it's about Calculate Field and not Calculate Value, I think you'll get the idea -- a code block w/in Calculate Value that breaks apart a string and returns a new string.
0 Kudos
LoganPugh
Occasional Contributor III
If you're going to be parsing strings a lot you will definitely want to look into regular expressions (see the re module in Python and the accompanying "how to" document).
0 Kudos
deleted-user-XuRyPg9pv1OY
New Contributor III
Dang! I always forget about the veritable plethora of ESRI blogs out there.

Thank you both for posting very useful links. They will be a tremendous help.

Matt Ruch
0 Kudos
deleted-user-XuRyPg9pv1OY
New Contributor III
I have been struggling with filename.split since the last post...

Why does the following Expression and Codeblock return "G16"? When I try return z[0], it returns the path and I want to get "S12."

newbase("S12_G16_V00_ULABSH10_00.shp")

def newbase(filename):
  import os
  y = os.path.split(filename)
  z = filename.split('_')
  return z[1]

Thanks!

Matt Ruch
0 Kudos
DaleHoneycutt
Regular Contributor
I tried this in a Python interactive window and it worked:
>>> import os
>>> newbase = r"C:\xxx\yyy\S12_G16_V00_ULABSH10_00.shp"
>>> y = os.path.split(newbase)
>>> print y
('C:\\xxx\\yyy', 'S12_G16_V00_ULABSH10_00.shp')
>>> print y[0]
C:\xxx\yyy
>>> print y[1]
S12_G16_V00_ULABSH10_00.shp
>>> z = y[1].split('_')
>>> print z
['S12', 'G16', 'V00', 'ULABSH10', '00.shp']
>>> print z[0]
S12
>>>


I think your issue is that os.path.filename returns two values?
0 Kudos