Skip to content

Instantly share code, notes, and snippets.

@kwilcox
Created December 2, 2013 22:28
Show Gist options
  • Save kwilcox/7760238 to your computer and use it in GitHub Desktop.
Save kwilcox/7760238 to your computer and use it in GitHub Desktop.
Avoid numpy/netcdf4-python memory errors on flat arrays
def set_data_array(nc, variable, data):
# What is 50% of the available memory
available_memory = psutil.virtual_memory().available * .50
if data.nbytes > available_memory:
# Houston...
# Try 10 sub arrays. Should have some logic here to determine
# a better number of arrays to split this into
smallers = np.array_slice(data, 10)
i = 0 # track start index
j = 0 # track end index
for small in smallers:
j = i + (small.size - 1)
variable[i:j] = small
i += small.size
nc.sync()
else:
# We can fit in memory twice
variable[:] = data
nc.sync()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment