Skip to content

Instantly share code, notes, and snippets.

@gidden
Created April 5, 2016 18:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gidden/4193a0ffb425f2c2afb0d2a7d4740467 to your computer and use it in GitHub Desktop.
Save gidden/4193a0ffb425f2c2afb0d2a7d4740467 to your computer and use it in GitHub Desktop.
Reading in custom-written raster bands causes seg fault
wget https://www.unidata.ucar.edu/software/netcdf/examples/tos_O1_2001-2002.nc
import rasterio as rio
with rio.drivers():
with rio.open('tos_O1_2001-2002.nc', 'r') as src:
raster = src.read()[0]
profile = src.profile
profile['count'] = 2
with rio.open('test.nc', 'w', **profile) as dst:
dst.write_band(1, raster)
dst.write_band(2, raster)
with rio.open('test.nc') as src:
rasters = src.read()
@perrygeo
Copy link

perrygeo commented Apr 6, 2016

Two quick comments:

  • you can read just the first band using src.read(1) rather than reading the entire dataset into a 3D array
  • you can write bands using dst.write(raster, 1) which is preferred over the write_band method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment