Python Extract Multiple Lat/long From NETCDF Files Using Xarray
I have a NC file (time, lat, long) Download from here and I am trying to extracting time series of multiple stations (lat/long points Download from here). So I tried it this way t
Solution 1:
What about if you include the station name in your zip command, and then insert the ID into the pandas dataframe line like this (by the way, I couldn't access your CSV file, so I simplified slightly the example with a dummy list).
import pandas as pd
import xarray as xr
nc_file = "example.nc"
NC = xr.open_dataset(nc_file)
#dummy locations and station id as I can't access the CSV
lat=[40,42,41]
lon=[-100,-105,-99]
name=["a","b","c"]
Newdf = pd.DataFrame([])
for i,j,id in zip(lat,lon,name):
dsloc = NC.sel(lat=i,lon=j,method='nearest')
DT=dsloc.to_dataframe()
# insert the name with your preferred column title:
DT.insert(loc=0,column="station",value=id)
Newdf=Newdf.append(DT,sort=True)
print(Newdf)
This gives me:
EVP lat lon station
time
2019-01-01 19:00:00 0.0527 39.938 -99.938 a
2019-01-01 23:00:00 0.0232 39.938 -99.938 a
2019-01-01 19:00:00 0.0125 41.938 -104.938 b
2019-01-01 23:00:00 0.0055 41.938 -104.938 b
2019-01-01 19:00:00 0.0527 40.938 -98.938 c
2019-01-01 23:00:00 0.0184 40.938 -98.938 c
Post a Comment for "Python Extract Multiple Lat/long From NETCDF Files Using Xarray"