Merge branch 'CEC' into 'master'

Update file Commodity Exchange Codes.xls

See merge request deemanone/materia!6
This commit is contained in:
Hendrik Dreesmann
2025-08-01 20:03:27 +02:00
6 changed files with 290 additions and 3 deletions

View File

@@ -24,7 +24,7 @@
"\n", "\n",
"data = \"../data/\"\n", "data = \"../data/\"\n",
"df = pd.read_csv(\"../data/psd_alldata.csv\", encoding=\"latin1\")\n", "df = pd.read_csv(\"../data/psd_alldata.csv\", encoding=\"latin1\")\n",
"\"\"\"\n", "\n",
"df.rename(columns={\n", "df.rename(columns={\n",
" 'Commodity_Description': 'commodity',\n", " 'Commodity_Description': 'commodity',\n",
" 'Country_Name': 'country',\n", " 'Country_Name': 'country',\n",
@@ -115,8 +115,7 @@
" 'Total Distribution', 'Ending Stocks', 'Net Supply',\n", " 'Total Distribution', 'Ending Stocks', 'Net Supply',\n",
" 'Supply-Demand Balance', 'Stock-to-Use Ratio (%)']]\n", " 'Supply-Demand Balance', 'Stock-to-Use Ratio (%)']]\n",
"combined_global.to_csv(\"global_summary_all.csv\", index=False)\n", "combined_global.to_csv(\"global_summary_all.csv\", index=False)\n",
"print(\"🌐 Combined global summary saved as 'global_summary_all.csv'\")\n", "print(\"🌐 Combined global summary saved as 'global_summary_all.csv'\")\n"
"\"\"\""
] ]
}, },
{ {

View File

@@ -0,0 +1,64 @@
/*
* Silver layer: Pivots the raw PSD data into a wide format,
* with key attributes ('Production', 'Imports', etc.) as columns.
* This is equivalent to step 2 of the Python script 03_Extraction.
*/
MODEL (
name transform.sqlmesh_materia.models.staging.stg_psd_alldata_1_filter_silver_layer,
kind INCREMENTAL_BY_TIME_RANGE (
time_column ingest_date
),
start '2006-08-01',
cron '@daily'
);
SELECT
commodity_code,
commodity_name,
country_code,
country_name,
ingest_date,
-- Replicate the Python script's pivot by using conditional aggregation
-- This creates a single row for each commodity-country-date combination
COALESCE(SUM(CASE WHEN attribute_name = 'Production' THEN value END), 0) AS Production,
COALESCE(SUM(CASE WHEN attribute_name = 'Imports' THEN value END), 0) AS Imports,
COALESCE(SUM(CASE WHEN attribute_name = 'Exports' THEN value END), 0) AS Exports,
COALESCE(SUM(CASE WHEN attribute_name = 'Total Distribution' THEN value END), 0) AS Total_Distribution,
COALESCE(SUM(CASE WHEN attribute_name = 'Ending Stocks' THEN value END), 0) AS Ending_Stocks,
COALESCE(SUM(CASE WHEN attribute_name = 'Beginning Stocks' THEN value END), 0) AS Beginning_Stocks,
COALESCE(SUM(CASE WHEN attribute_name = 'Total Supply' THEN value END), 0) AS Total_Supply,
COALESCE(SUM(CASE WHEN attribute_name = 'Domestic Consumption' THEN value END), 0) AS Domestic_Consumption,
COALESCE(SUM(CASE WHEN attribute_name = 'Domestic Demand' THEN value END), 0) AS Domestic_Demand,
COALESCE(SUM(CASE WHEN attribute_name = 'Food Use' THEN value END), 0) AS Food_Use,
COALESCE(SUM(CASE WHEN attribute_name = 'Industrial Use' THEN value END), 0) AS Industrial_Use,
COALESCE(SUM(CASE WHEN attribute_name = 'Seed Use' THEN value END), 0) AS Seed_Use,
COALESCE(SUM(CASE WHEN attribute_name = 'Waste' THEN value END), 0) AS Waste,
COALESCE(SUM(CASE WHEN attribute_name = 'Feed Use' THEN value END), 0) AS Feed_Use
FROM transform.sqlmesh_materia.models.staging.stg_psd_alldata_0
-- Filter for the specific attributes used in the pivot table for efficiency
WHERE attribute_name IN (
'Production',
'Imports',
'Exports',
'Total Distribution',
'Ending Stocks',
'Beginning Stocks',
'Total Supply',
'Domestic Consumption',
'Domestic Demand',
'Food Use',
'Industrial Use',
'Seed Use',
'Waste',
'Feed Use'
)
GROUP BY
commodity_code,
commodity_name,
country_code,
country_name,
ingest_date
ORDER BY
commodity_name,
country_name,
ingest_date;

View File

@@ -0,0 +1,110 @@
/*
* Gold layer: Calculates derived metrics like Net Supply, Trade Balance,
* and Stock-to-Use Ratio based on the pivoted silver layer data.
* This also includes the global aggregates, mimicking steps 3 and 4
* of the Python script 03_Extraction.
*/
MODEL (
name transform.sqlmesh_materia.models.staging.stg_psd_alldata_2_filter_gold_layer,
kind INCREMENTAL_BY_TIME_RANGE (
time_column ingest_date
),
start '2006-08-01',
cron '@daily'
);
-- CTE to calculate country-level derived metrics
WITH country_metrics AS (
SELECT
commodity_code,
commodity_name,
country_code,
country_name,
ingest_date,
Production,
Imports,
Exports,
Total_Distribution,
Ending_Stocks,
-- Derived metrics per country, mirroring Python script
(Production + Imports - Exports) AS Net_Supply,
(Exports - Imports) AS Trade_Balance,
(Production + Imports - Exports) - Total_Distribution AS Supply_Demand_Balance,
-- Handle division by zero for Stock-to-Use Ratio
(Ending_Stocks / NULLIF(Total_Distribution, 0)) * 100 AS Stock_to_Use_Ratio_pct,
-- Calculate Production YoY percentage change using a window function
(Production - LAG(Production, 1, 0) OVER (PARTITION BY commodity_code, country_code ORDER BY ingest_date)) / NULLIF(LAG(Production, 1, 0) OVER (PARTITION BY commodity_code, country_code ORDER BY ingest_date), 0) * 100 AS Production_YoY_pct
FROM transform.sqlmesh_materia.models.staging.stg_psd_alldata_1_filter_silver_layer
),
-- CTE to calculate global aggregates by summing up country-level data
global_aggregates AS (
SELECT
commodity_code,
commodity_name,
NULL::TEXT AS country_code, -- Use NULL for global aggregates
'Global' AS country_name,
ingest_date,
SUM(Production) AS Production,
SUM(Imports) AS Imports,
SUM(Exports) AS Exports,
SUM(Total_Distribution) AS Total_Distribution,
SUM(Ending_Stocks) AS Ending_Stocks
FROM transform.sqlmesh_materia.models.staging.stg_psd_alldata_1_filter_silver_layer
GROUP BY
commodity_code,
commodity_name,
ingest_date
),
-- CTE to calculate derived metrics for global aggregates
global_metrics AS (
SELECT
commodity_code,
commodity_name,
country_code,
country_name,
ingest_date,
Production,
Imports,
Exports,
Total_Distribution,
Ending_Stocks,
(Production + Imports - Exports) AS Net_Supply,
(Exports - Imports) AS Trade_Balance,
(Production + Imports - Exports) - Total_Distribution AS Supply_Demand_Balance,
(Ending_Stocks / NULLIF(Total_Distribution, 0)) * 100 AS Stock_to_Use_Ratio_pct,
(Production - LAG(Production, 1, 0) OVER (PARTITION BY commodity_code ORDER BY ingest_date)) / NULLIF(LAG(Production, 1, 0) OVER (PARTITION BY commodity_code ORDER BY ingest_date), 0) * 100 AS Production_YoY_pct
FROM global_aggregates
)
-- Combine country-level and global-level data into a single output
SELECT
hkey,
commodity_code,
commodity_name,
country_code,
country_name,
ingest_date,
Production,
Imports,
Exports,
Total_Distribution,
Ending_Stocks,
Net_Supply,
Trade_Balance,
Supply_Demand_Balance,
Stock_to_Use_Ratio_pct,
Production_YoY_pct
FROM (
SELECT
@GENERATE_SURROGATE_KEY(commodity_code, country_code, ingest_date) AS hkey,
*
FROM country_metrics
UNION ALL
SELECT
@GENERATE_SURROGATE_KEY(commodity_code, country_name, ingest_date) AS hkey,
*
FROM global_metrics
) AS combined_data
ORDER BY
commodity_name,
country_name,
ingest_date;

View File

@@ -0,0 +1,57 @@
commodity_name,exchange_code,exchange
Crude Oil WTI,CL,CME
Crude Oil Brent,BZ,ICE
Gasoline RBOB,RB,CME
Heating Oil,HO,CME
Natural Gas,NG,CME
Ethanol,CU,CME
Cocoa,CC,ICE
Cotton,CT,ICE
Orange Juice,FCOJ-A,ICE
Coffee,KC,ICE
Lumber,LBR,ICE
Sugar,SB,ICE
European Gas TTF,TTF,ICE
European Union Emissions Allowance,ECF,ICE
Gold,GC,CME
Silver,SI,CME
Platinum,PL,CME
Copper,HG,CME
Palladium,PA,CME
Live Cattle,LE,CME
Feeder Cattle,GF,CME
Lean Hogs,HE,CME
Corn,ZC,CME
Soybean Oil,ZL,CME
Soybean meal,ZM,CME
Oats,ZO,CME
Rough Rice,ZR,CME
Soybeans,ZS,CME
Wheat,ZW,CME
Canola,RS,ICE
Rebar,RB,SHFE
Hot-Rolled Coil,HC,SHFE
Nickel,NI,SHFE
Tin,SN,SHFE
Aluminum,AL,SHFE
Zinc,ZN,SHFE
Natural Rubber,RU,SHFE
Bitumen,BU,SHFE
Iron Ore,I,DCE
Palm Oil,P,DCE
Eggs,JD,DCE
Coking Coal,JM,DCE
Polyvinyl Chloride (PVC),V,DCE
White Sugar,SR,ZCE
Cotton,CF,ZCE
Apple,AP,ZCE
PTA,TA,ZCE
Methanol,MA,ZCE
LME Aluminum,AH,LME
LME Copper,CA,LME
LME Lead,PB,LME
LME Nickel,NI,LME
LME Tin,SN,LME
LME Zinc,ZS,LME
Iron Ore,TIO,SGX
Rubber,TSR,SGX
1 commodity_name exchange_code exchange
2 Crude Oil WTI CL CME
3 Crude Oil Brent BZ ICE
4 Gasoline RBOB RB CME
5 Heating Oil HO CME
6 Natural Gas NG CME
7 Ethanol CU CME
8 Cocoa CC ICE
9 Cotton CT ICE
10 Orange Juice FCOJ-A ICE
11 Coffee KC ICE
12 Lumber LBR ICE
13 Sugar SB ICE
14 European Gas TTF TTF ICE
15 European Union Emissions Allowance ECF ICE
16 Gold GC CME
17 Silver SI CME
18 Platinum PL CME
19 Copper HG CME
20 Palladium PA CME
21 Live Cattle LE CME
22 Feeder Cattle GF CME
23 Lean Hogs HE CME
24 Corn ZC CME
25 Soybean Oil ZL CME
26 Soybean meal ZM CME
27 Oats ZO CME
28 Rough Rice ZR CME
29 Soybeans ZS CME
30 Wheat ZW CME
31 Canola RS ICE
32 Rebar RB SHFE
33 Hot-Rolled Coil HC SHFE
34 Nickel NI SHFE
35 Tin SN SHFE
36 Aluminum AL SHFE
37 Zinc ZN SHFE
38 Natural Rubber RU SHFE
39 Bitumen BU SHFE
40 Iron Ore I DCE
41 Palm Oil P DCE
42 Eggs JD DCE
43 Coking Coal JM DCE
44 Polyvinyl Chloride (PVC) V DCE
45 White Sugar SR ZCE
46 Cotton CF ZCE
47 Apple AP ZCE
48 PTA TA ZCE
49 Methanol MA ZCE
50 LME Aluminum AH LME
51 LME Copper CA LME
52 LME Lead PB LME
53 LME Nickel NI LME
54 LME Tin SN LME
55 LME Zinc ZS LME
56 Iron Ore TIO SGX
57 Rubber TSR SGX

View File

@@ -0,0 +1,57 @@
commodity_name,exchange_code,exchange,commodity_code
Crude Oil WTI,CL,CME,NA
Crude Oil Brent,BZ,ICE,NA
Gasoline RBOB,RB,CME,NA
Heating Oil,HO,CME,NA
Natural Gas,NG,CME,NA
Ethanol,CU,CME,NA
Cocoa,CC,ICE,NA
Cotton,CT,ICE,2631000
Orange Juice,FCOJ-A,ICE,0585100
Coffee,KC,ICE,0711100
Lumber,LBR,ICE,NA
Sugar,SB,ICE,0612000
European Gas TTF,TTF,ICE,NA
European Union Emissions Allowance,ECF,ICE,NA
Gold,GC,CME,NA
Silver,SI,CME,NA
Platinum,PL,CME,NA
Copper,HG,CME,NA
Palladium,PA,CME,NA
Live Cattle,LE,CME,0011000
Feeder Cattle,GF,CME,0011000
Lean Hogs,HE,CME,NA
Corn,ZC,CME,0440000
Soybean Oil,ZL,CME,4232000
Soybean meal,ZM,CME,0813100
Oats,ZO,CME,0452000
Rough Rice,ZR,CME,0422110
Soybeans,ZS,CME,NA
Wheat,ZW,CME,0410000
Canola,RS,ICE,2226000
Rebar,RB,SHFE,NA
Hot-Rolled Coil,HC,SHFE,NA
Nickel,NI,SHFE,NA
Tin,SN,SHFE,NA
Aluminum,AL,SHFE,NA
Zinc,ZN,SHFE,NA
Natural Rubber,RU,SHFE,NA
Bitumen,BU,SHFE,NA
Iron Ore,I,DCE,NA
Palm Oil,P,DCE,4243000
Eggs,JD,DCE,NA
Coking Coal,JM,DCE,NA
Polyvinyl Chloride (PVC),V,DCE,NA
White Sugar,SR,ZCE,0612000
Cotton,CF,ZCE,2631000
Apple,AP,ZCE,0574000
PTA,TA,ZCE,NA
Methanol,MA,ZCE,NA
LME Aluminum,AH,LME,NA
LME Copper,CA,LME,NA
LME Lead,PB,LME,NA
LME Nickel,NI,LME,NA
LME Tin,SN,LME,NA
LME Zinc,ZS,LME,NA
Iron Ore,TIO,SGX,NA
Rubber,TSR,SGX,NA
1 commodity_name exchange_code exchange commodity_code
2 Crude Oil WTI CL CME NA
3 Crude Oil Brent BZ ICE NA
4 Gasoline RBOB RB CME NA
5 Heating Oil HO CME NA
6 Natural Gas NG CME NA
7 Ethanol CU CME NA
8 Cocoa CC ICE NA
9 Cotton CT ICE 2631000
10 Orange Juice FCOJ-A ICE 0585100
11 Coffee KC ICE 0711100
12 Lumber LBR ICE NA
13 Sugar SB ICE 0612000
14 European Gas TTF TTF ICE NA
15 European Union Emissions Allowance ECF ICE NA
16 Gold GC CME NA
17 Silver SI CME NA
18 Platinum PL CME NA
19 Copper HG CME NA
20 Palladium PA CME NA
21 Live Cattle LE CME 0011000
22 Feeder Cattle GF CME 0011000
23 Lean Hogs HE CME NA
24 Corn ZC CME 0440000
25 Soybean Oil ZL CME 4232000
26 Soybean meal ZM CME 0813100
27 Oats ZO CME 0452000
28 Rough Rice ZR CME 0422110
29 Soybeans ZS CME NA
30 Wheat ZW CME 0410000
31 Canola RS ICE 2226000
32 Rebar RB SHFE NA
33 Hot-Rolled Coil HC SHFE NA
34 Nickel NI SHFE NA
35 Tin SN SHFE NA
36 Aluminum AL SHFE NA
37 Zinc ZN SHFE NA
38 Natural Rubber RU SHFE NA
39 Bitumen BU SHFE NA
40 Iron Ore I DCE NA
41 Palm Oil P DCE 4243000
42 Eggs JD DCE NA
43 Coking Coal JM DCE NA
44 Polyvinyl Chloride (PVC) V DCE NA
45 White Sugar SR ZCE 0612000
46 Cotton CF ZCE 2631000
47 Apple AP ZCE 0574000
48 PTA TA ZCE NA
49 Methanol MA ZCE NA
50 LME Aluminum AH LME NA
51 LME Copper CA LME NA
52 LME Lead PB LME NA
53 LME Nickel NI LME NA
54 LME Tin SN LME NA
55 LME Zinc ZS LME NA
56 Iron Ore TIO SGX NA
57 Rubber TSR SGX NA