Created
June 11, 2014 07:54
-
-
Save indranilsinharoy/6baa240dc7d04bc73947 to your computer and use it in GitHub Desktop.
An example of how the zGetPOP() function could be written.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _getStartLine(line_list, pattern): | |
"""return the line number that is the starting line | |
for the data of interest, identified by the regex | |
pattern | |
Parameters | |
---------- | |
line_list : list | |
list of lines in the file returned by ``_readLinesFromFile()`` | |
pattern : string | |
regex pattern | |
Returns | |
------- | |
line_number : integer | |
liner_number/ index in the list where the ``pattern`` first | |
matched. If no match could be found, the function returns ``None`` | |
""" | |
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 | |
number of lines to read/ number of lines which contain | |
the 2D data | |
""" | |
data = [] | |
end_Line = start_line + number_of_lines | |
for lineNum, row in enumerate(line_list): | |
if start_line <= lineNum <= end_Line: | |
data.append([float(i) for i in row.split('\t')]) | |
return data | |
def zGetPOP(self, txtFileName2Use=None, keepFile=False, configFileName=None, | |
timeout=None): | |
"""Get Physical Optics Propagation (POP) information | |
Parameters | |
---------- | |
txtFileName2Use : string, optional | |
If passed, the POP analysis file will be named such. Pass a | |
specific ``txtFileName`` if you want to dump the file into a | |
separate directory. | |
keepFile : bool, optional | |
If false (default), the prescription file will be deleted after | |
use. If true, the file will persist. | |
configFileName : string, optional | |
If passed, the POP will be called with this configuration file | |
timeout : integer, optional | |
timeout in seconds | |
Returns | |
------- | |
peakIrradiance : float | |
the peak irradiance is the maximum power per unit area at any | |
point in the beam, measured in Source Units per lens unit squared | |
totalPower : float | |
the total power, or the integral of the irradiance over the entire | |
beam | |
fiberEfficiency_system : float | |
the efficiency of power transfer through the system | |
fiberEfficiency_receiver : float | |
the efficiency of the receiving fiber | |
coupling : float | |
the total coupling efficiency, the product of the system and | |
receiver efficiencies | |
pilotSize : float | |
the size of the gaussian beam at the surface | |
pilotWaist : float | |
the waist of the gaussian beam | |
pos : float | |
relative z position of the gaussian beam | |
rayleigh : float | |
the rayleigh range of the gaussian beam | |
powerGrid : 2D list | |
a two-dimensional list of the powers in the analysis grid | |
""" | |
if txtFileName2Use != None: | |
textFileName = txtFileName2Use | |
else: | |
cd = _os.path.dirname(_os.path.realpath(__file__)) | |
textFileName = _os.path.join(cd, "popInfo.txt") | |
getTextFlag = 1 if configFileName else 0 | |
ret = self.zGetTextFile(textFileName, 'Pop', configFileName, getTextFlag) | |
assert ret == 0 | |
line_list = _readLinesFromFile(_openFile(textFileName)) | |
# Get the Grid size | |
grid_line = line_list[_getStartLine(line_list, 'Grid size')] | |
grid_x, grid_y = [int(i) for i in re.findall('\d{2,5}', grid_line)] | |
# Peak Irradiance and Total Power | |
pattern = r'-*\d\.\d{4,6}[Ee][-\+]\d{3}' # pattern for Peak Irr, Tot Power, Pilot data | |
pattern_fe = r'\d\.\d{6}' # pattern for fiber efficiency | |
peakIrradiance, totalPower = None, None | |
pi_tp_line = line_list[_getStartLine(line_list, 'Peak Irradiance')] | |
pi_info, tp_info = pi_tp_line.split(',') | |
pi = re.search(pattern, pi_info) | |
tp = re.search(pattern, tp_info) | |
if pi: | |
peakIrradiance = float(pi.group()) | |
if tp: | |
totalPower = float(tp.group()) | |
# Pilot_size, Pilot_Waist, Pos, Rayleigh | |
pilotSize, pilotWaist, pos, rayleigh = None, None, None, None | |
pilot_line = line_list[_getStartLine(line_list, 'Pilot')] | |
p_size_info, p_waist_info, p_pos_info, p_rayleigh_info = pilot_line.split(',') | |
p_size = re.search(pattern, p_size_info) | |
p_waist = re.search(pattern, p_waist_info) | |
p_pos = re.search(pattern, p_pos_info) | |
p_rayleigh = re.search(pattern, p_rayleigh_info) | |
if p_size: | |
pilotSize = float(p_size.group()) | |
if p_waist: | |
pilotWaist = float(p_waist.group()) | |
if p_pos: | |
pos = float(p_pos.group()) | |
if p_rayleigh: | |
rayleigh = float(p_rayleigh.group()) | |
# Fiber Efficiency, Coupling | |
fiberEfficiencySystem, fiberEfficiencyReceiver, coupling = None, None, None | |
efficiency_coupling_line_number = _getStartLine(line_list, 'Fiber Efficiency') | |
if efficiency_coupling_line_number: | |
efficiency_coupling_line = line_list[efficiency_coupling_line_number] | |
efs_info, fer_info, cou_info = efficiency_coupling_line.split(',') | |
fes = re.search(pattern_fe, efs_info) | |
fer = re.search(pattern_fe, fer_info) | |
cou = re.search(pattern_fe, cou_info) | |
if fes: | |
fiberEfficiencySystem = float(fes.group()) | |
if fer: | |
fiberEfficiencyReceiver = float(fer.group()) | |
if cou: | |
coupling = float(cou.group()) | |
# Get the 2D data | |
pattern = r'(-*\d\.\d{4,6}[Ee][-\+]\d{3}\s*)' + r'{{{num}}}'.format(num=grid_x) | |
start_line = _getStartLine(line_list, pattern) | |
powerGrid = _get2DList(line_list, start_line, grid_y) | |
if not keepFile: | |
_deleteFile(textFileName) | |
return (peakIrradiance, totalPower, fiberEfficiencySystem, fiberEfficiencyReceiver, | |
coupling, pilotSize, pilotWaist, pos, rayleigh, powerGrid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment