Created
December 2, 2013 22:28
-
-
Save kwilcox/7760238 to your computer and use it in GitHub Desktop.
Avoid numpy/netcdf4-python memory errors on flat arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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