def __init__(self,run_number,year=0,filename=""):
        """Constructor. Reads file, stores and sorts data."""
        
        # Get the current year
        if year == 0:   year = datetime.datetime.now().year
        
        # read file if not provided
        if filename == "":
            
            # Get spectrometer directory. Based on rmlm's bnmr_20a.cpp.
            if run_number >= 40000 and run_number <= 44999:
                spect_dir = "bnmr/"
            elif run_number >= 45000 and run_number <= 49999:
                spect_dir = "bnqr/"
            else:
                raise ValueError("Run number out of range") 
                
            # look for environment variable
            if spect_dir == "bnmr/" and self.evar_bnmr in list(os.environ.keys()):
                filename = os.environ[self.evar_bnmr]
                
            elif spect_dir == "bnqr/" and self.evar_bnqr in list(os.environ.keys()):
                filename = os.environ[self.evar_bnqr]
                    
            # finalize file name
            if filename[-1] != '/':
                filename += '/'
            filename += repr(year) + "/0" + repr(run_number) + ".msr"
        
        # check file existence
        if not os.path.isfile(filename):
            raise RuntimeError("File %s does not exist" % filename)
        
        # Open file ----------------------------------------------------------
        fh = mp.open_read(filename)
        
        if fh < 0: 
            raise RuntimeError("__init__: open file failed. ")
        try:
            # Read run description
            self.exp = mp.get_exp_number(fh)
            self.run = mp.get_run_number(fh)
            self.duration = mp.get_elapsed_seconds(fh)
            self.start_time = mp.get_start_time(fh)
            self.end_time = mp.get_end_time(fh)
            
            try:
                self.title = str(mp.get_title(fh))
                self.lab = str(mp.get_lab(fh))
                self.area = str(mp.get_area(fh))
                self.method = str(mp.get_method(fh))
                self.sample = str(mp.get_sample(fh))
                self.orientation = str(mp.get_orientation(fh))
                self.das = str(mp.get_das(fh))
                self.mode = str(mp.get_insert(fh))
                self.experimenter = str(mp.get_experimenter(fh))
            except UnicodeEncodeError:
                self.title = mp.get_title(fh)
                self.lab = mp.get_lab(fh)
                self.area = mp.get_area(fh)
                self.method = mp.get_method(fh)
                self.sample = mp.get_sample(fh)
                self.orientation = mp.get_orientation(fh)
                self.das = mp.get_das(fh)
                self.mode = mp.get_insert(fh)
                self.experimenter = mp.get_experimenter(fh)
            
            # Read histograms
            n_hist = mp.get_hists(fh)[1]
            self.hist_list = []
            for i in range(1,n_hist+1):
                self.hist_list.append(bhist())
                self.hist_list[-1].id_number = i
                self.hist_list[-1].htype = mp.get_hist_type(fh,i)
                
                self.hist_list[-1].data = mp.get_hist_data(fh,i)
                
                self.hist_list[-1].n_bytes = mp.get_hist_n_bytes(fh,i)
                self.hist_list[-1].n_bins = mp.get_hist_n_bins(fh,i)
                self.hist_list[-1].n_events = mp.get_hist_n_events(fh,i)
                
                self.hist_list[-1].fs_per_bin = mp.get_hist_fs_per_bin(fh,i)
                self.hist_list[-1].s_per_bin = mp.get_hist_sec_per_bin(fh,i)
                self.hist_list[-1].t0_ps = mp.get_hist_t0_ps(fh,i)
                self.hist_list[-1].t0_bin = mp.get_hist_t0_bin(fh,i)
                
                self.hist_list[-1].good_bin1 = mp.get_hist_good_bin1(fh,i)
                self.hist_list[-1].good_bin2 = mp.get_hist_good_bin2(fh,i)
                self.hist_list[-1].background1 = mp.get_hist_background1(fh,i)
                self.hist_list[-1].background2 = mp.get_hist_background2(fh,i)
                
                try:
                    self.hist_list[-1].title = str(mp.get_hist_title(fh,i))
                except UnicodeEncodeError:
                    self.hist_list[-1].title = mp.get_hist_title(fh,i)
                
            # Read scalers
            #~ n_scaler = mp.get_scalers(fh)[1]
            #~ self.scaler_list = []
            #~ for i in range(1,n_scaler+1):
                #~ self.scaler_list.append(bscaler())
                #~ self.scaler_list[-1].counts = mp.get_scaler_counts(fh,i)
                #~ self.scaler_list[-1].id_number = i
                #~ try:
                    #~ self.scaler_list[-1].title = str(mp.get_scaler_label(fh,i))
                #~ except UnicodeEncodeError as e:
                    #~ self.scaler_list[-1].title = repr(e)
           
            # Read independent variables
            n_var = mp.get_ivars(fh)[1]
            self.var_list = []
            for i in range(1,n_var+1):
                self.var_list.append(bvar())
                self.var_list[-1].id_number = i
                self.var_list[-1].low = mp.get_ivar_low(fh,i)
                self.var_list[-1].high = mp.get_ivar_high(fh,i)
                self.var_list[-1].mean = mp.get_ivar_mean(fh,i)
                self.var_list[-1].std = mp.get_ivar_std(fh,i)
                self.var_list[-1].skew = mp.get_ivar_skewness(fh,i)
                
                try:
                    self.var_list[-1].title = str(mp.get_ivar_name(fh,i))
                    self.var_list[-1].description = str(\
                                                mp.get_ivar_description(fh,i))
                    self.var_list[-1].units = str(mp.get_ivar_units(fh,i))
                except UnicodeEncodeError:
                    self.var_list[-1].title = mp.get_ivar_name(fh,i)
                    self.var_list[-1].description =mp.get_ivar_description(fh,i)
                    self.var_list[-1].units = mp.get_ivar_units(fh,i)
            
        # Close file ----------------------------------------------------------
        finally:
            mp.close_read(fh)
        
        # Sort independent variables into dictionaries by title
        self.ppg = bdict()
        self.camp = bdict()
        self.epics = bdict()
        for v in self.var_list: 
            try:
                if 'PPG' in v.title:
                    self.ppg[self.dkeys[v.title.split("/")[-1]]] = v
                elif v.title[0] == "/":
                    self.camp[self.dkeys[v.title]] = v
                else:
                    self.epics[self.dkeys[v.title]] = v
            except (KeyError,IndexError):
                    message = '"' + v.title + '" not found in dkeys. '+\
                                "Data in list, but not sorted to dict."
                    warnings.warn(message,RuntimeWarning,stacklevel=2)
                    
        # Sort histograms into dictionaries by title and convert to doubles
        self.hist = bdict()
        for h in self.hist_list:
            self.hist[h.title] = h
            self.hist[h.title].data = self.hist[h.title].data.astype(float)
        
        # Sort scalers into dictionaries by title
        #~ self.sclr = bdict()
        #~ for s in self.scaler_list:
            #~ new_key = s.title.split("%")[-1].replace(" ","")
            #~ self.sclr[new_key] = s
            
        # set the date
        self.start_date = time.ctime(self.start_time)
        self.end_date = time.ctime(self.end_time)
        self.year = time.gmtime(self.start_time).tm_year
