[英]How to assign lat/lon coordinates to a dataset in python?
I have three xarray datasets, LATITUDE, LONGITUDE, WIND SPEED all having the same x,y dimensions.我有三个xarray数据集,纬度,经度,风速都具有相同的X,Y尺寸。 I want to assign the LATITUDE and LONGITUDE datasets as coordinates of WIND SPEED at each point in the x,y frame, so that the variable WIND SPEED has dimensions like this: WIND SPEED(LATITUDE, LONGITUDE).我想将 LATITUDE 和 LONGITUDE 数据集指定为 x,y 框架中每个点的 WIND SPEED 坐标,以便变量 WIND SPEED 具有如下尺寸:WIND SPEED(LATITUDE, LONGITUDE)。
How should I proceed?我应该如何进行? The input data is the output of a gridded weather model in Netcdf format.输入数据是 Netcdf 格式的网格天气模型的输出。 I have done some calculations from the input and I want to assign the coordinates to the outputs of the calculations(WIND SPEED).我已经从输入做了一些计算,我想将坐标分配给计算的输出(风速)。 Later I want to do spatial interpolation with with nearest neighbor method, so that I can get a value at any lat,lon within the dataset.后来我想用最近邻法进行空间插值,这样我就可以在数据集中的任何纬度、经度处获得一个值。 Latitude XArray Sample after importing:导入后的纬度 XArray 示例:
array([[21.821693, 21.821693, 21.821693, ..., 21.821693, 21.821693,
21.821693],
......................................................
[30.20221 , 30.20221 , 30.20221 , ..., 30.20221 , 30.20221 ,
30.20221 ]], dtype=float32)
Wind Speed Xarray:风速阵列:
array([[8.725852, 8.758366, 8.728758, ..., nan, nan, nan],
[8.502903, 8.563703, 8.574378, ..., nan, nan, nan],
........]] dtype=float32)
One solution would be to construct an list of dicts:一种解决方案是构建一个字典列表:
speed_lat_long_list_dict =
[ {id:1, speed:'1', lat:'20.8', long: '-18.5},
{id:2, speed:'3', lat:'24.8', long: '-14.5},
....
{id:n, speed:'n', lat:'n', long: 'n} ]
This would avoid confusion of setting co-ordinates to a duplicate speed value.这将避免将坐标设置为重复的速度值的混淆。 eg what do we do if we have different co-ordinates for the same speed measurement.例如,如果我们对相同的速度测量有不同的坐标,我们该怎么办。
This can be passed to a DataFrame should you want or you can process it using for loops or list comprehensions如果您愿意,可以将其传递给 DataFrame,或者您可以使用 for 循环或列表推导来处理它
You can merge your datasets and then assign the desired variables as coordinates:您可以合并数据集,然后将所需变量分配为坐标:
data = np.random.rand(50,50)
windspeed = xr.Dataset({'windspeed':(['x','y'], data)})
lattitude = xr.Dataset({'lattitude':(['x','y'], np.cos(data))})
longitude = xr.Dataset({'longitude':(['x','y'], np.sin(data))})
ds = xr.merge([windspeed, lattitude, longitude])
ds.set_coords(['lattitude','longitude'])
<xarray.Dataset> Dimensions: (x: 50, y: 50) Coordinates: lattitude (x, y) float64 0.7035 0.9987 0.917 0.9958 ... 0.593 0.93 0.7624 longitude (x, y) float64 0.7107 0.05069 0.3988 ... 0.8052 0.3675 0.6471 Dimensions without coordinates: x, y Data variables: windspeed (x, y) float64 0.7905 0.05071 0.4102 ... 0.936 0.3763 0.7037
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.