GFDRR / CCDR-tools

Geoanalytics for climate and disaster risk screening
https://gfdrr.github.io/CCDR-tools/
16 stars 9 forks source link

Fix xlsx export and necessarily refactor functions #42

Closed matamadio closed 6 days ago

matamadio commented 1 month ago

Describe the bug The current xlsx output owerwrites the file at each run. We want to only append (overwrite tab) if the file already exists. This is could be simply achieved by:

    file_prefix = f"{country}_ADM{adm_level}_{haz_cat}_{period}"

    # Create Excel writer object
    excel_file = os.path.join(common.OUTPUT_DIR, f"{file_prefix}_results.xlsx")

    # Check if the file exists, if not, create it
    if not os.path.exists(excel_file):
        wb = Workbook()
        wb.save(excel_file)

    # Determine the appropriate mode and if_sheet_exists parameter
    try:
        with pd.ExcelWriter(excel_file, engine='openpyxl', mode='a') as writer:
            writer.book.active  # This will raise an error if the file is invalid
        mode = 'a'
        if_sheet_exists = 'replace'
    except (InvalidFileException, FileNotFoundError):
        mode = 'w'
        if_sheet_exists = None

    # Use ExcelWriter with 'with' statement to ensure proper closing
    with pd.ExcelWriter(excel_file, engine='openpyxl', mode=mode, if_sheet_exists=if_sheet_exists) as excel_writer:

but this throws error as writing excel is occurring in both the GUI_F3.py and the runAnalysis.py and thus conflicting.

The following changes are needed:

This ensure that results are the same from manual_run (minus the chart and map plots). Note that data summary is used by GUI to plot charts.

I tried, but to no avail - fixing something, breaking something else.

matamadio commented 6 days ago

Fixed by latest pull requests (improve_code and unifying_code).