Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add variable to an existing Netcdf4 file using Python

I'd like to add an additional variable containing the Julian dates to an existing set of NetCDF climate data files. I've done a bit of python scripting but it has been a while so I'm rather rusty. After reading the "netCDF4 module" documentation I've tried to write the script to create a new variable using:

newvarJD= infile.create.Variable('Julian_Day','i4',                                      
                                ('lon','lat','time'))# attributes, varname, 
                                                       dtype, dimensions=()
                                    

but I get an "AttributeError: NetCDF: Attribute not found" when it reaches this line:

File "C:/WinPython64/WinPython-64bit-3.4.4.6Qt5/notebooks/netcfdfill.py", line 35, in newvarJD= infile.create.Variable('Julian_Day','i4',

So, I thought that the dimensions needed to be declared so I changed the code to do so:

lat_nc = infile.dimensions['lat'] #define dimensions for create.variable
lon_nc = infile.dimensions['lon']
time_nc = infile.dimensions['time'] 

but now I get a new error that says KeyError: 'lat'

I'm including my attempt of the script as I imagine I've several more errors. Would you be able to help me?

#**************************
# Access standard libraries
#**************************
from netCDF4 import Dataset
import numpy as np
import os
# Set the input/output directories
wrkDir = 'C:/Netcfd/BCSD/test'
Minifile = wrkDir + '/tasmin'

#***************************
# Add a Julian date variable to all *.nc file in directory
#****************************


     
inList = os.listdir(Minifile)  # List all the files in the 'tasmin' 
                                 directory
print(inList)

for fileName in inList:     # Step through each file
    ifile = fileName
    baseName, extension = os.path.splitext(ifile)
    if extension == '.nc':
        infile = Dataset("ifile", "r+", format="NETCDF4")#append to add 
                                                          Julian
        lat_nc = infile.dimensions['lat'] #define dimensions for 
                                           create.variable
        lon_nc = infile.dimensions['lon']
        time_nc = infile.dimensions['time']
        newvarJD= infile.create.Variable('Julian_Day','i4',
                                        ('lon_nc','lat_nc','time_nc'))# 
                                                varname,dtype, dimensions=()
        newvarJD.units= "Days"
        newvarJD.long_name = 'Annual Julian Days'
    
        JD = 0 # counter used to set Julian day value
        for i in range(len(time_nc)):
           JD = JD + 1 # start with Julina Day 1
           newvarJD = np.asarray(infile[:,:,:,JD])# write data into the 
                                                    variable created
        print ('New NC dims ->'(infile.shape))
    infile.close()    
     
like image 277
MapleMatrix Avatar asked Oct 14 '25 04:10

MapleMatrix


1 Answers

The function is called createVariable(). Your code should work with that fix.

like image 155
lpoorthuis Avatar answered Oct 16 '25 22:10

lpoorthuis



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!