NCAR / pynio

PyNIO is a multi-format data I/O package with a NetCDF-style interface
http://www.pyngl.ucar.edu/Nio.shtml
Apache License 2.0
112 stars 37 forks source link

All numpy data arrays read from PyNIO variables are being marked as read only #24

Closed mpkay closed 6 years ago

mpkay commented 6 years ago

In the current (1.5.1) release of PyNIO, accessed through the conda package (see below), we can no longer update data arrays retrieved from PyNIO variables in place with code like the following:

x = nc.variables["skintemp"][:] x *= 2.0

This raises an exception: ValueError: output array is read-only

Somehow the arrays have their writeable flag set to False.

You can set x to be writeable though and the operation will succeed: x = nc.variables["skintemp"][:] x.setflags(write=True) x *= 2.0

This behavior is consistent across any variables created for all files we've thrown at it (GRIB-2, NetCDF versions 3 and 4).

A sample program to illustrate the issue where we print the flags for the data arrays retrieved from all variables in a file:

!/usr/bin/env python

from future import print_function import sys import Nio

nc = Nio.open_file(sys.argv[1]) for v in nc.variables.keys(): arr = nc.variables[v][:] print(v) print(arr.flags) nc.close()

Below are some tests using one of NCAR's sample NetCDF files: https://www.unidata.ucar.edu/software/netcdf/examples/test_echam_spectral.nc

The operation fails in a conda environment created with the current release:

conda create -n pynio-test -c conda-forge pynio conda list | grep pynio

packages in environment at /home/mkay/.conda/envs/pynio-test:

pynio 1.5.1 py27h36f4ed2_2 conda-forge (pynio-test) > python ~/tmp/wrtest.py /tmp/test_echam_spectral-deflated.nc | grep WRITEABLE | sort | uniq -c 136 WRITEABLE : False

The above works if we fall back to the 1.5.0:

conda create -n pynio-test150 -c conda-forge pynio=1.5.0 source activate pynio-test150 conda list | grep pynio

packages in environment at /home/mkay/.conda/envs/pynio-test150:

pynio 1.5.0 py27h10838b4_5 conda-forge (pynio-test150) > python ~/tmp/wrtest.py /tmp/test_echam_spectral-deflated.nc | grep WRITEABLE | sort | uniq -c 136 WRITEABLE : True

mpkay commented 6 years ago

Looks like the package versions got filtered out.

In my 1.5.1 environment:

pynio                     1.5.1            py27h36f4ed2_2    conda-forge

In the 1.5.0 environment:

pynio                     1.5.0            py27h10838b4_5    conda-forge

Sorry about not catching that earlier.

bladwig1 commented 6 years ago

The numpy API behavior must have changed under the hood when PyNIO was ported to the newer 1.7 API in 1.5.1. Working on a fix for it.

khallock commented 6 years ago

Hi @mpkay, we just created a new PyNIO 1.5.2 release on conda-forge; if you continue to experience the same problem with the new build, please reopen this issue and let us know. Thanks!

mpkay commented 6 years ago

Hi @khallock -

Thanks for the quick turnaround! I created a test environment with the new package and my tests all pass now.