-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added dataseeder and run shell script
- Loading branch information
Amanda Eames
authored and
Amanda Eames
committed
Apr 29, 2024
1 parent
df48b95
commit 3bc97d1
Showing
4 changed files
with
113 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/bin/bash | ||
|
||
if command -v python3 &>/dev/null; then | ||
python_cmd=python3 | ||
else | ||
python_cmd=python | ||
fi | ||
|
||
export PGPASSWORD=$CC_GLOBAL_API_DB_PASSWORD | ||
export DB_URI="postgresql://$CC_GLOBAL_API_DB_USER:$CC_GLOBAL_API_DB_PASSWORD@$CC_GLOBAL_API_DB_HOST/$CC_GLOBAL_API_DB_NAME" | ||
|
||
# export DB_URI="postgresql://ccglobal:@localhost/ccglobal" | ||
# export CC_GLOBAL_API_DB_HOST="localhost" | ||
# export CC_GLOBAL_API_DB_USER="ccglobal" | ||
# export CC_GLOBAL_API_DB_NAME="ccglobal" | ||
|
||
# Argentinian | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
# load cammesa | ||
|
||
pushd importer/argentinian_datasets/cammesa/ | ||
|
||
$python_cmd ./transformation_cammesa.py --filepath ./ --database_uri $DB_URI | ||
|
||
psql -h $CC_GLOBAL_API_DB_HOST \ | ||
-U $CC_GLOBAL_API_DB_USER \ | ||
-d $CC_GLOBAL_API_DB_NAME \ | ||
-f load_cammesa.sql | ||
|
||
popd | ||
|
||
# Import datasources | ||
|
||
pushd importer/datasource_seeder | ||
psql -h $CC_GLOBAL_API_DB_HOST \ | ||
-U $CC_GLOBAL_API_DB_USER \ | ||
-d $CC_GLOBAL_API_DB_NAME \ | ||
-f ./import_datasource_seeder.sql | ||
popd |
55 changes: 26 additions & 29 deletions
55
global-api/importer/argentinian_datasets/cammesa/load_cammesa.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,30 @@ | ||
-- Create a staging table | ||
CREATE TEMP TABLE IF NOT EXISTS region_code_staging (LIKE regionwide_emissions INCLUDING ALL); | ||
|
||
-- Clear the staging table | ||
TRUNCATE region_code_staging; | ||
|
||
-- Load the staging table from the downloaded file | ||
\copy region_code_staging (id,source_name,"GPC_refno",region_name,region_code,temporal_granularity,year,activity_name,activity_value,activity_units,gas_name,emission_factor_value,emission_factor_units,emissions_value,emissions_units) FROM 'processed_cammesa_AR.csv' WITH (FORMAT CSV, HEADER); | ||
-- The ID column is not unique based on the processed records, | ||
-- we have multiple acitivty records for single region_code, year, gas_name, GPC_refno | ||
-- rather than upsert we will just delete existing source data and insert fresh with generated id to make record unique | ||
-- the route for regions will need to be aggregated over region_code, year, gas_name, GPC_refno to get accurate emissions values | ||
DELETE FROM regionwide_emissions WHERE source_name = 'cammesa'; | ||
|
||
-- Update the main table with the staging table | ||
INSERT INTO regionwide_emissions (id,source_name,"GPC_refno",region_name,region_code,temporal_granularity,year,activity_name,activity_value,activity_units,gas_name,emission_factor_value,emission_factor_units,emissions_value,emissions_units) | ||
SELECT id,source_name,"GPC_refno",region_name,region_code,temporal_granularity,year,activity_name,activity_value,activity_units,gas_name,emission_factor_value,emission_factor_units,emissions_value,emissions_units | ||
FROM region_code_staging | ||
ON CONFLICT ON CONSTRAINT regionwide_emissions_pkey | ||
DO UPDATE SET | ||
id = excluded.id, | ||
source_name = excluded.source_name, | ||
"GPC_refno" = excluded."GPC_refno", | ||
region_name = excluded.region_name, | ||
region_code = excluded.region_code, | ||
temporal_granularity = excluded.temporal_granularity, | ||
year = excluded.year, | ||
activity_name = excluded.activity_name, | ||
activity_value = excluded.activity_value, | ||
activity_units = excluded.activity_units, | ||
gas_name = excluded.gas_name, | ||
emission_factor_value = excluded.emission_factor_value, | ||
emission_factor_units = excluded.emission_factor_units, | ||
emissions_value = excluded.emissions_value, | ||
emissions_units = excluded.emissions_units; | ||
INSERT INTO regionwide_emissions ( | ||
id,source_name,"GPC_refno",region_name,region_code,temporal_granularity,year,activity_name,activity_value, | ||
activity_units,gas_name,emission_factor_value,emission_factor_units,emissions_value,emissions_units | ||
) | ||
SELECT gen_random_uuid() as id, | ||
source_name, | ||
"GPC_refno", | ||
region_name, | ||
region_code, | ||
temporal_granularity, | ||
year, | ||
activity_name, | ||
activity_value, | ||
activity_units, | ||
gas_name, | ||
emission_factor_value, | ||
emission_factor_units, | ||
emissions_value, | ||
emissions_units | ||
FROM cammesa_region_emissions_staging; | ||
|
||
-- Drop the staging table | ||
DROP TABLE region_code_staging; | ||
DROP TABLE cammesa_region_emissions_staging; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters