Skip to content

Commit

Permalink
improve reading .rxt files
Browse files Browse the repository at this point in the history
  • Loading branch information
scottprahl committed Mar 13, 2024
1 parent c82637d commit c18d5c0
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 166 deletions.
12 changes: 10 additions & 2 deletions iadpython/ad.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@

def stringify(form, x):
if x is None:
return 'None'
return form % x
s = 'None'
elif np.isscalar(x):
s = form % x
else:
mn = min(x)
mx = max(x)
s = form % mn
s += ' to '
s += form % mx
return s

class Sample():
"""Container class for details of a sample.
Expand Down
44 changes: 23 additions & 21 deletions iadpython/iad.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@
import scipy.optimize
import iadpython as iad

def stringify(form, x):
if x is None:
s = 'None'
elif np.isscalar(x):
s = form % x
else:
mn = min(x)
mx = max(x)
s = form % mn
s += ' to '
s += form % mx
return s


class Experiment():
"""Container class for details of an experiment."""
Expand Down Expand Up @@ -93,8 +106,14 @@ def __str__(self):
s = "---------------- Sample ---------------\n"
s += self.sample.__str__()
s += "\n--------------- Spheres ---------------\n"
if self.num_spheres == 0:
if not np.isscalar(self.num_spheres):
s += "number of spheres range (%s)\n" % stringify("%d",self.num_spheres)
elif self.num_spheres == 0:
s += "No spheres used.\n"
elif self.num_spheres == 1:
s += "A single integrating sphere was used.\n"
elif self.num_spheres == 2:
s += "Double integrating spheres were used.\n"
if self.r_sphere is not None:
s += "Reflectance Sphere--------\n"
s += self.r_sphere.__str__()
Expand All @@ -105,28 +124,11 @@ def __str__(self):
if self.include_measurements:
s += "\n------------- Measurements ------------\n"
s += " Reflection = "
if self.m_r is None:
s += "Missing\n"
elif np.isscalar(self.m_r):
s += "%.5f\n" % self.m_r
else:
s += "%s\n" % self.m_r.__str__()

s += stringify("%.5f", self.m_r) + "\n"
s += " Transmission = "
if self.m_t is None:
s += "Missing\n"
elif np.isscalar(self.m_r):
s += "%.5f\n" % self.m_t
else:
s += "%s\n" % self.m_t.__str__()

s += stringify("%.5f", self.m_t) + "\n"
s += " Unscattered Transmission = "
if self.m_u is None:
s += "Missing\n"
elif np.isscalar(self.m_r):
s += "%.5f\n" % self.m_u
else:
s += "%s\n" % self.m_u.__str__()
s += stringify("%.5f", self.m_u) + "\n"
return s

def check_measurements(self):
Expand Down
6 changes: 3 additions & 3 deletions iadpython/port.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ def hit(self):
r2 = (self.x - self.sphere.x)**2
r2 += (self.y - self.sphere.y)**2
r2 += (self.z - self.sphere.z)**2
print('cap center (%7.2f, %7.2f, %7.2f)' % (self.x,self.y,self.z))
print('pt on sph (%7.2f, %7.2f, %7.2f)' % (self.sphere.x,self.sphere.y,self.sphere.z))
print("cap distance %7.2f %7.2f"% (r2, self.chord2))
# print('cap center (%7.2f, %7.2f, %7.2f)' % (self.x,self.y,self.z))
# print('pt on sph (%7.2f, %7.2f, %7.2f)' % (self.sphere.x,self.sphere.y,self.sphere.z))
# print("cap distance %7.2f %7.2f"% (r2, self.chord2))
return r2 < self.chord2

def set_center(self, x, y, z):
Expand Down
145 changes: 108 additions & 37 deletions iadpython/rxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
>>> import iadpython
>>>
>>> filename = 'ink.rxt'
>>> exp = iadpython.read_iad_input(filename)
>>> exp = iadpython.read_rxt(filename)
>>> if exp.lambda0 is None:
>>> plt.plot(exp.m_r)
>>> else:
Expand All @@ -25,6 +25,7 @@
>>> plt.show()
"""

import os
import re
import numpy as np
import iadpython
Expand All @@ -35,10 +36,13 @@
def read_and_remove_notation(filename):
"""Read file and remove all whitespace and comments."""
s = ''

if not os.path.exists(filename):
raise ValueError('input file "%s" must end in ".rxt"' % filename)

with open(filename, encoding="utf-8") as f:
for line in f:
line = re.sub(r'#.*', '', line)
line = re.sub(r'\s', ' ', line)
line = re.sub(r'\s*#.*', '', line)
line = re.sub(r',', ' ', line)
s += line

Expand All @@ -47,10 +51,87 @@ def read_and_remove_notation(filename):

s = re.sub(r'IAD1', '', s)
s = re.sub(r'\s+', ' ', s)
s = re.sub(r'^\s+', '', s)
s = re.sub(r'\s+$', '', s)
s = s.rstrip()
s = s.lstrip()
return s

