#import gpibprologix import os import json import time import csv import matplotlib.dates as mdates import pandas as pd from plotly.subplots import make_subplots import plotly.graph_objects as go import datetime import numpy import GPIBPrologix import git import shutil import bme280 import smbus2 GPIB = GPIBPrologix.ResourceManager("/dev/ttyACM0") instObj = GPIB.open_resource(22) bus = smbus2.SMBus(1) calibration_params = bme280.load_calibration_params(bus, 0x76) ## user customisable functions def readFunc(instObj): return instObj.read() def writeFunc(instObj, input): instObj.write(input) time.sleep(1) def queryFunc(instObj, input): output = instObj.query(input) time.sleep(1) print(input, output) return output def getEnvironment(instObj, i2cbus): value = instObj.sample(i2cbus, 0x76, calibration_params) return [str(round(value.humidity,2)),str(round(value.temperature,2)),str(round(value.pressure,2))] ## the main program functionalities, should not be adjusted def readRunningConfigs(): searchPath = os.path.realpath(os.path.dirname(__file__))+'/RunningConfigs/' filenames = next(os.walk(searchPath), (None, None, []))[2] filenames = [item for item in filenames if str.endswith(item,'.json')] return filenames def doACAL(dcACAL, ohmACAL, acACAL): writeFunc(instObj,"END 2") writeFunc(instObj,"OFORMAT ASCII") writeFunc(instObj,"BEEP") if dcACAL == "yes": print("Starting DCV cal") writeFunc(instObj, "ACAL DCV") time.sleep(140+60) if acACAL == "yes": print("Starting AC cal") writeFunc(instObj, "ACAL AC") time.sleep(240+60) if ohmACAL == "yes": print("Starting Ohm cal") writeFunc(instObj, "ACAL OHMS") time.sleep(720+60) writeFunc(instObj, "DISP OFF, ;") writeFunc(instObj,"DISP MSG,\" \"") return 0 def interrogate3458A(instObj): output = [] d = datetime.datetime.now() dx = d - datetime.timedelta(microseconds=d.microsecond) output.append(dx.strftime("%d-%m-%y %H:%M:%S")) output.append(queryFunc(instObj,"TEMP?")) # get temperature in device output.append(queryFunc(instObj,"CAL? 1,1")) # get RREF cal value output.append(queryFunc(instObj,"CAL? 2,1")) # get VREF cal value output.append(queryFunc(instObj,"CAL? 78")) # get 10kohm ACAL gain constrant output.append(queryFunc(instObj,"CAL? 79")) # get 100kohm ACAL gain constrant output.append(queryFunc(instObj,"CAL? 71")) # get 1v0 ACAL gain constrant output.append(queryFunc(instObj,"CAL? 70")) # get 0v1 ACAL gain constrant output.append(queryFunc(instObj,"CAL? 86")) # get 1kohm ACAL ocomp constrant output.append(queryFunc(instObj,"CAL? 87")) # get 10kohm ACAL ocomp constrant output.append(queryFunc(instObj,"CAL? 176")) # get acal temperature for ohms output.append(queryFunc(instObj,"CAL? 59")) # get temperature from during calibration output.append(queryFunc(instObj,"CAL? 97")) # get 1mamp ACAL gain constrant output.append(queryFunc(instObj,"CAL? 72")) # get 10v ACAL gain constrant print(output) return output ## main program # read the config file and do the folder setup for file in readRunningConfigs(): dataPath = os.path.realpath(os.path.dirname(__file__))+'/RunningConfigs/'+file with open(dataPath, "r") as read_file: configData = json.load(read_file) serialPath = os.path.realpath(os.path.dirname(__file__))+'/data/'+configData['serial']+'/' if not os.path.exists(serialPath): os.makedirs(serialPath) with open(serialPath+configData['serial']+'.csv', 'a') as g: heading = ["DateTime","TEMP","CAL RREF","CAL VREF","G10K","G100K", "G1V0","G0V1","OCOMP1K","OCOMP10K", "ACALTEMP", "CALTEMP", "G1mA", "G10V"] if(configData['useBME']): heading = output.append(['EnvHumidity', 'EnvTemp', 'EnvPressure']) writer = csv.writer(g) writer.writerow(heading) doACAL(configData['ACAL-DCV'],configData['ACAL-OHMS'],configData['ACAL-ACV']) with open(serialPath+configData['serial']+'.csv', 'a') as f: output = interrogate3458A(instObj) if(configData['useBME']): data = getEnvironment(bme280, bus) output += data writer = csv.writer(f) writer.writerow(output) ## do the plotting for file in readRunningConfigs(): dataPath = os.path.realpath(os.path.dirname(__file__))+'/RunningConfigs/'+file with open(dataPath, "r") as read_file: configData = json.load(read_file) serialPath = os.path.realpath(os.path.dirname(__file__))+'/data/'+configData['serial']+'/' df = pd.read_csv(serialPath+configData['serial']+'.csv',delimiter=',', encoding="utf-8-sig") df.columns = df.columns.str.strip() time_x = pd.to_datetime(df['DateTime'],format='%d-%m-%y %H:%M:%S') coefficients = numpy.polyfit(df['TEMP'], df['G10V'], 1, rcond=None, full=False, w=None, cov=False) polynomial = numpy.poly1d(coefficients) #polynom_estimate = numpy.polyfit(mdates.date2num(time_x),df['G10V']-polynomial(df['TEMP']), 1, rcond=None, full=False, w=None, cov=False) polynom_estimate = numpy.polyfit(mdates.date2num(time_x),df['G10V'], 1, rcond=None, full=False, w=None, cov=False) p_est = numpy.poly1d(polynom_estimate) fig = make_subplots(rows=1, cols=2) #fig.add_trace(go.Scatter(x=time_x, y=df['G10V']-polynomial(df['TEMP']),mode='lines+markers',name='time vs cal72 w tempcomp'),row=1, col=1) fig.add_trace(go.Scatter(x=time_x, y=df['G10V'],mode='lines+markers',name='time vs cal72 w tempcomp'),row=1, col=1) fig.add_trace(go.Scatter(x=time_x, y=p_est(mdates.date2num(time_x)),mode='lines',line = dict(color='gray', dash='dash'),name='Estimated 1st order'), row=1, col=1) fig.add_trace(go.Scatter(x=df['TEMP'], y=df['G10V'],mode='lines+markers',name='temp vs cal72'),row=1, col=2) fig.add_trace(go.Scatter(x=df['TEMP'], y=polynomial(df['TEMP']),mode='lines',line = dict(color='gray', dash='dash'),name='Tempco 1st order'), row=1, col=2) datenow = datetime.datetime.now() date = datenow + datetime.timedelta(days=1) timeframe = (time_x.max()-time_x.min()).total_seconds()/(3600*24) alphaday = (p_est(mdates.date2num(date))-p_est(mdates.date2num(datenow))) driftrate = round((alphaday*1000000000)/(df.loc[:, 'G10V'].mean()*timeframe),2) annotation = f'Estimated drift: {driftrate} ppb/day' fig.add_annotation(dict(showarrow=False, text=annotation, xanchor='left', xref="paper", yref="paper", x=0, y=0)) tempcorate = round(((polynomial(1)-polynomial(0))*1000000000)/df.loc[:, 'G10V'].mean(),2) annotation = f'Estimated tempco: {tempcorate} ppb/degC' fig.add_annotation(dict(showarrow=False, text=annotation, xanchor='left', xref="paper", yref="paper", x=1, y=0)) fig.write_html(serialPath+configData['serial']+'.html') fig.write_image(serialPath+configData['serial']+'.png',width=1280, height=720) ## do the saving for file in readRunningConfigs(): localDataPath = os.path.realpath(os.path.dirname(__file__))+'/data/' gitDataRepo = os.path.realpath(os.path.dirname(__file__))+'/CommitDataRepo/' with open(dataPath, "r") as read_file: configData = json.load(read_file) if(configData['useGit']): remote = f"https://{configData['gitUser']}:{configData['gitPassword']}@{configData['gitAddress']}" if os.path.exists(gitDataRepo): shutil.rmtree(gitDataRepo, ignore_errors=True) git.Repo.clone_from(remote, gitDataRepo, b=configData['gitBranch']) shutil.copytree(localDataPath, gitDataRepo+'data/',dirs_exist_ok=True) repo = git.Repo(gitDataRepo) repo.git.add(gitDataRepo) repo.index.commit("Update") repo.remotes[0].push() ## if all ok, blamk screen to save VFD queryFunc(instObj, 'DISP OFF,""')