1
import netCDF4

def get_nc_var3d(nc_hndl, var, year):
    """
    Get value from netcdf for variable var for year
    :param nc_hndl: handle to netcdf file
    :param var: which variable to extract
    :param year:
    :return:
    """
    val = nc_hndl.variables[var][int(year), :, :]

    return val

I have this function which gets a slice from a netcdf file (array with dimension of 720 x 1440). I call this function from different functions multiple times. I would like to cache the output from this function and use that next time it is called with the same parameters. How can I do that?

Note: functools lrucache does not work as it returns this error:

TypeError: unhashable type: 'MaskedArray'
user308827
  • 21,227
  • 87
  • 254
  • 417
  • You're going to run into the same issue with pretty much any cache solution, since a cache generally relies on hashing to look up argument tuples in a table. – a p May 10 '16 at 02:16
  • see related query here: http://stackoverflow.com/questions/37129754/joblib-userwarning-while-trying-to-cache-results – user308827 May 10 '16 at 05:28

0 Answers0