Skip to content

Commit

Permalink
Use python-netcdf4 instead of python
Browse files Browse the repository at this point in the history
Allow for single factor experiments
Allow running multi-site ensembles
Update for v1 parameters for RD
  • Loading branch information
dmricciuto committed Apr 13, 2018
1 parent 824a133 commit 03bd485
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 76 deletions.
2 changes: 1 addition & 1 deletion adjust_restart.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env/python
import os, sys, csv, time, math
from optparse import OptionParser
import netcdf_functions as nffun
import netcdf4_functions as nffun
import glob

parser = OptionParser()
Expand Down
110 changes: 106 additions & 4 deletions case_copy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

import netcdf_functions as nffun
import netcdf4_functions as nffun
import os, sys, csv, time, math, numpy
from optparse import OptionParser

Expand Down Expand Up @@ -31,6 +31,16 @@
help = 'Year for initial data file')
parser.add_option('--spin_cycle', dest='spin_cycle', default=0, \
help = 'Number of years in spinup cycle')
parser.add_option('--1850_landuse', dest='nolanduse', default=False, \
action='store_true', help = '1850 land use (no dynamics)')
parser.add_option('--1850_co2', dest='noco2', default=False, \
action='store_true', help = 'use 1850 CO2')
parser.add_option('--1850_ndep', dest='nondep', default=False, \
action='store_true', help = 'use 1850 NDep')
parser.add_option('--suffix', dest='suffix', default='', \
help = 'use 1850 NDep')
parser.add_option('--machine', dest='machine', default='cades', \
help = 'machine')

(options, args) = parser.parse_args()

Expand All @@ -39,7 +49,12 @@
#create directory from original case
orig_dir = str(os.path.abspath(options.runroot)+'/'+casename+'/run')
new_dir = orig_dir.replace(options.site_orig, options.site_new)
if (options.suffix != ''):
new_dir = new_dir.replace(casename,casename+'_'+options.suffix)
print 'Copying from '+orig_dir+' to \n'+new_dir
if (new_dir == orig_dir):
print 'Error: New and old directories are the same. Exiting'
sys.exit(1)

#copy files to new directory
os.system('mkdir -p '+new_dir+'/timing/checkpoints')
Expand All @@ -60,34 +75,121 @@
for s in myinput:
if ('drv_in' in f and 'stop_n' in s and int(options.nyears) > 0):
s_out = ' stop_n = '+str(options.nyears)+'\n'
elif ('drv_in' in f and 'lnd_ntasks' in s):
np = int(s.split()[2])
s_out = s
elif ('drv_in' in f and 'restart_n' in s and int(options.nyears) > 0):
s_out = ' restart_n = '+str(options.nyears)+'\n'
elif ('lnd_in' in f and 'finidat =' in s and int(options.finyr) > 0):
year_orig = str((s.split('.')[-2:-1])[0])[0:4]
year_new = str(10000+int(options.finyr))[1:]
s_out = s.replace('.clm2.r.'+year_orig, '.clm2.r.'+year_new)
s_out = s_out.replace(options.site_orig, options.site_new)
if (options.suffix != ''):
s_out = s_out.replace(casename,casename+'_'+options.suffix)
elif ('lnd_in' in f and "hist_nhtfrq =" in s and \
int(options.spin_cycle) > 0):
nhtfrq = str(-8760*int(options.spin_cycle))
if ('ad_spinup' in new_dir):
s_out = 'hist_nhtfrq = '+nhtfrq+','+nhtfrq+'\n'
else:
s_out = 'hist_nhtfrq = '+nhtfrq+'\n'
elif ('lnd_in' in f and "flanduse_timeseries =" in s and \
options.nolanduse == True):
s_out = " flanduse_timeseries = ''\n"
elif ('lnd_in' in f and "do_transient_pfts" in s and \
options.nolanduse == True):
s_out = "do_transient_pfts = .false.\n"
elif ('lnd_in' in f and "do_harvest" in s and \
options.nolanduse == True):
s_out = "do_harvest = .false.\n"
elif ('lnd_in' in f and "co2_file =" in s and \
options.noco2 == True):
s_out = s.replace('.nc','_CON.nc')
elif ('lnd_in' in f and "stream_fldfilename_ndep" in s and \
options.nondep == True):
s_out = s.replace('.nc','_CON.nc')
elif ('diri =' not in s):
s_out = s.replace(options.site_orig, options.site_new)
if (options.suffix != ''):
s_out = s_out.replace(casename,casename+'_'+options.suffix)
elif ('diri' in s and 'lnd' in f):
exedir = s.split()[2][1:-4]
print exedir
s_out = s
else:
s_out = s
myoutput.write(s_out)
myoutput.close()
myinput.close()
os.system(' mv '+new_dir+'/'+f+'.tmp '+new_dir+'/'+f)
#Assume makepointdata has been run to generate surface and domain data
os.system('cp temp/surfdata.nc '+new_dir)
os.system('cp temp/domain.nc '+new_dir)
if (os.path.exists(orig_dir+'/surfdata.nc')):
os.system('cp '+orig_dir+'/surfdata.nc '+new_dir)
else:
os.system('cp temp/surfdata.nc '+new_dir)
if (os.path.exists(orig_dir+'/domain.nc')):
os.system('cp '+orig_dir+'/domain.nc '+new_dir)
else:
os.system('cp temp/domain.nc '+new_dir)


