Skip to content

Commit

Permalink
mv parse_fibers to util.py
Browse files Browse the repository at this point in the history
  • Loading branch information
julienguy committed Apr 6, 2020
1 parent fdfeb7f commit 063cd85
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 47 deletions.
48 changes: 1 addition & 47 deletions bin/plot_positioner
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,7 @@ import matplotlib.pyplot as plt
import numpy as np
from astropy.table import Table
from pkg_resources import resource_filename

def parse_fibers(fiber_string) :
"""
Short func that parses a string containing a comma separated list of
integers, which can include ":" or ".." or "-" labeled ranges
Args:
fiber_string (str) : list of integers or integer ranges
Returns (array 1-D):
1D numpy array listing all of the integers given in the list,
including enumerations of ranges given.
Note: this follows python-style ranges, i,e, 1:5 or 1..5 returns 1, 2, 3, 4
"""
if fiber_string is None :
return np.array([])
else:
fiber_string = str(fiber_string)

if len(fiber_string.strip(' \t'))==0:
return np.array([])

fibers=[]


for sub in fiber_string.split(',') :
sub = sub.replace(' ','')
if sub.isdigit() :
fibers.append(int(sub))
continue

match = False
for symbol in [':','..','-']:
if not match and symbol in sub:
tmp = sub.split(symbol)
if (len(tmp) == 2) and tmp[0].isdigit() and tmp[1].isdigit() :
match = True
for f in range(int(tmp[0]),int(tmp[1])) :
fibers.append(f)

if not match:
print("parsing error. Didn't understand {}".format(sub))
sys.exit(1)

return np.array(fibers)

from desimeter.util import parse_fibers

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="""Plot FVC spots, showing residuals with metrology""")
Expand Down
51 changes: 51 additions & 0 deletions py/desimeter/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Utility functions
"""

import numpy as np

def parse_fibers(fiber_string) :
"""
Short func that parses a string containing a comma separated list of
integers, which can include ":" or ".." or "-" labeled ranges
Args:
fiber_string (str) : list of integers or integer ranges
Returns (array 1-D):
1D numpy array listing all of the integers given in the list,
including enumerations of ranges given.
Note: this follows python-style ranges, i,e, 1:5 or 1..5 returns 1, 2, 3, 4
"""
if fiber_string is None :
return np.array([])
else:
fiber_string = str(fiber_string)

if len(fiber_string.strip(' \t'))==0:
return np.array([])

fibers=[]


for sub in fiber_string.split(',') :
sub = sub.replace(' ','')
if sub.isdigit() :
fibers.append(int(sub))
continue

match = False
for symbol in [':','..','-']:
if not match and symbol in sub:
tmp = sub.split(symbol)
if (len(tmp) == 2) and tmp[0].isdigit() and tmp[1].isdigit() :
match = True
for f in range(int(tmp[0]),int(tmp[1])) :
fibers.append(f)

if not match:
print("parsing error. Didn't understand {}".format(sub))
sys.exit(1)

return np.array(fibers)

0 comments on commit 063cd85

Please sign in to comment.