Skip to content

Commit ef2f4e2

Browse files
Merge pull request #6 from seequent/rectgeo-hardening
Added some more error handling within the rectgeo method. Unvalidated…
2 parents fa388f7 + 788ec2d commit ef2f4e2

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

t2grids.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -797,12 +797,11 @@ def find_origin_block(grid):
797797
def find_all_blocks_in_layer(grid, elev):
798798
return [blk for blk in grid.blocklist if blk.centre is not None and np.isclose(blk.centre[2], elev)]
799799

800-
def is_structured_grid(grid, ob):
801-
ob_layer_blocks = find_all_blocks_in_layer(grid, ob.centre[2])
800+
def is_structured_grid(layer_blocks):
802801
layer_block_dict = dict()
803-
for block in ob_layer_blocks:
802+
for block in layer_blocks:
804803
layer_block_dict[block.name] = block
805-
for block in ob_layer_blocks:
804+
for block in layer_blocks:
806805
neighbours = [layer_block_dict[neighbour_name] for neighbour_name in block.get_neighbour_names()
807806
if layer_block_dict.get(neighbour_name, None) is not None]
808807
if len(neighbours) > 4:
@@ -817,6 +816,8 @@ def con_name_index(con, blkname):
817816

818817
def next_block_in_direction(blk, last, direction, grid, max_volume = None):
819818
"""Finds next block in specified direction, and connection connecting them."""
819+
if not blk:
820+
return None, None
820821
cons = [con for con in blk.connection_name if
821822
grid.connection[con].direction == direction]
822823
if last: cons = [con for con in cons if last.name not in con]
@@ -899,7 +900,9 @@ def find_surface(geo, grid, blockmap, remove_inactive, max_volume):
899900
bottom_layer = geo.layerlist[-1]
900901
for col in geo.columnlist:
901902
geoblkname = geo.block_name(bottom_layer.name, col.name)
902-
blkname = blockmap[geoblkname]
903+
blkname = blockmap.get(geoblkname, None)
904+
if not blkname:
905+
raise Exception("Block not mapped")
903906
bottom_block = grid.block[blkname]
904907
colblocks, layerthicks = block_direction_track(grid, bottom_block, 3, max_volume)
905908
if colblocks:
@@ -926,7 +929,11 @@ def find_surface(geo, grid, blockmap, remove_inactive, max_volume):
926929
def match_position(geo, grid, ob):
927930
"""Rotate and translate geometry as needed."""
928931
blks, sp = block_direction_track(grid, ob, 1)
929-
angle = 0.5 * np.pi - vector_heading(blks[-1].centre[0:2] - ob.centre[:2])
932+
v = blks[-1].centre[0:2] - ob.centre[:2]
933+
if any(v):
934+
angle = 0.5 * np.pi - vector_heading(v)
935+
else:
936+
angle = 0
930937
from math import degrees
931938
angle = degrees(angle)
932939
geo.rotate(-angle, np.zeros(2))
@@ -950,6 +957,8 @@ def block_mapping(geo, grid, ob, nblks, max_volume):
950957
blk, last3 = start1, None
951958
for lay in geo.layerlist[::-1]:
952959
geoblkname = geo.block_name(lay.name, col.name)
960+
if not blk:
961+
raise Exception("Block not mapped")
953962
mapping[geoblkname] = blk.name
954963
next_blk,con = next_block_in_direction(blk, last3, 3, grid, max_volume)
955964
if next_blk is None: # incomplete column
@@ -984,15 +993,21 @@ def required_centres_present(grid, max_volume):
984993
else: ob = find_origin_block(self)
985994
if ob is None: raise Exception("Can't find origin block for grid.")
986995
else:
987-
if not is_structured_grid(self, ob):
996+
base_layer_blocks = find_all_blocks_in_layer(self, ob.centre[2])
997+
if not is_structured_grid(base_layer_blocks):
988998
raise Exception("Invalid number of neighbouring blocks.")
989999
spacings = block_spacings(self, ob, atmos_volume)
9901000
nblks = dict([(i, len(spacings[i])) for i in range(1,4)])
9911001
geo = mulgrid().rectangular(spacings[1], spacings[2], spacings[3],
9921002
convention = convention, atmos_type = atmos_type,
9931003
justify = justify, chars = chars, spaces = spaces,
9941004
block_order = block_order)
995-
blockmap = block_mapping(geo, self, ob, nblks, atmos_volume)
1005+
try:
1006+
blockmap = block_mapping(geo, self, ob, nblks, atmos_volume)
1007+
except Exception:
1008+
raise Exception("Unable to create geometry.")
1009+
if not all([getattr(b, 'name', None) in blockmap.values() for b in base_layer_blocks]):
1010+
raise Exception("Unable to create geometry.")
9961011
geo = match_position(geo, self, ob)
9971012
geo = find_surface(geo, self, blockmap, remove_inactive, atmos_volume)
9981013
geo.snap_columns_to_layers(layer_snap)

0 commit comments

Comments
 (0)