Просмотр исходного кода

Worked on error collection for bulk CRF/T reading

Johannes Gütschow 2 месяцев назад
Родитель
Сommit
5df3416056

+ 47 - 46
src/unfccc_ghg_data/unfccc_crf_reader/unfccc_crf_reader_core.py

@@ -279,59 +279,60 @@ def read_crf_table(  # noqa: PLR0913, PLR0912, PLR0915
         country_codes = [country_codes]
 
     # get file names and locations
-    input_files = get_crf_files(
-        country_codes=country_codes,
-        submission_year=submission_year,
-        data_year=data_year,
-        date_or_version=date_or_version,
-        folder=folder,
-        submission_type=submission_type,
-    )
-    # nasty fix for cases where exporting ran overnight and not all files have
-    # the same date_or_version. This is only applied for CRF as for CRT we use the
-    # version as main identifier
-    if submission_type == "CRF":
-        if (date_or_version is not None) and (len(country_codes) == 1):
-            if isinstance(data_year, list):
-                expected_files = len(data_year)
-            elif isinstance(data_year, int):
-                expected_files = 1
-            else:
-                expected_files = submission_year - 1990 - 1
-            if len(input_files) < expected_files:
-                print(
-                    f"Found only {len(input_files)} input files for {country_codes}. "
-                    f"Expected {expected_files}."
-                )
-                print(
-                    "Possibly exporting run overnight and some files have the previous "
-                    "day as date."
-                )
-                date_datetime = datetime.strptime(date_or_version, "%d%m%Y")
-                date_datetime = date_datetime - timedelta(days=1)
-                prv_date = date_datetime.strftime("%d%m%Y")
-                more_input_files = get_crf_files(
-                    country_codes=country_codes,
-                    submission_year=submission_year,
-                    data_year=data_year,
-                    date_or_version=prv_date,
-                    folder=folder,
-                    submission_type=submission_type,
-                )
-                if len(more_input_files) > 0:
-                    print(f"Found {len(more_input_files)} additional input files.")
-                    input_files = input_files + more_input_files
+    try:
+        input_files = get_crf_files(
+            country_codes=country_codes,
+            submission_year=submission_year,
+            data_year=data_year,
+            date_or_version=date_or_version,
+            folder=folder,
+            submission_type=submission_type,
+        )
+        # nasty fix for cases where exporting ran overnight and not all files have
+        # the same date_or_version. This is only applied for CRF as for CRT we use the
+        # version as main identifier
+        if submission_type == "CRF":
+            if (date_or_version is not None) and (len(country_codes) == 1):
+                if isinstance(data_year, list):
+                    expected_files = len(data_year)
+                elif isinstance(data_year, int):
+                    expected_files = 1
                 else:
-                    print("Found no additional input files")
-
-    if not input_files:
+                    expected_files = submission_year - 1990 - 1
+                if len(input_files) < expected_files:
+                    print(
+                        f"Found only {len(input_files)} input files for "
+                        f"{country_codes}. "
+                        f"Expected {expected_files}."
+                    )
+                    print(
+                        "Possibly exporting run overnight and some files have the "
+                        "previous day as date."
+                    )
+                    date_datetime = datetime.strptime(date_or_version, "%d%m%Y")
+                    date_datetime = date_datetime - timedelta(days=1)
+                    prv_date = date_datetime.strftime("%d%m%Y")
+                    more_input_files = get_crf_files(
+                        country_codes=country_codes,
+                        submission_year=submission_year,
+                        data_year=data_year,
+                        date_or_version=prv_date,
+                        folder=folder,
+                        submission_type=submission_type,
+                    )
+                    if len(more_input_files) > 0:
+                        print(f"Found {len(more_input_files)} additional input files.")
+                        input_files = input_files + more_input_files
+                    else:
+                        print("Found no additional input files")
+    except Exception as ex:
         raise NoCRFFilesError(  # noqa: TRY003
             f"No files found for {country_codes}, "
             f"submission_year={submission_year}, "
             f"data_year={data_year}, "
             f"date/version={date_or_version}, "
             f"folder={folder}."
-        )
+        ) from ex
 
     # get specification
     # if we only have a single country check if we might have a country specific

+ 11 - 6
src/unfccc_ghg_data/unfccc_crf_reader/unfccc_crf_reader_prod.py

@@ -397,12 +397,17 @@ def read_crf_for_country_datalad(
     if type not in ["CRF", "CRT"]:
         raise ValueError("Type must be CRF or CRT")  # noqa: TRY003
     # get all the info for the country
-    country_info = get_input_and_output_files_for_country(
-        country_code,
-        submission_year=submission_year,
-        verbose=True,
-        submission_type=type,
-    )
+    try:
+        country_info = get_input_and_output_files_for_country(
+            country_code,
+            submission_year=submission_year,
+            verbose=True,
+            submission_type=type,
+        )
+    except Exception as ex:
+        raise NoCRFFilesError(  # noqa: TRY003
+            "Problem determining input and output files."
+        ) from ex
 
     print(f"Attempting to read data for {type}{submission_year} from {country_code}.")
     print("#" * 80)