QUIC-Fire-TT / ttrs_quicfire

ttrs_quicfire is a Python library to easily configure burn models for plots of land defined using shape files for the quicfire model.
MIT License
0 stars 1 forks source link

Error Handling: As described in email #1

Closed zacharycope0 closed 2 years ago

zacharycope0 commented 2 years ago

Feature Description The functions that modify the QF arrays typically reference a shape file that it assumes to exist. Instead of having, for example, qf_arrs.mod_wetlands() crash the script if the wetlands shapefile isn't read in; I would rather there be a try statement with the except warning the user that the function didn't run because the script didn't read in the correct shapefile.

Getting Started Look at implementation of qf_arrs.build_fuelbreak() and qf.arrs.mod_wetlands(). Warn user if there script is not tracking the appropriated shapefiles to implement the function. Feel free to implement additional error handling where you see fit.

Screenshots image (4)

cbonesteel commented 2 years ago

https://github.com/QUIC-Fire-TT/ttrs_quicfire/blob/1e8fc9edd59130511f8551c7fc0f25f1314d09a1/ttrs_quicfire/build_shapefiles.py#L51-L53

I added this raise FileNotFound if the file_path is None in load_shapefile(). This allows us to do a try except anytime we call this function and if the filepath is saved as None it will allow us to handle it in any way we deem fit. I was initially running into an issue trying to do it in clip_to_bbox since mod_wetlands() called this function and then the file was opened in there.

https://github.com/QUIC-Fire-TT/ttrs_quicfire/blob/699a1ceac9f141cbe9bcd0f4821b1478d66a239c/ttrs_quicfire/quic_fire.py#L132-L134

This is because I assumed that everytime we used clip_to_bbox this is the way it was handled. I finally realized that as in the code below, sometimes the file was opened beforehand.

https://github.com/QUIC-Fire-TT/ttrs_quicfire/blob/699a1ceac9f141cbe9bcd0f4821b1478d66a239c/ttrs_quicfire/quic_fire.py#L172-L180

In the end, adding this raise to load_shapefile and changing the behavior of mod_wetlands was easier and may allow for the removal of the loads within clip_to_bbox. The only issue may be with the bbox filepaths in that function but if so a try catch should be able to handle that there for all instances of the bbox.

I believe for now this issue can be resolved but some fine tuning or cleanup may be needed in the future.

zacharycope0 commented 2 years ago

I think that should be good. I'll run a few test and merge it to main.

zacharycope0 commented 2 years ago

Cameron. This is working great! Before you merge I'd like to have functionality to warn the user that they are not building the simulation as expected:

Something like this

def load_shapefile(file_path):
    if file_path == None:
        #Idk the best way to let the user know what function isn't working properly
        print('statement to warning the user of the file they are missing/function that is messing up?')
        answer = input("Continue (y/n)")
        if answer[0].lower = 'y':
            raise FileNotFoundError
        else: sys.exit()

Otherwise the code keeps running and you don't see that the functions didn't run. image

cbonesteel commented 2 years ago

Since when burn_plot.shp doesn't exist nothing can be built, it is better to handle the continuation on a case by case basis. This means that I added a condition to the try except in mod_wetlands like so:

https://github.com/QUIC-Fire-TT/ttrs_quicfire/blob/1916494b8c1d6fdfba903b7d489629bbbc3e9784/ttrs_quicfire/quic_fire.py#L137-L145

This is how any additional try except for shapefile loads should be handled unless it is required, in which case only sys.exit() should be called.