Question

Reading ZRD files with MATLAB

  • 26 October 2022
  • 4 replies
  • 97 views

Hi team,

 

I want to be able to import Zemax ZRD binary files into MATLAB. I tried fopen and fread but had no luck. I see examples of code for reading Zemax ZBF files on the File Exchange but nothing for ZRD files.
Please help.


4 replies

Userlevel 3
Badge

Hi Priscilla,

Having written such an importer many years ago, I would not advise you to do it, at least not the way I did. Zemax 22.2 introduced a way to save the ZRD content as text tab separated value files. This makes huge files but the loading is much simpler.

This knowledge base article also offers a solution with the API and a DLL:

https://support.zemax.com/hc/en-us/articles/1500005576882-Batch-Processing-of-Ray-Trace-Data-using-ZOS-API-in-MATLAB-or-Python

If you still think you need to write such importer in Matlab, you may start from the python code below:

 

A few additional points:

  • Only uncompressed full data ZRD can be read and the actual format is described in the manual. This is not difficult per se, but having it to be fast may require some work. You do not want to use one fread per value, you want to read massive blocks (the “precision” and “skip” arguments of fread are useful for that). You may want to first do a pass to determine the number of your rays and their blocks size. Another way that may be good is using memory mapping (never tried it, but it looks like it would solve most of speed issues in reading for a hard drive).
  • There is a section in the manual documenting the ZRD format that is called “Comments on Data Structure Memory Packing”. Reading it is very important for the importer to work.
  • Rays are saved as “tree” in the ZRD format, that is each branch that has split from one ray is part of the ray. If you want to do some processing of the individual branches (for example, only the rays that have ghosted on a given surface), you will need to first split the trees into individual “single” rays. That is the most complicated part.

Hi Ray,

 

Thank you for your advice and support. I have used the sample batch ray reading .m file from the link you provided. I just changed the file name and directories in the code. However, I am getting the errors below; kindly assist on how to correct them.

 

 

Hi team,

 

Kindly assist with the above error; I am stuck so far that MATLAB seems not to understand some methods and functions in this ZRD file reading script. What to do to resolve the matter? 

 

Thank you all.

Prisila.

Userlevel 6
Badge +2

Hi Priscilla

I ran the MATLAB_ZRDLoaderFull.m file from https://support.zemax.com/hc/en-us/articles/1500005576882-Batch-Processing-of-Ray-Trace-Data-using-ZOS-API-in-MATLAB-or-Python and it worked without any problem. In the code, the dll should be left in the same folder as the Matlab file:

 

Let us know if this is what you did.

Reply