ImgPixSpectra/data/SpectralArray.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 #--------------------
00004 
00005 import numpy as np
00006 import os
00007 import sys
00008 
00009 #--------------------
00010 
00011 class SpectralArray :
00012     """Holds everything for 2D spactral array"""
00013 
00014 
00015     def __init__ ( self, fname ) :
00016         """Constructor initialization"""
00017         self.set_default_shape_parameters()
00018         self.get_shape_from_file(fname+'.sha') 
00019         self.get_array_from_file(fname)
00020 
00021 
00022     def get_array_from_file (self, fname) :
00023         print """Get array from file""", fname
00024         self.arr = np.loadtxt(fname, dtype=np.float32)
00025 
00026 
00027     def set_default_shape_parameters (self) :
00028         """Set default values for all shape parameters"""
00029         self.npixels =   0
00030         self.nbins   = 100
00031         self.amin    =   0
00032         self.amax    = 100
00033         self.nevents =   0
00034         self.fname   = ' '
00035 
00036 
00037     def get_shape_from_file (self, fname) :
00038         print """Get shape from file""", fname
00039         if os.path.exists(fname) :
00040             f=open(fname,'r')
00041             for line in f :
00042                 if len(line) == 1 : continue # line is empty
00043                 key = line.split()[0]
00044                 val = line.split()[1]
00045                 if   key == 'NPIXELS'  : self.npixels = int(val)
00046                 elif key == 'NBINS'    : self.nbins   = int(val)
00047                 elif key == 'AMIN'     : self.amin    = float(val)
00048                 elif key == 'AMAX'     : self.amax    = float(val)
00049                 elif key == 'NEVENTS'  : self.nevents = int(val)
00050                 elif key == 'ARRFNAME' : self.fname   = val
00051                 else :
00052                     print 'The record : %s %s \n is UNKNOWN in get_shape_from_file ()' % (key, val) 
00053             f.close()
00054         else :
00055             print 'The file %s does not exist' % (fname)
00056             print 'WILL USE DEFAULT CONFIGURATION PARAMETERS'
00057 
00058 
00059     def print_shape_parameters (self) :
00060         """Print shape parameters"""
00061         print 'NPIXELS  :', self.npixels
00062         print 'NBINS    :', self.nbins  
00063         print 'AMIN     :', self.amin   
00064         print 'AMAX     :', self.amax   
00065         print 'NEVENTS  :', self.nevents
00066         print 'ARRFNAME :', self.fname  
00067 
00068 
00069     def print_array_subset (self) :
00070         """Print a few array elements"""
00071         for pix in range(min(5,self.npixels)) :
00072             print '\nPixel', pix, 'spectrum:'
00073             for bin in range(self.nbins) :
00074                 print self.arr[pix][bin],
00075 
00076 #--------------------
00077 
00078 def main_test() :
00079     """Test example"""
00080     sa = SpectralArray ('sxr16410-r0081-opal-camera-pix-spectra.txt')
00081     sa.print_shape_parameters ()
00082     sa.print_array_subset ()
00083 
00084 
00085 if __name__ == "__main__" :
00086     main_test()
00087     sys.exit ( 'End of test' )
00088 
00089 #--------------------

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7