|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +HEAD_TEXT = " HEAD" |
| 4 | + |
| 5 | + |
| 6 | +def write_head( |
| 7 | + fbin, |
| 8 | + data, |
| 9 | + kstp=1, |
| 10 | + kper=1, |
| 11 | + pertim=1.0, |
| 12 | + totim=1.0, |
| 13 | + text=HEAD_TEXT, |
| 14 | + ilay=1, |
| 15 | +): |
| 16 | + dt = np.dtype( |
| 17 | + [ |
| 18 | + ("kstp", np.int32), |
| 19 | + ("kper", np.int32), |
| 20 | + ("pertim", np.float64), |
| 21 | + ("totim", np.float64), |
| 22 | + ("text", "S16"), |
| 23 | + ("ncol", np.int32), |
| 24 | + ("nrow", np.int32), |
| 25 | + ("ilay", np.int32), |
| 26 | + ] |
| 27 | + ) |
| 28 | + |
| 29 | + nrow = data.shape[0] |
| 30 | + ncol = data.shape[1] |
| 31 | + h = np.array((kstp, kper, pertim, totim, text, ncol, nrow, ilay), dtype=dt) |
| 32 | + h.tofile(fbin) |
| 33 | + data.tofile(fbin) |
| 34 | + |
| 35 | + |
| 36 | +def write_budget( |
| 37 | + fbin, |
| 38 | + data, |
| 39 | + kstp=1, |
| 40 | + kper=1, |
| 41 | + text=" FLOW-JA-FACE", |
| 42 | + imeth=1, |
| 43 | + delt=1.0, |
| 44 | + pertim=1.0, |
| 45 | + totim=1.0, |
| 46 | + text1id1=" GWF-1", |
| 47 | + text2id1=" GWF-1", |
| 48 | + text1id2=" GWF-1", |
| 49 | + text2id2=" NPF", |
| 50 | +): |
| 51 | + dt = np.dtype( |
| 52 | + [ |
| 53 | + ("kstp", np.int32), |
| 54 | + ("kper", np.int32), |
| 55 | + ("text", "S16"), |
| 56 | + ("ndim1", np.int32), |
| 57 | + ("ndim2", np.int32), |
| 58 | + ("ndim3", np.int32), |
| 59 | + ("imeth", np.int32), |
| 60 | + ("delt", np.float64), |
| 61 | + ("pertim", np.float64), |
| 62 | + ("totim", np.float64), |
| 63 | + ] |
| 64 | + ) |
| 65 | + |
| 66 | + if imeth == 1: |
| 67 | + ndim1 = data.shape[0] |
| 68 | + ndim2 = 1 |
| 69 | + ndim3 = -1 |
| 70 | + h = np.array( |
| 71 | + (kstp, kper, text, ndim1, ndim2, ndim3, imeth, delt, pertim, totim), |
| 72 | + dtype=dt, |
| 73 | + ) |
| 74 | + h.tofile(fbin) |
| 75 | + data.tofile(fbin) |
| 76 | + |
| 77 | + elif imeth == 6: |
| 78 | + ndim1 = 1 |
| 79 | + ndim2 = 1 |
| 80 | + ndim3 = -1 |
| 81 | + h = np.array( |
| 82 | + (kstp, kper, text, ndim1, ndim2, ndim3, imeth, delt, pertim, totim), |
| 83 | + dtype=dt, |
| 84 | + ) |
| 85 | + h.tofile(fbin) |
| 86 | + |
| 87 | + # write text1id1, ... |
| 88 | + dt = np.dtype( |
| 89 | + [ |
| 90 | + ("text1id1", "S16"), |
| 91 | + ("text1id2", "S16"), |
| 92 | + ("text2id1", "S16"), |
| 93 | + ("text2id2", "S16"), |
| 94 | + ] |
| 95 | + ) |
| 96 | + h = np.array((text1id1, text1id2, text2id1, text2id2), dtype=dt) |
| 97 | + h.tofile(fbin) |
| 98 | + |
| 99 | + # write ndat (number of floating point columns) |
| 100 | + colnames = data.dtype.names |
| 101 | + ndat = len(colnames) - 2 |
| 102 | + dt = np.dtype([("ndat", np.int32)]) |
| 103 | + h = np.array([(ndat,)], dtype=dt) |
| 104 | + h.tofile(fbin) |
| 105 | + |
| 106 | + # write auxiliary column names |
| 107 | + naux = ndat - 1 |
| 108 | + if naux > 0: |
| 109 | + auxtxt = [f"{colname:16}" for colname in colnames[3:]] |
| 110 | + |
| 111 | + auxtxt = tuple(auxtxt) |
| 112 | + |
| 113 | + dt = np.dtype([(colname, "S16") for colname in colnames[3:]]) |
| 114 | + |
| 115 | + h = np.array(auxtxt, dtype=dt) |
| 116 | + |
| 117 | + h.tofile(fbin) |
| 118 | + |
| 119 | + # write nlist |
| 120 | + nlist = data.shape[0] |
| 121 | + dt = np.dtype([("nlist", np.int32)]) |
| 122 | + h = np.array([(nlist,)], dtype=dt) |
| 123 | + h.tofile(fbin) |
| 124 | + |
| 125 | + # write the data |
| 126 | + data.tofile(fbin) |
| 127 | + else: |
| 128 | + raise Exception(f"unknown method code {imeth}") |
0 commit comments