nomad-parser-plugins-electronic 1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (272) hide show
  1. electronicparsers/__init__.py +1162 -0
  2. electronicparsers/abacus/__init__.py +20 -0
  3. electronicparsers/abacus/__main__.py +32 -0
  4. electronicparsers/abacus/metainfo/__init__.py +19 -0
  5. electronicparsers/abacus/metainfo/abacus.py +836 -0
  6. electronicparsers/abacus/nomad_plugin.yaml +44 -0
  7. electronicparsers/abacus/parser.py +1975 -0
  8. electronicparsers/abinit/__init__.py +19 -0
  9. electronicparsers/abinit/__main__.py +31 -0
  10. electronicparsers/abinit/metainfo/__init__.py +20 -0
  11. electronicparsers/abinit/metainfo/abinit.py +871 -0
  12. electronicparsers/abinit/metainfo/abinit_autogenerated.py +6794 -0
  13. electronicparsers/abinit/nomad_plugin.yaml +43 -0
  14. electronicparsers/abinit/parser.py +1922 -0
  15. electronicparsers/ams/__init__.py +19 -0
  16. electronicparsers/ams/__main__.py +31 -0
  17. electronicparsers/ams/metainfo/__init__.py +19 -0
  18. electronicparsers/ams/metainfo/ams.py +769 -0
  19. electronicparsers/ams/nomad_plugin.yaml +19 -0
  20. electronicparsers/ams/parser.py +1849 -0
  21. electronicparsers/ams/rkf.py +620 -0
  22. electronicparsers/atk/__init__.py +19 -0
  23. electronicparsers/atk/__main__.py +31 -0
  24. electronicparsers/atk/metainfo/__init__.py +19 -0
  25. electronicparsers/atk/metainfo/atk.py +71 -0
  26. electronicparsers/atk/nomad_plugin.yaml +34 -0
  27. electronicparsers/atk/parser.py +388 -0
  28. electronicparsers/bigdft/__init__.py +19 -0
  29. electronicparsers/bigdft/__main__.py +31 -0
  30. electronicparsers/bigdft/metainfo/__init__.py +18 -0
  31. electronicparsers/bigdft/nomad_plugin.yaml +19 -0
  32. electronicparsers/bigdft/parser.py +372 -0
  33. electronicparsers/castep/__init__.py +19 -0
  34. electronicparsers/castep/__main__.py +31 -0
  35. electronicparsers/castep/metainfo/__init__.py +19 -0
  36. electronicparsers/castep/metainfo/castep.py +2481 -0
  37. electronicparsers/castep/nomad_plugin.yaml +28 -0
  38. electronicparsers/castep/parser.py +1680 -0
  39. electronicparsers/charmm/__init__.py +19 -0
  40. electronicparsers/charmm/__main__.py +31 -0
  41. electronicparsers/charmm/metainfo/__init__.py +19 -0
  42. electronicparsers/charmm/metainfo/charmm.py +1931 -0
  43. electronicparsers/charmm/nomad_plugin.yaml +18 -0
  44. electronicparsers/charmm/parser.py +61 -0
  45. electronicparsers/cp2k/__init__.py +19 -0
  46. electronicparsers/cp2k/__main__.py +31 -0
  47. electronicparsers/cp2k/metainfo/__init__.py +19 -0
  48. electronicparsers/cp2k/metainfo/cp2k.py +59185 -0
  49. electronicparsers/cp2k/metainfo/cp2k_general.py +1014 -0
  50. electronicparsers/cp2k/nomad_plugin.yaml +49 -0
  51. electronicparsers/cp2k/parser.py +2324 -0
  52. electronicparsers/cpmd/__init__.py +19 -0
  53. electronicparsers/cpmd/__main__.py +31 -0
  54. electronicparsers/cpmd/metainfo/__init__.py +19 -0
  55. electronicparsers/cpmd/metainfo/cpmd.py +10733 -0
  56. electronicparsers/cpmd/metainfo/cpmd_general.py +988 -0
  57. electronicparsers/cpmd/nomad_plugin.yaml +19 -0
  58. electronicparsers/cpmd/parser.py +680 -0
  59. electronicparsers/crystal/__init__.py +19 -0
  60. electronicparsers/crystal/__main__.py +31 -0
  61. electronicparsers/crystal/metainfo/__init__.py +19 -0
  62. electronicparsers/crystal/metainfo/crystal.py +619 -0
  63. electronicparsers/crystal/nomad_plugin.yaml +36 -0
  64. electronicparsers/crystal/parser.py +1745 -0
  65. electronicparsers/dmol3/__init__.py +19 -0
  66. electronicparsers/dmol3/__main__.py +31 -0
  67. electronicparsers/dmol3/metainfo/__init__.py +19 -0
  68. electronicparsers/dmol3/metainfo/dmol3.py +664 -0
  69. electronicparsers/dmol3/nomad_plugin.yaml +19 -0
  70. electronicparsers/dmol3/parser.py +750 -0
  71. electronicparsers/edmft/__init__.py +19 -0
  72. electronicparsers/edmft/__main__.py +31 -0
  73. electronicparsers/edmft/metainfo/__init__.py +19 -0
  74. electronicparsers/edmft/metainfo/edmft.py +96 -0
  75. electronicparsers/edmft/nomad_plugin.yaml +53 -0
  76. electronicparsers/edmft/parser.py +912 -0
  77. electronicparsers/elk/__init__.py +19 -0
  78. electronicparsers/elk/__main__.py +31 -0
  79. electronicparsers/elk/metainfo/__init__.py +19 -0
  80. electronicparsers/elk/metainfo/elk.py +775 -0
  81. electronicparsers/elk/nomad_plugin.yaml +19 -0
  82. electronicparsers/elk/parser.py +493 -0
  83. electronicparsers/exciting/__init__.py +19 -0
  84. electronicparsers/exciting/__main__.py +31 -0
  85. electronicparsers/exciting/metainfo/__init__.py +19 -0
  86. electronicparsers/exciting/metainfo/exciting.py +2942 -0
  87. electronicparsers/exciting/nomad_plugin.yaml +83 -0
  88. electronicparsers/exciting/parser.py +3398 -0
  89. electronicparsers/fhiaims/__init__.py +19 -0
  90. electronicparsers/fhiaims/__main__.py +31 -0
  91. electronicparsers/fhiaims/metainfo/__init__.py +19 -0
  92. electronicparsers/fhiaims/metainfo/fhi_aims.py +1565 -0
  93. electronicparsers/fhiaims/nomad_plugin.yaml +52 -0
  94. electronicparsers/fhiaims/parser.py +2582 -0
  95. electronicparsers/fleur/__init__.py +19 -0
  96. electronicparsers/fleur/__main__.py +31 -0
  97. electronicparsers/fleur/metainfo/__init__.py +19 -0
  98. electronicparsers/fleur/metainfo/fleur.py +623 -0
  99. electronicparsers/fleur/nomad_plugin.yaml +19 -0
  100. electronicparsers/fleur/parser.py +952 -0
  101. electronicparsers/fplo/__init__.py +19 -0
  102. electronicparsers/fplo/__main__.py +31 -0
  103. electronicparsers/fplo/metainfo/__init__.py +19 -0
  104. electronicparsers/fplo/metainfo/fplo.py +188 -0
  105. electronicparsers/fplo/metainfo/fplo_input_autogenerated.py +2744 -0
  106. electronicparsers/fplo/metainfo/fplo_temporaries.py +319 -0
  107. electronicparsers/fplo/nomad_plugin.yaml +19 -0
  108. electronicparsers/fplo/parser.py +42 -0
  109. electronicparsers/gamess/__init__.py +19 -0
  110. electronicparsers/gamess/__main__.py +31 -0
  111. electronicparsers/gamess/metainfo/__init__.py +19 -0
  112. electronicparsers/gamess/metainfo/gamess.py +837 -0
  113. electronicparsers/gamess/nomad_plugin.yaml +19 -0
  114. electronicparsers/gamess/parser.py +941 -0
  115. electronicparsers/gaussian/__init__.py +19 -0
  116. electronicparsers/gaussian/__main__.py +31 -0
  117. electronicparsers/gaussian/metainfo/__init__.py +19 -0
  118. electronicparsers/gaussian/metainfo/gaussian.py +1707 -0
  119. electronicparsers/gaussian/nomad_plugin.yaml +18 -0
  120. electronicparsers/gaussian/parser.py +1556 -0
  121. electronicparsers/gpaw/__init__.py +19 -0
  122. electronicparsers/gpaw/__main__.py +31 -0
  123. electronicparsers/gpaw/metainfo/__init__.py +19 -0
  124. electronicparsers/gpaw/metainfo/gpaw.py +214 -0
  125. electronicparsers/gpaw/nomad_plugin.yaml +19 -0
  126. electronicparsers/gpaw/parser.py +671 -0
  127. electronicparsers/magres/__init__.py +19 -0
  128. electronicparsers/magres/__main__.py +31 -0
  129. electronicparsers/magres/metainfo/__init__.py +19 -0
  130. electronicparsers/magres/metainfo/magres.py +110 -0
  131. electronicparsers/magres/nomad_plugin.yaml +18 -0
  132. electronicparsers/magres/parser.py +505 -0
  133. electronicparsers/molcas/__init__.py +19 -0
  134. electronicparsers/molcas/__main__.py +31 -0
  135. electronicparsers/molcas/metainfo/__init__.py +19 -0
  136. electronicparsers/molcas/metainfo/molcas.py +151 -0
  137. electronicparsers/molcas/nomad_plugin.yaml +19 -0
  138. electronicparsers/molcas/parser.py +59 -0
  139. electronicparsers/mopac/__init__.py +19 -0
  140. electronicparsers/mopac/__main__.py +31 -0
  141. electronicparsers/mopac/metainfo/__init__.py +19 -0
  142. electronicparsers/mopac/metainfo/mopac.py +79 -0
  143. electronicparsers/mopac/nomad_plugin.yaml +19 -0
  144. electronicparsers/mopac/parser.py +304 -0
  145. electronicparsers/nwchem/__init__.py +19 -0
  146. electronicparsers/nwchem/__main__.py +31 -0
  147. electronicparsers/nwchem/metainfo/__init__.py +19 -0
  148. electronicparsers/nwchem/metainfo/nwchem.py +487 -0
  149. electronicparsers/nwchem/nomad_plugin.yaml +19 -0
  150. electronicparsers/nwchem/parser.py +757 -0
  151. electronicparsers/ocean/__init__.py +19 -0
  152. electronicparsers/ocean/__main__.py +31 -0
  153. electronicparsers/ocean/metainfo/__init__.py +19 -0
  154. electronicparsers/ocean/metainfo/ocean.py +410 -0
  155. electronicparsers/ocean/nomad_plugin.yaml +37 -0
  156. electronicparsers/ocean/parser.py +344 -0
  157. electronicparsers/octopus/__init__.py +19 -0
  158. electronicparsers/octopus/__main__.py +31 -0
  159. electronicparsers/octopus/metainfo/__init__.py +19 -0
  160. electronicparsers/octopus/metainfo/octopus.py +71 -0
  161. electronicparsers/octopus/metainfo/octopus_autogenerated.py +11647 -0
  162. electronicparsers/octopus/nomad_plugin.yaml +41 -0
  163. electronicparsers/octopus/parser.py +1214 -0
  164. electronicparsers/onetep/__init__.py +19 -0
  165. electronicparsers/onetep/__main__.py +31 -0
  166. electronicparsers/onetep/metainfo/__init__.py +19 -0
  167. electronicparsers/onetep/metainfo/onetep.py +3443 -0
  168. electronicparsers/onetep/nomad_plugin.yaml +19 -0
  169. electronicparsers/onetep/parser.py +577 -0
  170. electronicparsers/openmx/__init__.py +19 -0
  171. electronicparsers/openmx/__main__.py +31 -0
  172. electronicparsers/openmx/metainfo/__init__.py +19 -0
  173. electronicparsers/openmx/metainfo/openmx.py +24 -0
  174. electronicparsers/openmx/nomad_plugin.yaml +25 -0
  175. electronicparsers/openmx/parser.py +918 -0
  176. electronicparsers/orca/__init__.py +19 -0
  177. electronicparsers/orca/__main__.py +31 -0
  178. electronicparsers/orca/metainfo/__init__.py +19 -0
  179. electronicparsers/orca/metainfo/orca.py +1448 -0
  180. electronicparsers/orca/nomad_plugin.yaml +19 -0
  181. electronicparsers/orca/parser.py +1371 -0
  182. electronicparsers/psi4/__init__.py +19 -0
  183. electronicparsers/psi4/__main__.py +31 -0
  184. electronicparsers/psi4/metainfo/__init__.py +19 -0
  185. electronicparsers/psi4/metainfo/psi4.py +372 -0
  186. electronicparsers/psi4/nomad_plugin.yaml +27 -0
  187. electronicparsers/psi4/parser.py +1362 -0
  188. electronicparsers/qball/__init__.py +19 -0
  189. electronicparsers/qball/__main__.py +31 -0
  190. electronicparsers/qball/metainfo/__init__.py +19 -0
  191. electronicparsers/qball/metainfo/qball.py +27 -0
  192. electronicparsers/qball/nomad_plugin.yaml +25 -0
  193. electronicparsers/qball/parser.py +152 -0
  194. electronicparsers/qbox/__init__.py +19 -0
  195. electronicparsers/qbox/__main__.py +31 -0
  196. electronicparsers/qbox/metainfo/__init__.py +19 -0
  197. electronicparsers/qbox/metainfo/qbox.py +490 -0
  198. electronicparsers/qbox/nomad_plugin.yaml +19 -0
  199. electronicparsers/qbox/parser.py +508 -0
  200. electronicparsers/quantumespresso/__init__.py +19 -0
  201. electronicparsers/quantumespresso/__main__.py +31 -0
  202. electronicparsers/quantumespresso/metainfo/__init__.py +19 -0
  203. electronicparsers/quantumespresso/metainfo/quantum_espresso.py +3296 -0
  204. electronicparsers/quantumespresso/nomad_plugin.yaml +31 -0
  205. electronicparsers/quantumespresso/parser.py +3594 -0
  206. electronicparsers/siesta/__init__.py +19 -0
  207. electronicparsers/siesta/__main__.py +31 -0
  208. electronicparsers/siesta/metainfo/__init__.py +19 -0
  209. electronicparsers/siesta/metainfo/siesta.py +141 -0
  210. electronicparsers/siesta/metainfo/siesta_autogenerated.py +3365 -0
  211. electronicparsers/siesta/nomad_plugin.yaml +19 -0
  212. electronicparsers/siesta/parser.py +607 -0
  213. electronicparsers/soliddmft/__init__.py +19 -0
  214. electronicparsers/soliddmft/__main__.py +31 -0
  215. electronicparsers/soliddmft/metainfo/__init__.py +19 -0
  216. electronicparsers/soliddmft/metainfo/soliddmft.py +274 -0
  217. electronicparsers/soliddmft/nomad_plugin.yaml +26 -0
  218. electronicparsers/soliddmft/parser.py +727 -0
  219. electronicparsers/tbstudio/__init__.py +19 -0
  220. electronicparsers/tbstudio/__main__.py +31 -0
  221. electronicparsers/tbstudio/metainfo/__init__.py +19 -0
  222. electronicparsers/tbstudio/metainfo/tbstudio.py +34 -0
  223. electronicparsers/tbstudio/nomad_plugin.yaml +24 -0
  224. electronicparsers/tbstudio/parser.py +412 -0
  225. electronicparsers/turbomole/__init__.py +19 -0
  226. electronicparsers/turbomole/__main__.py +31 -0
  227. electronicparsers/turbomole/metainfo/__init__.py +19 -0
  228. electronicparsers/turbomole/metainfo/turbomole.py +727 -0
  229. electronicparsers/turbomole/nomad_plugin.yaml +18 -0
  230. electronicparsers/turbomole/parser.py +1585 -0
  231. electronicparsers/utils/__init__.py +20 -0
  232. electronicparsers/utils/magres_workflow.py +61 -0
  233. electronicparsers/utils/utils.py +636 -0
  234. electronicparsers/vasp/__init__.py +19 -0
  235. electronicparsers/vasp/__main__.py +31 -0
  236. electronicparsers/vasp/metainfo/__init__.py +19 -0
  237. electronicparsers/vasp/metainfo/vasp.py +169 -0
  238. electronicparsers/vasp/metainfo/vasp_incars.py +3428 -0
  239. electronicparsers/vasp/metainfo/vasp_incarsOut.py +3428 -0
  240. electronicparsers/vasp/metainfo/vasp_incarsUnknown.py +86 -0
  241. electronicparsers/vasp/nomad_plugin.yaml +28 -0
  242. electronicparsers/vasp/parser.py +2340 -0
  243. electronicparsers/w2dynamics/__init__.py +19 -0
  244. electronicparsers/w2dynamics/__main__.py +31 -0
  245. electronicparsers/w2dynamics/metainfo/__init__.py +19 -0
  246. electronicparsers/w2dynamics/metainfo/w2dynamics.py +1270 -0
  247. electronicparsers/w2dynamics/nomad_plugin.yaml +36 -0
  248. electronicparsers/w2dynamics/parser.py +674 -0
  249. electronicparsers/wannier90/__init__.py +19 -0
  250. electronicparsers/wannier90/__main__.py +31 -0
  251. electronicparsers/wannier90/metainfo/__init__.py +19 -0
  252. electronicparsers/wannier90/metainfo/wannier90.py +56 -0
  253. electronicparsers/wannier90/nomad_plugin.yaml +34 -0
  254. electronicparsers/wannier90/parser.py +671 -0
  255. electronicparsers/wien2k/__init__.py +19 -0
  256. electronicparsers/wien2k/__main__.py +31 -0
  257. electronicparsers/wien2k/metainfo/__init__.py +19 -0
  258. electronicparsers/wien2k/metainfo/wien2k.py +783 -0
  259. electronicparsers/wien2k/nomad_plugin.yaml +18 -0
  260. electronicparsers/wien2k/parser.py +1133 -0
  261. electronicparsers/yambo/__init__.py +19 -0
  262. electronicparsers/yambo/__main__.py +31 -0
  263. electronicparsers/yambo/metainfo/__init__.py +19 -0
  264. electronicparsers/yambo/metainfo/yambo.py +577 -0
  265. electronicparsers/yambo/nomad_plugin.yaml +31 -0
  266. electronicparsers/yambo/parser.py +896 -0
  267. nomad_parser_plugins_electronic-1.0.dist-info/LICENSE +202 -0
  268. nomad_parser_plugins_electronic-1.0.dist-info/METADATA +360 -0
  269. nomad_parser_plugins_electronic-1.0.dist-info/RECORD +272 -0
  270. nomad_parser_plugins_electronic-1.0.dist-info/WHEEL +5 -0
  271. nomad_parser_plugins_electronic-1.0.dist-info/entry_points.txt +43 -0
  272. nomad_parser_plugins_electronic-1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,620 @@
