示例#1
0
文件: io.py 项目: sandraskj/pdfstream
def write_out(saving_dir: str, filename: str, pdfgetter: PDFGetter) -> dict:
    """Write out data in pdfgetter into files"""
    data_dirs = {}
    for out_type in pdfgetter.config.outputtypes:
        data_dir = Path(saving_dir).joinpath(out_type)
        if not data_dir.exists():
            data_dir.mkdir()
        data_dirs.update({out_type: data_dir})
    dct = {}
    for out_type in pdfgetter.config.outputtypes:  # out_type in ('iq', 'sq', 'fq', 'gr')
        data_dir = data_dirs.get(out_type)
        out_file = data_dir.joinpath(
            Path(filename).with_suffix(".{}".format(out_type)).name)
        pdfgetter.writeOutput(str(out_file), out_type)
        dct.update({out_type: str(out_file)})
    return dct
示例#2
0
def run_pipe(tiff_file: str,
             saving_dir: str = ".",
             poni_file: str = None,
             pdfgetter: PDFGetter = None,
             refine_func: Callable = None) -> dict:
    """
    A pipeline process to integrate, transform and fit the data and return a dictionary of path to the resulting files.
    Parameters
    ----------
    tiff_file
        Path to the tiff file.
    saving_dir
        Path to the directory where the output data files will be saved. Default "."
    poni_file
        Path to the poni file. If None, the poni file in the saving_dir will be used.
    pdfgetter
        A PDFgetter to transform the chi file to gr file. If None, a default one will be created, see
        make_default_pdfgetter. Default None
    refine_func
        A function func(gr_file: str) to refine the G(r) of Ni. If None, a default refine function will be used, see
        refine. Default None

    Returns
    -------
    res_dct
        a dictionary that contains keys: "poni", "chi", "gr", "Rw", "csv", "fgr".
    """
    def find_poni(dir_to_search):
        files_found = recfind(dir_to_search, r".*\.poni")
        if len(files_found) < 1:
            raise Exception(f"Not found poni file in {dir_to_search}")
        elif len(files_found) > 1:
            _poni_file = files_found[0]
            print(f"Multiple file found in {dir_to_search}.\nUse {_poni_file}")
        else:
            _poni_file = files_found[0]
        return _poni_file

    poni_file = poni_file if poni_file else find_poni(saving_dir)

    wl = load(poni_file)["Wavelength"] * 1e10  # unit A
    result = dict(poni=poni_file, wl=wl)
    print(f"Run pipeline with wavelength: {wl:.4f} and poni file: {poni_file}")

    result["chi"], _ = xpdtools_int(poni_file,
                                    tiff_file,
                                    chi_dir=saving_dir,
                                    alpha=2.0)

    pdfgetter = pdfgetter if pdfgetter else make_default_pdfgetter()

    chi_q, chi_i = loaddata(result["chi"]).T
    pdfgetter(chi_q, chi_i)
    result["gr"] = os.path.splitext(result["chi"])[0] + ".gr"
    pdfgetter.writeOutput(result["gr"], "gr")

    if refine_func:
        pass
    else:
        refine_func = default_refine

    result["Rw"], result["csv"], result["fgr"] = refine_func(
        result["gr"], saving_dir)

    return result