calling lat and lon from a pandas dataframe in Cdo().remapnn
Added by Tyler Herrington over 4 years ago
Hello, I have stored a series of latitudes and longitudes read into python using pandas_read_csv: lat_csv and lon_csv.
What I would like to do is read these lat/lon coordinates into a Cdo().remapnn in order to return the soil temperature for the closest grid-cell to the lat/lon coordinates specified by lat_csv and lon_csv.
I am unsure of how to use cdo.remapnn() where lat and long are variables instead of manually specified numerical values, and wanted to see if anyone might have a suggestion here?
# Read in lat/lon coordinates of in-situ locations from .csv file
obs_loc = str("/praid/users/herringtont/soil_temp/Obs_Lat_Long.csv")
dframe = pd.read_csv(obs_loc)
lat_csv = dframe['Lat']
lon_csv = dframe['Long']
lon_csv2 = (lon_csv + 180.00).round(decimals=2) #csv file longitudes are offset by 180 relative to netcdf
#set directory and filename of in file
wdir = "/praid/users/herringtont/soil_temp/reanalysis/ERA5-Land/"
wfil = [wdir,"ERA5_soil_temp_layer1.nc"]
wkfil = "".join(wfil)
#set directory of out files
odir = "/praid/users/herringtont/soil_temp/reanalysis/ERA5-Land/site_level/"
#loop through each lat/lon pair from csv file
for i in range (0,1):
str_i = str(i)
ofil = [odir,"ERA5_soil_temp_layer1_site_",str_i]
otfil = "".join(ofil)
latf = lat_csv[i]
lonf = lon_csv2[i]
cdo = Cdo()
cdo.remapnn('lon=lonf/lat=latf', input=wkfil, output= otfil, options = '-f nc')
Thank you,
Tyler
Replies (1)
RE: calling lat and lon from a pandas dataframe in Cdo().remapnn - Added by Karin Meier-Fleischer over 4 years ago
Hi Tyler,
you can create a string variable containing the lon/lat values string:
remap_in = "".join(['lon=',str(lonf),'_','lat=',str(latf)]) cdo = Cdo() cdo.remapnn(remap_in, input=wkfil, output=otfil, options = '-f nc')
-Karin