check_processed_dataset.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # ---
  2. # jupyter:
  3. # jupytext:
  4. # formats: ipynb,py:percent
  5. # text_representation:
  6. # extension: .py
  7. # format_name: percent
  8. # format_version: '1.3'
  9. # jupytext_version: 1.16.5
  10. # kernelspec:
  11. # display_name: Python 3 (ipykernel)
  12. # language: python
  13. # name: python3
  14. # ---
  15. # %%
  16. import climate_categories as cc
  17. import plotly.express as px # type: ignore
  18. import plotly.graph_objects as go # type: ignore
  19. import primap2 as pm2
  20. from faostat_data_primap.helper.paths import (
  21. extracted_data_path,
  22. )
  23. # import plotly.graph_objects as go # type: ignore
  24. # %%
  25. ds_path = (
  26. extracted_data_path / "v2024-11-14/FAOSTAT_Agrifood_system_emissions_v2024-11-14.nc"
  27. )
  28. ds = pm2.open_dataset(ds_path)
  29. # %%
  30. ds
  31. # %%
  32. entity = "CO2"
  33. filtered = (
  34. ds[entity]
  35. .pr.loc[
  36. {
  37. "area (ISO3)": ["DEU"],
  38. }
  39. ]
  40. .squeeze()
  41. )
  42. # %%
  43. filtered_pandas = filtered.to_dataframe().reset_index()
  44. # %%
  45. filtered_pandas
  46. # %%
  47. fig = px.area(
  48. filtered_pandas,
  49. x="time",
  50. y="CO2",
  51. color="category (IPCC2006_PRIMAP)",
  52. # title="category split"
  53. )
  54. fig.update_layout(
  55. xaxis=dict(rangeslider=dict(visible=True), type="date"),
  56. legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="left", x=0),
  57. margin=dict(l=0, r=0, t=0, b=0), # distance to next element
  58. )
  59. fig.show()
  60. # %%
  61. filtered_pandas["category (IPCC2006_PRIMAP)"].unique()
  62. # %%
  63. # %%
  64. categories_cc = cc.IPCC2006_PRIMAP
  65. # %%
  66. fig = go.Figure()
  67. three_a_cats = [
  68. "3.A",
  69. "3.A.1",
  70. "3.A.1.a",
  71. "3.A.1.a.i",
  72. "3.A.1.a.ii",
  73. "3.A.1.b",
  74. "3.A.1.c",
  75. "3.A.1.d",
  76. "3.A.1.e",
  77. "3.A.1.f",
  78. "3.A.1.g",
  79. "3.A.1.h",
  80. "3.A.1.j",
  81. "3.A.2",
  82. "3.A.2.a",
  83. "3.A.2.a.i",
  84. "3.A.2.a.ii",
  85. "3.A.2.b",
  86. "3.A.2.c",
  87. "3.A.2.d",
  88. "3.A.2.e",
  89. "3.A.2.f",
  90. "3.A.2.g",
  91. "3.A.2.h",
  92. "3.A.2.i",
  93. "3.A.2.j",
  94. ]
  95. three_b_cats = ["3.B.1", "3.B.2", "3.B.3"]
  96. three_c_cats = [
  97. "3.C",
  98. "3.C.1",
  99. "3.C.1.a",
  100. "3.C.1.b",
  101. "3.C.1.c",
  102. "3.C.4",
  103. "3.C.5",
  104. "3.C.6",
  105. "3.C.7",
  106. ]
  107. for cat in three_c_cats:
  108. filtered_pandas_cat = filtered_pandas.loc[
  109. filtered_pandas["category (IPCC2006_PRIMAP)"] == cat
  110. ]
  111. fig.add_trace(
  112. go.Scatter(
  113. x=list(filtered_pandas_cat["time"]),
  114. y=list(filtered_pandas_cat[entity]),
  115. # mode=mode,
  116. marker_symbol="cross",
  117. marker_size=10,
  118. name=f"{cat} {categories_cc["3.C.1.b"].title}",
  119. # line=line_layout,
  120. # visible=self.source_scenario_visible[source_scenario],
  121. hovertemplate="%{y:.2e} ",
  122. )
  123. )
  124. fig.update_layout(
  125. xaxis=dict(
  126. rangeslider=dict(visible=True, thickness=0.05),
  127. type="date",
  128. ),
  129. yaxis=dict(
  130. autorange=True,
  131. ),
  132. legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="left", x=0),
  133. margin=dict(l=0, r=0, t=0, b=20), # distance to next element
  134. autosize=True,
  135. hovermode="x",
  136. yaxis_title=str(ds[entity].data.units),
  137. )
  138. fig.show()
  139. # %%
  140. # %%