ooo.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python2.7
  2. import os
  3. import shutil
  4. # Local imports
  5. import locator
  6. import nml
  7. import run
  8. def parse_args():
  9. import argparse
  10. parser = argparse.ArgumentParser()
  11. parser.add_argument("date", metavar="DATE",
  12. help="Run date.")
  13. parser.add_argument("-w", "--work-dir", default=os.getcwd())
  14. parser.add_argument("-f", "--forecast-types", default="forecast",
  15. help="Choice of forecast,persistence,climatology")
  16. parser.add_argument("-l", "--lead-times", default="12",
  17. help="Forecast lead times")
  18. parser.add_argument("-o", "--obs-types", default="profbfiles",
  19. help="Choice of namobs types.")
  20. parser.add_argument("--class4", dest="ln_cl4", action="store_true",
  21. help="Flag to choose class 4 file outputs")
  22. parser.add_argument("--dry-run", action="store_true",
  23. help="Flag to test namelist building without submitting.")
  24. parser.add_argument("--cmd", dest="command", default="./opa",
  25. help="Submit task to run.")
  26. parser.add_argument("-v", "--verbose", action="store_true",
  27. help="Prints difference between before and after namelists.")
  28. parser.add_argument("namelist", metavar="NAMELIST",
  29. help="NEMO namelist to edit.")
  30. args = parser.parse_args()
  31. args.forecast_types = args.forecast_types.split(',')
  32. args.obs_types = args.obs_types.split(',')
  33. args.lead_times = map(int, args.lead_times.split(','))
  34. return args
  35. def printdiff(text1, text2):
  36. # Provides nice text difference summary of namelists
  37. import difflib
  38. lines1 = text1.splitlines()
  39. lines2 = text2.splitlines()
  40. d = difflib.Differ()
  41. result = list(d.compare(lines1, lines2))
  42. text = '\n'.join(result)
  43. print text
  44. def main():
  45. args = parse_args()
  46. date = args.date
  47. print "Processing", args.namelist, " for", args.date
  48. # Move to working directory
  49. if not os.path.exists(args.work_dir):
  50. os.makedirs(args.work_dir)
  51. os.chdir(args.work_dir)
  52. # Collect forecast files
  53. types = args.forecast_types
  54. lead_times = args.lead_times
  55. namooo, namcl4 = locator.forecasts(date=date,
  56. types=types,
  57. lead_times=lead_times)
  58. # Process NEMO namelist
  59. text = nml.reader(args.namelist)
  60. sublists = nml.namelists(text)
  61. # Verbose save original text
  62. if args.verbose:
  63. original_text = text
  64. # namooo
  65. if "namooo" not in sublists:
  66. # Attach boilerplate
  67. text += nml.new("namooo")
  68. text = nml.update("namooo", text, data=namooo)
  69. # namcl4
  70. if "namcl4" not in sublists:
  71. # Attach boilerplate
  72. text += nml.new("namcl4")
  73. namcl4["cl4_leadtime"] = lead_times
  74. namcl4["cl4_date"] = nml.quote(date)
  75. namcl4["cl4_match_len"] = len(namcl4["cl4_vars"])
  76. namcl4["cl4_fcst_len"] = len(namcl4["cl4_leadtime"])
  77. # Add naming convention
  78. namcl4["cl4_sys"] = "FOAM"
  79. namcl4["cl4_cfg"] = "orca025"
  80. namcl4["cl4_vn"] = "'1.0'"
  81. namcl4["cl4_prefix"] = "class4"
  82. namcl4["cl4_contact"] = "example@example.com"
  83. namcl4["cl4_inst"] = "institute"
  84. text = nml.update("namcl4", text, data=namcl4)
  85. # namrun
  86. namrun = {"nn_date0": nml.quote(date)}
  87. text = nml.update("namrun", text, data=namrun)
  88. # namobs
  89. namobs = locator.observations(date=date,
  90. types=args.obs_types)
  91. namobs["ln_cl4"] = args.ln_cl4
  92. text = nml.update("namobs", text, data=namobs)
  93. # Verbose print namelist differences
  94. if args.verbose:
  95. printdiff(original_text, text)
  96. # pipe text to file
  97. tmp = args.namelist+".tmp"
  98. nml.writer(tmp, text)
  99. shutil.move(tmp, args.namelist)
  100. # Run job
  101. if not args.dry_run:
  102. run.submit(command=args.command)
  103. if __name__ == '__main__':
  104. main()