I use a python script and successfully execute some commands: open a zmx file in the back ground, add/change operands in the merit function, (local) optimization and finally i save the zmx file. When I manually open that file after the python script was run, I can see that everything was executed as intened.
But I fail to use a python script to: load a merit function that simply runs a zplm to export jpg, the exported file are 0 kB “empty”.
## not shown here, but working perfectly fine: # open zmx file, adding/changing operands in merit function, # changign values in the lensdata editor, t...] # running optimization and saving file
# Now I just want to save the focal shift window and spot diagram to jpg
ExportImage.MF is: VERS 210726 ZPLM 55 0 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00
and ZPL55.zpl is: GETSYSTEMDATA 1 EXPORTJPG 1, "C:\Test\Shift" EXPORTBMP 2, "C:\Test\Spot" OPTRETURN 0
Now, the funny thing is: when I manually open the zmx file, manually open the ExportImage.MF, it instantly works as expected: the Shift.jpg and Spot.jpg appear (about 44kB) and are the exact pictures I want. However, when I load ExportImage.MF via python, the jpgs appear also instantly but are empty.
What am I missing? Has anyone of you encountered that problem as well? I already tried different winnums, different names, different position in the code. But I have no clue, because the macro workds when I manually load it.
Thanks a lot in advance and best regards Sarah
Page 1 / 1
Hi Sarah,
I haven’t tried myself but this discussion gives you a sort of answer. In summary, what you are trying to do is not possible at the moment and is listed as a feature request. It should work in Interactive Extension mode though, where OpticStudio is open in parallel (this is because the graphics libraries required to create the images are then active when they wouldn’t be if you open OpticStudio in the background). Alternatively, the workaround seems to be to run these analysis in the ZOS-API and retrieve the data, most likely in the form of a text file that you can parse, to display them yourself.
I hope this helps. Take care,
David
Hello David,
First of all: thanks for that promt reply, it helps a lot - at least I can stop trying
When I landed in that dead end the first time, I already got the focal shift txt and plotted it, I just thought that I was missing something. However, that spot diagram txt file is quite useless in terms of plotting data it contains :D But to take the PSF is a nice idea - I already found that thread but failed to run that.
I receive an error from xLength = matrixData.GetLength(0) that says: TypeError: cannot unpack non-iterable NoneType object
huygensPSF = TheSystem.Analyses.New_HuygensPsf() #works fine huygensPSF.ApplyAndWaitForCompletion() #works fine huygensResults = huygensPSF.GetResults() #works fine matrixData = huygensResults.GetDataGrid(0).Values #works fine xLength = matrixData.GetLength(0) #fails
Thank you again for the hint to the python version, currently it’s Python 3.8.8.
And here is the code that works for me: (I copied most of the comments from Allie since they are quite useful)
#Not shown here: opening file e...]
# Open the Huygen's PSF. We will use the default settings for now huygensPSF = TheSystem.Analyses.New_HuygensPsf()
# Run the analysis with the current settings and pull the results huygensPSF.ApplyAndWaitForCompletion() huygensResults = huygensPSF.GetResults()
# The results will be split into multple data structures: Header, Intensity data, Metadata # We want the intensity data. This is the same data you will see in the Text tab of the analysis matrixData = huygensResults.GetDataGrid(0).Values
# some informaton about the variable i struggeld with: # type(matrixData) --> <class 'tuple'> # print(matrixData) --> # ((0.00041687, 0.00067239, ...), # (...), # ..., # (...)) # len(matrixData) = 32 and len(matrixDatatn])=32 (for n=0...31) psf resolution is 32x32
# here are some useful specs I found via print(dir(huygensResults.GetDataGrid(0))) xPxl = len(matrixData) # resolution, x pixels --> 32 yPxl = len(matrixDatat0]) # resolution, y pixels --> 32 dx = huygensResults.GetDataGrid(0).Dx # x data spacing --> 0.43484984 dy = huygensResults.GetDataGrid(0).Dy # y data spacing --> 0.43484984 minx = huygensResults.GetDataGrid(0).MinX # minimal x value --> -6.9575 miny = huygensResults.GetDataGrid(0).MinY # minimal y value --> -6.9575
# Plot the data plt.figure() plt.imshow(matrixData, cmap='jet', extent=tminx, -minx, miny, -miny]) #I'm not sure if the scale is correct or if you have to add dx plt.xlabel("x-position n"+r'$\mu$'+"m]") plt.ylabel("y-position n"+r'$\mu$'+"m]") plt.colorbar() plt.show()