Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat added migration file and update api for forecast #1053

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""add table emissions forecasting

Revision ID: 080e981ba25a
Revises: b1e6ab175504
Create Date: 2025-01-13 16:53:25.090340

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import text

# revision identifiers, used by Alembic.
revision: str = '080e981ba25a'
down_revision: Union[str, None] = 'b1e6ab175504'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:

conn = op.get_bind()

conn.execute(text("""
CREATE TABLE IF NOT EXISTS modelled.ghgi_emission_forecast(
id uuid NOT NULL,
actor_id varchar,
cluster_id int,
cluster_name json,
cluster_description json,
gpc_sector varchar,
forecast_year int,
future_year int,
growth_rate numeric,
spatial_granularity varchar,
datasource varchar,
CONSTRAINT ghgi_forecast PRIMARY KEY (id)
);
"""))

pass


def downgrade() -> None:
op.drop_table('ghgi_emission_forecast', schema='modelled')
pass
130 changes: 4 additions & 126 deletions global-api/routes/ghgi_emission_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,133 +10,11 @@ def db_city_emission_forecast(actor_id, forecast_year, spatial_granularity):
# TO DO: once the data has been loaded into the database we need to update the query
query = text(
"""
SELECT locode,cluster_id,cluster_name,cluster_description,gpc_sector,forecast_year,future_year,growth_rate
FROM (
SELECT 'BR SER' as locode,
3 as cluster_id,
'{"en": "Medium-sized cities with IPPU GHG emissions industries",
"es": "Ciudades de tamaño medio con industrias de emisiones de GHG IPPU",
"pt": "Cidades de médio porte com indústrias de emissões de GHG IPPU"}'::json AS cluster_name,
'{"en": "24 municipalities, 79% of which are small (<150k pop); high participation of services and industry in the GDP; GDP with high emissions intensity; high level of emissions per capita; high participation of industry in emissions. Medium-sized municipalities with specialization in emission-intensive industrial sectors.",
"es": "24 municipios, 79% de los cuales son pequeños (<150k pop); alta participación de los servicios y la industria en el PIB; PIB con alta intensidad de emisiones; alto nivel de emisiones per cápita; alta participación de la industria en las emisiones. Municipios de tamaño medio con especialización en sectores industriales intensivos en emisiones.",
"pt": "24 municípios, 79% dos quais são pequenos (<150k pop); alta participação de serviços e indústria no PIB; PIB com alta intensidade de emissões; alto nível de emissões per capita; alta participação da indústria nas emissões. Municípios de médio porte com especialização em setores industriais intensivos em emissões."
}'::json cluster_description,
TRIM(unnest(STRING_TO_ARRAY(gpc_sector,','))) AS gpc_sector,
2023 as forecast_year,
year as future_year,
value/100 as growth_rate
FROM (
SELECT 'III' AS gpc_sector, 2024 AS year, 0.670 AS value UNION ALL
SELECT 'III', 2025, 0.440 UNION ALL
SELECT 'III', 2026, 0.460 UNION ALL
SELECT 'III', 2027, 0.430 UNION ALL
SELECT 'III', 2028, 0.430 UNION ALL
SELECT 'III', 2029, 0.430 UNION ALL
SELECT 'III', 2030, 0.430 UNION ALL
SELECT 'III', 2031, 0.430 UNION ALL
SELECT 'III', 2032, 0.430 UNION ALL
SELECT 'III', 2033, 0.430 UNION ALL
SELECT 'III', 2034, 0.430 UNION ALL
SELECT 'III', 2035, 0.430 UNION ALL
SELECT 'III', 2036, 0.430 UNION ALL
SELECT 'III', 2037, 0.430 UNION ALL
SELECT 'III', 2038, 0.430 UNION ALL
SELECT 'III', 2039, 0.430 UNION ALL
SELECT 'III', 2040, 0.430 UNION ALL
SELECT 'III', 2041, 0.430 UNION ALL
SELECT 'III', 2042, 0.430 UNION ALL
SELECT 'III', 2043, 0.430 UNION ALL
SELECT 'III', 2044, 0.430 UNION ALL
SELECT 'III', 2045, 0.430 UNION ALL
SELECT 'III', 2046, 0.430 UNION ALL
SELECT 'III', 2047, 0.430 UNION ALL
SELECT 'III', 2048, 0.430 UNION ALL
SELECT 'III', 2049, 0.430 UNION ALL
SELECT 'III', 2050, 0.430 UNION ALL
SELECT 'I, II', 2024, 2.500 UNION ALL
SELECT 'I, II', 2025, 1.630 UNION ALL
SELECT 'I, II', 2026, 1.710 UNION ALL
SELECT 'I, II', 2027, 1.590 UNION ALL
SELECT 'I, II', 2028, 1.590 UNION ALL
SELECT 'I, II', 2029, 1.590 UNION ALL
SELECT 'I, II', 2030, 1.590 UNION ALL
SELECT 'I, II', 2031, 1.590 UNION ALL
SELECT 'I, II', 2032, 1.590 UNION ALL
SELECT 'I, II', 2033, 1.590 UNION ALL
SELECT 'I, II', 2034, 1.590 UNION ALL
SELECT 'I, II', 2035, 1.590 UNION ALL
SELECT 'I, II', 2036, 1.590 UNION ALL
SELECT 'I, II', 2037, 1.590 UNION ALL
SELECT 'I, II', 2038, 1.590 UNION ALL
SELECT 'I, II', 2039, 1.590 UNION ALL
SELECT 'I, II', 2040, 1.590 UNION ALL
SELECT 'I, II', 2041, 1.590 UNION ALL
SELECT 'I, II', 2042, 1.590 UNION ALL
SELECT 'I, II', 2043, 1.590 UNION ALL
SELECT 'I, II', 2044, 1.590 UNION ALL
SELECT 'I, II', 2045, 1.590 UNION ALL
SELECT 'I, II', 2046, 1.590 UNION ALL
SELECT 'I, II', 2047, 1.590 UNION ALL
SELECT 'I, II', 2048, 1.590 UNION ALL
SELECT 'I, II', 2049, 1.590 UNION ALL
SELECT 'I, II', 2050, 1.590 UNION all
SELECT 'V', 2024, 1.230 UNION ALL
SELECT 'V', 2025, 0.800 UNION ALL
SELECT 'V', 2026, 0.840 UNION ALL
SELECT 'V', 2027, 0.780 UNION ALL
SELECT 'V', 2028, 0.780 UNION ALL
SELECT 'V', 2029, 0.780 UNION ALL
SELECT 'V', 2030, 0.780 UNION ALL
SELECT 'V', 2031, 0.780 UNION ALL
SELECT 'V', 2032, 0.780 UNION ALL
SELECT 'V', 2033, 0.780 UNION ALL
SELECT 'V', 2034, 0.780 UNION ALL
SELECT 'V', 2035, 0.780 UNION ALL
SELECT 'V', 2036, 0.780 UNION ALL
SELECT 'V', 2037, 0.780 UNION ALL
SELECT 'V', 2038, 0.780 UNION ALL
SELECT 'V', 2039, 0.780 UNION ALL
SELECT 'V', 2040, 0.780 UNION ALL
SELECT 'V', 2041, 0.780 UNION ALL
SELECT 'V', 2042, 0.780 UNION ALL
SELECT 'V', 2043, 0.780 UNION ALL
SELECT 'V', 2044, 0.780 UNION ALL
SELECT 'V', 2045, 0.780 UNION ALL
SELECT 'V', 2046, 0.780 UNION ALL
SELECT 'V', 2047, 0.780 UNION ALL
SELECT 'V', 2048, 0.780 UNION ALL
SELECT 'V', 2049, 0.780 UNION ALL
SELECT 'V', 2050, 0.780 UNION ALL
SELECT 'IV', 2024, 1.960 UNION ALL
SELECT 'IV', 2025, 1.270 UNION ALL
SELECT 'IV', 2026, 1.330 UNION ALL
SELECT 'IV', 2027, 1.240 UNION ALL
SELECT 'IV', 2028, 1.240 UNION ALL
SELECT 'IV', 2029, 1.240 UNION ALL
SELECT 'IV', 2030, 1.240 UNION ALL
SELECT 'IV', 2031, 1.240 UNION ALL
SELECT 'IV', 2032, 1.240 UNION ALL
SELECT 'IV', 2033, 1.240 UNION ALL
SELECT 'IV', 2034, 1.240 UNION ALL
SELECT 'IV', 2035, 1.240 UNION ALL
SELECT 'IV', 2036, 1.240 UNION ALL
SELECT 'IV', 2037, 1.240 UNION ALL
SELECT 'IV', 2038, 1.240 UNION ALL
SELECT 'IV', 2039, 1.240 UNION ALL
SELECT 'IV', 2040, 1.240 UNION ALL
SELECT 'IV', 2041, 1.240 UNION ALL
SELECT 'IV', 2042, 1.240 UNION ALL
SELECT 'IV', 2043, 1.240 UNION ALL
SELECT 'IV', 2044, 1.240 UNION ALL
SELECT 'IV', 2045, 1.240 UNION ALL
SELECT 'IV', 2046, 1.240 UNION ALL
SELECT 'IV', 2047, 1.240 UNION ALL
SELECT 'IV', 2048, 1.240 UNION ALL
SELECT 'IV', 2049, 1.240 UNION ALL
SELECT 'IV', 2050, 1.240) t ) t
WHERE locode = :actor_id
SELECT actor_id,cluster_id,cluster_name,cluster_description,gpc_sector,forecast_year,future_year,growth_rate
FROM modelled.ghgi_emission_forecast
WHERE actor_id = :actor_id
AND forecast_year = :forecast_year
AND 'city' = :spatial_granularity
AND spatial_granularity = :spatial_granularity
"""
)

Expand Down
Loading