Skip to content

Commit

Permalink
"Freeman <[email protected]>"
Browse files Browse the repository at this point in the history
Signed-off-by: Freeman <[email protected]>
  • Loading branch information
Freeman-kuch committed Oct 20, 2022
2 parents fad7317 + c6f5ed2 commit c9afe5e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
30 changes: 24 additions & 6 deletions local/rest_api_gcbm/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)


Expand All @@ -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"])
Expand Down
25 changes: 11 additions & 14 deletions local/rest_api_gcbm/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ 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)
Expand Down Expand Up @@ -75,13 +72,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)
Expand Down Expand Up @@ -136,7 +133,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:
Expand All @@ -153,15 +150,15 @@ 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(
f"{input_dir}/mean_annual_temperature_moja.json", "w", encoding="utf8"
) 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(
Expand Down Expand Up @@ -313,31 +310,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)
Expand Down

0 comments on commit c9afe5e

Please sign in to comment.