From dbb5df99385c438689eb5b67e3f8dafc389c5a2f Mon Sep 17 00:00:00 2001 From: Freeman Date: Sat, 15 Oct 2022 22:22:21 +0100 Subject: [PATCH 1/2] Signed-off-by: freeman_kuch --- local/rest_api_gcbm/preprocess.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/local/rest_api_gcbm/preprocess.py b/local/rest_api_gcbm/preprocess.py index 261d2ed8..880ae326 100644 --- a/local/rest_api_gcbm/preprocess.py +++ b/local/rest_api_gcbm/preprocess.py @@ -16,12 +16,8 @@ def get_config_templates(input_dir): # current hack is to drop the last five characters, but thats very fragile def get_modules_cbm_config(input_dir): with open(f"{input_dir}/templates/modules_cbm.json", "r+") as modules_cbm_config: - disturbances = [] data = json.load(modules_cbm_config) - for file in os.listdir(f"{input_dir}/disturbances/"): - disturbances.append( - file.split(".")[0][:-5] - ) # drop `_moja` to match modules_cbm.json template + disturbances = [file.split(".")[0][:-5] for file in os.listdir(f"{input_dir}/disturbances/")] # drop `_moja` to match modules_cbm.json template modules_cbm_config.seek(0) data["Modules"]["CBMDisturbanceListener"]["settings"]["vars"] = disturbances json.dump(data, modules_cbm_config, indent=4) @@ -75,13 +71,13 @@ def get_provider_config(input_dir): cellLonSize = [] paths = [] - for root, dirs, files in os.walk(os.path.abspath(f"{input_dir}/disturbances/")): + for root, _, files in os.walk(os.path.abspath(f"{input_dir}/disturbances/")): for file in files: fp = os.path.join(root, file) Rasters.append(fp) paths.append(fp) - for root, dirs, files in os.walk(os.path.abspath(f"{input_dir}/classifiers/")): + for root, _, files in os.walk(os.path.abspath(f"{input_dir}/classifiers/")): for file in files: fp1 = os.path.join(root, file) Rasters.append(fp1) @@ -136,7 +132,7 @@ def get_provider_config(input_dir): # should be able to accept variable number of inputs, but requires # means for user to specify/verify correct ["attributes"] def get_input_layers(): - for root, dirs, files in os.walk( + for root, _, files in os.walk( os.path.abspath(f"{input_dir}/miscellaneous/") ): for file in files: @@ -153,7 +149,7 @@ def get_input_layers(): ) as json_file: dictionary["layer_type"] = "GridLayer" dictionary["layer_data"] = "Int16" - dictionary["nodata"] = nodatam[1] + dictionary["nodata"] = 32767 json.dump(dictionary, json_file, indent=4) with open( @@ -161,7 +157,7 @@ def get_input_layers(): ) as json_file: dictionary["layer_type"] = "GridLayer" dictionary["layer_data"] = "Float32" - dictionary["nodata"] = nodatam[0] + dictionary["nodata"] = 32767 json.dump(dictionary, json_file, indent=4) with open( @@ -313,31 +309,31 @@ def get_study_area(): get_study_area() - for root, dirs, files in os.walk(os.path.abspath(f"{input_dir}/disturbances/")): + for root, _, files in os.walk(os.path.abspath(f"{input_dir}/disturbances/")): for file in files: fp = os.path.join(root, file) Rasters.append(fp) paths.append(fp) - for root, dirs, files in os.walk(os.path.abspath(f"{input_dir}/classifiers/")): + for root, _, files in os.walk(os.path.abspath(f"{input_dir}/classifiers/")): for file in files: fp1 = os.path.join(root, file) Rasters.append(fp1) paths.append(fp1) - for root, dirs, files in os.walk( + for root, _, files in os.walk( os.path.abspath(f"{input_dir}/miscellaneous/") ): for file in files: fp2 = os.path.join(root, file) paths.append(fp2) - for root, dirs, files in os.walk(os.path.abspath(f"{input_dir}/templates/")): + for root, _, files in os.walk(os.path.abspath(f"{input_dir}/templates/")): for file in files: fp3 = os.path.join(root, file) paths.append(fp3) - for root, dirs, files in os.walk(os.path.abspath(f"{input_dir}/db/")): + for root, _, files in os.walk(os.path.abspath(f"{input_dir}/db/")): for file in files: fp4 = os.path.join(root, file) paths.append(fp4) From b711bc677d28a06646ec02c7770e831a06ae01f1 Mon Sep 17 00:00:00 2001 From: temitayo Date: Tue, 18 Oct 2022 14:11:20 +0100 Subject: [PATCH 2/2] fix: add a check to see if simulation output exists (#199) Signed-off-by: olalekan temitayo Signed-off-by: olalekan temitayo --- local/rest_api_gcbm/app.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/local/rest_api_gcbm/app.py b/local/rest_api_gcbm/app.py index f19a03ad..1feed739 100644 --- a/local/rest_api_gcbm/app.py +++ b/local/rest_api_gcbm/app.py @@ -462,9 +462,24 @@ def gcbm_download(): title = request.form.get("title") or "simulation" # Sanitize title title = "".join(c for c in title if c.isalnum()) + + output_dir = f"{os.getcwd()}/output/{title}.zip" + input_dir = f"{os.getcwd()}/input/{title}" + + # if the title has an input simulation and there is no output simulation then they should check the status. + if not os.path.exists(f"{output_dir}") and os.path.exists(f"{input_dir}"): + return { + "message": "You simulation is currently running, check the status via /gcbm/status" + } + + # if there is no input simulation and no output simulation then the simulation does not exist. + elif not os.path.exists(f"{output_dir}") and not os.path.exists(f"{input_dir}"): + return { + "message": "You don't have a simulation with this title kindly check the title and try again" + } + return send_file( - f"{os.getcwd()}/output/{title}.zip", - attachment_filename="{title}.zip", + f"{os.getcwd()}/output/{title}.zip", attachment_filename="{title}.zip", ) @@ -484,10 +499,13 @@ def gcbm_list_simulations(): for file in os.listdir(f"{os.getcwd()}/input"): list.append(file) - return { - "data": list, - "message": "To create a new simulation, create a request at gcbm/new. To access the results of the existing simulations, create a request at gcbm/download.", - }, 200 + return ( + { + "data": list, + "message": "To create a new simulation, create a request at gcbm/new. To access the results of the existing simulations, create a request at gcbm/download.", + }, + 200, + ) @app.route("/gcbm/status", methods=["POST"])