def fill_in_data(exp, data, column_letters_str):
if column_letters_str == '':
columns = int(exp.num_measures)
if data[0]>1:
columns+=1
else :
columns = len(column_letters_str)

data_in_columns = data.reshape(-1, columns)
exp.lambda0 = None
if column_letters_str == '':
col = 0
if data[0] > 1:
exp.lambda0 = data_in_columns[:,0]
col = 1
exp.m_r = data_in_columns[:,col]
if exp.num_measures >=2:
exp.m_t = data_in_columns[:,col+1]
if exp.num_measures >=3:
exp.m_u = data_in_columns[:,col+2]
if exp.num_measures >=4:
exp.r_sphere.r_wall = data_in_columns[:,col+3]
if exp.num_measures >=5:
exp.t_sphere.r_wall = data_in_columns[:,col+4]
if exp.num_measures >=6:
exp.r_sphere.r_std = data_in_columns[:,col+5]
if exp.num_measures >=7:
exp.t_sphere.r_std = data_in_columns[:,col+6]
if exp.num_measures >7:
raise ValueError('unimplemented')
return

for col, letter in enumerate(column_letters_str):
if letter=='a':
exp.default_a = data_in_columns[:,col]
elif letter=='b':
exp.default_b = data_in_columns[:,col]
elif letter=='B':
exp.d_beam = data_in_columns[:,col]
elif letter=='c':
exp.fraction_of_rc_in_mr = data_in_columns[:,col]
elif letter=='C':
exp.fraction_of_tc_in_mt = data_in_columns[:,col]
elif letter=='d':
exp.sample.d = data_in_columns[:,col]
elif letter=='D':
exp.sample.d_above = data_in_columns[:,col]
exp.sample.d_below = data_in_columns[:,col]
elif letter=='e':
exp.error = data_in_columns[:,col]
elif letter=='g':
exp.default_g = data_in_columns[:,col]
elif letter=='t':
exp.m_t = data_in_columns[:,col]
elif letter=='L':
exp.lambda0 = data_in_columns[:,col]
elif letter=='n':
exp.n = data_in_columns[:,col]
elif letter=='N':
exp.n_above = data_in_columns[:,col]
exp.n_below = data_in_columns[:,col]
elif letter=='r':
exp.m_r = data_in_columns[:,col]
elif letter=='R':
exp.r_sphere.r_std = data_in_columns[:,col]
elif letter=='S':
exp.num_spheres = data_in_columns[:,col]
elif letter=='T':
exp.t_sphere.r_rstd = data_in_columns[:,col]
elif letter=='u':
exp.m_u = data_in_columns[:,col]
elif letter=='w':
exp.r_sphere.r_wall = data_in_columns[:,col]
elif letter=='W':
exp.t_sphere.r_wall = data_in_columns[:,col]
else:
raise ValueError('unimplemented column type "%s"' % letter)

def read_rxt(filename):
"""Read an IAD input file in .rxt format.
Expand All @@ -62,7 +143,14 @@ def read_rxt(filename):
Experiment object
"""
s = read_and_remove_notation(filename)
x = np.array([float(value) for value in s.split(' ')])
x = s.split(' ')

# Remove single-letter entries and save them
column_letters = [item for item in x if len(item) == 1 and item.isalpha()]
x = [item for item in x if not (len(item) == 1 and item.isalpha())]
column_letters_str = ''.join(column_letters)

x = np.array([float(item) for item in x])

sample = iadpython.Sample(a=None, b=None, g=None)
sample.n = x[0]
Expand Down Expand Up @@ -93,35 +181,18 @@ def read_rxt(filename):
if exp.num_spheres > 0:
exp.t_sphere = iadpython.Sphere(x[12], x[13], x[14], x[15], 0, x[16])

exp.num_measures = x[17]

exp.lambda0 = np.zeros(0)
if exp.num_measures >= 1:
exp.m_r = np.zeros(0)

if exp.num_measures >= 2:
exp.m_t = np.zeros(0)

if exp.num_measures >= 3:
exp.m_u = np.zeros(0)

count_per_line = 1
for i in range(18, len(x)):

if x[i] > 1:
exp.lambda0 = np.append(exp.lambda0, x[i])
continue
if count_per_line == 1:
exp.m_r = np.append(exp.m_r, x[i])
elif count_per_line == 2:
exp.m_t = np.append(exp.m_t, x[i])
elif count_per_line == 3:
exp.m_u = np.append(exp.m_u, x[i])
if count_per_line >= exp.num_measures:
count_per_line = 1
else:
count_per_line += 1

if len(exp.lambda0) == 0:
exp.lambda0 = None
exp.num_measures = 0
if column_letters_str == '':
exp.num_measures = x[17]
data = x[18:]
else:
if 'r' in column_letters_str:
exp.num_measures += 1
if 't' in column_letters_str:
exp.num_measures += 1
if 'u' in column_letters_str:
exp.num_measures += 1
data = x[17:]

fill_in_data(exp, data, column_letters_str)
return exp
Loading

0 comments on commit c18d5c0

Please sign in to comment.