目前我处理一堆坐标(epsg: 4326 = >纬度/经度)及其对应的值(温度)。我们的目标是将这些坐标和它们的值写入一个简单的netcdf文件和显示在如QGIS(这样你将有一个为每个像素颜色的正方形/协调)
目前在分散数据格式,这就是为什么第一次插值。
插值后我试图写入数据(光栅格式)到一个netcdf文件,但这失败:
进口numpy从scipy.interpolate np进口griddata进口xarray xr进口熊猫一样pd进口rioxarray进口netCDF4 nc4 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] #准备一个网格插值ξ= np.arange(6.0, 14.0, 0.001)易= np.arange(48.0, 64.0, 0.001),易= np。meshgrid (xi, yi) #你可以看到稍大,那么使用网格坐标#插入子= griddata((经度、纬度)、温度(xi, yi)方法=“线性”)#时间写这到netcdf文件ds = nc4.Dataset(“测试。数控”、“w”,格式= ' NETCDF3_CLASSIC”) dim_time = ds。createDimension('时间',0)dim_lat = ds。createDimension (lat, len (yi)) dim_lon = ds。createDimension(经度,len (xi)) #这是正确的还是我该如何设置CRS epsg: 4326 ?crs = ds。createVariable crs (“WGS84”、“c”)。spatial_ref = " "“GEOGCS (“WGS 84”,基准面(“WGS_1984球体(“WGS 84”, 6378137, 298.257223563,权威[“EPSG”、“7030”]],权威[“EPSG”、“6326”]], PRIMEM[“格林威治”,0,权威[“EPSG”、“8901”]],单位(“学位”,0.01745329251994328,权威[“EPSG”、“9122”]],权威[“EPSG”、“4326”]]””“时间= ds。createVariable(“时间”、“f4”,“时间”)纬度= ds。createVariable(“纬度”、“f4”、“纬度”)纬度。单位=‘度’经度= ds。createVariable(“朗”、“f4”、“朗”)经度。单位=‘度’价值= ds。createVariable(“温度”、“f4”,(“时间”、“纬度”、“朗”,))#充满价值观纬度[:]= lat # = >错误:ValueError:形状不匹配:对象不能播放一个形状经度[:]=经度值(0::)=子值。grid_mapping = ' WGS84 ' ds.close ()
当我试着写例如纬度变量又:
ValueError:形状不匹配:对象不能播放一个形状
我调试代码,但没能找到错误,专家吗?
干杯!
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吗?
干杯!