Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading multiple netcdf files with the same variable (but different sources) into one xarray dataset

I have multiple datafiles in netcdf format and it's in different folders. I want to load all of them together. All of them are for the same variable, same (or almost same) period, but from different sources. How do I quickly load all of them? (I can seperately load them as different xarray datasets, but I have way too many)

I tried using:

xr.open_mfdataset('/media/rakshith/Seagate Expansion Drive/Good models/*.nc', concat_dim=None)

But I get the error:

TypeError: Cannot combine along dimension 'time' with mixed types. Found: DatetimeProlepticGregorian, DatetimeNoLeap.

This makes me think open_mfdataset cannot be used to do this.

like image 821
Rakshith Avatar asked Sep 11 '26 09:09

Rakshith


1 Answers

This is understandable behaviour from xarray. You are merging files with either standard calendars and files with leap year free calendars, i.e. files without 29th February.

Internally, these calendars will be represented in incompatible ways (they will use "seconds since X" and these will be defined differently), so xarray cannot merge them. You have two options. Either convert all files to a standard calendar, or convert all files to a leap year free option. You haven't explained your use case, so I cannot say which is the correct approach here.

If you wanted to convert all files to leap year free format, you could use my package nctoolkit. You could do this as follows for one file:

import nctoolkit as nc
ds = nc.open_data("foo.nc")
ds.no_leaps()
ds.to_nc("bar.nc")
like image 80
Robert Wilson Avatar answered Sep 14 '26 01:09

Robert Wilson