File indexing completed on 2026-04-10 07:49:17
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 """
0022 GPmt.py
0023 ========
0024
0025 * split off analytic PMT saving and loading into this GPmt class
0026 *
0027 * this is loaded C++ side with ggeo/GPmt
0028
0029
0030 """
0031
0032 import os, logging
0033 log = logging.getLogger(__name__)
0034 import numpy as np
0035 from opticks.ana.base import opticks_main, splitlines_
0036 from opticks.ana.nbase import Buf
0037 from gcsg import GCSG
0038
0039 class GPmt(object):
0040 atts = "boundaries materials lvnames pvnames".split()
0041
0042 def __init__(self, path, buf=None):
0043 """
0044 :param path: eg "$TMP/GPmt/0/GPmt.npy" "$IDPATH/GPmt/0/GPmt.npy"
0045 :param buf: part buffer
0046 """
0047 self.path = path
0048 self.xpath = os.path.expandvars(path)
0049 self.pdir = os.path.dirname(self.xpath)
0050 self.buf = buf
0051
0052 def txtpath(self, att):
0053 return self.xpath.replace(".npy","_%s.txt" % att)
0054
0055 def gcsgpath(self):
0056 return self.xpath.replace(".npy","_csg.npy")
0057
0058
0059 def save(self):
0060 log.info("GPmt.save buf %s to:%s attribs and gcsg in sidecars " % (repr(self.buf.shape), self.path) )
0061 if not os.path.exists(self.pdir):
0062 os.makedirs(self.pdir)
0063 pass
0064 for att in self.atts:
0065 if hasattr(self.buf, att):
0066 path = self.txtpath(att)
0067 log.info("saving %s to %s " % (att, path))
0068 with open(path,"w") as fp:
0069 fp.write("\n".join(getattr(self.buf,att)))
0070 pass
0071 pass
0072 pass
0073 if hasattr(self.buf,"gcsg"):
0074 path = self.gcsgpath()
0075 log.info("serializing gcsg to %s " % path)
0076 gcsgbuf = GCSG.serialize_list(self.buf.gcsg)
0077 if gcsgbuf is not None:
0078 log.info("saving gcsg to %s " % path)
0079
0080
0081 np.save(path, gcsgbuf)
0082 else:
0083 log.warning("gcsgbuf is None skip saving to %s " % path)
0084 pass
0085 pass
0086 np.save(self.xpath, self.buf)
0087
0088 def load(self):
0089 if not os.path.exists(self.pdir):
0090 log.fatal("no such path %s " % self.pdir)
0091 assert 0
0092
0093 a = np.load(self.xpath)
0094 self.buf = a.view(Buf)
0095
0096 for att in self.atts:
0097 path = self.txtpath(att)
0098 if os.path.exists(path):
0099 setattr(self.buf, att, splitlines_(path))
0100 else:
0101 log.warning("no such path %s " % path)
0102
0103 path = self.gcsgpath()
0104 if os.path.exists(path):
0105 self.gcsgbuf = np.load(path)
0106 else:
0107 log.warning("no such path %s " % path)
0108
0109
0110 def dump(self):
0111 print "buf %s " % repr(self.buf.shape)
0112 print "gcsgbuf %s " % repr(self.gcsgbuf.shape)
0113 for att in self.atts:
0114 tls = getattr(self.buf, att)
0115 print att, len(tls), " ".join(tls)
0116
0117
0118
0119 if __name__ == '__main__':
0120 args = opticks_main(apmtpath="$TMP/GPmt/0/GPmt.npy")
0121
0122
0123 p = GPmt(args.apmtpath)
0124 p.load()
0125 p.dump()
0126
0127
0128
0129
0130
0131