import os
import sys
import argparse

from parflow.tools.database.core import load_pfidb, write_dict_as_pfidb, sort_dict

def writeSortedPFIDB(input_file, output_file=None):
  if not os.path.exists(input_file):
    print(f'Input file does not exist: {input_file}')
    return

  if output_file is None:
    output_file = input_file

  write_dict_as_pfidb(sort_dict(load_pfidb(input_file)), output_file)


if __name__ == "__main__":
  parser = argparse.ArgumentParser(description="Parflow PFidb sorter")
  parser.add_argument("--input-file", "-i",
                       default=None,
                       dest="input",
                       help="Path to ParFlow database file to sort")
  parser.add_argument("--output-file", "-o",
                       default=None,
                       dest="output",
                       help="Output file path to write sorted result to")
  args = parser.parse_args()

  if args.input is None:
    parser.print_help()
    sys.exit(0)

  writeSortedPFIDB(args.input, args.output)
