@@ -127,31 +127,51 @@ def command_line(args):
127127 """Read the command line arguments (args) to retrieve the paths to the
128128 CMIP7 and CAM data request spreadsheets, the config file, and options.
129129 Return all argument values."""
130- parser = argparse .ArgumentParser (description = __doc__ ,
131- formatter_class = argparse .RawTextHelpFormatter )
130+ parser = argparse .ArgumentParser (description = __doc__ )
132131
133132 parser .add_argument ("CMIP_file" , type = str ,
134133 metavar = '<path to CMIP7 data request file>' )
135134 parser .add_argument ("CAM_file" , type = str ,
136135 metavar = '<path to CAM data request file>' )
136+ umod_def = os .path .join (__CAMDIR , "cime_config" , "usermods_dirs" )
137+ umod_help = ("Path to write namelist file entries. "
138+ f"default: { umod_def } " )
137139 parser .add_argument ("--usermods-dir" , dest = 'usermods' , type = str ,
138- default = os . path . join ( __CAMDIR , "cime_config" , "usermods_dirs" ) ,
139- help = "Path to write namelist file entries" )
140+ metavar = '<USERMODS FILEPATH>' , default = umod_def ,
141+ help = umod_help )
140142 parser .add_argument ("--overwrite" , action = 'store_true' , default = False ,
141143 help = "Overwrite namelist file(s) if they exist" )
144+ umod_def = os .path .join (__MYDIR , "usermods_sets.cfg" )
145+ umod_help = ("Path to configuration file for usermods sets. "
146+ f"default: { umod_def } " )
142147 parser .add_argument ("--usermods-config" , dest = 'cfgfile' , type = str ,
143- default = os .path .join (__MYDIR , "usermods_sets.cfg" ),
144- help = "Path to configuration file for usermods sets" )
145- parser .add_argument ("--error-on-missing" , action = 'store_true' ,
146- default = False ,
147- help = """Stop processing if any missing fields found.
148- Default is to produce fieldlist files by ignoring any
149- missing fields.""" )
150- parser .add_argument ("--max-line" , type = int , default = 80 ,
151- help = "Maximum line length for namelist files" )
148+ metavar = '<USERMODS CONFIG FILEPATH>' ,
149+ default = umod_def , help = umod_help )
150+ umod_help = ("Stop processing if any missing fields found. "
151+ "Default is to produce fieldlist files by ignoring any "
152+ "missing fields." )
153+ parser .add_argument ("--error-on-missing" , action = 'store_true' , default = False ,
154+ help = umod_help )
155+ umod_def = 80
156+ umod_help = f"Maximum line length for namelist files. default: { umod_def } "
157+ parser .add_argument ("--max-line" , type = int , default = umod_def , help = umod_help )
158+ umod_help = ("Produce more output on missing fields. "
159+ "By default, a field is only declared missing if it is "
160+ "not available in any configuration." )
161+ parser .add_argument ("--verbose" , action = 'store_true' , default = False ,
162+ help = umod_help )
163+ umod_help = ("Only process (update) the usermods sets specified."
164+ "By default, all configured usermods sets are processed "
165+ "and updated. On the command line, these section-names "
166+ "(the string in square brackets in the usermods-config "
167+ "file) is specified after the required arguments but "
168+ "before any options." )
169+ parser .add_argument ("usermod_sets" , nargs = "*" , default = [],
170+ help = umod_help )
152171 pargs = parser .parse_args (args )
153172 return (pargs .CMIP_file , pargs .CAM_file , pargs .usermods , pargs .cfgfile ,
154- pargs .overwrite , pargs .error_on_missing , pargs .max_line )
173+ pargs .overwrite , pargs .error_on_missing , pargs .max_line ,
174+ pargs .verbose , pargs .usermod_sets )
155175
156176def read_config_file (filename , usermods_dir , overwrite ):
157177 """Read a fincl group configuration (ini-style) file.
@@ -392,13 +412,17 @@ def check_for_missing_fieldnames(fixedset, data_request):
392412 return missing
393413
394414def generate_namelist_entries (data_request , usermod_config , fixed_fieldnames ,
395- cosp_fieldnames , aerocom_fieldnames , maxline ):
415+ cosp_fieldnames , aerocom_fieldnames ,
416+ usermods_sets , maxline ):
396417 """Write the sets of namelist entries represented by <data_request> to
397418 the usermods files defined in <usermod_config>.
398419 Return a dictionary of field names not found in the CAM fixed list. The missing
399420 names are found and reported from each config set """
400421 missing_fields = {}
401422 for usermod in usermod_config .values ():
423+ if usermods_sets and (usermod .name not in usermods_sets ):
424+ continue
425+ # end if
402426 lbreak = ''
403427 if not os .path .exists (usermod .dirname ):
404428 os .makedirs (usermod .dirname )
@@ -472,7 +496,7 @@ def generate_namelist_entries(data_request, usermod_config, fixed_fieldnames,
472496
473497if __name__ == "__main__" :
474498 arglist = command_line (sys .argv [1 :])
475- cmipfile , camfile , usermods , configfile , overwrite , error , maxline = arglist
499+ cmipfile , camfile , usermods , configfile , overwrite , error , maxline , verbose , usets = arglist
476500 # read configuration
477501 usermod_dict = read_config_file (configfile , usermods , overwrite )
478502 errmsg = "not producing any namelist usermods files"
@@ -486,9 +510,28 @@ def generate_namelist_entries(data_request, usermod_config, fixed_fieldnames,
486510 elif usermod_dict :
487511 data_request = combine_data_requests (cmip7_request , cam_request )
488512 missing = generate_namelist_entries (data_request , usermod_dict , fixed_fieldnames ,
489- cosp_fieldnames , aerocom_fieldnames , maxline )
513+ cosp_fieldnames , aerocom_fieldnames ,
514+ usets , maxline )
515+ num_sections = len (usermod_dict )
516+ if not verbose :
517+ # Remove missing fields that are defined in at least one usermod
518+ to_remove = []
519+ for field in missing :
520+ if len (missing [field ]) < num_sections :
521+ to_remove .append (field )
522+ # end if
523+ # end for
524+ for field in to_remove :
525+ del missing [field ]
526+ # end for
527+ # end if
490528 if missing :
491- print (f"The following { len (missing )} fields are not output from CAM:" )
529+ if verbose :
530+ print (f"The following { len (missing )} fields are not output in some "
531+ "usermod configurations" )
532+ else :
533+ print (f"The following { len (missing )} fields are not output from CAM:" )
534+ # end if
492535 # end if
493536 jstr = ', '
494537 for data_request , label in [(cmip7_request , "CMIP7" ), (cam_request , "CAM" )]:
@@ -500,7 +543,11 @@ def generate_namelist_entries(data_request, usermod_config, fixed_fieldnames,
500543 print (f"The following fields are from the { label } data request spreadsheet:" )
501544 message_shown = True
502545 # end if
503- print (f" { field } : { jstr .join (missing [field ])} " )
546+ if verbose :
547+ print (f" { field } : { jstr .join (missing [field ])} " )
548+ else :
549+ print (f" { field } " )
550+ # end if
504551 # end if
505552 # end for
506553 # end for
0 commit comments