Solved

Help with POP Python


Hello,

I am having some difficulty finding answers on this elsewhere online and have not found any helpful forum posts on this either so here I am. I am trying to communicate with the POP with the python ZOSAPI with the program in interactive mode. Currently I have tried this little code snippit to set the surface desired to 2 and pull the irradiance with not much luck. Python more or less yells at me with an error saying “IAS_ object has no attribute ‘EndSurface’ I am not having the best of luck finding a solution to this problem in the help document. Please Help! :)

FYI: See code snippit below

 

# initialize analysis tools
TheAnalyses = TheSystem.Analyses
POP = TheAnalyses.New_Analysis(ZOSAPI.Analysis.AnalysisIDM.PhysicalOpticsPropagation)
POP_set = POP.GetSettings()
POP_set.DataType = ZOSAPI.Analysis.PhysicalOptics.POPDataTypes.Irradiance
POP_set.EndSurface.SetSurfaceNumber(2)
POP.ApplyAndWaitForCompletion()
POP_result = POP.GetResults()
Irr = float(POP_result.DataGrids(1).Values)
 

Cheers,

Armand

icon

Best answer by MichaelH 30 May 2023, 18:20

View original

6 replies

Userlevel 6
Badge +2

Hi Armand, 

Can you check which version of PythonNET you are using?  If you’re using a version greater than 3.0, then you need to explicitly cast the GetSettings() interface:

POP_set = POP.GetSettings().__implementation__

PythonNET 3.x is "Fixed" | Zemax Community

Hi Michael,

I am using version 3.0.1 for pythonnet. I have a try with adding the __implementation__ line you mentioned and it seemed like it was able to run (assuming that you meant to explicitly type that in vs something else, sorry I am not the greatest when it comes to programming). And it was able to run the section of code until the Irr = _ line where it yells at me with a line saying “IAR_DataGrid[] object is not callable” is this still related to the same issue with pythonnet? Is it better to downgrade to the lower 2.5.x version of pythonnet? Thanks for your help! FYI, see code below.

# initialize analysis tools
TheAnalyses = TheSystem.Analyses
POP = TheAnalyses.New_Analysis(ZOSAPI.Analysis.AnalysisIDM.PhysicalOpticsPropagation)
POP_set = POP.GetSettings().__implementation__
POP_set.DataType = ZOSAPI.Analysis.PhysicalOptics.POPDataTypes.Irradiance
POP_set.Field.SetFieldNumber(1)
POP_set.EndSurface.SetSurfaceNumber(2)
POP.ApplyAndWaitForCompletion()
POP_result = POP.GetResults()
Irr = float(POP_result.DataGrids(1).Values)

Cheers,

Armand

Hi Michael,

Thanks for your help and I was finally able to solve the issue! Thought I would reply with the solution that worked for me in case anyone else gets stuck on this. (Still have the issue with “IAR_DataGrid[] object is not callable”, but one step at a time:) )

 

# initialize analysis tools
TheAnalyses = TheSystem.Analyses
POP = TheAnalyses.New_Analysis(ZOSAPI.Analysis.AnalysisIDM.PhysicalOpticsPropagation)
ws1 = POP.GetSettings()
POP_set = ZOSAPI.Analysis.PhysicalOptics.IAS_PhysicalOpticsPropagation(ws1)
POP_set.Field.SetFieldNumber(1)
POP_set.EndSurface.SetSurfaceNumber(2)
 
Cheers,
Armand
Userlevel 6
Badge +2

Hi Armand,

Please see the proper way for running the POP analysis and getting the results (data1 and data2 return the same values, just different ways of accessing the data):

import zosapi, os

# connect to the ZOSAPI
zos = zosapi.App()
TheSystem = zos.TheSystem
ZOSAPI = zos.ZOSAPI

# load a system
TheSystem.LoadFile(os.path.join(os.path.sep, TheSystem.TheApplication.SamplesDir, r'Sequential\Objectives\Double Gauss 28 degree field.zmx'), False)

# open the POP analysis window
pop = TheSystem.Analyses.New_Analysis(ZOSAPI.Analysis.AnalysisIDM.PhysicalOpticsPropagation)
# for PythonNet 3.x or greater
settings = pop.GetSettings().__implementation__
# for PythonNet 2.x
# settings = POP.GetSettings()
settings.Field.SetFieldNumber(1)
settings.EndSurface.SetSurfaceNumber(2)

# run the analysis
pop.ApplyAndWaitForCompletion()

# get the results from the analysis
results = pop.GetResults()

# this is a callable method accessed with parenthesis (0-indexed)
data1 = results.GetDataGrid(0).Values

# this is an array of data grids accessed with square brackets (0-indexed)
data2 = results.DataGrids[0].Values

# need to convert to list, not float, to change from .NET double[,] to python values
pyData1 = list(data1)
pyData2 = list(data2)

 

Userlevel 1

Hi Armand,

Please see the proper way for running the POP analysis and getting the results (data1 and data2 return the same values, just different ways of accessing the data):

import zosapi, os

# connect to the ZOSAPI
zos = zosapi.App()
TheSystem = zos.TheSystem
ZOSAPI = zos.ZOSAPI

# load a system
TheSystem.LoadFile(os.path.join(os.path.sep, TheSystem.TheApplication.SamplesDir, r'Sequential\Objectives\Double Gauss 28 degree field.zmx'), False)

# open the POP analysis window
pop = TheSystem.Analyses.New_Analysis(ZOSAPI.Analysis.AnalysisIDM.PhysicalOpticsPropagation)
# for PythonNet 3.x or greater
settings = pop.GetSettings().__implementation__
# for PythonNet 2.x
# settings = POP.GetSettings()
settings.Field.SetFieldNumber(1)
settings.EndSurface.SetSurfaceNumber(2)

# run the analysis
pop.ApplyAndWaitForCompletion()

# get the results from the analysis
results = pop.GetResults()

# this is a callable method accessed with parenthesis (0-indexed)
data1 = results.GetDataGrid(0).Values

# this is an array of data grids accessed with square brackets (0-indexed)
data2 = results.DataGrids[0].Values

# need to convert to list, not float, to change from .NET double[,] to python values
pyData1 = list(data1)
pyData2 = list(data2)

 

Hi Michael,

I tried using this code. however, when I open the POP analysis window using the same line, it automatically starts running the analysis.  Is there a way to avoid that?

 

-Alex

Userlevel 6
Badge +2

Hey Alex,

Simply replace New_Analysis with New_Analysis_SettingsFirst.

Reply