xzos/PyZDDE

problem with start_line in _get2DList

cyriluk opened this issue · 7 comments

Hi,

I am trying to get the FFT PSF array from Zemax 16 with a simple code in python. The code is :

import sys, numpy
import os

PyZDDEPath = '..\PyZDDE'

if PyZDDEPath not in sys.path:
sys.path.append(PyZDDEPath)
import pyzdde.zdde as pyz

file path

zDir = 'my path is here'
zmxfile = 'test.zmx'
filename = os.path.join(os.path.expanduser('~'), zDir, zmxfile)

pyz.setTextEncoding(1)
ln = pyz.createLink()
ln.zLoadFile(filename)

psfInfo, psfData = ln.zGetPSF()
print psfInfo

pyz.closeLink()

test.zmx is a simple paraxial lens, and I am trying to read the psf info.
I get the following error message:

X:\My Documents...>python zemaxGetPsf.py
TXT encoding is UNICODE; no change required
Traceback (most recent call last):
File "zemaxGetPsf.py", line 20, in
psfInfo, psfData = ln.zGetPSF()
File "..\PyZDDE\pyzdde\zdde.py", line 7241, in zGetPSF
psfGridData = _get2DList(line_list, start_line, img_grid_y)
File "..\PyZDDE\pyzdde\zdde.py", line 10329, in _get2DList
end_line = start_line + number_of_lines - 1
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Exception TypeError: "'NoneType' object is not callable" in <bound method PyZDDE
.del of PyZDDE(appName='ZEMAX', appNum=1, connection=True, macroPath=None)>
ignored

I have been through the list of reported issues but haven't found a solution to this problem.
Do you see what can be the issue? Thank you for the python package by the way, that is very good!
Best regards,
Cyril

Hi Cyril,
Can you send me the scripts (either send an email or use Gist (https://gist.github.com/) so that I can execute it here and see what is the problem. The function zGetPSF() internally uses zGetTextFile() to dump the PSF data from Zemax to a directory. If a filename is also passed along with zGetPSF() then the directory is derived from the passed filename, else the directory used is the location of the zemax lens file. It seems that for some reason zGetPSF() is unable to "read" the file dumped by zGetTextFile(). There could be several reasons for it: (1) for some reason, zGetTextFile() is not working as expected, and hence it is not dumping any file at all, (2) the txt file is probably there, but the expected encoding doesn't match the encoding set in Zemax, etc. .... and a few other reasons that I don't remember exactly right now. .... anyways, please try and pass a specific filename (with complete path) to zGetPSF(). Also, use keepFile=True to see what the function zGetTextFile() is doing.... that can give some hint.

Hi Indranil,
Thank you for the detailed explanation. I confirm that the txt file is created at the directory of the Zemax file. I have added my script here : https://gist.github.com/cyriluk.
I have also uploaded the zemax file called in my script.
test.zip
Thank you in advance for the help!

Hi Cyril,

Can you also upload the text file dumped?

FFTPsfAnalysisFile.txt
Here is the .txt file generated in the Zemax directory.

Hi Cyril,

I am sorry that you had to wait. I have been quite busy lately. Anyway, I think I have found the problem. I think that it (the problem) is due to a minor (format) change in the way the new version of Zemax (OpticStudio 16) saves the data. Since I have been using Zemax 15, I didn't see it earlier. I might have to update PyZDDE in few other places too. But for now, if you make the following changes (2 lines), to the method zGetPSF() in the file zdde.py, your current problem should be resolved:

Line # 7828 (or very close depending on your version)

Change the regex expression

from

ctr_ref_x, ctr_ref_y = [float(i) for i in _re.findall('-?\d\.\d{4,10}[Ee][-\+]\d{2}', ctr_ref_line)]

to

ctr_ref_x, ctr_ref_y = [float(i) for i in _re.findall('-?\d\.\d{4,10}[Ee][-\+]\d{2,3}', ctr_ref_line)]

and then,

Line #7837

from

pat = (r'(-?\d\.\d{4,6}[Ee][-\+]\d{2}\s*)' + r'{{{num}}}'

to

pat = (r'(-?\d\.\d{4,6}[Ee][-\+]\d{2,3}\s*)' + r'{{{num}}}'

Please do let me know if that fixed the problem. Once I hear back from you, I will modify other relevant places in the code and upload to Github.

Thank you for your patience.

Regards,
Indranil.

Hi Indranil,

Thank you, these 2 changes have resolved the issue. The zGetPSF() method is working perfectly now.

Best wishes,
Cyril

Hi Cyril,

Thanks for letting me know. Glad to know that the above suggestion fixed the problem. I was just looking at other issues and found that @B2015 (Ben) had actually reported (and suggested the same regex pattern as above) this issue (see #66).

I am reopening the issue so that it reminds me to put the fix into the main pyzdde branch.

Best regards,
Indranil.