Wednesday 15 July 2015

arcpy - Passing File from Python Script to ModelBuilder tool?



I have a script that produces a text file. The name of the text file it creates is determined by the input parameters chosen by the user. The user chooses what Layer, Stress Period, and Time Step to query form a binary file of Head values, and the text file is named for that Layer, Stress Period, and Timestep, and contains the Heads for that Layer, Stress Period, and Timestep.


I have inserted the script into a model, and the model is supposed to take that output text file and Copy Rows over to another location for further processing.


I added the script to a Toolbox. I set the parameters as follows: enter image description here


The Script is here:




import sys
import struct
import numpy as np
import matplotlib.pyplot as plt
import os.path


INPUTFILE = sys.argv[1]
PPER = sys.argv[2]
PSTP = sys.argv[3]
PLAY = sys.argv[4]
TXTLOC = sys.argv[5]

fid = open(INPUTFILE,'rb')

# Get the correct Offset value for the seek command

for PER in range(1):
for STP in range(1):
for k in range(1):
ITER = struct.unpack('i', fid.read(4))
KPER = struct.unpack('i', fid.read(4))
PERTIM = struct.unpack('f', fid.read(4))
TOTIM = struct.unpack('f', fid.read(4))
ITER = int(ITER[0])
KPER = int(KPER[0])


n = 0
while(n < 16):
TEXT = struct.unpack('s', fid.read(1))
n = n + 1

COL = struct.unpack('i', fid.read(4))
ROW = struct.unpack('i', fid.read(4))
LAY = struct.unpack('i', fid.read(4))
NCOL = COL[0]
NROW = ROW[0]

NLAY = LAY[0]

PPER = int(PPER)
PSTP = int(PSTP)
PLAY = int(PLAY)

# OFFSET assumes that the header data for each layer array has 44 bytes, and each HEAD value has 4 bytes
OFFSET = (44)+(4*NCOL*NROW*((((PPER-1)*2*NLAY))+((PSTP-1)*NLAY)+(PLAY-1)))
fid.seek(OFFSET)
from array import array

HEADS = array('f')
HEADS.fromfile(fid, NROW*NCOL)

# Get the number of cells to make the nodes list for the output file
CELLS = (NCOL*NROW)
NODES = list(range(1, CELLS+1))

# Make the output file
OUTPUT = np.column_stack((HEADS, NODES))
FILEOUT = os.path.join(TXTLOC,"\Heads_L"+str(PLAY)+"_SP"+str(PPER)+"_TS"+str(PSTP)+".txt")

np.savetxt(FILEOUT, OUTPUT, fmt='%12.4E',delimiter=' ')

fid.close()

The Model in which the Script is placed takes the output text file, which should be named by the variable FILEOUT that is created in the Script and named as an Output Parameter, and first runs the Copy Rows tool on it.


When I run the Model, it finishes the Script, but fails on the first next step (which is Copy Rows), as shown in the Model run report here:




Start Time: Tuesday, November 14, 2017 4:17:02 PM
Executing (Read Values From ModFlow Heads File): ReadHeads "X:\2015 Job Files\15-104 Westlands WD\GIS\HeadsProcessing\Output\HEADS.hds" 2 2 16 "X:\2015 Job Files\15-104 Westlands WD\GIS\HeadsProcessing\OutputProcessing\GIS" "X:\2015 Job Files\15-104 Westlands WD\GIS\HEADS_ReadHeads1.txt"
Start Time: Tuesday, November 14, 2017 4:17:09 PM

Running script ReadHeads...
Completed script ReadHeads...
Succeeded at Tuesday, November 14, 2017 4:17:10 PM (Elapsed Time: 0.46 seconds)
Executing (Copy Rows): CopyRows "X:\2015 Job Files\15-104 Westlands WD\GIS\HEADS_ReadHeads1.txt" "X:\2015 Job Files\15-104 Westlands WD\GIS\amk_processing.gdb\copiedvalues" #
Start Time: Tuesday, November 14, 2017 4:17:10 PM
ERROR 999999: Error executing function.
Failed to execute (Copy Rows).
Failed at Tuesday, November 14, 2017 4:17:10 PM (Elapsed Time: 0.65 seconds)
Failed to execute (ModFlowOutputHeadsContour).
Failed at Tuesday, November 14, 2017 4:17:11 PM (Elapsed Time: 8.42 seconds)




It completes the script, without errors, but then there are a couple problems. First, obviously, it failed to Copy Rows. But, more frustratingly, it also did not name the text file the right name or save it to the right location. It should have called it the result of this operation in the Script:


FILEOUT = os.path.join(TXTLOC,"\Heads_L"+str(PLAY)+"_SP"+str(PPER)+"_TS"+str(PSTP)+".txt"). 

But instead, it called it "X:\2015 Job Files\15-104 Westlands WD\GIS\HEADS_ReadHeads1.txt". That is just the generic default output file name and save location.


Furthermore, WHen i went to that location, no such file exists - nor is there a file in the location where I wanted it saved.


How do I make the Script save the text file in the manner I desired, and then pass it to the Model Tool as an input file?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...