Skip to content

Commit d130955

Browse files
committed
ENH: make unknonwn CIF occupancy default to 1
Add customizable default value to the `leading_float` helper.
1 parent 17aaf13 commit d130955

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/diffpy/structure/parsers/p_cif.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def _tr_atom_site_adp_type(a, value):
156156
_tr_atom_site_thermal_displace_type = _tr_atom_site_adp_type
157157

158158
def _tr_atom_site_occupancy(a, value):
159-
a.occupancy = leading_float(value)
159+
a.occupancy = leading_float(value, 1.0)
160160
_tr_atom_site_occupancy = staticmethod(_tr_atom_site_occupancy)
161161

162162
def _tr_atom_site_aniso_U_11(a, value):
@@ -622,19 +622,36 @@ def toLines(self, stru):
622622
# constant regular expression for leading_float()
623623
rx_float = re.compile(r'[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?')
624624

625-
def leading_float(s):
626-
"""Obtain first float from a string and ignore any trailing characters.
625+
def leading_float(s, d=0.0):
626+
"""Extract the first float from a string and ignore trailing characters.
627+
627628
Useful for extracting values from "value(std)" syntax.
628629
629-
Return float.
630+
Parameters
631+
----------
632+
s : str
633+
The string to be scanned for floating point value.
634+
d : float, optional
635+
The default value when `s` is "." or "?", which in CIF
636+
format stands for inapplicable and unknown, respectively.
637+
638+
Returns
639+
-------
640+
float
641+
The extracted floating point value.
642+
643+
Raises
644+
------
645+
ValueError
646+
When string does not start with a float.
630647
"""
631648
sbare = s.strip()
632649
mx = rx_float.match(sbare)
633650
if mx:
634651
rv = float(mx.group())
635652
elif sbare == '.' or sbare == '?':
636653
# CIF files may contain "." or "?" for unknown values
637-
rv = 0.0
654+
rv = d
638655
else:
639656
rv = float(sbare)
640657
return rv

0 commit comments

Comments
 (0)