-
Notifications
You must be signed in to change notification settings - Fork 1
/
mcnp_to_csv.py
48 lines (38 loc) · 1.85 KB
/
mcnp_to_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 1. generate array of boundaries for x, y, and z
# 2. generate array of centroid (x,y,z), result, relative error
import argparse
from pathlib import Path, PurePath
def mcnp_to_csv(mcnpFile, outputFile):
# append output filename with .csv regardless of what its file extension is
outputPath = PurePath(outputFile)
output_filename = f"{outputPath.stem}.csv"
# open files
with open(mcnpFile, 'r') as mcnpFile, open(output_filename, 'w') as outputFile:
# get array of boundaries for x,y,z
# 1a. find line that has x boundaries
mcnpLine = mcnpFile.readline()
while "X direction:" not in mcnpLine:
mcnpLine = mcnpFile.readline()
# 1b. split line into all the boundaries, write to file with comma separation
# do this 3 times for the 3 axes
dims = ['x', 'y', 'z']
for dim in dims:
outputStr = ','.join(mcnpLine.strip().split()[2:]) + '\n'
outputFile.write(outputStr)
mcnpLine = mcnpFile.readline()
# advance to the centroid and results section
while "Result" not in mcnpLine:
mcnpLine = mcnpFile.readline()
# 2. write all the centroid and result info to the csv file
mcnpLine = mcnpFile.readline() # this will have the first centroid (x,y,z) and result
# iterate through all voxels
while mcnpLine:
outputStr = ','.join(mcnpLine.strip().split()[1:]) + '\n'
outputFile.write(outputStr)
mcnpLine = mcnpFile.readline()
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog = "MCNP mesh tally to .csv converter")
parser.add_argument("mcnpFile", type = str, help = "input mcnp .txt filename")
parser.add_argument("outputFile", type = str, help = "output .csv filename")
args = parser.parse_args()
mcnp_to_csv(args.mcnpFile, args.outputFile)