<commit_before>#!/usr/bin/env python

import sys
import os
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from scipy.io import netcdf

import numpy as np

from Tkinter import *
from tkFileDialog import askdirectory
import ttk

def read_ncdf(file,vars=[]):
    try:
        f=netcdf.netcdf_file(file,'r')
    except IOError:
        print('Error: Cannot open file'+file)
        return 0

    if vars == []: vars=f.variables.keys()
    d=dict((v,f.variables[v].data) for v in vars)
    f.close()
    return d

class spectra:

    def __init__(self,dir):
        runid=os.path.basename(os.path.normpath(dir))
        self.has_spectra=os.path.isfile(dir+runid+'_spectra.cdf')

        if self.has_spectra:
            spec = read_ncdf(dir+runid+'_spectra.cdf')
            self.lam = spec['lambda']
            self.brems = spec['brems']
            self.fida = spec['fida']
            self.full = spec['full']
            self.half = spec['half']
            self.third = spec['third']
            self.halo = spec['halo']

            self.channels=dict(('Channel '+str(i+1),i) for i in range(0,self.brems.shape[0]))
            self.wl_min = np.min(self.lam)
            self.wl_max = np.max(self.lam)
            self.dlam=np.abs(self.lam[1]-self.lam[0])

            self.chan = StringVar(value='Channel 1')
            self.nbi_on = BooleanVar(value=False)
            self.fida_on = BooleanVar(value=False)
            self.brems_on = BooleanVar(value=True)
            self.legend_on = BooleanVar(value=True)

            if sum(self.full[0,:]) != 0: 
                self.nbi_on.set(True)

            if sum(self.fida[0,:]) != 0:
                self.fida_on.set(True)

    def plot_spectra(self,fig,canvas):
        if self.has_spectra:
            ch=self.channels[self.chan.get()]
            lam=self.lam
            if self.brems_on.get():
                brems=self.brems[ch,:]
            else:
                brems=0.0

            fig.clf()
            ax=fig.add_subplot(111)
            if self.nbi_on.get():
                full=self.full[ch,:]+brems
                half=self.half[ch,:]+brems
                third=self.third[ch,:]+brems
                halo=self.halo[ch,:]+brems

                ax.plot(lam,full,label='Full')
                ax.plot(lam,half,label='Half')
                ax.plot(lam,third,label='Third')
                ax.plot(lam,halo,label='Halo')

            if self.fida_on.get():
                fida=self.fida[ch,:]+brems
                ax.plot(lam,fida,label='Fida')


            if self.fida_on.get() or self.nbi_on.get():
                if self.legend_on.get(): ax.legend()
                ax.set_yscale('log')
                ax.set_xlabel('Wavelength [nm]')
                ax.set_ylabel('Ph/(s*nm*sr*m^2)')
                ax.set_title(self.chan.get())
                canvas.show()
            else: print('SPECTRA: No Spectra Selected')

        else: print('SPECTRA: No file')

    def plot_intensity(self,fig,canvas):
        if self.has_spectra:
            w1 = self.lam >= self.wl_min
            w2 = self.lam <= self.wl_max
            w=np.logical_and(w1,w2)
            intens=np.sum(self.fida[:,w],axis=1)*self.dlam
            ch=range(1,len(intens)+1)
            fig.clf()
            ax=fig.add_subplot(111)
            ax.plot(ch,intens)
            ax.set_title('FIDA Intensity vs. Channel')
            ax.set_ylabel('Ph/(s*sr*m^2)')
            ax.set_xlabel('Channel Number')
            ax.set_yscale('log')
            canvas.show()
        else: print('SPECTRA: No file')

