|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys, argparse |
| 4 | +from os import path |
| 5 | +import numpy as np |
| 6 | +from mctools import fluka |
| 7 | +from mctools.fluka.flair import Data |
| 8 | + |
| 9 | +def getEdges(xmin, xmax, nbins): |
| 10 | + """Return bin edges given the axis min, max and number of equidistant bins |
| 11 | +
|
| 12 | + """ |
| 13 | + |
| 14 | + dx = (xmax - xmin)/nbins |
| 15 | + x = np.arange(xmin, xmax, dx).tolist() |
| 16 | + x.append(xmax) |
| 17 | + return x |
| 18 | + |
| 19 | +def main(): |
| 20 | + """Convert usbsuw output into text. |
| 21 | +
|
| 22 | + Format: min and max edges of the bin along each axis followed by the value and its relative error. |
| 23 | + """ |
| 24 | + |
| 25 | + parser = argparse.ArgumentParser(description=main.__doc__, |
| 26 | + epilog="Homepage: https://github.com/kbat/mc-tools") |
| 27 | + parser.add_argument('usbsuw', type=str, help='usbsuw binary output') |
| 28 | + parser.add_argument('out', type=str, nargs='?', help='output ASCII file name', default="") |
| 29 | + parser.add_argument('-f', action='store_true', default=False, dest='force', help='overwrite output file') |
| 30 | + parser.add_argument('-v', '--verbose', action='store_true', default=False, dest='verbose', help='explain what is being done') |
| 31 | + |
| 32 | + args = parser.parse_args() |
| 33 | + |
| 34 | + if not path.isfile(args.usbsuw): |
| 35 | + print("usbsuw2txt: File %s does not exist." % args.usbsuw, file=sys.stderr) |
| 36 | + return 1 |
| 37 | + |
| 38 | + if args.out == "": |
| 39 | + outFileName = "%s%s" % (args.usbsuw,".txt") |
| 40 | + else: |
| 41 | + outFileName = args.out |
| 42 | + |
| 43 | + if not args.force and path.isfile(outFileName): |
| 44 | + print("usbsuw2txt: File %s already exists. Use '-f' to overwrite." % args.out, file=sys.stderr) |
| 45 | + return 1 |
| 46 | + |
| 47 | + b = Data.Usrbin() |
| 48 | + b.readHeader(args.usbsuw) |
| 49 | + |
| 50 | + ND = len(b.detector) |
| 51 | + |
| 52 | + if args.verbose: |
| 53 | + b.sayHeader() |
| 54 | + print("\n%d tallies found:" % ND) |
| 55 | + for i in range(ND): |
| 56 | + b.say(i) |
| 57 | + print("") |
| 58 | + |
| 59 | + with open(outFileName, "w") as fout: |
| 60 | + for i in range(ND): |
| 61 | + val = Data.unpackArray(b.readData(i)) |
| 62 | + err = Data.unpackArray(b.readStat(i)) |
| 63 | + det = b.detector[i] |
| 64 | + |
| 65 | + title = fluka.particle.get(det.score, "unknown") |
| 66 | + axes = "" |
| 67 | + if det.type % 10 in (0, 3, 4, 5, 6): # cartesian |
| 68 | + axes = "xmin xmax ymin ymax zmin zmax value relerr" |
| 69 | + elif det.type % 10 in (1, 7): |
| 70 | + axes = "rmin rmax phimin phimax zmin zmax value relerr" |
| 71 | + |
| 72 | + x = getEdges(det.xlow, det.xhigh, det.nx) |
| 73 | + y = getEdges(det.ylow, det.yhigh, det.ny) |
| 74 | + z = getEdges(det.zlow, det.zhigh, det.nz) |
| 75 | + |
| 76 | + print(f"# {title}", file=fout) |
| 77 | + print(f"# {axes}", file=fout) |
| 78 | + |
| 79 | + for i in range(det.nx): |
| 80 | + for j in range(det.ny): |
| 81 | + for k in range(det.nz): |
| 82 | + gbin = i + j * det.nx + k * det.nx * det.ny |
| 83 | + print(f"{x[i]:>12.5e} {x[i+1]:>12.5e} {y[j]:>12.5e} {y[j+1]:>12.5e} {z[k]:>12.5e} {z[k+1]:>12.5e} {val[gbin]:>12.5e} {err[gbin]:>.4f}", file=fout) |
| 84 | + |
| 85 | +if __name__=="__main__": |
| 86 | + sys.exit(main()) |
0 commit comments