Search code examples
pythonnumpynetcdfpython-xarraynetcdf4

Combining NetCDF files using Xarray


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?


Solution

  • 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.