<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from larch import Group , isgroup <EOL> from larch_plugins . xrf . deadtime import calc_icr , correction_factor <EOL> from larch_plugins . xrf . roi import ROI <EOL> def isLarchMCAGroup ( grp ) : <EOL> """<STR_LIT>""" <EOL> return isgroup ( grp , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Environment : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , desc = '<STR_LIT>' , addr = '<STR_LIT>' , val = '<STR_LIT>' ) : <EOL> self . addr = addr . strip ( ) <EOL> self . val = val . strip ( ) <EOL> self . desc = desc . strip ( ) <EOL> if self . desc . startswith ( '<STR_LIT:(>' ) and self . desc . endswith ( '<STR_LIT:)>' ) : <EOL> self . desc = self . desc [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . desc , <EOL> self . val , <EOL> self . addr ) <EOL> class MCA ( Group ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , counts = None , nchans = <NUM_LIT> , start_time = '<STR_LIT>' , <EOL> offset = <NUM_LIT:0> , slope = <NUM_LIT:0> , quad = <NUM_LIT:0> , name = '<STR_LIT>' , dt_factor = <NUM_LIT:1> , <EOL> real_time = <NUM_LIT:0> , live_time = <NUM_LIT:0> , input_counts = <NUM_LIT:0> , tau = <NUM_LIT:0> , ** kws ) : <EOL> self . name = name <EOL> self . nchans = nchans <EOL> self . environ = [ ] <EOL> self . rois = [ ] <EOL> self . counts = counts <EOL> self . offset = offset <EOL> self . slope = slope <EOL> self . quad = quad <EOL> self . start_time = start_time <EOL> self . real_time = real_time <EOL> self . live_time = live_time <EOL> self . input_counts = input_counts <EOL> self . total_counts = <NUM_LIT:0.> <EOL> self . tau = tau <EOL> self . icr_calc = - <NUM_LIT:1.0> <EOL> self . bgr = None <EOL> self . dt_factor = float ( dt_factor ) <EOL> if counts is not None : <EOL> self . nchans = len ( counts ) <EOL> self . total_counts = counts . sum ( ) <EOL> self . get_energy ( ) <EOL> self . _calc_correction ( ) <EOL> Group . __init__ ( self ) <EOL> def __repr__ ( self ) : <EOL> form = "<STR_LIT>" <EOL> return form % ( self . name , self . nchans , self . total_counts , self . real_time ) <EOL> def add_roi ( self , name = '<STR_LIT>' , left = <NUM_LIT:0> , right = <NUM_LIT:0> , bgr_width = <NUM_LIT:3> , <EOL> counts = None , sort = True ) : <EOL> """<STR_LIT>""" <EOL> name = name . strip ( ) <EOL> roi = ROI ( name = name , left = left , right = right , <EOL> bgr_width = bgr_width , counts = counts ) <EOL> rnames = [ r . name . lower ( ) for r in self . rois ] <EOL> if name . lower ( ) in rnames : <EOL> iroi = rnames . index ( name . lower ( ) ) <EOL> self . rois [ iroi ] = roi <EOL> else : <EOL> self . rois . append ( roi ) <EOL> if sort : <EOL> self . rois . sort ( ) <EOL> def get_roi_counts ( self , roi , net = False ) : <EOL> """<STR_LIT>""" <EOL> thisroi = None <EOL> if isinstance ( roi , ROI ) : <EOL> thisroi = roi <EOL> elif isinstance ( roi , int ) and roi < len ( self . rois ) : <EOL> thisroi = self . rois [ roi ] <EOL> elif isinstance ( roi , ( str , unicode ) ) : <EOL> rnames = [ r . name . lower ( ) for r in self . rois ] <EOL> for iroi , nam in enumerate ( rnames ) : <EOL> if nam . startswith ( roi . lower ( ) ) : <EOL> thisroi = self . rois [ iroi ] <EOL> break <EOL> if thisroi is None : <EOL> return None <EOL> return thisroi . get_counts ( self . counts , net = net ) <EOL> def add_environ ( self , desc = '<STR_LIT>' , val = '<STR_LIT>' , addr = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if len ( desc ) > <NUM_LIT:0> and len ( val ) > <NUM_LIT:0> : <EOL> self . environ . append ( Environment ( desc = desc , val = val , addr = addr ) ) <EOL> def update_correction ( self , tau = None ) : <EOL> """<STR_LIT>""" <EOL> if tau is not None : <EOL> self . tau = tau <EOL> self . _calc_correction ( ) <EOL> def _calc_correction ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . live_time <= <NUM_LIT:0> or self . real_time <= <NUM_LIT:0> : <EOL> self . dt_factor = <NUM_LIT:1.0> <EOL> return <EOL> if self . total_counts > <NUM_LIT:0> : <EOL> ocr = self . total_counts / self . live_time <EOL> else : <EOL> ocr = None <EOL> if self . tau >= <NUM_LIT:0> : <EOL> icr = calc_icr ( ocr , self . tau ) <EOL> if icr is None : <EOL> icr = <NUM_LIT:0> <EOL> self . icr_calc = icr <EOL> elif self . input_counts > <NUM_LIT:0> : <EOL> icr = self . input_counts / self . live_time <EOL> else : <EOL> icr = ocr = None <EOL> self . dt_factor = correction_factor ( self . real_time , self . live_time , <EOL> icr = icr , ocr = ocr ) <EOL> if self . dt_factor <= <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" ) <EOL> self . dt_factor = <NUM_LIT:1.0> <EOL> def get_counts ( self , correct = True ) : <EOL> """<STR_LIT>""" <EOL> if correct : <EOL> return ( self . dt_factor * self . counts ) . astype ( np . int ) <EOL> else : <EOL> return self . counts <EOL> def get_energy ( self ) : <EOL> """<STR_LIT>""" <EOL> chans = np . arange ( self . nchans , dtype = np . float ) <EOL> self . energy = self . offset + chans * ( self . slope + chans * self . quad ) <EOL> return self . energy <EOL> def save_columnfile ( self , filename , headerlines = None ) : <EOL> "<STR_LIT>" <EOL> f = open ( filename , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" % self . name ) <EOL> if headerlines is not None : <EOL> for i in headerlines : <EOL> f . write ( "<STR_LIT>" % i ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" % self . offset ) <EOL> f . write ( "<STR_LIT>" % self . slope ) <EOL> f . write ( "<STR_LIT>" % self . quad ) <EOL> f . write ( "<STR_LIT>" % self . real_time ) <EOL> f . write ( "<STR_LIT>" % self . live_time ) <EOL> roiform = "<STR_LIT>" <EOL> for i , r in enumerate ( self . rois ) : <EOL> f . write ( roiform % ( i + <NUM_LIT:1> , r . name , r . left , r . right ) ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> for e , d in zip ( self . energy , self . counts ) : <EOL> dlog = <NUM_LIT:0.> <EOL> if d > <NUM_LIT:0> : dlog = np . log10 ( max ( d , <NUM_LIT:1> ) ) <EOL> f . write ( "<STR_LIT>" % ( e , d , dlog ) ) <EOL> f . write ( "<STR_LIT:\n>" ) <EOL> f . close ( ) <EOL> def save_mcafile ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> fp = open ( filename , '<STR_LIT:w>' ) <EOL> fp . write ( '<STR_LIT>' ) <EOL> fp . write ( '<STR_LIT>' ) <EOL> fp . write ( '<STR_LIT>' % self . start_time ) <EOL> fp . write ( '<STR_LIT>' % len ( self . counts ) ) <EOL> fp . write ( '<STR_LIT>' % self . real_time ) <EOL> fp . write ( '<STR_LIT>' % self . live_time ) <EOL> fp . write ( '<STR_LIT>' % self . offset ) <EOL> fp . write ( '<STR_LIT>' % self . slope ) <EOL> fp . write ( '<STR_LIT>' % self . quad ) <EOL> fp . write ( '<STR_LIT>' % len ( self . rois ) ) <EOL> for i , roi in enumerate ( self . rois ) : <EOL> fp . write ( '<STR_LIT>' % ( i , roi . left ) ) <EOL> fp . write ( '<STR_LIT>' % ( i , roi . right ) ) <EOL> fp . write ( '<STR_LIT>' % ( i , roi . name ) ) <EOL> for e in self . environ : <EOL> fp . write ( '<STR_LIT>' % ( e . addr , e . val , e . desc ) ) <EOL> fp . write ( '<STR_LIT>' ) <EOL> for d in self . counts : <EOL> fp . write ( "<STR_LIT>" % d ) <EOL> fp . close ( ) <EOL> def create_mca ( counts = None , nchans = <NUM_LIT> , offset = <NUM_LIT:0> , slope = <NUM_LIT:0> , quad = <NUM_LIT:0> , <EOL> name = '<STR_LIT>' , start_time = '<STR_LIT>' , real_time = <NUM_LIT:0> , live_time = <NUM_LIT:0> , <EOL> dt_factor = <NUM_LIT:1> , input_counts = <NUM_LIT:0> , tau = <NUM_LIT:0> , _larch = None , ** kws ) : <EOL> """<STR_LIT>""" <EOL> return MCA ( counts = counts , nchans = nchans , name = name , <EOL> start_time = start_time , offset = offset , slope = slope , <EOL> quad = quad , dt_factor = dt_factor , real_time = real_time , <EOL> live_time = live_time , input_counts = input_counts , <EOL> tau = tau , ** kws ) <EOL> def registerLarchPlugin ( ) : <EOL> return ( '<STR_LIT>' , { '<STR_LIT>' : create_mca } ) </s>
