rioxarray(或 xarray)在使用 to_netcdf 重新投影和保存后将 spatial_ref 坐标转换为变量?
rioxarray (or xarray) converts spatial_ref coordinate to variable after reprojecting and saving with to_netcdf?
我有一个“dataarray”,我正在尝试使用 rioxarray 重新投影它。然而,当我使用 xarray.to_netcdf 进行重投影后,保存的文件是一个数据集,其中“spatial_ref”坐标转换为一个变量。我不确定是 xarray 还是 rioxarray.reprojection 导致了这种行为。以下是一些显示问题的代码:
import xarray as xr
import rioxarray
from pyproj import CRS
lst = xr.open_dataset("lst.nc") # File which carries the original CRS
luc = xr.open_rasterio("luc.tif") # File with the desired projection system
file_to_reproject = xr.open_dataarray("myfile.nc") # File to reproject
cc_lst = CRS.from_cf(lst.crs.attrs) # Get the CRS
cc_luc = luc.rio.crs
file_to_reproject = file_to_reproject.rio.write_crs(cc_lst) # write crs
file_reprojected= file_to_reproject.rio.reproject(cc_luc) #reproject the file
print(file_reprojected)
<xarray.DataArray (season: 4, y: 4343, x: 4172)>
array([[[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan]]])
Coordinates:
* x (x) float64 -3.382e+06 -3.381e+06 ... 4.817e+05 4.826e+05
* y (y) float64 5.361e+06 5.36e+06 ... 1.338e+06 1.337e+06
* season (season) object 'DJF' 'JJA' 'MAM' 'SON'
spatial_ref int64 0
# however after saving the file and reloading it I get a dataset where
# spatial_ref turned into variable
file_reprojected.to_netcdf("file_reprojected.nc")
ds= xr.open_dataset("file_reprojected.nc")
>>> print(ds)
<xarray.Dataset>
Dimensions: (season: 4, x: 4172, y: 4343)
Coordinates:
* x (x) float64 -3.382e+06 ... 4.826e+05
* y (y) float64 5.361e+06 5.36e+06 ... 1.337e+06
* season (season) object 'DJF' 'JJA' 'MAM' 'SON'
Data variables:
spatial_ref int64 ...
__xarray_dataarray_variable__ (season, y, x) float64 ...
# Here is the content of spatial_ref
>>> print(ds["spatial_ref"])
<xarray.DataArray 'spatial_ref' ()>
array(0)
Attributes: (12/19)
crs_wkt: PROJCS["unknown",GEOGCS["unknown",DATUM["...
semi_major_axis: 6378137.0
semi_minor_axis: 6356752.314140356
inverse_flattening: 298.257222101
reference_ellipsoid_name: GRS 1980
longitude_of_prime_meridian: 0.0
... ...
如您所见,在重新投影数据数组并保存后,文件变成了数据集,其中 spatial_ref 作为新变量。
我只对地理参考数据阵列感兴趣。我该如何解决这个问题?
编辑1
我认为它可能与这个 (here) 问题有关
编辑2
我在导入重新投影的文件时忘记提及没有 crs:
ds= xr.open_dataset("file_reprojected.nc")
ds.rio.crs # Retruns nothing
问题是我在使用 xarray.open_dataarray 时没有设置 decode_coords="all"。有了以下一切看起来没问题:
file_to_reproject = xr.open_dataarray("myfile.nc",decode_coords="all")
我有一个“dataarray”,我正在尝试使用 rioxarray 重新投影它。然而,当我使用 xarray.to_netcdf 进行重投影后,保存的文件是一个数据集,其中“spatial_ref”坐标转换为一个变量。我不确定是 xarray 还是 rioxarray.reprojection 导致了这种行为。以下是一些显示问题的代码:
import xarray as xr
import rioxarray
from pyproj import CRS
lst = xr.open_dataset("lst.nc") # File which carries the original CRS
luc = xr.open_rasterio("luc.tif") # File with the desired projection system
file_to_reproject = xr.open_dataarray("myfile.nc") # File to reproject
cc_lst = CRS.from_cf(lst.crs.attrs) # Get the CRS
cc_luc = luc.rio.crs
file_to_reproject = file_to_reproject.rio.write_crs(cc_lst) # write crs
file_reprojected= file_to_reproject.rio.reproject(cc_luc) #reproject the file
print(file_reprojected)
<xarray.DataArray (season: 4, y: 4343, x: 4172)>
array([[[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan]]])
Coordinates:
* x (x) float64 -3.382e+06 -3.381e+06 ... 4.817e+05 4.826e+05
* y (y) float64 5.361e+06 5.36e+06 ... 1.338e+06 1.337e+06
* season (season) object 'DJF' 'JJA' 'MAM' 'SON'
spatial_ref int64 0
# however after saving the file and reloading it I get a dataset where
# spatial_ref turned into variable
file_reprojected.to_netcdf("file_reprojected.nc")
ds= xr.open_dataset("file_reprojected.nc")
>>> print(ds)
<xarray.Dataset>
Dimensions: (season: 4, x: 4172, y: 4343)
Coordinates:
* x (x) float64 -3.382e+06 ... 4.826e+05
* y (y) float64 5.361e+06 5.36e+06 ... 1.337e+06
* season (season) object 'DJF' 'JJA' 'MAM' 'SON'
Data variables:
spatial_ref int64 ...
__xarray_dataarray_variable__ (season, y, x) float64 ...
# Here is the content of spatial_ref
>>> print(ds["spatial_ref"])
<xarray.DataArray 'spatial_ref' ()>
array(0)
Attributes: (12/19)
crs_wkt: PROJCS["unknown",GEOGCS["unknown",DATUM["...
semi_major_axis: 6378137.0
semi_minor_axis: 6356752.314140356
inverse_flattening: 298.257222101
reference_ellipsoid_name: GRS 1980
longitude_of_prime_meridian: 0.0
... ...
如您所见,在重新投影数据数组并保存后,文件变成了数据集,其中 spatial_ref 作为新变量。
我只对地理参考数据阵列感兴趣。我该如何解决这个问题?
编辑1
我认为它可能与这个 (here) 问题有关
编辑2
我在导入重新投影的文件时忘记提及没有 crs:
ds= xr.open_dataset("file_reprojected.nc")
ds.rio.crs # Retruns nothing
问题是我在使用 xarray.open_dataarray 时没有设置 decode_coords="all"。有了以下一切看起来没问题:
file_to_reproject = xr.open_dataarray("myfile.nc",decode_coords="all")