Skip to content

Commit

Permalink
Merge pull request #11 from glemieux/update_batchpatch-histchange
Browse files Browse the repository at this point in the history
Update BatchPatchParams stack to modify the history attribute
  • Loading branch information
rgknox authored Oct 31, 2022
2 parents cfe1d96 + e0c8ab9 commit 72866b4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
27 changes: 18 additions & 9 deletions tools/BatchPatchParams.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ def parse_syscall_str(fnamein,fnameout,pft_index,param_name,param_val):
if(pft_index==0):
sys_call_str = "../tools/modify_fates_paramfile.py"+" --fin " + fnamein + \
" --fout " + fnameout + " --var " + param_name + " --silent " +\
" --val " + "\" "+param_val+"\"" + " --overwrite --all"
" --val " + "\" "+param_val+"\"" + " --nohist --overwrite --all"
else:
pft_str_index="{}".format(pft_index)
sys_call_str = "../tools/modify_fates_paramfile.py"+" --fin " + fnamein + \
" --fout " + fnameout + " --var " + param_name + " --silent " +\
" --val " + "\" "+param_val+"\"" + " --overwrite --pft "+pft_str_index
" --val " + "\" "+param_val+"\"" + " --nohist --overwrite --pft "+pft_str_index

if(debug):
print(sys_call_str)
Expand All @@ -96,7 +96,7 @@ def main():
#[base_cdl,new_cdl,pftlist,paramlist] = load_xml(args.xmlfile)

xmlroot = et.parse(args.xmlfile).getroot()
print("\nOpenend: "+args.xmlfile)
print("\nOpened: "+args.xmlfile)

base_cdl = xmlroot.find('base_file').text
new_cdl = xmlroot.find('new_file').text
Expand All @@ -111,13 +111,9 @@ def main():

# Use FatesPFTIndexSwapper.py to prune out unwanted PFTs
pft_trim_list = xmlroot.find('pft_trim_list').text.replace(" ","")
swapcmd="../tools/FatesPFTIndexSwapper.py --pft-indices="+pft_trim_list+" --fin="+base_nc+" --fout="+new_nc #+" 1>/dev/null"
swapcmd="../tools/FatesPFTIndexSwapper.py --pft-indices="+pft_trim_list+" --fin="+base_nc+" --fout="+new_nc+" --nohist" #+" 1>/dev/null"
os.system(swapcmd)

# We open the new parameter file. We only use this
# to do some dimension checking.
fp_nc = netcdf.netcdf_file(base_nc, 'r')

# On subsequent parameters, overwrite the file

paramroot = xmlroot.find('parameters')
Expand Down Expand Up @@ -182,9 +178,22 @@ def main():

# Dump the new file to the cdl
os.system("ncdump "+newer_nc+" > "+new_cdl)


# Generate ncfile and append history
final_nc = new_cdl.replace('.cdl','.nc')
gencmd = "ncgen -o "+final_nc+" "+new_cdl
os.system(gencmd)
fp_nc = netcdf.netcdf_file(final_nc, 'a')
fp_nc.history = "This file was generated by BatchPatchParams.py:\n"\
"CDL Base File = {}\n"\
"XML patch file = {}"\
.format(base_cdl,args.xmlfile)
fp_nc.close()

# Re-generate the cdl file
os.system("ncdump "+final_nc+" > "+new_cdl)


print("\nBatch parameter transfer complete\n")
print("\nGenerated: {}\n".format(new_cdl))

Expand Down
14 changes: 9 additions & 5 deletions tools/FatesPFTIndexSwapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ def interp_args(argv):
output_fname = "none"
donor_pft_indices = -9
donot_pft_indices_str = ''
histflag = True
try:
opts, args = getopt.getopt(argv, 'h',["fin=","fout=","pft-indices="])
opts, args = getopt.getopt(argv, 'h',["fin=","fout=","pft-indices=","nohist"])
except getopt.GetoptError as err:
print('Argument error, see usage')
usage()
Expand All @@ -103,6 +104,8 @@ def interp_args(argv):
output_fname = a
elif o in ("--pft-indices"):
donor_pft_indices_str = a.strip()
elif o in ("--nohist"):
histflag = False
else:
assert False, "unhandled option"

Expand All @@ -127,7 +130,7 @@ def interp_args(argv):
donor_pft_indices.append(int(strpft))


return (input_fname,output_fname,donor_pft_indices)
return (input_fname,output_fname,donor_pft_indices,histflag)


# ========================================================================================
Expand All @@ -139,7 +142,7 @@ def interp_args(argv):
def main(argv):

# Interpret the arguments to the script
[input_fname,output_fname,donor_pft_indices] = interp_args(argv)
[input_fname,output_fname,donor_pft_indices,histflag] = interp_args(argv)

num_pft_out = len(donor_pft_indices)

Expand Down Expand Up @@ -252,8 +255,9 @@ def main(argv):
out_var.units = in_var.units
out_var.long_name = in_var.long_name

fp_out.history = "This file was made from FatesPFTIndexSwapper.py \n Input File = {} \n Indices = {}"\
.format(input_fname,donor_pft_indices)
if(histflag):
fp_out.history = "This file was made from FatesPFTIndexSwapper.py \n Input File = {} \n Indices = {}"\
.format(input_fname,donor_pft_indices)

#var_out.mode = var.mode
#fp.flush()
Expand Down

0 comments on commit 72866b4

Please sign in to comment.