I am having a hard time when I want to add new groups on a existing netcdf file with python. MWE works on my machine.
MWE
import os
import numpy as np
from scipy.io import netcdf
from netCDF4 import Dataset
try:
os.remove('test.nc')
except OSError:
pass
mydata = np.linspace(1,30,30)
g=Dataset('test.nc','w',format='NETCDF4')
g.description = 'The data in this file was created for a test'
g.close()
#Do stuff
g=Dataset('test.nc','r+',format='NETCDF4')
g1 = g.createGroup('grp1')
g1.createDimension('dim', len(mydata))
ncdata = g1.createVariable('data1', 'f8', ('dim',)); ncdata[:] = mydata
ncdata = g1.createVariable('data2', 'f8', ('dim',)); ncdata[:] = mydata
g.close()
#Do stuff
g=Dataset('test.nc','r+',format='NETCDF4')
g1 = g.createGroup('grp2')
g1.createDimension('dim', len(mydata))
ncdata = g1.createVariable('data1', 'f8', ('dim',)); ncdata[:] = mydata
ncdata = g1.createVariable('data2', 'f8', ('dim',)); ncdata[:] = mydata
g.close()
However I can't open my file with ncview
, instead I get that
Ncview 1.93g David W. Pierce 24 February 2009 http://meteora.ucsd.edu:80/~pierce/ncview_home_page.html Copyright (C) 1993 through 2009, David W. Pierce Ncview comes with ABSOLUTELY NO WARRANTY; for details type
ncview -w'. This is free software licensed under the Gnu General Public License version 3; type
ncview -w'. This is free software licensed under the Gnu General Public License version 3; type
ncview -w'. This is free software licensed under the Gnu General Public License version 3; type
ncview -c' for redistribution details.no displayable variables found!
It says no displayable variables found!
, but via ncdump -h test.nc
I am able to see the following lines;
netcdf test {
// global attributes: :description = "The data in this file was created for a test" ;
group: grp1 { dimensions: dim = 30 ; variables: double data1(dim) ; double data2(dim) ; } // group grp1
group: grp2 { dimensions: dim = 30 ; variables: double data1(dim) ; double data2(dim) ; } // group grp2 }
Am I missing something in my Python
script? (I am thinking ncview
is okay cause I am able to see very same data when I do not save them inside a netcdf group )
You might try upgrading your ncview version; 1.93g is pretty old. 2.1.6 (released fall 2015) is available here: http://meteora.ucsd.edu/~pierce/ncview_home_page.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.