|
22 | 22 |
|
23 | 23 | from .error import ParsingError |
24 | 24 | from .phyvars import FIELD_FILES_H5, SFIELD_FILES_H5 |
| 25 | +from .xdmf import XmlStream |
25 | 26 |
|
26 | 27 | if typing.TYPE_CHECKING: |
27 | 28 | from pathlib import Path |
@@ -853,66 +854,69 @@ def _maybe_get( |
853 | 854 |
|
854 | 855 | @cached_property |
855 | 856 | def _data(self) -> Mapping[int, XmfEntry]: |
856 | | - # Geometry stuff from surface field is not useful |
| 857 | + xs = XmlStream(filepath=self.path) |
857 | 858 | data = {} |
858 | | - root = xmlET.parse(str(self.path)).getroot() |
859 | | - for snap in root[0][0]: |
860 | | - time = self._maybe_get(snap, "Time", "Value", float) |
861 | | - mo_lambda = self._maybe_get(snap, "mo_lambda", "Value", float) |
862 | | - mo_thick_sol = self._maybe_get(snap, "mo_thick_sol", "Value", float) |
| 859 | + for _ in xs.iter_tag("Time"): |
| 860 | + time = float(xs.current.attrib["Value"]) |
| 861 | + xs.advance() |
| 862 | + extra: dict[str, float] = {} |
| 863 | + while xs.current.tag != "Grid": |
| 864 | + # mo_lambda, mo_thick_sol |
| 865 | + extra[xs.current.tag] = float(xs.current.attrib["Value"]) |
| 866 | + xs.advance() |
863 | 867 |
|
864 | 868 | yin_yang = False |
865 | 869 | twod = None |
866 | 870 |
|
867 | | - elt_subdomain = _try_find(self.path, snap, "Grid") |
868 | | - elt_geom = _try_find(self.path, elt_subdomain, "Geometry") |
869 | | - if elt_geom.get("Type") == "X_Y": |
870 | | - twod = "" |
871 | | - for data_item in elt_geom.findall("DataItem"): |
872 | | - coord = _try_text(self.path, data_item).strip()[-1] |
873 | | - if coord in "XYZ": |
874 | | - twod += coord |
875 | | - data_item = _try_find(self.path, elt_geom, "DataItem") |
876 | | - data_text = _try_text(self.path, data_item) |
877 | | - coord_shape = _get_dim(self.path, data_item) |
878 | | - coord_filepattern = data_text.strip().split(":/", 1)[0] |
879 | | - coord_file_chunks = coord_filepattern.split("_") |
880 | | - coord_file_chunks[-2] = "{icore:05d}" |
881 | | - coord_filepattern = "_".join(coord_file_chunks) |
| 871 | + xs.skip_to_tag("Geometry") |
| 872 | + with xs.load() as elt_geom: |
| 873 | + if elt_geom.get("Type") == "X_Y": |
| 874 | + twod = "" |
| 875 | + for data_item in elt_geom: |
| 876 | + coord = _try_text(xs.filepath, data_item).strip()[-1] |
| 877 | + if coord in "XYZ": |
| 878 | + twod += coord |
| 879 | + data_item = elt_geom[0] |
| 880 | + data_text = _try_text(xs.filepath, data_item) |
| 881 | + coord_shape = _get_dim(xs.filepath, data_item) |
| 882 | + coord_filepattern = data_text.strip().split(":/", 1)[0] |
| 883 | + coord_file_chunks = coord_filepattern.split("_") |
| 884 | + coord_file_chunks[-2] = "{icore:05d}" |
| 885 | + coord_filepattern = "_".join(coord_file_chunks) |
882 | 886 |
|
883 | 887 | fields_info = {} |
884 | | - for elt_fvar in elt_subdomain.findall("Attribute"): |
885 | | - name = _try_get(self.path, elt_fvar, "Name") |
886 | | - elt_data = _try_find(self.path, elt_fvar, "DataItem") |
887 | | - shape = _get_dim(self.path, elt_data) |
888 | | - data_text = _try_text(self.path, elt_data) |
889 | | - h5file, group = data_text.strip().split(":/", 1) |
890 | | - isnap = int(group[-5:]) |
891 | | - i0_yin = int(group[-11:-6]) - 1 |
892 | | - ifile = int(h5file[-14:-9]) |
893 | | - fields_info[name] = (ifile, shape) |
894 | | - |
895 | | - i1_yin = i0_yin |
| 888 | + while xs.current.tag == "Attribute": |
| 889 | + with xs.load() as elt_fvar: |
| 890 | + name = elt_fvar.attrib["Name"] |
| 891 | + elt_data = elt_fvar[0] |
| 892 | + shape = _get_dim(xs.filepath, elt_data) |
| 893 | + data_text = _try_text(xs.filepath, elt_data) |
| 894 | + h5file, group = data_text.strip().split(":/", 1) |
| 895 | + isnap = int(group[-5:]) |
| 896 | + i0_yin = int(group[-11:-6]) - 1 |
| 897 | + ifile = int(h5file[-14:-9]) |
| 898 | + fields_info[name] = (ifile, shape) |
| 899 | + |
| 900 | + i1_yin = i0_yin + 1 |
896 | 901 | i0_yang = 0 |
897 | 902 | i1_yang = 0 |
898 | | - for elt_subdomain in snap.findall("Grid"): |
899 | | - elt_name = _try_get(self.path, elt_subdomain, "Name") |
900 | | - if elt_name.startswith("meshYang"): |
901 | | - yin_yang = True |
902 | | - elt_fvar = _try_find(self.path, elt_subdomain, "Attribute") |
903 | | - elt_data = _try_find(self.path, elt_fvar, "DataItem") |
904 | | - data_text = _try_text(self.path, elt_data) |
905 | | - _, group = data_text.strip().split(":/", 1) |
906 | | - i0_yang = int(group[-11:-6]) - 1 |
907 | | - i1_yang = i0_yang + (i1_yin - i0_yin) |
| 903 | + for _ in xs.iter_tag("Grid"): |
| 904 | + if xs.current.attrib["GridType"] == "Collection": |
908 | 905 | break |
909 | | - i1_yin += 1 |
| 906 | + if (name := xs.current.attrib["Name"]).startswith("meshYang"): |
| 907 | + if i1_yang == 0: |
| 908 | + yin_yang = True |
| 909 | + i0_yang = int(name[-5:]) - 1 |
| 910 | + i1_yang = i0_yang + (i1_yin - i0_yin) |
| 911 | + else: |
| 912 | + i1_yin += 1 |
| 913 | + xs.drop() |
910 | 914 |
|
911 | 915 | data[isnap] = XmfEntry( |
912 | 916 | isnap=isnap, |
913 | 917 | time=time, |
914 | | - mo_lambda=mo_lambda, |
915 | | - mo_thick_sol=mo_thick_sol, |
| 918 | + mo_lambda=extra.get("mo_lambda"), |
| 919 | + mo_thick_sol=extra.get("mo_thick_sol"), |
916 | 920 | yin_yang=yin_yang, |
917 | 921 | twod=twod, |
918 | 922 | coord_filepattern=coord_filepattern, |
|
0 commit comments