# Externally Reading USRBIN Files

**URL:** <https://fluka-forum.web.cern.ch/t/externally-reading-usrbin-files/5635>\
**Category:** Scoring\
**Created:** [2 February 2024 21:03 UTC](https://fluka-forum.web.cern.ch/t/externally-reading-usrbin-files/5635 "2024-02-02T21:03:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dirk.A.Bartkoski](https://fluka-forum.web.cern.ch/user_avatar/fluka-forum.web.cern.ch/dirk.a.bartkoski/32/697_2.png) [@Dirk.A.Bartkoski](https://fluka-forum.web.cern.ch/u/Dirk.A.Bartkoski)\
**Post date:** [2 February 2024 21:03 UTC](https://fluka-forum.web.cern.ch/t/externally-reading-usrbin-files/5635/1 "2024-02-02T21:03:42Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![horvathd](https://fluka-forum.web.cern.ch/user_avatar/fluka-forum.web.cern.ch/horvathd/32/10584_2.png) [@horvathd](https://fluka-forum.web.cern.ch/u/horvathd)\
**Post date:** [5 February 2024 15:32 UTC](https://fluka-forum.web.cern.ch/t/externally-reading-usrbin-files/5635/3 "2024-02-05T15:32:04Z")

</div>

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.

```auto
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')

```

Cheers,  
David

---

<div class="post-metadata">

**Author:** ![Dirk.A.Bartkoski](https://fluka-forum.web.cern.ch/user_avatar/fluka-forum.web.cern.ch/dirk.a.bartkoski/32/697_2.png) [@Dirk.A.Bartkoski](https://fluka-forum.web.cern.ch/u/Dirk.A.Bartkoski)\
**Post date:** [7 February 2024 01:04 UTC](https://fluka-forum.web.cern.ch/t/externally-reading-usrbin-files/5635/4 "2024-02-07T01:04:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rversaci](https://fluka-forum.web.cern.ch/letter_avatar_proxy/v4/letter/r/cc9497/32.png) [@rversaci](https://fluka-forum.web.cern.ch/u/rversaci)\
**Post date:** [7 February 2024 05:59 UTC](https://fluka-forum.web.cern.ch/t/externally-reading-usrbin-files/5635/5 "2024-02-07T05:59:40Z")

</div>


