@@ -93,7 +93,9 @@ def fill_with_random_data(structure, max_children=3):
93
93
child .value = random_data (child .metadata .data_type , child .metadata .ndim )
94
94
95
95
96
- def maybe_set_random_value (primitive : IDSPrimitive , leave_empty : float ) -> None :
96
+ def maybe_set_random_value (
97
+ primitive : IDSPrimitive , leave_empty : float , skip_complex : bool
98
+ ) -> None :
97
99
"""Set the value of an IDS primitive with a certain chance.
98
100
99
101
If the IDSPrimitive has coordinates, then the size of the coordinates is taken into
@@ -153,7 +155,7 @@ def maybe_set_random_value(primitive: IDSPrimitive, leave_empty: float) -> None:
153
155
# Scale chance of not setting a coordinate by our number of dimensions,
154
156
# such that overall there is roughly a 50% chance that any coordinate
155
157
# remains empty
156
- maybe_set_random_value (coordinate_element , 0.5 ** ndim )
158
+ maybe_set_random_value (coordinate_element , 0.5 ** ndim , skip_complex )
157
159
size = coordinate_element .shape [0 if coordinate .references else dim ]
158
160
159
161
if coordinate .size : # coordinateX = <path> OR 1...1
@@ -176,13 +178,18 @@ def maybe_set_random_value(primitive: IDSPrimitive, leave_empty: float) -> None:
176
178
elif primitive .metadata .data_type is IDSDataType .FLT :
177
179
primitive .value = np .random .random_sample (size = shape )
178
180
elif primitive .metadata .data_type is IDSDataType .CPX :
181
+ if skip_complex :
182
+ # If we are skipping complex numbers then leave the value empty.
183
+ return
179
184
val = np .random .random_sample (shape ) + 1j * np .random .random_sample (shape )
180
185
primitive .value = val
181
186
else :
182
187
raise ValueError (f"Invalid IDS data type: { primitive .metadata .data_type } " )
183
188
184
189
185
- def fill_consistent (structure : IDSStructure , leave_empty : float = 0.2 ):
190
+ def fill_consistent (
191
+ structure : IDSStructure , leave_empty : float = 0.2 , skip_complex : bool = False
192
+ ):
186
193
"""Fill a structure with random data, such that coordinate sizes are consistent.
187
194
188
195
Sets homogeneous_time to heterogeneous (always).
@@ -196,6 +203,9 @@ def fill_consistent(structure: IDSStructure, leave_empty: float = 0.2):
196
203
exclusive_coordinates: list of IDSPrimitives that have exclusive alternative
197
204
coordinates. These are initially not filled, and only at the very end of
198
205
filling an IDSToplevel, a choice is made between the exclusive coordinates.
206
+ skip_complex: Whether to skip over populating complex numbers. This is
207
+ useful for maintaining compatibility with older versions of netCDF4
208
+ (<1.7.0) where complex numbers are not supported.
199
209
"""
200
210
if isinstance (structure , IDSToplevel ):
201
211
unsupported_ids_name = (
@@ -218,7 +228,9 @@ def fill_consistent(structure: IDSStructure, leave_empty: float = 0.2):
218
228
219
229
for child in structure :
220
230
if isinstance (child , IDSStructure ):
221
- exclusive_coordinates .extend (fill_consistent (child , leave_empty ))
231
+ exclusive_coordinates .extend (
232
+ fill_consistent (child , leave_empty , skip_complex )
233
+ )
222
234
223
235
elif isinstance (child , IDSStructArray ):
224
236
if child .metadata .coordinates [0 ].references :
@@ -230,7 +242,7 @@ def fill_consistent(structure: IDSStructure, leave_empty: float = 0.2):
230
242
if isinstance (coor , IDSPrimitive ):
231
243
# maybe fill with random data:
232
244
try :
233
- maybe_set_random_value (coor , leave_empty )
245
+ maybe_set_random_value (coor , leave_empty , skip_complex )
234
246
except (RuntimeError , ValueError ):
235
247
pass
236
248
child .resize (len (coor ))
@@ -244,7 +256,9 @@ def fill_consistent(structure: IDSStructure, leave_empty: float = 0.2):
244
256
else :
245
257
child .resize (child .metadata .coordinates [0 ].size or 1 )
246
258
for ele in child :
247
- exclusive_coordinates .extend (fill_consistent (ele , leave_empty ))
259
+ exclusive_coordinates .extend (
260
+ fill_consistent (ele , leave_empty , skip_complex )
261
+ )
248
262
249
263
else : # IDSPrimitive
250
264
coordinates = child .metadata .coordinates
@@ -256,7 +270,7 @@ def fill_consistent(structure: IDSStructure, leave_empty: float = 0.2):
256
270
exclusive_coordinates .append (child )
257
271
else :
258
272
try :
259
- maybe_set_random_value (child , leave_empty )
273
+ maybe_set_random_value (child , leave_empty , skip_complex )
260
274
except (RuntimeError , ValueError ):
261
275
pass
262
276
@@ -278,7 +292,7 @@ def fill_consistent(structure: IDSStructure, leave_empty: float = 0.2):
278
292
coor = filled_refs .pop ()
279
293
unset_coordinate (coor )
280
294
281
- maybe_set_random_value (element , leave_empty )
295
+ maybe_set_random_value (element , leave_empty , skip_complex )
282
296
else :
283
297
return exclusive_coordinates
284
298
0 commit comments