简体   繁体   English

如何在python中为数据集分配纬度/经度坐标?

[英]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

如何在坐标(纬度,经度)中计算截然相反的坐标,其中-90 <lat<90 and -180<lon<180< div><div id="text_translate"><p> 给定坐标点 X=(lat, lon) 和圆心 C=(lat_center, lon_center) 我想计算点 X 截然相反的坐标(假设 X 在圆心为 C 的圆内) .</p><p> 例如,如果 C=(45.9, 180),则与 X=(45.9, -179) 截然相反的值应该是 (45.9, 179)。</p><p> 以下 function 是一个近似值,但不能解决纬度在 (-90, 90) 和经度 (-180, 180) 之间的问题。</p><pre> def f(lat, lon, center): lat_center = center[0] lon_center = center[1] dist_lon = np.abs(lon - lon_center) if np.abs(lon - lon_center)&lt;180 else 360 - np.abs(lon - lon_center) dist_lat = np.abs(lat - lat_center) if np.abs(lat - lat_center)&lt;180 else 360 - np.abs(lat - lat_center) lon_op = lon_center + dist_lon if lon_center + dist_lon.= lon else lon_center - dist_lon lat_op = lat_center + dist_lat if lat_center + dist_lat,= lat else lat_center - dist_lat return np,round(lat_op. 2), np.round(lon_op, 2)</pre> </div></lat<90> - How to calculate diametrically opposite in coordinates(lat, lon) where -90<lat<90 and -180<lon<180

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何识别xarray中的时间,长度和纬度坐标? - How to identify time, lon, and lat coordinates in xarray? 从 xarray 数据集中删除经纬度坐标和索引 - Drop lat lon coordinates and index from xarray dataset 如何使用cartopy将显示坐标转换为geographoc坐标(纬度,经度)? - How to convert display coordinates to geographoc coordinates (lat, lon) using cartopy? Python,Numpy,Scipy:如何从GPS坐标中排除位置错误? (平均lon,lat) - Python, numpy, scipy: How to exclude a location error from GPS coordinates? (averaging lon, lat) 如何在坐标(纬度,经度)中计算截然相反的坐标,其中-90 <lat<90 and -180<lon<180< div><div id="text_translate"><p> 给定坐标点 X=(lat, lon) 和圆心 C=(lat_center, lon_center) 我想计算点 X 截然相反的坐标(假设 X 在圆心为 C 的圆内) .</p><p> 例如,如果 C=(45.9, 180),则与 X=(45.9, -179) 截然相反的值应该是 (45.9, 179)。</p><p> 以下 function 是一个近似值,但不能解决纬度在 (-90, 90) 和经度 (-180, 180) 之间的问题。</p><pre> def f(lat, lon, center): lat_center = center[0] lon_center = center[1] dist_lon = np.abs(lon - lon_center) if np.abs(lon - lon_center)&lt;180 else 360 - np.abs(lon - lon_center) dist_lat = np.abs(lat - lat_center) if np.abs(lat - lat_center)&lt;180 else 360 - np.abs(lat - lat_center) lon_op = lon_center + dist_lon if lon_center + dist_lon.= lon else lon_center - dist_lon lat_op = lat_center + dist_lat if lat_center + dist_lat,= lat else lat_center - dist_lat return np,round(lat_op. 2), np.round(lon_op, 2)</pre> </div></lat<90> - How to calculate diametrically opposite in coordinates(lat, lon) where -90<lat<90 and -180<lon<180 将经纬度坐标添加到单独的列(python/dataframe) - Adding Lat Lon coordinates to separate columns (python/dataframe) 如何从 lon/lat 坐标计算形状并用 plotly 绘制 - How to calculate the shape from lon/lat coordinates and draw it with plotly 如何在 python 中将纬度/经度值转换为十进制? - How to convert lat/lon values to decimal in python? 如何将x,y坐标投影到netcdf文件中的纬度/经度 - How to project x,y coordinates to lat/lon in netcdf file 如何在 LAT/LON NMEA 坐标中获得 6 位小数 - How to get 6 decimal places in LAT/LON NMEA Coordinates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM