File indexing completed on 2026-04-09 07:48:47
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 """
0022 """
0023
0024 import numpy as np
0025 import os, logging
0026 from opticks.ana.bpath import BPath
0027 from opticks.ana.key import Key
0028
0029 log = logging.getLogger(__name__)
0030
0031
0032 def _dirname(path, n):
0033 for _ in range(n):
0034 path = os.path.dirname(path)
0035 return path
0036
0037
0038 def opticks_environment(ok):
0039 log.debug(" ( opticks_environment")
0040 env = OpticksEnv(ok)
0041 if ok.dumpenv:
0042 env.dump()
0043 pass
0044
0045 log.debug(" ) opticks_environment")
0046
0047
0048 class OpticksEnv(object):
0049 """
0050 TODO: dependency on IDPATH when loading evt is dodgy as its making
0051 the assumption that the geocache that IDPATH points at matches
0052 the geocache of the loaded evt...
0053
0054 this is true for test geometries too, as they still have a basis geocache
0055
0056 FIX: by recording the geocache dir with the evt (is probably already is ?)
0057 and be match asserted upon on load, dont just rely upon it
0058
0059 """
0060 def _idfilename(self):
0061 """
0062 layout 0
0063 /usr/local/opticks/opticksdata/export/juno1707/g4_00.a181a603769c1f98ad927e7367c7aa51.dae
0064 -> g4_00.dae
0065
0066 layout > 0
0067 /usr/local/opticks/geocache/juno1707/g4_00.dae/a181a603769c1f98ad927e7367c7aa51/1
0068 -> g4_00.dae
0069
0070 """
0071 log.debug("_opticks_idfilename layout %d : idpath %s " % (self.layout, self.idpath ))
0072 if self.layout == 0:
0073 name = os.path.basename(self.idpath)
0074 elem = name.split(".")
0075 assert len(elem) == 3 , ("form of idpath inconsistent with layout ", self.layout, self.idpath)
0076 idfilename = "%s.%s" % (elem[0],elem[2])
0077 elif self.layout > 0:
0078 idfilename = os.path.basename(os.path.dirname(os.path.dirname(self.idpath)))
0079 else:
0080 assert False, (self.layout, "unexpected layout")
0081 pass
0082 log.debug("_opticks_idfilename layout %d : idpath %s -> idfilename %s " % (self.layout, self.idpath, idfilename))
0083 return idfilename
0084
0085 def _idname(self):
0086 """
0087 idpath0="/usr/local/opticks/opticksdata/export/DayaBay_VGDX_20140414-1300/g4_00.96ff965744a2f6b78c24e33c80d3a4cd.dae"
0088 idpath1="/usr/local/opticks/geocache/DayaBay_VGDX_20140414-1300/g4_00.dae/96ff965744a2f6b78c24e33c80d3a4cd/1"
0089
0090 -> 'DayaBay_VGDX_20140414-1300'
0091 """
0092 if self.layout == 0:
0093 idname = self.idpath.split("/")[-2]
0094 else:
0095 idname = self.idpath.split("/")[-4]
0096 pass
0097 return idname
0098
0099 def _detector(self):
0100 """
0101 does not make any sense in direct approach
0102 """
0103 idname = self._idname()
0104 dbeg = idname.split("_")[0]
0105 if dbeg in ["DayaBay","LingAo","Far"]:
0106 detector = "DayaBay"
0107 else:
0108 detector = dbeg
0109 pass
0110 log.debug("_opticks_detector idpath %s -> detector %s " % (self.idpath, detector))
0111 return detector
0112
0113 def _detector_dir(self):
0114 """
0115 in layout 1, this yields /usr/local/opticks/opticksdata/export/juno1707/
0116 but should be looking in IDPATH ?
0117 """
0118 detector = self._detector()
0119 return os.path.join(self.env["OPTICKS_EXPORT_DIR"], detector)
0120
0121
0122 def __init__(self, ok, legacy=False):
0123 self.ok = ok
0124 self.ext = {}
0125 self.env = {}
0126
0127
0128
0129
0130 self.direct_init()
0131
0132 self.common_init()
0133
0134
0135 def get_install_prefix_from_OKTest(self):
0136 """
0137 Determine based on location of opticks executables two levels down
0138 from install prefix
0139 """
0140 return os.path.dirname(os.path.dirname(os.popen("which OKTest").read().strip()))
0141
0142 def get_install_prefix(self):
0143 return os.environ["OPTICKS_PREFIX"]
0144
0145 def direct_init(self):
0146 """
0147 Direct approach
0148
0149 * IDPATH is not allowed as an input, it is an internal envvar only
0150
0151
0152 Hmm : this assumes the geocache dir is sibling to installcache, which
0153 is no longer true.
0154 """
0155
0156
0157 assert "OPTICKS_KEY" in os.environ, "OPTICKS_KEY envvar is required"
0158 self.key = Key(os.environ["OPTICKS_KEY"])
0159
0160 keydir = self.key.keydir
0161 assert os.path.isdir(keydir), "keydir %s is required to exist " % keydir
0162
0163
0164
0165 os.environ["GEOCACHE"] = keydir
0166
0167 log.debug("direct_init keydir %s " % keydir )
0168
0169
0170 self.install_prefix = self.get_install_prefix()
0171
0172 self.setdefault("OPTICKS_INSTALL_PREFIX", self.install_prefix)
0173 self.setdefault("OPTICKS_INSTALL_CACHE", os.path.join(self.install_prefix, "installcache"))
0174
0175
0176 def _get_TMP(self):
0177 return os.environ.get("TMP", os.path.expandvars("/tmp/$USER/opticks"))
0178 TMP = property(_get_TMP)
0179
0180 def common_init(self):
0181 self.setdefault("OPTICKS_EVENT_BASE", self.TMP )
0182 self.setdefault("TMP", self.TMP )
0183
0184 def legacy_init(self):
0185 assert "IDPATH" in os.environ, "IDPATH envvar is required, for legacy running"
0186 if "OPTICKS_KEY" in os.environ:
0187 del os.environ['OPTICKS_KEY']
0188 log.warning("legacy_init : OPTICKS_KEY envvar deleted for legacy running, unset IDPATH to use direct_init" )
0189 pass
0190
0191 IDPATH = os.environ["IDPATH"]
0192 os.environ["GEOCACHE"] = IDPATH
0193
0194 self.idpath = IDPATH
0195
0196 self.idp = BPath(IDPATH)
0197 self.srcpath = self.idp.srcpath
0198 self.layout = self.idp.layout
0199
0200 idfold = _dirname(IDPATH,1)
0201 idfilename = self._idfilename()
0202
0203 self.setdefault("OPTICKS_IDPATH", IDPATH )
0204 self.setdefault("OPTICKS_IDFOLD", idfold )
0205 self.setdefault("OPTICKS_IDFILENAME", idfilename )
0206
0207 if self.layout == 0:
0208 self.install_prefix = _dirname(IDPATH, 4)
0209 self.setdefault("OPTICKS_DAEPATH", os.path.join(idfold, idfilename))
0210 self.setdefault("OPTICKS_GDMLPATH", os.path.join(idfold, idfilename.replace(".dae",".gdml")))
0211 self.setdefault("OPTICKS_GLTFPATH", os.path.join(idfold, idfilename.replace(".dae",".gltf")))
0212 self.setdefault("OPTICKS_DATA_DIR", _dirname(IDPATH,3))
0213 self.setdefault("OPTICKS_EXPORT_DIR", _dirname(IDPATH,2))
0214 else:
0215 self.install_prefix = _dirname(IDPATH, 5)
0216 self.setdefault("OPTICKS_DAEPATH", self.srcpath)
0217 self.setdefault("OPTICKS_GDMLPATH", self.srcpath.replace(".dae",".gdml"))
0218 self.setdefault("OPTICKS_GLTFPATH", self.srcpath.replace(".dae",".gltf"))
0219 self.setdefault("OPTICKS_DATA_DIR", _dirname(self.srcpath,3))
0220 self.setdefault("OPTICKS_EXPORT_DIR", _dirname(self.srcpath,2))
0221 pass
0222
0223 self.setdefault("OPTICKS_INSTALL_PREFIX", self.install_prefix)
0224 self.setdefault("OPTICKS_INSTALL_CACHE", os.path.join(self.install_prefix, "installcache"))
0225 log.debug("install_prefix : %s " % self.install_prefix )
0226
0227 self.setdefault("OPTICKS_DETECTOR", self._detector())
0228 self.setdefault("OPTICKS_DETECTOR_DIR", self._detector_dir())
0229 self.setdefault("OPTICKS_QUERY", "")
0230
0231 def bash_export(self, path="$TMP/opticks_env.bash"):
0232 """
0233 """
0234 lines = []
0235 for k,v in self.env.items():
0236 line = "export %s=%s " % (k,v)
0237 lines.append(line)
0238
0239 path = self.ok.resolve(path)
0240
0241 dir_ = os.path.dirname(path)
0242 if not os.path.isdir(dir_):
0243 os.makedirs(dir_)
0244
0245
0246 open(path,"w").write("\n".join(lines))
0247
0248
0249 def setdefault(self, k, v):
0250 """
0251 If the key is already in the environment does not
0252 change it, just records into ext
0253 """
0254 self.env[k] = v
0255 if k in os.environ:
0256 self.ext[k] = os.environ[k]
0257 else:
0258 os.environ[k] = v
0259
0260 def dump(self, st=("OPTICKS","IDPATH")):
0261 for k,v in os.environ.items():
0262 if k.startswith(st):
0263 if k in self.ext:
0264 msg = "(ext)"
0265 else:
0266 msg = " "
0267 pass
0268 log.info(" %5s%30s : %s " % (msg,k,v))
0269
0270
0271
0272 if __name__ == '__main__':
0273 logging.basicConfig(level=logging.INFO)
0274 opticks_environment(dump=True)
0275
0276
0277