From 327a750693a8b731d20d78ca8fa455991f5d0487 Mon Sep 17 00:00:00 2001 From: rosskush Date: Fri, 8 Oct 2021 12:25:52 -0500 Subject: [PATCH] line-length-79 --- flopy/utils/binaryfile.py | 198 ++++++++++++++++++++++++++++++-------- 1 file changed, 158 insertions(+), 40 deletions(-) diff --git a/flopy/utils/binaryfile.py b/flopy/utils/binaryfile.py index fa94de9564..471dd8b297 100755 --- a/flopy/utils/binaryfile.py +++ b/flopy/utils/binaryfile.py @@ -54,13 +54,19 @@ def set_values(self, **kwargs): try: self.header[0][k] = int(kwargs[k]) except: - print(f"{k} key not available in {self.header_type} " "header dtype") + print( + f"{k} key not available in {self.header_type} " + "header dtype" + ) for k in fkey: if k in kwargs.keys(): try: self.header[0][k] = float(kwargs[k]) except: - print(f"{k} key not available " f"in {self.header_type} header dtype") + print( + f"{k} key not available " + f"in {self.header_type} header dtype" + ) for k in ckey: if k in kwargs.keys(): # Convert to upper case to be consistent case used by MODFLOW @@ -254,7 +260,9 @@ def get_headfile_precision(filename): result = "double" except: f.close() - raise ValueError(f"Could not determine the precision of the headfile {filename}") + raise ValueError( + f"Could not determine the precision of the headfile {filename}" + ) # close and return result f.close() @@ -341,7 +349,11 @@ def get_databytes(self, header): size of the data array, in bytes, following the header """ - return np.int64(header["ncol"]) * np.int64(header["nrow"]) * np.int64(self.realtype(1).nbytes) + return ( + np.int64(header["ncol"]) + * np.int64(header["nrow"]) + * np.int64(self.realtype(1).nbytes) + ) def _read_data(self, shp): return binaryread(self.file, self.realtype, shape=shp) @@ -394,7 +406,9 @@ def get_ts(self, idx): for k, i, j in kijlist: ioffset = (i * self.ncol + j) * self.realtype(1).nbytes for irec, header in enumerate(self.recordarray): - ilay = header["ilay"] - 1 # change ilay from header to zero-based + ilay = ( + header["ilay"] - 1 + ) # change ilay from header to zero-based if ilay != k: continue ipos = int(self.iposarray[irec]) @@ -461,7 +475,9 @@ class HeadFile(BinaryLayerFile): """ - def __init__(self, filename, text="head", precision="auto", verbose=False, **kwargs): + def __init__( + self, filename, text="head", precision="auto", verbose=False, **kwargs + ): self.text = text.encode() if precision == "auto": precision = get_headfile_precision(filename) @@ -469,7 +485,9 @@ def __init__(self, filename, text="head", precision="auto", verbose=False, **kwa s = f"Error. Precision could not be determined for {filename}" print(s) raise Exception() - self.header_dtype = BinaryHeader.set_dtype(bintype="Head", precision=precision) + self.header_dtype = BinaryHeader.set_dtype( + bintype="Head", precision=precision + ) super().__init__(filename, precision, verbose, kwargs) return @@ -535,7 +553,9 @@ def __init__( s = f"Error. Precision could not be determined for {filename}" print(s) raise Exception() - self.header_dtype = BinaryHeader.set_dtype(bintype="Ucn", precision=precision) + self.header_dtype = BinaryHeader.set_dtype( + bintype="Ucn", precision=precision + ) super().__init__(filename, precision, verbose, kwargs) return @@ -663,7 +683,9 @@ def __init__(self, filename, precision="auto", verbose=False, **kwargs): self.nnodes = self.modelgrid.nnodes if not success: - raise Exception(f"Budget file could not be read using {precision} precision") + raise Exception( + f"Budget file could not be read using {precision} precision" + ) return @@ -799,7 +821,9 @@ def _build_index(self): self.nrecords += 1 totim = header["totim"] if totim == 0: - totim = self._totim_from_kstpkper((header["kstp"] - 1, header["kper"] - 1)) + totim = self._totim_from_kstpkper( + (header["kstp"] - 1, header["kper"] - 1) + ) header["totim"] = totim if totim >= 0 and totim not in self.times: self.times.append(totim) @@ -847,7 +871,11 @@ def _build_index(self): s = s.decode() print(f"{itxt}: {s}") print("file position: ", ipos) - if int(header["imeth"]) != 5 and int(header["imeth"]) != 6 and int(header["imeth"]) != 7: + if ( + int(header["imeth"]) != 5 + and int(header["imeth"]) != 6 + and int(header["imeth"]) != 7 + ): print("") # set the nrow, ncol, and nlay if they have not been set @@ -861,9 +889,13 @@ def _build_index(self): self.nlay = np.abs(header["nlay"]) # store record and byte position mapping - self.recorddict[tuple(header)] = ipos # store the position right after header2 + self.recorddict[ + tuple(header) + ] = ipos # store the position right after header2 self.recordarray.append(header) - self.iposarray.append(ipos) # store the position right after header2 + self.iposarray.append( + ipos + ) # store the position right after header2 # skip over the data to the next record and set ipos self._skip_record(header) @@ -908,7 +940,11 @@ def _skip_record(self, header): print("naux: ", naux) print("nlist: ", nlist) print("") - nbytes = nlist * (np.int32(1).nbytes + self.realtype(1).nbytes + naux * self.realtype(1).nbytes) + nbytes = nlist * ( + np.int32(1).nbytes + + self.realtype(1).nbytes + + naux * self.realtype(1).nbytes + ) elif imeth == 6: # read rest of list data nauxp1 = binaryread(self.file, np.int32)[0] @@ -921,7 +957,11 @@ def _skip_record(self, header): print("naux: ", naux) print("nlist: ", nlist) print("") - nbytes = nlist * (np.int32(1).nbytes * 2 + self.realtype(1).nbytes + naux * self.realtype(1).nbytes) + nbytes = nlist * ( + np.int32(1).nbytes * 2 + + self.realtype(1).nbytes + + naux * self.realtype(1).nbytes + ) else: raise Exception(f"invalid method code {imeth}") if nbytes != 0: @@ -939,7 +979,9 @@ def _get_header(self): # fill header2 by first reading imeth, delt, pertim and totim # and then adding modelnames and paknames if imeth = 6 temp = binaryread(self.file, self.header2_dtype0, (1,)) - header2 = np.array([(0, 0.0, 0.0, 0.0, "", "", "", "")], dtype=self.header2_dtype) + header2 = np.array( + [(0, 0.0, 0.0, 0.0, "", "", "", "")], dtype=self.header2_dtype + ) for name in temp.dtype.names: header2[name] = temp[name] if int(header2["imeth"]) == 6: @@ -948,7 +990,9 @@ def _get_header(self): header2["modelnam2"] = binaryread(self.file, str, charlen=16) header2["paknam2"] = binaryread(self.file, str, charlen=16) else: - header2 = np.array([(0, 0.0, 0.0, 0.0, "", "", "", "")], dtype=self.header2_dtype) + header2 = np.array( + [(0, 0.0, 0.0, 0.0, "", "", "", "")], dtype=self.header2_dtype + ) fullheader = join_struct_arrays([header1, header2]) return fullheader[0] @@ -990,7 +1034,10 @@ def _find_paknam(self, paknam): paknam16 = t break if paknam16 is None: - raise Exception("The specified package name string is not " "in the budget file.") + raise Exception( + "The specified package name string is not " + "in the budget file." + ) return paknam16 def list_records(self): @@ -1227,25 +1274,51 @@ def get_data( kstp1 = kstpkper[0] + 1 kper1 = kstpkper[1] + 1 if text is None and paknam is None: - select_indices = np.where((self.recordarray["kstp"] == kstp1) & (self.recordarray["kper"] == kper1)) + select_indices = np.where( + (self.recordarray["kstp"] == kstp1) + & (self.recordarray["kper"] == kper1) + ) else: if paknam is None and text is not None: - select_indices = np.where((self.recordarray["kstp"] == kstp1) & (self.recordarray["kper"] == kper1) & (self.recordarray["text"] == text16)) + select_indices = np.where( + (self.recordarray["kstp"] == kstp1) + & (self.recordarray["kper"] == kper1) + & (self.recordarray["text"] == text16) + ) elif text is None and paknam is not None: - select_indices = np.where((self.recordarray["kstp"] == kstp1) & (self.recordarray["kper"] == kper1) & (self.recordarray["paknam"] == paknam16)) + select_indices = np.where( + (self.recordarray["kstp"] == kstp1) + & (self.recordarray["kper"] == kper1) + & (self.recordarray["paknam"] == paknam16) + ) else: - select_indices = np.where((self.recordarray["kstp"] == kstp1) & (self.recordarray["kper"] == kper1) & (self.recordarray["text"] == text16) & (self.recordarray["paknam"] == paknam16)) + select_indices = np.where( + (self.recordarray["kstp"] == kstp1) + & (self.recordarray["kper"] == kper1) + & (self.recordarray["text"] == text16) + & (self.recordarray["paknam"] == paknam16) + ) elif totim is not None: if text is None and paknam is None: select_indices = np.where((self.recordarray["totim"] == totim)) else: if paknam is None and text is not None: - select_indices = np.where((self.recordarray["totim"] == totim) & (self.recordarray["text"] == text16)) + select_indices = np.where( + (self.recordarray["totim"] == totim) + & (self.recordarray["text"] == text16) + ) elif text is None and paknam is not None: - select_indices = np.where((self.recordarray["totim"] == totim) & (self.recordarray["paknam"] == paknam16)) + select_indices = np.where( + (self.recordarray["totim"] == totim) + & (self.recordarray["paknam"] == paknam16) + ) else: - select_indices = np.where((self.recordarray["totim"] == totim) & (self.recordarray["text"] == text16) & (self.recordarray["paknam"] == paknam16)) + select_indices = np.where( + (self.recordarray["totim"] == totim) + & (self.recordarray["text"] == text16) + & (self.recordarray["paknam"] == paknam16) + ) # allow for idx to be a list or a scalar elif idx is not None: @@ -1259,7 +1332,10 @@ def get_data( select_indices = np.where((self.recordarray["text"] == text16)) else: - raise TypeError("get_data() missing 1 required argument: 'kstpkper', 'totim', " "'idx', or 'text'") + raise TypeError( + "get_data() missing 1 required argument: 'kstpkper', 'totim', " + "'idx', or 'text'" + ) # build and return the record list if isinstance(select_indices, tuple): @@ -1308,7 +1384,10 @@ def get_ts(self, idx, text=None, times=None): """ # issue exception if text not provided if text is None: - raise Exception("text keyword must be provided to CellBudgetFile " "get_ts() method.") + raise Exception( + "text keyword must be provided to CellBudgetFile " + "get_ts() method." + ) kijlist = self._build_kijlist(idx) nstation = self._get_nstation(idx, kijlist) @@ -1325,7 +1404,11 @@ def get_ts(self, idx, text=None, times=None): if isinstance(times, np.ndarray): times = times.tolist() if len(times) != len(kk): - raise Exception("times passed to CellBudgetFile get_ts() " "method must be equal to {} " "not {}".format(len(kk), len(times))) + raise Exception( + "times passed to CellBudgetFile get_ts() " + "method must be equal to {} " + "not {}".format(len(kk), len(times)) + ) timesint = times for idx, t in enumerate(timesint): result[idx, 0] = t @@ -1345,13 +1428,25 @@ def get_ts(self, idx, text=None, times=None): # skip missing data - required for storage if len(v) > 0: if self.modelgrid is None: - s = "A modelgrid instance must be provided during " "instantiation to get IMETH=6 timeseries data" + s = ( + "A modelgrid instance must be provided during " + "instantiation to get IMETH=6 timeseries data" + ) raise AssertionError(s) if self.modelgrid.grid_type == "structured": - ndx = [lrc[0] * (self.modelgrid.nrow * self.modelgrid.ncol) + lrc[1] * self.modelgrid.ncol + (lrc[2] + 1) for lrc in kijlist] + ndx = [ + lrc[0] + * (self.modelgrid.nrow * self.modelgrid.ncol) + + lrc[1] * self.modelgrid.ncol + + (lrc[2] + 1) + for lrc in kijlist + ] else: - ndx = [lrc[0] * self.modelgrid.ncpl + (lrc[-1] + 1) for lrc in kijlist] + ndx = [ + lrc[0] * self.modelgrid.ncpl + (lrc[-1] + 1) + for lrc in kijlist + ] for vv in v: field = vv.dtype.names[2] @@ -1380,7 +1475,10 @@ def _build_kijlist(self, idx): if j < 0 or j > self.ncol - 1: fail = True if fail: - raise Exception("Invalid cell index. Cell {} not within model grid: " "{}".format((k, i, j), (self.nlay, self.nrow, self.ncol))) + raise Exception( + "Invalid cell index. Cell {} not within model grid: " + "{}".format((k, i, j), (self.nlay, self.nrow, self.ncol)) + ) return kijlist def _get_nstation(self, idx, kijlist): @@ -1391,7 +1489,9 @@ def _get_nstation(self, idx, kijlist): def _init_result(self, nstation): # Initialize result array and put times in first column - result = np.empty((len(self.kstpkper), nstation + 1), dtype=self.realtype) + result = np.empty( + (len(self.kstpkper), nstation + 1), dtype=self.realtype + ) result[:, :] = np.nan if len(self.times) == result.shape[0]: result[:, 0] = np.array(self.times) @@ -1453,13 +1553,17 @@ def get_record(self, idx, full3D=False): if self.verbose: s += f"an array of shape {(nlay, nrow, ncol)}" print(s) - return binaryread(self.file, self.realtype(1), shape=(nlay, nrow, ncol)) + return binaryread( + self.file, self.realtype(1), shape=(nlay, nrow, ncol) + ) # imeth 1 elif imeth == 1: if self.verbose: s += f"an array of shape {(nlay, nrow, ncol)}" print(s) - return binaryread(self.file, self.realtype(1), shape=(nlay, nrow, ncol)) + return binaryread( + self.file, self.realtype(1), shape=(nlay, nrow, ncol) + ) # imeth 2 elif imeth == 2: @@ -1467,7 +1571,10 @@ def get_record(self, idx, full3D=False): dtype = np.dtype([("node", np.int32), ("q", self.realtype)]) if self.verbose: if full3D: - s += f"a numpy masked array of " f"size ({nlay}, {nrow}, {ncol})" + s += ( + f"a numpy masked array of " + f"size ({nlay}, {nrow}, {ncol})" + ) else: s += f"a numpy recarray of size ({nlist}, 2)" print(s) @@ -1483,9 +1590,16 @@ def get_record(self, idx, full3D=False): data = binaryread(self.file, self.realtype(1), shape=(nrow, ncol)) if self.verbose: if full3D: - s += "a numpy masked array of size " f"({nlay}, {nrow}, {ncol})" + s += ( + "a numpy masked array of size " + f"({nlay}, {nrow}, {ncol})" + ) else: - s += "a list of two 2D numpy arrays. The first is an " f"integer layer array of shape ({nrow}, {ncol}). The " f"second is real data array of shape ({nrow}, {ncol})" + s += ( + "a list of two 2D numpy arrays. The first is an " + f"integer layer array of shape ({nrow}, {ncol}). The " + f"second is real data array of shape ({nrow}, {ncol})" + ) print(s) if full3D: out = np.ma.zeros(self.nnodes, dtype=np.float32) @@ -1754,7 +1868,9 @@ class HeadUFile(BinaryLayerFile): """ - def __init__(self, filename, text="headu", precision="auto", verbose=False, **kwargs): + def __init__( + self, filename, text="headu", precision="auto", verbose=False, **kwargs + ): """ Class constructor """ @@ -1765,7 +1881,9 @@ def __init__(self, filename, text="headu", precision="auto", verbose=False, **kw s = f"Error. Precision could not be determined for {filename}" print(s) raise Exception() - self.header_dtype = BinaryHeader.set_dtype(bintype="Head", precision=precision) + self.header_dtype = BinaryHeader.set_dtype( + bintype="Head", precision=precision + ) super().__init__(filename, precision, verbose, kwargs) return