Skip to content

Commit

Permalink
Merge pull request #92 from LucVV/Functionalities
Browse files Browse the repository at this point in the history
Functionalities
  • Loading branch information
LucVV committed Jun 27, 2019
2 parents cf0820d + 06ae45e commit 379db53
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
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 @@ -9790,8 +9800,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 @@ -9801,14 +9816,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 @@ -12165,17 +12192,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)

0 comments on commit 379db53

Please sign in to comment.