Skip to content

Commit d66245a

Browse files
black format
1 parent a86de75 commit d66245a

2 files changed

Lines changed: 324 additions & 236 deletions

File tree

schimpy/gr3.py

Lines changed: 84 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@
99
import numpy as np
1010
import os
1111

12+
1213
class Gr3IO(schimpy.base_io.BaseIO):
13-
""" A class that manages I/O of GR3 files
14-
"""
14+
"""A class that manages I/O of GR3 files"""
1515

1616
def __init__(self, logger=None):
17-
""" Constructor
18-
"""
17+
"""Constructor"""
1918
super(Gr3IO, self).__init__(logger=logger)
2019
self._mesh = None
2120

2221
def read(self, gr3_fname="hgrid.gr3", mode=0):
23-
""" Read in a hgrid.gr3 file.
24-
If mode is 1, it does not read in boundary information.
22+
"""Read in a hgrid.gr3 file.
23+
If mode is 1, it does not read in boundary information.
2524
"""
2625
if self._logger is not None:
2726
self._logger.info("Reading in a gr3 file: %s ..." % gr3_fname)
@@ -72,7 +71,7 @@ def _read_header(self, f):
7271
(n_elems, n_nodes) = list(map(int, tokens[:2]))
7372
if n_nodes <= 0 or n_elems <= 0:
7473
raise ValueError("The Header information of GR3 file is corrupted.")
75-
self._mesh.allocate(n_elems, n_nodes) # Allocate memory
74+
self._mesh.allocate(n_elems, n_nodes) # Allocate memory
7675
self._n_elems = n_elems
7776
self._n_nodes = n_nodes
7877

@@ -96,8 +95,9 @@ def _read_elems(self, f):
9695
raise ValueError("Element block is corrupt.")
9796
type_elem = int(tkn[1])
9897
if type_elem < 3 or type_elem > 4:
99-
raise ValueError("Currently only triangular and " \
100-
"quadrilateral are supported.")
98+
raise ValueError(
99+
"Currently only triangular and " "quadrilateral are supported."
100+
)
101101
# Zero-based connectivities
102102
if type_elem == 3:
103103
connectivities = np.subtract(np.array(list(map(int, tkn[2:5]))), 1)
@@ -108,8 +108,7 @@ def _read_elems(self, f):
108108
self._mesh.set_elem(elem_i, connectivities)
109109

110110
def _read_boundaries(self, f):
111-
""" Read boundary information
112-
"""
111+
"""Read boundary information"""
113112
## Assuming the order of the different boundary types are consitent
114113
## Open boundaries
115114
# # of open boundaries
@@ -123,8 +122,9 @@ def _read_boundaries(self, f):
123122
tokens, ok = self._read_and_parse_line(f, 1)
124123
if not ok:
125124
print("Line: {}".format(self.linecounter))
126-
raise Exception("The number of total open boundary nodes is not"\
127-
" correctly provided.")
125+
raise Exception(
126+
"The number of total open boundary nodes is not" " correctly provided."
127+
)
128128
n_open_boundary_nodes = int(tokens[0])
129129

130130
# open boundaries
@@ -133,8 +133,9 @@ def _read_boundaries(self, f):
133133
tokens, ok = self._read_and_parse_line(f, 1)
134134
if not ok:
135135
print("Line: {}".format(self.linecounter))
136-
raise Exception("The number of nodes for a boundary is not"\
137-
" correctly provided.")
136+
raise Exception(
137+
"The number of nodes for a boundary is not" " correctly provided."
138+
)
138139
# Comment
139140
comment = None
140141
if len(tokens) > 1:
@@ -146,14 +147,11 @@ def _read_boundaries(self, f):
146147
tokens, ok = self._read_and_parse_line(f, 1)
147148
if not ok:
148149
print("Line: {}".format(self.linecounter))
149-
raise ValueError("Node for boundary not"\
150-
" correctly provided.")
150+
raise ValueError("Node for boundary not" " correctly provided.")
151151

152-
node = int(tokens[0]) - 1 # Zero based
152+
node = int(tokens[0]) - 1 # Zero based
153153
nodes.append(node)
154-
self._mesh.add_boundary(nodes,
155-
BoundaryType.OPEN,
156-
comment)
154+
self._mesh.add_boundary(nodes, BoundaryType.OPEN, comment)
157155

158156
# land boundaries
159157
# I found out that there is no distinction between
@@ -167,16 +165,18 @@ def _read_boundaries(self, f):
167165
tokens, ok = self._read_and_parse_line(f, 1)
168166
if not ok:
169167
print("Line: {}".format(self.linecounter))
170-
raise Exception("The number of total land boundary nodes is " \
171-
"not provided properly.")
168+
raise Exception(
169+
"The number of total land boundary nodes is " "not provided properly."
170+
)
172171
# n_land_boundary_nodes = int(tokens[0])
173172
for _ in range(n_land_boundaries):
174173
# # of nodes of this open boundary
175-
(tokens, ok) = self._read_and_parse_line(f,1)
174+
(tokens, ok) = self._read_and_parse_line(f, 1)
176175
if not ok:
177176
print("Line: {}".format(self.linecounter))
178-
raise Exception("The number of nodes for a boundary is "\
179-
"not provided properly.")
177+
raise Exception(
178+
"The number of nodes for a boundary is " "not provided properly."
179+
)
180180
# Comment
181181
comment = None
182182
if len(tokens) > 1:
@@ -189,27 +189,27 @@ def _read_boundaries(self, f):
189189
if not ok:
190190
print("Line: {}".format(self.linecounter))
191191
raise Exception("Node for a boundary not correctly provided.")
192-
node = int(tokens[0]) - 1 # Zero based
192+
node = int(tokens[0]) - 1 # Zero based
193193
nodes.append(node)
194194
self._mesh.add_boundary(nodes, BoundaryType.LAND, comment)
195195

196196
def write(self, mesh, fname, node_attr=None, boundary=False):
197-
""" Write a GR3 format grid.
198-
mesh = SCHISM mesh (schism_mesh) instance
199-
fname = output file name
200-
node_attr = a list of node attribute
201-
boundary = If true, boundary information will be added. Otherwise,
202-
it will not be appended.
197+
"""Write a GR3 format grid.
198+
mesh = SCHISM mesh (schism_mesh) instance
199+
fname = output file name
200+
node_attr = a list of node attribute
201+
boundary = If true, boundary information will be added. Otherwise,
202+
it will not be appended.
203203
"""
204204
if self._logger is not None:
205205
self._logger.info("Writing an gr3 file: %s" % fname)
206-
f = open(fname, 'w')
206+
f = open(fname, "w")
207207
# Header
208-
# if mesh.comment is None:
208+
# if mesh.comment is None:
209209
buf = "%s\n" % os.path.basename(fname)
210-
# else:
211-
# buf = "%s !modified by the preprocessing tool\n" \
212-
# % mesh.comment
210+
# else:
211+
# buf = "%s !modified by the preprocessing tool\n" \
212+
# % mesh.comment
213213

