Skip to content

Instantly share code, notes, and snippets.

@indranilsinharoy
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indranilsinharoy/808383db78c47a356afb to your computer and use it in GitHub Desktop.
Save indranilsinharoy/808383db78c47a356afb to your computer and use it in GitHub Desktop.
Here is one way of doing what I suggested in the email about the extraction of the 2D data from the POP file dumped by Zemax.
import pyzdde.zdde as pyz
import numpy as np
import matplotlib.pyplot as plt
import re
# Helper functions
def _getStartLine(line_list, pattern):
"""return the line number that is the starting line
for the data of interest, identified by the regex
pattern
"""
pat = re.compile(pattern)
for line_num, line in enumerate(line_list):
if re.match(pat, line.strip()):
return line_num
def _get2DList(line_list, start_line, number_of_lines):
"""returns a 2D list of data read
Parameters
----------
line_list : list
list of lines read from a file
start_line : integer
index of line_list
number_of_lines : integer
"""
data = []
end_line = start_line + number_of_lines
for lineNum, row in enumerate(line_list):
if start_line <= lineNum <= end_line:
data.append(row.split('\t'))
return data
# To test the helper functions
_readLinesFromFile = pyz._readLinesFromFile
_openFile = pyz._openFile
txtFileName = r'C:\PROGRAMSANDEXPERIMENTS\PYTHON\OPTICS\zemaxInteractions\POPInfo.txt' # substitute this file with your own
line_list = _readLinesFromFile(_openFile(txtFileName))
# get data in the form of 2D list
pattern = r'(-*\d\.\d{4,6}[Ee][-\+]\d{3}\s*)' + r'{{{num}}}'.format(num=128) # 128 was set in POP settings in Zemax
start_line = _getStartLine(line_list, pattern)
data = _get2DList(line_list, start_line, 128)
# plot
npdata = np.array(data, dtype=np.float32)
plt.imshow(npdata)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment