@@ -146,52 +146,33 @@ def unstructure_component(value: Component) -> dict[str, Any]:
146146def unstructure_oc (value : Any ) -> dict [str , Any ]:
147147 data = xattree .asdict (value )
148148 for block_name , block in get_blocks (value .dfn ).items ():
149- if block_name == "perioddata" :
150- # Unstructure all four arrays
151- save_head = unstructure_array (data .get ("save_head" , {}))
152- save_budget = unstructure_array (data .get ("save_budget" , {}))
153- print_head = unstructure_array (data .get ("print_head" , {}))
154- print_budget = unstructure_array (data .get ("print_budget" , {}))
155-
156- # Collect all unique periods
149+ if block_name == "period" :
150+ # Dynamically collect all recarray fields in perioddata block
151+ array_fields = []
152+ for field_name , field in block .items ():
153+ # Try to split field_name into action and kind, e.g. save_head -> ("save", "head")
154+ action , rtype = field_name .split ("_" )
155+ array_fields .append ((action , rtype , field_name ))
156+
157+ # Unstructure all arrays and collect all unique periods
158+ arrays = {}
157159 all_periods = set () # type: ignore
158- for d in (save_head , save_budget , print_head , print_budget ):
159- if isinstance (d , dict ):
160- all_periods .update (d .keys ())
160+ for action , rtype , field_name in array_fields :
161+ arr = unstructure_array (data .get (field_name , {}))
162+ arrays [(action , rtype )] = arr
163+ if isinstance (arr , dict ):
164+ all_periods .update (arr .keys ())
161165 all_periods = sorted (all_periods ) # type: ignore
162166
163- saverecord = {} # type: ignore
164- printrecord = {} # type: ignore
167+ perioddata = {} # type: ignore
165168 for kper in all_periods :
166- # Save head
167- if kper in save_head :
168- v = save_head [kper ]
169- if kper not in saverecord :
170- saverecord [kper ] = []
171- saverecord [kper ].append ({"action" : "save" , "type" : "head" , "ocsetting" : v })
172- # Save budget
173- if kper in save_budget :
174- v = save_budget [kper ]
175- if kper not in saverecord :
176- saverecord [kper ] = []
177- saverecord [kper ].append ({"action" : "save" , "type" : "budget" , "ocsetting" : v })
178- # Print head
179- if kper in print_head :
180- v = print_head [kper ]
181- if kper not in printrecord :
182- printrecord [kper ] = []
183- printrecord [kper ].append ({"action" : "print" , "type" : "head" , "ocsetting" : v })
184- # Print budget
185- if kper in print_budget :
186- v = print_budget [kper ]
187- if kper not in printrecord :
188- printrecord [kper ] = []
189- printrecord [kper ].append ({"action" : "print" , "type" : "budget" , "ocsetting" : v })
190-
191- data ["saverecord" ] = saverecord
192- data ["printrecord" ] = printrecord
193- data ["save" ] = "save"
194- data ["print" ] = "print"
169+ for (action , rtype ), arr in arrays .items ():
170+ if kper in arr :
171+ if kper not in perioddata :
172+ perioddata [kper ] = []
173+ perioddata [kper ].append ((action , rtype , arr [kper ]))
174+
175+ data ["period" ] = perioddata
195176 else :
196177 for field_name , field in block .items ():
197178 # unstructure arrays destined for list-based input
0 commit comments