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


Userlevel 6
Badge +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

11 replies

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,

Userlevel 6
Badge +2

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.

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

Userlevel 6
Badge +2

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

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. 

Userlevel 6
Badge +2

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.

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? 

 

Userlevel 6
Badge +2

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.

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

Userlevel 1

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?

Userlevel 6
Badge +2

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!

Reply