[code]#Log data to text file
def printToFile(fileName, fileHeader, tagPath1, tagPath2, header1, header2, startTime, endTime, intervalMins):
#Set up file parameters
filePath = ‘C:/—removed----/’ + system.date.format(endTime, ‘yyyy-MM-dd HH-mm’) + ’ ’ + fileName + ‘.txt’
fileTimestamp = ‘Report generated: ’ + system.date.format(system.date.now(), ‘EEEE, d MMMM YYYY, hh:mm:ss a’)
fileHeader = ‘’’ [client name removed]
[project name removed] Data Log
±-------------------------------------------------------------------------------+
|’’’+fileHeader.center(80)+’’’|
|’’’+fileTimestamp.center(80)+’’’|
±-------------------------------------------------------------------------------+
±-------------------±----------------------------±----------------------------+
| |’’’ + header1.center(29) +’’’|’’’ + header2.center(29) +’’’|
| Date Time | Min Max Avg | Min Max Avg |
±-------------------±----------------------------±----------------------------+
‘’’
#create file and write header
file = open(filePath, 'w+')
file.write(fileHeader)
try:
#get 'minimum' dataset
minDataSet = system.tag.queryTagHistory(
paths = [tagPath1, tagPath2],
startDate = startTime,
endDate = endTime,
aggregationMode = 'Minimum',
intervalMinutes = intervalMins,
ignoreBadQuality = False,
timeout = 10000
)
#get 'maximum' dataset
maxDataSet = system.tag.queryTagHistory(
paths = [tagPath1, tagPath2],
startDate = startTime,
endDate = endTime,
aggregationMode = 'Maximum',
intervalMinutes = intervalMins,
ignoreBadQuality = False,
timeout = 10000
)
#get 'average' dataset
avgDataSet = system.tag.queryTagHistory(
paths = [tagPath1, tagPath2],
startDate = startTime,
endDate = endTime,
aggregationMode = 'Average',
intervalMinutes = intervalMins,
ignoreBadQuality = False,
timeout = 10000
)
#for each row in the returned datasets, log the timestamp, min, max and average to the file
for row in range(avgDataSet.getRowCount()):
timestamp = system.date.format(avgDataSet.getValueAt(row, 0), 'YYYY-MM-dd HH:mm:ss')
newRow = '|' + timestamp + ' |'
for col in range(1, avgDataSet.getColumnCount()):
minVal = '%.1F' % minDataSet.getValueAt(row, col)
maxVal = '%.1F' % maxDataSet.getValueAt(row, col)
avgVal = '%.1F' % avgDataSet.getValueAt(row, col)
newRow = newRow + minVal.rjust(7) + ' ' + maxVal.rjust(7) + ' ' + avgVal.rjust(7) + ' |'
file.write(newRow + '\n')
#append fileFooter and close file
fileFooter = '''+--------------------+-----------------------------+-----------------------------+
End of Report
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Checked By (Print Name): _________________________________ *
* *
* (Signature): _________________________________ *
* *
* (Date): _________________________________ *
* *
* *
* OK DEVIATION REPORT (circle as applicable) *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *'''
file.write(fileFooter)
except:
try:
system.gui.errorBox("Error printing " + fileName + ' data log',"Error")
except:
print "Error printing " + fileName + ' data log'
file.close()
return filePath[/code]
Please excuse the flashbacks to 1989 and dot matrix printers
What has me the most confused is where it stops. Each time it’s at the exact same timestamp: I’m logging midnight to midnight at 15 minute intervals and it stops at 22:00:00. It prints the “|” character, the timestamp, the space after it, but not the following “|” character.
timestamp = system.date.format(avgDataSet.getValueAt(row, 0), 'YYYY-MM-dd HH:mm:ss')
newRow = '|' + timestamp + ' |'
…should set newRow to e.g. “|2018-11-14 22:00:00 |”. Then the following lines should add each data point. Then it should print the whole line to the text file. I’d be less confused if it printed the whole timestamp but no values - maybe there was a problem retrieving values. I’d be less confused if it printed no timestamp at all, maybe there was something up with the timestamp or the script otherwise got hung up. But to get to the line where it starts printing to the text file indicates that it must have successfully executed the newRow = timestamp and the for loop to get all of the column data - only to crash out halfway through printing it to the file, in the exact same spot each time.
And then of course, to have it fix itself a couple of minutes later is just completely odd.