class npa:

    def __init__(self,dir):
        runid=os.path.basename(os.path.normpath(dir))

        self._has_npa=os.path.isfile(dir+runid+'_npa.cdf')
        self._has_wght=os.path.isfile(dir+runid+'_npa_weight_function.cdf')
        self._has_neut=os.path.isfile(dir+runid+'_neutrals.cdf')
        self._has_geo=os.path.isfile(dir+runid+'_inputs.cdf')

        if self._has_npa:
            npa = read_ncdf(dir+runid+'_npa.cdf')
            self.npa_energy=npa['energy']
            self.npa_flux=npa['flux']
            self.ipos=npa['ipos']
            self.counts=npa['counts']
        if self._has_wght:
            wght = read_ncdf(dir+runid+'_npa_weight_function.cdf')
            self.w_energy=wght['energy']
            self.w_flux=wght['flux']
        if self._has_neut:
            neut = read_ncdf(dir+runid+'_neutrals.cdf')
            self.dens=neut['fdens'].sum(0).sum(0)+neut['hdens'].sum(0).sum(0)+\
                      neut['tdens'].sum(0).sum(0)+neut['halodens'].sum(0).sum(0)
        if self._has_geo:
            geo = read_ncdf(dir+runid+'_inputs.cdf',vars=['x_grid','y_grid','xlos','ylos','xlens','ylens','chan_id'])
            self.x_grid=geo['x_grid']
            self.y_grid=geo['y_grid']
            chan_id=geo['chan_id']
            w = chan_id == 1
            self.xlos=geo['xlos'][w]
            self.ylos=geo['ylos'][w]
            self.xlens=geo['xlens'][w]
            self.ylens=geo['ylens'][w]

        if (self._has_npa or self._has_wght):
            self.channels=dict(('Channel '+str(i+1),i) for i in range(0,len(self.npa_flux[:,0])))
        
        self.chan=StringVar(value='Channel 1')

    def plot_neutral_birth(self,fig,canvas):
        if self._has_npa:
            fig.clf()
            ax=fig.add_subplot(111)
            ch=self.channels[self.chan.get()]
            if self._has_neut and self._has_geo:
                ax.plot(self.x_grid[0,:,:],self.y_grid[0,:,:],'k,')
                ax.contour(self.x_grid[0,:,:],self.y_grid[0,:,:],self.dens,20)
                ax.plot([self.xlos[ch],self.xlens[ch]],[self.ylos[ch],self.ylens[ch]],'k')
            ax.plot(self.ipos[ch,0,0:self.counts[ch]],self.ipos[ch,1,0:self.counts[ch]],'k,',alpha=.3)            
            ax.set_title('Neutral Birth Position')
            ax.set_xlim(min(self.x_grid[0,0,:]),max(self.x_grid[0,0,:]))
            ax.set_ylim(min(self.y_grid[0,:,0]),max(self.y_grid[0,:,0]))
            ax.set_xlabel('x [cm]')
            ax.set_ylabel('y [cm]')
            canvas.show()
        else: print('NPA: No file')

    def plot_flux(self,fig,canvas):
        if self._has_npa or self._has_wght:
            fig.clf()
            ax=fig.add_subplot(111)
            ch=self.channels[self.chan.get()]
            if self._has_npa:
                ax.step(self.npa_energy,self.npa_flux[ch,:],label='MC Flux')
            if self._has_wght:
                ax.plot(self.w_energy,self.w_flux[ch,:],label='WF Flux')

            ax.legend()
            ax.set_title('Neutral Flux: '+self.chan.get())
            ax.set_ylabel('Flux')
            ax.set_xlabel('Energy [keV]')
            canvas.show()
        else: print('NPA: No file')

class weights:

    def __init__(self,dir):
        runid=os.path.basename(os.path.normpath(dir))
        self._has_npa_wght=os.path.isfile(dir+runid+'_npa_weight_function.cdf')
        self._has_fida_wght=os.path.isfile(dir+runid+'_fida_weight_function.cdf')

        if self._has_fida_wght:
            fida=read_ncdf(dir+runid+'_fida_weight_function.cdf')
            self.f_energy=fida['energy']
            self.f_pitch=fida['pitch']
            self.lam=fida['lambda']
            self.dlam=np.abs(self.lam[1]-self.lam[0])
            self.wl_max=np.max(self.lam)
            self.wl_min=np.min(self.lam)
            self.f_rad=fida['radius']
            self.f_wght=fida['wfunct']
            self.f_chan=len(self.f_rad)
            self.fida_chans=dict(('Channel '+str(i+1),i) for i in range(0,self.f_chan))

        if self._has_npa_wght:
            npa=read_ncdf(dir+runid+'_npa_weight_function.cdf')
            self.n_energy=npa['energy']
            self.n_pitch=npa['pitch']
            self.n_wght=npa['wfunct']
            self.n_rad=npa['radius']
            self.n_chan=len(self.n_rad)
            self.npa_chans=dict(('Channel '+str(i+1),i) for i in range(0,self.n_chan))
 
        self.lam_val=DoubleVar(value=655.0)
        self.fida_chan=StringVar(value='Channel 1')
        self.npa_chan=StringVar(value='Channel 1')
        
    def plot_npa_weights(self,fig,canvas):
        if self._has_npa_wght:
            ch=self.npa_chans[self.npa_chan.get()]
            fig.clf()
            ax=fig.add_subplot(111)
            c=ax.contourf(self.n_energy,self.n_pitch,self.n_wght[ch,:,:],50)
            fig.colorbar(c)
            ax.set_title('NPA Weight')
            ax.set_ylabel('Pitch')
            ax.set_xlabel('Energy [keV]')
            canvas.show()

    def plot_fida_weights(self,fig,canvas):
        if self._has_fida_wght:
            ch=self.fida_chans[self.fida_chan.get()]
            wl=float(self.lam_val.get())
            ind=np.argmin(np.abs(self.lam-wl))
            fig.clf()
            ax=fig.add_subplot(111)
            c=ax.contourf(self.f_energy,self.f_pitch,self.f_wght[ch,:,:,ind],30)
            fig.colorbar(c)
            ax.set_xlabel('Energy [keV]')
            ax.set_ylabel('Pitch')
            ax.set_title('FIDA Weight')
            canvas.show()

