Python Loop and save to NetCDF


I am trying to create a new dimension and add current data to a new variable in a NetCDF file using python.

Lat/Lon are 2D arrays (variables). id is a dimension.

from netCDF4 import Dataset

data = Dataset(netdata,'a')
lat = data.variables['lat'][:]
lon = data.variables['lon'][:]
id = data.dimensions['id']

ageme = []

age = 1

new_age = data.createVariable('age', np.float32)           

for i in range(0,len(id)):
    if lat[0,i] != -10.85000038:
        for eachstep in range(0,len(lat)):
            ageme = i, lat[eachstep, i], lon[eachstep,i], age
            new_age = ageme
            age = age + 1

I think there is a small mistake around "new_age = ageme" this bit of code, but can't quite fix it!

This video can help you solving your question :)
By: admin