otterpy/JFSphoto.py

234 lines
8.9 KiB
Python

from tkinter.colorchooser import askcolor
from tkinter import filedialog
from tkinter import messagebox
import tkinter as tk
import csv
import numpy as np
from configparser import ConfigParser
####################################### object
class Jfsphoto (object):
def __init__(self):
self.nm = 0
self.nmi = 0
self.nm2 = 0
self.nm2i = 0
self.nm_left = 0
self.nm_right = 0
self.nm_step = 0
self.tnm_left = tk.StringVar()
self.tnm_right = tk.StringVar()
self.tnm_step = tk.StringVar()
self.nmData16 = np.zeros(3694, np.uint16)
########## nm scale checkButton default nm-scale is off
self.nm_checked = tk.IntVar()
self.nm_checked.set(0)
########## darkline
self.darkData16 = np.zeros(3694, np.float32)
self.darkline_checked = tk.IntVar()
self.darkline_checked.set(0)
########## baseline of the lightsource
self.baseData16 = np.zeros(3694, np.float32)
self.baseline_checked = tk.IntVar()
self.baseline_checked.set(0)
self.baseline_start=0
self.baseline_end=0
self.abs_trans= tk.IntVar()
########## set to transition
self.abs_trans.set(1)
def do_calibrate(self):
win = tk.Toplevel()
win.geometry("450x200+100+100")
self.lab1 = tk.Label(win, text='Please enter filenames and nm of the peaks').grid(row=0,column=0,columnspan=6)
########## first peak
self.tnm = tk.StringVar()
self.tnm.set(str(self.nm))
self.tnmi = tk.StringVar()
self.tnmi.set(str(self.nmi))
self.lab2 = tk.Label(win,text='first Peak nm').grid(row=1,column=0)
self.en1 = tk.Entry(win,textvariable=self.tnm, width= 10).grid(row=1,column=1)
self.bt1 = tk.Button(win,text="select File",command=lambda: self.openfile(1)).grid(row=1,column=2)
self.lnm = tk.Label(win,textvariable =self.tnm).grid(row=1,column=3)
self.lnm = tk.Label(win,text =" nm by index ").grid(row=1,column=4)
self.lmi = tk.Label(win,textvariable=self.tnmi).grid(row=1,column=5)
########## second peak
self.tnm2 = tk.StringVar()
self.tnm2.set(str(self.nm2))
self.tnm2i = tk.StringVar()
self.tnm2i.set(str(self.nm2i))
self.lab3 = tk.Label(win,text='second Peak nm').grid(row=2,column=0)
self.en2 = tk.Entry(win,textvariable=self.tnm2, width= 10).grid(row=2,column=1)
self.bt2 = tk.Button(win,text="select File",command=lambda: self.openfile(2)).grid(row=2,column=2)
self.lnm2 = tk.Label(win,textvariable =self.tnm2).grid(row=2,column=3)
self.lnm2 = tk.Label(win,text =" nm by index ").grid(row=2,column=4)
self.lm2i = tk.Label(win,textvariable=self.tnm2i).grid(row=2,column=5)
########### calibration
self.tnm_left.set(str(self.nm_left))
self.tnm_right.set(str(self.nm_right))
self.tnm_step.set(str(self.nm_step))
self.lab4 = tk.Label(win,text="left border [nm]").grid(row=3,column=0)
self.lab5 = tk.Label(win,textvariable=self.tnm_left).grid(row=3,column=1)
self.lab6 = tk.Label(win,text="right border [nm]").grid(row=3,column=2)
self.lab7 = tk.Label(win,textvariable=self.tnm_right).grid(row=3,column=3)
self.lab8 = tk.Label(win,text="[nm]/point").grid(row=3,column=4)
self.lab9 = tk.Label(win,textvariable=self.tnm_step).grid(row=3,column=5)
self.bt3 = tk.Button(win,text="Calibrieren",command=self.calibrate,state=tk.DISABLED)
self.bt3.grid(row=4,column=0)
self.bt4 = tk.Button(win,text="Load Config",command=self.conf_read).grid(row=4,column=1)
self.bt5 = tk.Button(win,text="Save Config",command=self.conf_write).grid(row=4,column=2)
########### dialog modal
win.focus_set()
win.grab_set()
win.wait_window()
def do_msg(self,txt):
self.mroot = tk.Tk()
self.mroot.minsize(100,50)
self.mroot.title(" Info ")
self.label = tk.Label(self.mroot, text=txt,bg="yellow",fg="blue")
self.label.pack()
self.button = tk.Button(self.mroot, text='OK', width=25, command=self.mroot.destroy)
self.button.pack()
self.mroot.mainloop()
############ Darkline
def do_save_darkline(self,dark):
x = np.min(dark)
if (x < 3700):
self.do_msg(" This is not a Darkline ")
return 0
else :
self.darkData16 = dark*1.0
return 1
def get_darkline_checked(self):
return self.darkline_checked.get()
############ Baseline depends on the capacity of the light source block spektralrange if the intensity is to low
def do_save_baseline(self,base):
### no margin at all
doIt = True
#### no left margin
left = True
x = np.max(base)
if (x < 500):
self.do_msg(" This is not a Baseine \n Is the Lightsouce switch on ? ")
return 0
else:
for i in range(0,3694):
if (base[i] < 200) :
if doIt:
if left:
self.baseline_start = i
else:
self.baseline_end = i
doIt = False
self.baseData16[i] = 0
else:
if (base[i] > 300):
left = False
self.baseData16[i] = base[i]*1.0
print(self.baseline_start," ",self.baseline_end)
return 1
def get_baseline_checked(self):
return self.baseline_checked.get()
############ Calibration and Load/Save Configuration Part 2
def checkit(self):
self.nm = int(self.tnm.get())
self.nmi = int(self.tnmi.get())
self.nm2 = int(self.tnm2.get())
self.nm2i = int(self.tnm2i.get())
if ((self.nm > 0) & (self.nmi > 0) & (self.nm2 > 0) & (self.nm2i > 0)):
self.bt3.config(state = tk.NORMAL)
def calibrate(self):
self.nm_step = round((self.nm2 - self.nm)/(self.nm2i-self.nmi),5)
self.nm_left = round(self.nm-(self.nm_step*self.nmi),0)
self.nm_right = round(self.nm2 + self.nm_step*(3694-self.nm2i),0)
self.tnm_left.set(str(self.nm_left))
self.tnm_right.set(str(self.nm_right))
self.tnm_step.set(str(self.nm_step))
def nm_scale_ok(self):
if ((self.nm_left > 0) & (self.nm_right > 0)):
return 1
else:
return 0
def get_nm_checked(self):
if (self.nm_scale_ok()):
return self.nm_checked.get()
else:
return 0
def set_nm_scale(self):
if (self.nm_scale_ok):
self.nmData16 = np.linspace(self.nm_left,self.nm_right,3694)
def get_nm_scale(self):
self.set_nm_scale()
return self.nmData16
def openfile(self,xx):
rxData16 = np.zeros(3694, np.uint16)
filename = filedialog.askopenfilename(defaultextension=".dat", title="Open file")
line_count = 0
try:
with open(filename) as csvfile:
readCSV = csv.reader(csvfile, delimiter=' ')
for row in readCSV:
if (line_count == 3):
self.SHsent = int(row[1])
self.ICGsent = int(row[6])
if (line_count > 3):
rxData16[line_count-4] = int(row[1])
line_count += 1
if (xx==1):
self.nmi = np.argmin(rxData16)
self.tnmi.set(str(self.nmi))
if (xx==2):
self.nm2i = np.argmin(rxData16)
self.tnm2i.set(str(self.nm2i))
self.checkit()
except IOError:
messagebox.showerror("By the great otter!","There's a problem opening the file.")
def conf_write(self):
config = ConfigParser()
config.read('config.ini')
lis = config.sections()
if ('main' not in lis):
config.add_section('main')
config.set('main','nm_left',str(self.nm_left))
config.set('main','nm_right',str(self.nm_right))
config.set('main','nm_step',str(self.nm_step))
with open('config.ini','w') as f:
config.write(f)
def conf_read(self):
config = ConfigParser()
try:
config.read('config.ini')
self.nm_left = float(config.get('main','nm_left',fallback='0'))
self.tnm_left.set(config.get('main','nm_left',fallback='0'))
self.nm_right = float(config.get('main','nm_right',fallback='0'))
self.tnm_right.set(config.get('main','nm_right',fallback='0'))
self.nm_step = float(config.get('main','nm_step',fallback='0'))
self.tnm_step.set(config.get('main','nm_step',fallback='0'))
self.set_nm_scale()
except IOError:
print("By the great otter!","No config.ini file")