2

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?

Megan Martin
  • 221
  • 1
  • 9
  • Does this answer your question? [xarray select nearest lat/lon with multi-dimension coordinates](https://stackoverflow.com/questions/58758480/xarray-select-nearest-lat-lon-with-multi-dimension-coordinates) – Michael Delgado May 25 '22 at 00:18
  • specifically I mean you can re-project the station lat/lons to the CRS of the dataset, then select using x, y. That's definitely the most direct way to do this, especially if you want to reindex to a large number of coordinates – Michael Delgado May 25 '22 at 00:25
  • Ah yes that's helpful! I am trying to transform all of the station lat, lons to the x y coordinate system based on the answer by Karl Schneider to that question in the link above. How would I apply "x,y = data_crs.transform_point(lat,lon,src_crs=ccrs.PlateCarree())" to the entire array of Lat (Lat=CMStations.lat) and Lon (Lon=CMStations.lon) from the station csv? I'm assuming I'd have to loop through each of them? – Megan Martin May 25 '22 at 19:37
  • that's a good question :) what's the CRS of your data? – Michael Delgado May 25 '22 at 19:58
  • I'm assuming CRS is the type of grid? I think this is lambert conformal. Here's the metadata: Conventions: CF-1.2 centerlat:50.0 centerlon:-107.0 latcorners:[ 1.000001,0.897945,46.3544,46.63433 ] loncorners:[-145.5,-68.32005,-2.569891,148.6418 ] platform:Model standardpar1:50.0 standardpar2:50.000001 dataset_title:NARR references:https://www.esrl.noaa.gov/psd/data/gridded/data.narr.html dimensions(sizes):time(2920),y(277),x(349) variables(dimensions):float64 time(time), float32 lat(y,x), float32 lon(y,x), float32 y(y), float32 x(x), int32 Lambert_Conformal(), float32 air(time,y,x) – Megan Martin May 25 '22 at 20:12
  • so you can reshape vectors of points with geopandas - e.g. `gpd.points_from_xy(xx.ravel(), yy.ravel(), crs=ccrs.PlateCarree ()).to_crs(data_crs).x.reshape(xx.shape)` and `gpd.points_from_xy(xx.ravel(), yy.ravel(), crs=ccrs.PlateCarree()).to_crs(data_crs).yy.reshape(yy.shape)`. I think this could use a better answer and maybe even an addition to the docs but I'm slammed at the moment. hope this helps! – Michael Delgado May 27 '22 at 02:44
  • The error indicates that the 'lat','lon' dimensions you indicated does not exist. If you look at the summary of your data, it only has 'time','x', and 'y' dimensions. – Dani56 Jul 01 '22 at 03:30

0 Answers0