'xarray - Merge multiple DataArray objects with overlapping or missing time coordinates
I have multiple netCDF files and they are loaded into DataArray objects using xarray with three dimensions [time, latitude, longitude].
The latitude and longitude coordinates are identical while some have overlapping or missing coordinates in the time dimension.
**da1**:
time (time) datetime64[ns] 2017-10-03T18:00:00 ... 2017-10-...
array(['2017-10-03T18:00:00.000000000', '2017-10-03T19:00:00.000000000',
'2017-10-03T20:00:00.000000000', '2017-10-03T21:00:00.000000000',
'2017-10-03T22:00:00.000000000', '2017-10-03T23:00:00.000000000',
'2017-10-04T00:00:00.000000000', '2017-10-04T01:00:00.000000000',
'2017-10-04T02:00:00.000000000', '2017-10-04T03:00:00.000000000',
'2017-10-04T04:00:00.000000000', '2017-10-04T05:00:00.000000000',
'2017-10-04T06:00:00.000000000', '2017-10-04T07:00:00.000000000',
'2017-10-04T08:00:00.000000000', '2017-10-04T09:00:00.000000000',
'2017-10-04T10:00:00.000000000', '2017-10-04T11:00:00.000000000',
'2017-10-04T12:00:00.000000000', '2017-10-04T13:00:00.000000000',
'2017-10-04T14:00:00.000000000', '2017-10-04T15:00:00.000000000',
'2017-10-04T16:00:00.000000000', '2017-10-04T17:00:00.000000000',
'2017-10-04T18:00:00.000000000', '2017-10-04T19:00:00.000000000'],
dtype='datetime64[ns]')
**d2**:
time
(time)
datetime64[ns]
2017-10-04T13:00:00 ... 2017-10-...
array(['2017-10-04T13:00:00.000000000', '2017-10-04T14:00:00.000000000',
'2017-10-04T15:00:00.000000000', '2017-10-04T16:00:00.000000000',
'2017-10-04T17:00:00.000000000', '2017-10-04T18:00:00.000000000',
'2017-10-04T19:00:00.000000000', '2017-10-04T20:00:00.000000000',
'2017-10-04T21:00:00.000000000', '2017-10-04T22:00:00.000000000',
'2017-10-04T23:00:00.000000000', '2017-10-05T00:00:00.000000000',
'2017-10-05T01:00:00.000000000', '2017-10-05T02:00:00.000000000',
'2017-10-05T03:00:00.000000000', '2017-10-05T04:00:00.000000000',
'2017-10-05T05:00:00.000000000', '2017-10-05T06:00:00.000000000',
'2017-10-05T07:00:00.000000000', '2017-10-05T08:00:00.000000000',
'2017-10-05T09:00:00.000000000', '2017-10-05T10:00:00.000000000',
'2017-10-05T11:00:00.000000000', '2017-10-05T12:00:00.000000000',
'2017-10-05T13:00:00.000000000', '2017-10-05T14:00:00.000000000'],
dtype='datetime64[ns]')
I would like to run "concat" or "merge" or "combine" them by the time coordinates. The time coordinates would run from '2017-10-03T18:00:00.000000000' to '2017-10-05T14:00:00.000000000'. Use the newer values where overlapping coordinates occur. For example, use the value from the da2 where time = '2017-10-04T13:00:00.000000000'. Use nan if the time coordinates are missing from the input DataArray objects.
I tried:
da_merged = xr.merge([da2, da1], compat="override")
But I ended up with a whole bunch of nan values for the time coordi?nates in da1. Can I have some directions which method/params I should use
Solution 1:[1]
I hope you solved it, but for me it worked simply doing:
da_merged=xr.Dataset()
file_paths_list =[da1,da2]
for file in file_paths_list:
da_merged = xr.merge([da_merged,xr.open_mfdataset(file)],compat='override')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RubenS |
