pyswmm / swmmio

Python tools for interacting with, editing, and visualizing EPA SWMM5 models
https://swmmio.readthedocs.io
MIT License
126 stars 41 forks source link

provide read/write interface to all INP sections #57

Open aerispaha opened 5 years ago

aerispaha commented 5 years ago

Summary

All INP sections that follow the common tabular structure with unique indices as the first column can be read via the swmmio.utils.create_dataframeINP function. However, only a limited number of these sections have been added as properties in the higher-level swmmio.core.inp object.

Add coverage for read/writing all INP sections from the swmmio.core.inp object. INP sections are defined currently as properties with setter/getters in the swmmio.core module here. List of INP sections described here in the OWA SWMM repo.

INP Sections to Cover

BuczynskiRafal commented 1 year ago

Hi, I think I am using swmmio incorrectly or there was some issue in the code at the "Polygons" section.
The event occurs when using the "replace_inp_section" function. The code I am executing:

def _add_coords(self, subcatchment_id):
    exist = [
        (self.model.inp.polygons["X"][-1], self.model.inp.polygons["Y"][-1]),
        (self.model.inp.polygons["X"][-2], self.model.inp.polygons["Y"][-2]),
        (self.model.inp.polygons["X"][-3], self.model.inp.polygons["Y"][-3]),
        (self.model.inp.polygons["X"][-4], self.model.inp.polygons["Y"][-4]),
    ]
    coords = pd.DataFrame(
        data={
            "X": [exist[0][0], exist[1][0], exist[2][0], exist[3][0]],
            "Y": [exist[0][1] - 5, exist[1][1] - 5, exist[2][1] - 5, exist[3][1] - 5],
        },
        index=[subcatchment_id for _ in range(4)]
    )
    coords.index.names = ['Name']
    self.model.inp.polygons = pd.concat([self.model.inp.polygons, coords])
    replace_inp_section(self.model.inp.path, "[Polygons]", self.model.inp.polygons)

I see that SWMM writes literally "Polygons" in the *inp file, swmmio modifies this name by writing "POLYGONS".

Here is the section I have in the file before executing the code

[Polygons]
;;Subcatchment   X-Coord            Y-Coord           
;;-------------- ------------------ ------------------
S1               777180.000         592590.000        
S1               777180.000         592585.000        
S1               777175.000         592585.000        
S1               777175.000         592590.000        

This is what the section looks like after the first execution of "replace_inp_section"

[POLYGONS]
;;   X          Y         
S1   777180.0   592590.0
S1   777180.0   592585.0
S1   777175.0   592585.0
S1   777175.0   592590.0
S2   777175.0   592585.0
S2   777175.0   592580.0
S2   777180.0   592580.0
S2   777180.0   592585.0

This is what the sections looks like after the second execution of "replace_inp_section"

[POLYGONS]
;;   X          Y         
S1   777180.0   592590.0
S1   777180.0   592585.0
S1   777175.0   592585.0
S1   777175.0   592590.0
S2   777175.0   592585.0
S2   777175.0   592580.0
S2   777180.0   592580.0
S2   777180.0   592585.0

[POLYGONS]
;;   X          Y         
S1   777180.0   592590.0
S1   777180.0   592585.0
S1   777175.0   592585.0
S1   777175.0   592590.0
S2   777175.0   592585.0
S2   777175.0   592580.0
S2   777180.0   592580.0
S2   777180.0   592585.0
S3   777180.0   592580.0
S3   777180.0   592575.0
S3   777175.0   592575.0
S3   777175.0   592580.0

In the code I execute I always use "[Polygons]", after the second execution of "replace_inp_section" the sections are duplicated. The problem doesn't occur when I change the header to "[POLYGONS]" before executing the code, and in my function I also use "[POLYGONS]"

If I should post this thread in discussions I apologize and please delete.

aerispaha commented 1 year ago

@BuczynskiRafal this is very interesting - I suspect that you've uncovered a bug. That said, I think we should continue this conversation in a new issue. Do you mind creating an issue and restating what you've described above?

If you've found a solution already (e.g. by changing the header to [POLYGONS]), please feel free to submit a pull request with your proposed changes.

Thanks for finding and documenting this issue so clearly!

BuczynskiRafal commented 1 year ago

I have created a separate issue, at my spare time I will prepare a PR in which I will propose a solution.