class neutrals:

    def __init__(self,dir):
        runid=os.path.basename(os.path.normpath(dir))
        self._has_neut=os.path.isfile(dir+runid+'_neutrals.cdf')
        self._has_geo=os.path.isfile(dir+runid+'_inputs.cdf')
      
        if self._has_neut and self._has_geo:
            neut=read_ncdf(dir+runid+'_neutrals.cdf')
            geo=read_ncdf(dir+runid+'_inputs.cdf',vars=['x_grid','y_grid','z_grid','u_grid','v_grid','w_grid'])
            self.fdens=neut['fdens'].sum(0)
            self.hdens=neut['hdens'].sum(0)
            self.tdens=neut['tdens'].sum(0)
            self.halodens=neut['halodens'].sum(0)
            self.x_grid=geo['x_grid']
            self.y_grid=geo['y_grid']
            self.z_grid=geo['z_grid']
            self.u_grid=geo['u_grid']
            self.v_grid=geo['v_grid']
            self.w_grid=geo['w_grid']

        ##Radio Buttons Variable
        self.plot_type=StringVar(value='XY')

        ##Checkbox Variables
        self.use_uvw = BooleanVar(value=False)
        self.full_on = BooleanVar(value=True)
        self.half_on = BooleanVar(value=True)
        self.third_on = BooleanVar(value=True)
        self.halo_on = BooleanVar(value=True)

    def plot_neutrals(self,fig,canvas):
        full_on = self.full_on.get()
        half_on = self.half_on.get()
        third_on = self.third_on.get()
        halo_on = self.halo_on.get()
        torf=lambda T: 1 if T else 0
        if (self._has_neut and self._has_geo) and (full_on or half_on or third_on or halo_on):
            fig.clf()
            ax=fig.add_subplot(111)
            pt=self.plot_type.get()
            if pt == 'X':
                if self.use_uvw.get(): 
                    x=self.u_grid[0,0,:]
                    ax.set_xlabel('U [cm]')
                else:
                    x=self.x_grid[0,0,:]
                    ax.set_xlabel('X [cm]')
                fdens=self.fdens.sum(0).sum(0)
                hdens=self.hdens.sum(0).sum(0)
                tdens=self.tdens.sum(0).sum(0)
                halodens=self.halodens.sum(0).sum(0)
                if full_on: ax.plot(x,fdens,label='Full')
                if half_on: ax.plot(x,hdens,label='Half')
                if third_on: ax.plot(x,tdens,label='Third')
                if halo_on: ax.plot(x,halodens,label='Halo')
                ax.legend()
                ax.set_title('Neutral Density')
                ax.set_ylabel('Density [cm^-3]')
                canvas.show()
            if pt == 'Y':
                if self.use_uvw.get(): 
                    x=self.v_grid[0,:,0]
                    ax.set_xlabel('V [cm]')
                else:
                    x=self.y_grid[0,:,0]
                    ax.set_xlabel('Y [cm]')
                fdens=self.fdens.sum(0).sum(1)
                hdens=self.hdens.sum(0).sum(1)
                tdens=self.tdens.sum(0).sum(1)
                halodens=self.halodens.sum(0).sum(1)
                if full_on: ax.plot(x,fdens,label='Full')
                if half_on: ax.plot(x,hdens,label='Half')
                if third_on: ax.plot(x,tdens,label='Third')
                if halo_on: ax.plot(x,halodens,label='Halo')
                ax.legend()
                ax.set_title('Neutral Density')
                ax.set_ylabel('Density [cm^-3]')
                canvas.show()
            if pt == 'Z':
                if self.use_uvw.get(): 
                    x=self.w_grid[:,0,0]
                    ax.set_xlabel('W [cm]')
                else:
                    x=self.z_grid[:,0,0]
                    ax.set_xlabel('Z [cm]')
                fdens=self.fdens.sum(1).sum(1)
                hdens=self.hdens.sum(1).sum(1)
                tdens=self.tdens.sum(1).sum(1)
                halodens=self.halodens.sum(1).sum(1)
                if full_on: ax.plot(x,fdens,label='Full')
                if half_on: ax.plot(x,hdens,label='Half')
                if third_on: ax.plot(x,tdens,label='Third')
                if halo_on: ax.plot(x,halodens,label='Halo')
                ax.legend()
                ax.set_title('Neutral Density')
                ax.set_ylabel('Density [cm^-3]')
                canvas.show()
            if pt == 'XY':
                if self.use_uvw.get(): 
                    x=self.u_grid[0,:,:]
                    y=self.v_grid[0,:,:]
                    ax.set_xlabel('U [cm]')
                    ax.set_ylabel('V [cm]')
                else:
                    x=self.x_grid[0,:,:]
                    y=self.y_grid[0,:,:]
                    ax.set_xlabel('X [cm]')
                    ax.set_ylabel('Y [cm]')

                fdens=self.fdens.sum(0)
                hdens=self.hdens.sum(0)
                tdens=self.tdens.sum(0)
                halodens=self.halodens.sum(0)

                dens=fdens*torf(full_on)+hdens*torf(half_on)+tdens*torf(third_on)+halodens*torf(halo_on)
                c=ax.contourf(x,y,dens,50)
                fig.colorbar(c)
                ax.set_title('Neutral Density')
                canvas.show()
            if pt == 'XZ':
                if self.use_uvw.get(): 
                    x=self.u_grid[:,0,:]
                    y=self.w_grid[:,0,:]
                    ax.set_xlabel('U [cm]')
                    ax.set_ylabel('W [cm]')
                else:
                    x=self.x_grid[:,0,:]
                    y=self.z_grid[:,0,:]
                    ax.set_xlabel('X [cm]')
                    ax.set_ylabel('Z [cm]')

                fdens=self.fdens.sum(1)
                hdens=self.hdens.sum(1)
                tdens=self.tdens.sum(1)
                halodens=self.halodens.sum(1)

                dens=fdens*torf(full_on)+hdens*torf(half_on)+tdens*torf(third_on)+halodens*torf(halo_on)
                c=ax.contourf(x,y,dens,50)
                fig.colorbar(c)
                ax.set_title('Neutral Density')
                canvas.show()
            if pt == 'YZ':
                if self.use_uvw.get(): 
                    x=self.v_grid[:,:,0]
                    y=self.w_grid[:,:,0]
                    ax.set_xlabel('V [cm]')
                    ax.set_ylabel('W [cm]')
                else:
                    x=self.y_grid[:,:,0]
                    y=self.z_grid[:,:,0]
                    ax.set_xlabel('Y [cm]')
                    ax.set_ylabel('Z [cm]')

                fdens=self.fdens.sum(2)
                hdens=self.hdens.sum(2)
                tdens=self.tdens.sum(2)
                halodens=self.halodens.sum(2)

                dens=fdens*torf(full_on)+hdens*torf(half_on)+tdens*torf(third_on)+halodens*torf(halo_on)
                c=ax.contourf(x,y,dens,50)
                fig.colorbar(c)
                ax.set_title('Neutral Density')
                canvas.show()
                
