Collection of scripts and small programs used by the EVN Support Scientists at JIVE during the regular data processing of EVN observations.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
4.5 KiB

  1. #!/usr/bin/env python3
  2. """
  3. Creates the {exp}.pipelet.
  4. Given the default template, customizes it to include the basic data from the given experiment.
  5. Version: 1.0
  6. Date: April 2019
  7. Author: Benito Marcote (
  8. """
  9. import os
  10. import sys
  11. import glob
  12. import argparse
  13. from datetime import datetime as dt
  14. __version__ = 1.0
  15. # The .comment file template is located in the same directory as this script. Or it should be.
  16. template_pipelet_file = os.path.dirname(os.path.abspath(__file__)) + '/template.pipelet'
  17. help_str = """Creates a .pipelet file in the current directory.
  18. This letter is the content that must be sent to the PI after pipelining an experiment.
  19. It contains information about the pipeline output and the credentials to access the data.
  20. It takes the credentials from the username_password.auth file that should be placed in the current directory
  21. (otherwise specify its file, or the username and password as parameters).
  22. The user must provide the following information:
  23. - The experiment name (case insensitive).
  24. - Who are you (the Support Scientist). Type your surname.
  25. """
  26. parser = argparse.ArgumentParser(description=help_str, prog='')
  27. parser.add_argument('-v', '--version', action='version', version='%(prog)s {}'.format(__version__))
  28. parser.add_argument('-o', '--output', type=str, default='.', help='Output directory where the file {experiment}.pipelet will be store (by default the current directory)')
  29. parser.add_argument('-c', '--credentials', type=str, default=None, help='Auth file containing the username and password in its name (required if *.auth is not in current directory).')
  30. parser.add_argument('-u', '--username', type=str, default=None, help='Username to access the data (required if no credential file exists)')
  31. parser.add_argument('-p', '--password', type=str, default=None, help='Password to access the data (required if no credential file exists)')
  32. parser.add_argument('experiment', type=str, default=None, help='Experiment name.')
  33. parser.add_argument('jss', type=str, default='', help='JIVE Support Scientist doing the post-processing (your surname).')
  34. args = parser.parse_args()
  35. jss = {'marcote': 'Benito Marcote', 'immer': 'Katharina Immer',
  36. 'nair': 'Dhanya Nair', 'bayandina': 'Olga Bayandina', 'campbell': 'Bob Campbell',
  37. 'paragi': 'Zsolt Paragi', 'rmc': 'Bob Campbell', 'zparagi': 'Zsolt Paragi'}
  38. def get_credentials_from_filename(thefile):
  39. """Assumes that the file name is made of the form username_password[.auth]
  40. It thus can only contain one '_' character, which separates both sides.
  41. Returns the username, and password.
  42. """
  43. thefile = thefile.split('/')[-1]
  44. assert thefile.count('_') == 1
  45. return thefile.split('.')[0].split('_')
  46. # One of the following conditions must be true
  47. if args.experiment.upper()[0] == 'N' or args.experiment.upper()[0] == 'F':
  48. print('This is an NME or test. No credentials will be set.')
  49. username, password = '', ''
  50. elif args.credentials is not None:
  51. username, password = get_credentials_from_filename(args.credentials)
  52. elif (args.username is not None) and (args.password is not None):
  53. username, password = args.username, args.password
  54. elif ((args.username is not None) and (args.password is None)) or ((args.username is None) and (args.password is not None)):
  55. raise AttributeError('Both username and password must be passed, or none of them.')
  56. else:
  57. credential_file = glob.glob('*.auth')
  58. if len(credential_file) == 0:
  59. raise AttributeError('No credential *.auth file found and not enough paramenters passed')
  60. elif len(credential_file) > 1:
  61. raise AttributeError('Multiple *auth files found. Please specify the one to be used.')
  62. username, password = get_credentials_from_filename(credential_file[0])
  63. with open(template_pipelet_file, 'r') as template:
  64. full_text =
  65. full_text = full_text.format(expname=args.experiment.upper(),
  66. delimiter='-'*(41+len(args.experiment)),
  67. username=username, password=password, supsci=jss[args.jss])
  68. pipelet_file = open('{}/{}.pipelet'.format(
  69. args.output if args.output[-1] != '/' else args.output[:-1],
  70. args.experiment.lower()), 'w')
  71. pipelet_file.write(full_text)
  72. pipelet_file.close()
  73. if args.output == '.':
  74. print('\nFile {0}.pipelet created successfully.'.format(args.experiment.lower()))
  75. else:
  76. print('\nFile {0}.pipelet created successfully in {1}/.'.format(args.experiment.lower(), args.output if args.output[-1] != '/' else args.output[:-1]))