0

我正在尝试将 netcdf 文件转换为光栅 (tif) 格式。我创建了一个脚本,不久前它运行良好。但是现在,当我尝试对不同的文件使用相同的简单脚本时,分辨率会0.5 x 0.50.5 x 0.5263158. 范围也从:

-100.25, -73.25, 28.75, 48.75

-100.5, -73, 28.48684, 49.01316

我也尝试过在 R 中使用不同的光栅包,但它们返回一条消息说单元格不等间距。文件(附在此处)可能是一个很好的问题,但我看不出在哪里以及如何。

复制代码:


# load netcdf file
import xarray as xr 
import rioxarray

xds = xr.open_dataset('output_shocks_us/hybrid_gfdl-esm4_ssp126_2015co2_yield_soybean_shift_2017-2044.nc')
xds = xds.rename({'lat':'y','lon':'x', 'time':'band'})

# Add CRS
xds.rio.write_crs("epsg:4326", inplace=True)

# Convert to geotiff
xds["yield-soy-noirr"].rio.to_raster('hybrid_gfdl-esm4_ssp126_2015co2_yield_soybean_shift_2017-2044_test.tif')
rio = xr.open_rasterio("hybrid_gfdl-esm4_ssp126_2015co2_yield_soybean_shift_2017-2044_test.tif")

print(xds)
print(rio)

完整的结果是:

print(xds)
<xarray.Dataset>
Dimensions:          (y: 39, x: 55)
Coordinates:
    band             int64 2025
  * y                (y) float64 28.75 30.25 30.75 31.25 ... 47.75 48.25 48.75
  * x                (x) float64 -100.2 -99.75 -99.25 ... -74.25 -73.75 -73.25
    spatial_ref      int32 0
Data variables:
    yield-soy-noirr  (y, x) float64 nan nan nan nan nan ... nan nan nan nan nan
Attributes:
    grid_mapping:  spatial_ref

############
print(rio)
<xarray.DataArray (band: 1, y: 39, x: 55)>
array([[[     nan,      nan, ...,      nan,      nan],
        [     nan,      nan, ...,      nan,      nan],
        ...,
        [     nan, 0.672842, ...,      nan,      nan],
        [     nan,      nan, ...,      nan,      nan]]])
Coordinates:
  * band     (band) int32 1
  * y        (y) float64 28.75 29.28 29.8 30.33 30.86 ... 47.17 47.7 48.22 48.75
  * x        (x) float64 -100.2 -99.75 -99.25 -98.75 ... -74.25 -73.75 -73.25
Attributes:
    transform:      (0.5, 0.0, -100.5, 0.0, 0.5263157894736842, 28.4868421052...
    crs:            +init=epsg:4326
    res:            (0.5, -0.5263157894736842)
    is_tiled:       0
    nodatavals:     (nan,)
    scales:         (1.0,)
    offsets:        (0.0,)
    descriptions:   ('yield-soy-noirr',)
    AREA_OR_POINT:  Area
    grid_mapping:   spatial_ref
4

0 回答 0