class viewer:

    def __init__(self,parent):
<commit_msg>a wee bit of documentation<commit_after>#!/usr/bin/env python

import sys
import os
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from scipy.io import netcdf

import numpy as np

from Tkinter import *
from tkFileDialog import askdirectory
import ttk

def read_ncdf(file,vars=[]):
    """ Reads a netCDF file and returns a dict with its variables
    
    Parameters
    ----------
    file : string
        The netCDF file to be read
    vars : string list, optional
        List of variables to be read from file

    Returns
    -------
    d : A python dict containing the files variables

    Examples
    --------
    >>> d=read_ncdf('test.cdf')
    >>> d1=read_ncdf('test.cdf',vars=['var1'])
    >>> d2=read_ncdf('test.cdf',vars=['var1','var2'])

    """
    try:
        f=netcdf.netcdf_file(file,'r')
    except IOError:
        print('Error: Cannot open file'+file)
        return 0

    if vars == []: vars=f.variables.keys()
    d=dict((v,f.variables[v].data) for v in vars)
    f.close()
    return d

class spectra:
    """ Spectra object that contains plot methods and parameters"""
    def __init__(self,dir):
        runid=os.path.basename(os.path.normpath(dir))
        self.has_spectra=os.path.isfile(dir+runid+'_spectra.cdf')

        if self.has_spectra:
            spec = read_ncdf(dir+runid+'_spectra.cdf')
            self.lam = spec['lambda']
            self.brems = spec['brems']
            self.fida = spec['fida']
            self.full = spec['full']
            self.half = spec['half']
            self.third = spec['third']
            self.halo = spec['halo']

            self.channels=dict(('Channel '+str(i+1),i) for i in range(0,self.brems.shape[0]))
            self.wl_min = np.min(self.lam)
            self.wl_max = np.max(self.lam)
            self.dlam=np.abs(self.lam[1]-self.lam[0])

            self.chan = StringVar(value='Channel 1')
            self.nbi_on = BooleanVar(value=False)
            self.fida_on = BooleanVar(value=False)
            self.brems_on = BooleanVar(value=True)
            self.legend_on = BooleanVar(value=True)

            if sum(self.full[0,:]) != 0: 
                self.nbi_on.set(True)

            if sum(self.fida[0,:]) != 0:
                self.fida_on.set(True)

    def plot_spectra(self,fig,canvas):
        if self.has_spectra:
            ch=self.channels[self.chan.get()]
            lam=self.lam
            if self.brems_on.get():
                brems=self.brems[ch,:]
            else:
                brems=0.0

            fig.clf()
            ax=fig.add_subplot(111)
            if self.nbi_on.get():
                full=self.full[ch,:]+brems
                half=self.half[ch,:]+brems
                third=self.third[ch,:]+brems
                halo=self.halo[ch,:]+brems

                ax.plot(lam,full,label='Full')
                ax.plot(lam,half,label='Half')
                ax.plot(lam,third,label='Third')
                ax.plot(lam,halo,label='Halo')

            if self.fida_on.get():
                fida=self.fida[ch,:]+brems
                ax.plot(lam,fida,label='Fida')


            if self.fida_on.get() or self.nbi_on.get():
                if self.legend_on.get(): ax.legend()
                ax.set_yscale('log')
                ax.set_xlabel('Wavelength [nm]')
                ax.set_ylabel('Ph/(s*nm*sr*m^2)')
                ax.set_title(self.chan.get())
                canvas.show()
            else: print('SPECTRA: No Spectra Selected')

        else: print('SPECTRA: No file')

    def plot_intensity(self,fig,canvas):
        if self.has_spectra:
            w1 = self.lam >= self.wl_min
            w2 = self.lam <= self.wl_max
            w=np.logical_and(w1,w2)
            intens=np.sum(self.fida[:,w],axis=1)*self.dlam
            ch=range(1,len(intens)+1)
            fig.clf()
            ax=fig.add_subplot(111)
            ax.plot(ch,intens)
            ax.set_title('FIDA Intensity vs. Channel')
            ax.set_ylabel('Ph/(s*sr*m^2)')
            ax.set_xlabel('Channel Number')
            ax.set_yscale('log')
            canvas.show()
        else: print('SPECTRA: No file')

