1- import types
2- from typing import Union , get_args , get_origin
31
42import numpy as np
53from numpy .typing import NDArray
86from flopy4 .mf6 .constants import FILL_DNODATA
97
108
11- def convert_array (value , self_ , field ):
9+ def convert_array (value , self_ , field ) -> NDArray :
1210 if not isinstance (value , dict ):
1311 # if not a dict, assume it's a numpy array
1412 # and let xarray deal with it if it isn't
@@ -29,20 +27,8 @@ def convert_array(value, self_, field):
2927 if any (unresolved ):
3028 raise ValueError (f"Couldn't resolve dims: { unresolved } " )
3129
32- # extract dtype
33- origin = get_origin (field .type )
34- args = get_args (field .type )
35- if origin in (Union , types .UnionType ) and args [- 1 ] is types .NoneType :
36- origin = args [0 ] # Optional
37- if origin is NDArray :
38- dtype = args [1 ]
39- elif origin is np .ndarray :
40- dtype = args [0 ]
41- else :
42- raise ValueError (f"Expected NDArray, got { origin } " )
43-
4430 # create array
45- a = np .full (shape , fill_value = FILL_DNODATA , dtype = dtype )
31+ a = np .full (shape , fill_value = FILL_DNODATA , dtype = field . dtype )
4632
4733 def _get_nn (cellid ):
4834 match len (cellid ):
@@ -59,13 +45,13 @@ def _get_nn(cellid):
5945
6046 # populate array. TODO: is there a way to do this
6147 # without hardcoding awareness of kper and cellid?
62- if "kper " in dims :
48+ if "per " in dims :
6349 for kper , period in value .items ():
6450 if kper == "*" :
6551 kper = 0
6652 match len (shape ):
6753 case 1 :
68- a [kper ] = v
54+ a [kper ] = value
6955 case _:
7056 for cellid , v in period .items ():
7157 nn = _get_nn (cellid )
@@ -75,6 +61,6 @@ def _get_nn(cellid):
7561 else :
7662 for cellid , v in value .items ():
7763 nn = _get_nn (cellid )
78- a [kper , nn ] = v
64+ a [nn ] = v
7965
8066 return a
0 commit comments