降水要素
import xarray as xr
import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pymannkendall
from scipy.stats import pearsonr
from sklearn.metrics import r2_score,mean_squared_error
# 思路
# bias = obs / gcm
# gcm_downscale = gcm * bias
# 读取数据
gcm_pre = xr.open_dataset(r"G:\CMIP6\precip\ew_six_cmip_precip.nc")
obs_pre = xr.open_dataset(r"G:\Data\tp_processed\del_runnian_ERA5_tp_canjian_1985_2021_daily.nc")
# 计算多年月平均降水
obs_pre_monthlymean = obs_pre.tp.resample(time="1M").sum()
gcm_pre_monthlymean = gcm_pre.precip.resample(time="1M").sum()
obs_pre_multimonthlymean = obs_pre_monthlymean.groupby(obs_pre_monthlymean.time.dt.month).mean()
gcm_pre_multimonthlymean = gcm_pre_monthlymean.groupby(gcm_pre_monthlymean.time.dt.month).mean()
obs_nc = xr.Dataset({"precip":
(('time','lat','lon'),obs_pre_multimonthlymean.values.astype("float32"))},
coords={"time":gcm_pre_multimonthlymean.month.values,
"lat":gcm_pre_multimonthlymean.lat.values,
"lon":gcm_pre_multimonthlymean.lon.values})
gcm_nc = xr.Dataset({"precip":
(('time','lat','lon'),gcm_pre_multimonthlymean.values.astype("float32"))},
coords={"time":gcm_pre_multimonthlymean.month.values,
"lat":gcm_pre_multimonthlymean.lat.values,
"lon":gcm_pre_multimonthlymean.lon.values})
# 计算偏差
delta_pre = obs_nc.precip/gcm_nc.precip
delta_pre.to_netcdf(r"G:\CMIP6\precip\delta_hist_precip.nc")
# 降尺度
result = []
for i in range(1,13)[:]:
itmp = gcm_pre.sel(time=gcm_pre.time.dt.month==i)*delta_pre.sel(time=i)
itmp = itmp.persist()
itmp2 = xr.Dataset({"precip":
(('time','lat','lon'),itmp.precip.values.astype("float32"))},
coords={"time":itmp.time.values,
"la