class npa:
    """ NPA object that contains plot methods and parameters"""
    def __init__(self,dir):
        runid=os.path.basename(os.path.normpath(dir))

        self._has_npa=os.path.isfile(dir+runid+'_npa.cdf')
        self._has_wght=os.path.isfile(dir+runid+'_npa_weight_function.cdf')
        self._has_neut=os.path.isfile(dir+runid+'_neutrals.cdf')
        self._has_geo=os.path.isfile(dir+runid+'_inputs.cdf')

        if self._has_npa:
            npa = read_ncdf(dir+runid+'_npa.cdf')
            self.npa_energy=npa['energy']
            self.npa_flux=npa['flux']
            self.ipos=npa['ipos']
            self.counts=npa['counts']
        if self._has_wght:
            wght = read_ncdf(dir+runid+'_npa_weight_function.cdf')
            self.w_energy=wght['energy']
            self.w_flux=wght['flux']
        if self._has_neut:
            neut = read_ncdf(dir+runid+'_neutrals.cdf')
            self.dens=neut['fdens'].sum(0).sum(0)+neut['hdens'].sum(0).sum(0)+\
                      neut['tdens'].sum(0).sum(0)+neut['halodens'].sum(0).sum(0)
        if self._has_geo:
            geo = read_ncdf(dir+runid+'_inputs.cdf',vars=['x_grid','y_grid','xlos','ylos','xlens','ylens','chan_id'])
            self.x_grid=geo['x_grid']
            self.y_grid=geo['y_grid']
            chan_id=geo['chan_id']
            w = chan_id == 1
            self.xlos=geo['xlos'][w]
            self.ylos=geo['ylos'][w]
            self.xlens=geo['xlens'][w]
            self.ylens=geo['ylens'][w]

        if (self._has_npa or self._has_wght):
            self.channels=dict(('Channel '+str(i+1),i) for i in range(0,len(self.npa_flux[:,0])))
        
        self.chan=StringVar(value='Channel 1')

    def plot_neutral_birth(self,fig,canvas):
        if self._has_npa:
            fig.clf()
            ax=fig.add_subplot(111)
            ch=self.channels[self.chan.get()]
            if self._has_neut and self._has_geo:
                ax.plot(self.x_grid[0,:,:],self.y_grid[0,:,:],'k,')
                ax.contour(self.x_grid[0,:,:],self.y_grid[0,:,:],self.dens,20)
                ax.plot([self.xlos[ch],self.xlens[ch]],[self.ylos[ch],self.ylens[ch]],'k')
            ax.plot(self.ipos[ch,0,0:self.counts[ch]],self.ipos[ch,1,0:self.counts[ch]],'k,',alpha=.3)            
            ax.set_title('Neutral Birth Position')
            ax.set_xlim(min(self.x_grid[0,0,:]),max(self.x_grid[0,0,:]))
            ax.set_ylim(min(self.y_grid[0,:,0]),max(self.y_grid[0,:,0]))
            ax.set_xlabel('x [cm]')
            ax.set_ylabel('y [cm]')
            canvas.show()
        else: print('NPA: No file')

    def plot_flux(self,fig,canvas):
        if self._has_npa or self._has_wght:
            fig.clf()
            ax=fig.add_subplot(111)
            ch=self.channels[self.chan.get()]
            if self._has_npa:
                ax.step(self.npa_energy,self.npa_flux[ch,:],label='MC Flux')
            if self._has_wght:
                ax.plot(self.w_energy,self.w_flux[ch,:],label='WF Flux')

            ax.legend()
            ax.set_title('Neutral Flux: '+self.chan.get())
            ax.set_ylabel('Flux')
            ax.set_xlabel('Energy [keV]')
            canvas.show()
        else: print('NPA: No file')

