0

I read a netCDF file using xarray. The dataset contains lat, lon information of the population for the years: 1975, 1990, 2000 and 2015.

The dataset looks like the following and I made it also available here:

import xarray as xr
ds = xr.open_dataset('borneo_pop_t.nc')
ds

enter image description here

For each pixel I would like to have the information of each year between 1975 and 2000 given the trend of the data points I have. In particular I would like to generate more information and different layers of the population for the missing years.

How can I do that?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
emax
  • 6,965
  • 19
  • 74
  • 141

1 Answers1

0

You can use xarray's interpolation function.

Using your variable names,

import pandas as pd

# Create time series of 1x a year data
# (you can use any date range; you can generate pretty much 
# any sequence you need with pd.date_range()) 
dates = pd.date_range('1975-01-01','2015-01-01',freq='1Y') 

# Linear interpolation
ds_interp = ds.interp(time=dates)

Note a few things:

  1. This code just generates a simple linear interpolation, though ds.interp() supports everything that scipy.interpolate.interp1d() does - e.g., cubic, polynomial, etc. interpolation. Check the docks linked above for examples.
  2. This of course doesn't create new information; you still only "know" the population at the original points. Be wary what consequences interpolating the data will have on your understanding of what the population actually was in a given year.
ks905383
  • 160
  • 7