Skip to content

Commit

Permalink
Merge pull request #569 from Open-Earth-Foundation/fix_country_route
Browse files Browse the repository at this point in the history
fix added co2eq values into country route
  • Loading branch information
amanda-eames authored Jul 18, 2024
2 parents 5fbe4c8 + 51bdfb5 commit 930653b
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions global-api/routes/country_code_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,49 @@ def get_emissions_by_country_and_year(source_name: str, country_code: str, year:

emissions = totals["totals"]["emissions"]

total_co2eq_100yr = 0
total_co2eq_20yr = 0

co2e = list(filter(lambda x: x["gas_name"] == "co2e", records))

if (len(co2e) > 0):
val = str(int(round(float(co2e[0]["emissions_value"]))))
emissions["co2eq_100yr"] = val
emissions["co2eq_20yr"] = val
val = int(round(float(co2e[0]["emissions_value"])))
total_co2eq_100yr += val
total_co2eq_20yr += val

co2 = list(filter(lambda x: x["gas_name"] == "co2", records))

if (len(co2) > 0):
val = str(int(round(float(co2[0]["emissions_value"]))))
emissions["co2_mass"] = val
emissions["co2_co2eq"] = val
val = int(round(float(co2[0]["emissions_value"])))
emissions["co2_mass"] = str(val)
emissions["co2_co2eq"] = str(val)
total_co2eq_100yr += val
total_co2eq_20yr += val


ch4 = list(filter(lambda x: x["gas_name"] == "ch4", records))

if (len(ch4) > 0):
val = str(int(round(float(ch4[0]["emissions_value"]))))
emissions["ch4_mass"] = val
val = int(round(float(ch4[0]["emissions_value"])))
emissions["ch4_mass"] = str(val)
emissions["ch4_co2eq_100yr"] = str(int(round(float(ch4[0]["emissions_value"] * gas_to_gwp100["ch4"]))))
emissions["ch4_co2eq_20yr"] = str(int(round(float(ch4[0]["emissions_value"] * gas_to_gwp20["ch4"]))))
total_co2eq_100yr += val * gas_to_gwp100["ch4"]
total_co2eq_20yr += val * gas_to_gwp20["ch4"]


n2o = list(filter(lambda x: x["gas_name"] == "n2o", records))

if (len(n2o) > 0):
val = str(int(round(float(n2o[0]["emissions_value"]))))
emissions["n2o_mass"] = val
val = int(round(float(n2o[0]["emissions_value"])))
emissions["n2o_mass"] = str(val)
emissions["n2o_co2eq_100yr"] = str(int(round(float(n2o[0]["emissions_value"] * gas_to_gwp100["n2o"]))))
emissions["n2o_co2eq_20yr"] = str(int(round(float(n2o[0]["emissions_value"] * gas_to_gwp20["n2o"]))))
total_co2eq_100yr += val * gas_to_gwp100["n2o"]
total_co2eq_20yr += val * gas_to_gwp20["n2o"]


emissions["co2eq_100yr"] = str(int(round(total_co2eq_100yr)))
emissions["co2eq_20yr"] = str(int(round(total_co2eq_20yr)))

return {**totals}

0 comments on commit 930653b

Please sign in to comment.