Skip to content

Commit 994404f

Browse files
committed
Remove old implementation of region allocation/reduction endpoints
1 parent 118787f commit 994404f

File tree

1 file changed

+9
-86
lines changed

1 file changed

+9
-86
lines changed

ws.py

Lines changed: 9 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -756,49 +756,9 @@ def ndc_projections(region):
756756
return {"ndc_inventory": ndc_range_inventory(region), "ndc_jones": ndc_range_jones(region)}
757757

758758

759-
def get_ds(region):
760-
if region not in available_region_files:
761-
raise ValueError(f"Region {region} not found")
762-
fn = available_region_files[region]
763-
return xr.open_dataset(fn)
764-
765-
766-
def emission_allocation_per_method_old(region, allocation_method):
767-
selection = global_pathway_choices()
768-
ds = get_ds(region)[allocation_method].sel(**selection).rename(Time="time")
769-
# set time as the first dimension
770-
dim_order = ["time"] + [dim for dim in ds.dims if dim != "time"]
771-
ds = ds.transpose(*dim_order)
772-
773-
# extract the 'most reasonable' (mr) df which will be the main trajectory line
774-
mr_selection = dict()
775-
if allocation_method in ["PC", "PCC", "AP", "GDR", "ECPC"]:
776-
mr_selection.update(Scenario="SSP2")
777-
if allocation_method in ["PCC", "ECPC"]:
778-
mr_selection.update(Convergence_year=DEFAULT_CONVERGENCE_YEAR)
779-
if allocation_method in ["ECPC", "GDR"]:
780-
mr_selection.update(Historical_startyear=DEFAULT_HISTORICAL_STARTYEAR)
781-
if allocation_method == "ECPC":
782-
mr_selection.update(
783-
Discount_factor=DEFAULT_DISCOUNT_FACTOR,
784-
)
785-
if allocation_method == "GDR":
786-
mr_selection.update(RCI_weight=DEFAULT_RCI_WEIGHT, Capability_threshold=DEFAULT_CAPABILITY_THRESHOLD)
787-
788-
mr_df = ds.sel(**mr_selection).to_pandas().rename("mean")
789-
790-
if mr_df.isna().all():
791-
return None
792-
793-
agg_dims = [dim for dim in ds.dims if dim != "time"]
794-
min_df = ds.min(agg_dims, skipna=True).to_pandas().rename("min")
795-
max_df = ds.max(agg_dims, skipna=True).to_pandas().rename("max")
796-
797-
return pd.concat([mr_df, min_df, max_df], axis=1).reset_index().dropna().to_dict(orient="records")
798-
799-
800759
allocation_methods = {"PC", "PCC", "AP", "GDR", "ECPC", "GF"}
801760

761+
802762
def emission_allocation_per_method(allocator_ds, allocation_method):
803763
ds = allocator_ds.rename(Time="time")
804764
dim_order = ["time"] + [dim for dim in ds.dims if dim != "time"]
@@ -830,14 +790,15 @@ def emission_allocation_per_method(allocator_ds, allocation_method):
830790

831791
return pd.concat([mr_df, min_df, max_df], axis=1).reset_index().dropna().to_dict(orient="records")
832792

793+
833794
@app.get("/timeseries/<region>/emissions/allocations")
834795
def emission_allocations(region):
835796
allocator = create_allocator(region)
836797
allocations = {}
837798
selection = global_pathway_choices()
838799
for allocation_method in allocation_methods:
839800
allocation_data = emission_allocation_per_method(
840-
allocator.xr_total[allocation_method].sel(**selection),
801+
allocator.xr_total[allocation_method].sel(**selection),
841802
allocation_method,
842803
)
843804
if allocation_data is None:
@@ -848,11 +809,13 @@ def emission_allocations(region):
848809

849810
@lru_cache(maxsize=20)
850811
def create_allocator(region):
851-
lulucf="incl"
852-
gas="GHG"
853-
input_file = '../effort-sharing/notebooks/input.yml'
812+
lulucf = "incl"
813+
gas = "GHG"
814+
input_file = "../effort-sharing/notebooks/input.yml"
854815
allocator = allocation(
855-
region, lulucf=lulucf, gas=gas,
816+
region,
817+
lulucf=lulucf,
818+
gas=gas,
856819
input_file=input_file,
857820
)
858821
allocator.gf()
@@ -867,18 +830,6 @@ def create_allocator(region):
867830
allocator.gdr()
868831
return allocator
869832

870-
def emission_allocations_old(region):
871-
"""
872-
http://127.0.0.1:5000/timeseries/USA/emissions/allocations?exceedanceRisk=0.67&negativeEmissions=0.4&temperature=1.8: 36.94ms
873-
"""
874-
allocations = {}
875-
for allocation_method in allocation_methods:
876-
allocation = emission_allocation_per_method_old(region, allocation_method)
877-
if allocation is None:
878-
continue
879-
allocations[allocation_method] = allocation
880-
return allocations
881-
882833

883834
@app.get("/statistics/reductions/<region>")
884835
def allocation_reduction(region):
@@ -909,34 +860,6 @@ def allocation_reduction(region):
909860

910861
return reductions
911862

912-
def allocation_reduction_old(region):
913-
periods = (2030, 2040)
914-
selection = dict(
915-
**global_pathway_choices(),
916-
Time=periods,
917-
)
918-
919-
hist = ds_global.GHG_hist.sel(Region=region, Time=1990).values + 0
920-
ds = get_ds(region)
921-
922-
reductions = {}
923-
for allocation_method in allocation_methods:
924-
pselection = selection.copy()
925-
if allocation_method in ["PC", "PCC", "AP", "GDR", "ECPC"]:
926-
pselection.update(Scenario="SSP2")
927-
if allocation_method in ["PCC", "ECPC"]:
928-
pselection.update(Convergence_year=DEFAULT_CONVERGENCE_YEAR)
929-
reductions[allocation_method] = {}
930-
for period in periods:
931-
pselection.update(Time=period)
932-
es = ds[allocation_method].sel(**pselection).mean().values + 0
933-
if np.isnan(es) or np.isnan(hist) or hist == 0:
934-
reductions[allocation_method][period] = None
935-
else:
936-
reductions[allocation_method][period] = -(es - hist) / hist * 100
937-
938-
return reductions
939-
940863

941864
if __name__ == "__main__":
942865
print(create_allocator("BRA"))

0 commit comments

Comments
 (0)