gpt4 book ai didi

python - 分割一个大的ndarray

转载 作者:行者123 更新时间:2023-11-28 18:35:46 26 4
gpt4 key购买 nike

我对 python 很陌生,对 pandas、numpy 更陌生。我正在尝试格式化 GPS RINEX 文件,以便将文件拆分为卫星(总共 32 个)。然后每个文件(即卫星)应按纪元(30 秒间隔)格式化,然后每个信号的数据(总共 7 个)显示在相应的列中。例如:

SV1
2014-11-07 00:00:00 L1 L2 P1 P2 C1 S1 S2
2014-11-07 00:00:30 L1 L2 P1 P2 C1 S1 S2
2014-11-07 00:00:30 L1 L2 P1 P2 C1 S1 S2

我正在处理的代码,特别是函数是:

def read_data_chunk(self, RINEXfile, CHUNK_SIZE = 10000):
obss = np.empty((CHUNK_SIZE, TOTAL_SATS, len(self.obs_types)), dtype=np.float64) * np.NaN
llis = np.zeros((CHUNK_SIZE, TOTAL_SATS, len(self.obs_types)), dtype=np.uint8)
signal_strengths = np.zeros((CHUNK_SIZE, TOTAL_SATS, len(self.obs_types)), dtype=np.uint8)
epochs = np.zeros(CHUNK_SIZE, dtype='datetime64[us]')
flags = np.zeros(CHUNK_SIZE, dtype=np.uint8)

i = 0
while True:
hdr = self.read_epoch_header(RINEXfile)
#print hdr
if hdr is None:
break
epoch, flags[i], sats = hdr
epochs[i] = np.datetime64(epoch)
sat_map = np.ones(len(sats)) * -1
for n, sat in enumerate(sats):
if sat[0] == 'G':
sat_map[n] = int(sat[1:]) - 1
obss[i], llis[i], signal_strengths[i] = self.read_obs(RINEXfile, len(sats), sat_map)
i += 1
if i >= CHUNK_SIZE:
break

print "obss.ndim: {0}".format(obss.ndim)
print "obss.shape: {0}" .format(obss.shape)
print "obss.size: {0}".format(obss.size)
print "obss.dtype: {0}".format(obss.dtype)
print "obss.itemsize: {0}".format(obss.itemsize)
print "obss: {0}".format(obss)

y = np.split(obss, 32, 1)
print "y.ndim: {0}".format(y[3].ndim)
print "y.shape: {0}" .format(y[3].shape)
print "y.size: {0}".format(y[3].size)
print "y_0: {0}".format(y[3])

return obss[:i], llis[:i], signal_strengths[:i], epochs[:i], flags[:i]

打印语句只是为了了解所涉及的维度,其结果:

obss.ndim: 3
obss.shape: (10000L, 32L, 7L)
obss.size: 2240000
obss.dtype: float64
obss.itemsize: 8
y.ndim: 3
y.shape: (10000L, 1L, 7L)
y.size: 70000

我遇到的确切问题是如何精确操作,以便将阵列分成后续的 32 个部分(即卫星)。以下是到目前为止的输出示例:

sats = np.rollaxis(obss, 1, 0) 
sat = sats[5] #sv6
sat.shape: (10000L, 7L)
sat.ndim: 2
sat.size: 70000
sat.dtype: float64
sat.item
size: 8
sat: [[ -7.28308440e+06 -5.66279406e+06 2.38582902e+07 ..., 2.38582906e+07 4.70000000e+01 4.20000000e+01] [ -7.32362993e+06 -5.69438797e+06 2.38505736e+07 ..., 2.38505742e+07 4.70000000e+01 4.20000000e+01] [ -7.36367675e+06 -5.72559325e+06 2.38429526e+07 ..., 2.38429528e+07 4.60000000e+01 4.20000000e+01]

上面的输出是第 6 颗卫星(“sat”)的输出,显示了前 3 个时期的信号。我尝试使用以下代码分别打开新文件,但生成的文本文件仅显示以下输出:

代码:

for i in range(32): 
sat = obss[:, i]
open(((("sv{0}").format(sat)),'w').writelines(sat))

在文本文件中输出:

ø ø ø ø ø ø ø 

很明显,我忽略了对数组的操作有问题。 read_data_chunk 函数从 read_data 函数调用:

def read_data(self, RINEXfile): 
obs_data_chunks = []
while True:
obss, _, _, epochs, _ = self.read_data_chunk(RINEXfile)
if obss.shape[0] == 0:
break

obs_data_chunks.append(pd.Panel( np.rollaxis(obss, 1, 0), items=['G%02d' % d for d in range(1, 33)], major_axis=epochs,minor_axis=self.obs_types).dropna(axis=0, how='all').dropna(axis=2, how='all'))

print "obs_data_chunks: {0}".format(obs_data_chunks)
self.data = pd.concat(obs_data_chunks, axis=1)

我尝试的下一步是在上面的代码中,因为我认为这个数组可能是要被操作的正确数组。最终打印语句:

obs_data_chunks: [<class 'pandas.core.panel.Panel'> 
Dimensions: 32 (items) x 2880 (major_axis) x 7 (minor_axis)
Items axis: G01 to G32
Major_axis axis: 2014-04-27 00:00:00 to 2014-04-27 23:59:30
Minor_axis axis: L1 to S2]

我试图弄清楚如何使用以下方法处理 obs_data_chunks 数组:

odc = np.rollaxis(obs_data_chunks, 1) 
odc_temp = odc[5]

但收到错误:AttributeError: 'list' object has no attribute 'ndim'

最佳答案

这取决于您想对这 32 个卫星子集做什么。据我所知,你目前拥有 obss 的方式,形状为 (10000, 32, 7),你已经在某种程度上“拆分”了它。您可以通过以下方式访问它们:

  1. 沿“卫星”维度切片,即 axis=1:

    sat = obss[:, 0]  # all the data for satellite 0, with shape (10000, 7)
    sat = obss[:, i] # for any i from 0 through 31.
    sats = obss[:, :3] # the first three satellites
  2. 如果你发现你主要是通过卫星索引,你可以用np.rollaxis把它的轴移到前面。 :

    sats = np.rollaxis(obss, 1)
    sats.shape
    # (32, 10000, 7)
    sat = sats[i] # satellite i, equivalent to obss[:, i]
    sat = sats[:3] # first three satellites
  3. 如果您想像在 y = np.split(obss) 示例中那样遍历卫星,更简单的方法是:

    for i in range(32):
    sat = obss[:, i]
    ...

    或者,如果您为 sats 滚动轴,您可以这样做:

    sats = np.rollaxis(obss, 1)
    for sat in sats:
    ...
  4. 最后,如果你真的想要卫星列表,你可以这样做

    sats = np.rollaxis(obss, 1)
    satlist = list(sats)

关于python - 分割一个大的ndarray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32570449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com