Skip to content

Commit

Permalink
Add an option to compute the tiles sequentially
Browse files Browse the repository at this point in the history
  • Loading branch information
ghislainv committed May 31, 2024
1 parent ef19c46 commit 724e876
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
43 changes: 28 additions & 15 deletions geefcc/get_fcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def get_fcc(aoi,
perc=75,
tile_size=1,
# crop_to_aoi=False,
parallel=False,
ncpu=None,
output_file="fcc.tif"):
"""Get forest cover change data.
Expand Down Expand Up @@ -51,6 +52,9 @@ def get_fcc(aoi,
:param tile_size: Tile size for parallel computing.
:param parallel: Logical. Parallel (if ``True``) or sequential (if
``False``) computing. Default to ``False``.
:param ncpu: Number of CPU to use for parallel computing. If None,
it will be set to the number of cores on the computer minus
one.
Expand Down Expand Up @@ -104,23 +108,32 @@ def get_fcc(aoi,
out_dir_tiles = opj(out_dir, "forest_tiles")
make_dir(out_dir_tiles)

# Write tiles in parallel
# https://superfastpython.com/multiprocessing-pool-starmap_async/
# Message
print(f"get_fcc running, {ntiles} tiles .", end="", flush=True)
# create and configure the process pool
if ncpu is None:
ncpu = os.cpu_count() - 1
with mp.Pool(processes=ncpu) as pool:
# prepare arguments
args = [(i, ext, ntiles, forest, proj, scale, out_dir_tiles)
for (i, ext) in enumerate(grid)]
# issue many tasks asynchronously to the process pool
_ = pool.starmap_async(geeic2geotiff, args)
# close the pool
pool.close()
# wait for all issued tasks to complete
pool.join()

# Sequential computing
if parallel is False:
# Loop on tiles
for (i, ext) in enumerate(grid):
geeic2geotiff(i, ext, ntiles, forest, proj, scale, out_dir_tiles)

# Parallel computing
if parallel is True:
# Write tiles in parallel
# https://superfastpython.com/multiprocessing-pool-starmap_async/
# create and configure the process pool
if ncpu is None:
ncpu = os.cpu_count() - 1
with mp.Pool(processes=ncpu) as pool:
# prepare arguments
args = [(i, ext, ntiles, forest, proj, scale, out_dir_tiles)
for (i, ext) in enumerate(grid)]
# issue many tasks asynchronously to the process pool
_ = pool.starmap_async(geeic2geotiff, args)
# close the pool
pool.close()
# wait for all issued tasks to complete
pool.join()

# Geotiff from tiles
geotiff_from_tiles(output_file)
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@


# find_version
def find_version():
def find_version(pkg_name):
"""Finding package version."""
with open("geefcc/__init__.py", encoding="utf-8") as init_file:
with open(f"{pkg_name}/__init__.py", encoding="utf-8") as init_file:
init_text = init_file.read()
_version = (re.search('^__version__\\s*=\\s*"(.*)"',
init_text, re.M)
.group(1))
return _version


version = find_version()
version = find_version("geefcc")

# reStructuredText README file
with io.open("README.rst", encoding="utf-8") as f:
Expand Down

0 comments on commit 724e876

Please sign in to comment.