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?

3
  • 1
    Do you want to combine the 2 files along time? In that case, xr.concat([file1, file2], concat_dim='time') should work. Commented Oct 10, 2020 at 9: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. Commented Oct 14, 2020 at 2: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 :) Commented Oct 16, 2020 at 12:29

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.