New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
ooo.py in branches/2013/dev_r3987_UKMO4_OBS/NEMOGCM/TOOLS/OBSTOOLS/OOO/ooo – NEMO

source: branches/2013/dev_r3987_UKMO4_OBS/NEMOGCM/TOOLS/OBSTOOLS/OOO/ooo/ooo.py @ 4139

Last change on this file since 4139 was 4138, checked in by andrewryan, 11 years ago

fixed quotation marks in namelist generator

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