An easier way to create NetCDF files using Python

I just wrote a module that I should’ve written years ago. It’s a simple wrapper for Python NetCDF modules that simplifies creating NetCDF files. Instead of writing dozens of createVariable and createDimension, and adding lots of redundant information like variable typecodes and lengths, let’s use a simple DSL to create our file:

from pup import *

class Test(NetCDF):
    # NC_GLOBAL attributes go here
    history = 'Created for a test'

    # dimensions need to be set explicitly only when they
    # have no variable associated with them
    dim0 = Dimension(2)

    # variables that don't specify dimensions are assumed to
    # be their own dimension
    time = Variable(range(10), record=True, units='days since 2008-01-01')

    # now a variable with dimensions (time,)
    temperature = Variable(range(10), (time,), units='deg C')

Test.save('simple.nc')

The module is called “pup”, just so I can call the file “pup.py”. Note that is does some smart things in order to make the code more concise: for example, we don’t need to define the “time” dimension, or neither specify the typecode of the temperature variable.

To install it: pip install Puppy. Enjoy!