class weights:
    """ Weights object that contains plot methods and parameters"""
    def __init__(self,dir):
        runid=os.path.basename(os.path.normpath(dir))
        self._has_npa_wght=os.path.isfile(dir+runid+'_npa_weight_function.cdf')
        self._has_fida_wght=os.path.isfile(dir+runid+'_fida_weight_function.cdf')

        if self._has_fida_wght:
            fida=read_ncdf(dir+runid+'_fida_weight_function.cdf')
            self.f_energy=fida['energy']
            self.f_pitch=fida['pitch']
            self.lam=fida['lambda']
            self.dlam=np.abs(self.lam[1]-self.lam[0])
            self.wl_max=np.max(self.lam)
            self.wl_min=np.min(self.lam)
            self.f_rad=fida['radius']
            self.f_wght=fida['wfunct']
            self.f_chan=len(self.f_rad)
            self.fida_chans=dict(('Channel '+str(i+1),i) for i in range(0,self.f_chan))

        if self._has_npa_wght:
            npa=read_ncdf(dir+runid+'_npa_weight_function.cdf')
            self.n_energy=npa['energy']
            self.n_pitch=npa['pitch']
            self.n_wght=npa['wfunct']
            self.n_rad=npa['radius']
            self.n_chan=len(self.n_rad)
            self.npa_chans=dict(('Channel '+str(i+1),i) for i in range(0,self.n_chan))
 
        self.lam_val=DoubleVar(value=655.0)
        self.fida_chan=StringVar(value='Channel 1')
        self.npa_chan=StringVar(value='Channel 1')
        
    def plot_npa_weights(self,fig,canvas):
        if self._has_npa_wght:
            ch=self.npa_chans[self.npa_chan.get()]
            fig.clf()
            ax=fig.add_subplot(111)
            c=ax.contourf(self.n_energy,self.n_pitch,self.n_wght[ch,:,:],50)
            fig.colorbar(c)
            ax.set_title('NPA Weight')
            ax.set_ylabel('Pitch')
            ax.set_xlabel('Energy [keV]')
            canvas.show()

    def plot_fida_weights(self,fig,canvas):
        if self._has_fida_wght:
            ch=self.fida_chans[self.fida_chan.get()]
            wl=float(self.lam_val.get())
            ind=np.argmin(np.abs(self.lam-wl))
            fig.clf()
            ax=fig.add_subplot(111)
            c=ax.contourf(self.f_energy,self.f_pitch,self.f_wght[ch,:,:,ind],30)
            fig.colorbar(c)
            ax.set_xlabel('Energy [keV]')
            ax.set_ylabel('Pitch')
            ax.set_title('FIDA Weight')
            canvas.show()

