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.
 
 

139 lines
4.2 KiB

  1. #!/usr/bin/env python3
  2. """
  3. Splits a key file that contains multiple experiments to create single-experiment
  4. key files.
  5. Usage: keyfile_split.py keyfile
  6. Options:
  7. keyfile : str Path to the key file to be imported
  8. Version: 0.0
  9. Date: Feb 2019
  10. Written by Benito Marcote (marcote@jive.eu)
  11. """
  12. import sys
  13. import argparse
  14. import numpy as np
  15. # from pyrap import tables as pt
  16. help_keyfile = 'keyfile to be read'
  17. help_v = 'Print more detailed log messages'
  18. help_experiments = """In case you want to explicitely especify the experiments to process,
  19. which can be a subset of all experiments available in the keyfile.
  20. """
  21. usage = "%(prog)s [-h] <keyfile>"
  22. description="""Splits a key file that contains multiple experiments to create single-experiment
  23. key files. By default the new files to be created will be named as keyfile-##.key
  24. The following things must be properly set up in the keyfile:
  25. - A line as:
  26. expt = 'e-EVN: exp1, exp2, ...' where exp1, exp2, ... are the experiment names.
  27. or
  28. expt = 'exp1, exp2, ...' where exp1, exp2, ... are the experiment names.
  29. - Inside srccat, sources from different experiments must be separated by a comment line as:
  30. ! EXPNAME {following words are accepted after a space character}
  31. """
  32. parser = argparse.ArgumentParser(description=description, prog='keyfile_split.py', usage=usage)
  33. parser.add_argument('keyfile', type=str, help=help_keyfile)
  34. parser.add_argument('--version', action='version', version='%(prog)s 0.0')
  35. parser.add_argument("-v", "--verbose", default=True, action="store_false" , help=help_v)
  36. parser.add_argument('-e', '--experiments', default=None,type=str, help=help_experiments)
  37. arguments = parser.parse_args()
  38. verbose = arguments.verbose
  39. exps = {} # Names with all experiments included in the keyfile
  40. class Keyfile:
  41. def __init__(self, expcode):
  42. self.expcode = expcode
  43. self.lines = []
  44. def write_keyfile(self, outputname):
  45. # Update the expcode in the lines, including such line after the version one.
  46. f = open(outputname, 'w')
  47. # f.writelines()
  48. # Required functions
  49. def get_experiments(exptline):
  50. """Receives a line like: expt = 'e-EVN: exp1, exp2, ...' or expt = 'exp1, exp2, ...'
  51. and returns a list with all experiments (e.g. exp1, exp2, ...).
  52. """
  53. exptline = exptline.strip().replace("'", '')
  54. if ':' in exptline:
  55. exptline = exptline.split(':')[1]
  56. else:
  57. exptline = exptline.split('=')[1]
  58. return [i.strip() for i in exptline.split(',')]
  59. class Checks():
  60. self.experiments = False
  61. # self.
  62. with open(arguments.keyfile, 'r').readlines() as key:
  63. in_srccat = False
  64. for i,aline in enumerate(key):
  65. if ('expt = ' in aline) or ('expt=' in aline):
  66. experiments = get_experiments(exptline)
  67. # Create a Keyfile for each experiment, saving all previous lines (up to now)
  68. if arguments.experiments is not None:
  69. for an_exp in arguments.experiments:
  70. assert an_exp in experiments
  71. exps[an_exp] = Keyfile(an_exp)
  72. else:
  73. for an_exp in experiments:
  74. exps[an_exp] = Keyfile(an_exp)
  75. for an_exps in exps:
  76. for a_past_line in key[:i]:
  77. exps[an_exps].lines.append(a_past_line)
  78. # Update checks?
  79. if 'srccat' in aline:
  80. in_srccat = True
  81. for an_exps in exps:
  82. exps[an_exps].lines.append(aline)
  83. inside_exp = None
  84. for srcline in key[i+1:]:
  85. if 'endcat' in srcline:
  86. in_srccat = False
  87. break
  88. if '! ' in srcline: # do a better comparison for the names !!!!!!!!!!!!!!
  89. inside_exp = srcline.replace('!','').strip().split(' ')[0]
  90. else:
  91. if inside_exp is None:
  92. for an_exps in exps:
  93. exps[an_exps].lines.append(srcline)
  94. else:
  95. exps[inside_exp].lines.append(srcline)
  96. if in_srccat:
  97. continue
  98. else:
  99. for an_exps in exps:
  100. exps[an_exps].lines.append(srcline)