-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
116 lines (106 loc) · 3.84 KB
/
Snakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from pathlib import Path
import json
from typing import List
import os
# Default configuration with option to override via --config
OUTPUT_DIR = Path(config.get("output_dir", "./output"))
HUCS_FILE = Path(config.get("hucs_file", "./data/target_huc10s.csv"))
PARAMS_FILE = Path(config.get("params_file", "./params/params_10m.toml"))
# helper function used in download_all and process_all
def get_hucs() -> List[str]:
import pandas as pd
# if file exists
if os.path.exists(HUCS_FILE):
return pd.read_csv(HUCS_FILE)['hucid'].tolist()
else:
return []
# WORKFLOW
# 1. Setup
# 2. Download
# 3. Process
# 4. Postprocess (mosaic)
# setup
rule setup:
output:
na_boundaries = "data/north_america.shp",
us_boundaries = "data/us_states.shp",
whitebox_init = "data/whitebox_init.txt",
run:
shell("poetry run python src/dl_land_shape.py")
shell("poetry run python src/init_whitebox.py")
rule generate_targets:
output:
target_huc10s = "data/target_huc10s.csv",
run:
shell("poetry run python src/generate_target_huc10s.py")
# download
rule download:
input:
expand(
[OUTPUT_DIR / "{hucid}/{hucid}-dem.tif",
OUTPUT_DIR / "{hucid}/{hucid}-flowlines.shp"],
hucid=get_hucs()
)
# process
rule process:
input:
expand(
OUTPUT_DIR / "floors" / "{hucid}-floors.tif",
hucid=get_hucs()
)
# postprocess
rule mosaic:
input:
floors = expand(OUTPUT_DIR / "floors" / "{hucid}-floors.tif", hucid=get_hucs()),
us_land_file = "data/us_states.shp",
na_land_file = "data/north_america.shp"
output:
directory(OUTPUT_DIR / "mosaic_ca_watershed"),
directory(OUTPUT_DIR / "mosaic_ca_state"),
directory(OUTPUT_DIR / "mosaic_all"),
shell:
"""
mkdir -p {output}
poetry run python src/mosaic.py {OUTPUT_DIR}/floors {OUTPUT_DIR}/mosaic_ca_state {input.na_land_file} {input.us_land_file} --state-boundary-clip --level huc6
poetry run python src/mosaic.py {OUTPUT_DIR}/floors {OUTPUT_DIR}/mosaic_ca_watershed {input.na_land_file} {input.us_land_file} --watershed-boundary-clip --level huc6
poetry run python src/mosaic.py {OUTPUT_DIR}/floors {OUTPUT_DIR}/mosaic_all {input.na_land_file} {input.us_land_file} --level huc6
"""
rule download_one:
input:
us_land_file = "data/us_states.shp",
na_land_file = "data/north_america.shp"
output:
dem = OUTPUT_DIR / "{hucid}/{hucid}-dem.tif",
flowlines = OUTPUT_DIR / "{hucid}/{hucid}-flowlines.shp",
resources:
download_slots=1
shell:
"poetry run python src/dl_dem_and_flowlines.py "
"{wildcards.hucid} {output.dem} {output.flowlines} "
"{input.us_land_file} {input.na_land_file}"
rule process_one:
input:
dem = OUTPUT_DIR / "{hucid}/{hucid}-dem.tif",
flowlines = OUTPUT_DIR / "{hucid}/{hucid}-flowlines.shp",
whitebox_init = "data/whitebox_init.txt",
params:
working_dir = lambda wildcards: OUTPUT_DIR / f"{wildcards.hucid}_working_dir",
param_file = PARAMS_FILE,
output:
floor_ofile = OUTPUT_DIR / "floors" / "{hucid}-floors.tif",
wp_ofile = OUTPUT_DIR / "floors" / "{hucid}-wp.shp",
flowlines_ofile = OUTPUT_DIR / "floors" / "{hucid}-flowlines.shp",
log_file = OUTPUT_DIR / "floors" / "{hucid}-run.log"
shell:
"""
poetry run python -m valleyx \
--dem_file {input.dem} \
--flowlines_file {input.flowlines} \
--param_file {params.param_file} \
--working_dir {params.working_dir} \
--wp_ofile {output.wp_ofile} \
--flowlines_ofile {output.flowlines_ofile} \
--enable_logging \
--log_file {output.log_file} \
--floor_ofile {output.floor_ofile}
"""