os.system('pwd')
if ('20TR' in options.casename):
if ('20TR' in options.casename and not options.nolanduse):
os.system('cp temp/*pftdyn*.nc '+new_dir)


#if a global file exists, modify
if (os.path.exists('temp/global_'+options.casename+'_0.pbs') and options.suffix != ''):
file_in = open('temp/global_'+options.casename+'_0.pbs','r')
file_out =open('temp/global_'+options.casename+'_'+options.suffix+'.pbs','w')
mpicmd = 'mpirun -np '+str(np)
if ('cades' in options.machine):
mpicmd = '/software/dev_tools/swtree/cs400_centos7.2_pe2016-08/openmpi/1.10.3/centos7.2_gnu5.3.0/bin/mpirun'+ \
' -np '+str(np)+' --hostfile $PBS_NODEFILE '
elif (('titan' in options.machine or 'eos' in options.machine) and int(options.ninst) == 1):
mpicmd = 'aprun -n '+str(np)
elif ('cori' in options.machine or 'edison' in options.machine):
mpicmd = 'srun -n '+str(np)

for s in file_in:
if "#" in s:
file_out.write(s)
file_out.write('\n\n')
file_out.write('cd '+new_dir+'\n')
file_out.write(mpicmd+' '+exedir+'/e3sm.exe\n')
file_in.close()
file_out.close()
print "Submitting the job:"
if ('cori' in options.machine or 'edison' in options.machine):
os.system('sbatch temp/global_'+options.casename+'_'+options.suffix+'.pbs')
else:
os.system('qsub temp/global_'+options.casename+'_'+options.suffix+'.pbs')

#if an ensemble script exists, make a copy for this site
if (options.site_orig != ''):
case_prefix = options.casename.split(options.site_orig)[0][:-1]
ens_fname = 'scripts/'+case_prefix+'/ensemble_run_'+options.casename.replace(options.site_orig, \
options.site_new)+'.pbs'

if os.path.exists('scripts/'+case_prefix+'/ensemble_run_'+options.casename+'.pbs'):
os.system('cp scripts/'+case_prefix+'/ensemble_run_'+options.casename+'.pbs '+ens_fname)
myfile = open(ens_fname,'r')
myfile_out = open(ens_fname+'_temp','w')

for s in myfile:
args = s.split(' ')
if ('--case' in args):
caseind = args.index('--case')
print caseind
args[caseind+1] = args[caseind+1].replace(options.site_orig,options.site_new)
siteind = args.index('--site')
args[siteind+1] = args[siteind+1].replace(options.site_orig,options.site_new)
sout = " ".join(args)
myfile_out.write(sout)
else:
myfile_out.write(s.replace(options.site_orig,options.site_new))
myfile.close()
myfile_out.close()
os.system('mv '+ens_fname+'_temp '+ens_fname)

6 changes: 3 additions & 3 deletions ensemble_copy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

import netcdf_functions as nffun
import netcdf4_functions as nffun
import os, sys, csv, time, math, numpy
from optparse import OptionParser

Expand Down Expand Up @@ -175,10 +175,10 @@
elif (parm_indices[pnum] == 0):
param = parm_values[pnum]
else:
param[:] = parm_values[pnum]
param[...] = parm_values[pnum]
ierr = nffun.putvar(myfile, p, param)
if ('fr_flig' in p):
param=nffun.getvar(myfile, 'fr_fcel')
param[:]=1.0-parm_values[pnum]-parm_values[pnum-1]
param[...]=1.0-parm_values[pnum]-parm_values[pnum-1]
ierr = nffun.putvar(myfile, 'fr_fcel', param)
pnum = pnum+1
8 changes: 7 additions & 1 deletion ensemble_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,13 @@ def putvar(fname, varname, varvals):
if (isfirstcase):
exedir = os.path.abspath(orig_dir+'/../bld/')
if (options.norun == False):
os.system(exedir+'/acme.exe > ccsm_log.txt')
if os.path.isfile(exedir+'/acme.exe'):
os.system(exedir+'/acme.exe > acme_log.txt')
elif os.path.isfile(exedir+'/e3sm.exe'):
os.system(exedir+'/e3sm.exe > e3sm_log.txt')
elif os.path.isfile(exedir+'/cesm.exe'):
os.system(exedir+'/cesm.exe > cesm_log.txt')