214214
f.write(buf)
215215
n_elems = mesh.n_elems()
@@ -219,72 +219,91 @@ def write(self, mesh, fname, node_attr=None, boundary=False):
219219
# Nodes
220220
for i in range(n_nodes):
221221
if not node_attr is None:
222-
buf = "%d %18.8f %18.8f %18.8f\n" % (i + 1, \
223-
mesh.nodes[i, 0], \
224-
mesh.nodes[i, 1], \
225-
node_attr[i])
222+
buf = "%d %18.8f %18.8f %18.8f\n" % (
223+
i + 1,
224+
mesh.nodes[i, 0],
225+
mesh.nodes[i, 1],
226+
node_attr[i],
227+
)
226228

227229
else:
228-
buf = "%d %18.8f %18.8f %18.8f\n" % (i + 1, \
229-
mesh.nodes[i, 0], \
230-
mesh.nodes[i, 1], \
231-
mesh.nodes[i, 2])
230+
buf = "%d %18.8f %18.8f %18.8f\n" % (
231+
i + 1,
232+
mesh.nodes[i, 0],
233+
mesh.nodes[i, 1],
234+
mesh.nodes[i, 2],
235+
)
232236
f.write(buf)
233237

234238
# Elements
235239
for elem_i in range(n_elems):
236240
elem = mesh.elem(elem_i) + 1
237241
n_nodes = len(elem)
238-
buf = '%d %d' % (elem_i + 1, n_nodes)
239-
fmt = ' %d' * n_nodes + '\n'
242+
buf = "%d %d" % (elem_i + 1, n_nodes)
243+
fmt = " %d" * n_nodes + "\n"
240244
buf += fmt % tuple(elem)
241245
f.write(buf)
242246

