Externally Reading USRBIN Files

I would like to take the output of a cartesian binned dose from USRBIN and send the binary .bnn file to a different program to use as a dose map.

This requires me to read the binary .bnn and extract the binned data and reconstruct a 3 dimensional array. I have used the usbrea exectuable to convert the file to ASCII just to see what it would look like (I have also used the FLAIR ASCII converter as well).

I have tried to go through the usbrea.f to figure out how to read the .bnn directly but I cannot quite decipher how to do it. I know there is some header information that gets read before the actual binned values but I am not sure how to correctly read out the .bnn.

Any help would be greatly appreciated.

For reference, my .bnn is binned as follows.

Cartesian binning n. 1 "Dose " , generalized particle n. 228
X coordinate: from -4.2750E+00 to 4.2750E+00 cm, 171 bins ( 5.0000E-02 cm wide)
Y coordinate: from -4.2750E+00 to 4.2750E+00 cm, 171 bins ( 5.0000E-02 cm wide)
Z coordinate: from -7.0000E+00 to 0.0000E+00 cm, 140 bins ( 5.0000E-02 cm wide)

Dear Dirk,

please find the following Python code to read a simple URSBIN binary result file. Please note, that it only works when the the .bnn fine contains data from only one scoring.

from scipy.io import FortranFile

filename = 'result.bnn'

f = FortranFile(filename, 'r')

record = f.read_record('a80', 'a32', '<f4', '<i4', '<i4', '<i4')

# Simulation info
simulation_title = record[0].item().decode("utf-8")
total_weight = record[2].item()
primary_ncase = record[3].item()
primary_mcase = record[4].item()
cycles = record[5].item()

record = f.read_record('<i4', 'a10', '<i4', '<i4',
                       '<f4', '<f4', '<i4', '<f4',
                       '<f4', '<f4', '<i4', '<f4',
                       '<f4', '<f4', '<i4', '<f4',
                       '<i4', '<f4', '<f4', '<f4')

# Scoring info
scoring_name = record[1].item().decode("utf-8")
scored_quantity = record[3].item()
xmin = record[4].item()
xmax = record[5].item()
xbins = record[6].item()
xbin_width = record[7].item()
ymin = record[8].item()
ymax = record[9].item()
ybins = record[10].item()
ybin_width = record[11].item()
zmin = record[12].item()
zmax = record[13].item()
zbins = record[14].item()
zbin_width = record[15].item()

# Bin values
value = f.read_record('<f4').reshape(xbins, ybins, zbins, order='F')

record = f.read_record('a10', '<i4')

# Relative error
error = f.read_record('<f4').reshape(xbins, ybins, zbins, order='F')



Thank you. With this I was able to read it with Python as well as figure out how to read it with MATLAB.