Exemple #1
0
    def read_file(self, file, netcdf=0):
        """
      Reads a disk file into an Med object. The file contains the information
      from the Med object which it makes sense to store permanently, but does
      not contain all of the internal state information for the Med.

      Inputs:
         file:
            The name of the disk file to read.
      """
        if (netcdf != 0):
            r = Mca.read_netcdf_file(file)
        else:
            r = Mca.read_ascii_file(file)
        self.name = file
        self.n_detectors = r['n_detectors']
        self.mcas = []
        for i in range(self.n_detectors):
            self.mcas.append(Mca.Mca())
            self.mcas[i].set_rois(r['rois'][i])
            self.mcas[i].set_data(r['data'][i])
            self.mcas[i].set_name(self.name + ':' + str(i + 1))
        self.set_elapsed(r['elapsed'])
        self.set_calibration(r['calibration'])
        self.set_environment(r['environment'])
Exemple #2
0
 def write_peak_file(self):
    file = tkFileDialog.asksaveasfilename(parent=self.widgets.top, 
                              title='Output file',
                              filetypes=[('Peak files','*.pks'),
                                         ('All files','*')])
    if (file == ''): return
    Mca.write_peaks(file, self.fit.peaks, self.fit.background)
Exemple #3
0
   def read_file(self, file, netcdf=0):
      """
      Reads a disk file into an Med object. The file contains the information
      from the Med object which it makes sense to store permanently, but does
      not contain all of the internal state information for the Med.

      Inputs:
         file:
            The name of the disk file to read.
      """
      if (netcdf != 0):
         r = Mca.read_netcdf_file(file)
      else:
         r = Mca.read_ascii_file(file)
      self.name = file
      self.n_detectors = r['n_detectors']
      self.mcas = []
      for i in range(self.n_detectors):
         self.mcas.append(Mca.Mca())
         self.mcas[i].set_rois(r['rois'][i])
         self.mcas[i].set_data(r['data'][i])
         self.mcas[i].set_name(self.name + ':' + str(i+1))
      self.set_elapsed(r['elapsed'])
      self.set_calibration(r['calibration'])
      self.set_environment(r['environment'])
Exemple #4
0
    def read_environment_file(self, file):
        """
      Reads a file containing the "environment" PVs.  The values and desriptions of these
      PVs are stored in the data files written by Mca.write_file().

      Inputs:
         file:
            The name of the file containing the environment PVs. This is an ASCII file
            with each line containing a process variable name, followed by a
            space and a description field.
      """
        self.environment = []
        try:
            fp = open(file, 'r')
            lines = fp.readlines()
            fp.close()
        except:
            return
        for line in lines:
            env = Mca.McaEnvironment()
            pos = line.find(' ')
            if (pos != -1):
                env.name = line[0:pos]
                env.description = line[pos + 1:].strip()
            else:
                env.name = line.strip()
                env.description = ' '
            self.environment.append(env)
Exemple #5
0
 def read_peak_file(self):
    file = tkFileDialog.askopenfilename(parent=self.widgets.top,
                              title='Peaks file',
                              filetypes=[('Peak files','*.pks'),
                                         ('All files','*')])
    if (file == ''): return
    r = Mca.read_peaks(file)
    self.fit.peaks = r['peaks']
    self.fit.background = r['background']
    self.index = 0
    self.update_globals()
    self.update_peaks()
Exemple #6
0
 def get_elapsed(self):
     """
   Reads the elapsed information from the EPICS mca record.  Stores this information
   in the epicsMca object, and returns an McaElapsed object with this information.
   """
     elapsed = Mca.McaElapsed()
     pvs = self.pvs['elapsed']
     for pv in pvs.keys():
         pvs[pv].array_get()
     pvs[pv].pend_io()
     elapsed.real_time = pvs['ertm'].getValue()
     elapsed.live_time = pvs['eltm'].getValue()
     elapsed.total_counts = pvs['act'].getValue()
     elapsed.read_time = pvs['rtim'].getValue()
     elapsed.start_time = string.strip(pvs['stim'].getValue())
     Mca.Mca.set_elapsed(self, elapsed)
     return elapsed
Exemple #7
0
 def get_calibration(self):
     """
   Reads the calibration information from the EPICS mca record.  Stores this information
   in the epicsMca object, and returns an McaCalibration object with this information.
   """
     calibration = Mca.McaCalibration()
     pvs = self.pvs['calibration']
     for pv in pvs.keys():
         pvs[pv].array_get()
     pvs[pv].pend_io()
     calibration.offset = pvs['calo'].getValue()
     calibration.slope = pvs['cals'].getValue()
     calibration.quad = pvs['calq'].getValue()
     calibration.two_theta = pvs['tth'].getValue()
     calibration.units = pvs['egu'].getValue()
     Mca.Mca.set_calibration(self, calibration)
     return calibration
