tech_climate
tech_climate

Reputation: 163

How to create scalar dimension in xarray?

I am trying to create a dataset in xarray which I plan to write to netcdf later but I am getting an error since my time dimension is a scalar.

df = xr.Dataset(
        data_vars={'latentHeating':    (('time', 'level', 'latitude', 'longitude' ), lh),
                   'surfacePrecipRate':    (('time', 'latitude', 'longitude' ), spr),
                   'stratiformFraction':    (('time', 'latitude', 'longitude'), stratfrac)},
        coords={ 'time':time_,
                'level': lev,
                'longitude': lon,
                'latitude': lat,
            })

ValueError                                Traceback (most recent call last)

ValueError: dimension 'time' already exists as a scalar variable

Upvotes: 2

Views: 2470

Answers (1)

Manmeet Singh
Manmeet Singh

Reputation: 400

This is a known problem in xarray You can check https://github.com/pydata/xarray/issues/1709

As a turnaround, you can create scalar dimension in xarray by using the numpy function atleast_1d

You can try the following

df = xr.Dataset(
        data_vars={'latentHeating':    (('time', 'level', 'latitude', 'longitude' ), lh),
                   'surfacePrecipRate':    (('time', 'latitude', 'longitude' ), spr),
                   'stratiformFraction':    (('time', 'latitude', 'longitude'), stratfrac)},
        coords={ 'time':np.atleast_1d(time_),
                'level': lev,
                'longitude': lon,
                'latitude': lat,
            })

Upvotes: 3

Related Questions