Skip to main content

Python Reading Writing Binary Files (ZRD, ZBF, DAT, SDF)

  • Aug 1, 2022 1:28 PM UTC
  • Mar 23, 2023 10:25 AM UTC
  • 16 replies
  • 3443 views

Michael Cheng
Zemax Staff
Forum|alt.badge.img+2

The download contains 3 Python scripts:

  • a script to read / parse a ZRD (Zemax Ray Database) file
  • a script to read / parse a Binary ZBF (Zemax Beam File) file
  • a script to read / parse a Binary DAT / SDF Source File

 

Click here to download

Date Version OpticStudio Version Comment
2022/01/01 1.0 - Creation

16 replies

Daniel
  • Single Emitter
  • Aug 1, 2022 5:22 PM UTC

Hi there,

I’m looking through the script for parsing ZRD files. I see that a lot of values are unpacked as “d” meaning a C double. When I look in the Zemax documentation for ZRD files, it says that the uncompressed ZRD is stored with 64-bit doubles but that the compressed ZRDs are stored with 32-bit floats.

Is the ZRD parsing script meant to work for compressed files?

Cheers,


Michael Cheng
Zemax Staff
Forum|alt.badge.img+2
  • Author
  • Zemax Staff
  • Aug 2, 2022 2:06 PM UTC

Hi Daniel,

Thank you for discussion. Unfortuantely, this script only works with uncompressed ZRD. The format in the compressed ZRD is IP that cannot be shared publicly.

I suggest you read the compressed ZRD using ZOS-API. You can find a good example in the Zemax installation in \Documents\Zemax\ZOS-API Sample Code\Python\PythonStandalone_05_Read_ZRD_File.py.

I hope this helps.

Please let me know if you have any questions.

Thank you.


Jimmy Zheng
  • Monochrome
  • Aug 23, 2022 5:02 PM UTC

Thanks Michael, do you happen to have the matlab version as well


Michael Cheng
Zemax Staff
Forum|alt.badge.img+2
  • Author
  • Zemax Staff
  • Aug 24, 2022 2:09 AM UTC

Hi Jimmy,

Thank you for checking!

Unfortunately I don’t have an example for MATLAB. I think the key point is you need to know how to read/write a binary file and parse it as double or string in MATLAB. I just had a quick search and can see corresponded tutorial. With this reference, I think it’s possible to convert the code in Python to MATLAB.

https://www.mathworks.com/help/matlab/ref/fread.html

I’m sorry for inconvenience. Please let me know if you have any further question. Thank you!

 

Best regards,

Michael


Jimmy Zheng
  • Monochrome
  • Aug 24, 2022 6:44 PM UTC

Hi Jimmy,

Thank you for checking!

Unfortunately I don’t have an example for MATLAB. I think the key point is you need to know how to read/write a binary file and parse it as double or string in MATLAB. I just had a quick search and can see corresponded tutorial. With this reference, I think it’s possible to convert the code in Python to MATLAB.

https://www.mathworks.com/help/matlab/ref/fread.html

I’m sorry for inconvenience. Please let me know if you have any further question. Thank you!

 

Best regards,

Michael

Cool bean, I can’t seem to be able to download the python script from your post. 


Michael Cheng
Zemax Staff
Forum|alt.badge.img+2
  • Author
  • Zemax Staff
  • Aug 25, 2022 4:31 AM UTC

Cool bean, I can’t seem to be able to download the python script from your post. 

Hi Jimmy,

Your account need to associated with an active Zemax license in order to access the download link. You can find more information in this post: 

Please do not hesitate to let me know if still have any question.

Thank you.


Jimmy Zheng
  • Monochrome
  • Aug 25, 2022 4:46 AM UTC

Cool bean, I can’t seem to be able to download the python script from your post. 

Hi Jimmy,

Your account need to associated with an active Zemax license in order to access the download link. You can find more information in this post: 

Please do not hesitate to let me know if still have any question.

Thank you.

I have an active license, do I need to open a support ticket? 

 


Michael Cheng
Zemax Staff
Forum|alt.badge.img+2
  • Author
  • Zemax Staff
  • Aug 25, 2022 7:02 AM UTC

I have an active license, do I need to open a support ticket? 

 

Hi Jimmy,

Yes, that’s a little weird if you are End User of a supoprted license but cannot use the following link.

Please feel free to send a support ticket so that we can help to see if there is any issue in your account and send the file directly to you in the meantime.

Thank you.


Jimmy Zheng
  • Monochrome
  • Aug 26, 2022 12:45 AM UTC

I have an active license, do I need to open a support ticket? 

 

Hi Jimmy,

Yes, that’s a little weird if you are End User of a supoprted license but cannot use the following link.

Please feel free to send a support ticket so that we can help to see if there is any issue in your account and send the file directly to you in the meantime.

Thank you.

Thanks Michael. Allie fixed it


Alex.Tripsas

Hi Michael,

This is great!  Thank you for making this.  However, for reading the ZBF files, it looks like there’s just header information.  Have you done any work to extract the actual E-field data?


Michael Cheng
Zemax Staff
Forum|alt.badge.img+2
  • Author
  • Zemax Staff
  • Sep 2, 2023 7:18 AM UTC

Hi Alex,

Yes, sorry confusion. Unfortunately, I didn’t made the code for reading the part of E-field matrix. However, this code example demonstrates how you can read binary data and parse them to a meaningful number in Python. Using the same command, you can follow the description in the Help file as below to read the rest of the matrix data.

The Analyze Tab (sequential ui mode) > Laser and Fibers Group > About Physical Optics Propagation > Defining the Initial Beam > File (defining the initial beam) > Zemax Beam File (ZBF) Binary Format 

Please let us know if you have any questions.

Thank you!


Kris
  • Infrared
  • Nov 1, 2024 11:52 AM UTC

I have run into very similar need for a fully-functional Matlab code @Jimmy Zheng. Furthermore, I needed a much more powerful tool for path grouping and analysis than what the native Zemax can provide. I thus came up with my own fully independent and fully operational Matlab function to do this. Needless to say, @Michael Cheng Python code served as an inspiration how to write the raw data loading part. I am delighted to share my function on https://community.zemax.com/star-module-user-group-85/matlab-script-to-group-rays-from-zrd-file-to-separate-paths-5430

Hope you find it useful folks!


jesusmunozjmj
Forum|alt.badge.img+1

Hi,

Thank you for the scripts to read .ZRD files. I was able to get them working with a test optical system from Zemax, but I ran into an issue when trying to use the dictionary with a different .zmx file from another optical system ("error: unpack requires a buffer of 8 bytes"). Are there any limitations I should be aware of, or should it work with any optical system?

Best regards,  
Jesús Muñoz


Kris
  • Infrared
  • Nov 12, 2024 2:36 PM UTC

Strictly speaking, it can only work with uncompressed .ZRD files and version 2002. Maybe there is an issue with that?


jesusmunozjmj
Forum|alt.badge.img+1

Hi!

Thank you for your help. I didn’t realize it, but the ray tracing was being done with the ZRD data compression mode on. Do you know if there’s an attribute in the `ray_trace` function in Python to generate an uncompressed version of the ZRD during tracing? For example, with polarization: `NSCRayTrace.UsePolarization = True`.

Best regards,

Jesús.


Kris
  • Infrared
  • Nov 14, 2024 11:43 AM UTC

Unfortunately, I never used the Zemax API so far, so I cannot tell. I always performed all operations manually, i.e. within Zemax environment.