UPDATE # 1:
我调整你的源代码与坐标epsg: 3857格式(它是正确地绘制在QGIS matplotlib但无形的):
进口numpy从scipy.interpolate np进口griddata进口xarray xr进口熊猫一样pd进口rioxarray进口netCDF4 nc4 matplotlib进口。pyplot plt从美观。几何进口点导入geopandas ....加仑日# - - -输入数据lat =(50.1, 50.2, 50.3, 50.4, 50.5, 62]朗= (8.1,8.2,8.3,8.4,8.5,12]temp =[1、2、3、4、5、6] #放入熊猫Dataframe df = pd。DataFrame({“纬度”:纬度,经度:经度,“临时”:临时})#准备几何pointShp =[一点(x, y) x, y在zip (df。经度,df.latitude)] pointGpd =加仑日。GeoDataFrame (df、几何= pointShp crs = EPSG: 4326) # Reproject point3857 = pointGpd.to_crs (EPSG: 3857) point3857 [x] = point3857。应用(λx: x.geometry.centroid。x轴= 1)point3857 [y] = point3857。应用(λx: x.geometry.centroid。y轴= 1)df = point3857 [[‘x’,‘y’,‘临时’]]朗=列表(df (' x ']) lat =列表(df [y]) temp =列表(df(临时的))#进行源代码# - - -项目输入数据在规则的网格ξ= np.arange (min(朗),max(朗),1000)易= np.arange (min (lat)、马克斯(lat), 1000),易= np。meshgrid (xi, yi)子= np.zeros_like (xi, dtype = np.float32) * -999 #子= griddata((经度、纬度)、温度(xi, yi)方法=“线性”)我的范围(len(临时):idx = np。argmin (np。√(xi-lon[我])* * 2 + (yi-lat[我])* * 2)子(np)。unravel_index (idx xi.shape)] = temp[我]# 0换成nan值(我有看不见的像素代替黑色背景)np。(子,子= = 0,None) #——检查……= >看起来不错plt.figure (figsize =(15日7))plt.subplot plt (1、2、1)。散射(经度、纬度、临时、临时)plt.subplot plt (1、2、2)。pcolor (xi,咦,np.where (np.isnan(子),0,zi)) plt.show() # - - -打开NetCDF文件编写与nc4.Dataset(“测试。数控”、“w”,格式=“NETCDF3_CLASSIC”)作为ds: #——初始化数据集的维度dim_time = ds。createDimension('时间',0)dim_lat = ds。createDimension('lat', yi.shape[0]) dim_lon = ds.createDimension('lon', xi.shape[1]) # --- Create the corresponding variables for the dimensions time = ds.createVariable('time', np.float32, 'time') latitude = ds.createVariable('lat', np.float32, 'lat') latitude.units = ['degrees north'] latitude.axis = ['Y'] latitude.standard_name = ['latitude'] longitude = ds.createVariable('lon', np.float32, 'lon') longitude.units = ['degrees east'] longitude.axis = ['X'] longitude.standard_name = ['longitude'] # --- Fill with 1D (!) arrays of xi/yi, as the meshgrid returns 2D arrays... time[:] = 0 latitude[:] = yi[:,0] longitude[:] = xi[0,:] # --- Create a coordinate reference system crs = ds.createVariable('WGS84', 'c') crs.spatial_ref = """GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]""" # --- Ready the Temperature data field value = ds.createVariable('temperature', np.float32, ('time','lat','lon')) value.grid_mapping = 'WGS84' # the crs variable name value.grid_mapping_name = 'latitude_longitude' # --- Fill with values value[0,:,:] = zi
我需要做任何修改在netcdf netcdf变量?(如单位或轴?)
我能保持crs。spatial_ref /值。grid_mapping /值。grid_mapping_name吗?
干杯!