27 lines
1.8 KiB
Python
27 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Sum timestamped external power readings at the nominal polling interval."""
|
|
import argparse, csv, datetime as dt, json
|
|
from pathlib import Path
|
|
p=argparse.ArgumentParser();p.add_argument('run');p.add_argument('power_csv');p.add_argument('--idle-watts',type=float);p.add_argument('--interval-seconds',type=float,default=1);a=p.parse_args()
|
|
run=json.loads(Path(a.run).read_text());start=run['measurement_start_ms']/1000;end=run['measurement_end_ms']/1000
|
|
points=[]
|
|
with open(a.power_csv) as f:
|
|
for r in csv.DictReader(f):
|
|
instant=dt.datetime.fromisoformat(r['timestamp'].replace('Z','+00:00'))
|
|
if instant.tzinfo is None: p.error('Power timestamps require a timezone')
|
|
watts=float(r['power_watts'])
|
|
if not 0<=watts<float('inf'):p.error('Power must be finite and nonnegative')
|
|
points.append((instant.timestamp(),watts))
|
|
if len(points)<2 or any(b[0]<=a[0] for a,b in zip(points,points[1:])):p.error('Power timestamps must be strictly increasing')
|
|
if points[0][0]>start or points[-1][0]<end:p.error('Power samples must cover the entire measurement interval')
|
|
if a.interval_seconds<=0:p.error('Interval must be positive')
|
|
measured=[power for timestamp,power in points if start<=timestamp<end]
|
|
if not measured:p.error('No power readings in measurement window')
|
|
coverage=len(measured)*a.interval_seconds
|
|
joules=sum(measured)*a.interval_seconds
|
|
result={'duration_seconds':end-start,'power_sample_count':len(measured),'nominal_sample_coverage_seconds':coverage,'method':'sum of recorded power readings multiplied by nominal interval','joules':joules,'kWh':joules/3600000,'average_watts':sum(measured)/len(measured)}
|
|
if a.idle_watts is not None:result['joules_above_idle']=joules-a.idle_watts*coverage
|
|
count=run.get('measured_http_samples',0)
|
|
if count:result['joules_per_http_request']=joules/count
|
|
print(json.dumps(result,indent=2))
|