improve robustness of _read_zone
@@ -173,53 +173,38 @@ def _read_variables(line):
def _read_zone(line, variables):
# Gather zone entries in a dict
- # We can only process the zone record character by character due to
- # value of VARLOCATION containing both comma and equality characters.
line = line[5:]
zone = {}
- i = 0
- key, value, read_key = "", "", True
- is_varlocation, is_end = False, False
- while True:
- char = line[i] if line[i] != " " else ""
- if char == "=":
- read_key = False
- is_varlocation = key == "VARLOCATION"
+ # Look for VARLOCATION (problematic since it contains both ',' and '=')
+ ivar = line.find("VARLOCATION")
- if is_varlocation:
- i += 1
- while True:
- char = line[i] if line[i] != " " else ""
- value += char
- if line[i] == ")":
- break
+ # If zone contains VARLOCATION, process it and remove the key/value pair
+ if ivar >= 0:
+ i1, i2 = line.find("("), line.find(")")
+ zone["VARLOCATION"] = line[i1:i2 + 1].replace(" ", "")
+ line = line[:ivar] + line[i2 + 1:]
+
+ # Split remaining key/value pairs separated by '='
+ line = [l for l in line.replace(",", "").split() if l != "="]
+ i = 0
+ while i < len(line)-1:
+ l = line[i]
+
+ if "=" in l:
+ if not (l.startswith("=") or l.endswith("=")):
+ key, value = l.split("=")
else:
+ key = l.replace("=", "")
+ value = line[i+1]
i += 1
- is_varlocation, is_end = False, True
- i += 1 # Skip comma
- else:
- if char != ",":
- if char != "=":
- if read_key:
- key += char
else:
- value += char
- else:
- is_end = True
-
- if is_end:
- if key in zone_key_to_type.keys():
- zone[key] = zone_key_to_type[key](value)
- key, value, read_key = "", "", True
- is_end = False
+ key = l
+ value = line[i+1].replace("=", "")
+ i += 1
- if i >= len(line) - 1:
if key in zone_key_to_type.keys():
zone[key] = zone_key_to_type[key](value)
- break
- else:
- i += 1
# Check that the grid is unstructured
if "F" in zone.keys():
