def merge_screen_files(original_file,continued_file,merged_file,step_to_continue_from):
  """
  Removes the appropriate lines from the original screen file
  and add the step to continue from number to the steps of the second file
  and then tacks that editted file to the end of the original file.
  """
  cosy_output = CosyOutput()
  cosy_output.injectScreenFile(original_file)
  row_numbers = cosy_output.returnRowNumbersWithKeyValuePair("step number",step_to_continue_from,comparison = 'gt')
  for row_number in row_numbers:
    del cosy_output.table[row_number]
  cosy_output.injectScreenFile(continued_file,adjust_step_number=step_to_continue_from)
  cosy_output.writeToFile(merged_file)
import sys
import os
import argparse
from folder_convention import parse_folder_for_metadata
from cosy_output import CosyOutput

parser = argparse.ArgumentParser(description='Write the two columns for the velocity data: z,v/c')
parser.add_argument('screen_file', type=str, help='The path screen.txt are stored.', default=None)
args = parser.parse_args()

print "z,v/c";
with open(args.list_of_directories) as f:
  for line in f:
    line = line.rstrip()
    output =[]
    folder_metadata = parse_folder_for_metadata(line)
    output.append(folder_metadata["total_electrons"])
    cosy_output = CosyOutput()
    cosy_output.injectScreenFile(os.path.join(line,"2-Cosy/screen.txt"))
    row_numbers = cosy_output.returnRowNumbersWithKeyValuePair("time",120e-12,comparison = 'gt')
    if row_numbers == []:
      continue
    output.append(str(100 * int(cosy_output.returnTableValue("number macroparticles",row_numbers[0]))))
    output.append(folder_metadata["applied_field"])
    print ",".join(output)
    
  cosy_output = CosyOutput()
  cosy_output.injectScreenFile(screen_file)
  last_step = cosy_output.returnTableValue("step number")
  parameter_set.step_to_continue_from = int((floor(float(last_step)/float(10))-1)*10)
  
  #Create output dir
  continue_dir = os.path.join(parameter_set.returnStepnamePath("Cosy"),"continue_from_" + str(parameter_set.step_to_continue_from))
  if not os.path.exists(continue_dir) or not os.path.isdir(continue_dir):
    os.mkdir(continue_dir)

  #Grab the correct step's files for new initial conditions file.
  parameter_set.initial_conditions_file = os.path.join(continue_dir,"InitCondition_from_" + str(parameter_set.step_to_continue_from) + ".txt")
  make_continuing_initial_condition_file(parameter_set.returnStepnamePath("Cosy"),parameter_set.step_to_continue_from, parameter_set.initial_conditions_file)

  #Edit existing PE-setup.txt file.
  relevant_row_numbers = cosy_output.returnRowNumbersWithKeyValuePair("step number",str(parameter_set.step_to_continue_from))
  parameter_set.time_to_continue_from = cosy_output.returnTableValue("time",row_number=relevant_row_numbers[0])
  parameter_set.total_particles_generated = cosy_output.returnTableValue("number macroparticles",row_number=relevant_row_numbers[0])
  pe_setup_in = os.path.join(parameter_set.returnStepnamePath("Cosy"),"PE-setup.txt")
  pe_setup_out = os.path.join(continue_dir,"PE-setup.txt")
  edit_PE_setup_file(pe_setup_in,pe_setup_out,parameter_set)

  #Link the rest of the files.
  pulse_file = os.path.join(args.pos_dir,parameter_set.returnPEMainFilename())
  hole_file = os.path.join(args.pos_dir,parameter_set.returnHoleFilename())
  initial_conditions_file = os.path.join(parameter_set.returnStepnamePath("Generate_initial_conditions"),parameter_set.returnInitialConditionsFilename())
  os.symlink(pulse_file, os.path.join(continue_dir,parameter_set.pulse_file))
  os.symlink(hole_file, os.path.join(continue_dir,parameter_set.hole_file))
  os.symlink(args.bin_file, os.path.join(continue_dir,"cosy.bin"))
  with open(os.path.join(continue_dir,"cosy_run.sh"),'w') as output:
    template_string = fill_template(args.run_template,parameter_set)