运行下面的测试代码,得到一些奇怪的结果:
import numpy as np
from netCDF4 import Dataset
array1 = np.array([[1,2,3]])
array2 = np.array([[4,5,6],[7,8,9]])
rootgrp = Dataset("D:/test.nc", "w", format="NETCDF4")
rootgrp.createDimension("0", None)
rootgrp.createDimension("1", None)
variable1 = rootgrp.createVariable("variable1", array1.dtype.str, ("0", "1"), fill_value=-1)
variable2 = rootgrp.createVariable("variable2", array2.dtype.str, ("0", "1"), fill_value=-1)
variable1[:] = array1
variable2[:] = array2
result1 = rootgrp['variable1'][:]
result2 = rootgrp['variable2'][:]result1返回:
masked_array(
data=[[1, 2, 3],
[--, 0, 0]],
mask=[[False, False, False],
[ True, False, False]],
fill_value=-1)但我期望填充值-1出现在第二行的所有值中,就像掩码一样,所有的True都在第二行,因为没有数据,如下所示:
masked_array(
data=[[1, 2, 3],
[-1, -1, -1]],
mask=[[False, False, False],
[ True, True, True]],
fill_value=-1)然后result2返回:
masked_array(
data=[[4, 5, 6],
[7, 8, 9]],
mask=False,
fill_value=999999)但是需要一个填充值also -1,如下所示:
masked_array(
data=[[4, 5, 6],
[7, 8, 9]],
mask=False,
fill_value=-1)我到底搞错了什么?
发布于 2021-08-08 07:26:34
我认为这种行为源于对netCFD文件存储数据方式的误解。从我从您的代码中可以看出,您正在尝试创建一个包含二维数据的文件,然后添加要存储的数据。
除了值之外,netCFD文件还保存每个尺寸的值范围。如名称维度所示,这些值必须保存为一维变量。这会告诉文件数据有多大。将尺寸保存为变量的需求在tutorial中进行了说明
确实,可以使用无限的变量,就像您在rootgrp.createDimension("0", None)行中使用None所做的那样,但这些变量通常仍然具有一维变量,因为您知道它们是维度。
在这种情况下,您可以使用以下代码来达到预期的效果:
import numpy as np
from netCDF4 import Dataset
# define the variables ranges
variable_array1 = np.array([1, 2])
variable_array2 = np.array([1, 2, 3])
# define the data to be stored
data_array1 = np.array([[1, 2, 3]])
data_array2 = np.array([[4, 5, 6], [7, 8, 9]])
rootgrp = Dataset("D:/test.nc", "w", format="NETCDF4")
rootgrp.createDimension("0", 2)
rootgrp.createDimension("1", 3)
# register the variables
variable1 = rootgrp.createVariable("variable1", variable_array1.dtype, ("0",), fill_value=-1)
variable2 = rootgrp.createVariable("variable2", variable_array2.dtype, ("1",), fill_value=-1)
# register the data
data1 = rootgrp.createVariable("data1", data_array1.dtype, ("0", "1"), fill_value=-1)
data2 = rootgrp.createVariable("data2", data_array2.dtype, ("0", "1"), fill_value=-1)
# add the variable values
variable1[:] = variable_array1
variable2[:] = variable_array2
# add the data
data1[0, :] = data_array1
data2[:, :] = data_array2
result1 = rootgrp['data1'][:]
result2 = rootgrp['data2'][:]如果您确实需要将尺寸变量设置为无限制,则仍然需要首先添加当前尺寸的值。
直接添加数据会导致与您面临的问题相同的错误。为了解决这个问题,我们可以使用NumPy附加数据,然后将其传递给文件。
在这种情况下,以下代码可能会有所帮助:
import numpy as np
from netCDF4 import Dataset
# define the variables ranges
variable_array1 = np.array([1, 2])
variable_array2 = np.array([1, 2, 3])
# define the data to be stored
data_array1 = np.array([[1, 2, 3]])
data_array2 = np.array([[4, 5, 6], [7, 8, 9]])
rootgrp = Dataset("D:/test.nc", "w", format="NETCDF4")
rootgrp.createDimension("0", None)
rootgrp.createDimension("1", None)
# register the variables
variable1 = rootgrp.createVariable("variable1", variable_array1.dtype, ("0",), fill_value=-1)
variable2 = rootgrp.createVariable("variable2", variable_array2.dtype, ("1",), fill_value=-1)
# register the data
data1 = rootgrp.createVariable("data1", data_array1.dtype, ("0", "1"), fill_value=-1)
data2 = rootgrp.createVariable("data2", data_array2.dtype, ("0", "1"), fill_value=-1)
# add the variable values
variable1[:] = variable_array1
variable2[:] = variable_array2
# convert the data to python
data = data1[:]
# append the data
data[0, :] = data_array1
data1[:] = data
# convert the data to python
data = data2[:]
# append the data
data[:] = data_array2
data2[:, :] = data
result1 = rootgrp['data1'][:]
result2 = rootgrp['data2'][:]如果你不同意我的观点,文件需要有尺寸的一维变量才能工作,你可能已经找到了库的边缘情况,在这种情况下,你可能会认为它是库中的一个bug,你可能想在project's GitHub中提交一个工单。
这是我的变通方法:
import numpy as np
from netCDF4 import Dataset
array1 = np.array([[1, 2, 3]])
array2 = np.array([[4, 5, 6], [7, 8, 9]])
rootgrp = Dataset("D:/test.nc", "w", format="NETCDF4")
rootgrp.createDimension("0", None)
rootgrp.createDimension("1", None)
# register the variables
variable1 = rootgrp.createVariable("variable1", array1.dtype, ("0", "1"), fill_value=-1)
variable2 = rootgrp.createVariable("variable2", array2.dtype, ("0", "1"), fill_value=-1)
# add the variable values
variable2[:] = array2
variable = np.full_like(variable2[:], -1)
variable[:] = array2
variable2[:] = variable
# add the variable values
variable1[:] = array1
variable = np.full_like(variable1[:], -1)
variable[0, :] = array1
variable1[:] = variable
result1 = rootgrp['variable1'][:]
result2 = rootgrp['variable2'][:]同样,如果你不需要维度是无限的:
import numpy as np
from netCDF4 import Dataset
array1 = np.array([[1, 2, 3]])
array2 = np.array([[4, 5, 6], [7, 8, 9]])
rootgrp = Dataset("D:/test.nc", "w", format="NETCDF4")
rootgrp.createDimension("0", 2)
rootgrp.createDimension("1", 3)
# register the variables
variable1 = rootgrp.createVariable("variable1", array1.dtype, ("0", "1"), fill_value=-1)
variable2 = rootgrp.createVariable("variable2", array2.dtype, ("0", "1"), fill_value=-1)
# add the variable values
variable1[0, :] = array1
variable2[:] = array2
result1 = rootgrp['variable1'][:]
result2 = rootgrp['variable2'][:]https://stackoverflow.com/questions/68503306
复制相似问题