Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:49

0001 #!/usr/bin/env python
0002 """ 
0003 Using approx hc eV to nm conversion of 1240 as it seems that was done upstream, 
0004 hence using this approx value will actually be better as it should 
0005 give the measurement nm from LS group.
0006 
0007 Rather than using the more precise 1239.8418754199977 which 
0008 will give nm slightly off 
0009 """
0010 #hc_eVnm=1239.8418754199977  # h_Planck*c_light*1e12    
0011 hc_eVnm=1240.0
0012 
0013 import sys, os, numpy as np, logging, argparse, inspect
0014 log = logging.getLogger(__name__)
0015 tx_load = lambda _:list(map(str.strip, open(_).readlines())) # py3 needs the list, otherwise stays as map 
0016 
0017 from opticks.ana.key import keydir
0018 KEYDIR = keydir()
0019 
0020 class GMaterialLib(object):
0021 
0022     RINDEX = (0,0)
0023     ABSLEN = (0,1)
0024     RAYLEIGH = (0,2)
0025     REEMPROB = (0,3)
0026         
0027     GROUPVEL = (1,0)
0028     SPARE1 = (1,1) 
0029     SPARE2 = (1,2) 
0030     SPARE3 = (1,3) 
0031 
0032     @classmethod 
0033     def Qwn(cls):
0034         qwn = {}
0035         for k, v in inspect.getmembers(cls): 
0036             if not type(v) is tuple: continue
0037             qwn[k] = v 
0038         pass
0039         return qwn 
0040 
0041     def __init__(self):
0042         self.qwn = self.Qwn()
0043         names = tx_load(os.path.join(KEYDIR, "GItemList/GMaterialLib.txt"))
0044         props = np.load(os.path.join(KEYDIR, "GMaterialLib/GMaterialLib.npy"))
0045         assert len(names) == len(props) 
0046         self.names = names
0047         self.props = props
0048 
0049         # TODO:domain should be persisted in geocache, not assumed fixed
0050         nm = np.linspace(60.,820.,39)   # standard wavelength domain with increasing wavelenth (nm)
0051         ev = hc_eVnm/nm 
0052 
0053         self.nm = nm
0054         self.ev = ev
0055          
0056 
0057     def material_idx(self, mat_name):
0058         return self.names.index(mat_name)
0059 
0060     def __call__(self, spec):
0061         mat_name, qty_name = spec.split(".")
0062         return self.prop(mat_name, qty_name)
0063 
0064     def prop(self, mat_name, qty_name):
0065         """
0066         :param mat_name: material name eg LS 
0067         :param qty_name: quantity name eg RINDEX, ABSLEN, RAYLEIGH, REEMPROB, GROUPVEL 
0068         :return a: np.ndarray property as function of wavelength eg of shape (39,) 
0069         """
0070         assert mat_name in self.names
0071         midx = self.material_idx(mat_name)
0072 
0073         assert qty_name in self.qwn
0074         qgrp,qidx =  self.qwn.get(qty_name)       
0075         a = self.props[midx,qgrp,:,qidx] 
0076         return a
0077 
0078     def __repr__(self):
0079         return "GMaterialLib %d " % len(self.names) 
0080     def __str__(self):
0081         return "\n".join([repr(self)] + self.names)
0082 
0083 
0084 if __name__ == '__main__':
0085     mlib = GMaterialLib()
0086     #print(mlib)
0087 
0088     rils = mlib.prop("LS",      "RINDEX")
0089     riac = mlib.prop("Acrylic", "RINDEX")
0090     riai = mlib.prop("Air",     "RINDEX")
0091     ripy = mlib.prop("Pyrex",   "RINDEX")
0092     riwt = mlib.prop("Water",   "RINDEX")
0093 
0094     assert np.all( riai == riai[0] )   
0095 
0096     for mat_name in mlib.names:
0097         ri = mlib.prop(mat_name, "RINDEX")
0098         if np.all(ri == 1.): continue 
0099         print(mat_name)
0100         print(ri)
0101     pass
0102 
0103