isfirstcase=False

#--------- code to post-process ensebmle member and cacluate total normalized SSE ----------
Expand Down
2 changes: 2 additions & 0 deletions global_fullrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ def get_regional_bounds(myregion):
cmd_trns = cmd_trns + ' --ilambvars'
if (options.dailyvars):
cmd_trns = cmd_trns + ' --dailyvars'
if (options.dailyrunoff):
cmd_trns = cmd_trns+' --dailyrunoff'

#transient phase 2 (CRU-NCEP only, without coupler bypass)
if ((options.cruncep or options.gswp3) and not options.cpl_bypass):
Expand Down
14 changes: 7 additions & 7 deletions makepointdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os, sys, csv, time, math
from optparse import OptionParser
import numpy
import netcdf_functions as nffun
import netcdf4_functions as nffun

parser = OptionParser()

Expand Down Expand Up @@ -269,11 +269,11 @@
xv = nffun.getvar(domainfile_new, 'xv')
yv = nffun.getvar(domainfile_new, 'yv')
area = nffun.getvar(domainfile_new, 'area')
frac = 1.0
mask = 1
frac[0] = 1.0
mask[0] = 1
if (options.site != ''):
xc= lon[n]
yc = lat[n]
xc[0] = lon[n]
yc[0] = lat[n]
xv[0][0][0] = lon[n]-resx/2
xv[0][0][1] = lon[n]+resx/2
xv[0][0][2] = lon[n]-resx/2
Expand All @@ -282,7 +282,7 @@
yv[0][0][1] = lat[n]-resy/2
yv[0][0][2] = lat[n]+resy/2
yv[0][0][3] = lat[n]+resy/2
area = resx*resy*math.pi/180*math.pi/180
area[0] = resx*resy*math.pi/180*math.pi/180
ierr = nffun.putvar(domainfile_new, 'xc', xc)
ierr = nffun.putvar(domainfile_new, 'yc', yc)
ierr = nffun.putvar(domainfile_new, 'xv', xv)
Expand Down Expand Up @@ -400,7 +400,7 @@
if (options.site != ''):
longxy[0][0] = lon[n]
latixy[0][0] = lat[n]
area = 111.2*resy*111.321*math.cos((lon[n]*resx)*math.pi/180)*resx
area[0] = 111.2*resy*111.321*math.cos((lon[n]*resx)*math.pi/180)*resx

if (not options.surfdata_grid):
pct_wetland[0][0] = 0.0
Expand Down
10 changes: 8 additions & 2 deletions manage_ensemble.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
import sys,os, time
import numpy as np
import netcdf_functions as nffun
import netcdf4_functions as nffun
import subprocess
from mpi4py import MPI
from optparse import OptionParser
Expand Down Expand Up @@ -177,7 +177,13 @@ def postproc(myvars, myyear_start, myyear_end, myday_start, myday_end, \
rundir = options.runroot+'/UQ/'+options.casename+'/g'+jobst[1:]+'/'
os.chdir(rundir)
#Run the executable
os.system(options.exeroot+'/acme.exe > acme.log')
exedir = options.exeroot
if os.path.isfile(exedir+'/acme.exe'):
os.system(exedir+'/acme.exe > acme_log.txt')
elif os.path.isfile(exedir+'/e3sm.exe'):
os.system(exedir+'/e3sm.exe > e3sm_log.txt')
elif os.path.isfile(exedir+'/cesm.exe'):
os.system(exedir+'/cesm.exe > cesm_log.txt')
if (do_postproc):
ierr = postproc(myvars, myyear_start, myyear_end, myday_start, \
myday_end, myavg_pd, myfactor, myoffset, myjob, \
Expand Down
18 changes: 18 additions & 0 deletions netcdf4_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#Python utilities for reading and writing variables to a netcdf file
# using Scientific Python OR scipy, whichever available

def getvar(fname, varname):
from netCDF4 import Dataset
nffile = Dataset(fname,"r")
varvals = nffile.variables[varname][:]
nffile.close()
return varvals

def putvar(fname, varname, varvals):
from netCDF4 import Dataset
import numpy as np
nffile = Dataset(fname,"a")
nffile.variables[varname][...] = varvals
nffile.close()
ierr = 0
return ierr
Binary file added netcdf4_functions.pyc
Binary file not shown.
5 changes: 4 additions & 1 deletion netcdf_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def putvar(fname, varname, varvals):
if (usescipy):
nffile = netcdf.netcdf_file(fname,"a",mmap=False)
var = nffile.variables[varname]
var[:] = varvals
if (len(varvals) > 1):
var[:] = varvals
else:

nffile.close()
else:
nffile = netcdf.NetCDFFile(fname,"a")
Expand Down
Loading

0 comments on commit 03bd485

Please sign in to comment.