Skip to content
Merged
8 changes: 5 additions & 3 deletions src/pysme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import colorlog
import tqdm
from pathlib import Path
from .config import Config

# numpy 2.x 兼容 shim:把内部实现映射回 numpy.lib.format
try:
Expand Down Expand Up @@ -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文件路径
Expand Down Expand Up @@ -125,4 +127,4 @@ def emit(self, record):
"solve",
"uncertainties",
"smelib",
]
]
5 changes: 2 additions & 3 deletions src/pysme/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

logger = logging.getLogger(__name__)


def _requires_load(func):
def func_new(self, *args, **kwargs):
if self._cfg is None:
Expand All @@ -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")

Expand Down
1 change: 1 addition & 0 deletions src/pysme/config_default.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
29 changes: 10 additions & 19 deletions src/pysme/init_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"),
)
expanduser(f"~/.sme/datafiles_nlte.json"),
)
4 changes: 3 additions & 1 deletion src/pysme/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'])

Expand Down
Loading