Skip to content

Commit

Permalink
before RB
Browse files Browse the repository at this point in the history
  • Loading branch information
RubelMozumder committed Dec 1, 2023
1 parent 0c3b445 commit ce5b4af
Showing 1 changed file with 99 additions and 31 deletions.
130 changes: 99 additions & 31 deletions pynxtools/dataconverter/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import logging
import os
import sys
from typing import List, Tuple, Optional
from typing import List, Tuple
import xml.etree.ElementTree as ET

Expand Down Expand Up @@ -60,66 +61,134 @@ def get_names_of_all_readers() -> List[str]:
return all_readers


# pylint: disable=too-many-arguments,too-many-locals,W1203,R0912,W0613
def convert(input_file: Tuple[str],
reader: str,
nxdl: str,
output: str,
generate_template: bool = False,
fair: bool = False,
undocumented: bool = False,
logger_: logging.Logger = None,
**kwargs):
"""The conversion routine that takes the input parameters and calls the necessary functions."""
if logger_:
logger = logger_
else:
logger = pynx_logger
def get_nxdl_root_and_path(nxdl: str):
"""Get xml root element and file path from nxdl name e.g. NXapm.
Parameters
----------
nxdl: str
Name of nxdl file e.g. NXapm from NXapm.nxdl.xml.
Returns
-------
ET.root
Root element of nxdl file.
str
Path of nxdl file.
Raises
------
FileNotFoundError
Error if no file with the given nxdl name is found.
"""
# Reading in the NXDL and generating a template
definitions_path = nexus.get_nexus_definitions_path()
if nxdl == "NXtest":
nxdl_path = os.path.join(
nxdl_f_path = os.path.join(
f"{os.path.abspath(os.path.dirname(__file__))}/../../",
"tests", "data", "dataconverter", "NXtest.nxdl.xml")
elif nxdl == "NXroot":
nxdl_path = os.path.join(definitions_path, "base_classes", "NXroot.nxdl.xml")
nxdl_f_path = os.path.join(definitions_path, "base_classes", "NXroot.nxdl.xml")
else:
nxdl_path = os.path.join(definitions_path, "contributed_definitions", f"{nxdl}.nxdl.xml")
if not os.path.exists(nxdl_path):
nxdl_path = os.path.join(definitions_path, "applications", f"{nxdl}.nxdl.xml")
if not os.path.exists(nxdl_path):
nxdl_f_path = os.path.join(definitions_path, "contributed_definitions", f"{nxdl}.nxdl.xml")
if not os.path.exists(nxdl_f_path):
nxdl_f_path = os.path.join(definitions_path, "applications", f"{nxdl}.nxdl.xml")
if not os.path.exists(nxdl_f_path):
nxdl_f_path = os.path.join(definitions_path, "base_classes", f"{nxdl}.nxdl.xml")
if not os.path.exists(nxdl_f_path):
raise FileNotFoundError(f"The nxdl file, {nxdl}, was not found.")

nxdl_root = ET.parse(nxdl_path).getroot()
return ET.parse(nxdl_f_path).getroot(), nxdl_f_path


def transfer_data_into_template(input_file,
reader, nxdl_name,
nxdl_root: Optional[ET.Element] = None,
logger_: logging.Logger = None,
**kwargs):
"""Transfer parse and merged data from input experimental file, config file and eln.
Experimental and eln files will be parsed and finally will be merged into template.
Before returning the template validate the template data.
Parameters
----------
input_file : Union[tuple[str], str]
Tuple of files or file
reader: str
Name of reader such as xps
nxdl_name : str
Root name of nxdl file, e.g. NXmpes from NXmpes.nxdl.xml
nxdl_root : ET.element
Root element of nxdl file, otherwise provide nxdl_name
Returns
-------
Template
Template filled with data from raw file and eln file.
"""
if logger_:
logger = logger_
else:
logger = pynx_logger
if nxdl_root is None:
nxdl_root, _ = get_nxdl_root_and_path(nxdl=nxdl_name)

template = Template()
helpers.generate_template_from_nxdl(nxdl_root, template)
if generate_template:
logger.info(template)
return

# Setting up all the input data
if isinstance(input_file, str):
input_file = (input_file,)

bulletpoint = "\n\u2022 "
logger.info("Using %s reader to convert the given files: %s ",
reader,
bulletpoint.join((" ", *input_file)))

data_reader = get_reader(reader)
if not (nxdl in data_reader.supported_nxdls or "*" in data_reader.supported_nxdls):
if not (nxdl_name in data_reader.supported_nxdls or "*" in data_reader.supported_nxdls):
raise NotImplementedError("The chosen NXDL isn't supported by the selected reader.")

data = data_reader().read( # type: ignore[operator]
template=Template(template),
file_paths=input_file,
**kwargs,
**kwargs
)
helpers.validate_data_dict(template, data, nxdl_root, logger)
return data


# pylint: disable=too-many-arguments,too-many-locals,W1203,R0912,W0613
def convert(input_file: Tuple[str],
reader: str,
nxdl: str,
output: str,
generate_template: bool = False,
fair: bool = False,
undocumented: bool = False,
logger_: logging.Logger = None,
**kwargs):
"""The conversion routine that takes the input parameters and calls the necessary functions."""
if logger_:
logger = logger_
else:
logger = pynx_logger

nxdl_root, nxdl_f_path = get_nxdl_root_and_path(nxdl)

if generate_template:
template = Template()
helpers.generate_template_from_nxdl(nxdl_root, template)
logger.info(template)
return

data = transfer_data_into_template(input_file=input_file, reader=reader,
nxdl_name=nxdl, nxdl_root=nxdl_root,
logger_=logger, **kwargs)
if fair and data.undocumented.keys():
logger.warning("There are undocumented paths in the template. This is not acceptable!")
return

for path in data.undocumented.keys():
if "/@default" in path:
continue
Expand All @@ -128,8 +197,7 @@ def convert(input_file: Tuple[str],
)

helpers.add_default_root_attributes(data=data, filename=os.path.basename(output))

Writer(data=data, nxdl_path=nxdl_path, output_path=output).write()
Writer(data=data, nxdl_f_path=nxdl_f_path, output_path=output).write()

logger.info("The output file generated: %s", output)

Expand Down

0 comments on commit ce5b4af

Please sign in to comment.