|
| 1 | +from pathlib import Path |
1 | 2 | from typing import Any |
2 | 3 |
|
| 4 | +import numpy as np |
| 5 | +import xarray as xr |
3 | 6 | from lark import Token, Transformer |
4 | 7 |
|
5 | 8 |
|
6 | | -class GenericTransformer(Transformer): |
| 9 | +class BasicTransformer(Transformer): |
7 | 10 | """ |
8 | | - Generic transformer for MF6 input files. Works only with the generic |
9 | | - grammar. Returns structures of blocks consisting of lines of tokens. |
| 11 | + Basic transformer for MF6 input files. Works only with the basic |
| 12 | + grammar. Yields blocks simply as collections of lines of tokens. |
10 | 13 | """ |
11 | 14 |
|
12 | 15 | def start(self, items: list[Any]) -> dict[str, Any]: |
@@ -48,3 +51,123 @@ def CNAME(self, token: Token) -> str: |
48 | 51 |
|
49 | 52 | def INT(self, token: Token) -> int: |
50 | 53 | return int(token) |
| 54 | + |
| 55 | + |
| 56 | +class ArrayTransformer(Transformer): |
| 57 | + """ |
| 58 | + Transformer for MF6 array input format. Returns xarray DataArrays |
| 59 | + for internal/constant arrays and Path objects for external arrays, |
| 60 | + inside a dictionary which also contains control information. This |
| 61 | + is a first step towards a smarter parser/transformer for the full |
| 62 | + MF6 input format specification. |
| 63 | + """ |
| 64 | + |
| 65 | + def start(self, items: list[Any]) -> dict: |
| 66 | + return items[0] |
| 67 | + |
| 68 | + def readarray(self, items: list[Any]) -> dict: |
| 69 | + infos = items[0] |
| 70 | + if isinstance(infos, list): |
| 71 | + data = xr.concat([info["data"] for info in infos if "data" in info], dim="layer") |
| 72 | + return { |
| 73 | + "control": [info["control"] for info in infos if "control" in info], |
| 74 | + "data": data, |
| 75 | + "attrs": {k: v for k, v in infos[0].items() if k not in ["data"]}, |
| 76 | + "dims": {"layer": len(infos)}, |
| 77 | + } |
| 78 | + return infos |
| 79 | + |
| 80 | + def single_array(self, items: list[Any]) -> dict: |
| 81 | + netcdf = items[0] |
| 82 | + info = items[-1] |
| 83 | + if netcdf: |
| 84 | + info["netcdf"] = netcdf |
| 85 | + return ArrayTransformer.try_create_dataarray(info) |
| 86 | + |
| 87 | + def layered_array(self, items: list[Any]) -> list[dict]: |
| 88 | + netcdf = items[0] |
| 89 | + infos = [] |
| 90 | + for info in items[2:]: |
| 91 | + if info is None: |
| 92 | + continue |
| 93 | + if netcdf: |
| 94 | + info["netcdf"] = netcdf |
| 95 | + infos.append(ArrayTransformer.try_create_dataarray(info)) |
| 96 | + return infos |
| 97 | + |
| 98 | + def array(self, items: list[Any]) -> dict[str, Any]: |
| 99 | + control = items[0] |
| 100 | + data = items[1] if len(items) > 1 else None |
| 101 | + if (value := control.get("value", None)) is not None: |
| 102 | + data = value |
| 103 | + return {"control": control, "data": data} |
| 104 | + |
| 105 | + def control(self, items: list[Any]) -> dict[str, Any]: |
| 106 | + return items[0] |
| 107 | + |
| 108 | + def constant(self, items: list[Any]) -> dict[str, Any]: |
| 109 | + return {"type": "constant", "value": items[0]} |
| 110 | + |
| 111 | + def internal(self, items: list[Any]) -> dict[str, Any]: |
| 112 | + result = {"type": "internal"} |
| 113 | + for item in items: |
| 114 | + if item is not None: |
| 115 | + result.update(item) |
| 116 | + return result |
| 117 | + |
| 118 | + def external(self, items: list[Any]) -> dict[str, Any]: |
| 119 | + return {"type": "external", "value": items[0]} |
| 120 | + |
| 121 | + def factor(self, items: list[Any]) -> dict[str, float]: |
| 122 | + return {"factor": items[0]} |
| 123 | + |
| 124 | + def iprn(self, items: list[Any]) -> dict[str, int]: |
| 125 | + return {"iprn": items[0]} |
| 126 | + |
| 127 | + def binary(self, items: list[Any]) -> dict[str, bool]: |
| 128 | + return {"binary": True} |
| 129 | + |
| 130 | + def filename(self, items: list[Any]) -> Path: |
| 131 | + return Path(items[0]) |
| 132 | + |
| 133 | + def data(self, items: list[Any]) -> np.ndarray: |
| 134 | + return np.array(items) |
| 135 | + |
| 136 | + def netcdf(self, items: list[Any]) -> dict[str, bool]: |
| 137 | + return {"netcdf": True} |
| 138 | + |
| 139 | + def NUMBER(self, token: Token) -> int | float: |
| 140 | + return float(token) |
| 141 | + |
| 142 | + def SIGNED_NUMBER(self, token: Token) -> int | float: |
| 143 | + return self.NUMBER(token) |
| 144 | + |
| 145 | + def INT(self, token: Token) -> int: |
| 146 | + return int(token) |
| 147 | + |
| 148 | + def SIGNED_INT(self, token: Token) -> int: |
| 149 | + return int(token) |
| 150 | + |
| 151 | + def ESCAPED_STRING(self, token: Token) -> str: |
| 152 | + # Remove quotes from escaped string |
| 153 | + value = str(token) |
| 154 | + if value.startswith('"') and value.endswith('"'): |
| 155 | + return value[1:-1] |
| 156 | + return value |
| 157 | + |
| 158 | + @staticmethod |
| 159 | + def try_create_dataarray(array_info: dict) -> dict: |
| 160 | + """Create an xarray DataArray from MF6 array information.""" |
| 161 | + control = array_info["control"] |
| 162 | + match control["type"]: |
| 163 | + case "constant": |
| 164 | + data = control["value"] |
| 165 | + array_info["data"] = xr.DataArray(data=data) |
| 166 | + case "internal": |
| 167 | + data = array_info["data"] |
| 168 | + factor = control.get("factor", 1.0) |
| 169 | + data = data * factor |
| 170 | + array_info["data"] = xr.DataArray(data=data) |
| 171 | + case "external": |
| 172 | + pass |
| 173 | + return array_info |
0 commit comments