|
1 | 1 | import pandas as pd
|
| 2 | +# check yDim, xDim are off by one or not |
2 | 3 |
|
3 |
| -# TODO turn into class with these functions as its methods |
4 |
| -def readEnc(filename=None): |
5 |
| - '''Read a Delft3d enclosure file. Return list of x coordinates and list of y coordinates for easy plotting of enclosure''' |
6 |
| - if not filename: |
7 |
| - print("No file name supplied!") |
8 |
| - return |
9 |
| - enclosure_x = [] |
10 |
| - enclosure_y = [] |
11 |
| - with open(filename,'r') as f: |
12 |
| - for i, line in enumerate(f): |
13 |
| - # boy this is hacky change to list comprehension? |
14 |
| - enclosure_x.append(int(line.split(" ")[1])) |
15 |
| - enclosure_y.append(int(line.split(" ")[2])) |
16 |
| - |
17 |
| - enc_coords = {'x': enclosure_x, 'y': enclosure_y} |
18 |
| - coords_df = pd.DataFrame(data=enc_coords) |
19 |
| - |
20 |
| - display(coords_df) |
21 |
| - |
22 |
| - return enclosure_x, enclosure_y |
| 4 | +# demo enc function |
| 5 | +# writeEncFileWithChannel(xDim, yDim, new_filename=self.filenames['enc'], |
| 6 | +# bank_left=bank_left, bank_right=bank_right+2, |
| 7 | +# channel_length_index=channel_length_index-1) |
23 | 8 |
|
24 |
| -# Remember: Enclosure needs to be larger than boundary locations! |
| 9 | +# demo make new grid |
| 10 | +# newGrid = Grid() |
| 11 | +# newGrid.x = self.grid['x_grid'] # matrix |
| 12 | +# newGrid.y = self.grid['y_grid'] # matrix |
| 13 | +# newGrid.shape = self.grid['shape'] # list |
| 14 | +# newGrid.properties = {'Coordinate System': 'Cartesian', 'xori': 0.0, 'yori': 0.0, 'alfori': 0.0} |
| 15 | +# Grid.write(newGrid, self.filenames['grid']) |
25 | 16 |
|
26 |
| - |
27 |
| -# SIMPLE ENCLOSURE FILE spanning complete domain |
28 |
| -def writeSimpleEncFile(xDim, yDim, new_enc_filename=None): |
29 |
| - ''' Write rectangular enclosure''' |
30 |
| - if not new_enc_filename: |
31 |
| - print("No .enc filename specified aborting!") |
| 17 | +class Enclosure(): |
| 18 | + ''' |
| 19 | + Create a Delft3D enclosure file |
| 20 | + Enclosure excludes gridcells from computation |
| 21 | + |
| 22 | + Create a new enclosure |
| 23 | + enc = Enclosure() |
32 | 24 |
|
33 |
| - enclosureX = [1, xDim + 1, xDim + 1, 1, 1] |
34 |
| - enclosureY = [1, 1, yDim + 1, yDim + 1, 1] |
| 25 | + Load a enclosure from .enc file |
| 26 | + enc = Enclosure.read('channelonly.enc') |
35 | 27 |
|
36 |
| - print(" ----- Writing simple rectangular enclosure -----") |
37 |
| - with open(new_enc_filename, 'w') as enclosureOutfile: |
38 |
| - zipped = list(zip(enclosureX, enclosureY)) # merge x and y lists |
39 |
| - enclosureLines = [('{:>6}{:>6}\n'.format(line[0], line[1])) for line in zipped] # would line for line in work as well? |
40 |
| - enclosureOutfile.writelines(enclosureLines) |
41 |
| - |
42 |
| -def writeEncFileWithChannel(xDim, yDim, new_enc_filename=None, bank_left=None, bank_right=None, channel_length_index=None): # Add channel_width argument |
43 |
| - ''' Write enclosure file, ignoring 'banks' around channel''' |
44 |
| - if not new_enc_filename: |
45 |
| - raise Exception("No enclosure file name supplied!") |
| 28 | + Write enclosure to .enc file |
| 29 | + Enclosure.writeSimpleEnc(enc, 'complete.enc') |
46 | 30 |
|
47 |
| - print(" ----- Writing enclosure file with channel -----") |
48 |
| - enclosureX = [1, bank_left, bank_left, bank_right, bank_right, xDim+1, xDim+1, 1, 1] |
49 |
| - enclosureY = [channel_length_index, channel_length_index, 1, 1, channel_length_index, channel_length_index, yDim+1, yDim+1, channel_length_index] |
| 31 | + Remember: Enclosure needs to be larger than boundary locations! |
| 32 | + ''' |
| 33 | + def __init__(self, *args, **kwargs): |
| 34 | + self.dims = kwargs.get('dims') |
| 35 | + if not self.dims: |
| 36 | + raise Exception("No tuple of dimenstinos provided?") |
| 37 | + self.filename = kwargs.get('filename') |
| 38 | + xDim, yDim = self.dims |
| 39 | + |
| 40 | + if kwargs.get("channel_length_index"): |
| 41 | + self.channel_length_index = kwargs.get("channel_length_index") |
| 42 | + self.bank_left = kwargs.get('bank_left') |
| 43 | + self.bank_right = kwargs.get('bank_right') |
| 44 | + self.x = [1, self.bank_left, self.bank_left, self.bank_right, self.bank_right, xDim+1, xDim+1, 1, 1] |
| 45 | + self.y = [self.channel_length_index, self.channel_length_index, 1, 1, self.channel_length_index, self.channel_length_index, yDim+1, yDim+1, self.channel_length_index] |
| 46 | + elif kwargs.get('read_filename'): |
| 47 | + self.x = kwargs.get('x') # list of x coords |
| 48 | + self.y = kwargs.get('y') # list of y coords |
| 49 | + else: |
| 50 | + self.x = [1, xDim + 1, xDim + 1, 1, 1] |
| 51 | + self.y = [1, 1, yDim + 1, yDim + 1, 1] |
| 52 | + |
| 53 | + |
| 54 | + def __repr__(self): |
| 55 | + repr_string = f"New Enclosure with xDim {self.dims[0]} and yDim {self.dims[1]}" |
| 56 | + |
| 57 | + # if self.channel_length_index and self.bank_left and self.bank_right: # can't acces these properties if they are not arguments to Enclosure |
| 58 | + # repr_string += f" with channel at y = {self.channel_length_index} from {self.bank_left} to {self.bank_right}" |
| 59 | + |
| 60 | + return repr_string |
50 | 61 |
|
51 |
| - with open(new_enc_filename, 'w') as enclosureOutfile: |
52 |
| - zipped = list(zip(enclosureX, enclosureY)) |
53 |
| - enclosureLines = [('{:>6}{:>6}\n'.format(line[0], line[1])) for line in zipped] # print both padded to 6 characters wide |
54 |
| -# [print(enclosureLine) for enclosureLine in enclosureLines] |
55 |
| - enclosureOutfile.writelines(enclosureLines) |
| 62 | + def display(self): |
| 63 | + enc_coords = {'x': self.x, 'y': self.y} |
| 64 | + enc_coords_df = pd.DataFrame(data=enc_coords) |
| 65 | + |
| 66 | + return display(enc_coords_df) |
| 67 | + |
| 68 | + @staticmethod |
| 69 | + def read(filename=None): |
| 70 | + '''Read a Delft3d enclosure file. Return list of x coordinates and list of y coordinates for easy plotting of enclosure''' |
| 71 | + if not filename: |
| 72 | + raise Exception("No file name supplied!") |
| 73 | + |
| 74 | + with open(filename, 'r') as f: |
| 75 | + enc_coords = [(int(line.split(" ")[1]), int(line.split(" ")[2])) for line in f] |
| 76 | + |
| 77 | + enc_x, enc_y = zip(*enc_coords) # unzip |
| 78 | + |
| 79 | + xDim = max(enc_x) |
| 80 | + yDim = max(enc_y) |
| 81 | + dims = (xDim, yDim) |
| 82 | + |
| 83 | + # how do i know from read file wether is has channel |
| 84 | + |
| 85 | + enc = Enclosure(dims=dims, read_filename=True, x=enc_x, y=enc_y) |
| 86 | + |
| 87 | + Enclosure.x = enc_x |
| 88 | + Enclosure.y = enc_y |
| 89 | + |
| 90 | + enc_coords = {'x': enc_x, 'y': enc_y} |
| 91 | + enc_coords_df = pd.DataFrame(data=enc_coords) |
| 92 | + |
| 93 | + display(enc_coords_df) |
| 94 | + |
| 95 | + Enclosure.filename = filename |
| 96 | + |
| 97 | + return enc |
| 98 | + |
| 99 | + def write(self): # Add channel_width argument |
| 100 | + ''' |
| 101 | + Write enclosure file, |
| 102 | + * with_channel: option to ignore 'banks' around channel |
| 103 | + ''' |
| 104 | + if not self.filename: |
| 105 | + raise Exception("No enclosure filename supplied!") |
| 106 | + |
| 107 | + if not self.channel_length_index or not self.bank_left or not self.bank_right: |
| 108 | + raise Exception("bank_left, bank_right, and channel_length_index keyword arguments have to be provided when excluding areas from enclosure!") |
| 109 | + print(" ----- Writing enclosure file excluding channel -----") |
| 110 | + else: |
| 111 | + print(" ----- Writing simple (rectangular) enclosure -----") |
| 112 | + |
| 113 | + with open(self.filename, 'w') as enclosureOutfile: |
| 114 | + zipped = list(zip(self.x, self.y)) |
| 115 | + enclosureLines = [('{:>6}{:>6}\n'.format(line[0], line[1])) for line in zipped] # pad both to 6 characters wide |
| 116 | + enclosureOutfile.writelines(enclosureLines) |
| 117 | + |
| 118 | + print(" ----- Wrote new .enc file to", self.filename) |
56 | 119 |
|
57 |
| - return [enclosureX, enclosureY] |
| 120 | + return [self.x, self.y] |
0 commit comments