Last active
February 3, 2024 14:34
-
-
Save ftessier/f1840bc915dc97e0bc1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys, re, os.path, math | |
# usage | |
if (len(sys.argv) < 2): | |
print ('''\nusage:) | |
%s filenames | |
filenames space separated list of files to plot | |
examples: egs-sum-3ddose.py *.3ddose > sum.3ddose | |
(add dose for all .3ddose files and write the result to file sum.3ddose) | |
'''%(os.path.basename(sys.argv[0])) | |
sys.exit(1) | |
# simplistic command-line parsing | |
files = sys.argv[1:] # array of all files to process | |
# loop over all files | |
count = 0 | |
for file in files: | |
# open 3ddose file | |
dosefile = open(file, 'r') | |
# get voxel counts on first line | |
nx, ny, nz = map(int,dosefile.readline().split()) # number of voxels along x, y, z | |
Ng = (nx+1) + (ny+1) + (nz+1) # total number of voxel grid values (voxels+1) | |
Nd = nx*ny*nz # total number of data points | |
# get voxel grid, dose and relative errors | |
data = map(float,dosefile.read().split()) # read the rest of the file | |
xgrid = data[:nx+1] # voxel boundaries in x (nx+1 values, 0 to nx) | |
ygrid = data[nx+1:nx+1+ny+1] # voxel boundaries in y (ny+1 values, nx+1 to nx+1+ny) | |
zgrid = data[nx+1+ny+1:Ng] # voxel boundaries in z (nz+1 values, rest up to Ng-1) | |
dose = data[Ng:Nd+Ng] # flat array of Nd = nx*ny*nz dose values | |
errs = data[Nd+Ng:] # flat array of Nd = nx*ny*nz relative error values | |
del data # make sure we don't refer to data by mistake from now on | |
# close 3ddose file | |
dosefile.close() | |
# declare or check accumulation arrays of size Nd | |
if (count == 0): | |
sumNd = Nd | |
sumdose = [0] * Nd | |
sumerrs = [0] * Nd | |
else: | |
if (Nd != sumNd): | |
print ("ERROR: non-matching grid size in file %s (%d, but expected %d)") % (file, Nd, sumNd) | |
sys.exit(1) | |
sumdose = [a+b for (a,b) in zip(sumdose,dose)] | |
sumerrs = [a+b**2 for (a,b) in zip(sumerrs,errs)] | |
count += 1 | |
# compute average and its uncertainty | |
sumdose[:] = [x/count for x in sumdose] | |
sumerrs[:] = [math.sqrt(x)/count for x in sumerrs] | |
# print result in .3ddose format | |
print ("%12d%12d%12d" % (nx, ny, nz)) | |
print (" ".join(map(str,xgrid))) | |
print (" ".join(map(str,ygrid))) | |
print (" ".join(map(str,zgrid))) | |
print (" ".join(map(str,sumdose))) | |
print (" ".join(map(str,sumerrs))) |
This is because this script was written and used a long time ago under Python 2, before Python required (...)
around print
!! I have added the brackets, let me know if anything else fails.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! How this script works? When I try to use it it gives me this error:
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
Am I missing something? I'm using EGSnrc for Ubuntu