Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change dimension name in netcdf file with python

Tags:

python

netcdf

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.

like image 549
AJaramillo Avatar asked Apr 07 '26 11:04

AJaramillo


1 Answers

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.

like image 166
AJaramillo Avatar answered Apr 09 '26 00:04

AJaramillo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!