Skip to content

Commit

Permalink
Removing subfolders in data
Browse files Browse the repository at this point in the history
  • Loading branch information
ghislainv committed Jun 11, 2024
1 parent a729b30 commit 2f2157c
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 deletions forestatrisk/data/country_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def country_compute(
"""

# Create output directory
make_dir(output_dir)

# Reproject GADM
ofile = os.path.join(temp_dir, "ctry_PROJ.gpkg")
ifile = os.path.join(temp_dir, "gadm41_" + iso3 + "_0.gpkg")
Expand All @@ -61,7 +64,6 @@ def country_compute(
dstSRS=proj,
reproject=True,
format="GPKG",
layerCreationOptions=["ENCODING=UTF-8"],
)
gdal.VectorTranslate(ofile, ifile, options=param)

Expand All @@ -87,14 +89,12 @@ def country_compute(
compute_wdpa(iso3, proj, extent_reg)
compute_biomass_avitabile(proj, extent_reg)
# Moving created files
make_dir(os.path.join(wd, output_dir, "emissions"))
copy2("AGB.tif", os.path.join(wd, output_dir, "emissions"))
dist_files = [f for f in glob("dist_*.tif") if f[-6] != "t"]
proj_files = [f for f in glob("*_PROJ.*") if f != "ctry_PROJ.tif"]
other_files = ["altitude.tif", "slope.tif", "pa.tif"]
files = dist_files + proj_files + other_files
for file in files:
copy2(file, os.path.join(wd, output_dir))
other_files = ["altitude.tif", "slope.tif", "pa.tif", "AGB.tif"]
ifiles = dist_files + proj_files + other_files
for ifile in ifiles:
copy2(ifile, os.path.join(wd, output_dir))
os.chdir(wd)

# Computing forest data
Expand All @@ -104,32 +104,15 @@ def country_compute(
os.chdir(temp_dir)
# Perform computations
compute_forest(proj, extent_reg)
# Create directories
make_dir(os.path.join(wd, output_dir, "forest"))
make_dir(os.path.join(wd, output_dir, "validation"))
make_dir(os.path.join(wd, output_dir, "forecast"))
# Copy files for modelling
ifiles = ["dist_edge_t1.tif", "dist_defor_t1.tif", "fcc12.tif"]
ofiles = ["dist_edge.tif", "dist_defor.tif", "fcc.tif"]
for (ifile, ofile) in zip(ifiles, ofiles):
forest_files = [f"forest_t{i + 1}.tif" for i in range(3)]
dist_edge_files = [f"dist_edge_t{i + 1}.tif" for i in range(3)]
dist_defor_files = [f"dist_defor_t{i + 1}.tif" for i in range(3)]
fcc_files = ["fcc12.tif", "fcc13.tif", "fcc123.tif"]
ifiles = forest_files + dist_edge_files + dist_defor_files + fcc_files
for ifile in ifiles:
if os.path.isfile(ifile):
copy2(ifile, os.path.join(wd, output_dir, ofile))
# Copy files for validation
files = ["dist_edge_t2.tif", "dist_defor_t2.tif"]
for file in files:
if os.path.isfile(file):
copy2(file, os.path.join(wd, output_dir, "validation"))
# Copy files for forecast
files = ["dist_edge_t3.tif", "dist_defor_t3.tif", "fcc13.tif"]
for file in files:
if os.path.isfile(file):
copy2(file, os.path.join(wd, output_dir, "forecast"))
# Forest data
files = [f"forest_t{i}.tif" for i in range(4)]
files.append("fcc123.tif")
for file in files:
if os.path.isfile(file):
copy2(file, os.path.join(wd, output_dir, "forest"))
copy2(ifile, os.path.join(wd, output_dir))
os.chdir(wd)

# Keep or remove directory
Expand Down

0 comments on commit 2f2157c

Please sign in to comment.