Skip to content

Instantly share code, notes, and snippets.

@jarvisms
Last active May 1, 2019 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarvisms/9cf779dd904392e9769f1b0c0072604e to your computer and use it in GitHub Desktop.
Save jarvisms/9cf779dd904392e9769f1b0c0072604e to your computer and use it in GitHub Desktop.
Downloading DCS meter readings to a CSV file
import pythondcs # From https://github.com/jarvisms/pythondcs
import csv # Standard Library
from datetime import date
dcs = pythondcs.DCSSession(DCSURL,USERNAME,PASSWORD) # Credentials as needed
targetmeters = [123,456,789] # List as many IDs as you need. In this case all assumed to be registers.
with open("output.csv", "w", newline="") as outputcsv: # Adjust file as needed
csvwriter = csv.writer(outputcsv)
### Output in "Periodic Channel Export" format as needed to import into TEAM Sigma aM&T software, coloumns are:
### Channel ID, Time (HH:MM:SS), Date (DD/MM/YYYY), Reading Value, Reading Flag
for id in targetmeters:
n=0
for row in dcs.get_readings(id, start=date(2017,8,1), end=date(2019,4,30), iterator=True): # Adjust get_readings as needed
if not row["isGenerated"]: # Skip if its padding
status = 15 if row["isInterpolated"] else 4 # 4="No Error", 15="Estimated by data collector" (as understoof by TEAM Sigma)
newrow = [
f"DCS{id}", # Store the register ID in the format as needed
row["startTime"].strftime("%H:%M:%S"), # Format the time how you want
row["startTime"].strftime("%d/%m/%Y"), # Format the date how you want
row["totalValue"], # Get the reading itself
status,
]
_ = csvwriter.writerow(newrow) # write that row to the file. The _ just suppresses the bytes written output to stdout
n+=1
print(f"{n} readings were not padding and were written to the file")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment