Exemplo n.º 1
0
def setup_rundirs(prefix, scan, conf_dir, **kwargs):
    """
    Concludes the experiment directory, creates main configuration files, and calls function to copy other configuration files.

    Parameters
    ----------
    prefix : str
        prefix to name of the experiment/data reconstruction
    scan : str
        a range of scans to prepare data from
    conf_dir : str
        directory from where the configuration files will be copied
    specfile : str
        optional, from kwargs, specfile configuration to write to config file
    copy_prep : bool
        optional, from kwargs, if sets to true, the prepared file is also copied
        
    Returns
    -------
    nothing
    """
    id = prefix + '_' + scan

    if not os.path.isdir(conf_dir):
        print('configured directory ' + conf_dir + ' does not exist')
        return

    main_conf = os.path.join(conf_dir, 'config')
    if not os.path.isfile(main_conf):
        print('the configuration directory does not contain "config" file')
        return

    if not ver.ver_config_prep(main_conf):
        return

    try:
        with open(main_conf, 'r') as f:
            config_map = cfg.Config(f.read())
    except Exception as e:
        print('Please check the configuration file ' + main_conf +
              '. Cannot parse ' + str(e))
        return

    try:
        working_dir = config_map.working_dir.strip()
    except:
        working_dir = os.getcwd()

    experiment_dir = os.path.join(working_dir, id)
    if not os.path.exists(experiment_dir):
        os.makedirs(experiment_dir)
    # copy config_data, config_rec, cofig_disp files from cofig directory into the experiment conf directory
    experiment_conf_dir = os.path.join(experiment_dir, 'conf')
    if not os.path.exists(experiment_conf_dir):
        os.makedirs(experiment_conf_dir)

    # here we want the command line to be used if present, so need to check if None was passed or not.
    if 'specfile' in kwargs:
        specfile = kwargs['specfile']
    if specfile is None:
        try:
            specfile = config_map.specfile.strip()
        except:
            print("Specfile not in config or command line")

    # Based on params passed to this function create a temp config file and then copy it to the experiment dir.
    experiment_main_config = os.path.join(experiment_conf_dir, 'config')
    conf_map = {}
    conf_map['working_dir'] = '"' + working_dir + '"'
    conf_map['experiment_id'] = '"' + prefix + '"'
    conf_map['scan'] = '"' + scan + '"'
    if specfile is not None:
        conf_map['specfile'] = '"' + specfile + '"'
    temp_file = os.path.join(experiment_conf_dir, 'temp')
    with open(temp_file, 'a') as f:
        for key in conf_map:
            value = conf_map[key]
            if len(value) > 0:
                f.write(key + ' = ' + conf_map[key] + '\n')
    f.close()
    if not ver.ver_config(temp_file):
        print('please check the entered parameters. Cannot save this format')
    else:
        shutil.copy(temp_file, experiment_main_config)
    os.remove(temp_file)

    copy_conf(conf_dir, experiment_conf_dir)
    if 'copy_prep' in kwargs:
        copy_prep = kwargs['copy_prep']
    if copy_prep:
        # use abspath to get rid of trailing dir sep if it is there
        other_exp_dir = os.path.split(os.path.abspath(conf_dir))[0]
        new_exp_dir = os.path.split(os.path.abspath(experiment_conf_dir))[0]

        # get case of single scan or summed
        prep_dir_list = glob.glob(os.path.join(other_exp_dir, 'prep'),
                                  recursive=True)
        for dir in prep_dir_list:
            shutil.copytree(dir, os.path.join(new_exp_dir, 'prep'))

            # get case of split scans
        prep_dir_list = glob.glob(os.path.join(other_exp_dir, "scan*/prep"),
                                  recursive=True)
        for dir in prep_dir_list:
            scandir = os.path.basename(os.path.split(dir)[0])
            shutil.copytree(dir, os.path.join(new_exp_dir, *(scandir, 'prep')))
    return experiment_dir
def create_exp(prefix, scan, working_dir, **args):
    """
    Concludes experiment name, creates directory, and "conf" subdirectory with initial configuration files.

    Parameters
    ----------
    prefix : str
        a literal ID of the experiment
    scan : str
        string indicating scan number, or scan range
        ex1: 5
        ex2: 67 - 89
    working_dir : str
        directory where the file will be saved
    specfile : str
        optional, name of specfile that was saved during the experiment

    Returns
    -------
    experiment_dir : str
        directory where the new experiment is located
    """
    id = prefix + '_' + scan

    if not os.path.isdir(working_dir):
        print('working directory ' + working_dir + ' does not exist')
        return

    experiment_dir = os.path.join(working_dir, id)
    if not os.path.exists(experiment_dir):
        os.makedirs(experiment_dir)
    else:
        print('experiment with this id already exists')
        return experiment_dir

    experiment_conf_dir = os.path.join(experiment_dir, 'conf')
    if not os.path.exists(experiment_conf_dir):
        os.makedirs(experiment_conf_dir)

    # Based on params passed to this function create a temp config file and then copy it to the experiment dir.
    experiment_main_config = os.path.join(experiment_conf_dir, 'config')
    conf_map = {}
    conf_map['working_dir'] = '"' + working_dir + '"'
    conf_map['experiment_id'] = '"' + prefix + '"'
    conf_map['scan'] = '"' + scan + '"'
    if 'specfile' in args:
        conf_map['specfile'] = '"' + args['specfile'] + '"'
    if 'beamline' in args:
        conf_map['beamline'] = '"' + args['beamline'] + '"'

    temp_file = os.path.join(experiment_conf_dir, 'temp')
    with open(temp_file, 'a') as f:
        for key in conf_map:
            value = conf_map[key]
            if len(value) > 0:
                f.write(key + ' = ' + conf_map[key] + '\n')
    f.close()
    if not ver.ver_config(temp_file):
        print('please check the entered parameters. Cannot save this format')
    else:
        shutil.copy(temp_file, experiment_main_config)
    os.remove(temp_file)

    # create simple configuration for each phase
    create_conf_prep(experiment_conf_dir)
    create_conf_data(experiment_conf_dir)
    create_conf_rec(experiment_conf_dir)
    create_conf_disp(experiment_conf_dir)

    return experiment_dir
def write_conf(conf_map, dir, file):
    """
    It creates configuration file from the parameters included in dictionary, verifies, and saves in the configuration directory.
    Parameters
    ----------
    conf_map : dict
        dictionary containing configuration parameters
    dir : str
        a directory where the configuration file will be saved
    file : str
        name of the configuration file to save
    Returns
    -------
    nothing
    """
    # create "temp" file first, verify it, and if ok, copy to a configuration file
    if not os.path.exists(dir):
        os.makedirs(dir)
    conf_file = os.path.join(dir, file)
    temp_file = os.path.join(dir, 'temp')
    if os.path.isfile(temp_file):
        os.remove(temp_file)
    with open(temp_file, 'a') as f:
        for key in conf_map:
            value = conf_map[key]
            if len(value) > 0:
                f.write(key + ' = ' + conf_map[key] + '\n')
    f.close()

    if file == 'config':
        if not ver.ver_config(temp_file):
            os.remove(temp_file)
            msg_window(
                'please check the entries in the main window. Cannot save this format'
            )
            return False
    elif file == 'config_prep':
        if not ver.ver_config_prep(temp_file):
            os.remove(temp_file)
            msg_window(
                'please check the entries in the Data prep tab. Cannot save this format'
            )
            return False
    elif file == 'config_data':
        if not ver.ver_config_data(temp_file):
            os.remove(temp_file)
            msg_window(
                'please check the entries in the Data tab. Cannot save this format'
            )
            return False
    elif file.endswith('config_rec'):
        if not ver.ver_config_rec(temp_file):
            os.remove(temp_file)
            msg_window(
                'please check the entries in the Reconstruction tab. Cannot save this format'
            )
            return False
    elif file == 'config_disp':
        if not ver.ver_config_disp(temp_file):
            os.remove(temp_file)
            msg_window(
                'please check the entries in the Display tab. Cannot save this format'
            )
            return False
    # copy if verified
    shutil.copy(temp_file, conf_file)
    os.remove(temp_file)
    return True