diff --git a/common/python/nomadcore/elements.py b/common/python/nomadcore/elements.py new file mode 100644 index 0000000000000000000000000000000000000000..413919f4ec7d347bc0da28e031e9e489f1c4b1b8 --- /dev/null +++ b/common/python/nomadcore/elements.py @@ -0,0 +1,87 @@ +import logging +LOGGER = logging.getLogger("nomad") + + +"""List of atomic symbols in order.""" +_symbols = [ + '', 'H', 'He', 'Li', 'Be', + 'B', 'C', 'N', 'O', 'F', + 'Ne', 'Na', 'Mg', 'Al', 'Si', + 'P', 'S', 'Cl', 'Ar', 'K', + 'Ca', 'Sc', 'Ti', 'V', 'Cr', + 'Mn', 'Fe', 'Co', 'Ni', 'Cu', + 'Zn', 'Ga', 'Ge', 'As', 'Se', + 'Br', 'Kr', 'Rb', 'Sr', 'Y', + 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', + 'Rh', 'Pd', 'Ag', 'Cd', 'In', + 'Sn', 'Sb', 'Te', 'I', 'Xe', + 'Cs', 'Ba', 'La', 'Ce', 'Pr', + 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', + 'Tb', 'Dy', 'Ho', 'Er', 'Tm', + 'Yb', 'Lu', 'Hf', 'Ta', 'W', + 'Re', 'Os', 'Ir', 'Pt', 'Au', + 'Hg', 'Tl', 'Pb', 'Bi', 'Po', + 'At', 'Rn', 'Fr', 'Ra', 'Ac', + 'Th', 'Pa', 'U', 'Np', 'Pu', + 'Am', 'Cm', 'Bk', 'Cf', 'Es', + 'Fm', 'Md', 'No', 'Lr' +] + +"""Dict of atomic symbols with their atomic number""" +_numbers = { + 'H': 1, 'He': 2, 'Li': 3, 'Be': 4, + 'B': 5, 'C': 6, 'N': 7, 'O': 8, 'F': 9, + 'Ne': 10, 'Na': 11, 'Mg': 12, 'Al': 13, 'Si': 14, + 'P': 15, 'S': 16, 'Cl': 17, 'Ar': 18, 'K': 19, + 'Ca': 20, 'Sc': 21, 'Ti': 22, 'V': 23, 'Cr': 24, + 'Mn': 25, 'Fe': 26, 'Co': 27, 'Ni': 28, 'Cu': 29, + 'Zn': 30, 'Ga': 31, 'Ge': 32, 'As': 33, 'Se': 34, + 'Br': 35, 'Kr': 36, 'Rb': 37, 'Sr': 38, 'Y': 39, + 'Zr': 40, 'Nb': 41, 'Mo': 42, 'Tc': 43, 'Ru': 44, + 'Rh': 45, 'Pd': 46, 'Ag': 47, 'Cd': 48, 'In': 49, + 'Sn': 50, 'Sb': 51, 'Te': 52, 'I': 53, 'Xe': 54, + 'Cs': 55, 'Ba': 56, 'La': 57, 'Ce': 58, 'Pr': 59, + 'Nd': 60, 'Pm': 61, 'Sm': 62, 'Eu': 63, 'Gd': 64, + 'Tb': 65, 'Dy': 66, 'Ho': 67, 'Er': 68, 'Tm': 69, + 'Yb': 70, 'Lu': 71, 'Hf': 72, 'Ta': 73, 'W': 74, + 'Re': 75, 'Os': 76, 'Ir': 77, 'Pt': 78, 'Au': 79, + 'Hg': 80, 'Tl': 81, 'Pb': 82, 'Bi': 83, 'Po': 84, + 'At': 85, 'Rn': 86, 'Fr': 87, 'Ra': 88, 'Ac': 89, + 'Th': 90, 'Pa': 91, 'U': 92, 'Np': 93, 'Pu': 94, + 'Am': 95, 'Cm': 96, 'Bk': 97, 'Cf': 98, 'Es': 99, + 'Fm': 100, 'Md': 101, 'No': 102, 'Lr': 103, +} + + +def get_atom_number(symbol): + """Given an element symbol, return the atomic number (number of protons). + + Args: + symbol(str): The atomic symbol for the given element. + + Returns: + The atomic number (number of protons) of the element. If no species + could be found, returns None. + """ + number = _numbers.get(symbol) + if number is None: + raise KeyError("The element symbol '{}' is not valid. Remember to use the capitalized form of the name.".format(symbol)) + return number + + +def get_atom_symbol(number): + """Given an atomic number, returns the symbol for that element. + + Args: + number(int): The atomic number (number of protons) of the element. + + Returns: + The atomic symbol for the given element. If no species could be found, + returns None. + """ + try: + symbol = _symbols[number] + except IndexError: + raise IndexError("No element with atomic number '{}'.".format(number)) + + return symbol diff --git a/common/python/nomadcore/local_backend.py b/common/python/nomadcore/local_backend.py index ee3f543dc8fcdd40237ebafcbec33f6cb70eedf5..355bacdfa46fddce9edbfcaf3eb066d3c4a63478 100644 --- a/common/python/nomadcore/local_backend.py +++ b/common/python/nomadcore/local_backend.py @@ -96,10 +96,10 @@ class LocalBackend(object): # Check the type dtype_str = dataManager.metaInfo.dtypeStr - numpy_types = self.python_type_for_metainfo_type(dtype_str) + single_types = self.single_value_type_for_metainfo_type(dtype_str) actual_numpy_type = type(value) - if actual_numpy_type not in numpy_types: - raise TypeError("The given value for metainfo '{}' is of incorrrect type. The type was '{}' when it should be one of '{}'".format(metaName, actual_numpy_type, numpy_types)) + if actual_numpy_type not in single_types: + raise TypeError("The given value for metainfo '{}' is of incorrrect type. The type was '{}' when it should be one of '{}'".format(metaName, actual_numpy_type, single_types)) dataManager.superSectionManager.addValue(dataManager.metaInfo, value, gIndex) @@ -143,44 +143,44 @@ class LocalBackend(object): # Check the type dtype_str = dataManager.metaInfo.dtypeStr - numpy_types = self.numpy_type_for_metainfo_type(dtype_str) + array_types = self.array_type_for_metainfo_type(dtype_str) actual_numpy_type = values.dtype.type - if actual_numpy_type not in numpy_types: + if actual_numpy_type not in array_types: raise TypeError("The given array for metainfo '{}' has incorrect type of values in it. The values given are '{}', whereas the datatype given in metainfo is '{}'".format(metaName, actual_numpy_type, dtype_str)) dataManager.superSectionManager.addArrayValues(dataManager.metaInfo, values, gIndex) - def numpy_type_for_metainfo_type(self, dtypeStr): + def array_type_for_metainfo_type(self, dtypeStr): """Returns a list of numpy types correspoding to the dtypeStr of a metainfo. """ if dtypeStr == "f": - return [np.float_, np.float32, np.int_] + return [np.float_, np.float64, np.float32, np.int_, np.int64, np.int32, np.int16, np.int8] elif dtypeStr == "i": - return [np.int_] + return [np.int_, np.int64, np.int32, np.int16, np.int8] elif dtypeStr == "b": return [np.bool_] elif dtypeStr == "C": return [np.string_, np.unicode_] elif dtypeStr == "r": - return [np.int_] + return [np.int_, np.int64, np.int32, np.int16, np.int8] else: raise TypeError("Could not determine the numpy type for metainfo type '{}'".format(dtypeStr)) - def python_type_for_metainfo_type(self, dtypeStr): - """Returns a list of numpy types correspoding to the dtypeStr of a + def single_value_type_for_metainfo_type(self, dtypeStr): + """Returns a list of numpy types corresponding to the dtypeStr of a metainfo. """ if dtypeStr == "f": - return [float, int, np.float64] + return [float, int, np.float_, np.float64, np.float32] elif dtypeStr == "i": - return [int] + return [int, np.int_, np.int64, np.int32, np.int16, np.int8] elif dtypeStr == "b": - return [bool] + return [bool, np.bool_] elif dtypeStr == "C": - return [type(b""), type(u""), str] + return [type(b""), type(u""), str, np.string_, np.unicode_] elif dtypeStr == "r": - return [int] + return [int, np.int_, np.int64, np.int32, np.int16, np.int8] else: raise TypeError("Could not determine the type for metainfo type '{}'".format(dtypeStr)) diff --git a/common/python/nomadcore/simple_parser.py b/common/python/nomadcore/simple_parser.py index e7133b41f7dfe08acb28c4c088f51c31436aa6e5..77795ad2b3cf854a64d24ea0fbdff3af96b74a2b 100644 --- a/common/python/nomadcore/simple_parser.py +++ b/common/python/nomadcore/simple_parser.py @@ -607,8 +607,6 @@ class CompiledMatcher(object): def addValue(self, backend, metaName, value): """adds a value with unit conversions (only for the groups in start and endRe)""" - if metaName == "x_nwchem_qmd_step_dipole": - print(value) converter = self.converters.get(metaName, None) if converter: value = converter(value) @@ -636,16 +634,15 @@ class CompiledMatcher(object): result_dict = {} # If a separate transform function was defined in the matcher, call it - # and ignore any named groups. if self.matcher.startReTransform is not None: self.matcher.startReTransform(parser.backend, m.groups()) - else: - for k,v in sorted(m.groupdict().items()): - if v is None: - # a group may be optional (subexpression of ? or | in regex) - continue - k_converted, v_converted = self.addStrValue(parser.backend, k, v) - result_dict[k_converted] = v_converted + + for k, v in sorted(m.groupdict().items()): + if v is None: + # a group may be optional (subexpression of ? or | in regex) + continue + k_converted, v_converted = self.addStrValue(parser.backend, k, v) + result_dict[k_converted] = v_converted # Emit fixed start values if self.matcher.fixedStartValues: