diff --git a/src/pysme/__init__.py b/src/pysme/__init__.py index f871451b..c6fe4599 100644 --- a/src/pysme/__init__.py +++ b/src/pysme/__init__.py @@ -13,6 +13,7 @@ import colorlog import tqdm from pathlib import Path +from .config import Config # numpy 2.x 兼容 shim:把内部实现映射回 numpy.lib.format try: @@ -89,13 +90,14 @@ def emit(self, record): libtools.compile_interface() # Extract the 3DNLTE H line profiles -if not os.path.exists('~/.sme/hlineprof/lineprof.dat'): +config = Config() +if not os.path.exists(f'{config["data.hlineprof"]}/lineprof.dat'): """Setup the H line profile data during package installation""" import gzip from pathlib import Path # 创建目标目录 - target_dir = os.path.expanduser("~/.sme/hlineprof") + target_dir = os.path.expanduser(config['data.hlineprof']) Path(target_dir).mkdir(parents=True, exist_ok=True) # 获取包内的gz文件路径 @@ -125,4 +127,4 @@ def emit(self, record): "solve", "uncertainties", "smelib", -] \ No newline at end of file +] diff --git a/src/pysme/config.py b/src/pysme/config.py index 728741e3..d93931a8 100644 --- a/src/pysme/config.py +++ b/src/pysme/config.py @@ -9,7 +9,6 @@ logger = logging.getLogger(__name__) - def _requires_load(func): def func_new(self, *args, **kwargs): if self._cfg is None: @@ -20,13 +19,13 @@ def func_new(self, *args, **kwargs): class Config: - def __init__(self, fname="~/.sme/config.json"): + def __init__(self, fname=f"~/.sme/config.json"): self.filename = fname self._cfg = None if not exists(self.filename): logger.info( - f"No cconfiguration file found at {self.filename}, using default values instead" + f"No configuration file found at {self.filename}, using default values instead" ) self.filename = join(dirname(__file__), "config_default.json") diff --git a/src/pysme/config_default.json b/src/pysme/config_default.json index 28821ba8..2b21ea77 100644 --- a/src/pysme/config_default.json +++ b/src/pysme/config_default.json @@ -1,5 +1,6 @@ { "data.file_server": "https://sme.astro.uu.se/atmos", + "data.hlineprof": "~/.sme/hlineprof", "data.atmospheres": "~/.sme/atmospheres", "data.nlte_grids": "~/.sme/nlte_grids", "data.cache.atmospheres": "~/.sme/atmospheres/cache", diff --git a/src/pysme/init_config.py b/src/pysme/init_config.py index dfc0a897..af691e6b 100644 --- a/src/pysme/init_config.py +++ b/src/pysme/init_config.py @@ -4,13 +4,15 @@ def ensure_user_config(): # Create folder structure for config files - directory = expanduser("~/.sme/") + directory = expanduser(f"~/.sme/") conf = join(directory, "config.json") + hlineprof = join(directory, "hlineprof") atmo = join(directory, "atmospheres") nlte = join(directory, "nlte_grids") cache_atmo = join(atmo, "cache") cache_nlte = join(nlte, "cache") + os.makedirs(directory, exist_ok=True) os.makedirs(directory, exist_ok=True) os.makedirs(atmo, exist_ok=True) os.makedirs(nlte, exist_ok=True) @@ -21,32 +23,21 @@ def ensure_user_config(): if not exists(conf): print('Create config file') # Hardcode default settings? - defaults = { - "data.file_server": "http://sme.astro.uu.se/atmos", - "data.atmospheres": "~/.sme/atmospheres", - "data.nlte_grids": "~/.sme/nlte_grids", - "data.cache.atmospheres": "~/.sme/atmospheres/cache", - "data.cache.nlte_grids": "~/.sme/nlte_grids/cache", - "data.pointers.atmospheres": "datafiles_atmospheres.json", - "data.pointers.nlte_grids": "datafiles_nlte.json", - } - - # Save file to disk - with open(conf, "w") as f: - json.dump(defaults, f) + config_filepath = join(dirname(__file__), "config_default.json") + copy(config_filepath, conf) # else: # print("Configuration file already exists") # Copy datafile pointers, for use in the GUI - if not exists(expanduser("~/.sme/datafiles_atmospheres.json")): + if not exists(expanduser(f"~/.sme/datafiles_atmospheres.json")): print("Copy references to datafiles for atmospheres to config directory") copy( join(dirname(__file__), "datafiles_atmospheres.json"), - expanduser("~/.sme/datafiles_atmospheres.json"), + expanduser(f"~/.sme/datafiles_atmospheres.json"), ) - if not exists(expanduser("~/.sme/datafiles_nlte.json")): + if not exists(expanduser(f"~/.sme/datafiles_nlte.json")): print("Copy references to datafiles for nlte to config directory") copy( join(dirname(__file__), "datafiles_nlte.json"), - expanduser("~/.sme/datafiles_nlte.json"), - ) \ No newline at end of file + expanduser(f"~/.sme/datafiles_nlte.json"), + ) diff --git a/src/pysme/util.py b/src/pysme/util.py index 42aee284..a12f17a1 100644 --- a/src/pysme/util.py +++ b/src/pysme/util.py @@ -26,6 +26,7 @@ from . import __version__ as smeversion from .sme_synth import SME_DLL +from .config import Config logger = logging.getLogger(__name__) show_progress_bars = False @@ -434,7 +435,8 @@ def parse_args(): args = parser.parse_args() return args.sme, args.vald, args.fitparameters -H_lineprof = pd.read_csv(os.path.expanduser("~/.sme/hlineprof/lineprof.dat"), sep=' +', names=['Teff', 'logg', 'Fe_H', 'nu', 'wl', 'wlair', 'mu', 'wmu', 'Ic', 'I'], engine='python') +config = Config() +H_lineprof = pd.read_csv(os.path.expanduser(f"{config['data.hlineprof']}/lineprof.dat"), sep=' +', names=['Teff', 'logg', 'Fe_H', 'nu', 'wl', 'wlair', 'mu', 'wmu', 'Ic', 'I'], engine='python') H_lineprof['wl'] *= 10 H_lineprof['wl'] = vac2air(H_lineprof['wl'])