xzos / PyZDDE

Zemax/ OpticStudio Extension using Python
MIT License
154 stars 64 forks source link

zGetPSF #66

Closed B2015 closed 7 years ago

B2015 commented 8 years ago

Hello,

I recently gave a piece of code using PyZDDE (trying to preach the good news around) to a colleague and it looks like he's not able to make it work.

From the logs I see that the problem occurs in zGetPSF where the "assert ret == 0" is false (ret gets a value of -1 instead of 0). This value is returned by zGetTextFile. From the documentation I understand that such a value of -1 is associated with the fact that a text file could not be saved (Zemax may not have received a full path name or extention). The log seems quite self-explaining, the problem being that I can't test my colleague's issue further since he's currently not in the country.

So I was wondering whether you already experienced such an issue using zGetPSF, I'm sure it's most likely coming from something we forgot to do at setup but without the possibility to make printouts etc, its tough to solve.

Thanks much for your help,

Best regards,

Ben

indranilsinharoy commented 8 years ago

Hi Ben,

Thanks very much for spreading the word.

I have seen these type of problems a few times before (that's the reason why I decided to put the "assert ret == 0" in order to localize the error. Although I don't have an exact solution, but I have noted a few things that could potentially result in this type of problem:

  1. The first thing to check would be that the text encoding between PyZDDE and Zemax is the same. To see the text encoding set in PyZDDE use pyz.getTextEncoding(), assuming you have done import pyzdde.zdde as pyz at the beginning. To set the text encoding, use pyz.setTextEncoding().
  2. If the filename(s) of the text or (especially) the .cfg file is very long (I don't have an estimate of how long). I have seen this problem when I tried to do "image simulation", and I have documented it (under the "NOTE" section at the beginning of this IPython notebook. Using a shorter name for the .cfg file generally works OK. In order to debug, I would copy the .cfg file to some temporary directory (e.g. C:\tmp), and provide a very short names (e.g. c:\tmp\tmp.txt, and c:\tmp\<lens_filename.cfg>) to ensure that the function works. Also, I would set keepFile=True to see what file has been dumped.
  3. If step 2 works, fine then the problem could be due to long file name or due to access permission to a particular directory in Windows.
  4. If step 2 didn't work, then there could be a problem with the settings file (I understand that the likelihood of this is very small. Generally, if the .cfg file is not valid, Zemax is supposed to use the default .cfg file .... however, I have seen a problem like this when I tried to do "Image simulation". In that case, the error is generally sometime like IOError: [Errno 2] No such file or directory: ...
  5. See if it related to https://github.com/indranilsinharoy/PyZDDE/issues/55 ().

I wish Zemax would have returned a more useful error code.

I hope that helps.

Best regards, Indranil.

B2015 commented 8 years ago

Hello Indranil,

Thanks much for the help! We already checked your first point and made sure the text encoding is correct. I think that you second point may be spot on because the path he's using is quite long, I'll suggest him to put the Zemax files somewhere else and see if it works well.

By the way, I tested a script using zGetPSF on yet another colleague's computer (did I tell you about spreading the word ;-) ), and found a small issue while trying to read the 2D psf_data. Indeed, the zdde regex is looking for number where the exponent contains 3 digits while for some reasons, the text file returned by zemax/OS on my other colleague's computer has only two digits. To prevent this I would suggest transforming the current pattern:

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

into:

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

Cheers,

Ben

indranilsinharoy commented 8 years ago

Hi Ben,

Thanks very much for the suggestion on the regex pattern. I will update the code based on your suggestion. Till then I will keep this issue open.

Best regards, Indranil.