3
我使用python創建netCDF文件。當我嘗試將值(數據)分配給變量的部分(或切片)時,取決於切片的「類型」,我可以或不能分配這些值。切片類型影響numpy陣列的可變性
我不知道爲什麼。任何有助於瞭解爲什麼這是將不勝感激。
例如爲:
import numpy as np
from netCDF4 import Dataset
nb_steps = 2
nb_lat = 3
nb_lon = 4
# open/create file
f = Dataset('/home/ccorbel/Desktop/test.nc', 'w', format='NETCDF3_CLASSIC')
f.createDimension('lat', nb_lat)
f.createDimension('lon', nb_lon)
f.createDimension('time', nb_steps)
# create/fill variables
variables = {}
variables['t'] = f.createVariable('temperature', 'float64', ('time', 'lat', 'lon'))
variables['t'][...] = np.zeros((nb_steps, nb_lat, nb_lon))
# "equivalent" to [0, :, ::-1]
slc = [0, slice(None, None, None), slice(None, None, -1)]
# "equivalent" to [0, :, :]
slc2 = [0, slice(None, None, None), slice(None, None, None)]
# "equivalent" to [:, ::-1]
slc3 = [ slice(None, None, None), slice(None, None, -1)]
print type(variables['t'])
# type 'netCDF4.Variable'
print type(variables['t'][slc])
# type 'numpy.ndarray'
print type(variables['t'][slc][...])
# type 'numpy.ndarray'
print np.shape(variables['t'][slc])
# (3, 4)
# variables['t'][slc] = np.random.random((nb_lat, nb_lon))
# return IndexError: too many indices
variables['t'][slc][...] = np.random.random((nb_lat, nb_lon))
print '\n', variables['t'][...]
# [[[ 0. 0. 0. 0.]
# [ 0. 0. 0. 0.]
# [ 0. 0. 0. 0.]]
#
# [[ 0. 0. 0. 0.]
# [ 0. 0. 0. 0.]
# [ 0. 0. 0. 0.]]]
variables['t'][...] = np.zeros((nb_steps, nb_lat, nb_lon)) # reset
variables['t'][slc2] = np.random.random((nb_lat, nb_lon))[slc3]
print '\n', variables['t'][...]
# [[[ 0.17502009 0.98414122 0.89686025 0.11072791]
# [ 0.51351626 0.09234043 0.54314083 0.937711 ]
# [ 0.98732418 0.22714407 0.87387761 0.44653219]]
# [[ 0. 0. 0. 0. ]
# [ 0. 0. 0. 0. ]
# [ 0. 0. 0. 0. ]]]
variables['t'][...] = np.zeros((nb_steps, nb_lat, nb_lon)) # reset
#variables['t'][0, :, ::-1] = np.random.random((nb_lat, nb_lon))
# return IndexError: too many indices
variables['t'][0, :, ::-1][...] = np.random.random((nb_lat, nb_lon))
print '\n', variables['t'][...]
# [[[ 0. 0. 0. 0.]
# [ 0. 0. 0. 0.]
# [ 0. 0. 0. 0.]]
# [[ 0. 0. 0. 0.]
# [ 0. 0. 0. 0.]
# [ 0. 0. 0. 0.]]]
variables['t'][...] = np.zeros((nb_steps, nb_lat, nb_lon)) # reset
variables['t'][0, :, :] = np.random.random((nb_lat, nb_lon))[:, ::-1]
print '\n', variables['t'][...]
# [[[ 0.61406835 0.11069783 0.28667398 0.45018246]
# [ 0.3833354 0.98871281 0.55559104 0.60415683]
# [ 0.75200954 0.75106639 0.11688565 0.14264615]]
# [[ 0. 0. 0. 0. ]
# [ 0. 0. 0. 0. ]
# [ 0. 0. 0. 0. ]]]
variables['t'][...] = np.zeros((nb_steps, nb_lat, nb_lon)) # reset
variables['t'][0, :, :] = np.random.random((nb_lat, nb_lon))[slc3]
print '\n', variables['t'][...]
# [[[ 0.09437484 0.45757906 0.81116891 0.23580254]
# [ 0.37316425 0.06768454 0.20259876 0.42127472]
# [ 0.78879307 0.62535419 0.08942293 0.68789143]]
# [[ 0. 0. 0. 0. ]
# [ 0. 0. 0. 0. ]
# [ 0. 0. 0. 0. ]]]
f.close()
嗯,我仍然不知道爲什麼,但是,我也相信這是因爲右側的「double」索引[] []。我還記得曾經在某處閱讀索引時正在創建一個副本,這可能會解釋,因爲分配可能是對A的(片)副本而不是對A本身的副本。 – Christophe
你可以閱讀更多關於numpy的索引/切片里爾:http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html 但請記住,有一些黑暗魔法,使得在分度作業的左側正常工作。這個黑暗魔法並不是爲了處理'a [slc1] [slc2]'而設計的。在你的情況下,雖然你正在做'[slc1] [...] = something',那麼爲什麼不寫'a [slc1] = something'? –