Skip to content

Commit

Permalink
Merge pull request ESCOMP#329 from ckoven/pythontoolfixes
Browse files Browse the repository at this point in the history
Updates to modify_fates_paramfile python script
  • Loading branch information
rgknox authored Feb 13, 2018
2 parents 34e7722 + ce6ac74 commit a3b2f4a
Showing 1 changed file with 67 additions and 51 deletions.
118 changes: 67 additions & 51 deletions tools/modify_fates_paramfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from scipy.io import netcdf as nc
import argparse
import shutil
import tempfile


# ========================================================================================
# ========================================================================================
Expand All @@ -38,60 +40,74 @@ def main():
parser.add_argument('--silent', '--s', dest='silent', help="prevent writing of output.", action="store_true")
#
args = parser.parse_args()
# print(args.varname, args.pftnum, args.inputfname, args.outputfname, args.val, args.overwrite)
#
# check to see if output file exists
if os.path.isfile(args.outputfname):
if args.overwrite:
if not args.silent:
print('replacing file: '+args.outputfname)
os.remove(args.outputfname)
else:
raise ValueError('Output file already exists and overwrite flag not specified for filename: '+args.outputfname)
#
shutil.copyfile(args.inputfname, args.outputfname)
#
ncfile = nc.netcdf_file(args.outputfname, 'a')
#
var = ncfile.variables[args.varname]
# work with the file in some random temprary place so that if something goes wrong nothing happens to original file and it doesn't make an output file
tempdir = tempfile.mkdtemp()
tempfilename = os.path.join(tempdir, 'temp_fates_param_file.nc')
#
### check to make sure that, if a PFT is specified, the variable has a PFT dimension, and if not, then it doesn't. and also that shape is reasonable.
ndim_file = len(var.dimensions)
ispftvar = False
for i in range(ndim_file):
if var.dimensions[i] == 'fates_pft':
ispftvar = True
npft_file = var.shape[i]
pftdim = 0
else:
npft_file = None
pftdim = None
if args.pftnum == None and ispftvar:
raise ValueError('pft value is missing but variable has pft dimension.')
if args.pftnum != None and not ispftvar:
raise ValueError('pft value is present but variable does not have pft dimension.')
if ndim_file > 1:
raise ValueError('variable dimensionality is too high for this script')
if ndim_file == 1 and not ispftvar:
raise ValueError('variable dimensionality is too high for this script')
if args.pftnum != None and ispftvar:
if args.pftnum > npft_file:
raise ValueError('PFT specified ('+str(args.pftnum)+') is larger than the number of PFTs in the file ('+str(npft_file)+').')
if pftdim == 0:
try:
shutil.copyfile(args.inputfname, tempfilename)
#
ncfile = nc.netcdf_file(tempfilename, 'a')
#
var = ncfile.variables[args.varname]
#
### check to make sure that, if a PFT is specified, the variable has a PFT dimension, and if not, then it doesn't. and also that shape is reasonable.
ndim_file = len(var.dimensions)
ispftvar = False
# for purposes of current stat eof this script, assume 1D
if ndim_file > 1:
raise ValueError('variable dimensionality is too high for this script')
if ndim_file < 1:
raise ValueError('variable dimensionality is too low for this script. FATES assumes even scalars have a 1-length dimension')
for i in range(ndim_file):
if var.dimensions[i] == 'fates_pft':
ispftvar = True
npft_file = var.shape[i]
pftdim = 0
elif var.dimensions[i] == 'fates_scalar':
npft_file = None
pftdim = None
else:
raise ValueError('variable is not on either the PFT or scalar dimension')
if args.pftnum == None and ispftvar:
raise ValueError('pft value is missing but variable has pft dimension.')
if args.pftnum != None and not ispftvar:
raise ValueError('pft value is present but variable does not have pft dimension.')
if args.pftnum != None and ispftvar:
if args.pftnum > npft_file:
raise ValueError('PFT specified ('+str(args.pftnum)+') is larger than the number of PFTs in the file ('+str(npft_file)+').')
if pftdim == 0:
if not args.silent:
print('replacing prior value of variable '+args.varname+', for PFT '+str(args.pftnum)+', which was '+str(var[args.pftnum-1])+', with new value of '+str(args.val))
var[args.pftnum-1] = args.val
elif args.pftnum == None and not ispftvar:
if not args.silent:
print('replacing prior value of variable '+args.varname+', for PFT '+str(args.pftnum)+', which was '+str(var[args.pftnum-1])+', with new value of '+str(args.val))
var[args.pftnum-1] = args.val
elif args.pftnum == None and not ispftvar:
if not args.silent:
print('replacing prior value of variable '+args.varname+', which was '+str(var[:])+', with new value of '+str(args.val))
var[:] = args.val
else:
raise ValueError('Nothing happened somehow.')
#
#
ncfile.close()


print('replacing prior value of variable '+args.varname+', which was '+str(var[:])+', with new value of '+str(args.val))
var[:] = args.val
else:
raise ValueError('Nothing happened somehow.')
#
#
ncfile.close()
#
#
# now move file from temprary location to final location
#
# check to see if output file exists
if os.path.isfile(args.outputfname):
if args.overwrite:
if not args.silent:
print('replacing file: '+args.outputfname)
os.remove(args.outputfname)
else:
raise ValueError('Output file already exists and overwrite flag not specified for filename: '+args.outputfname)
#
shutil.move(tempfilename, args.outputfname)
shutil.rmtree(tempdir, ignore_errors=True)
except:
shutil.rmtree(tempdir, ignore_errors=True)
raise


# =======================================================================================
Expand Down

0 comments on commit a3b2f4a

Please sign in to comment.