4

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()    
     
ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
MapleMatrix
  • 99
  • 1
  • 2
  • 5

2 Answers2

2

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

lpoorthuis
  • 111
  • 4
1

This didn't work for me. The problem was the assignments as shown below. Instead of:

lat_nc = infile.dimensions['lat']

use

lat_nc = infile.dimensions['lat'].name

and make sure you don't put quotes around the name of the dimension when creating the variable:

newvarJD= infile.createVariable('Julian_Day','i4',(lon_nc,lat_nc,time_nc))
Nick
  • 3,454
  • 6
  • 33
  • 56
gthom74
  • 11
  • 2