File indexing completed on 2026-04-09 07:48:46
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 import sys, os, numpy as np, logging, argparse
0023 log = logging.getLogger(__name__)
0024 tx_load = lambda _:list(map(str.strip, open(_).readlines()))
0025
0026 from opticks.ana.key import keydir
0027 KEYDIR = keydir()
0028
0029 class BLib(object):
0030 @classmethod
0031 def parse_args(cls, doc, **kwa):
0032 np.set_printoptions(suppress=True, precision=3 )
0033 parser = argparse.ArgumentParser(doc)
0034
0035 parser.add_argument( "--level", default="info", help="logging level" )
0036 parser.add_argument( "-b","--brief", action="store_true", default=False )
0037 parser.add_argument( "-n","--names", action="store_true", default=False )
0038 parser.add_argument( "-s","--selection", default="", help="comma delimited list of selected boundary indices" )
0039 args = parser.parse_args()
0040 fmt = '[%(asctime)s] p%(process)s {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s'
0041 logging.basicConfig(level=getattr(logging,args.level.upper()), format=fmt)
0042 return args
0043
0044 @classmethod
0045 def old_make(cls, path):
0046 return cls(cls.find_idpath(path))
0047
0048 @classmethod
0049 def old_find_idpath(cls, path):
0050 """
0051 Heuristically convert any absolute path inside the idpath into the idpath
0052 by looking for path element of length 32 corresponding to the digest string.
0053 """
0054 elem = path.split("/")
0055 elen = list(map(len,elem))
0056 try:
0057 digp = elen.index(32)
0058 idpath = "/".join(elem[:digp+2])
0059 except ValueError:
0060 log.warning("failed to find_idpath from directory : fallback to envvar")
0061 idpath = os.environ.get("IDPATH", None)
0062 pass
0063 return idpath
0064
0065
0066 def path(self, rel):
0067 return os.path.join(self.keydir, rel)
0068
0069 def __init__(self, kd=KEYDIR):
0070 """
0071 Load boundary lib index and the GItemList text files with material and surface names
0072 """
0073 self.keydir = kd
0074 blib = np.load(self.path("GBndLib/GBndLibIndex.npy"))
0075 mlib = tx_load(self.path("GItemList/GMaterialLib.txt"))
0076 slib = tx_load(self.path("GItemList/GSurfaceLib.txt"))
0077 self.blib = blib
0078 self.mlib = mlib
0079 self.slib = slib
0080 self._selection = range(len(self.blib))
0081 def mname(self, idx):
0082 return self.mlib[idx] if idx < len(self.mlib) else ""
0083 def sname(self, idx):
0084 return self.slib[idx] if idx < len(self.slib) else ""
0085 def bname(self, idx):
0086 assert idx > -1, idx
0087 omat, osur, isur, imat = self.blib[idx]
0088 return "/".join(
0089 [ self.mname(omat),
0090 self.sname(osur),
0091 self.sname(isur),
0092 self.mname(imat) ] )
0093
0094
0095 def __repr__(self):
0096 return " nbnd %3d nmat %3d nsur %3d " % ( len(self.blib), len(self.mlib), len(self.slib))
0097
0098 def _set_selection(self, q):
0099 self._selection = list(map(int, q.split(",")))
0100 def _get_selection(self):
0101 return self._selection
0102 selection = property(_get_selection, _set_selection)
0103
0104 def __str__(self):
0105 return "\n".join([repr(self)] + list(map(lambda _:"%3d : %3d : %s " % ( _, _+1, self.bname(_)) , self.selection)))
0106 def brief(self):
0107 rng = range(len(self.blib))
0108 rng = rng[0:5] + rng[-5:]
0109 return "\n".join([repr(self)] + list(map(lambda _:"%3d : %s " % ( _, self.bname(_)) , rng )))
0110
0111 def names(self):
0112 return "\n".join(map(lambda _:self.bname(_) , self.selection ))
0113
0114 def format(self, bn):
0115 """
0116 :param bn: array of 1-based boundary indices, as obtained from eg a.bn[0]
0117 """
0118 return "\n".join(["%3d : %s" % (b, self.bname(abs(b)-1)) for b in list(filter(lambda b:b != 0, bn))])
0119
0120
0121
0122 if __name__ == '__main__':
0123
0124 args = BLib.parse_args(__doc__)
0125
0126 blib = BLib()
0127
0128 if args.selection:
0129 blib.selection = args.selection
0130 pass
0131
0132 if args.brief:
0133 print(blib.brief())
0134 elif args.names:
0135 print(blib.names())
0136 else:
0137 print(blib)
0138 pass
0139
0140
0141