I have a csv of weather stations and their corresponding latitudes and longitudes. I also have a gridded dataset of temperature trends. I want to find and create an array of the temperature trends of the grid points that most closely match the latitude and longitude of the weather stations. Here's what I have so far:
from netCDF4 import Dataset as netcdf_dataset
import numpy as np
import xarray as xr
import pandas as pd
#open NASA GISS gridded temperature netcdf file
df = xr.open_dataset('BerkeleyTmaxTrends.nc')
#open csv of weather stations
CMStations=pd.read_csv('Slope95.csv')
#pull out latitude and longitude from station csv
Lat=CMStations.lat
Lon=CMStations.lon
#find trend value of nearest grid point
gridtrend=[]
for i in Lat:
for j in Lon:
pt=df.sel(lat=[i],lon=[j],method="nearest")
gridtrend.append(pt.trend)
I don't think I'm looping through properly. The length of Lat and Lon from the weather station is csv is 225 so I want a final gridtrend
array that also has a length of 225. When I use the code above I get a list that is 50625 long. How can I fix this loop?
Here is a screenshot of what my gridded temperature df looks like: