Solved

Reading in Detector Data to e.g. python

  • 7 October 2021
  • 3 replies
  • 882 views

I use Detectorviewers in text mode to export the results of my simulations to disk.

Futher processing is done via python, where the plain textfile gets parsed.

I prefer to not using  the ZOS API as this limits me in the python verison i use.

 

Problem is: The Detector Viewer numbers are really error-prone as sometimes i close a window and open another one and the numbers i have to put into SAVEWINDOW keyword are just wrong by then.

I would prefer using the detector number instead using SAVEDETECTOR keyword. But this saves in some binary format i did not find any specs upon for reading that in.

 

Is there any description on the DDR/DDx file format?

Is there any way to obtaining the detector data into python?

 

Best regards

Olli

 

icon

Best answer by Ray 7 October 2021, 11:12

View original

3 replies

Userlevel 4
Badge

You can find the description of the DDR/DDC/DDV formats in the help, in a section titled  “Saving and Loading Detector Data”.

I would advise staying away from the text format if you can, the binary versions have more information and are more reliable. The time spent in writing an importer is worth it in the long run. I don’t know if there is one in python (check pyZDDE perhaps). You may also be able to access the data via the API.

Hi,

I made a quick and dirty script to read a DDR file in python 3.9. Please find it below:

--------------------------------------------------------------------

# imports
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import struct

filename = "tube_output2D.DDR"

def get_integer(file):
    return struct.unpack('I', file.read(4))[0]
def get_float(file):
    return struct.unpack('d', file.read(8))[0]


fmt_d_data = 'ddddd'
struct_len = struct.calcsize(fmt_d_data)
struct_unpack = struct.Struct(fmt_d_data).unpack_from
inc_int_pos, inc_int_ang, coh_real, coh_imag, coh_amp = [],[],[],[],[]

with open(filename,"rb") as file:
    # unpack little endian integers
    version = get_integer(file)
    type_det = get_integer(file)
    lens_units = get_integer(file)
    source_units = get_integer(file)
    source_multiplier = get_integer(file)
    print(f'{version=}')
    print(f'{type_det=}')

    i_data_len = 50 
    i_data = []
    for _ in range(i_data_len):
        i_data.append(get_integer(file))   

    x_pixels =i_data[0]
    y_pixels = i_data[1]
    n_rays_spatial_detector = i_data[2]
    n_rays_angular_detector = i_data[3]
    print(f'{x_pixels=}')
    print(f'{y_pixels=}')
    print(f'{n_rays_spatial_detector=}')
    print(f'{n_rays_angular_detector=}')
    print(f'{i_data=}')
    #  there is a gap of 4 bytes that appears between 
    # the last int member (i_data) and the first double member (d_data)
    gap = get_integer(file)   
    
    d_data = []
    for _ in range(i_data_len):
        d_data.append(get_float(file))   
    print(f'{d_data=}')   

    ray_trace_method = struct.unpack('I', file.read(4))[0]
    print(f'{ray_trace_method=}')
    while True:
        data = file.read(struct_len)
        if not data: break
        s = struct_unpack(data)
        inc_int_pos.append(s[0])
        inc_int_ang.append(s[1])
        coh_real.append(s[2])
        coh_imag.append(s[3])
        coh_amp.append(s[4])        

Userlevel 4
Badge +1

Hello Oliver,

You can refer the Python code for reading DDR File by Victor . This works fine which gives the these information.Thanks Oliver for your Python code . 

x_pixels=120
y_pixels=120
n_rays_spatial_detector=4748226
n_rays_angular_detector=4748226

 

You can also refer example 8 in our ZOS API Synatx help file to save the DDR and read each pixel flux of rectangle Detector 

 

Thanks

Sahil

Reply