backscattering-analyzer/src/backscattering_analyzer/settings.py
2024-05-22 16:56:04 +02:00

185 lines
6.9 KiB
Python

from pathlib import Path
from rich.console import Console
from rich.theme import Theme
from rich.traceback import install as traceback_install
class Settings:
def __init__(self, options: list):
self.theme = Theme(
{
"main": "white bold",
"option": "grey50 italic",
"argument": "red",
"description": "green italic",
}
)
self.console = Console(theme=self.theme)
traceback_install(console=self.console, show_locals=True)
self.version = "0.1.0"
self.help = (
"[main]display[/main] [option]\\[options][/option]"
"\n [argument]-b --bench[/argument] [description]bench of the experiment[/description]"
"\n [argument]-d --date[/argument] [description]date of the experiment[/description]"
"\n [argument]-h --help[/argument] [description]print this help and exit[/description]"
"\n [argument]-v --verbose[/argument] [description]be verbose[/description]"
"\n [argument]-V --version[/argument] [description]print version number and exit[/description]"
)
self.verbose = False
self.bench = "SDB1"
self.date = "2023_03_24"
self.folder = Path("/home/demagny/data")
self.wavelength = 1.064e-6 # m
self.calib_bench = 1.15
self.calib_mirror = 1.15
self.scattering_factor = [1.3e-4, 1e-16] # parameter to change
self.vibration_frequency = 0.2
index = 0
while index < len(options):
option = options[index]
try:
if option[0] != "-":
if option == "h":
option = "--help"
if option == "V":
option = "--version"
if "v" in option:
options[index] = option.replace("v", "")
option = "--verbose"
index -= 1
if option == "b":
index += 1
try:
options[index] = "--bench={}".format(
options[index]
)
except IndexError:
raise Exception(
"bench name needed after b option"
)
if option == "d":
index += 1
try:
options[index] = "--date={}".format(
options[index]
)
except IndexError:
raise Exception(
"date needed after d option"
)
if option[0] == "-":
if option[1] != "-":
if option == "-h":
option = "--help"
if option == "-V":
option = "--version"
if "v" in option:
options[index] = option.replace("v", "")
option = "--verbose"
index -= 1
if option == "-b":
index += 1
try:
options[index] = "--bench={}".format(
options[index]
)
except IndexError:
raise Exception(
"bench name needed after -b option"
)
if option == "-d":
index += 1
try:
options[index] = "--date={}".format(
options[index]
)
except IndexError:
raise Exception(
"date needed after -d option"
)
if option[:2] == "--":
if option == "--help":
self.console.print(self.help)
exit(0)
if option == "--version":
self.console.print(self.version)
exit(0)
if option == "--verbose":
self.verbose = True
try:
if option[:8] == "--bench=":
self.bench = str(option[8:]).upper()
except IndexError:
continue
try:
if option[:7] == "--date=":
self.date = option[7:]
except IndexError:
continue
else:
raise Exception("unknown option {}".format(option))
except IndexError:
pass
index += 1
if self.date == "2023_03_24":
self.modelisation = "scatterCouplingO4.mat"
self.power_in = 23 # W
self.power_out = 8e-3 # W
elif self.date == "2023_12_21":
self.modelisation = "scatterCouplingMisalignSR.mat"
self.power_in = 12.1 # W
self.power_out = 8e-3 # W
elif self.date == "2024_01_21":
self.modelisation = "scatterCouplingMisalignSR.mat"
self.power_in = 12.1 # W
self.power_out = 8e-3 # W
else:
self.modelisation = "scatterCouplingO4.mat"
self.power_in = 23 # W
self.power_out = 8e-3 # W
def bench_file(self) -> Path:
return self.folder / (
"{bench}_{date}_ben.csv".format(
bench=self.bench,
date=self.date,
)
)
def mirror_file(self) -> Path:
return self.folder / (
"{bench}_{date}_mir.csv".format(
bench=self.bench,
date=self.date,
)
)
def data_file(self) -> Path:
return self.folder / (
"{bench}_{date}_dat.csv".format(
bench=self.bench,
date=self.date,
)
)
def reference_file(self) -> Path:
return self.folder / (
"{bench}_{date}_ref.csv".format(
bench=self.bench,
date=self.date,
)
)
def modelisation_file(self) -> Path:
return self.folder / (self.modelisation)
def coupling_name(self) -> str:
return "{bench}coupling".format(bench=self.bench)
def log(self, message) -> None:
if self.verbose:
self.console.log(message, _stack_offset=2)