def __init__ ( self , run_number , year = 0 , filename = "" ) : if year == 0 : year = datetime . datetime . now ( ) . year if filename == "" : if run_number >= 40000 and run_number <= 44999 : spect_dir = "bnmr/" elif run_number >= 45000 and run_number <= 49999 : spect_dir = "bnqr/" else : raise ValueError ( "Run number out of range" ) if spect_dir == "bnmr/" and self . evar_bnmr in list ( os . environ . keys ( ) ) : filename = os . environ [ self . evar_bnmr ] elif spect_dir == "bnqr/" and self . evar_bnqr in list ( os . environ . keys ( ) ) : filename = os . environ [ self . evar_bnqr ] if filename [ - 1 ] != '/' : filename += '/' filename += repr ( year ) + "/0" + repr ( run_number ) + ".msr" if not os . path . isfile ( filename ) : raise RuntimeError ( "File %s does not exist" % filename ) fh = mp . open_read ( filename ) if fh < 0 : raise RuntimeError ( "__init__: open file failed. " ) try : self . exp = mp . get_exp_number ( fh ) self . run = mp . get_run_number ( fh ) self . duration = mp . get_elapsed_seconds ( fh ) self . start_time = mp . get_start_time ( fh ) self . end_time = mp . get_end_time ( fh ) try : self . title = str ( mp . get_title ( fh ) ) self . lab = str ( mp . get_lab ( fh ) ) self . area = str ( mp . get_area ( fh ) ) self . method = str ( mp . get_method ( fh ) ) self . sample = str ( mp . get_sample ( fh ) ) self . orientation = str ( mp . get_orientation ( fh ) ) self . das = str ( mp . get_das ( fh ) ) self . mode = str ( mp . get_insert ( fh ) ) self . experimenter = str ( mp . get_experimenter ( fh ) ) except UnicodeEncodeError : self . title = mp . get_title ( fh ) self . lab = mp . get_lab ( fh ) self . area = mp . get_area ( fh ) self . method = mp . get_method ( fh ) self . sample = mp . get_sample ( fh ) self . orientation = mp . get_orientation ( fh ) self . das = mp . get_das ( fh ) self . mode = mp . get_insert ( fh ) self . experimenter = mp . get_experimenter ( fh ) n_hist = mp . get_hists ( fh ) [ 1 ] self . hist_list = [ ] for i in range ( 1 , n_hist + 1 ) : self . hist_list . append ( bhist ( ) ) self . hist_list [ - 1 ] . id_number = i self . hist_list [ - 1 ] . htype = mp . get_hist_type ( fh , i ) self . hist_list [ - 1 ] . data = mp . get_hist_data ( fh , i ) self . hist_list [ - 1 ] . n_bytes = mp . get_hist_n_bytes ( fh , i ) self . hist_list [ - 1 ] . n_bins = mp . get_hist_n_bins ( fh , i ) self . hist_list [ - 1 ] . n_events = mp . get_hist_n_events ( fh , i ) self . hist_list [ - 1 ] . fs_per_bin = mp . get_hist_fs_per_bin ( fh , i ) self . hist_list [ - 1 ] . s_per_bin = mp . get_hist_sec_per_bin ( fh , i ) self . hist_list [ - 1 ] . t0_ps = mp . get_hist_t0_ps ( fh , i ) self . hist_list [ - 1 ] . t0_bin = mp . get_hist_t0_bin ( fh , i ) self . hist_list [ - 1 ] . good_bin1 = mp . get_hist_good_bin1 ( fh , i ) self . hist_list [ - 1 ] . good_bin2 = mp . get_hist_good_bin2 ( fh , i ) self . hist_list [ - 1 ] . background1 = mp . get_hist_background1 ( fh , i ) self . hist_list [ - 1 ] . background2 = mp . get_hist_background2 ( fh , i ) try : self . hist_list [ - 1 ] . title = str ( mp . get_hist_title ( fh , i ) ) except UnicodeEncodeError : self . hist_list [ - 1 ] . title = mp . get_hist_title ( fh , i ) n_var = mp . get_ivars ( fh ) [ 1 ] self . var_list = [ ] for i in range ( 1 , n_var + 1 ) : self . var_list . append ( bvar ( ) ) self . var_list [ - 1 ] . id_number = i self . var_list [ - 1 ] . low = mp . get_ivar_low ( fh , i ) self . var_list [ - 1 ] . high = mp . get_ivar_high ( fh , i ) self . var_list [ - 1 ] . mean = mp . get_ivar_mean ( fh , i ) self . var_list [ - 1 ] . std = mp . get_ivar_std ( fh , i ) self . var_list [ - 1 ] . skew = mp . get_ivar_skewness ( fh , i ) try : self . var_list [ - 1 ] . title = str ( mp . get_ivar_name ( fh , i ) ) self . var_list [ - 1 ] . description = str ( mp . get_ivar_description ( fh , i ) ) self . var_list [ - 1 ] . units = str ( mp . get_ivar_units ( fh , i ) ) except UnicodeEncodeError : self . var_list [ - 1 ] . title = mp . get_ivar_name ( fh , i ) self . var_list [ - 1 ] . description = mp . get_ivar_description ( fh , i ) self . var_list [ - 1 ] . units = mp . get_ivar_units ( fh , i ) finally : mp . close_read ( fh ) self . ppg = bdict ( ) self . camp = bdict ( ) self . epics = bdict ( ) for v in self . var_list : try : if 'PPG' in v . title : self . ppg [ self . dkeys [ v . title . split ( "/" ) [ - 1 ] ] ] = v elif v . title [ 0 ] == "/" : self . camp [ self . dkeys [ v . title ] ] = v else : self . epics [ self . dkeys [ v . title ] ] = v except ( KeyError , IndexError ) : message = '"' + v . title + '" not found in dkeys. ' + "Data in list, but not sorted to dict." warnings . warn ( message , RuntimeWarning , stacklevel = 2 ) self . hist = bdict ( ) for h in self . hist_list : self . hist [ h . title ] = h self . hist [ h . title ] . data = self . hist [ h . title ] . data . astype ( float ) self . start_date = time . ctime ( self . start_time ) self . end_date = time . ctime ( self . end_time ) self . year = time . gmtime ( self . start_time ) . tm_year
Constructor. Reads file, stores and sorts data.