[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[python #CBE-169305]: netcdf4 in python



Hi,

Unfortunately, there is no way to delete variables from a netCDF file, even at 
the C level. For more information, see this netcdf-c mailing list post:

https://www.unidata.ucar.edu/mailing_lists/archives/netcdfgroup/1998/msg00050.html

As far as switching between define and data mode, I'm not having any problems:

    from netCDF4 import Dataset
    import numpy as np

    nc = Dataset('test2.nc', 'w')
    var = nc.createVariable('temp', np.float32)
    var[:] = 1.
    nc.close()

    nc = Dataset('test2.nc', 'r+')
    var2 = nc.createVariable('nextTemp', np.int16)
    var2[:] = 5
    nc.close()

    print(Dataset('test2.nc', 'r'))

prints:

<class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF4 data model, file format HDF5):
    dimensions(sizes): 
    variables(dimensions): float32 temp(), int16 nextTemp()
    groups: 


There must be something specific to your code that's going wrong. I'm not sure 
if it'a bug or not. If you're still having problems with the error about 
switching modes, I'd recommend opening an issue at:

https://github.com/Unidata/netcdf4-python/issues/new

Ryan

> Hi,
> 
> I am using the netcdf4 package in python. I want to read a netcdf file,
> edit some variables, delete some variables, and then overwrite the original
> file with that. I am using the "r+" mode for that. I am running into 2
> problems: First, the webpage indicates that netcdf4 automatically switches
> between define mode and data mode, but when I try to run my code, I get an
> error message: "RuntimeError: NetCDF: Operation not allowed in define
> mode." I also get that error when I try to access the data while I am in
> python debugging mode (ipdb). Is there a way to actively switsch between
> define and data mode?
> 
> 
> Second, I want to delete some of the existing variables:
> 
> for vv in self.nc_.variables.keys():
> if vv not in keep:
> del self.nc_.variables[vv]
> 
> That deletes the variables in the netcdf file (while in ipdb mode), but
> when I look at the resulting netcdf file with ncdump, the renaming worked
> while the deleted variables are back in the file.
> 
> How can I delete variables in netcdf 4?

Ticket Details
===================
Ticket ID: CBE-169305
Department: Support Python
Priority: Low
Status: Closed
===================
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata 
inquiry tracking system and then made publicly available through the web.  If 
you do not want to have your interactions made available in this way, you must 
let us know in each email you send to us.