Commit 95f0a01e authored by Zolnierczuk, Piotr's avatar Zolnierczuk, Piotr
Browse files

updates to xyz plots

new option --details
parent 35f04586
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -355,7 +355,7 @@ def add_xyz_options(subparser, parents=None):
    pars = subparser.add_parser('xyz', parents=parents,
                      help='xyz plot (.dat or .h5 files)',
                      description='create xyz analysis plot')
    pars.set_defaults(overwrite=False, qmin=0, qmax=np.inf, deltaq=0.001)
    pars.set_defaults(overwrite=False, qmin=0, qmax=np.inf, deltaq=0.005, details=False)
    pars.add_argument('file', metavar='filename', help='file to process', nargs='+')
    pars.add_argument('--qmin', dest='qmin', type=float,
                      help='qmin (default=%(default)s)')
@@ -363,6 +363,8 @@ def add_xyz_options(subparser, parents=None):
                      help='qmax (default=%(default)s)')
    pars.add_argument('--delta-q', dest='deltaq', type=float,
                      help='q histogram bin witdth')
    pars.add_argument('--details', dest='details', action='store_true',
                      help='print more details of xyz analysis')

def add_diffrun_options(subparser, parents=None):
    "diffrun options"
+54 −19
Original line number Diff line number Diff line
@@ -139,7 +139,7 @@ def plot_xyz_single(hdfile, fig, axis, axis1=None, **kwargs):



def xyz_analysis(results, axis, norm=False, output=None):
def xyz_analysis(results, axis, norm=False, output=None, details=False):
    "XYZ decomposition"

    pcha = []
@@ -148,23 +148,42 @@ def xyz_analysis(results, axis, norm=False, output=None):
    pcha = np.average(np.asarray(pcha))
    if norm:
        pcha = 1

    cnorm    = {}
    cdetails = {}

    for key, val in results.items():
        cnorm[key] = val['counts']/val['proton_charge']*pcha
    #SX = cnorm['x_up']+cnorm['x_dn']
    #SY = cnorm['y_up']+cnorm['y_dn']
    #SZ = cnorm['z_up']+cnorm['z_dn']
    #SAV = (SX+SY+SZ)/3
    DX = cnorm['x_up']-cnorm['x_dn']
    DY = cnorm['y_up']-cnorm['y_dn']
    DZ = cnorm['z_up']-cnorm['z_dn']
    M  = 2*DZ - (DX + DY)
    #cnorm['sx']  = SX
    #cnorm['sy']  = SY
    #cnorm['sz']  = SZ
    #cnorm['sav'] = SAV/2
    cnorm['mag'] = M

    sX  = cnorm['x_up'] + cnorm['x_dn']
    sY  = cnorm['y_up'] + cnorm['y_dn']
    sZ  = cnorm['z_up'] + cnorm['z_dn']
    sUP = cnorm['x_up'] + cnorm['y_up'] + cnorm['z_up']
    sDN = cnorm['x_dn'] + cnorm['y_dn'] + cnorm['z_dn']
    #
    dX  = cnorm['x_up'] - cnorm['x_dn']
    dY  = cnorm['y_up'] - cnorm['y_dn']
    dZ  = cnorm['z_up'] - cnorm['z_dn']
    #
    sM_UP  = +2*cnorm['z_up'] - (cnorm['x_up'] + cnorm['y_up'])
    sM_DN  = -2*cnorm['z_dn'] + (cnorm['x_dn'] + cnorm['y_dn'])
    sTOT= (sUP + sDN)/3
    sM  =  2*dZ - (dX + dY)
    sN  = (2*sUP - sDN)/6
    sI  = sTOT - sN - sM

    cnorm['m_mag'] = sM
    cnorm['n_coh'] = sN
    cnorm['i_inc'] = sI

    cdetails['sum_ave'] = sTOT
    cdetails['sum_x'  ] = sX
    cdetails['sum_y'  ] = sY
    cdetails['sum_z'  ] = sZ
    #cdetails['m_ave'  ] = sM_UP + sM_DN
    cdetails['m_up/2']  = sM_UP
    cdetails['m_dn/2']  = sM_DN


    keys = list(cnorm.keys())
    vals = np.asarray(list(cnorm.values()))
    axis.step(keys, vals, where='mid')
@@ -172,21 +191,36 @@ def xyz_analysis(results, axis, norm=False, output=None):

    # fixing yticks with matplotlib.ticker "FixedLocator"
    axis.xaxis.set_ticks(range(len(keys)))
    axis.set_xticklabels(keys, rotation=25 , ha='right')
    axis.set_xticklabels(keys, rotation=40 , ha='right')

    out = StringIO()
    out.write("#label   count rate \taverage q \taverage wavelength\n")
    out.write("#label   counts \taverage q \taverage wavelength\n")
    for key, cval in cnorm.items():
        if key.startswith('m_mag'):
            out.write("#summary\n")
        if norm:
            out.write("%-8.8s %10.6f" % (key, cval))
        else:
            out.write("%-8.8s %10.1f" % (key, cval))
        if key != 'mag':
        if key in ('m_mag', 'n_coh', 'i_inc'):
            out.write("\t%5.5s/tot %9.3f%%" % (key, 100*cval/sTOT))
        else:
            vals = results.get(key)
            if vals is not None:
                out.write("\t%6.4f %6.4f" % vals.get('qave'))
                out.write("\t%5.2f %5.2f" % vals.get('lave'))
        out.write('\n')

    if details:
        out.write("\n#details\n")
        keys = list(cdetails.keys())
        vals = np.asarray(list(cdetails.values()))
        for key, cval in cdetails.items():
            if norm:
                out.write("%-8.8s %10.6f\n" % (key, cval))
            else:
                out.write("%-8.8s %10.1f\n" % (key, cval))

    if output:
        with open(output, 'w', encoding='ascii') as fd:
            fd.write(out.getvalue())
@@ -199,6 +233,7 @@ def plot_xyz(*hdfiles, **kwargs):
    "plot xyz"
    normalize = kwargs.pop('normalize', False)
    savefile  = kwargs.pop('savefile', None)
    details   = kwargs.pop('details', False)

    index_map = dict(z_up=(0,0), y_up=(0,1), x_up=(0,2),
                     z_dn=(1,0), y_dn=(1,1), x_dn=(1,2))
@@ -239,7 +274,7 @@ def plot_xyz(*hdfiles, **kwargs):
            res = plot_xyz_single(hdf5file, fig, ax, axis1=axis1, label=label, **kwargs)
            results.update({label:res})
    #
    xyz_analysis(results, ax7, norm=normalize, output=savefile)
    xyz_analysis(results, ax7, norm=normalize, output=savefile, details=details)

    #
    title = title.replace('_', ' ')
+2 −2
Original line number Diff line number Diff line
@@ -3,8 +3,8 @@ PySEN revision module
"""
import sys
__version__  = "1.2"
__release__  = "rc7"
__date__     = "Jan 6, 2023"
__release__  = "rc8"
__date__     = "Jan 20, 2023"

def version(full=False):
    "get pysen version number"