# Extract time series values from a 3D (lon,lat,time) NetCDF file using Python

I want to extract time series from a variable in a 3D (lon,lat,time) netcdf file at specific lon/lat points. The raster is a nc file where:

``````cell: 1.5°×1.5°
time step: 1 month
lat extension: (0.75,89.25,1.5)
lon extension: (0.75,359.25,1.5)
time: (201001,205012)
step scale: cell center
``````

I have a point shapefile (lat/lon) where a point is (108.3°,58.6°). I just want to extract the cell values of the whole time steps in where points located. Are there directly method to carry out it？

I think a way that round the points number according to lat/lon step: changing the P lat/long to cell lat/lon where point located. For instance, P(108.3°, 58.6°) is in cell (108°,60°) because scale step is 1.5°. Now, the question is how to change P(108.3°,58.6°) to cell(108°,60°) used to slice .nc file.

So you have 3D data (lon,lat,time) in a netcdf file and you want to extract a time-series as a specific location in Python, right?

Here's one way using netCDF4: http://nbviewer.jupyter.org/gist/rsignell-usgs/4113653

But it's way easier using Xarray: http://nbviewer.jupyter.org/gist/rsignell-usgs/e032db75e748cf5922d38d8be9e0ecef

``````import xarray as xr

fname = 'http://thredds.ucar.edu/thredds/dodsC/grib/NCEP/GFS/Global_onedeg/Best'  # Remote OPeNDAP Dataset
#fname = 'my_raster_time_series_data.nc'   # Local NetCDF file

ds = xr.open_dataset(fname)
dsloc = ds.sel(lon=230.5,lat=55.0,method='nearest')

dsloc['Wind_speed_gust_surface'].plot();
``````

• The easiest way to install `xarray` is with the free Anaconda python distribution. After installing Anaconda, just type `conda install -c conda-forge xarray` Jan 20, 2017 at 12:30