243247
# Boundaries
244248
if boundary:
245249
# Open
246-
buf = "%d = Number of open boundaries\n" \
247-
% mesh.n_boundaries(BoundaryType.OPEN)
250+
buf = "%d = Number of open boundaries\n" % mesh.n_boundaries(
251+
BoundaryType.OPEN
252+
)
248253
f.write(buf)
249-
buf = "%d = Total number of open boundary nodes\n" \
250-
% mesh.n_boundary_nodes(BoundaryType.OPEN)
254+
buf = "%d = Total number of open boundary nodes\n" % mesh.n_boundary_nodes(
255+
BoundaryType.OPEN
256+
)
251257
f.write(buf)
252258
openbound_count = 0
253259
for boundary in mesh.boundaries:
254260
if boundary.btype == BoundaryType.OPEN:
255261
openbound_count += 1
256262
if boundary.comment is None:
257-
buf = "%d = Number of nodes for open boundary %d\n" % \
258-
(boundary.n_nodes(), openbound_count)
263+
buf = "%d = Number of nodes for open boundary %d\n" % (
264+
boundary.n_nodes(),
265+
openbound_count,
266+
)
259267
else:
260268
buf = "%d %s\n" % (boundary.n_nodes(), boundary.comment)
261269
f.write(buf)
262270
buf = ""
263271
for node_i in boundary.nodes:
264272
buf += "%d\n" % (node_i + 1)
265273
f.write(buf)
266-
# else:
267-
# raise Exception("Unsupported boundary type.")
274+
# else:
275+
# raise Exception("Unsupported boundary type.")
268276

269277
# Land
270-
buf = "%d = Number of land boundaries\n" \
271-
% (mesh.n_boundaries(BoundaryType.LAND))
278+
buf = "%d = Number of land boundaries\n" % (
279+
mesh.n_boundaries(BoundaryType.LAND)
280+
)
272281
f.write(buf)
273-
buf = "%d = Total number of land boundary nodes\n" \
274-
% (mesh.n_boundary_nodes(BoundaryType.LAND))
282+
buf = "%d = Total number of land boundary nodes\n" % (
283+
mesh.n_boundary_nodes(BoundaryType.LAND)
284+
)
275285
f.write(buf)
276286
landbound_count = 0
277287
islandbound_count = 0
278288
for boundary in mesh.boundaries:
279-
if (boundary.btype == BoundaryType.LAND or
280-
boundary.btype== BoundaryType.ISLAND):
289+
if (
290+
boundary.btype == BoundaryType.LAND
291+
or boundary.btype == BoundaryType.ISLAND
292+
):
281293
landbound_count += 1
282294
island_flag = 1 if BoundaryType.ISLAND else 0
283295
if boundary.comment is None:
284-
buf = "%d %d = Number of nodes for land boundary %d\n" % \
285-
(boundary.n_nodes(), island_flag, landbound_count)
296+
buf = "%d %d = Number of nodes for land boundary %d\n" % (
297+
boundary.n_nodes(),
298+
island_flag,
299+
landbound_count,
300+
)
286301
else:
287-
buf = "%d %d %s\n" % (boundary.n_nodes(), island_flag, boundary.comment)
302+
buf = "%d %d %s\n" % (
303+
boundary.n_nodes(),
304+
island_flag,
305+
boundary.comment,
306+
)
288307
f.write(buf)
289308
buf = ""
290309
for node_i in boundary.nodes:

0 commit comments

Comments
 (0)