|
15 | 15 | import re |
16 | 16 | import sys |
17 | 17 |
|
| 18 | +# Input and output codes |
18 | 19 | _FREQUENCY_COLNAME = "CMIP7 Freq." |
19 | 20 | _MODELTYPE_COLNAME = "Modelling Realm - Primary" |
20 | 21 | _REGION_COLNAME = "Region" |
|
27 | 28 | '3hr':'! 3-hourly output', '1hr':'! 1-hourly output', |
28 | 29 | 'subhr':'! timestep output'} |
29 | 30 |
|
| 31 | +# Relative paths |
| 32 | +__MYDIR = os.path.dirname(__file__) |
| 33 | +__CAMDIR = os.path.dirname(os.path.dirname(__MYDIR)) |
| 34 | + |
30 | 35 | class Usermod(): |
31 | 36 | """Class to hold information about a history usermod directory""" |
32 | 37 |
|
@@ -99,19 +104,21 @@ def is_number(text): |
99 | 104 | return val |
100 | 105 |
|
101 | 106 | def command_line(args): |
102 | | - """Read the command line arguments (args) to retrieve the path to the |
103 | | - CMIP7 data request spreadsheet. |
104 | | - Return the path to the spreadsheet file.""" |
| 107 | + """Read the command line arguments (args) to retrieve the paths to the |
| 108 | + CMIP7 and CAM data request spreadsheets, the config file, and options. |
| 109 | + Return all argument values.""" |
105 | 110 | parser = argparse.ArgumentParser(description=__doc__, |
106 | 111 | formatter_class=argparse.RawTextHelpFormatter) |
107 | 112 |
|
108 | | - parser.add_argument("csv_file", metavar='<path to CMIP7 data request file>', type=str) |
109 | | - parser.add_argument("--usermods_dir", type=str, default="-", |
| 113 | + parser.add_argument("cmip_file", metavar='<path to CMIP7 data request file>', type=str) |
| 114 | + parser.add_argument("cam_file", metavar='<path to CAM data request file>', type=str) |
| 115 | + parser.add_argument("--usermods_dir", type=str, |
| 116 | + default=os.path.join(__CAMDIR,"cime_config", "usermods_dirs") |
110 | 117 | help="Path to write namelist file entries") |
111 | 118 | parser.add_argument("--overwrite", action='store_true', default=False, |
112 | 119 | help="Overwrite namelist file(s) if they exist") |
113 | 120 | pargs = parser.parse_args(args) |
114 | | - return pargs.csv_file, pargs.usermods_dir, pargs.overwrite |
| 121 | + return pargs.cmip_file, cam_file, pargs.usermods_dir, pargs.overwrite |
115 | 122 |
|
116 | 123 | def read_config_file(filename): |
117 | 124 | """Read a fincl group configuration (ini-style) file. |
@@ -146,37 +153,29 @@ def flex_open(filename=None, mode='w'): |
146 | 153 | if fh is not sys.stdout: |
147 | 154 | fh.close() |
148 | 155 |
|
149 | | -def read_diagnostic_fieldnames(include_cosp=False): |
150 | | - """Read the master list of CAM diagnostic (history) fieldnames from the |
151 | | - saved master list. |
152 | | - If <include_cosp> is True, include the COSP diagnostic fieldnames in the |
153 | | - master list. Otherwise, keep a separate list of COSP fieldnames |
154 | | - Return the list of fieldnames and the list of COSP fieldnames.""" |
155 | | - |
156 | | - cam_diag_fieldname_file = os.path.join(os.path.dirname(__file__), |
157 | | - "master_fieldlist.txt") |
| 156 | +def read_fieldname_file(filename): |
| 157 | + """Read a fieldname file and return all fieldnames as a list.""" |
| 158 | + diag_fieldname_file = os.path.join(__MYDIR, filename) |
158 | 159 | all_fieldnames = [] |
159 | | - cosp_fieldnames = [] |
160 | | - with open(cam_diag_fieldname_file, mode='r') as infile: |
| 160 | + with open(diag_fieldname_file, mode='r') as infile: |
161 | 161 | for line in infile: |
162 | 162 | fieldnames = [x.strip() for x in line.split()] |
163 | 163 | all_fieldnames.extend(fieldnames) |
164 | 164 | # end for |
165 | 165 | # end with |
166 | | - # Handle COSP fieldnames separately |
167 | | - cosp_diag_fieldname_file = os.path.join(os.path.dirname(__file__), |
168 | | - "cosp_fieldlist.txt") |
169 | | - with open(cosp_diag_fieldname_file, mode='r') as infile: |
170 | | - for line in infile: |
171 | | - fieldnames = [x.strip() for x in line.split()] |
172 | | - if include_cosp: |
173 | | - all_fieldnames.extend(fieldnames) |
174 | | - else: |
175 | | - cosp_fieldnames.extend(fieldnames) |
176 | | - # end for |
177 | | - # end with |
178 | | - # end if |
179 | | - return all_fieldnames, cosp_fieldnames |
| 166 | + return all_fieldnames |
| 167 | + |
| 168 | +def read_diagnostic_fieldnames(): |
| 169 | + """Read the master list of CAM diagnostic (history) fieldnames from the |
| 170 | + saved master list. |
| 171 | + Keep separate lists of COSP and Aerocom fieldnames |
| 172 | + Return the fieldname three lists.""" |
| 173 | + |
| 174 | + all_fieldnames = read_fieldname_file("master_fieldlist.txt") |
| 175 | + cosp_fieldnames = read_fieldname_file("cosp_fieldlist.txt") |
| 176 | + aerocom_fieldnames = read_fieldname_file("aerocom_fieldlist.txt") |
| 177 | + |
| 178 | + return all_fieldnames, cosp_fieldnames, aerocom_fieldnames |
180 | 179 |
|
181 | 180 | def parse_spreadsheet(csvfile, model_name="atmos"): |
182 | 181 | """Parse <csvfile> and return a dictionary of the requested CAM fields at |
@@ -295,13 +294,13 @@ def generate_namelist_entries(data_request, nl_filename, maxline=125, hist_files |
295 | 294 | ############################################################################### |
296 | 295 |
|
297 | 296 | if __name__ == "__main__": |
298 | | - csvfile, usermods_dir, overwrite = command_line(sys.argv[1:]) |
| 297 | + cmipfile, camfile, usermods_dir, overwrite = command_line(sys.argv[1:]) |
299 | 298 | if not overwrite and os.path.exists(nl_filename): |
300 | 299 | raise ValueError(f"namelist file, '{nl_filename}', exists, aborting") |
301 | 300 | # end if |
302 | 301 | # read configuration |
303 | 302 | usermod_dict = read_config_file(config_file) |
304 | | - all_fieldnames, cosp_fieldnames = read_diagnostic_fieldnames(include_cosp) |
| 303 | + all_fieldnames, cosp_fieldnames, aerocom_fieldnames = read_diagnostic_fieldnames() |
305 | 304 | data_request = parse_spreadsheet(csvfile) |
306 | 305 | missing = check_for_missing_fieldnames(all_fieldnames, data_request) |
307 | 306 | # Separate the COSP fields from the others |
|
0 commit comments