forked from dmricciuto/OLMT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjust_restart.py
executable file
·118 lines (106 loc) · 5.56 KB
/
adjust_restart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env/python
import os, sys, csv, time, math
from optparse import OptionParser
import netcdf4_functions as nffun
import glob
parser = OptionParser()
parser.add_option('--rundir', dest='rundir', default="", \
help = 'location of run directory')
parser.add_option('--casename', dest='casename', default="", \
help='Name of case')
parser.add_option('--restart_year', dest='restart_year', default="", \
help='Year of restart file to modify')
parser.add_option('--BGC', dest='bgc', default=False, action="store_true", \
help='Flag to set for BGC compsets')
parser.add_option('--harvest', dest='harvest', default=False, action="store_true", \
help='Add a 95% above ground harvest')
parser.add_option('--model_name', dest='model_name', default="clm2", \
help='Model name used in restart file (clm2 or elm)')
(options,args)=parser.parse_args()
#The purpose of this code is to replace the restart value with the average value of the last met cycle
#This will bring the accelerated pools more into line with the expected results.
casename = options.casename
if (options.restart_year == ''):
#if restart_year not provided, take the last existing restart file
restart_file = glob.glob(options.rundir+'/'+casename+'.'+options.model_name+'.r.*.nc')
if (len(restart_file) > 1):
restart_file_last = restart_file[-1]
else:
restart_file_last = restart_file[0]
year = int(restart_file_last[-19:-15])
else:
year = int(options.restart_year)
if ('BGC' in casename):
options.bgc = True
fname_restart = options.rundir+'/'+casename+'.'+options.model_name+'.r.'+str(10000+year)[1:]+ \
'-01-01-00000.nc'
fname_hist = options.rundir+'/'+casename+'.'+options.model_name+'.h1.'+str(10000+year)[1:]+ \
'-01-01-00000.nc'
#save original restart file
if (os.path.isfile(fname_restart+'.orig')):
os.system('cp '+fname_restart+'.orig '+fname_restart)
else:
os.system('cp '+fname_restart+' '+fname_restart+'.orig')
os.system('chmod a-w '+fname_restart+'.orig')
#Accelerated pools: Coarse woody debris, dead stem, coarse root, soil3 and soil4 C and N
var_names = ['DEADSTEMC', 'DEADSTEMN', 'DEADSTEMP', 'DEADCROOTC', 'DEADCROOTN', 'DEADCROOTP']
if (options.bgc):
var_names2d = ['CWDC_vr', 'CWDN_vr', 'CWDP_vr', 'SOIL3C_vr', 'SOIL3N_vr', 'SOIL3P_vr', 'SOIL2C_vr', \
'SOIL2N_vr', 'SOIL2P_vr']
else:
var_names2d = ['CWDC_vr', 'CWDN_vr', 'CWDP_vr', 'SOIL4C_vr', 'SOIL4N_vr', 'SOIL4P_vr', 'SOIL3C_vr', \
'SOIL3N_vr', 'SOIL3P_vr']
if (options.harvest):
var_names_harvest = ['DEADSTEMC','DEADSTEMN','DEADSTEMP','LIVESTEMN','LIVESTEMC','LIVESTEMP', \
'LEAFC', 'LEAFN', 'LEAFP', 'DEADSTEMC_STORAGE', 'DEADSTEMN_STORAGE', \
'DEADSTEMP_STORAGE', 'LIVESTEMC_STORAGE', 'LIVESTEMN_STORAGE', \
'LIVESTEMP_STORAGE', 'LEAFC_STORAGE', 'LEAFN_STORAGE', 'LEAFP_STORAGE', \
'FROOTC_STORAGE', 'FROOTC', 'FROOTN_STORAGE', 'FROOTN', 'FROOTP_STORAGE', \
'FROOTP', 'LIVECROOTC', 'LIVECROOTC_STORAGE', 'LIVECROOTN', 'LIVECROOTN_STORAGE', \
'LIVECROOTP', 'LIVECROOTP_STORAGE', 'DEADCROOTC', 'DEADCROOTC_STORAGE', \
'DEADCROOTN', 'DEADCROOTN_STORAGE', 'DEADCROOTP', 'DEADCROOTP_STORAGE', 'XSMRPOOL', \
'XSMRPOOL_RECOVER']
if (options.harvest):
for v in range(0,len(var_names_harvest)):
rest_vals = nffun.getvar(fname_restart, var_names_harvest[v].lower())
#Loop through all valid values in the restart file.
n_rest = len(rest_vals)
for i in range(0,n_rest):
rest_vals[i] = rest_vals[i]*0.05
if (var_names_harvest[v] == 'LEAFC'):
rest_vals[i] = 0.33/0.03
print(rest_vals[i])
elif (var_names_harvest[v] == 'FROOTC'):
rest_vals[i] = 0.33/0.03*0.666
elif (var_names_harvest[v] == 'LEAFN'):
rest_vals[i] = 0.33/0.03/25.0
elif (var_names_harvest[v] == 'FROOTN'):
rest_vals[i] = 0.33/0.03/42.0
ierr = nffun.putvar(fname_restart, var_names_harvest[v].lower(), rest_vals)
else:
for v in range(0,len(var_names)):
hist_vals = nffun.getvar(fname_hist, var_names[v])
rest_vals = nffun.getvar(fname_restart, var_names[v].lower())
#Loop through all valid values in the restart file.
n_rest = len(rest_vals)
for i in range(0,n_rest):
if (float(rest_vals[i]) > 0.0 and float(hist_vals[0][i]) < 1.0e10 and float(hist_vals[0][i]) > 0.001):
rest_vals[i] = hist_vals[0][i]
ierr = nffun.putvar(fname_restart, var_names[v].lower(), rest_vals)
#get a single, non-depth dependent variable to count # of columns
rest_vals = nffun.getvar(fname_restart, 'fpg')
n_rest = len(rest_vals)
for v in range(0,len(var_names2d)):
hist_vals = nffun.getvar(fname_hist, var_names2d[v])
rest_vals = nffun.getvar(fname_restart, var_names2d[v].lower())
#Loop through all valid values in the restart file.
for i in range(0,n_rest):
for j in range(0,10):
if (float(rest_vals[i][j]) > 0.0 and float(hist_vals[0][j][i]) < 1.0e10 and float(hist_vals[0][j][i]) > 1e-10):
rest_vals[i][j] = hist_vals[0][j][i]
ierr = nffun.putvar(fname_restart, var_names2d[v].lower(), rest_vals)
#Remove negative Ppool values
#os.system("ncap2 -O -s 'ppool=0.2*npool' "+fname_restart+" "+fname_restart)
#os.system("ncap2 -O -s 'defdim(\"cohort\",5663)' -s 'defdim(\"levurb\",5)' -s " \
# +"'defdim(\"string_length\",64)' -s 'defdim(\"levtrc\",10)' " + \
# fname_restart+" "+fname_restart)