for v in myvars: ndays_total = 0 output = [] n_years = myyear_end[index]-myyear_start[index]+1 npy=1 for y in range(myyear_start[index],myyear_end[index]+1): if (mypft[index] <= 0 or 'PFT' in v): fname = rundir+case+'.'+options.model_name+'.h0.'+str(10000+y)[1:]+'-01-01-00000.nc' myindex = max(0,mypft[index]) hol_add = 1 else: fname = rundir+case+'.'+options.model_name+'.h1.'+str(10000+y)[1:]+'-01-01-00000.nc' myindex = mypft[index] hol_add = 17 if (os.path.exists(fname)): mydata = nffun.getvar(fname,v) if (len(mydata) < 10): npy = 1 elif (len(mydata) >= 365): #does not currently allow hourly npy = 365 else: print(fname) mydata = np.zeros([npy,34], np.float)+np.NaN #get output and average over days/years n_days = myday_end[index]-myday_start[index]+1 ndays_total = ndays_total + n_days #get number of timesteps per output file if (npy == 365): for d in range(myday_start[index]-1,myday_end[index]): if ('US-SPR' in case):
def calc_costfucntion(constraints, thisjob, runroot, case): #Calculate cost function value (SSE) given the data constraints in the provided directory rundir = runroot+'/UQ/'+case+'/g'+str(100000+thisjob)[1:]+'/' sse = 0 os.system('rm '+rundir+case+'.clm2*_constraint.nc') myoutput = open(rundir+'myoutput_sse.txt','w') for filename in os.listdir(constraints): if (not os.path.isdir(filename)): myinput = open(constraints+'/'+filename,'r') myvarname = filename.split('.')[0] #model variable is filename #code to deal with special variables and/or aggregation #------------- lnum = 0 #line number year = 0 for s in myinput: if (lnum == 0): header = s.split() elif (len(header) == len(s.split())): hnum = 0 PFT=-1 #default: don't use PFT-specific info # if specified, use h1 file (PFT-specific) doy=-1 #default: annual average month=-1 #default: don't use monthly data depth=-1 unc = -999 for h in header: if (h.lower() == 'year'): year_last = year year = int(s.split()[hnum]) if (h.lower() == 'doy'): doy = int(s.split()[hnum]) if (h.lower() == 'month'): month = int(s.split()[hnum]) if (h.lower() == 'pft'): PFT = int(s.split()[hnum]) if (h.lower() == 'value'): value = float(s.split()[hnum]) if (h.lower() == 'depth'): depth = float(s.split()[hnum]) if ('unc' in h.lower()): unc = float(s.split()[hnum]) hnum = hnum+1 #get the relevant variable/dataset #Assumes annual file with daily output if (PFT == -1): myfile = rundir+case+'.clm2.h0.'+str(year)+'-01-01-00000_constraint.nc' myfileo = rundir+case+'.clm2.h0.'+str(year)+'-01-01-00000.nc' else: myfile = rundir+case+'.clm2.h1.'+str(year)+'-01-01-00000_constraint.nc' myfileo = rundir+case+'.clm2.h1.'+str(year)+'-01-01-00000.nc' #post processing of model output with nco to match constraining variables if (not os.path.isfile(myfile)): os.system('cp '+myfileo+' '+myfile) if ('h1' in myfile and ('STEMC' in myvarname or 'AGBIOMASS' in myvarname)): os.system('ncap2 -3 -s "STEMC=DEADSTEMC+LIVESTEMC" '+myfile+' '+myfile+'.tmp') os.system('mv '+myfile+'.tmp '+myfile) os.system('ncap2 -3 -s "AGBIOMASS=DEADSTEMC+LIVESTEMC+LEAFC" '+myfile+' '+myfile+'.tmp') os.system('mv '+myfile+'.tmp '+myfile) if ('h0' in myfile and 'WTHT' in myvarname): #Water table height relative to hollow bottoms (7.5cm below hollow gridcell mean) os.system('ncap2 -3 -s "WTHT=(ZWT*-1+0.225)*1000 " '+myfile+' '+myfile+'.tmp') os.system('mv '+myfile+'.tmp '+myfile) myvals = nffun.getvar(myfile, myvarname) if (doy > 0 and value > -900): #Daily constraint if (myvarname == 'WTHT'): unc = 50.0 #no uncertainty given for water table height. if (PFT > 0): #PFT-specific constraints (daily) if (myvarname == 'AGBIOMASS' and PFT == 3): #Both tree types - Larch and spruce, hummock+hollow model_val = (myvals[doy,PFT-1]*0.25+myvals[doy,PFT]*0.25)*0.75 \ +(myvals[doy,PFT+16]*0.25+myvals[doy,PFT+17]*0.25)*0.25 else: #use hummock+hollow model_val = myvals[doy,PFT-1]*0.25*0.75 + myvals[doy,PFT+16]*0.25*0.25 if (unc < 0): unc = value*0.25 #default uncertainty set to 25% sse = sse + ((model_val-value) /unc)**2 elif (depth > 0): #depth-specific constraint in cm (relative to hollow) layers = [0,1.8,4.5,9.1,16.6,28.9,49.3,82.9,138.3,229.6,343.3] for l in range(0,10): if (depth >= layers[l] and depth < layers[l+1]): thislayer = l model_val = myvals[doy,thislayer,1] #Hollow sse = sse + ((model_val-value) / unc )**2 else: #Column-level constraint (daily) #Water table, column-level (no PFT info), use hummock only model_val = myvals[doy,0] sse = sse + ((model_val-value) / unc )**2 elif (value > -900): #Monthly or annual constraint daysm=[0,31,59,90,120,151,181,212,243,273,304,334,365] if (month > 0): lbound = daysm[month-1] ubound = daysm[month] else: lbound = 0 ubound = 365 #model_val = sum(myvals[0:365,PFT-1]*0.25*0.75)*24*3600 + \ # sum(myvals[0:365,PFT+16]*0.25*0.25)*24*3600 if (PFT <= 0): model_val = sum(myvals[lbound:ubound,0]) / (ubound - lbound) else: model_val = sum(myvals[lbound:ubound,PFT-1]) / (ubound - lbound) sse = sse + ((model_val-value) / unc )**2 myoutput.write(str(myvarname)+' '+str(year)+' '+str(month)+' '+str(PFT)+' '+ \ str(model_val)+' '+str(value)+' '+str(unc)+' '+str(sse)+'\n') lnum = lnum+1 myoutput.close() return sse
os.system(' mv ' + ens_dir + '/' + f + '.tmp ' + ens_dir + '/' + f) pnum = 0 CNP_parms = ['ks_sorption', 'r_desorp', 'r_weather', 'r_adsorp', 'k_s1_biochem', 'smax', 'k_s3_biochem', \ 'r_occlude', 'k_s4_biochem', 'k_s2_biochem'] fates_seed_zeroed = [False, False] for p in parm_names: if ('INI' in p): if ('BGC' in casename): scalevars = ['soil3c_vr', 'soil3n_vr', 'soil3p_vr'] else: scalevars = ['soil4c_vr', 'soil4n_vr', 'soil4p_vr'] sumvars = ['totsomc', 'totsomp', 'totcolc', 'totcoln', 'totcolp'] for v in scalevars: myvar = nffun.getvar(finidat_file_new, v) myvar = parm_values[pnum] * myvar ierr = nffun.putvar(finidat_file_new, v, myvar) elif (p == 'lai'): myfile = surffile param = nffun.getvar(myfile, 'MONTHLY_LAI') param[:, :, :, :] = parm_values[pnum] ierr = nffun.putvar(myfile, 'MONTHLY_LAI', param) elif (p != 'co2'): if (p in CNP_parms): myfile = CNPfile elif ('fates' in p): myfile = fates_paramfile else: myfile = pftfile param = nffun.getvar(myfile, p)
#get corresponding 0.5x0.5 and 1.9x2.5 degree grid cells if (options.res == 'hcru_hcru'): longxy = (numpy.cumsum(numpy.ones([721])) - 1) * 0.5 latixy = (numpy.cumsum(numpy.ones([361])) - 1) * 0.5 - 90.0 elif (options.res == 'f19_f19'): longxy = (numpy.cumsum(numpy.ones([145])) - 1) * 2.5 - 1.25 latixy_centers = (numpy.cumsum(numpy.ones([96])) - 1) * (180.0 / 95) - 90.0 latixy = numpy.zeros([97], numpy.float) longxy[0] = 0 latixy[0] = -90 latixy[96] = 90 for i in range(1, 96): latixy[i] = (latixy_centers[i - 1] + latixy_centers[i]) / 2.0 else: longxy = nffun.getvar(surffile_orig, 'LONGXY') latixy = nffun.getvar(surffile_orig, 'LATIXY') xgrid_min = [] xgrid_max = [] ygrid_min = [] ygrid_max = [] for n in range(0, n_grids): if (issite): lon_bounds = [lon[n], lon[n]] lat_bounds = [lat[n], lat[n]] xgrid_min.append(-1) xgrid_max.append(-1) ygrid_min.append(-1) ygrid_max.append(-1) if ('ne' in options.res):
os.system(' mv '+ens_dir+'/'+f+'.tmp '+ens_dir+'/'+f) pnum = 0 CNP_parms = ['ks_sorption', 'r_desorp', 'r_weather', 'r_adsorp', 'k_s1_biochem', 'smax', 'k_s3_biochem', \ 'r_occlude', 'k_s4_biochem', 'k_s2_biochem'] fates_seed_zeroed=[False,False] for p in parm_names: if ('INI' in p): if ('BGC' in casename): scalevars = ['soil3c_vr','soil3n_vr','soil3p_vr'] else: scalevars = ['soil4c_vr','soil4n_vr','soil4p_vr'] sumvars = ['totsomc','totsomp','totcolc','totcoln','totcolp'] for v in scalevars: myvar = nffun.getvar(finidat_file_new, v) myvar = parm_values[pnum] * myvar ierr = nffun.putvar(finidat_file_new, v, myvar) elif (p == 'lai'): myfile = surffile param = nffun.getvar(myfile, 'MONTHLY_LAI') param[:,:,:,:] = parm_values[pnum] ierr = nffun.putvar(myfile, 'MONTHLY_LAI', param) elif (p != 'co2'): if (p in CNP_parms): myfile= CNPfile elif ('fates' in p): myfile = fates_paramfile else: myfile = pftfile param = nffun.getvar(myfile,p)
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: