read_UNFCCC_submission.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # this script takes submission and country as input (from doit) and
  2. # runs the appropriate script to extract the submission data
  3. import datalad.api
  4. import argparse
  5. from get_submissions_info import get_possible_inputs
  6. from get_submissions_info import get_possible_outputs
  7. from UNFCCC_GHG_data.helper import root_path, get_code_file
  8. # Find the right function and possible input and output files and
  9. # read the data using datalad run.
  10. parser = argparse.ArgumentParser()
  11. parser.add_argument('--country', help='Country name or UNFCCC_GHG_data')
  12. parser.add_argument('--submission', help='Submission to read')
  13. args = parser.parse_args()
  14. country = args.country
  15. submission = args.submission
  16. print(f"Attempting to extract data for {submission} from {country}.")
  17. print("#"*80)
  18. print("")
  19. # get the correct script
  20. script_name = get_code_file(country, submission)
  21. if script_name is not None:
  22. print(f"Found UNFCCC_GHG_data file {script_name}")
  23. print("")
  24. # get possible input files
  25. input_files = get_possible_inputs(country, submission)
  26. if not input_files:
  27. print(f"No possible input files found for {country}, {submission}. "
  28. f"Something might be wrong here.")
  29. else:
  30. print(f"Found the following input_files:")
  31. for file in input_files:
  32. print(file)
  33. print("")
  34. # make input files absolute to avoid datalad confusions when
  35. # root directory is via symlink
  36. input_files = [root_path / file for file in input_files]
  37. # convert file's path to str
  38. input_files = [file.as_posix() for file in input_files]
  39. # get possible output files
  40. output_files = get_possible_outputs(country, submission)
  41. if not output_files:
  42. print(f"No possible output files found for {country}, {submission}. "
  43. f"This is either the first run or something is wrong.")
  44. else:
  45. print(f"Found the following output_files:")
  46. for file in output_files:
  47. print(file)
  48. print("")
  49. # convert file path's to str
  50. output_files = [file.as_posix() for file in output_files]
  51. print(f"Run the script using datalad run via the python api")
  52. datalad.api.run(
  53. cmd=f"./venv/bin/python3 {script_name.as_posix()}",
  54. dataset=root_path,
  55. message=f"Read data for {country}, {submission}.",
  56. inputs=input_files,
  57. outputs=output_files,
  58. dry_run=None,
  59. explicit=True,
  60. )
  61. else:
  62. # no UNFCCC_GHG_data found.
  63. print(f"No UNFCCC_GHG_data found to read {submission} from {country}")
  64. print(f"Use 'doit country_info --country={country} to get "
  65. f"a list of available submissions and datasets.")