2

I am trying to combine multiple NetCDF files using xarray.

Here are my dimensions:

Dimensions:        (Time: 1, XCells: 2000, YCells: 1000) 
Coordinates:
    longitude      (YCells, XCells) float32 
    latitude       (YCells, XCells) float32
  * Time           (Time) datetime64[ns]  
Dimensions without coordinates: XCells, YCells

Combine by_coords only works for 1-dimensional coordinates. combining spatial netcdf files using xarray python

However, when I use combine="nested", It repeats latitude and longitude for Time.

float longitude(Time, YCells, XCells);
longitude:_FillValue = NaNf;
float latitude(Time, YCells, XCells);
latitude:_FillValue = NaNf;

Latitude and Longitude are 2-D, but the same across the time. Does Xarray have a way of combining this data?

Jeanne Lane
  • 495
  • 1
  • 9
  • 26
  • 1
    Do you want to combine the 2 files along time? In that case, `xr.concat([file1, file2], concat_dim='time')` should work. – Light_B Oct 10 '20 at 09:55
  • 1
    Thank you for your suggestion! That resulted in repeating latitude and longitude for the times--my longitude and latitude are 2-D. I was able to successfully combine the datasets by manipulating the numpy arrays and converting them to NetCDF datasets. – Jeanne Lane Oct 14 '20 at 02:07
  • 1
    I understand the problem for you. Yes, `concat` will repeat the original the original dims in this way. Maybe xarray has a easier way to handle this, but I would also approach it by assigning new lon and lat coordinates. Great that it worked for you, and please post your answer so that it might be of use to someone in the future :) – Light_B Oct 16 '20 at 12:29

1 Answers1

0

I combined the files by time using netcdf-python and numpy.

The code for copying one dataset to another is similar to Xavier Ho's solution here: python netcdf: making a copy of all variables and attributes but one

Variables that I didn't want to repeat I copied directly. For the time dimension and variable altering with time, I altered the copy expression using numpy slices.

Jeanne Lane
  • 495
  • 1
  • 9
  • 26