Forráskód Böngészése

adapt CRF code for CRT

Johannes Gütschow 4 hónapja
szülő
commit
454af2024d

+ 28 - 12
src/unfccc_ghg_data/unfccc_crf_reader/unfccc_crf_reader_core.py

@@ -843,19 +843,35 @@ def get_info_from_crf_filename(
     filename = os.path.splitext(filename)[0]
     name_parts = filename.split("_")
     file_info = {}
-    file_info["party"] = name_parts[0]
-    file_info["submission_year"] = int(name_parts[1])
-    try:
-        file_info["data_year"] = int(name_parts[2])
-    except:  # noqa: E722
-        print(f"Data year string {name_parts[2]} " "could not be converted to int.")
-        file_info["data_year"] = name_parts[2]
-    file_info["date"] = name_parts[3]
-    # the last part (time code) is missing for CRT tables
-    if len(name_parts) > 4:  # noqa: PLR2004
-        file_info["extra"] = name_parts[4]
+    if len(name_parts) >= 4:  # noqa: PLR2004
+        # CRF file name convention (unfortunately also used for some CRT files)
+        file_info["party"] = name_parts[0]
+        file_info["submission_year"] = int(name_parts[1])
+        try:
+            file_info["data_year"] = int(name_parts[2])
+        except:  # noqa: E722
+            print(f"Data year string {name_parts[2]} " "could not be converted to int.")
+            file_info["data_year"] = name_parts[2]
+        file_info["date"] = name_parts[3]
+        # the last part (time code) is missing for CRT tables in CRF sile format
+        if len(name_parts) > 4:  # noqa: PLR2004
+            file_info["extra"] = name_parts[4]
+        else:
+            file_info["extra"] = ""
     else:
-        file_info["extra"] = ""
+        # not enough parts, we probably have a CRT file with different separator
+        name_parts = filename.split("-")
+        file_info["party"] = name_parts[0]
+        file_info["submission_year"] = int(name_parts[2])
+        file_info["version"] = int(name_parts[3])
+        try:
+            file_info["data_year"] = int(name_parts[4])
+        except:  # noqa: E722
+            print(f"Data year string {name_parts[4]} " "could not be converted to int.")
+            file_info["data_year"] = name_parts[4]
+        file_info["date"] = name_parts[5]
+        file_info["extra"] = name_parts[6]
+
     return file_info
 
 

+ 8 - 3
src/unfccc_ghg_data/unfccc_crf_reader/unfccc_crf_reader_devel.py

@@ -15,7 +15,7 @@ import pandas as pd
 import primap2 as pm2
 import xarray as xr
 
-from unfccc_ghg_data.helper import get_country_name, log_path
+from unfccc_ghg_data.helper import all_countries, get_country_name, log_path
 
 from . import crf_specifications as crf
 from .unfccc_crf_reader_core import (
@@ -82,8 +82,13 @@ def read_year_to_test_specs(  # noqa: PLR0912, PLR0915
 
     if country_code is not None:
         countries_to_read = [country_code]
-    else:
-        countries_to_read = all_crf_countries
+    else:  # noqa: PLR5501
+        if type == "CRF":
+            countries_to_read = all_crf_countries
+        elif type == "CRT":
+            countries_to_read = all_countries
+        else:
+            raise ValueError("Type must be CRF or CRT")  # noqa: TRY003
     for country_code in countries_to_read:
         # get country name
         country_name = get_country_name(country_code)