In [1]:
%%file model.c
#include <stdio.h>
#include "netcdf.h"

// a simple model, conforming to IRF convention
void init() {
    /* initialize the model */
}

void update(double t) {
   /* do a timestep */   
   int stat;
   int ncid;
   int varid;
   // fixed sizes for example
   size_t start[] = {0};
   size_t count[] = {10};
   double data[10];
   
   // open an in memory file
   stat = nc_open("diskless.nc", NC_DISKLESS, &ncid);
   printf("opening: %s\n", nc_strerror(stat));
   // Lookup the rain variable
   stat = nc_inq_varid(ncid, "rain", &varid);   
   printf("get variable: %s (%d)\n", nc_strerror(stat), varid);
   // Read the data
   stat = nc_get_vara_double(ncid, varid, start, count, data);
   printf("get data: %s (%f - %f)\n", nc_strerror(stat), data[0], data[9]);
   // No need to close the diskless file, responsibilty of creator
   
}                      

void finalize() {
   /* clean up the model */
}


Overwriting model.c

In [3]:
%%bash
# compile
gcc -c -fPIC -I/home/fedor/.local/include model.c 
# link
gcc model.o -shared  -lnetcdf -L/home/fedor/.local/lib -o libmodel.so

In [4]:
%%python
# do this in a subprocess so we can get the stdout
# wrap the model
import ctypes
model = ctypes.cdll.LoadLibrary('libmodel.so')
model.update.argtypes = [ctypes.c_double]


# create a netCDF file
import netCDF4
ds = netCDF4.Dataset("diskless.nc", mode="w", diskless=True, persist=False)
ds.createDimension("points", 10)
rain = ds.createVariable("rain", "double", ("points",))
rain[:] = range(10)

# run the model
model.init()
model.update(1.0)
model.finalize()

ds.close()


opening: No error
get variable: No error (0)
get data: No error (0.000000 - 9.000000)

In [5]:
!ls diskless.nc


ls: cannot access diskless.nc: No such file or directory

In [ ]: