Skip to content
Snippets Groups Projects
Commit aa541d51 authored by Karl-Hermann Wieners's avatar Karl-Hermann Wieners
Browse files

Added new features to script generator

* Added optional template for namelists (namcouple)
* Added info line for generated files (mkexp_input variable)
* Added model directory to 'getexp' output
parent 7716aef4
No related branches found
No related tags found
No related merge requests found
......@@ -49,11 +49,13 @@ except ExpConfigError as error:
die(error.message, 2)
print("EXP_ID='{0}'".format(config.experiment_id))
print("MODEL_DIR='{0}'".format(config['MODEL_DIR']))
print("SCRIPT_DIR='{0}'".format(config['SCRIPT_DIR']))
print("WORK_DIR='{0}'".format(config['WORK_DIR']))
print("DATA_DIR='{0}'".format(config['DATA_DIR']))
if not (os.path.isdir(config['SCRIPT_DIR']) and
if not (os.path.isdir(config['MODEL_DIR']) and
os.path.isdir(config['SCRIPT_DIR']) and
os.path.isdir(config['WORK_DIR']) and
os.path.isdir(config['DATA_DIR'])):
die("data for experiment '{0}' does not exist".format(config.experiment_id))
......
......@@ -51,7 +51,14 @@ def chmod_plus_x(file_name):
# Templates
def get_template_name(experiment_kind, job_id):
def get_template_name(base_name):
'''Determine script template, using experiments' library path.'''
template_name = os.path.join(ExpConfig.exp_lib_dir, base_name+'.tmpl')
if not os.path.exists(template_name):
feedback.die("no template for '%s'", base_name)
return template_name
def get_exp_template_name(experiment_kind, job_id):
'''Determine script template, taking system default if type is unsupported.'''
run_template_name = os.path.join(ExpConfig.exp_lib_dir,
experiment_kind+'.'+job_id+'.tmpl')
......@@ -61,11 +68,19 @@ def get_template_name(experiment_kind, job_id):
ExpConfig.default_name+'.'+job_id+'.tmpl')
return run_template_name
def get_script_name(experiment_id, job_id):
def get_script_name(base_name):
'''Determine job script name.'''
return os.path.join(script_dir, base_name)
def get_exp_script_name(experiment_id, job_id):
'''Determine job script name.'''
return os.path.join(script_dir, experiment_id+'.'+job_id)
def expand_template(template_dict, template_name, expanded_name):
def expand_template(template_dict, template_name):
'''Replace keywords in template string using the given dictionary.'''
return template_env.get_template(template_name).render(template_dict)
def expand_template_file(template_dict, template_name, expanded_name):
'''Replace keywords in template file using the given dictionary.'''
expanded_file = open(expanded_name, 'w')
for line in template_env.get_template(template_name).generate(template_dict):
......@@ -74,6 +89,8 @@ def expand_template(template_dict, template_name, expanded_name):
expanded_file.close()
chmod_plus_x(expanded_name)
# Namelist formatting
def format_atom(value):
......@@ -163,9 +180,10 @@ if invalid_args:
# Read and store configuration info from input and experiments' library
# Store environment as default for control settings, then add config from files
extra_dict = dict(map(lambda x: x.split('=', 1), sys.argv))
extra_dict.update(mkexp_input='Generated by $$Id$$')
try:
config = ExpConfig(experiment_config_name,
dict(map(lambda x: x.split('=', 1), sys.argv)))
config = ExpConfig(experiment_config_name, extra_dict)
except ExpConfigError as error:
feedback.die(error.message, 2)
......@@ -250,12 +268,17 @@ for subjob, subconfig in config['jobs'].iteritems():
for namelist, groups in job_config['namelists'].iteritems():
if isinstance(groups, dict):
namelist_name = re.sub(r'\W', '_', namelist.upper())
job_config[namelist_name] = format_namelist(groups)
if groups.get('use_template'):
job_config[namelist_name] = \
expand_template(groups, get_template_name(namelist))
else:
job_config[namelist_name] = format_namelist(groups)
# Generate job script
expand_template(job_config,
get_template_name(config.experiment_kind, template_job),
get_script_name(config.experiment_id, subjob))
expand_template_file(job_config,
get_exp_template_name(config.experiment_kind,
template_job),
get_exp_script_name(config.experiment_id, subjob))
# Create README file from experiment description
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment