1

My code is:

import numpy as np
import rasterio
from rasterio.warp import calculate_default_transform, reproject, Resampling
from netCDF4 import Dataset

mask=rasterio.open(path1)
dst_crs='EPSG:4326'
with rasterio.open(path2) as src:
    transform, width, height = calculate_default_transform(
        src.crs, dst_crs, mask.width, mask.height, *mask.bounds)
    kwargs = src.meta.copy()
    kwargs.update({
        'crs': dst_crs,
        'transform': transform,
        'width': width,
        'height': height
        })

    dst=Dataset(pathdst, "w", format="NETCDF4")
    for i in range(1, src.count + 1):
        reproject(
            source=rasterio.band(src, i),
            destination=dst.band(i),
            src_transform=src.transform,
            src_crs=src.crs,
            dst_transform=transform,
            dst_crs=dst_crs,
            resampling=Resampling.nearest)

results in an error

AttributeError: NetCDF: Attribute not found

What is the right way to make a multiband cycle here? The best solution would be writing reprojected bands (each one is a time) with transform parameters from mask (not a multiband file) from src to dst. I believe I should create a time dimension for dst.

Full error is:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-104-2d687936926e> in <module>
     21         reproject(
     22             source=rasterio.band(src, i),
---> 23             destination=dst.band(i),
     24             src_transform=src.transform,
     25             src_crs=src.crs,

netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Dataset.__getattr__()

netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Dataset.getncattr()

netCDF4/_netCDF4.pyx in netCDF4._netCDF4._get_att()

netCDF4/_netCDF4.pyx in netCDF4._netCDF4._ensure_nc_success()

By the way. The file appears in the destination folder (96B).

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.