I can't say my solution is the most elegant, but it has been working fine for 3+ years. The basic workflow is a yes/no button that when the user selects 'Yes' the appropriate file for the work started date and unit is searched and parsed for the values. MPC results are stored in .CSV files with a standard naming system, the code looks up the correct file and reads the data and stores it as a string.
import glob
import csv
import os, sys
import pandas as pd
from pathlib import Path #instead of glob?
# This function returns a string variable containing Varian's MPC QA results
# with the input parameters of treatment unit and test Day
# Function is called from QATrack+ (tested using version 2.9.0)
def mpc_data(unit, date_string):
#Import the unit name, return the latest Varian MPC geometry results as csv string
#unit = "H192128" = Edge 2128
# Match the QAT+ machine name with the Varian name of maching in TDS folder path.
unit_id = ['2128'] # For qatrack+
mpc_linac_id = ["H192128"]
unit_string=mpc_linac_id[unit_id.index(unit)]
# Format of date_string = '2016-11-06 18:03:00+00:00'
get_date=date_string.split(" ")[0] # remove the time from date time
# Root path to MPC data
MPC_DB_PATH = r"//YOURSERVERNAME/VA_Transfer/TDS/"
os.system(r'net use w: /del')
if not os.path.isdir("w:"):
os.system(r'NET USE w: \\YOURSERVERNAME \VA_Transfer\TDS /USER:R2\Varian YOURPASSWORD')
print("Mapped drive to w:")
# Get the latest 'Results.csv' file for the specified machine for date requested.
try:
path_match = f'w:{unit_string}/MPCChecks/*{get_date}*-GeometryCheckTemplate6xMVkV/Results.csv'
csv_file = max(glob.glob(path_match))
#check the file name if needed
print('Found File: {}'.format(csv_file))
ret_result = ''
with open(csv_file, 'r', newline='', encoding='utf-8') as csvfile:
csv_reader = csv.reader(csvfile, delimiter=',')
for row in csv_reader:
if 'MLCLeaf' not in row[0] and 'MLCBacklash' not in row[0]: # filter out this data
s = row[0].replace(u'\xb0','deg') # replace the degree symbol with 'deg'
fld = s.rpartition('/')[2]
ret_result += f'{fld},{row[1]};' # shorten the name of each data field, 1024 character limit in QAT+
except Exception as e:
print(f'Error: {e}')
ret_result = f'Err: {str(e)[0:1015]}'
finally:
ret_result += mpc_data_optional(unit, date_string, '6xFFF') #add Beam data for 6xFFF and 10xFFF if completed on date.
ret_result += mpc_data_optional(unit, date_string, '10xFFF')
r=ret_result[:-1] # remove last semicolon
print("Returning results:")
print(r)
# Delete the mapped drive (w:)
os.system(r'net use w: /del')
return r
def mpc_data_optional(unit, date_string, energy): #Get the Beam results for the optional beams if test performed that day
#Import the unit name, return the latest Varian MPC geometry results as csv string
#unit = "H192128" = Edge 2128
# Match the QAT+ machine name with the Varian name of maching in TDS folder path.
unit_id = ['2128'] # For qatrack+
mpc_linac_id = ["H192128"]
unit_string=mpc_linac_id[unit_id.index(unit)]
# Format of date_string = '2016-11-06 18:03:00+00:00'
get_date=date_string.split(" ")[0] # remove the time from date time
# Root path to MPC data
MPC_DB_PATH = r"//
YOURSERVERNAME /VA_Transfer/TDS/"
# Get the latest 'Results.csv' file for the specified machine for date requested.
try:
# MPC version 1.1:
#csv_file = (max(glob.glob(MPC_DB_PATH + unit_string + "/MPCChecks/*" + get_date + "*-" + energy + "-Beam/Results.csv")))
# MPC Verrsion 2.2 (TruBeam 2.7):
csv_file = (max(glob.glob(MPC_DB_PATH + unit_string + "/MPCChecks/*" + get_date + "*-BeamCheckTemplate" + energy + "/Results.csv")))
#check the file name if needed
ret_result = ''
with open(csv_file, 'r', newline='', encoding='utf-8') as csvfile:
csv_reader = csv.reader(csvfile, delimiter=',')
for row in csv_reader:
s=row[0].replace(u'\xb0','deg') # replace the degree symbol with 'deg'
ret_result += s.rpartition('/')[2] + '-' + energy + ',' + row[1] + ';' # shorten the name of each data field, 1024 character limit in QAT+
except:
ret_result = energy + " , not done;"
finally:
return ret_result
## To get data from the string, the easiest way is to convert to Dictionary:
#s=r.split(';')
#mpc_dict = dict(item.split(",") for item in s)
## Lookup values by entering the property in dictionary.
#print mpc_dict['IsoCenterSize [mm]'] # as example
#used for testing function outside QAT+
if __name__ == '__main__':
unit = sys.argv[1]
date_string = sys.argv[2]
mpc_data(unit,date_string)
This should at least get you started. Good luck!