0

I have downloaded 2 different netcdf4 files. Both of them are about the same area(domain) and have the same variables (temperature). What is different is the years that the data is gathered. For example the first one is from 1-1-1950 until 31-12-1980 and the second one is from 1-1-1981 until 31-12-2000. What I want is basically to add them together in one object so I can have the information from 1980 until 2000. I am using the xarray package and I found two commands : concat and merge. However I am not really sure which one to use and how (syntax-wise). The following links provides information for both methods

https://xarray.pydata.org/en/stable/generated/xarray.concat.html  
https://xarray.pydata.org/en/stable/generated/xarray.merge.html#xarray.merge

For example if data1 is the information read from the 1st file and data2 from the second, how can I use one of those methods(and which one?). I think I should use concat but it is a bit confusing for me. It should be noted that the 3-d matrices (data1 and data2) have the following shape : (time, longitude, latitude) where longitude and latitude are of the same size. The only one that differs is time

Alex
  • 149
  • 8

1 Answers1

0

For combining two timeseries with the same variables and coordinates along the time dimension, you can indeed use the concat method.

Let's create some sample data

import xarray as xr
ds = xr.tutorial.open_dataset('rasm').load()

ds80 = ds.sel(time='1980')
ds81 = ds.sel(time='1981')
> ds80
<xarray.Dataset>
Dimensions:  (time: 4, y: 205, x: 275)
Coordinates:
  * time     (time) object 1980-09-16 12:00:00 ... 1980-12-17 00:00:00
    xc       (y, x) float64 189.2 189.4 189.6 189.7 ... 17.65 17.4 17.15 16.91
    yc       (y, x) float64 16.53 16.78 17.02 17.27 ... 28.26 28.01 27.76 27.51
Dimensions without coordinates: y, x
Data variables:
    Tair     (time, y, x) float64 nan nan nan nan nan ... 13.4 11.94 11.72 12.01
Attributes:
    ...

Now we can use concat to concatenate along the time axis

xr.concat([ds80, ds81], dim='time')

Actually, in this simple case, merge would work as well:

xr.merge([ds80, ds81])

In general, merge is intended mainly for combining data with different variables and always returns a Dataset.

More information can be gained in the documentation section Combining data.

Dahn
  • 1,397
  • 1
  • 10
  • 29