下面是将网格数据插值到站点的代from pathlib import Path
import pandas as pd
import numpy as np
import netCDF4 as nc
# 读取站点信息
stations_info = pd.read_excel(r'D:MLgrid to stationsstations.xlsx', 'Sheet1', index_col=None, na_values=['NA'])
# 读取网格数据
dataset = nc.Dataset(r"D:MLgrid to stationsERA5.TEST.nc")
print(dataset)
# 经纬度
longitude = dataset.variables['longitude'][:].data
latitude = dataset.variables['latitude'][:].data
# 温度
t = dataset.variables['t'][:, :, :].data # 获取所有时次的温度数据
# 将格点范围内的站点筛选出来
lonSta, latSta = stations_info['经度'].to_numpy(), stations_info['纬度'].to_numpy()
# 定义获取最临近格点坐标索引的方法
def nearest_position(stn_lat, stn_lon, lat2d, lon2d):
difflat = stn_lat - lat2d
difflon = stn_lon - lon2d
rad = np.multiply(difflat, difflat) + np.multiply(difflon, difflon)
aa = np.where(rad == np.min(rad))
ind = np.squeeze(np.array(aa))
return tuple(ind)
# 将一维的经纬度数据网格二维化
lon2D, lat2D = np.meshgrid(longitude, latitude)
# 创建一个 DataFrame 用于存储插值数据
t_sta_nearest_df = pd.DataFrame(index=range(len(lonSta)), columns=[f'{hour:02d}h' for hour in range(24)])
# 对每个站点进行插值计算
for i in range(len(lonSta)):
t_nearest = []
for t_index in range(t.shape[0]): # 对每个时间点进行操作
indexSta = nearest_position(latSta[i], lonSta[i], lat2D, lon2D)
jSta, iSta = indexSta[0], indexSta[1]
t_nearest.append(t[t_index, jSta, iSta]) # 将当前时间点的结果添加到列表中
t_sta_nearest_df.loc[i] = t_nearest
# 添加时间标题行
t_sta_nearest_df.columns = [f'{hour:02d}h' for hour in range(24)]
# 将插值数据添加到站点信息 DataFrame 中
stations_info = pd.concat([stations_info, t_sta_nearest_df], axis=1)
# 将数据保存为新的xlsx文件
stations_info.to_excel('D:/ML/grid to stations/weather_station_data.xlsx', sheet_name='Sheet1', index=False)
from pathlib import Path
import pandas as pd
import numpy as np
import netCDF4 as nc
from scipy.interpolate import griddata
# 读取站点信息
stations_info = pd.read_excel(r'D:MLgrid to stationsstations.xlsx', 'Sheet1', index_col=None, na_values=['NA'])
# 读取网格数据
dataset = nc.Dataset(r"D:MLgrid to stationsERA5.TEST.nc")
# 经纬度
longitude = dataset.variables['longitude'][:].data
latitude = dataset.variables['latitude'][:].data
# 温度
t = dataset.variables['t'][:, :, :].data # 获取所有时次的温度数据
# 将格点范围内的站点筛选出来
lonSta, latSta = stations_info['经度'].to_numpy(), stations_info['纬度'].to_numpy()
# 将一维的经纬度数据网格化
lon2D, lat2D = np.meshgrid(longitude, latitude)
# 创建一个 DataFrame 用于存储插值数据
t_sta_nearest_df = pd.DataFrame(index=range(len(lonSta)), columns=[f'{hour:02d}h' for hour in range(24)])
# 对每个站点进行插值计算
for i in range(len(lonSta)):
t_nearest = []
for t_index in range(t.shape[0]): # 对每个时间点进行操作
t_values = t[t_index, :, :].flatten() # 获取当前时间点的所有温度值
grid_points = np.vstack((lon2D.flatten(), lat2D.flatten())).T # 网格坐标
station_point = np.array([[lonSta[i], latSta[i]]]) # 站点坐标
t_interp = griddata(grid_points, t_values, station_point, method='cubic') # 双三次插值
t_nearest.append(t_interp[0]) # 将插值结果添加到列表中
t_sta_nearest_df.loc[i] = t_nearest
# 添加时间标题行
t_sta_nearest_df.columns = [f'{hour:02d}h' for hour in range(24)]
# 将插值数据添加到站点信息 DataFrame 中
stations_info = pd.concat([stations_info, t_sta_nearest_df], axis=1)
# 将数据保存为新的xlsx文件
stations_info.to_excel('D:/ML/grid to stations/weather_station_data_SSC.xlsx', sheet_name='Sheet1', index=False)
#printf("hello world!")删掉
原创文章,作者:guozi,如若转载,请注明出处:https://www.sudun.com/ask/78284.html