1
+ import numpy
2
+ import os
3
+ import struct
4
+ from bisect import bisect
5
+ from collections import OrderedDict
6
+
7
+ from nomad.utils import get_logger
8
+
9
+ logger = get_logger(__name__)
10
+
11
+ __all__ = ['KFFile', 'KFReader', 'KFHistory']
12
+
13
+
14
+ def rkf_to_dict(rkffile):
15
+ """
16
+ return a python dictionary of an rkf file
17
+ """
18
+ data = KFFile(rkffile)
19
+ json_data = {}
20
+ all_sections = data.sections()
21
+ for sections in all_sections:
22
+ json_data[sections] = data.read_section(sections)
23
+ return json_data
24
+
25
+
26
+ class KFReader:
27
+ """A python class to read binary files in KF format.This class offers read-only access to any
28
+ fragment of data from a KF file.Unlike other Python KF readers, this one does not use the
29
+ Fortran binary ``dmpkf`` to process KF files, but instead reads and interprets raw binary
30
+ data straight from the file, on Python level. That approach results in significant speedup
31
+ (by a factor of few hundreds for large files extracted variable by variable).
32
+ The constructor argument *path* should be a string with a path (relative or absolute)
33
+ to an existing KF file.*blocksize* indicates the length of basic KF file block.
34
+ So far, all KF files produced by any of Amsterdam Modeling Suite programs have the same block
35
+ size of 4096 bytes. Organization of data inside KF file can depend on a machine on which
36
+ this file was produced. Two parameters can vary: the length of integer (32 or 64 bit) and
37
+ endian (little or big). These parameters have to be determined before any reading can take place,
38
+ otherwise the results will have no sense. If the constructor argument *autodetect* is ``True``,
39
+ the constructor attempts to automatically detect the format of a given KF file, allowing to read
40
+ files created on a machine with different endian or integer length. This automatic detection is enabled
41
+ by default and it is advised to leave it that way. If you wish to disable it, you should set ``endian``
42
+ and ``word`` attributes manually before reading anything (see the code for details).
43
+ .. note ::
44
+ This class consists of quite technical, low level code. If you don't need to modify or
45
+ extend |KFReader|, you can safely ignore all private methods, all you need is
46
+ :meth:`~KFReader.read` and occasionally :meth:`~KFReader.__iter__`
47
+
48
+ """
49
+
50
+ _sizes = {'s': 1, 'i': 4, 'd': 8, 'q': 8}
51
+
52
+ def __init__(self, path, blocksize=4096, autodetect=True):
53
+ if os.path.isfile(path):
54
+ self.path = os.path.abspath(path)
55
+ else:
56
+ logger.warning('File {} not present'.format(path))
57
+
58
+ self._blocksize = blocksize
59
+ self.endian = '<' # endian: '<' = little, '>' = big
60
+ self.word = 'i' # length of int: 'i' = 4 bits, 'q' = 8 bits
61
+ self._sections = None
62
+ if autodetect:
63
+ self._autodetect()
64
+
65
+ def read(self, section, variable):
66
+ """
67
+ Extract and return data for a *variable* located in a *section*.
68
+ For single-value numerical or boolean variables returned value is
69
+ a single number or bool. For longer variables this method returns a
70
+ list of values. For string variables a single string is returned.
71
+ """
72
+
73
+ if self._sections is None:
74
+ self._create_index()
75
+
76
+ try:
77
+ tmp = self._sections[section]
78
+ except KeyError:
79
+ raise KeyError('Section {} not present in {}'.format(section, self.path))
80
+ try:
81
+ vtype, vlb, vstart, vlen = tmp[variable]
82
+ except KeyError:
83
+ raise KeyError(
84
+ 'Variable {} not present in section {} of {}'.format(
85
+ variable, section, self.path
86
+ )
87
+ )
88
+
89
+ ret = []
90
+ first = True
91
+ with open(self.path, 'rb') as f:
92
+ for i in KFReader._datablocks(self._data[section], vlb):
93
+ if first:
94
+ ret = self._get_data(self._read_block(f, i), vtype)[vstart - 1 :]
95
+ first = False
96
+ else:
97
+ ret += self._get_data(self._read_block(f, i), vtype)
98
+ if len(ret) >= vlen:
99
+ ret = ret[:vlen]
100
+ if isinstance(ret, bytes):
101
+ try:
102
+ return ret.decode()
103
+ except UnicodeDecodeError:
104
+ return ret.decode('Latin-1')
105
+ elif len(ret) == 1:
106
+ return ret[0]
107
+ else:
108
+ return ret
109
+
110
+ def __iter__(self):
111
+ """Iteration yields pairs of section name and variable name."""
112
+ if self._sections is None:
113
+ self._create_index()
114
+ for section in self._sections:
115
+ for variable in self._sections[section]:
116
+ yield section, variable
117
+
118
+ def _autodetect(self):
119
+ """
120
+ Try to automatically detect the format (int size and endian) of
121
+ this KF file.
122
+ """
123
+ with open(self.path, 'rb') as f:
124
+ b = f.read(128)
125
+
126
+ blocksize = struct.unpack(b'i', b[28:32])[0]
127
+ self._blocksize = 4096 if blocksize == 538976288 else blocksize
128
+
129
+ one = b[80:84]
130
+
131
+ if struct.unpack(b'32s', b[48:80])[0] == b'SUPERINDEX ':
132
+ self.word = 'i'
133
+ elif struct.unpack(b'32s', b[64:96])[0] == b'SUPERINDEX ':
134
+ self.word = 'q'
135
+ one = b[96:104]
136
+ else:
137
+ pass
138
+ return
139
+
140
+ for e in ['<', '>']:
141
+ if struct.unpack(str(e + self.word), one)[0] == 1:
142
+ self.endian = e
143
+ # d = {'q': '8 bytes', 'i': '4 bytes', '<': 'little endian', '>': 'big endian'}
144
+
145
+ def _read_block(self, f, pos):
146
+ """Read a single block of binary data from posistion *pos* in file *f*."""
147
+ f.seek((pos - 1) * self._blocksize)
148
+ return f.read(self._blocksize)
149
+
150
+ def _parse(self, block, format): # format = [(32,'s'),(4,'i'),(2,'d')]
151
+ """
152
+ Translate a *block* of binary data into list of values in specified *format*.
153
+ *format* should be a list of pairs *(a,t)* where *t* is one of the following
154
+ characters: ``'s'`` for string (bytes), ``'i'`` for 32-bit integer, ``'q'`` for
155
+ 64-bit integer and *a* is the number of occurrences (or length of a string).
156
+ For example, if *format* is equal to ``[(32,'s'),(4,'i'),(2,'d'),(2,'i')]``,
157
+ the contents of *block* are divided into 72 bytes (32*1 + 4*4 + 2*8 + 2*4 = 72)
158
+ chunks (possibly droping the last one, if it's shorter than 72 bytes).
159
+ Then each chunk is translated to a 9-tuple of bytes, 4 ints, 2 floats and 2 ints.
160
+ List of such tuples is the returned value.
161
+ """
162
+ step = 0
163
+ formatstring = self.endian
164
+ for a, t in format:
165
+ step += a * self._sizes[t]
166
+ formatstring += str(a) + t
167
+
168
+ if step > 0:
169
+ end = (len(block) // step) * step
170
+ return list(struct.iter_unpack(formatstring, block[:end]))
171
+ else:
172
+ return []
173
+
174
+ def _get_data(self, datablock, vtype):
175
+ """Extract all data of a given type from a single data block.
176
+ Returned value is a list of values (int, float, or bool) or a
177
+ single "bytes" object.
178
+ """
179
+ hlen = 4 * self._sizes[self.word]
180
+ i, d, s, b = self._parse(datablock[:hlen], [(4, self.word)])[0]
181
+ contents = self._parse(
182
+ datablock[hlen:], zip((i, d, s, b), (self.word, 'd', 's', self.word))
183
+ )
184
+ if contents:
185
+ contents = contents[
186
+ 0
187
+ ] # there won't be more than one chunk of data in any data block
188
+ if vtype == 1:
189
+ return list(contents[:i])
190
+ elif vtype == 2:
191
+ return list(contents[i : i + d])
192
+ elif vtype == 3:
193
+ return contents[i + d]
194
+ elif vtype == 4:
195
+ return list(map(bool, contents[i + d + 1 :]))
196
+ else:
197
+ raise KeyError('Unknown vtype')
198
+ else:
199
+ return []
200
+
201
+ def _create_index(self):
202
+ """Find and parse relevant index blocks of KFFile to extract the information
203
+ about location of all sections and variables.
204
+
205
+ Two dictionaries are populated during this process. ``_data`` contains, for each
206
+ section, a list of triples describing how logical blocks of data are mapped into
207
+ physical ones. For example, ``_data['General'] = [(3,6,12), (9,40,45)]`` means
208
+ that logical blocks 3-8 of section ``General`` are located in physical blocks
209
+ 6-11 and logical blocks 9-13 in physical blocks 40-44. This list is always sorted
210
+ via first tuple elements allowing efficient access to arbitrary logical block of each section.
211
+
212
+ The second dictionary, ``_sections``, is used to locate each variable within its
213
+ section. For each section, it contains another dictionary of each variable of this
214
+ section. So ``_section[sec][var]`` contains all information needed to extract
215
+ variable ``var`` from section ``sec``. This is a 4-tuple containing the following
216
+ information: variable type, logic block in which the variable first occurs, position
217
+ within this block where its data start and the length of the variable. Combining
218
+ this information with mapping stored in ``_data`` allows to extract each single variable.
219
+ """
220
+
221
+ hlen = 32 + 7 * self._sizes[self.word] # length of index block header
222
+
223
+ with open(self.path, 'rb') as f:
224
+ superlist = self._parse(self._read_block(f, 1), [(32, 's'), (4, self.word)])
225
+ nextsuper = superlist[0][4]
226
+ while nextsuper != 1:
227
+ nsl = self._parse(
228
+ self._read_block(f, nextsuper), [(32, 's'), (4, self.word)]
229
+ )
230
+ nextsuper = nsl[0][4]
231
+ superlist += nsl
232
+
233
+ self._data = {} # list of triples to convert logical to physical block numbers
234
+ self._sections = {}
235
+ for (
236
+ key,
237
+ pb,
238
+ lb,
239
+ le,
240
+ ty,
241
+ ) in superlist: # pb=physical block, lb=logical block, le=length, ty=type (3 for index, 4 for data)
242
+ try:
243
+ key = key.decode()
244
+ except UnicodeDecodeError:
245
+ key = key.decode('Latin-1')
246
+ key = key.rstrip(' ')
247
+ if key in ['SUPERINDEX', 'EMPTY']:
248
+ continue
249
+ if ty == 4: # data block
250
+ if key not in self._data:
251
+ self._data[key] = []
252
+ self._data[key].append((lb, pb, pb + le))
253
+ elif ty == 3: # index block
254
+ if key not in self._sections:
255
+ self._sections[key] = {}
256
+ for i in range(le):
257
+ indexblock = self._read_block(f, pb + i)
258
+ body = self._parse(
259
+ indexblock[hlen:], [(32, 's'), (6, self.word)]
260
+ )
261
+ for var, vlb, vstart, _, _, vused, vtype in body:
262
+ try:
263
+ var = var.decode()
264
+ except UnicodeDecodeError:
265
+ var = var.decode('Latin-1')
266
+ var = var.rstrip(' ')
267
+ if var == 'EMPTY':
268
+ continue
269
+ self._sections[key][var] = (vtype, vlb, vstart, vused)
270
+
271
+ for k, v in self._data.items():
272
+ lbs = []
273
+ pbs = []
274
+ for lb, first, last in sorted(v):
275
+ lbs.append(lb)
276
+ pbs.append((first, last))
277
+ self._data[k] = (lbs, pbs)
278
+
279
+ @staticmethod
280
+ def _datablocks(lst, n=1):
281
+ """Transform a tuple of lists ``([x1,x2,...], [(a1,b1),(a2,b2),...])`` into an iterator over ``range(a1,b1)+range(a2,b2)+...`` Iteration starts from nth element of this list."""
282
+ i = bisect(lst[0], n) - 1
283
+ lb = lst[0][i]
284
+ first, last = lst[1][i]
285
+ ret = first + n - lb
286
+ while i < len(lst[1]):
287
+ while ret < last:
288
+ yield ret
289
+ ret += 1
290
+ i += 1
291
+ if i < len(lst[1]):
292
+ ret, last = lst[1][i]
293
+
294
+
295
+ class KFFile:
296
+ """A class for reading and writing binary files in KF format.
297
+
298
+ This class acts as a wrapper around |KFReader| collecting all the data written by user
299
+ in some "temporary zone" and using Fortran binaries ``udmpkf`` and ``cpkf`` to write
300
+ this data to the physical file when needed.
301
+
302
+ The constructor argument *path* should be a string with a path to an existing KF file
303
+ or a new KF file that you wish to create. If a path to existing file is passed, new
304
+ |KFReader| instance is created allowing to read all the data from this file.
305
+
306
+ When :meth:`~KFFile.write` method is used, the new data is not immediately written to
307
+ a disk. Instead of that, it is temporarily stored in ``tmpdata`` dictionary. When
308
+ method :meth:`~KFFile.save` is invoked, contents of that dictionary are written to a
309
+ physical file and ``tmpdata`` is emptied.
310
+
311
+ Other methods like :meth:`~KFFile.read` or :meth:`~KFFile.delete_section` are aware of
312
+ ``tmpdata`` and work flawlessly, regardless if :meth:`~KFFile.save` was called or not.
313
+
314
+ By default, :meth:`~KFFile.save` is automatically invoked after each :meth:`~KFFile.write`,
315
+ so physical file on a disk is always "actual". This behavior can be adjusted with *autosave*
316
+ constructor parameter. Having autosave enabled is usually a good idea, however, if you
317
+ need to write a lot of small pieces of data to your file, the overhead of calling
318
+ ``udmpkf`` and ``cpkf`` after *every* :meth:`~KFFile.write` can lead to significant
319
+ delays. In such a case it is advised to disable autosave and call :meth:`~KFFile.save`
320
+ manually, when needed.
321
+
322
+ Dictionary-like bracket notation can be used as a shortcut to read and write variables::
323
+
324
+ mykf = KFFile('someexistingkffile.kf')
325
+ #all three below are equivalent
326
+ x = mykf['General%Termination Status']
327
+ x = mykf[('General','Termination Status')]
328
+ x = mykf.read('General','Termination Status')
329
+
330
+ #all three below are equivalent
331
+ mykf['Geometry%xyz'] = somevariable
332
+ mykf[('Geometry','xyz')] = somevariable
333
+ mykf.write('Geometry','xyz', somevariable)
334
+
335
+ """
336
+
337
+ _types = {
338
+ int: (1, 8, lambda x: '%10i' % x),
339
+ float: (2, 3, lambda x: '%26.16e' % x),
340
+ str: (3, 80, lambda x: x),
341
+ bool: (4, 80, lambda x: 'T' if x else 'F'),
342
+ }
343
+
344
+ def __init__(self, path, autosave=True):
345
+ self.autosave = autosave
346
+ self.path = os.path.abspath(path)
347
+ self.tmpdata = OrderedDict()
348
+ self.reader = KFReader(self.path)
349
+
350
+ def read(self, section, variable, return_as_list=False):
351
+ """Extract and return data for a *variable* located in a *section*.
352
+
353
+ By default, for single-value numerical or boolean variables returned value is a
354
+ single number or bool. For longer variables this method returns a list of values.
355
+ For string variables a single string is returned. This behavior can be changed by
356
+ setting *return_as_list* parameter to ``True``. In that case the returned value is
357
+ always a list of numbers (possibly of length 1) or a single string.
358
+ """
359
+ if section in self.tmpdata and variable in self.tmpdata[section]:
360
+ ret = self.tmpdata[section][variable]
361
+ else:
362
+ ret = self.reader.read(section, variable)
363
+ if return_as_list and isinstance(ret, (int, float, bool)):
364
+ ret = [ret]
365
+ return ret
366
+
367
+ def write(self, section, variable, value):
368
+ """Write a *variable* with a *value* in a *section* . If such a variable already
369
+ exists in this section, the old value is overwritten."""
370
+ if not isinstance(value, (int, bool, float, str, list)):
371
+ raise ValueError('Trying to store improper value in KFFile')
372
+ if isinstance(value, list):
373
+ if len(value) == 0:
374
+ raise ValueError('Cannot store empty lists in KFFile')
375
+ if any(not isinstance(i, type(value[0])) for i in value):
376
+ raise ValueError(
377
+ 'Lists stored in KFFile must have all elements of the same type'
378
+ )
379
+ if not isinstance(value[0], (int, bool, float, str)):
380
+ raise ValueError(
381
+ 'Only lists of int, float, str or bool can be stored in KFFile'
382
+ )
383
+
384
+ if section not in self.tmpdata:
385
+ self.tmpdata[section] = OrderedDict()
386
+ self.tmpdata[section][variable] = value
387
+
388
+ # if self.autosave:
389
+ # self.save()
390
+
391
+ def sections(self):
392
+ """Return a list with all section names, ordered alphabetically."""
393
+ ret = set(self.tmpdata)
394
+ if self.reader:
395
+ if self.reader._sections is None:
396
+ self.reader._create_index()
397
+ ret |= set(self.reader._sections)
398
+ ret = list(ret)
399
+ ret.sort()
400
+ return ret
401
+
402
+ def read_section(self, section):
403
+ """Return a dictionary with all variables from a given *section*.
404
+
405
+ .. note::
406
+
407
+ Some sections can contain very large amount of data. Turning them into
408
+ dictionaries can cause memory shortage or performance issues. Use this method
409
+ carefully.
410
+
411
+ """
412
+ ret = {}
413
+ for sec, var in self:
414
+ if sec == section:
415
+ ret[var] = self.read(sec, var)
416
+ if len(ret) == 0:
417
+ logger.warning(
418
+ "WARNING: Section '{}' not present in {} or present, but empty. Returning empty dictionary".format(
419
+ section, self.path
420
+ ),
421
+ 1,
422
+ )
423
+ return ret
424
+
425
+ def get_skeleton(self):
426
+ """Return a dictionary reflecting the structure of this KF file. Each key in that
427
+ dictionary corresponds to a section name of the KF file with the value being a set
428
+ of variable names."""
429
+ ret = {}
430
+ for sec, var in self:
431
+ if sec not in ret:
432
+ ret[sec] = set()
433
+ ret[sec].add(var)
434
+ return ret
435
+
436
+ def __getitem__(self, name):
437
+ """Allow to use ``x = mykf['section%variable']`` or ``x = mykf[('section','variable')]``
438
+ instead of ``x = kf.read('section', 'variable')``."""
439
+ section, variable = KFFile._split(name)
440
+ return self.read(section, variable)
441
+
442
+ def __setitem__(self, name, value):
443
+ """
444
+ Allow to use ``mykf['section%variable'] = value`` or ``mykf[('section','variable')] = value``
445
+ instead of ``kf.write('section', 'variable', value)``.
446
+ """
447
+ section, variable = KFFile._split(name)
448
+ self.write(section, variable, value)
449
+
450
+ def __iter__(self):
451
+ """Iteration yields pairs of section name and variable name."""
452
+ ret = set()
453
+ if self.reader:
454
+ for sec, var in self.reader:
455
+ ret.add((sec, var))
456
+ for sec in self.tmpdata:
457
+ for var in self.tmpdata[sec]:
458
+ ret.add((sec, var))
459
+ ret = list(ret)
460
+ ret.sort(key=lambda x: x[0] + x[1])
461
+ for i in ret:
462
+ yield i
463
+
464
+ def __contains__(self, arg):
465
+ """Implements Python ``in`` operator for KFFiles. *arg* can be a single string
466
+ with a section name or a pair of strings (section, variable)."""
467
+ if isinstance(arg, str):
468
+ return arg in self.sections()
469
+ if (
470
+ isinstance(arg, tuple)
471
+ and len(arg) == 2
472
+ and isinstance(arg[0], str)
473
+ and isinstance(arg[1], str)
474
+ ):
475
+ try:
476
+ self.read(*arg)
477
+ return True
478
+ except KeyError:
479
+ return False
480
+ raise TypeError(
481
+ "'in <KFFile>' requires string of a pair of strings as left operand"
482
+ )
483
+
484
+ @staticmethod
485
+ def _split(name):
486
+ """Ensure that a key used in bracket notation is of the form ``'section%variable'``
487
+ or ``('section','variable')``. If so, return a tuple ``('section','variable')``."""
488
+ if isinstance(name, tuple) and len(name) == 2:
489
+ return name[0], name[1]
490
+ if isinstance(name, str):
491
+ s = name.split('%')
492
+ if len(s) == 2:
493
+ return s[0], s[1]
494
+ raise ValueError('Improper key used in KFFile dictionary-like notation')
495
+
496
+ @staticmethod
497
+ def _str(val):
498
+ """Return a string representation of *val* in the form that can be understood by ``udmpkf``."""
499
+ if isinstance(val, (int, bool, float)):
500
+ val = [val]
501
+ valtype = type(val[0])
502
+ t, step, f = KFFile._types[valtype]
503
+ nl = len(val)
504
+ if valtype is str and isinstance(val, list):
505
+ # udmpkf reads 160 characters per variable, split over max. 80 per line, to make a string array
506
+ nl = nl * 160
507
+ step = 1
508
+ splitstrings = [[s[0:80], s[80:160]] for s in val]
509
+ val = [item for sublist in splitstrings for item in sublist]
510
+
511
+ ret = '%10i%10i%10i' % (nl, nl, t)
512
+ for i, el in enumerate(val):
513
+ if i % step == 0:
514
+ ret += '\n'
515
+ ret += f(el)
516
+ return ret
517
+
518
+
519
+ class KFHistory:
520
+ """A class for reading "History" sections of files in the KF format.
521
+
522
+ This class acts as a wrapper around |KFReader| enabling convenient iteration over
523
+ entries (frames) of History sections.
524
+
525
+ The constructor argument *kf* should be a |KFReader| instance attached to an existing
526
+ KF file. The *section* argument then holds a name of the desired History-like section,
527
+ such as "History" or "MDHistory".
528
+
529
+ The :meth:`~KFHistory.read_all` method can be used used to easily read all values of a
530
+ particular history item into a single numpy array.
531
+
532
+ To iterate over the frames in a history section, use :meth:`~KFHistory.iter` or
533
+ :meth:`~KFHistory.iter_optional`. The former raises an exception if the selected
534
+ variable is not present in the history, while the latter returns a given default value instead.
535
+
536
+ Usage::
537
+
538
+ kf = KFReader('somefile.rkf')
539
+ mdhistory = KFHistory(kf, 'MDHistory')
540
+
541
+ for T, p in mdhistory.iter('Temperature'), mdhistory.iter_optional('Pressure', 0):
542
+ print(T, p)
543
+
544
+ """
545
+
546
+ def __init__(self, kf, section):
547
+ self.kf = kf
548
+ self.section = section
549
+ self.nsteps = kf.read(section, 'nEntries')
550
+ self.shapes = {}
551
+ self.blocked = set()
552
+
553
+ if (section, 'nBlocks') in kf:
554
+ self.nblocks = kf.read(section, 'nBlocks')
555
+ else:
556
+ self.nblocks = 0
557
+
558
+ def read_all(self, name):
559
+ """Return a numpy array containing the values of history item *name* from all frames."""
560
+ if name not in self.shapes:
561
+ self._init_shape(name)
562
+ if name in self.blocked:
563
+ return numpy.concatenate(
564
+ [
565
+ numpy.atleast_1d(
566
+ self.kf.read(self.section, '{}({})'.format(name, i))
567
+ )
568
+ for i in range(1, self.nblocks + 1)
569
+ ]
570
+ )
571
+ else:
572
+ return numpy.asarray(
573
+ [
574
+ self.kf.read(self.section, '{}({})'.format(name, i))
575
+ for i in range(1, self.nsteps + 1)
576
+ ]
577
+ )
578
+
579
+ def iter(self, name):
580
+ """Iterate over the values of history item *name*."""
581
+ if name not in self.shapes:
582
+ self._init_shape(name)
583
+ if name in self.blocked:
584
+ for i in range(1, self.nblocks + 1):
585
+ block = self.kf.read(self.section, '{}({})'.format(name, i))
586
+ try:
587
+ yield from block
588
+ except TypeError:
589
+ # one-element blocks are not iterable (KFReader returns them as scalars)
590
+ yield block
591
+ else:
592
+ for i in range(1, self.nsteps + 1):
593
+ yield self.kf.read(self.section, '{}({})'.format(name, i))
594
+
595
+ def iter_optional(self, name, default=None):
596
+ """Iterate over the values of history item *name*, returning *default* if the item is not present."""
597
+ if (self.section, name + '(1)') in self.kf:
598
+ yield from self.iter(name)
599
+ else:
600
+ while True:
601
+ yield default
602
+
603
+ def _init_shape(self, name):
604
+ shapevar = name + '(dim)'
605
+ if (self.section, shapevar) in self.kf:
606
+ shape = self.kf.read(self.section, shapevar)
607
+ try:
608
+ # shape is a list (variable "name" is at least rank-2)
609
+ self.shapes[name] = tuple(shape)
610
+ except TypeError:
611
+ # shape is a scalar (variable "name" is a scalar or rank-1)
612
+ self.shapes[name] = (shape,)
613
+ perAtomVar = name + '(perAtom)'
614
+ if self.nblocks and (self.section, perAtomVar) in self.kf:
615
+ perAtom = self.kf.read(self.section, perAtomVar)
616
+ if not perAtom and self.shapes[name] == (1,):
617
+ self.blocked.add(name)
618
+ else:
619
+ # no shape info => assume scalar/rank-1 (read as is, no blocks)
620
+ self.shapes[name] = (1,)
@@ -0,0 +1,19 @@
1
+ #
2
+ # Copyright The NOMAD Authors.
3
+ #
4
+ # This file is part of NOMAD.
5
+ # See https://nomad-lab.eu for further info.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ from .parser import ATKParser
@@ -0,0 +1,31 @@
1
+ #
2
+ # Copyright The NOMAD Authors.
3
+ #
4
+ # This file is part of NOMAD.
5
+ # See https://nomad-lab.eu for further info.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ import sys
20
+ import json
21
+ import logging
22
+
23
+ from nomad.utils import configure_logging
24
+ from nomad.datamodel import EntryArchive
25
+ from electronicparsers.atk import ATKParser
26
+
27
+ if __name__ == '__main__':
28
+ configure_logging(console_log_level=logging.DEBUG)
29
+ archive = EntryArchive()
30
+ ATKParser().parse(sys.argv[1], archive, logging)
31
+ json.dump(archive.m_to_dict(), sys.stdout, indent=2)
@@ -0,0 +1,19 @@
1
+ #
2
+ # Copyright The NOMAD Authors.
3
+ #
4
+ # This file is part of NOMAD.
5
+ # See https://nomad-lab.eu for further info.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ from . import atk