Skip to content

Commit d1b576d

Browse files
authored
Merge pull request #33 from FrontierDevelopmentLab/feature/bqa-masks
Feature/bqa masks
2 parents 0944377 + d90ad87 commit d1b576d

5 files changed

Lines changed: 73 additions & 10 deletions

File tree

conf/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ defaults:
2525
- builder: gcp
2626
- cloud: gcp
2727
- preparer: gcp
28+
- plugins: gcp
2829
- _self_
2930
tasks:
3031
- build
@@ -33,6 +34,7 @@ tasks:
3334
- schedule
3435
- prepare
3536
- deploy
37+
- plugins
3638

3739
hydra:
3840
run:

conf/plugins/gcp.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_target_: satextractor.plugins.gcp_mtl_plugin.copy_mtl_files

src/satextractor/cli.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,19 @@ def deployer(cfg):
146146
topic,
147147
)
148148

149-
# extraction_tasks_path = os.path.join(
150-
# ".", cfg.dataset_name + "_extraction_tasks.pkl",
151-
# )
152149

153-
# logger.info(f"deploying on {cfg.deployer._target_} to {cfg.cloud.storage_root}",)
150+
def plugins(cfg):
154151

155-
# extraction_tasks = pickle.load(open(extraction_tasks_path, "rb"))
152+
logger.info(f"using {cfg.plugins._target_} as plugin")
156153

157-
# # check tiles meet spec
158-
# for t in extraction_tasks:
159-
# assert isinstance(t, ExtractionTask), "Task does not match ExtractionTask spec"
154+
extraction_tasks = pickle.load(open(cfg.extraction_tasks, "rb"))
160155

161-
# hydra.utils.instantiate(cfg.deployer, extraction_tasks)
156+
hydra.utils.call(
157+
cfg.plugins,
158+
cfg.credentials,
159+
extraction_tasks,
160+
f"{cfg.cloud.storage_prefix}/{cfg.cloud.storage_root}/{cfg.dataset_name}",
161+
)
162162

163163

164164
@hydra.main(config_path="./../../conf", config_name="config")
@@ -181,7 +181,8 @@ def main(cfg: DictConfig):
181181
"schedule",
182182
"prepare",
183183
"deploy",
184-
], "valid tasks are [build, stac, tile, schedule, prepare, deploy]"
184+
"plugins",
185+
], "valid tasks are [build, stac, tile, schedule, prepare, deploy, plugins]"
185186

186187
logger.info(f"Running tasks {cfg.tasks}")
187188

@@ -203,6 +204,9 @@ def main(cfg: DictConfig):
203204
if "deploy" in cfg.tasks:
204205
deployer(cfg)
205206

207+
if "plugins" in cfg.tasks:
208+
plugins(cfg)
209+
206210
return 0
207211

208212

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .gcp_mtl_plugin import copy_mtl_files # noqa
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from typing import List
2+
3+
from gcsfs import GCSFileSystem
4+
from joblib import delayed
5+
from joblib import Parallel
6+
from satextractor.models import ExtractionTask
7+
from satextractor.utils import tqdm_joblib
8+
from tqdm import tqdm
9+
10+
11+
def copy_if_exists(fs, src, dst):
12+
if fs.exists(src):
13+
fs.copy(src, dst)
14+
15+
16+
def copy_mtl_files(
17+
credentials: str,
18+
tasks: List[ExtractionTask],
19+
storage_path: str,
20+
n_jobs: int = -1,
21+
) -> bool:
22+
fs = GCSFileSystem(token=credentials)
23+
mtl_files_info = set(
24+
[
25+
(
26+
task.constellation,
27+
tile.id,
28+
str(task.sensing_time)[:10],
29+
task.item_collection[0].assets["B1"].href.replace("B1.TIF", "MTL.txt"),
30+
)
31+
for task in tasks
32+
for tile in task.tiles
33+
if "landsat" in task.constellation
34+
],
35+
)
36+
37+
to_copy = []
38+
for mtl_info in mtl_files_info:
39+
src = mtl_info[3]
40+
dst = (
41+
f"{storage_path}/{mtl_info[1]}/{mtl_info[0]}/metadata/{mtl_info[2]}_MTL.txt"
42+
)
43+
to_copy.append((src, dst))
44+
45+
with tqdm_joblib(
46+
tqdm(
47+
desc=f"parallel copying MTL files on {storage_path}",
48+
total=len(to_copy),
49+
),
50+
):
51+
Parallel(n_jobs=n_jobs, verbose=0, prefer="threads")(
52+
[delayed(copy_if_exists)(fs, src, dst) for src, dst in to_copy],
53+
)
54+
55+
return True

0 commit comments

Comments
 (0)