Skip to content

Commit

Permalink
Use a random file name for vrt to avoid pb when run in parallel
Browse files Browse the repository at this point in the history
We used gdal.BuildVRT("/vsimem/var.vrt", ...) but this can lead to
problems if several files are generated at the same time when run in
parallel. We now use a random file name with 6 digits.
  • Loading branch information
ghislainv committed Jun 17, 2024
1 parent 601447f commit 5ec7a54
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 6 additions & 3 deletions forestatrisk/predict/predict_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from glob import glob
import os
import sys
import random

# Third party imports
import numpy as np
Expand Down Expand Up @@ -90,10 +91,12 @@ def predict_raster(
yRes=-gt[5],
separate=True,
)
rand_int = random.randint(1, 999999)
vrt_file = f"/vsimem/var_{rand_int:06}.vrt"
cback = gdal.TermProgress if verbose else 0
gdal.BuildVRT("/vsimem/var.vrt", raster_list,
gdal.BuildVRT(vrt_file, raster_list,
options=param, callback=cback)
stack = gdal.Open("/vsimem/var.vrt")
stack = gdal.Open(vrt_file)
nband = stack.RasterCount
proj = stack.GetProjection()

Expand All @@ -109,7 +112,7 @@ def predict_raster(
bandND = bandND.astype(np.float32)

# Make blocks
blockinfo = makeblock("/vsimem/var.vrt", blk_rows=blk_rows)
blockinfo = makeblock(vrt_file, blk_rows=blk_rows)
nblock = blockinfo[0]
nblock_x = blockinfo[1]
x = blockinfo[3]
Expand Down
9 changes: 6 additions & 3 deletions forestatrisk/predict/predict_raster_binomial_iCAR.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from glob import glob
import os
import sys
import random

# Third party imports
import numpy as np
Expand Down Expand Up @@ -108,10 +109,12 @@ def predict_raster_binomial_iCAR(
yRes=-gt[5],
separate=True,
)
rand_int = random.randint(1, 999999)
vrt_file = f"/vsimem/var_{rand_int:06}.vrt"
cback = gdal.TermProgress if verbose else 0
gdal.BuildVRT("/vsimem/var.vrt", raster_list,
gdal.BuildVRT(vrt_file, raster_list,
options=param, callback=cback)
stack = gdal.Open("/vsimem/var.vrt")
stack = gdal.Open(vrt_file)
nband = stack.RasterCount
proj = stack.GetProjection()

Expand All @@ -127,7 +130,7 @@ def predict_raster_binomial_iCAR(
bandND = bandND.astype(np.float32)

# Make blocks
blockinfo = makeblock("/vsimem/var.vrt", blk_rows=blk_rows)
blockinfo = makeblock(vrt_file, blk_rows=blk_rows)
nblock = blockinfo[0]
nblock_x = blockinfo[1]
x = blockinfo[3]
Expand Down

0 comments on commit 5ec7a54

Please sign in to comment.