diff --git a/flopy/mfusg/mfusg.py b/flopy/mfusg/mfusg.py index b7817240e..db79228e0 100644 --- a/flopy/mfusg/mfusg.py +++ b/flopy/mfusg/mfusg.py @@ -535,7 +535,7 @@ def _send_load_messages(model, files_successfully_loaded, files_not_loaded): print(f" {os.path.basename(fname)}") -def fmt_string(array): +def fmt_string(array, free=False): """ Returns a C-style fmt string for numpy savetxt that corresponds to the dtype. @@ -543,14 +543,21 @@ def fmt_string(array): Parameters ---------- array : numpy array + free : bool, optional + If True, use high-precision format (%16.9G) for floats which requires + FREE format in the BAS file. If False (default), use fixed 10-character + format (%10.2e) compatible with MODFLOW-USG's fixed-width input format. """ fmts = [] + # When FREE format is enabled in BAS, we can use high-precision output. + # Without FREE, MODFLOW-USG expects 10-character fixed-width fields. + float_fmt = "%16.9G" if free else "%10.2e" for field in array.dtype.descr: vtype = field[1][1].lower() if vtype in {"i", "b"}: fmts.append("%10d") elif vtype == "f": - fmts.append("%10.2e") + fmts.append(float_fmt) elif vtype == "o": fmts.append("%10s") elif vtype == "s": diff --git a/flopy/mfusg/mfusgcln.py b/flopy/mfusg/mfusgcln.py index 15741bb6f..d2e87798c 100644 --- a/flopy/mfusg/mfusgcln.py +++ b/flopy/mfusg/mfusgcln.py @@ -543,18 +543,23 @@ def write_file(self, f=None, check=False): f_cln.write(self.iac_cln.get_file_entry()) f_cln.write(self.ja_cln.get_file_entry()) - np.savetxt(f_cln, self.node_prop, fmt=fmt_string(self.node_prop), delimiter="") + free = self.parent.free_format_input + np.savetxt( + f_cln, self.node_prop, fmt=fmt_string(self.node_prop, free), delimiter="" + ) - np.savetxt(f_cln, self.cln_gwc, fmt=fmt_string(self.cln_gwc), delimiter="") + np.savetxt( + f_cln, self.cln_gwc, fmt=fmt_string(self.cln_gwc, free), delimiter="" + ) if self.nconduityp > 0: np.savetxt( - f_cln, self.cln_circ, fmt=fmt_string(self.cln_circ), delimiter="" + f_cln, self.cln_circ, fmt=fmt_string(self.cln_circ, free), delimiter="" ) if self.nrectyp > 0: np.savetxt( - f_cln, self.cln_rect, fmt=fmt_string(self.cln_rect), delimiter="" + f_cln, self.cln_rect, fmt=fmt_string(self.cln_rect, free), delimiter="" ) f_cln.write(self.ibound.get_file_entry())