Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在numpy阵列中扩展一系列非均匀netcdf数据_Python_Arrays_Numpy_Netcdf - Fatal编程技术网

Python 在numpy阵列中扩展一系列非均匀netcdf数据

Python 在numpy阵列中扩展一系列非均匀netcdf数据,python,arrays,numpy,netcdf,Python,Arrays,Numpy,Netcdf,我是python的新手,如果已经有人问过我,我深表歉意 使用python和numpy,我试图通过迭代调用append()将多个netcdf文件中的数据收集到单个数组中 天真地说,我正在尝试这样做: from numpy import * from pupynere import netcdf_file x = array([]) y = [...some list of files...] for file in y: ncfile = netcdf_file(file,'r')

我是python的新手,如果已经有人问过我,我深表歉意

使用python和numpy,我试图通过迭代调用
append()
将多个netcdf文件中的数据收集到单个数组中

天真地说,我正在尝试这样做:

from numpy import *
from pupynere import netcdf_file

x = array([])
y = [...some list of files...]

for file in y:
    ncfile = netcdf_file(file,'r')
    xFragment = ncfile.variables["varname"][:]
    ncfile.close()
    x = append(x, xFragment)
x = [] # a normal python list, not np.array
y = [...some list of files...]

for file in y:
    ncfile = netcdf_file(file,'r')
    xFragment = ncfile.variables["varname"][:]
    ncfile.close()
    x.append(xFragment)

combined_array = concatenate(x, axis=0)
我知道在正常情况下这是个坏主意,因为它会在每次
append()
调用上重新分配新内存。但有两件事阻碍了x的预分配:

1) 文件沿轴0的大小不一定相同(但沿后续轴的大小应该相同),因此我需要事先读取每个文件的数组大小,以预先计算x的最终大小

然而

2) 据我所知,pupynere(和其他netcdf模块)在打开文件时将整个文件加载到内存中,而不仅仅是一个引用(例如其他环境中的许多netcdf模块)。因此,为了预先分配,我必须打开文件两次

有很多(>100)个大(>1GB)文件,所以根据我所知,过度分配和重塑是不现实的

我的第一个问题是我是否错过了一些聪明的方法来预先分配

我的第二个问题更严重。上述代码段适用于一维数组。但如果我试图加载一个矩阵,那么初始化就成了一个问题。我可以将一维数组附加到空数组:

append( array([]), array([1, 2, 3]) )
但我不能将空数组附加到矩阵:

append( array([]), array([ [1, 2], [3, 4] ]), axis=0)
我相信像x.extend(xFragment)这样的东西可以工作,但我认为numpy阵列没有这种功能。我还可以通过将第一个文件视为特例来避免初始化问题,但如果有更好的方法,我更愿意避免这种情况


如果有人能提供帮助或建议,或能发现我的方法存在问题,我将不胜感激。谢谢

您可以解决这两个问题,方法是首先将文件中的数组加载到数组列表中,然后使用来联接所有数组。大概是这样的:

from numpy import *
from pupynere import netcdf_file

x = array([])
y = [...some list of files...]

for file in y:
    ncfile = netcdf_file(file,'r')
    xFragment = ncfile.variables["varname"][:]
    ncfile.close()
    x = append(x, xFragment)
x = [] # a normal python list, not np.array
y = [...some list of files...]

for file in y:
    ncfile = netcdf_file(file,'r')
    xFragment = ncfile.variables["varname"][:]
    ncfile.close()
    x.append(xFragment)

combined_array = concatenate(x, axis=0)

您可以通过以下方法解决这两个问题:首先将文件中的数组加载到数组列表中,然后使用连接所有数组。大概是这样的:

from numpy import *
from pupynere import netcdf_file

x = array([])
y = [...some list of files...]

for file in y:
    ncfile = netcdf_file(file,'r')
    xFragment = ncfile.variables["varname"][:]
    ncfile.close()
    x = append(x, xFragment)
x = [] # a normal python list, not np.array
y = [...some list of files...]

for file in y:
    ncfile = netcdf_file(file,'r')
    xFragment = ncfile.variables["varname"][:]
    ncfile.close()
    x.append(xFragment)

combined_array = concatenate(x, axis=0)

netcdf数据以numpy数组的形式出现,因此这将为每个append语句创建如下数组列表:[array(…)、array(…)、array(…)…]。也许如果我先将numpy数组转换为常规列表,然后在最后再转换为numpy数组,这会奏效吗?在尝试了这个想法之后,我决定这可能不是我想要的。每次迭代我都必须调用xFragment.tolist(),这似乎并不比在第一次迭代中将x设置为xFragment和在后续迭代中使用x.append(xFragment)有所改进。这正是concatenate希望得到的。你是对的,我第一次尝试你的建议时一定没有正确使用concatenate。它似乎适用于一些较小的示例。不幸的是,在这些较大的数据集上,concatenate似乎太慢,或者根本不起作用(可能它使用了大量内存?)。我可能做错了什么,但你的建议似乎在原则上有效。感谢您的帮助。netcdf数据以numpy数组的形式提供,因此这将为每个append语句创建如下数组列表:[array(…)、array(…)、array(…)…]。也许如果我先将numpy数组转换为常规列表,然后在最后再转换为numpy数组,这会奏效吗?在尝试了这个想法之后,我决定这可能不是我想要的。每次迭代我都必须调用xFragment.tolist(),这似乎并不比在第一次迭代中将x设置为xFragment和在后续迭代中使用x.append(xFragment)有所改进。这正是concatenate希望得到的。你是对的,我第一次尝试你的建议时一定没有正确使用concatenate。它似乎适用于一些较小的示例。不幸的是,在这些较大的数据集上,concatenate似乎太慢,或者根本不起作用(可能它使用了大量内存?)。我可能做错了什么,但你的建议似乎在原则上有效。谢谢你的帮助。