Skip to content Skip to sidebar Skip to footer

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,idinzip(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:

EVPlatlonstationtime2019-01-01 19:00:00  0.052739.938-99.938a2019-01-01 23:00:00  0.023239.938-99.938a2019-01-01 19:00:00  0.012541.938-104.938b2019-01-01 23:00:00  0.005541.938-104.938b2019-01-01 19:00:00  0.052740.938-98.938c2019-01-01 23:00:00  0.018440.938-98.938c

Post a Comment for "Python Extract Multiple Lat/long From Netcdf Files Using Xarray"