Exemplo n.º 1
0
 def _configure(self, config):
     d = ConfigDict.ConfigDict()
     d.read(config["file"])
     if "concentrations" not in d:
         d["concentrations"] = {}
     if "attenuators" not in d:
         d["attenuators"] = {}
         d["attenuators"]["Matrix"] = [1, "Water", 1.0, 0.01, 45.0, 45.0]
     if "flux" in config:
         d["concentrations"]["flux"] = float(config["flux"])
     if "time" in config:
         d["concentrations"]["time"] = float(config["time"])
     self.mcafit.mcafit.configure(d)
Exemplo n.º 2
0
    def _configure(self, config):
        d = ConfigDict.ConfigDict()
        d.read(config["file"])
        if not d.has_key('concentrations'):
            d['concentrations'] = {}
        if not d.has_key('attenuators'):
            d['attenuators'] = {}
            d['attenuators']['Matrix'] = [1, 'Water', 1.0, 0.01, 45.0, 45.0]
        if config.has_key('flux'):
            d['concentrations']['flux'] = float(config['flux'])
        if config.has_key('time'):
            d['concentrations']['time'] = float(config['time'])

        self.mcafit.mcafit.configure(d)
    def _configure(self, config):
        d = ConfigDict.ConfigDict()
        d.read(config["file"])
        if 'concentrations' not in d:
            d['concentrations'] = {}
        if 'attenuators' not in d:
            d['attenuators'] = {}
            d['attenuators']['Matrix'] = [1, 'Water', 1.0, 0.01, 45.0, 45.0]
        if 'flux' in config:
            d['concentrations']['flux'] = float(config['flux'])
        if 'time' in config:
            d['concentrations']['time'] = float(config['time'])

        self.mcafit.mcafit.configure(d)
Exemplo n.º 4
0
def gplot_rh5(h5file, channel='channel00'):
    f = h5py.File(h5file, 'r')
    spe = np.array(f['raw/' + channel + '/sumspec'])
    try:
        names = [n.decode('utf8') for n in f['fit/' + channel + '/names']]
        cfg = f['fit/' + channel + '/cfg'][()].decode('utf8')
    except Exception:
        names = None
        cfg = None

    if cfg is not None:  # we're looking for the energy calibration values...
        from PyMca5.PyMca import ConfigDict
        config = ConfigDict.ConfigDict()
        config.read(cfg)
        cfg = [
            config['detector']['zero'], config['detector']['gain'],
            config['fit']['energy'][0]
        ]  #zero, gain, E_Rayleigh

    f.close()

    return spe, names, cfg
Exemplo n.º 5
0
def plot(data,
         labels=None,
         cfg=None,
         xrange=None,
         yrange=None,
         normtochan=None,
         savename='plot.png'):
    data = np.array(data)  #expected dimensions: [N, channels]
    if len(data.shape) == 1:
        data = np.reshape((1, data.shape[0]))
    elif len(data.shape) >= 3:
        print("Error: expected data dimensions: [N, channels]; ", data.shape)
        return

    if cfg is not None:  # we're looking for the energy calibration values...
        from PyMca5.PyMca import ConfigDict
        config = ConfigDict.ConfigDict()
        config.read(cfg)
        cfg = [
            config['detector']['zero'], config['detector']['gain'],
            config['fit']['energy'][0]
        ]  #zero, gain, E_Rayleigh
        names = []
        peaks = list(config['peaks'].keys())
        for peak in peaks:
            for linetype in config['peaks'][peak]:
                names.append(peak + ' ' + str(linetype))

    plt.figure(figsize=(20, 15))
    for j in range(0, data.shape[0]):
        spe = data[j, :]
        if labels is None:
            lbl = 'spectrum ' + str(j)
        else:
            lbl = labels[j]
        if normtochan is not None:
            spe = spe[:] / spe[normtochan]
        if cfg is None:
            zero = 0.
            gain = 1.
            xtitle = "Channel Number"
        else:
            zero = cfg[0]
            gain = cfg[1]
            xtitle = "Energy [keV]"
        # plot the spectrum, Ylog, X-axis converted to Energy (keV) if PyMca cfg provided
        if xrange is None:
            xrange = (zero, (spe.shape[0] - 1) * gain + zero)
        plt.plot(np.linspace(0, spe.shape[0] - 1, num=spe.shape[0]) * gain +
                 zero,
                 spe,
                 label=lbl,
                 linestyle='-')

    ax = plt.gca()
    handles, labels = ax.get_legend_handles_labels()
    plt.yscale('log')
    plt.xlim(xrange)
    if yrange is not None:
        plt.ylim(yrange)
    plt.legend(handles, labels, loc='best')
    ax.set_xlabel(xtitle, fontsize=16)
    ax.set_ylabel("Intensity [counts]", fontsize=16)
    ax.tick_params(axis='both', which='major', labelsize=14)
    ax.tick_params(axis='x', which='minor', bottom=True)

    # add peak annotation if names and cfg provided
    if cfg is not None:
        # x axis of plot is in Energy (keV)
        # determine peak energy values (Rayl energy = cfg[2])
        from PyMca5.PyMcaPhysics.xrf import Elements
        for n in names:
            if n != 'Rayl' and n != 'Compt':
                if n.split(" ")[1][0] == 'K':
                    energy = Elements.getxrayenergy(
                        n.split(" ")[0],
                        n.split(" ")[1] + 'L3')  #Ka1
                elif n.split(" ")[1][0] == 'L':
                    energy = Elements.getxrayenergy(
                        n.split(" ")[0],
                        n.split(" ")[1] + '3M5')  #La1
                elif n.split(" ")[1][0] == 'M':
                    energy = Elements.getxrayenergy(
                        n.split(" ")[0],
                        n.split(" ")[1] + '5N7')  #Ma1
            elif n == 'Rayl':
                energy = cfg[2]
            else:
                energy = None
            # if energy not None, plot label at this energy value
            if energy is not None:
                idx = max(np.where(handles[0].get_xdata() <= energy)[-1])
                yval = 10**(
                    np.log10(max([hand.get_ydata()[idx]
                                  for hand in handles])) * 1.025)
                # plot the text label X% above this value
                plt.text(energy,
                         yval,
                         n,
                         horizontalalignment='center',
                         fontsize=14)

    plt.show()

    plt.savefig(savename)  #, bbox_inches='tight', pad_inches=0)
    plt.close()