Exemple #8
0
 def insert_or_append(self, insert):
    new_peak = Mca.McaPeak()
    if (self.callback_command != None):
       cursor = self.callback_command(get_cursor=1)
       energy = self.mca.channel_to_energy(cursor)
    else:
       energy=0.
    new_peak.initial_energy = energy
    # Make a reasonble estimate of initial FWHM
    new_peak.initial_fwhm = .15
    new_peak.energy_flag=1
    new_peak.fwhm_flag=1
    if (insert):
       self.fit.peaks.insert(self.index, new_peak)
    else:
       self.fit.peaks.insert(self.index+1, new_peak)
       self.index = self.index + 1
    self.update_peaks()
Exemple #9
0
    def __init__(self, n_detectors=16, file=None):
        """
      Initialization code for creating a new Med object.

      Keywords:
         n_detectors:
            The number of detectors (Mca objects) in the Med.

         file:
            The name of a disk file to read into the Med after it is created.
            The number of detectors in the Med will be changed if the number of
            Mca objects in the disk file is different from the Med.
      """
        Mca.Mca.__init__(self)  # Invoke base class initialization
        self.n_detectors = n_detectors
        self.mcas = []
        for i in range(n_detectors):
            self.mcas.append(Mca.Mca())
        if (file != None): self.read_file(file)
Exemple #10
0
 def get_presets(self):
     """
   Reads the preset information from the EPICS mca record.  Stores this information
   in the epicsMca object, and returns an McaPresets object with this information.
   """
     presets = Mca.McaPresets()
     pvs = self.pvs['presets']
     for pv in pvs.keys():
         pvs[pv].array_get()
     pvs[pv].pend_io()
     presets.real_time = pvs['prtm'].getValue()
     presets.live_time = pvs['pltm'].getValue()
     presets.total_counts = pvs['pct'].getValue()
     presets.start_channel = pvs['pctl'].getValue()
     presets.end_channel = pvs['pcth'].getValue()
     presets.dwell = pvs['dwel'].getValue()
     presets.channel_advance = pvs['chas'].getValue()
     presets.prescale = pvs['pscl'].getValue()
     Mca.Mca.set_presets(self, presets)
     return presets
Exemple #11
0
 def get_rois(self, energy=0):
     """
   Reads the ROI information from the EPICS mca record.  Stores this information
   in the epicsMca object, and returns a list of McaROI objects with this information.
   """
     for i in range(self.max_rois):
         pvs = self.roi_def_pvs[i]
         for pv in pvs.keys():
             pvs[pv].array_get()
     pvs[pv].pend_io()
     rois = []
     for i in range(self.max_rois):
         roi = Mca.McaROI()
         pvs = self.roi_def_pvs[i]
         r = 'R' + str(i)
         roi.left = pvs[r + 'lo'].getValue()
         roi.right = pvs[r + 'hi'].getValue()
         roi.label = pvs[r + 'nm'].getValue()
         roi.bgd_width = pvs[r + 'bg'].getValue()
         roi.use = 1
         if (roi.left > 0) and (roi.right > 0): rois.append(roi)
     Mca.Mca.set_rois(self, rois)
     return Mca.Mca.get_rois(self, energy=energy)
Exemple #12
0
 def clear_peaks(self):
    self.fit.peaks=[Mca.McaPeak()]
    self.index=0
    self.update_peaks()
