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 @ 4136

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

added a dry run flag to quick script to easily test namelist editing

  • Property svn:executable set to *
File size: 3.1 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("namelist", metavar="NAMELIST", 
28                        help="NEMO namelist to edit.")
29    args = parser.parse_args()
30    args.forecast_types = args.forecast_types.split(',')
31    args.obs_types = args.obs_types.split(',')
32    args.lead_times = map(int, args.lead_times.split(','))
33    return args
34
35def main():
36    args = parse_args()
37    date = args.date
38    print "Processing", args.namelist, " for", args.date
39   
40    # Move to working directory
41    if not os.path.exists(args.work_dir):
42        os.makedirs(args.work_dir)
43    os.chdir(args.work_dir)
44
45    # Collect forecast files
46    types = args.forecast_types
47    lead_times = args.lead_times
48    namooo, namcl4 = locator.forecasts(date=date,
49                                       types=types,
50                                       lead_times=lead_times)
51
52    # Process NEMO namelist
53    text = nml.reader(args.namelist)
54    sublists = nml.namelists(text)
55
56    # namooo
57    if "namooo" not in sublists:
58        # Attach boilerplate
59        text += nml.new("namooo")
60    text = nml.update("namooo", text, data=namooo)
61
62    # namcl4
63    if "namcl4" not in sublists:
64        # Attach boilerplate
65        text += nml.new("namcl4")
66    namcl4["cl4_leadtime"] = lead_times
67    namcl4["cl4_date"] = nml.quote(date)
68    namcl4["cl4_match_len"] = len(namcl4["cl4_vars"])
69    namcl4["cl4_fcst_len"] = len(namcl4["cl4_leadtime"])
70    # Add naming convention
71    namcl4["cl4_sys"] = "FOAM"
72    namcl4["cl4_cfg"] = "orca025"
73    namcl4["cl4_vn"] = "'1.0'"
74    namcl4["cl4_prefix"] = "class4"
75    namcl4["cl4_contact"] = "example@example.com"
76    namcl4["cl4_inst"] = "institute"
77    text = nml.update("namcl4", text, data=namcl4)
78
79    # namrun
80    namrun = {"nn_date0": date}
81    text = nml.update("namrun", text, data=namrun)
82
83    # namobs
84    namobs = locator.observations(date=date,
85                                  types=args.obs_types)
86    namobs["ln_cl4"] = args.ln_cl4
87    text = nml.update("namobs", text, data=namobs)
88
89    # pipe text to file
90    tmp = args.namelist+".tmp"
91    nml.writer(tmp, text)
92    shutil.move(tmp, args.namelist)
93
94    # Run job
95    if not args.dry_run:
96        run.submit()
97   
98
99
100if __name__ == '__main__':
101    main()
102
Note: See TracBrowser for help on using the repository browser.