Skip to content

Commit ef3b64b

Browse files
committed
Add parameters config file and add it to collection assets
1 parent 834cdea commit ef3b64b

File tree

2 files changed

+71
-17
lines changed

2 files changed

+71
-17
lines changed

workflows/new.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
from stormhub.logger import initialize_logger
22
from stormhub.met.storm_catalog import new_catalog, new_collection
3+
import json
4+
import logging
5+
import shutil
6+
import pystac
7+
8+
def save_config(config_file="params-config.json", **kwargs):
9+
"""Automatically saves configuration parameters to a JSON file."""
10+
with open(config_file, "w") as file:
11+
json.dump(kwargs, file, indent=4)
12+
13+
def add_config_to_collection(storm_collection, config_filename="params-config.json"):
14+
"""Add config file to collection assets."""
15+
collection_path = storm_collection.self_href
16+
collection_dir = os.path.dirname(collection_path)
17+
os.makedirs(collection_dir, exist_ok=True)
18+
19+
config_dest_path = os.path.join(collection_dir, config_filename)
20+
shutil.copy(config_filename, config_dest_path)
21+
22+
collection = pystac.Collection.from_file(collection_path)
23+
24+
collection.add_asset(
25+
"params-config",
26+
pystac.Asset(
27+
href=config_filename,
28+
media_type=pystac.MediaType.JSON,
29+
description= "Contains the configuration parameters used to generate the bighorn storm items.",
30+
roles=["metadata"],
31+
title="Configuration Parameters",
32+
),
33+
)
34+
collection.save_object()
35+
36+
337

438
if __name__ == "__main__":
539
initialize_logger()
@@ -25,12 +59,25 @@
2559
# Collection Args
2660
storm_duration_hours = 48
2761
min_precip_threshold = 2.5
62+
63+
save_config(
64+
start_date=start_date,
65+
end_date=end_date,
66+
top_n_events=top_n_events,
67+
storm_duration_hours=storm_duration_hours,
68+
min_precip_threshold=min_precip_threshold,
69+
root_dir=root_dir,
70+
catalog_id=catalog_id,
71+
local_directory=local_directory,
72+
)
2873
storm_collection = new_collection(
2974
storm_catalog,
3075
start_date,
3176
end_date,
3277
storm_duration_hours,
3378
min_precip_threshold,
3479
top_n_events,
35-
check_every_n_hours=6,
80+
check_every_n_hours=24,
3681
)
82+
# Add config file as a STAC collection asset
83+
add_config_to_collection(storm_collection)

workflows/resume.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
11
import os
2-
2+
import json
33
from stormhub.logger import initialize_logger
44
from stormhub.met.storm_catalog import resume_collection
55
from stormhub.utils import StacPathManager
66

7+
8+
def load_config(config_path):
9+
"""Load configuration from a JSON file."""
10+
with open(config_path, "r") as file:
11+
return json.load(file)
12+
13+
714
if __name__ == "__main__":
815
initialize_logger()
916

10-
# Catalog Args
11-
root_dir = "<local-dir>"
12-
config_file = f"{root_dir}/duwamish/config.json"
13-
catalog_id = "duwamish"
14-
local_directory = f"{root_dir}"
17+
config_path = "params-config.json"
18+
config = load_config(config_path)
1519

20+
# Extract config values
21+
start_date = config["start_date"]
22+
end_date = config["end_date"]
23+
top_n_events = config["top_n_events"]
24+
storm_duration_hours = config["storm_duration_hours"]
25+
min_precip_threshold = config["min_precip_threshold"]
26+
root_dir = config["root_dir"]
27+
catalog_id = config["catalog_id"]
28+
local_directory = config["local_directory"]
29+
30+
# Set up storm catalog file
1631
spm = StacPathManager(os.path.join(local_directory, catalog_id))
1732
storm_catalog_file = spm.catalog_file
1833

19-
# All Collection Args
20-
start_date = "1979-02-01"
21-
end_date = "2024-12-31"
22-
top_n_events = 440
23-
24-
# Collection 1 Args
25-
storm_duration_hours = 96
26-
min_precip_threshold = 4
27-
34+
# Resume collection with loaded parameters
2835
storm_collection = resume_collection(
2936
catalog=storm_catalog_file,
3037
start_date=start_date,
3138
end_date=end_date,
3239
storm_duration=storm_duration_hours,
3340
min_precip_threshold=min_precip_threshold,
3441
top_n_events=top_n_events,
35-
check_every_n_hours=6,
42+
check_every_n_hours=24,
3643
with_tb=False,
3744
create_items=True,
3845
)

0 commit comments

Comments
 (0)