class neutrals:
    """ Neutrals object that contains plot methods and parameters"""
    def __init__(self,dir):
        runid=os.path.basename(os.path.normpath(dir))
        self._has_neut=os.path.isfile(dir+runid+'_neutrals.cdf')
        self._has_geo=os.path.isfile(dir+runid+'_inputs.cdf')
      
        if self._has_neut and self._has_geo:
            neut=read_ncdf(dir+runid+'_neutrals.cdf')
            geo=read_ncdf(dir+runid+'_inputs.cdf',vars=['x_grid','y_grid','z_grid','u_grid','v_grid','w_grid'])
            self.fdens=neut['fdens'].sum(0)
            self.hdens=neut['hdens'].sum(0)
            self.tdens=neut['tdens'].sum(0)
            self.halodens=neut['halodens'].sum(0)
            self.x_grid=geo['x_grid']
            self.y_grid=geo['y_grid']
            self.z_grid=geo['z_grid']
            self.u_grid=geo['u_grid']
            self.v_grid=geo['v_grid']
            self.w_grid=geo['w_grid']

        ##Radio Buttons Variable
        self.plot_type=StringVar(value='XY')

        ##Checkbox Variables
        self.use_uvw = BooleanVar(value=False)
        self.full_on = BooleanVar(value=True)
        self.half_on = BooleanVar(value=True)
        self.third_on = BooleanVar(value=True)
        self.halo_on = BooleanVar(value=True)

    def plot_neutrals(self,fig,canvas):
        full_on = self.full_on.get()
        half_on = self.half_on.get()
        third_on = self.third_on.get()
        halo_on = self.halo_on.get()
        torf=lambda T: 1 if T else 0
        if (self._has_neut and self._has_geo) and (full_on or half_on or third_on or halo_on):
            fig.clf()
            ax=fig.add_subplot(111)
            pt=self.plot_type.get()
            if pt == 'X':
                if self.use_uvw.get(): 
                    x=self.u_grid[0,0,:]
                    ax.set_xlabel('U [cm]')
                else:
                    x=self.x_grid[0,0,:]
                    ax.set_xlabel('X [cm]')
                fdens=self.fdens.sum(0).sum(0)
                hdens=self.hdens.sum(0).sum(0)
                tdens=self.tdens.sum(0).sum(0)
                halodens=self.halodens.sum(0).sum(0)
                if full_on: ax.plot(x,fdens,label='Full')
                if half_on: ax.plot(x,hdens,label='Half')
                if third_on: ax.plot(x,tdens,label='Third')
                if halo_on: ax.plot(x,halodens,label='Halo')
                ax.legend()
                ax.set_title('Neutral Density')
                ax.set_ylabel('Density [cm^-3]')
                canvas.show()
            if pt == 'Y':
                if self.use_uvw.get(): 
                    x=self.v_grid[0,:,0]
                    ax.set_xlabel('V [cm]')
                else:
                    x=self.y_grid[0,:,0]
                    ax.set_xlabel('Y [cm]')
                fdens=self.fdens.sum(0).sum(1)
                hdens=self.hdens.sum(0).sum(1)
                tdens=self.tdens.sum(0).sum(1)
                halodens=self.halodens.sum(0).sum(1)
                if full_on: ax.plot(x,fdens,label='Full')
                if half_on: ax.plot(x,hdens,label='Half')
                if third_on: ax.plot(x,tdens,label='Third')
                if halo_on: ax.plot(x,halodens,label='Halo')
                ax.legend()
                ax.set_title('Neutral Density')
                ax.set_ylabel('Density [cm^-3]')
                canvas.show()
            if pt == 'Z':
                if self.use_uvw.get(): 
                    x=self.w_grid[:,0,0]
                    ax.set_xlabel('W [cm]')
                else:
                    x=self.z_grid[:,0,0]
                    ax.set_xlabel('Z [cm]')
                fdens=self.fdens.sum(1).sum(1)
                hdens=self.hdens.sum(1).sum(1)
                tdens=self.tdens.sum(1).sum(1)
                halodens=self.halodens.sum(1).sum(1)
                if full_on: ax.plot(x,fdens,label='Full')
                if half_on: ax.plot(x,hdens,label='Half')
                if third_on: ax.plot(x,tdens,label='Third')
                if halo_on: ax.plot(x,halodens,label='Halo')
                ax.legend()
                ax.set_title('Neutral Density')
                ax.set_ylabel('Density [cm^-3]')
                canvas.show()
            if pt == 'XY':
                if self.use_uvw.get(): 
                    x=self.u_grid[0,:,:]
                    y=self.v_grid[0,:,:]
                    ax.set_xlabel('U [cm]')
                    ax.set_ylabel('V [cm]')
                else:
                    x=self.x_grid[0,:,:]
                    y=self.y_grid[0,:,:]
                    ax.set_xlabel('X [cm]')
                    ax.set_ylabel('Y [cm]')

                fdens=self.fdens.sum(0)
                hdens=self.hdens.sum(0)
                tdens=self.tdens.sum(0)
                halodens=self.halodens.sum(0)

                dens=fdens*torf(full_on)+hdens*torf(half_on)+tdens*torf(third_on)+halodens*torf(halo_on)
                c=ax.contourf(x,y,dens,50)
                fig.colorbar(c)
                ax.set_title('Neutral Density')
                canvas.show()
            if pt == 'XZ':
                if self.use_uvw.get(): 
                    x=self.u_grid[:,0,:]
                    y=self.w_grid[:,0,:]
                    ax.set_xlabel('U [cm]')
                    ax.set_ylabel('W [cm]')
                else:
                    x=self.x_grid[:,0,:]
                    y=self.z_grid[:,0,:]
                    ax.set_xlabel('X [cm]')
                    ax.set_ylabel('Z [cm]')

                fdens=self.fdens.sum(1)
                hdens=self.hdens.sum(1)
                tdens=self.tdens.sum(1)
                halodens=self.halodens.sum(1)

                dens=fdens*torf(full_on)+hdens*torf(half_on)+tdens*torf(third_on)+halodens*torf(halo_on)
                c=ax.contourf(x,y,dens,50)
                fig.colorbar(c)
                ax.set_title('Neutral Density')
                canvas.show()
            if pt == 'YZ':
                if self.use_uvw.get(): 
                    x=self.v_grid[:,:,0]
                    y=self.w_grid[:,:,0]
                    ax.set_xlabel('V [cm]')
                    ax.set_ylabel('W [cm]')
                else:
                    x=self.y_grid[:,:,0]
                    y=self.z_grid[:,:,0]
                    ax.set_xlabel('Y [cm]')
                    ax.set_ylabel('Z [cm]')

                fdens=self.fdens.sum(2)
                hdens=self.hdens.sum(2)
                tdens=self.tdens.sum(2)
                halodens=self.halodens.sum(2)

                dens=fdens*torf(full_on)+hdens*torf(half_on)+tdens*torf(third_on)+halodens*torf(halo_on)
                c=ax.contourf(x,y,dens,50)
                fig.colorbar(c)
                ax.set_title('Neutral Density')
                canvas.show()
                
class viewer:
    """Class that contains FIDAsim result viewer window"""
    def __init__(self,parent):

{'fraction_non_alphanumeric': 0.15091159797763137, 'fraction_numerical': 0.01038762065267351, 'mean_word_length': 1.7399882461590126, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1298934', 'n_tokens_mistral': 12068, 'n_tokens_neox': 11491, 'n_words': 1368}