Browse Source

work on doit setup and country info display

Johannes Gütschow 3 years ago
parent
commit
ef3ffa1e94

+ 22 - 0
code/UNFCCC_reader/country_info.py

@@ -0,0 +1,22 @@
+# this script takes country as input (from doit) and
+# runs displays available submissions and datasets
+
+import argparse
+from get_submissions_info import get_country_submissions
+from get_submissions_info import get_country_datasets
+
+# Find the right function and possible input and output files and
+# read the data using datalad run.
+parser = argparse.ArgumentParser()
+parser.add_argument('--country', help='Country name or code')
+args = parser.parse_args()
+country = args.country
+
+# print available submissions
+print("="*15 + " Available submissions " + "="*15)
+get_country_submissions(country, True)
+print("")
+
+#print available datasets
+print("="*15 + " Available datasets " + "="*15)
+get_country_datasets(country, True)

+ 27 - 19
code/UNFCCC_reader/get_submissions_info.py

@@ -45,33 +45,39 @@ def get_country_submissions(
         print(f"Country name {country_name} maps to ISO code {country_code}")
 
     country_submissions = {}
+    if print_sub:
+        print(f"#" * 80)
+        print(f"The following submissions are available for {country_name}")
     for item in data_folder.iterdir():
         if item.is_dir():
             if print_sub:
                 print("")
-                print("#" * 80)
+                print("-" * 80)
                 print(f"Data folder {item.name}")
+                print("-" * 80)
             with open(item / "folder_mapping.json", "r") as mapping_file:
                 folder_mapping = json.load(mapping_file)
-            country_folders = folder_mapping[country_code]
-            if isinstance(country_folders, str):
-                # only one folder
-                country_folders = [country_folders]
+            if country_code in folder_mapping:
+                country_folders = folder_mapping[country_code]
+                if isinstance(country_folders, str):
+                    # only one folder
+                    country_folders = [country_folders]
 
-            submission_folders = []
-            for country_folder in country_folders:
-                current_folder = item / country_folder
-                if print_sub:
-                    print("-" * 80)
-                    print(f"Submissions in folder {country_folder}:")
+                submission_folders = []
+                for country_folder in country_folders:
+                    current_folder = item / country_folder
+                    if print_sub:
+                        print(f"Submissions in folder {country_folder}:")
 
-                for submission_folder in current_folder.iterdir():
-                    if submission_folder.is_dir():
-                        if print_sub:
-                            print(submission_folder.name)
-                        submission_folders.append(submission_folder.name)
+                    for submission_folder in current_folder.iterdir():
+                        if submission_folder.is_dir():
+                            if print_sub:
+                                print(submission_folder.name)
+                            submission_folders.append(submission_folder.name)
 
-            country_submissions[item.name] = submission_folders
+                country_submissions[item.name] = submission_folders
+            else:
+                print(f"No submissions available for {country_name}.")
 
     return country_submissions
 
@@ -455,8 +461,10 @@ def get_code_file(
                         print(f"Found code file {file.relative_to(rootpath)}")
                 code_file_path = file
 
-    return code_file_path.relative_to(rootpath)
-
+    if code_file_path is not None:
+        return code_file_path.relative_to(rootpath)
+    else:
+        return None
 
 def create_folder_mapping(
         folder: str,

+ 17 - 12
code/UNFCCC_reader/read_UNFCCC_submission.py

@@ -1,23 +1,26 @@
-# this script takes submission and country as input (from make) and
+# this script takes submission and country as input (from doit) and
 # runs the appropriate script to extract the submission data
 
 import sys
 import datalad.api
 from pathlib import Path
+import argparse
 from get_submissions_info import get_code_file
 from get_submissions_info import get_possible_inputs
 from get_submissions_info import get_possible_outputs
 
 
-if len(sys.argv) > 3:
-    raise TypeError('Too many arguments given. '
-                    'Need exactly two arguments (country, submission)')
-elif len(sys.argv) < 3:
-    raise TypeError('Too few arguments given. '
-                    'Need exactly two arguments (country, submission)')
 
-country = sys.argv[1]
-submission = sys.argv[2]
+# Find the right function and possible input and output files and
+# read the data using datalad run.
+parser = argparse.ArgumentParser()
+parser.add_argument('--country', help='Country name or code')
+parser.add_argument('--submission', help='Submission to read')
+
+args = parser.parse_args()
+
+country = args.country
+submission = args.submission
 
 codepath = Path(__file__).parent
 rootpath = codepath / ".." / ".."
@@ -29,7 +32,7 @@ print("")
 
 # get the correct script
 script_name = get_code_file(country, submission)
-if script_name:
+if script_name is not None:
     print(f"Found code file {script_name}")
     print("")
 
@@ -69,10 +72,12 @@ if script_name:
         message=f"Read data for {country}, {submission}.",
         inputs=input_files,
         outputs=output_files,
-        dry_run=None
+        dry_run=None,
+        explicit=True,
     )
 else:
     # no code found.
     print(f"No code found to read {submission} from {country}")
-    # TODO write info on available submissions and data
+    print(f"Use 'doit country_info --country={country} to get "
+          f"a list of available submissions and datasets.")
 

+ 18 - 1
dodo.py

@@ -1,4 +1,5 @@
 # define tasks for UNFCCC data repository
+from doit import get_var
 
 # Tasks for getting submissions and downloading them
 def task_update_bur():
@@ -61,10 +62,16 @@ def task_download_ndc():
 # read UNFCCC submissions.
 # datalad run is called from within the read_UNFCCC_submission.py script
 # add parameters and pass them to script
+read_config = {
+    "country": get_var('country', None),
+    "submission": get_var('submission', None),
+}
+
 def task_read_unfccc_submission():
     """ Read submission for a country (if code exists) """
     return {
-        'actions': ['./venv/bin/python code/UNFCCC_downloader/read_UNFCCC_submission.py'],
+        'actions': [f"./venv/bin/python code/UNFCCC_reader/read_UNFCCC_submission.py "
+                    f"--country={read_config['country']} --submission={read_config['submission']}"],
         'verbosity': 2,
         'params': [{'name': 'country',
                     'short': 'c',
@@ -81,4 +88,14 @@ def task_read_unfccc_submission():
                     'type': str,
                     },
                    ],
+    }
+
+
+def task_country_info():
+    """ Print information on submissions and datasets
+    available for given country"""
+    return {
+        'actions': [f"./venv/bin/python code/UNFCCC_reader/country_info.py "
+                    f"--country={read_config['country']}"],
+        'verbosity': 2,
     }