Skip to content

Commit

Permalink
General skelaton for xrd reader.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubelMozumder committed Nov 24, 2023
1 parent 1a4b496 commit 0270cf6
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 3 deletions.
15 changes: 15 additions & 0 deletions pynxtools/dataconverter/readers/xrd/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
100 changes: 100 additions & 0 deletions pynxtools/dataconverter/readers/xrd/reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from typing import Tuple, Any, Dict, Union
import json
from pynxtools.dataconverter.readers.xrd.xrd_parser import parse_and_convert_file
from pynxtools.dataconverter.readers.utils import flatten_and_replace, FlattenSettings
import yaml
from pynxtools.dataconverter.template import Template
from pynxtools.dataconverter.readers.base.reader import BaseReader


CONVERT_DICT = {
'Instrument': 'INSTRUMENT[instrument]',
'Software': 'SOFTWARE[software]',
'Hardware': 'Hardware[hardware]',
'Analyser': 'ELECTRONANALYSER[electronanalyser]',
'Beam': 'BEAM[beam]',
'unit': '@units',
'version': '@version',
'Sample': 'SAMPLE[sample]',
'User': 'USER[user]',
'Data': 'DATA[data]',
'Source': 'SOURCE[source]',
'Environment': 'ENVIRONMENT[environment]',
'Sample_bias': 'SAMPLE_BIAS[sample_bias]'
}

REPLACE_NESTED: Dict[str, str] = {}


class STMReader(BaseReader):
""" Reader for XPS.
"""

supported_nxdls = ["NXroot"]

def read(self,
template: dict = None,
file_paths: Tuple[str] = None,
objects: Tuple[Any] = None):
"""
General read menthod to prepare the template.
"""
# has_sxm_file: bool = False
# sxm_file: str = ""
# has_dat_file: bool = False
# dat_file: str = ""
filled_template: Union[Dict, None] = Template()
# config_dict: Union[Dict[str, Any], None] = None
eln_dict: Union[Dict[str, Any], None] = None
config_dict: Dict = {}

data_file: str = ""
for file in file_paths:
ext = file.rsplit('.', 1)[-1]
if ext == 'json':
with open(file, mode="r", encoding="utf-8") as fl_obj:
config_dict = json.load(fl_obj)
elif ext in ['yaml', 'yml']:
with open(file, mode="r", encoding="utf-8") as fl_obj:
eln_dict = flatten_and_replace(
FlattenSettings(
yaml.safe_load(fl_obj),
CONVERT_DICT,
REPLACE_NESTED
)
)
else:
xrd_dict = parse_and_convert_file(file)
# TODO combine nyaml, json, and xrd data here

# Get rid of empty concept and cleaning up Template
for key, val in template.items():

if val is None:
del template[key]
else:
filled_template[key] = val
if not filled_template.keys():
raise ValueError("Reader could not read anything! Check for input files and the"
" corresponding extention.")
return filled_template


READER = STMReader
15 changes: 15 additions & 0 deletions pynxtools/dataconverter/readers/xrd/xrd_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
"""
XRD file parser collection.
TODO: Extend the module level doc.
"""
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re # for regular expressions
import os # for file path operations
import xml.etree.ElementTree as ET # for XML parsing
Expand All @@ -14,7 +34,7 @@ def __init__(self, file_path):

def read_file(self):
'''Reads the content of a file from the given file path.
Returns:
str: The content of the file.
'''
Expand Down Expand Up @@ -45,7 +65,7 @@ def parse_metadata(self):
root = ET.fromstring(content)

ns_version = root.tag.split("}")[0].strip("{")
ns = {'xrd': ns_version}
ns = {'xrd': ns_version}

xrd_measurement = root.find("xrd:xrdMeasurement", ns)

Expand Down Expand Up @@ -103,7 +123,7 @@ def __init__(self, file_path):

def identify_format(self):
'''Identifies the format of a given file.
Returns:
str: The file extension of the file.
'''
Expand Down

0 comments on commit 0270cf6

Please sign in to comment.