Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functionalities #92

Merged
merged 6 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions pyzdde/zdde.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,17 @@ def apr(self):
def apr(self, val):
self._apr = val


@property
def connection(self):
"""Checks status of connection

Returns
-------
status: bool
True = connection online
False = connection offline
"""
return self._connection

# ZEMAX <--> PyZDDE client connection methods
#--------------------------------------------
Expand Down Expand Up @@ -9776,8 +9786,13 @@ def zGetZernike(self, which='fringe', settingsFile=None, txtFile=None,
"Maximum fit error"]
meta = []
for i, pat in enumerate(meta_patterns):
meta_line = line_list[_getFirstLineOfInterest(line_list, pat)]
meta.append(float(_re.search(r'\d{1,3}\.\d{4,8}', meta_line).group()))

line_index = _getFirstLineOfInterest(line_list, pat)
if line_index is not None:
meta_line = line_list[line_index]
meta.append(float(_re.search(r'\d{1,3}\.\d{4,8}', meta_line).group()))
else:
meta.append(_math.nan)

info = _co.namedtuple('zInfo', ['pToVChief', 'pToVCentroid', 'rmsToChief',
'rmsToCentroid', 'variance', 'strehl',
Expand All @@ -9787,14 +9802,26 @@ def zGetZernike(self, which='fringe', settingsFile=None, txtFile=None,
# Extract coefficients
start_line_pat = "Z\s+1\s+-?\d{1,3}\.\d{4,8}"
start_line = _getFirstLineOfInterest(line_list, start_line_pat)
coeff_pat = _re.compile("-?\d{1,3}\.\d{4,8}")
zCoeffs = [0]*(line_list_len - start_line)
if start_line is not None: # Zernikes obtained successfully
coeff_pat = _re.compile("-?\d{1,3}\.\d{4,8}")
zCoeffs = [0] * (line_list_len - start_line)

for i, line in enumerate(line_list[start_line:]):
zCoeffs[i] = float(_re.findall(coeff_pat, line)[0])

zCoeffId = _co.namedtuple('zCoeff', ['Z{}'.format(i + 1) for i in range(line_list_len - start_line)])

else: # Zernikes were not obtained
# Return maximum amount of zernike coefficients, filled with NaN
if which.lower() == 'fringe':
maxZern = 37 # maximum for the fringe zernike coefficients
else:
maxZern = 231 # maximum for standard and annular zernike coefficients

zCoeffs = [_math.nan] * maxZern

for i, line in enumerate(line_list[start_line:]):
zCoeffs[i] = float(_re.findall(coeff_pat, line)[0])
zCoeffId = _co.namedtuple('zCoeff', ['Z{}'.format(i) for i in range(1, maxZern + 1)])

zCoeffId = _co.namedtuple('zCoeff',
['Z{}'.format(i+1) for i in range(line_list_len - start_line)])
zCoeff = zCoeffId(*zCoeffs)

if not keepFile:
Expand Down Expand Up @@ -12151,17 +12178,20 @@ def _getDecodedLineFromFile(fileObj):
with fenc as f:
for line in f:
decodedLine = line.decode(unicode_type)
decodedLine = _zfu.checkDecimalSeparators(decodedLine)
yield decodedLine.rstrip()
else: # ascii
with fileObj as f:
for line in f:
if _global_pyver3: # ascii and Python 3.x
line = _zfu.checkDecimalSeparators(line)
yield line.rstrip()
else: # ascii and Python 2.x
try:
decodedLine = line.decode('raw-unicode-escape')
except:
decodedLine = line.decode('ascii', 'replace')
decodedLine = _zfu.checkDecimalSeparators(decodedLine)
yield decodedLine.rstrip()

def _readLinesFromFile(fileObj):
Expand Down
18 changes: 18 additions & 0 deletions pyzdde/zfileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from struct import unpack as _unpack
from struct import pack as _pack
import math as _math
import re as _re

import pyzdde.config as _config
_global_pyver3 = _config._global_pyver3
Expand Down Expand Up @@ -884,3 +885,20 @@ def randomGridSagFile(mu=0, sigma=1, semidia=1, nx=201, ny=201, unitflag=0,
delx, dely, unitflag, xdec, ydec,
fname, comment, fext)
return z, gridsagfile


def checkDecimalSeparators(string):
"""Replaces all comma decimals separators into points in the input string.

Parameters
----------
string: str
The input string in which the separators are to be replaced

Returns
-------
string: str
The new string with the replaced decimal separators
"""
return _re.sub(r'((?<=\d)|(?<=\A)|(?<=-)|(?<=\s)),(?=\d)', r'.', string)