2
votes

I'd like to use xarray to handle a dataset with time-dependent coordinates. More precisely, I deal with storm-centered forecasts, which results in dimensions (time, lat, lon) BUT lat, lon are a function of time as the storm moves.

It seems as there is no native way in xarray to deal with such a case, but what are possible workarounds? It is cumbersome to store every timestep independently, however, using xr.concat results in a single lat, lon coordinate for all times... I wondered if applying time1.interp_like(time2) could help. In essence padding all timesteps with nan to the maximal extend of lat, lon over time... Any ideas?

1
These questions come up frequently regarding xarray. It all comes down to what you mean by "deal with." Could you be more specific about what you want to do with the (time-dependent) lat lon coordinates? Plot? Calculate something? - Ryan
Plotting is an issue... In my case, for calculations, it doesn't matter much, however, I would still be interested in a solution for that too - Bachbold

1 Answers

4
votes

If your goal is plotting, then you can solve this problem using non-dimension coordinates.

Consider the following toy example. Here j and i are "logical" dimensions which don't correspond to any physical location (just array indexes), while lon and lat are the time-dependent geographical coordinates.

import xarray as xr
import numpy as np

# create a dummy dataset
ds = xr.Dataset({'foo': (('time', 'j', 'i'), np.random.rand(10, 30, 30))})

# add non-dimension coordinates that depend on time
ds.coords['lon'] = 190 + 0.25 * ds.i + 0.01 * ds.time
ds.coords['lat'] = 24 + 0.25 * ds.j + 0.02 * ds.time
print(ds)

# select a specific time and plot
ds.foo.sel(time=5).plot(x='lon', y='lat')

The dataset repr is

<xarray.Dataset>
Dimensions:  (i: 30, j: 30, time: 10)
Coordinates:
    lon      (i, time) float64 190.0 190.0 190.0 190.0 ... 197.3 197.3 197.3
    lat      (j, time) float64 24.0 24.02 24.04 24.06 ... 31.39 31.41 31.43
Dimensions without coordinates: i, j, time
Data variables:
    foo      (time, j, i) float64 0.7443 0.2943 0.4479 ... 0.5386 0.3574 0.5597

And the figure looks like this:

output of plot

You could adapt this sort of approach to the scenario you described