tp_audine_elec.py 6.78 KB
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 11 15:39:17 2021

@author: alain

$ cd ~/guitastro/gui/tp_audine_elec 
$ python3 tp_audine_elec.py
"""

import os
import sys
import yaml
import subprocess
import time
from PIL import ImageTk
import tkinter as tk

path = os.path.abspath("../../src")
if path not in sys.path:
        sys.path.insert(0,path)
import guitastro

# #####################################################################
# #####################################################################
# #####################################################################
# Main
# #####################################################################
# #####################################################################
# #####################################################################

if __name__ == "__main__":

    # --- Get path_images for examples
    ima = guitastro.Ima()
    path_products = ima.copy_data2products()
    fconfig = os.path.join(path_products,"tp_audine_elec.yml")
    if not os.path.exists(fconfig):
        config = {}
        conf = {}
        conf["fits_path"] = "/home/user/Documents"
        config["visu1"] = conf
    else:
        with open(fconfig, 'r') as stream:
            try:
                config = yaml.safe_load(stream)
            except yaml.YAMLError as exc:
                print(exc)
                
    infos = {}
    infos['demarrer_acq'] = "Appuyez sur le bouton Acquisition pour prendre une image"
    infos['acq_en_cours'] = "Acquisition en cours. Attendre quelques secondes... Prendre des mesures à l'oscilloscope"
    var = {}

    def save_config():
        global fconfig
        with open(fconfig, 'w') as yaml_file:
            yaml.dump(config, yaml_file, default_flow_style=False)

    def ima1_load():
        global widgets
        path_cur = config["visu1"]["fits_path"]
        ima1.rootdir = path_cur
        fullfile = ima1.load()
        if fullfile != "":
            path_new = os.path.dirname(fullfile)
            if path_cur != path_new and path_new != "":
                config["visu1"]["fits_path"] = path_new
                save_config()
        visu1.autocuts()
        visu1.disp()
        widgets["button_saveima_1"].configure(state= tk.NORMAL)

    def ima1_save():
        ima1.save()

    # Fonction appelée par l'appui du bouton
    def acquisition():
        global widgets
        global infos
        global var
        widgets["button_loadima_1"].configure(state= tk.DISABLED)
        widgets["button_saveima_1"].configure(state= tk.DISABLED)
        widgets["button_acq"].configure(state= tk.DISABLED)
        var['info'].set(infos['acq_en_cours'])
        # ---        
        expo = var_exp.get()
        if expo < 0:
            expo = 0
        if expo > 30:
            expo = 30
        var_exp.set(expo)
        # ---        
        binn = var_bin.get()
        if binn < 1:
            binn = 1
        if binn > 100:
            binn = 100
        var_bin.set(binn)
        # ---
        tkroot.update()
        # ---
        argus = []
        argus.append("/home/user/iut/tp_ccd/bin/Debug/tp_ccd")
        argus.append(f"{expo}")
        argus.append(f"{binn}")
        # --- Launch the acquisition process
        message = f'subprocess.run({argus},capture_output=True)'
        print(message)
        t0 = time.time()
        res = subprocess.run(argus, capture_output=True)
        stdo = res.stdout
        #lstdos = str(stdo).split("\\n")
        dt = time.time() - t0
        message = f"Acquisition done in {dt:.3f} sec"
        print(message)
        print(stdo)
        # ---
        time.sleep(0.5)
        fullfile = ima1.load("/home/user/Documents/image.fit")
        path_new = os.path.dirname(fullfile)
        config["visu1"]["fits_path"] = path_new
        ima1.rootdir = path_new
        save_config()
	    # ---
        visu1.autocuts()
        visu1.disp()
        widgets["button_loadima_1"].configure(state= tk.NORMAL)
        widgets["button_saveima_1"].configure(state= tk.NORMAL)
        widgets["button_acq"].configure(state= tk.NORMAL)
        var['info'].set(infos['demarrer_acq'])
        
        # update the plot
        tkroot.update()

    # --- load an image from a FITS file
    ima1 = guitastro.Ima()
    ima1.extension = ".fit"

    # --- Create a simple Tk interface
    tkroot = tk.Tk()
    dimx = 795
    dimy = 785
    geom = f"{dimx}x{dimy}+120+80"
    tkroot.geometry(geom)
    tkroot.title("TP CCD Electronique - BUT S5")
    tkroot.minsize(height=dimy, width=dimx)
    tkroot.maxsize(height=dimy, width=dimx)
    
    # ---
    frame1 = tk.Frame(tkroot)
    frame1.pack(side = tk.TOP, fill=tk.X, expand=tk.NO)
    # ---
    widgets = {}
    # ---
    frame1m = tk.Frame(frame1)
    # ---
    test = ImageTk.PhotoImage(file='logo_iut_mp.png')
    label1 = tk.Label(frame1m, image=test)
    label1.image = test
    label1.pack(side = tk.LEFT, padx=2)
    # ---
    var_exp = tk.DoubleVar()
    var_exp.set(2.0)
    label_exp = tk.Label(frame1m, text="Temps de pose (s)")
    label_exp.pack(side = tk.LEFT)
    entry_exp = tk.Entry(frame1m, width=4, textvariable=var_exp)
    entry_exp.pack(side = tk.LEFT)
    # ---
    var_bin = tk.IntVar()
    var_bin.set(1)
    label_bin = tk.Label(frame1m, text="Binning")
    label_bin.pack(side = tk.LEFT)
    entry_bin = tk.Entry(frame1m, width=4, textvariable=var_bin)
    entry_bin.pack(side = tk.LEFT)
    # ---
    widgets["button_acq"] = tk.Button(frame1m, text="Acquisition", command=acquisition, padx=20, pady=20, font='sans 16 bold')
    widgets["button_acq"].pack(side = tk.LEFT, pady=5, padx=5)
    # ---
    widgets["button_loadima_1"] = tk.Button(frame1m, text="Load FITS", command=ima1_load)
    widgets["button_loadima_1"].pack(side=tk.LEFT, padx = 2)
    widgets["button_saveima_1"] = tk.Button(frame1m, text="Save FITS", command=ima1_save, state= tk.DISABLED)
    widgets["button_saveima_1"].pack(side=tk.LEFT, padx = 2)
    frame1m.pack(side = tk.TOP, fill=tk.X, expand=tk.NO)
    
    # ---
    frame2 = tk.Frame(tkroot)
    # ---
    var['info'] = tk.StringVar()
    var['info'].set(infos['demarrer_acq'])
    widgets["label_info"] = tk.Label(frame2, textvariable=var['info'], padx = 10)
    widgets["label_info"].pack(side = tk.LEFT)
    frame2.pack(side = tk.TOP, fill=tk.X, expand=tk.NO)
    
    # --- Create and use a visu
    try:
        frame1i = tk.Frame(tkroot)
        visu1 = guitastro.Visu(frame1i)
        visu1.ima(ima1)
        frame1i.pack(side = tk.TOP, fill=tk.BOTH, expand=tk.YES)
    except:
        msg = "{}".format(sys.exc_info())
        print(msg)

    # --- Launch the event loop of the user interface
    tkroot.pack_propagate(0)
    tkroot.update()
    tkroot.mainloop()