Writing the output trajectory to file