Example #1
0
def lwpla(uvdata, outfile, inv=None, plv=1,docol=0,plcolors=False):
	HF.delete_file(aips_out+outfile)
	#Must set indata, outfile
  #assert (indata != None, outfile != None)

	lwpla = AIPSTask('lwpla')
	lwpla.indata = uvdata
	lwpla.outfile = aips_out+outfile
	lwpla.lpen = 1.
	lwpla.dparm[6] = 4
	lwpla.dparm[8] = 9
	lwpla.docolor = docol
	if docol==1:
		i=1
		for plc in plcolors:
			lwpla.plcolors[i]=[None]+plc
			i+=1
	lwpla.plver = plv
	if (inv == None or inv == 0):
		inv = uvdata.table_highver('AIPS PL')
	lwpla.inver = inv
	HF.print_inp_to_log(lwpla,'LWPLA')
	try:
		lwpla()
	except:
		sys.stdout.write('LWPLA did encounter an error.\n PL files will remain.\n Change input parameters and try again.')
	else:
		sys.stdout.write('PL files written to plotfile {0}.\n PL files will be deleted now.\n'.format(outfile))
		logger.info('PL files written to plotfile %s.\n PL files will be deleted now.\n',outfile)
		extd(uvdata,ine='PL',inv=-1)
Example #2
0
def _convert_to_yuv_and_delete(output_directory, file_name, pattern):
  """Converts a PNG file to a YUV file and deletes the PNG file.

  Args:
    output_directory(string): The output directory for the YUV file.
    file_name(string): The PNG file name.
    pattern(string): The file pattern of the PNG/YUV file. The PNG/YUV files are
      named patternxx..x.png/yuv, where xx..x are digits starting from 00..0.
  Return:
    (bool): True upon successful conversion, false otherwise.
  """
  # Pattern should be in file name
  if not pattern in file_name:
    return False
  pattern_position = file_name.rfind(pattern)

  # Strip the path to the PNG file and replace the png extension with yuv
  yuv_file_name = file_name[pattern_position:-3] + 'yuv'
  yuv_file_name = os.path.join(output_directory, yuv_file_name)

  command = ['ffmpeg', '-i', '%s' % (file_name), '-pix_fmt', 'yuv420p',
             '%s' % (yuv_file_name)]
  try:
    helper_functions.run_shell_command(
        command, msg=('Error during PNG to YUV conversion of %s' %
                       file_name));
    helper_functions.delete_file(file_name)
  except helper_functions.HelperError, err:
    print err
    return Flase
Example #3
0
def _convert_to_yuv_and_delete(output_directory, file_name, pattern):
    """Converts a PNG file to a YUV file and deletes the PNG file.

  Args:
    output_directory(string): The output directory for the YUV file.
    file_name(string): The PNG file name.
    pattern(string): The file pattern of the PNG/YUV file. The PNG/YUV files are
      named patternxx..x.png/yuv, where xx..x are digits starting from 00..0.
  Return:
    (bool): True upon successful conversion, false otherwise.
  """
    # Pattern should be in file name
    if not pattern in file_name:
        return False
    pattern_position = file_name.rfind(pattern)

    # Strip the path to the PNG file and replace the png extension with yuv
    yuv_file_name = file_name[pattern_position:-3] + 'yuv'
    yuv_file_name = os.path.join(output_directory, yuv_file_name)

    command = [
        'ffmpeg', '-i',
        '%s' % (file_name), '-pix_fmt', 'yuv420p',
        '%s' % (yuv_file_name)
    ]
    try:
        helper_functions.run_shell_command(
            command,
            msg=('Error during PNG to YUV conversion of %s' % file_name))
        helper_functions.delete_file(file_name)
    except helper_functions.HelperError, err:
        print err
        return Flase
Example #4
0
def _generate_stats_file(stats_file_name, input_directory='.'):
  """Generate statistics file.

  The function generates a statistics file. The contents of the file are in the
  format <frame_name> <barcode>, where frame name is the name of every frame
  (effectively the frame number) and barcode is the decoded barcode. The frames
  and the helper .txt files are removed after they have been used.
  """
  file_prefix = os.path.join(input_directory, 'frame_')
  stats_file = open(stats_file_name, 'w')

  for i in range(1, _count_frames_in(input_directory=input_directory) + 1):
    frame_number = helper_functions.zero_pad(i)
    barcode_file_name = file_prefix + frame_number + '.txt'
    png_frame = file_prefix + frame_number + '.png'
    entry_frame_number = helper_functions.zero_pad(i-1)
    entry = 'frame_' + entry_frame_number + ' '

    if os.path.isfile(barcode_file_name):
      barcode = _read_barcode_from_text_file(barcode_file_name)
      helper_functions.delete_file(barcode_file_name)

      if _check_barcode(barcode):
        entry += (helper_functions.zero_pad(int(barcode[0:11])) + '\n')
      else:
        entry += 'Barcode error\n'  # Barcode is wrongly detected.
    else:  # Barcode file doesn't exist.
      entry += 'Barcode error\n'

    stats_file.write(entry)
    helper_functions.delete_file(png_frame)

  stats_file.close()