Exemple #13
0
   def __init__(self, mca, fit=None, command=None):
      class widgets:
         pass
      self.results_file = 'fit_results.txt'
      self.spreadsheet_file = 'fit_spreadsheet.txt'
      self.mca = mca
      self.callback_command = command
      self.widgets = widgets()
      self.background_mca = copy.deepcopy(mca)
      self.fit_mca = copy.deepcopy(mca)
      self.mark_peaks_mca = copy.deepcopy(mca)
      data = self.background_mca.get_data() * 0
      self.background_mca.set_data(data)
      self.fit_mca.set_data(data)
      self.mark_peaks_mca.set_data(data)
      self.index=0
      if (fit == None):
         self.fit = Mca.McaFit(mca)
      else:
         self.fit = fit
      # Update the fit object to be consistent with the Mca
      self.fit.update(self.fit_mca)
      
      if (len(self.fit.peaks) == 0): self.fit.peaks=[Mca.McaPeak()]
      xsize=10
      top = myTkTop.myTkTop()
      Pmw.initialise(top)
      top.title('Peak Fit')
      self.widgets.top = top
      frame = Frame(top, borderwidth=1, relief='raised')
      frame.pack(fill=X)
      mb = Pmw.MenuBar(frame)
      mb.pack(fill=X)
      mb.addmenu('File', '', side='left')
      self.widgets.file = mb.component('File-menu')
      mb.addmenuitem('File', 'command', 
                      label='Read peaks...', 
                      command=self.read_peak_file)
      mb.addmenuitem('File', 'command', 
                      label='Write peaks...',
                      command=self.write_peak_file)
      mb.addmenuitem('File', 'command', 
                      label='Results file...', command=self.set_results_file)
      mb.addmenuitem('File', 'command', 
                      label='Spreadsheet file...', 
                      command=self.set_spreadsheet_file)
      mb.addmenuitem('File', 'command', 'Exit', label='Exit',
                      command=self.menu_exit)
      t = Pmw.Group(top, tag_text='Background Parameters'); t.pack(fill=X)
      bcol = t.interior()
      row = Frame(bcol); row.pack(side=TOP)
      background = self.fit.background
      self.exponent_options = ('2','4','6')
      self.widgets.exponent = t = Pmw.OptionMenu(row, labelpos=N,
                                    label_text='Exponent',
                                    items=self.exponent_options,
                                    initialitem = str(background.exponent))
      t.pack(side=LEFT)
      self.widgets.top_width = t = Pmw.EntryField(row,
                                    value=background.top_width,
                                    entry_width=8, entry_justify=CENTER,
                                    labelpos=N, label_text='Top width',
                                    validate={'validator':'real'})
      t.pack(side=LEFT)
      self.widgets.bottom_width = t = Pmw.EntryField(row,
                                       value=background.bottom_width,
                                       entry_width=8, entry_justify=CENTER,
                                       labelpos=N, label_text='Bottom width',
                                       validate={'validator':'real'})
      t.pack(side=LEFT)
      self.tangent_options = ('No', 'Yes')
      self.widgets.tangent = t = Pmw.OptionMenu(row, labelpos=N,
                                        label_text='Tangent?',
                                        items=self.tangent_options,
                                        menubutton_width=3,
                                        initialitem = background.tangent)
      t.pack(side=LEFT)
      self.compress_options = ('1', '2', '4', '8', '16')
      self.widgets.compress = t = Pmw.OptionMenu(row, labelpos=N,
                                        label_text='Compression',
                                        items=self.compress_options,
                                        menubutton_width=2,
                                        initialitem = str(background.compress))
      t.pack(side=LEFT)
      row = Frame(bcol); row.pack(side=TOP)
      self.widgets.fit_background = t = Button(row, text='Fit background',
                                               command=self.fit_background)
      t.pack(side=LEFT)
      self.widgets.plot_background = t = Button(row, text='Re-plot background',
                                                command=self.plot_background)
      t.pack(side=LEFT)

      t = Pmw.Group(top, tag_text='Peak Fit Parameters'); t.pack(fill=X)
      fcol = t.interior()
      t = Pmw.Group(fcol, tag_text='Initial energy calibration'); 
      t.pack(side=TOP)
      row = t.interior()
      self.widgets.energy_cal_offset = t = Pmw.EntryField(row,
                            value=self.fit.initial_energy_offset,
                            entry_width=10, entry_justify=CENTER,
                            labelpos=W, label_text='Offset:',
                            validate={'validator':'real'})
      t.pack(side=LEFT)
      self.widgets.energy_cal_slope = t = Pmw.EntryField(row,
                            value=self.fit.initial_energy_slope,
                            entry_width=10, entry_justify=CENTER,
                            labelpos=W, label_text='Slope:',
                            validate={'validator':'real'})
      t.pack(side=LEFT)
      self.optimize_options = ('Fix', 'Optimize')
      self.widgets.energy_cal_flag = t = Pmw.OptionMenu(row, labelpos=W,
                                        label_text='Flag:',
                                        items=self.optimize_options,
                                        menubutton_width=8,
                                        initialitem = self.fit.energy_flag)
      t.pack(side=LEFT)
      t = Pmw.Group(fcol, tag_text='Initial FWHM calibration');
      t.pack(side=TOP)
      row = t.interior()
      self.widgets.fwhm_cal_offset = t = Pmw.EntryField(row, 
                            value=self.fit.initial_fwhm_offset,
                            command = self.update_peaks,
                            entry_width=10, entry_justify=CENTER,
                            labelpos=W, label_text='Offset:',
                            validate={'validator':'real'})
      t.pack(side=LEFT)
      self.widgets.fwhm_cal_slope = t = Pmw.EntryField(row,
                            value=self.fit.initial_fwhm_slope,
                            command = self.update_peaks,
                            entry_width=10, entry_justify=CENTER,
                            labelpos=W, label_text='Slope:',
                            validate={'validator':'real'})
      t.pack(side=LEFT)
      self.widgets.fwhm_cal_flag = t = Pmw.OptionMenu(row, labelpos=W,
                                        label_text='Flag:',
                                        items=self.optimize_options,
                                        menubutton_width=8,
                                        initialitem = self.fit.fwhm_flag)
      t.pack(side=LEFT)

      width=10
      t = Pmw.Group(fcol, tag_text='Peak parameters'); t.pack(fill=X)
      pcol = t.interior()
      row = Frame(pcol); row.pack()
      self.widgets.label = t = Pmw.EntryField(row, value=' ',
                            entry_width=width, entry_justify=CENTER,
                            labelpos=N, label_text='Label',
                            command=self.peak_label)
      t.pack(side=LEFT)
      self.widgets.energy = t = Pmw.EntryField(row, value=0.,
                            entry_width=width, entry_justify=CENTER,
                            labelpos=N, label_text='Energy',
                            validate={'validator':'real'},
                            command=self.peak_params)
      t.pack(side=LEFT)

      self.widgets.energy_flag = t = Pmw.OptionMenu(row, labelpos=N,
                                        label_text='Energy flag',
                                        items=self.optimize_options,
                                        initialitem = 1,
                                        menubutton_width=8,
                                        command=self.peak_params)
      t.pack(side=LEFT)
      self.widgets.fwhm = t = Pmw.EntryField(row, value=0.,
                            entry_width=width, entry_justify=CENTER,
                            labelpos=N, label_text='FWHM',
                            validate={'validator':'real'},
                            command=self.peak_params)
      t.pack(side=LEFT)
      self.global_optimize_options = ('Global', 'Optimize', 'Fix')
      self.widgets.fwhm_flag = t = Pmw.OptionMenu(row, labelpos=N,
                                        label_text='FWHM flag',
                                        items=self.global_optimize_options,
                                        initialitem = 0,
                                        menubutton_width=8,
                                        command=self.peak_params)
      t.pack(side=LEFT)
      self.widgets.ampl_factor = t = Pmw.EntryField(row, value=0.,
                            entry_width=width, entry_justify=CENTER,
                            labelpos=N, label_text='Ampl. ratio',
                            validate={'validator':'real'},
                            command=self.peak_params)
      t.pack(side=LEFT)
      row = Frame(pcol); row.pack(side=TOP, anchor=W)
      col = Frame(row); col.pack(side=LEFT)
      width=7
      self.widgets.insert = t = Button(col, text='Insert', width=width,
                           command=lambda s=self: s.insert_or_append(insert=1)); 
      t.pack()
      self.widgets.append = t = Button(col, text='Append', width=width,
                           command=lambda s=self: s.insert_or_append(insert=0)); 
      t.pack()
      self.widgets.delete = t = Button(col, text='Delete', width=width,
                                       command=self.delete_peak); 
      t.pack()
      self.widgets.clear_peaks = t = Button(col, text='Clear', width=width,
                                            command=self.clear_peaks) 
      t.pack()
      self.widgets.sort = t = Button(col, text='Sort', width=width,
                                     command=self.sort_peaks); 
      t.pack()
      self.widgets.mark_peaks = t = Button(col, text='Mark all', width=width,
                                           command=self.mark_peaks) 
      t.pack()

      self.widgets.peak_list = t = Pmw.ScrolledListBox(row,
                                            listbox_font=('Courier','8'), 
                                            listbox_width=65, listbox_height=10,
                                            vscrollmode='static',
                                            hscrollmode='none',
                                            selectioncommand=self.new_index)
      t.pack()

      width=40
      row = Frame(fcol); row.pack(anchor=W)
      t = Label(row, text='Fit results file:'); t.pack(side=LEFT)
      self.widgets.results_file_name = t = Label(row, 
                            text=self.results_file,
                            foreground='blue', background='white')
      t.pack(side=LEFT)
      row = Frame(fcol); row.pack(anchor=W)
      t = Label(row, text='Spreadsheet file:'); t.pack(side=LEFT)
      self.widgets.spreadsheet_file_name = t = Label(row, 
                            text=self.spreadsheet_file,
                            foreground='blue', background='white')
      t.pack(side=LEFT)
      self.widgets.append_mode = t = Pmw.OptionMenu(fcol, labelpos=W,
            label_text='Overwrite or append results and spreadsheet files:',
                                        items=('Overwrite', 'Append'),
                                        menubutton_width=9,
                                        initialitem = 1)
      t.pack(anchor=W)
      row = Frame(fcol); row.pack(side=TOP)
      self.widgets.fit_peaks = t = Button(row, text='Fit peaks',
                                          command=self.fit_peaks) 
      t.pack(side=LEFT)
      self.widgets.plot_fit = t = Button(row, text='Re-plot fit',
                                         command=self.plot_fit)
      t.pack(side=LEFT)
      self.update_peaks()