AJaramillo
AJaramillo

Reputation: 440

Change dimension name in netcdf file with python

Hi I want to edit some information in a NetCDF file, to give an example suppose that you have a ncdump of the file with the next info:

NetCDF dimension information:
 Name: lon
    size: 144
    type: dtype('float64')
 Name: lat
    size: 73
    type: dtype('float64')
 Name: time
    size: 29220
    type: dtype('float64')
NetCDF variable information:
 Name: rlut
    dimensions: (u'time', u'lat', u'lon')
    type: dtype('float32')

I want to change 'lon' for 'longitude'. I tried with:

from netCDF4 import Dataset
path="Here goes the file path"
f=Dataset(path,'r+')
f.renameDimension(u'lon',u'longitude')
f.close()

But after this when I tried to read the file again for doing something different the file doesn't work anymore.

Any help I will Thank you.

Upvotes: 2

Views: 8279

Answers (4)

Eric
Eric

Reputation: 147

Even though this is quite an old post it might still be valuable to add this:

The indicated method by Ajaramillo did not work in my case because it was missing to rename also the variable name, not just the dimension name. This worked for me:

from netCDF4 import Dataset
path="Here goes the file path"
f=Dataset(path,'r+')
f.renameDimension(u'lon',u'longitude')
f.renameVariable(u'lon',u'longitude')
f.renameDimension(u'lat',u'latitude')
f.renameVariable(u'lat',u'latitude')
f.close()

Upvotes: 3

AJaramillo
AJaramillo

Reputation: 440

Thanks N1B4 for the suggestion of using NCO, it is a very good option for working and editing NetCDF files.

I want to post here a sketch of my solution for anyone that might be interested in modifying a NetCDF file using python and the netcdf4 library. The idea is to create a new NetCDF file importing the info from an existing file.

#First import the netcdf4 library
from netCDF4 import Dataset  # http://code.google.com/p/netcdf4-python/

# Read en existing NetCDF file and create a new one
# f is going to be the existing NetCDF file from where we want to import data
# and g is going to be the new file.

f=Dataset('pathtoexistingfile','r') # r is for read only
g=Dataset('name of the new file','w') # w if for creating a file
                                      # if the file already exists it  
                                      # file will be deleted 


# To copy the global attributes of the netCDF file  

for attname in f.ncattrs():
    setattr(g,attname,getattr(f,attname))

# To copy the dimension of the netCDF file

for dimname,dim in f.dimensions.iteritems():
       # if you want to make changes in the dimensions of the new file
       # you should add your own conditions here before the creation of the dimension.
        g.createDimension(dimname,len(dim))


# To copy the variables of the netCDF file

for varname,ncvar in f.variables.iteritems():
       # if you want to make changes in the variables of the new file
       # you should add your own conditions here before the creation of the variable.
       var = g.createVariable(varname,ncvar.dtype,ncvar.dimensions)
       #Proceed to copy the variable attributes
       for attname in ncvar.ncattrs():  
          setattr(var,attname,getattr(ncvar,attname))
       #Finally copy the variable data to the new created variable
       var[:] = ncvar[:]


f.close()
g.close()

I hope that this might work for you.

Upvotes: 4

Eric Bridger
Eric Bridger

Reputation: 3794

I think you'd also need to modify any references to the renamed dimension. E.g. your variable, rlut, which has a dimension of 'lon', which has been renamed to 'longitude'. Not sure if this can be done via in place editing. You may need to create a new copy of the file using:

createVariable('rlut', 'f4', ('time', 'lat', 'longitude')

Upvotes: 1

N1B4
N1B4

Reputation: 3453

If you don't need to use Python, I'd recommend NCO's ncrename function: http://nco.sourceforge.net/nco.html#ncrename-netCDF-Renamer

ncrename -d lon,longitude sample_file.nc  

Upvotes: 2

Related Questions