#! /usr/bin/env python3
import os
import configparser
import klassez as kz
import argparse
from .base import BaseCommand
from .t1t2ne_utils import istract
from . import f_findfs
[docs]
def config(foundpath, basedir=None, hsqcexpno=None, tractexpno=None):
"""
Configures the script for use on a spectrometer, and is reserved to the NMR superuser.
The function prompts the user to input the path to the topspin installation and checks if the user is in the list of NMR superusers.
If the user is not a superuser, a PermissionError is raised and the function exits.
If the user is a superuser, the function prompts the user to input the path to a standard dataset, the experiment number of the TRACT experiment in the standard dataset, and the experiment number of the HSQC experiment in the standard dataset (used only to suggest a number of scans for the experiments).
The function then loads the TRACT experiment from the standard dataset, checks if it is a TRACT experiment, and extracts B0 field strength from the acqus file.
Finally, it creates a config file in the curdir folder of topspin with the extracted information and exits.
Parameters
----------
None
Returns
-------
None
The function creates a config file in the curdir folder of topspin with the extracted information and exits.
"""
fspath = input('Please provide the path to topspin: ') or foundpath
nmrsupath = os.path.join(fspath, 'conf', 'nmrsuperuser')
with open(nmrsupath) as file:
nmrsulist = [line.rstrip() for line in file]
#check if the user corresponds to the admin
if os.getlogin() not in nmrsulist:
raise PermissionError('Only the NMR superuser can write the config file. Exiting.')
else:
config_p = configparser.ConfigParser()
pathtodataset = input('Please provide the path to a standard dataset to be used as example') or basedir
if not os.path.isdir(pathtodataset):
raise ValueError(f"The provided path {pathtodataset} is not a valid directory.")
tractexpno = input('Please provide the experiment number of the TRACT experiment in the standard dataset') or tractexpno
hsqcexpno = input('Please provide the experiment number of the HSQC experiment in the standard dataset (used only to suggest a number of scans for the experiments)') or hsqcexpno
if tractexpno is None and hsqcexpno is None:
raise ValueError('At least one of the two arguments --tract and --hsqc must be provided to specify the experiment numbers of the TRACT and HSQC experiments in the standard dataset, if the standard dataset is provided.')
S_config= kz.Pseudo_2D(os.path.join(pathtodataset, f'{tractexpno}'))
if not istract(S_config):
raise NameError(f'Experiment {tractexpno} is not a TRACT experiment')
config_p['EXPERIMENT'] = {
'basedir': pathtodataset,
'expno': tractexpno,
'hsqcexpno': hsqcexpno,
}
pathtocurdir = os.path.join(fspath, 'prog', 'curdir', os.getlogin(), 'tract_analysis_config.ini')
with open(pathtocurdir, 'w') as configfile:
config_p.write(configfile)
exit()