import time
from datetime import date
# update the timestamp value
t_stamp = int( time.time() )
# And the ordinal date for the filename
d = date.fromtimestamp(t_stamp)
#filePath = "C:\\CSV Files\\" + d.isoformat() + "DAS_Config" + ".csv"
filePath = "C:\\CSV Files\\DAS_Config" + ".csv"
values = ""
############# Write tags to CSV file #############
#Collect the values to be sent
paths = ["[Beckhoff CTU]Devices/BOP Pressure",
"[Beckhoff CTU]Devices/Chain Front Stretch",
"[Beckhoff CTU]Devices/Casing Pressure"]
values = system.tag.readBlocking(paths)
#print values
if system.file.fileExists(filePath):
dataOut = []
# The cvs reader needs a header so lets just use column count numbers for now
#system.file.writeFile(filePath,"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39\r\n",0) # Write file with "append" flag = 1 (TRUE)
dataOut = ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39'}'"]
# Put the values into a data list dataOut
for i in range(len(values)):
dataOut.append(values[i].value)
# Add the timestamp
# dataOut.append(t_stamp)
# print dataOut
# convert to CSV format
csvData = system.dataset.toCSV(dataOut,0)
trimmed = csvData
# trim the csvData removing unnecessary characters as needed
trimmed = csvData.replace('"',"").replace("[","").replace("]","").replace(" ","").replace("'","").replace("\r","").replace("\n","").replace("{","").replace("},","\n").replace("}","")
# I only want to write rows of data (no header)
try:
system.file.writeFile(filePath,trimmed,0) # Write file with "append" flag = 0 (FALSE)
except:
system.gui.messageBox("CSV File write error " + filePath + "\r\nMake sure that the file is not being accessed externally")
else:
system.gui.messageBox("CSV File DAS_Config error\r\n" + filePath + "\r\nConfig file does not exist so create a new file")
system.file.writeFile(filePath,"DAS_Config",1) # Write file with "append" flag = 1 (TRUE)
############# Read tag data from CSV and put back into tag #############
import csv
# Prompt for CSV File
path = system.file.openFile("csv")
file = open(path)
# Read file content to reader object
csvData = csv.reader(file)
print(csvData)
# Convert file content to dataset
headers = next(csvData)
data = []
for row in csvData:
data.append(row)
ds = system.dataset.toDataSet(headers, data)
print ds
# convert tp PyDataset
pds = system.dataset.toPyDataSet(ds)
print pds
#print pds[1][26]
# copy data back into Tags ???? This is where i am having trouble figuring out how to put the data back into the tags.
for row in pds:
system.tag.writeBlocking(paths, pds)