xarray

Xarray open_mfdataset without time dim

Xarray have a convenient method to open multi-dataset parallelly. However, sometimes time are encoded in the filename and are not in th .nc coordinate. So we need a method. For example, the dataset of my lab CAQRA[1] do this.

Stackoverflow [2]:

Create a function which adds a time dimension to a DataArray, and fill it with a arbitrary date:

1
2
3
def add_time_dim(xda):
xda = xda.expand_dims(time = [datetime.now()])
return xda

Then, pass this function to the preprocess argument when running the open_mfdataset functions:

1
data = xr.open_mfdataset(files, preprocess = add_time_dim)

Finally, fill the time dimension with my dates:

1
data['time'] = dates

Github [3]

I inspired also by an issue in Github and get the code below:

1
2
3
4
5
6
7
8
9
10
11
def func(ds):
ds = ds.expand_dims(path=[ds.encoding['source']])
return ds


data = xr.open_mfdataset(

# '/Users/sam/Documents/data/Reanalysisv4/CN-Reanalysis20200[1-3]*.nc',
'/Users/sam/Documents/data/Reanalysisv4/CN-Reanalysis20200101*.nc',
preprocess=func
)

xarray add crs data

Use rioxarray instead

load

1
rds = xarray.open_dataset("../../test/test_data/input/PLANET_SCOPE_3D.nc", decode_coords="all")

write

1
xda.rio.write_crs(4326, inplace=True)

  1. https://www.scidb.cn/en/detail?dataSetId=696756084735475712 ↩︎

  2. https://stackoverflow.com/questions/65376109/use-xarray-open-mfdataset-on-files-with-no-time-dimension-included ↩︎

  3. https://github.com/pydata/xarray/issues/2550 ↩︎