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

Fix for detectors (inlc. angle space) #104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
76 changes: 36 additions & 40 deletions pyzdde/zfileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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 @@ -655,36 +654,50 @@ def readDetectorViewerTextFile(pyz, textFileName, displayData=False):
detNumSurfNum = line_list[pyz._getFirstLineOfInterest(line_list, detNumSurfNumPat)]
detNum = int(pyz._re.search(r'\d{1,4}', detNumSurfNum.split(',')[0]).group())
nscgSurfNum = int(pyz._re.search(r'\d{1,4}', detNumSurfNum.split(',')[1]).group())
sizePixelsHitsPat = r'Size[0-9a-zA-Z\s\,\.]*Pixels[0-9a-zA-Z\s\,\.]*Total\sHits'
sizePixelsHits = line_list[pyz._getFirstLineOfInterest(line_list, sizePixelsHitsPat)]
sizeinfo, pixelsinfo , hitsinfo = sizePixelsHits.split(',')
#note: width<-->rows<-->xPix;; height<-->cols<-->yPix
width, height = [float(each) for each in pyz._re.findall(r'\d{1,4}\.\d{1,8}', sizeinfo)]
# sizePixelsHitsPat = r'Size[0-9a-zA-Z\s\,\.]*Pixels[0-9a-zA-Z\s\,\.]*Total\sHits'
# Above pattern won't work for angle space detectors
sizePixelsHitsPat = r'.*Total\sHits' # Change to also allow angle space
totalHitsLineNum = pyz._getFirstLineOfInterest(line_list, sizePixelsHitsPat)
sizePixelsHits = line_list[totalHitsLineNum]
detector_position = True # Generally in position space but...
if 'deg' in sizePixelsHits: # ...if in angle space...
detector_position = False # ...not in position space.
if detector_position: # As usual if position_space...
sizeinfo, pixelsinfo , hitsinfo = sizePixelsHits.split(',')
width, height = [float(each) for each in pyz._re.findall(r'\d{1,4}\.\d{1,8}', sizeinfo)]
else: # ...but if in angle_space...
sizeinfox, sizeinfoy, pixelsinfo, hitsinfo = sizePixelsHits.split(',') # ...needs another variable
wi, wf = [float(each) for each in pyz._re.findall(r'[-]?\d{1,4}\.\d{1,8}', sizeinfox)] # 2 numbers here
hi, hf = [float(each) for each in pyz._re.findall(r'[-]?\d{1,4}\.\d{1,8}', sizeinfoy)] # 2 numbers here
width, height = (wi, wf), (hi, hf) # Store as before, now tuples of floats instead of floats.
#note: width<-->rows<-->xPix;; height<-->cols<-->yPix
xPix, yPix = [int(each) for each in pyz._re.findall(r'\d{1,6}', pixelsinfo)]
totHits = int(pyz._re.search(r'\d{1,10}', hitsinfo).group())

#peak irradiance and total power. only present for irradiance types
peakIrr, totPow = None, None
peakIrrLineNum = pyz._getFirstLineOfInterest(line_list, 'Peak\sIrradiance')
if peakIrrLineNum:
peakIrr = float(pyz._re.search(r'\d{1,4}\.\d{3,8}[Ee][-\+]\d{3}',
line_list[peakIrrLineNum]).group())
totPow = float(pyz._re.search(r'\d{1,4}\.\d{3,8}[Ee][-\+]\d{3}',
line_list[peakIrrLineNum + 1]).group())
peakIrr, totPow = None, None
#peakIrrLineNum = pyz._getFirstLineOfInterest(line_list, 'Peak\sIrradiance')
# Attention: peakIrr breaks when using radiance -> using totalHits as reference
if totalHitsLineNum:
peakIrr = float(pyz._re.search(r'\d{1,4}\.\d{3,8}[Ee][-\+]\d{2,3}',
line_list[totalHitsLineNum + 2]).group())
totPow = float(pyz._re.search(r'\d{1,4}\.\d{3,8}[Ee][-\+]\d{2,3}',
line_list[totalHitsLineNum + 3]).group())

# section of text starting with 'Smoothing' (common to all)
smoothLineNum = pyz._getFirstLineOfInterest(line_list, 'Smoothing')
smooth = line_list[smoothLineNum].split(':')[1].strip()
smooth = 0 if smooth == 'None' else int(smooth)
dType = line_list[smoothLineNum + 1].split(':')[1].strip()
posX = float(line_list[smoothLineNum + 2].split(':')[1].strip()) # 'Detector X'
posY = float(line_list[smoothLineNum + 3].split(':')[1].strip()) # 'Detector Y'
posZ = float(line_list[smoothLineNum + 4].split(':')[1].strip()) # 'Detector Z'
tiltX = float(line_list[smoothLineNum + 5].split(':')[1].strip())
tiltY = float(line_list[smoothLineNum + 6].split(':')[1].strip())
tiltZ = float(line_list[smoothLineNum + 7].split(':')[1].strip())
posUnits = line_list[smoothLineNum + 8].split(':')[1].strip()
units = line_list[smoothLineNum + 9].split(':')[1].strip()
dtypeLineNum = pyz._getFirstLineOfInterest(line_list, 'Data Type')
dType = line_list[dtypeLineNum].split(':')[1].strip()
posX = float(line_list[dtypeLineNum + 1].split(':')[1].strip()) # 'Detector X'
posY = float(line_list[dtypeLineNum + 2].split(':')[1].strip()) # 'Detector Y'
posZ = float(line_list[dtypeLineNum + 3].split(':')[1].strip()) # 'Detector Z'
tiltX = float(line_list[dtypeLineNum + 4].split(':')[1].strip())
tiltY = float(line_list[dtypeLineNum + 5].split(':')[1].strip())
tiltZ = float(line_list[dtypeLineNum + 6].split(':')[1].strip())
posUnits = line_list[dtypeLineNum + 7].split(':')[1].strip()
units = line_list[dtypeLineNum + 8].split(':')[1].strip()

# determine "showAs" type
rowPat = r'Row\s[0-9A-Za-z]*,\s*Y'
Expand Down Expand Up @@ -718,7 +731,7 @@ def readDetectorViewerTextFile(pyz, textFileName, displayData=False):
# note: it is still possible to 1d data here if `showAsRowCol` was corrupted
rowOrCol, rowColNum, rowColVal = None, None, None # meta-data not available for 2D data
if displayData:
dataPat = (r'\s*\d{1,4}\s*(-?\d{1,4}\.\d{3,8}([Ee][-\+]\d{3,8})*\s*)'
dataPat = (r'\s*\d{1,4}\s*(-?\d{1,4}\.\d{3,8}([Ee][-\+]\d{2,8})*\s*)'
+ r'{{{num}}}'.format(num=xPix))
start_line = pyz._getFirstLineOfInterest(line_list, dataPat)
gridData = pyz._get2DList(line_list, start_line, yPix, startCol=1)
Expand Down Expand Up @@ -885,20 +898,3 @@ 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)