当纬度和经度是数据集坐标的函数时,如何找到最近的网格点?

发布于 2025-01-31 14:41:59 字数 933 浏览 4 评论 0原文

我有一个网格温度数据的NetCDF文件,还有一个天气站的CSV。我正在尝试找到最接近气象站的网格点。我遇到的问题是NetCDF文件的纬度和经度是X和Y值的函数。 过去,这是我为找到最近的网格点所做的:

#import libraries
import os
import matplotlib.pyplot as plt
from netCDF4 import Dataset as netcdf_dataset
import numpy as np
import xarray as xr
import pandas as pd

#open netcdf file of gridded temperature
df=xr.open_dataset('/home/mmartin/LauNath/air.2m.2015.nc')

#open csv of weather stations
CMStations=pd.read_csv('Slope95.csv')

#Pull out variables
StationList=CMStations.station
City=CMStations.city
Lat=CMStations.lat
Lon=CMStations.lon

#find nearest grid points
NearGrid=df.sel(lat=Lat.to_xarray(), lon=Lon.to_xarray(), method='nearest')

这当然不起作用: 'ValueError:尺寸或多指数级别['lat','lon'不存在', 但是我不确定如何修改它。如何将此方法与此嵌套的NetCDF文件一起使用?

I have a netcdf file of gridded temperature data, and a csv of weather stations. I am trying to find the grid points that are closest to the weather stations. The problem I'm having is that the latitude and longitude of the netcdf file are functions of x and y values.
enter image description here
In the past this is what I've done to find nearest grid points:

#import libraries
import os
import matplotlib.pyplot as plt
from netCDF4 import Dataset as netcdf_dataset
import numpy as np
import xarray as xr
import pandas as pd

#open netcdf file of gridded temperature
df=xr.open_dataset('/home/mmartin/LauNath/air.2m.2015.nc')

#open csv of weather stations
CMStations=pd.read_csv('Slope95.csv')

#Pull out variables
StationList=CMStations.station
City=CMStations.city
Lat=CMStations.lat
Lon=CMStations.lon

#find nearest grid points
NearGrid=df.sel(lat=Lat.to_xarray(), lon=Lon.to_xarray(), method='nearest')

This of course doesn't work:
'ValueError: dimensions or multi-index levels ['lat', 'lon'] do not exist',
but I am unsure how to modify it. How can I use this method with this nested netcdf file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文