In [4]: data_out
Out[4]:
<xarray.DataArray (time: 2, y: 2400, x: 2400)> Size: 92MB
array([[[ 0. , 0. , 0. , ...,
0. , 0. , 0. ],
[ 0. , 0. , 0. , ...,
0. , 0. , 0. ],
[ 0. , 0. , 0. , ...,
0. , 0. , 0. ],
...,
[3102745.32245305, 3099395.65992008, 3096046.04169629, ...,
0. , 0. , 0. ],
[3097108.93459378, 3093759.06215556, 3090409.23388688, ...,
0. , 0. , 0. ],
[3091471.03450028, 3088120.95185706, 3084770.91324366, ...,
0. , 0. , 0. ]],
[[ 0. , 0. , 0. , ...,
0. , 0. , 0. ],
[ 0. , 0. , 0. , ...,
0. , 0. , 0. ],
[ 0. , 0. , 0. , ...,
0. , 0. , 0. ],
...,
[3281400.8388549 , 3278053.05613346, 3274705.25451338, ...,
0. , 0. , 0. ],
[3274617.22569414, 3271274.50480555, 3267931.76674866, ...,
0. , 0. , 0. ],
[3267852.4005159 , 3264514.74535506, 3261177.0747575 , ...,
0. , 0. , 0. ]]])
Coordinates:
* time (time) datetime64[ns] 16B 2022-01-01 2022-01-01T01:00:00
x (y, x) float64 46MB 150.1 150.1 150.1 ... -177.3 -177.3 -177.3
y (y, x) float64 46MB -30.0 -30.0 -30.0 ... -40.0 -40.0 -40.0
spatial_ref int64 8B 0
Attributes:
regrid_method: bilinear
In [5]: data_out.spatial_ref
Out[5]:
<xarray.DataArray 'spatial_ref' ()> Size: 8B
array(0)
Coordinates:
spatial_ref int64 8B 0
Attributes:
crs_wkt: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["...
semi_major_axis: 6378137.0
semi_minor_axis: 6356752.314245179
inverse_flattening: 298.257223563
reference_ellipsoid_name: WGS 84
longitude_of_prime_meridian: 0.0
prime_meridian_name: Greenwich
geographic_crs_name: WGS 84
horizontal_datum_name: World Geodetic System 1984
grid_mapping_name: latitude_longitude
spatial_ref: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["...
In [6]: data_out_nztm = data_out.rio.reproject('EPGS:2193') [20/4699]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[6], line 1
----> 1 data_out_nztm = data_out.rio.reproject('EPGS:2193')
File ~/mambaforge/envs/pyVPRM3/lib/python3.12/site-packages/rioxarray/raster_array.py:468, in RasterArray.reproject(self, dst_crs, resolution, shape, transform, resampling,
nodata, **kwargs)
461 kwargs.setdefault("gcps", gcps)
463 use_affine = (
464 "gcps" not in kwargs
465 and "rpcs" not in kwargs
466 and "src_geoloc_array" not in kwargs
467 )
--> 468 src_affine = None if not use_affine else self.transform(recalc=True)
469 if transform is None:
470 dst_affine, dst_width, dst_height = _make_dst_affine(
471 src_data_array=self._obj,
472 src_crs=self.crs,
(...) 476 **kwargs,
477 )
File ~/mambaforge/envs/pyVPRM3/lib/python3.12/site-packages/rioxarray/rioxarray.py:707, in XRasterBase.transform(self, recalc)
704 return transform
706 try:
--> 707 src_left, _, _, src_top = self._unordered_bounds(recalc=recalc)
708 src_resolution_x, src_resolution_y = self.resolution(recalc=recalc)
709 except (DimensionMissingCoordinateError, DimensionError):
File ~/mambaforge/envs/pyVPRM3/lib/python3.12/site-packages/rioxarray/rioxarray.py:1079, in XRasterBase._unordered_bounds(self, recalc)
1062 def _unordered_bounds(
1063 self, recalc: bool = False
1064 ) -> tuple[float, float, float, float]:
1065 """
1066 Unordered bounds.
1067
(...) 1077 Outermost coordinates of the `xarray.DataArray` | `xarray.Dataset`.
1078 """
-> 1079 resolution_x, resolution_y = self.resolution(recalc=recalc)
1081 try:
1082 # attempt to get bounds from xarray coordinate values
1083 left, bottom, right, top = self._internal_bounds()
File ~/mambaforge/envs/pyVPRM3/lib/python3.12/site-packages/rioxarray/rioxarray.py:1046, in XRasterBase.resolution(self, recalc)
1043 # if the coordinates of the spatial dimensions are missing
1044 # use the cached transform resolution
1045 try:
-> 1046 left, bottom, right, top = self._internal_bounds()
1047 except DimensionMissingCoordinateError:
1048 if transform is None:
File ~/mambaforge/envs/pyVPRM3/lib/python3.12/site-packages/rioxarray/rioxarray.py:1009, in XRasterBase._internal_bounds(self)
1007 raise DimensionMissingCoordinateError(f"{self.y_dim} missing coordinates.")
1008 try:
-> 1009 left = float(self._obj[self.x_dim][0])
1010 right = float(self._obj[self.x_dim][-1])
1011 top = float(self._obj[self.y_dim][0])
File ~/mambaforge/envs/pyVPRM3/lib/python3.12/site-packages/xarray/core/common.py:157, in AbstractArray.__float__(self)
156 def __float__(self: Any) -> float:
--> 157 return float(self.values)
TypeError: only length-1 arrays can be converted to Python scalars
The reprojection results in a TypeError exception.
I expected an xarray dataset in reprojected coordinates.
I'm trying to reproject ERA5 data from lon/lat (EPSG:4326) to New Zealand Transverse Mercator (EPSG:2193). I'm getting "TypeError: only length-1 arrays can be converted to Python scalars" from reproject. As far as I can tell my usage and data are quite analogous to this example in the documentation.
Code Sample, a copy-pastable example if possible
A "Minimal, Complete and Verifiable Example" will make it much easier for maintainers to help you:
http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports
Problem description
The reprojection results in a TypeError exception.
Expected Output
I expected an xarray dataset in reprojected coordinates.
Environment Information
Installation method
Conda environment information (if you installed with conda):
Details
Environment (conda list):Details about
condaand system (conda info):Details