Loading Grid Sag Surface data file with python

  • 21 October 2020
  • 2 replies
  • 316 views

I am writing an Interactive Extension with python (following this KB article). The python script generates a Grid Sag Surface data file (following the format and instructions in these two links: here and here). This file loads properly when manually using the Import tool in the Surface Properties drop-down, as explained in the first link. 


The goal is to automate this, and I have struggled to figure out the python code to do the import. Based on the second link, I tried the following:


  gSurf1 = TheLDE.GetSurfaceAt(5)   # Surface 5 is the grid sag


  gSurf1.ImportData.ImportDataFile(gFile)   # Load in sag data


This did not throw an error. Nor did it work. Nothing changed in the prescription. I have written several interactive extensions in the past, so I think that the basic procedure is Ok. I suspect that the problem is with the syntax of the import statement (which is intended for MATLAB). 


I searched the ZOS-API Help tool for both ImportData and ImportDataFile. There are some cryptic entries, but it is not clear how to translate them into python (or to the above MATLAB statement for that matter).


FWIW, I am using the old-style COM link, not .NET


Thanks in advance for any help.


Tom


2 replies

Userlevel 6
Badge +2

Hi Tom,


It looks like you are missing the path when you import the DAT file. Right now, your code is importing gFile where gFile = 'Sid_Grid.dat'. You need to update gFile with the full path to the file location. I used this code in your file, and surface 5 updated as expected:


 


gfile1 = 'C:\\Users\\Alexandra.Culler\\Documents\\Zemax\\Objects\\Grid Files\\SagMap.dat'

gFile = 'SagMap.dat'     # Grid sag values go here

#--- Execute preliminaries and set up useful variables 

TheConnection,TheApplication,TheSystem,TheLDE,TheAnalyses = doZemaxPreliminaries()   # Does ugly Zemax boilerplate

gSurf1 = TheLDE.GetSurfaceAt(gridSurface1) # Extract grid sag surface for mirror

...

#- Load in Grid Sag Surface

gSurf1.ImportData.ImportDataFile(gfile1)   # Load in sag data 

 


I have provided an example regarding this point in a separate follow-up article here. It's not immediately obvious in the body of the KBA, but if you open the code, you will see that filename1 is defined as:


 



 


If you download the attachments, you should be able to open the Matlab code in Notepad to view the full setup!


Let me know if you have any other questions about this!


Best,


Allie

Hi Allie,


Thanks! That worked!


The KB articles I cited always mentioned the importance of placing the file in \Grid Files, and when you load the file manually within OpticStudio, it defaults to that folder. I just assumed that the script would work the same way (or that it would throw a 'File not found' error).


Excellent support. Thanks again.


Best,


Tom


 

Reply