def interp(inputvar,inputz,outputz,vartype="data",inputt=None,inputp=None): """Interpolate an input 3D dataset from an input 3D elevation grid to the output 3D grid if vartype is set to "p" pressure, it will perform a more sophisticated interpolation, but that requires REAL temperature data if vartype is set to "t" temperature, it will convert to potential temperature before converting, but that requires pressure data """ # for pressure interpolation, use a temperature dependant vertical shift if vartype=="p": if inputt==None: raise(ValueError("Need Temperature to interpolate pressure")) return interp_pressure(inputvar,inputz,outputz,inputt) # if we were given real temperature, convert to potential temperature if vartype=="t": if inputp==None: raise(ValueError("Need Pressure data to interpolate temperature")) else: inputvar=inputvar*units.exner(inputp) output=_vinterp(inputvar,inputz,outputz) if vartype=="t": inputvar=inputvar/units.exner(inputp) print("WARNING!! Returning potential temperature, not REAL temperature") print(" Use interpolated pressure to convert back to REAL temperature") return output
def interp_multivar(inputvar,outputvar,timestep,inputz,outputz,vartype="data",inputt=None,inputp=None): """Interpolate an input 3D dataset from an input 3D elevation grid to the output 3D grid if vartype is set to "p" pressure, it will perform a more sophisticated interpolation, but that requires REAL temperature data if vartype is set to "t" temperature, it will convert to potential temperature before converting, but that requires pressure data """ interp_keys=[] for vartype in inputvar.keys(): # for pressure interpolation, use a temperature dependant vertical shift if vartype=="p": if inputt==None: raise(ValueError("Need Temperature to interpolate pressure")) outputvar[vartype][timestep]=interp_pressure(inputvar[vartype][timestep], inputz, outputz, inputt) elif vartype!="z": interp_keys.append(vartype) # if we were given real temperature, convert to potential temperature if vartype=="t": if inputp==None: raise(ValueError("Need Pressure data to interpolate temperature")) else: inputvar[vartype][timestep]=inputvar[vartype][timestep]*units.exner(inputp) _vinterp_multivar(inputvar,outputvar,timestep,inputz,outputz,interp_keys) if "t" in inputvar.keys(): inputvar["t"][timestep]=inputvar["t"][timestep]/units.exner(inputp) print("WARNING!! Returning potential temperature, not REAL temperature") print(" Use interpolated pressure to convert back to REAL temperature")
def load_erai_means(wind_option): """docstring for load_erai_means""" eraid="erai/" varlist=["p","rh","ta","ua","va","z"] if (wind_option=="nowind"): varlist=["p","rh","ta","z"] outputdata=[] month_mid_point_doy=(start_day_per_month[1:]+np.array(start_day_per_month[:-1]))*0.5 for month in range(1,13): curoutput=Bunch(doy=month_mid_point_doy[month-1]) for v in varlist: if v=="ta": ta=mygis.read_nc(eraid+"regridded_ERAi_to_cesm_month{0:02}.nc".format(month),v).data else: curoutput[v]=mygis.read_nc(eraid+"regridded_ERAi_to_cesm_month{0:02}.nc".format(month),v).data curoutput["theta"] = ta / units.exner(curoutput.p) # erai is probably "upside down" so reverse the vertical dimension of all variables if curoutput.p[0,0,0]<curoutput.p[-1,0,0]: for v in curoutput.keys(): if v!="doy": curoutput[v]=curoutput[v][::-1,:,:] outputdata.append(curoutput) return outputdata
def load_erai_means(wind_option): """docstring for load_erai_means""" eraid = "erai/" varlist = ["p", "rh", "ta", "ua", "va", "z"] if (wind_option == "nowind"): varlist = ["p", "rh", "ta", "z"] outputdata = [] month_mid_point_doy = (start_day_per_month[1:] + np.array(start_day_per_month[:-1])) * 0.5 for month in range(1, 13): curoutput = Bunch(doy=month_mid_point_doy[month - 1]) for v in varlist: if v == "ta": ta = mygis.read_nc( eraid + "regridded_ERAi_to_cesm_month{0:02}.nc".format(month), v).data else: curoutput[v] = mygis.read_nc( eraid + "regridded_ERAi_to_cesm_month{0:02}.nc".format(month), v).data curoutput["theta"] = ta / units.exner(curoutput.p) # erai is probably "upside down" so reverse the vertical dimension of all variables if curoutput.p[0, 0, 0] < curoutput.p[-1, 0, 0]: for v in curoutput.keys(): if v != "doy": curoutput[v] = curoutput[v][::-1, :, :] outputdata.append(curoutput) return outputdata
def vinterp_q(file_name=None): """interpolate all variables to a common z level""" print("Interpolating quantities") if file_name != None: file_search = file_name qvarlist = ["qv", "theta", "p", "u", "v"] qdata = Bunch() print("Loading data") t0 = time.time() for varname in qvarlist: print(varname) qdata[varname] = mygis.read_files(file_search, varname, axis=0) print("z") qdata["z"] = mygis.read_files(file_search, "z", axis=0) mean_z = mygis.read_nc("annual_mean_z.nc", "z").data print(" Reading data took {0:6.2f} seconds".format((time.time() - t0))) t0 = time.time() temperature = qdata.theta * units.exner(qdata.p) print(" Temperature conversion took {0:6.2f} seconds".format( (time.time() - t0))) t0 = time.time() print("Computing RH") qdata["rh"] = compute_rh(qdata, temperature) qvarlist.append("rh") print(" RH calculation took {0:6.2f} seconds".format((time.time() - t0))) print("Interpolating") qout = Bunch() for varname in qvarlist: qout[varname] = np.zeros(qdata[varname].shape) t0 = time.time() nsteps = qdata.z.shape[0] for i in range(nsteps): vertical_interp.interp_multivar(qdata, qout, i, qdata.z[i], mean_z, vartype=varname, inputt=temperature[i], inputp=qdata.p[i]) print(" Interpolation took {0:6.2f} seconds per timestep".format( (time.time() - t0) / nsteps)) print("Writing data") write_interpolated_6hrly(qout, mean_z, file_name)
def interp_era_to_cesm(erai,cesm): """docstring for interp_era_to_cesm""" print("Monthly vertical interpolation") varlist=erai[0].keys() #["p","rh","theta","ua","va"] mean_z=cesm[0].z inputp=np.zeros(erai[0].p.shape) for i in range(len(erai)): print("Month {}".format(i+1)) inputp[:]=erai[i].p[:] temperature=erai[i].theta * units.exner(inputp) for v in varlist: if (v!="doy") and (v!="z"): erai[i][v]=vertical_interp.interp(erai[i][v], erai[i]["z"], mean_z, vartype=v, inputt=temperature, inputp=inputp) erai[i]["z"]=mean_z return erai
def vinterp_q(file_name=None): """interpolate all variables to a common z level""" print("Interpolating quantities") if file_name!=None: file_search=file_name qvarlist=["qv","theta","p","u","v"] qdata=Bunch() print("Loading data") t0=time.time() for varname in qvarlist: print(varname) qdata[varname]=mygis.read_files(file_search,varname,axis=0) print("z") qdata["z"]=mygis.read_files(file_search,"z",axis=0) mean_z=mygis.read_nc("annual_mean_z.nc","z").data print(" Reading data took {0:6.2f} seconds".format((time.time()-t0))) t0=time.time() temperature=qdata.theta*units.exner(qdata.p) print(" Temperature conversion took {0:6.2f} seconds".format((time.time()-t0))) t0=time.time() print("Computing RH") qdata["rh"]=compute_rh(qdata,temperature) qvarlist.append("rh") print(" RH calculation took {0:6.2f} seconds".format((time.time()-t0))) print("Interpolating") qout=Bunch() for varname in qvarlist: qout[varname]=np.zeros(qdata[varname].shape) t0=time.time() nsteps=qdata.z.shape[0] for i in range(nsteps): vertical_interp.interp_multivar(qdata,qout,i, qdata.z[i], mean_z,vartype=varname, inputt=temperature[i],inputp=qdata.p[i]) print(" Interpolation took {0:6.2f} seconds per timestep".format((time.time()-t0)/nsteps)) print("Writing data") write_interpolated_6hrly(qout,mean_z,file_name)
def apply_bias_correction(wind_option): """docstring for apply_bias_correction""" varlist = ["theta", "p", "u", "v", "rh"] if wind_option == "nowind": print("Skipping U and V bias correction.") biases = load_biases(wind_option) current_files = glob.glob("cesm_*.nc") current_files.sort() for f in current_files: print("Bias Correcting : " + f) output_data = mygis.Dataset(f, mode="a") for v in varlist: print(" Variable : " + v) try: d = mygis.read_nc("vinterpolated/" + v + "_" + f, v).data except: d = mygis.read_nc(v + "_" + f, v).data if (wind_option == "nowind") and ((v == "u") or (v == "v")): pass if (wind_option == "meanwind") and ((v == "u") or (v == "v")): for i in range(d.shape[0]): windbias = biases[int(np.round(i / float(times_per_day)))][v] for j in range(d.shape[1]): d[i, j, ...] -= np.mean(windbias[j, ...]) else: for i in range(d.shape[0]): d[i, ...] -= biases[int(np.round(i / float(times_per_day)))][v] if v == "rh": d[d < 1e-10] = 1e-10 d[d > 100] = 100 rh = d else: output_data.variables[v][:] = d p = output_data.variables["p"][:] / 100.0 # convert to mb t = output_data.variables["theta"][:] * units.exner(p) output_data.variables["qv"][:] = units.rh2sh(t, p, rh) output_data.variables["z"] = biases[0]["z"][np.newaxis, :, :, :] output_data.close()
def apply_bias_correction(wind_option): """docstring for apply_bias_correction""" varlist=["theta","p","u","v","rh"] if wind_option=="nowind": print("Skipping U and V bias correction.") biases=load_biases(wind_option) current_files=glob.glob("cesm_*.nc") current_files.sort() for f in current_files: print("Bias Correcting : "+f) output_data=mygis.Dataset(f,mode="a") for v in varlist: print(" Variable : "+v) try: d=mygis.read_nc("vinterpolated/"+v+"_"+f,v).data except: d=mygis.read_nc(v+"_"+f,v).data if (wind_option=="nowind") and ((v=="u") or (v=="v")): pass if (wind_option=="meanwind") and ((v=="u") or (v=="v")): for i in range(d.shape[0]): windbias=biases[int(np.round(i/float(times_per_day)))][v] for j in range(d.shape[1]): d[i,j,...]-=np.mean(windbias[j,...]) else: for i in range(d.shape[0]): d[i,...]-=biases[int(np.round(i/float(times_per_day)))][v] if v=="rh": d[d<1e-10]=1e-10 d[d>100]=100 rh=d else: output_data.variables[v][:]=d p=output_data.variables["p"][:]/100.0 # convert to mb t=output_data.variables["theta"][:] * units.exner(p) output_data.variables["qv"][:]=units.rh2sh(t,p,rh) output_data.variables["z"]=biases[0]["z"][np.newaxis,:,:,:] output_data.close()
def interp_era_to_cesm(erai, cesm): """docstring for interp_era_to_cesm""" print("Monthly vertical interpolation") varlist = erai[0].keys() #["p","rh","theta","ua","va"] mean_z = cesm[0].z inputp = np.zeros(erai[0].p.shape) for i in range(len(erai)): print("Month {}".format(i + 1)) inputp[:] = erai[i].p[:] temperature = erai[i].theta * units.exner(inputp) for v in varlist: if (v != "doy") and (v != "z"): erai[i][v] = vertical_interp.interp(erai[i][v], erai[i]["z"], mean_z, vartype=v, inputt=temperature, inputp=inputp) erai[i]["z"] = mean_z return erai