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.
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?