Example #5
0
def prtan(uvdata):
	outprint = aips_out+uvdata.name+'_prtan.txt'
	HF.delete_file(outprint)
	prtan = AIPSTask('prtan')
	prtan.indata = uvdata
	prtan.docrt = -1
	prtan.outprint = outprint
	prtan()
Example #6
0
def imhead(uvdata):
	header = uvdata.header
	tables = uvdata.tables
	outfile = aips_out+uvdata.name+'_header.txt'
	HF.delete_file(outfile)
	header_file = open(aips_out+uvdata.name+'_header.txt','a')
	header_file.write('Header for uvdata {0}\n'.format(uvdata.name))
	for key in header.keys():
		header_file.write('{0}: {1}\n'.format(key,header[key]))
	for row in tables:
		header_file.write('{0}\n'.format(row))
	header_file.close()
Example #7
0
def _main():
    """The main function.

  A simple invocation will be:
  ./tools/barcode_tools/barcode_encoder.py --barcode_height=32
  --base_frame_width=352 --base_frame_height=288
  --base_yuv=<path_and_name_of_base_file>
  --output_yuv=<path and name_of_output_file>
  """
    options = _parse_args()
    # The barcodes with will be different than the base frame width only if
    # explicitly specified at the command line.
    if options.barcode_width == _DEFAULT_BARCODE_WIDTH:
        options.barcode_width = options.base_frame_width
    # If the user provides a value for the barcodes YUV video file, we will keep
    # it. Otherwise we create a temp file which is removed after it has been used.
    keep_barcodes_yuv_file = False
    if options.barcodes_yuv != _DEFAULT_BARCODES_FILE:
        keep_barcodes_yuv_file = True

    # Calculate the number of barcodes - it is equal to the number of frames in
    # the base file.
    number_of_barcodes = calculate_frames_number_from_yuv(
        options.base_frame_width, options.base_frame_height, options.base_yuv)

    script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
    zxing_dir = os.path.join(script_dir, 'third_party', 'zxing')
    # Generate barcodes - will generate them in PNG.
    generate_upca_barcodes(number_of_barcodes,
                           options.barcode_width,
                           options.barcode_height,
                           output_directory=options.png_barcodes_output_dir,
                           path_to_zxing=zxing_dir)
    # Convert the PNG barcodes to to YUV format.
    convert_png_to_yuv_barcodes(options.png_barcodes_input_dir,
                                options.yuv_barcodes_output_dir)
    # Combine the YUV barcodes into one YUV file.
    combine_yuv_frames_into_one_file(
        options.barcodes_yuv, input_directory=options.yuv_frames_input_dir)
    # Overlay the barcodes over the base file.
    overlay_yuv_files(options.barcode_width, options.barcode_height,
                      options.base_frame_width, options.base_frame_height,
                      options.barcodes_yuv, options.base_yuv,
                      options.output_yuv)

    if not keep_barcodes_yuv_file:
        # Remove the temporary barcodes YUV file
        helper_functions.delete_file(options.barcodes_yuv)
Example #8
0
def _main():
  """The main function.

  A simple invocation will be:
  ./tools/barcode_tools/barcode_encoder.py --barcode_height=32
  --base_frame_width=352 --base_frame_height=288
  --base_yuv=<path_and_name_of_base_file>
  --output_yuv=<path and name_of_output_file>
  """
  options = _parse_args()
  # The barcodes with will be different than the base frame width only if
  # explicitly specified at the command line.
  if options.barcode_width == _DEFAULT_BARCODE_WIDTH:
    options.barcode_width = options.base_frame_width
  # If the user provides a value for the barcodes YUV video file, we will keep
  # it. Otherwise we create a temp file which is removed after it has been used.
  keep_barcodes_yuv_file = False
  if options.barcodes_yuv != _DEFAULT_BARCODES_FILE:
    keep_barcodes_yuv_file = True

  # Calculate the number of barcodes - it is equal to the number of frames in
  # the base file.
  number_of_barcodes = calculate_frames_number_from_yuv(
      options.base_frame_width, options.base_frame_height, options.base_yuv)

  script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
  zxing_dir = os.path.join(script_dir, 'third_party', 'zxing')
  # Generate barcodes - will generate them in PNG.
  generate_upca_barcodes(number_of_barcodes, options.barcode_width,
                         options.barcode_height,
                         output_directory=options.png_barcodes_output_dir,
                         path_to_zxing=zxing_dir)
  # Convert the PNG barcodes to to YUV format.
  convert_png_to_yuv_barcodes(options.png_barcodes_input_dir,
                              options.yuv_barcodes_output_dir)
  # Combine the YUV barcodes into one YUV file.
  combine_yuv_frames_into_one_file(options.barcodes_yuv,
                                   input_directory=options.yuv_frames_input_dir)
  # Overlay the barcodes over the base file.
  overlay_yuv_files(options.barcode_width, options.barcode_height,
                    options.base_frame_width, options.base_frame_height,
                    options.barcodes_yuv, options.base_yuv, options.output_yuv)

  if not keep_barcodes_yuv_file:
    # Remove the temporary barcodes YUV file
    helper_functions.delete_file(options.barcodes_yuv)
