1- import yaml
1+ import hydra
2+ from omegaconf import OmegaConf
3+ import json
24
35conda : "requirements.yaml"
4- configfile : "conf/config.yaml"
56
6- # == Load configuration ==
7+ # the workflow configuration file is orchestrated by hydra
8+ # read config with hydra
9+ with hydra .initialize (config_path = "conf" , version_base = None ):
10+ cfg = hydra .compose (config_name = "config" , overrides = [])
11+ #print(OmegaConf.to_yaml(cfg))
712
8- # dynamic config files
9- defaults_dict = {key : value for d in config ['defaults' ] if isinstance (d , dict ) for key , value in d .items ()}
10- shapefiles_cfg = yaml .safe_load (open (f"conf/shapefiles/{ defaults_dict ['shapefiles' ]} .yaml" , 'r' ))
11- # == Define variables ==
12- shapefile_list = shapefiles_cfg .keys ()
13- print (shapefile_list )
13+ # convert to dict of single shapefile dicts
14+ shapefiles_cfg = OmegaConf .to_container (cfg .shapefiles , resolve = True )
15+ #print(shapefiles_cfg)
16+ shapefiles_cfg_dict = {shapefile ["name" ]: "[" + json .dumps (shapefile ).replace ('"' , '' ) + "]" for shapefile in shapefiles_cfg }
17+ #print(shapefiles_cfg_dict)
18+ shapefiles_list = list (shapefiles_cfg_dict .keys ())
19+ #print(shapefiles_list)
20+ # print(f"""
21+ # python src/aggregate_climate_types.py "+shapefiles={shapefiles_cfg_dict[shapefiles_list[0]]}"
22+ # """)
23+
24+ #raise ValueError("stop here")
1425
1526rule all :
1627 input :
1728 expand (f"data/output/climate_types_raster2polygon/climate_types_{{shapefile_name}}.parquet" ,
18- shapefile_name = shapefile_list
29+ shapefile_name = shapefiles_list
1930 )
2031
2132rule download_climate_types :
2233 output :
23- f"data/input/climate_types/{ config [ ' climate_types_file' ] } "
34+ f"data/input/climate_types/{ cfg . climate_types_file } "
2435 shell :
2536 "python src/download_climate_types.py"
2637
@@ -33,11 +44,17 @@ rule download_climate_types:
3344
3445rule aggregate_climate_types :
3546 input :
36- f"data/input/climate_types/{ config [ ' climate_types_file' ] } " ,
47+ f"data/input/climate_types/{ cfg . climate_types_file } " ,
3748 f"data/input/shapefiles/{{shapefile_name}}/{{shapefile_name}}.shp"
3849 output :
3950 f"data/output/climate_types_raster2polygon/climate_types_{{shapefile_name}}.parquet" ,
4051 f"data/intermediate/climate_pcts/climate_pcts_{{shapefile_name}}.json" ,
4152 f"data/intermediate/climate_pcts/climate_types_{{shapefile_name}}.csv"
53+ params :
54+ shapefile_name = lambda wildcards : shapefiles_cfg_dict [wildcards .shapefile_name ]
4255 shell :
43- f"python src/aggregate_climate_types.py"
56+ (f"""
57+ echo {{wildcards.shapefile_name}}
58+ python src/aggregate_climate_types.py "+shapefiles={{params.shapefile_name}}"
59+ """ )
60+ #python src/aggregate_climate_types.py "+shapefiles=[{name: CAN_ADM2, url: null, idvar: shapeID, output_idvar: id}]"
0 commit comments