Skip to content

Commit 75e2134

Browse files
JMGilbertJMGilbert
authored andcommitted
replicated energy and labor input generation
1 parent 57280d3 commit 75e2134

File tree

1 file changed

+93
-3
lines changed

1 file changed

+93
-3
lines changed

dscim/preprocessing/input_damages.py

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pathlib import Path
1515
from itertools import product
1616
from functools import partial
17-
from p_tqdm import p_map
17+
from p_tqdm import p_map, p_umap
1818
from dscim.menu.simple_storage import EconVars
1919
from zarr.errors import GroupNotFoundError
2020

@@ -220,13 +220,50 @@ def concatenate_labor_damages(
220220

221221
# save out
222222
if format_file == "zarr":
223-
concat_ds.to_zarr(f"{path_to_file}.zarr", consolidated=True, mode="w")
223+
to_store = concat_ds.copy()
224+
for var in to_store.variables:
225+
to_store[var].encoding.clear()
226+
227+
to_store.to_zarr(f"{path_to_file}.zarr", mode="w", consolidated=True)
224228
elif format_file == "netcdf":
225229
concat_ds.to_netcdf(f"{path_to_file}.nc4")
226230

227231
return concat_ds
228232

229233

234+
def calculate_labor_batch_damages(batch, ec, input_path, save_path):
235+
print(f"Processing batch={batch} damages in {os.getpid()}")
236+
concatenate_labor_damages(
237+
input_path=input_path,
238+
save_path=save_path,
239+
ec_cls=ec,
240+
variable="rebased",
241+
val_type="wage-levels",
242+
format_file="zarr",
243+
query=f"exists==True&batch=='batch{batch}'",
244+
)
245+
print(f"Saved!")
246+
247+
248+
def calculate_labor_damages(
249+
path_econ="/shares/gcp/integration/float32/dscim_input_data/econvars/zarrs/integration-econ-bc39.zarr",
250+
input_path="/shares/gcp/outputs/labor/impacts-woodwork/mc_correct_rebasing_for_integration",
251+
save_path="/shares/gcp/integration/float32/input_data_histclim/labor_data/replication/",
252+
):
253+
ec = EconVars(path_econ)
254+
# process in 3 rounds to limit memory usage
255+
for i in range(0, 3):
256+
partial_func = partial(
257+
calculate_labor_batch_damages,
258+
input_path=input_path,
259+
save_path=save_path,
260+
ec=ec,
261+
)
262+
print("Processing batches:")
263+
print(list(range(i * 5, i * 5 + 5)))
264+
r = p_umap(partial_func, list(range(i * 5, i * 5 + 5)))
265+
266+
230267
def compute_ag_damages(
231268
input_path,
232269
save_path,
@@ -561,13 +598,66 @@ def concatenate_energy_damages(
561598
logger.info(f"Concatenating and processing {i}")
562599

563600
if format_file == "zarr":
564-
concat_ds.to_zarr(f"{path_to_file}.zarr", consolidated=True, mode="w")
601+
to_store = concat_ds.copy()
602+
for var in to_store.variables:
603+
to_store[var].encoding.clear()
604+
605+
to_store.to_zarr(f"{path_to_file}.zarr", mode="w", consolidated=True)
565606
elif format_file == "netcdf":
566607
concat_ds.to_netcdf(f"{path_to_file}.nc4")
567608

568609
return concat_ds
569610

570611

612+
def calculate_energy_batch_damages(batch, ec, input_path, save_path):
613+
print(f"Processing batch={batch} damages in {os.getpid()}")
614+
concatenate_energy_damages(
615+
input_path=input_path,
616+
file_prefix="TINV_clim_integration_total_energy",
617+
save_path=save_path,
618+
ec_cls=ec,
619+
variable="rebased",
620+
format_file="zarr",
621+
query=f"exists==True&batch=='batch{batch}'",
622+
)
623+
print(f"Saved!")
624+
625+
626+
def calculate_energy_damages(
627+
re_calculate=True,
628+
path_econ="/shares/gcp/integration/float32/dscim_input_data/econvars/zarrs/integration-econ-bc39.zarr",
629+
input_path="/shares/gcp/outputs/energy_pixel_interaction/impacts-blueghost/integration_resampled",
630+
save_path="/shares/gcp/integration/float32/input_data_histclim/energy_data/replication_2022aug/",
631+
):
632+
ec = EconVars(path_econ)
633+
634+
if re_calculate:
635+
read_energy_files_parallel(
636+
input_path=input_path,
637+
save_path=save_path,
638+
ec_cls=ec,
639+
seed="TINV_clim_integration_total_energy_delta",
640+
)
641+
read_energy_files_parallel(
642+
input_path=input_path,
643+
save_path=save_path,
644+
ec_cls=ec,
645+
seed="TINV_clim_integration_total_energy_histclim",
646+
)
647+
648+
# process in 3 rounds to limit memory usage
649+
for i in range(0, 3):
650+
partial_func = partial(
651+
calculate_energy_batch_damages,
652+
input_path=input_path,
653+
save_path=save_path,
654+
ec=ec,
655+
)
656+
print("Processing batches:")
657+
print(list(range(i * 5, i * 5 + 5)))
658+
r = p_umap(partial_func, list(range(i * 5, i * 5 + 5)))
659+
660+
571661
def prep_mortality_damages(
572662
gcms,
573663
paths,

0 commit comments

Comments
 (0)