Example #9
0
def listr(uvdata,bif=0,eif=0,optype='SCAN',ine='CL',inv=1,sources=[],dparm=[]):
	if len(dparm)>0:
		outprint = aips_out+uvdata.name+'_listr_'+optype+'dparm1='+str(dparm[0])+'.txt'
	else:
		outprint = aips_out+uvdata.name+'_listr_'+optype+'.txt'
	HF.delete_file(outprint)
	listr = AIPSTask('listr')
	listr.indata = uvdata
	listr.optype = optype
	listr.eif = eif
	listr.bif = bif
	listr.dparm[1:] = dparm
	listr.sources[1:]= sources
	listr.inext = ine
	listr.inver = inv
	listr.docrt = -1
	listr.outprint = outprint 
	listr()
Example #10
0
def _add_to_file_and_delete(output_file, file_name):
    """Adds the contents of a file to a previously opened file.

  Args:
    output_file(file): The ouput file, previously opened.
    file_name(string): The file name of the file to add to the output file.

  Return:
    (bool): True if successful, False otherwise.
  """
    input_file = open(file_name, "rb")
    input_file_contents = input_file.read()
    output_file.write(input_file_contents)
    input_file.close()
    return helper_functions.delete_file(file_name)
Example #11
0
def _add_to_file_and_delete(output_file, file_name):
  """Adds the contents of a file to a previously opened file.

  Args:
    output_file(file): The ouput file, previously opened.
    file_name(string): The file name of the file to add to the output file.

  Return:
    (bool): True if successful, False otherwise.
  """
  input_file = open(file_name, "rb")
  input_file_contents = input_file.read()
  output_file.write(input_file_contents)
  input_file.close()
  return helper_functions.delete_file(file_name)
Example #12
0
def do_apcal(uvdata,
             inv=0,
             aparm=[0],
             tyv=1,
             dofit=[0],
             tau0=[0],
             trecvr=[0],
             opcode='',
             calin='',
             savelog=True,
             repeat=2):
    apcal_log = aips_out + uvdata.name + '_APCAL_fit.log'
    temp = apcal_log.split('.')
    HF.delete_file(temp[0] + '*' + temp[1])
    apcal_log = HF.filename_append(apcal_log)
    AT.apcal(uvdata,
             aparm=aparm,
             inv=inv,
             tyv=tyv,
             dofit=dofit,
             tau0=tau0,
             trecvr=trecvr,
             opcode=opcode,
             calin=calin,
             savelog=apcal_log)
    trecvr, tau0 = get_apcal_fit(uvdata, apcal_log)
    trecvr_str = [','.join([str(x) for x in trecvr])]
    tau0_str = [','.join([str(x) for x in tau0])]

    if repeat > 1:
        for i in range(repeat - 1):
            trecvr, tau0 = get_apcal_fit(uvdata, apcal_log)
            apcal_log = HF.filename_append(apcal_log)
            uvdata.zap_table('SN', uvdata.table_highver('SN'))
            #dofit=15*[1]
            AT.apcal(uvdata,
                     aparm=aparm,
                     inv=inv,
                     tyv=tyv,
                     dofit=dofit,
                     tau0=tau0,
                     trecvr=trecvr,
                     opcode=opcode,
                     calin=calin,
                     savelog=apcal_log)
            trecvr2_str = [','.join([str(x) for x in trecvr])]
            tau02_str = [','.join([str(x) for x in tau0])]

    logger.info('''
	APCAL did run %i times. First with assuming typical values of 
	trecvr=100 and tau0=0.1 for all telescopes.
	For each following run the fit values for from the run of
	APCAL before was used as input for tau0 and trecvr.
					
	First run fit:
	%s
	%s

	Last run fit:
	%s
	%s
						
	SN%i produced.
	''' % (repeat, trecvr_str, tau0_str, trecvr2_str, tau02_str,
        uvdata.table_highver('SN')))