简体   繁体   English

使用python将多个2d数组合并/合并为单个数组

[英]combining/merging multiple 2d arrays into single array by using python

I have four 2 dimensional np arrays. 我有四个二维np数组。 Shape of each array is (203 , 135). 每个阵列的形状是(203,135)。 Now I want join all these arrays into one single array with respect to latitude and longitude. 现在,我想将所有这些数组就纬度和经度合并为一个数组。

I have used code below to read data 我已使用下面的代码读取数据

import pandas as pd
import numpy as np
import os
import glob
from pyhdf import SD
import datetime
import mpl_toolkits.basemap.pyproj as pyproj

DATA = ({})
files = glob.glob('MOD04*')
files.sort()
for n, f in enumerate(files):       
    SDS_NAME='Deep_Blue_Aerosol_Optical_Depth_550_Land'
    hdf=SD.SD(f)
    lat = hdf.select('Latitude')
    latitude = lat[:]
    min_lat=latitude.min()
    max_lat=latitude.max()
    lon = hdf.select('Longitude')
    longitude = lon[:]
    min_lon=longitude.min()
    max_lon=longitude.max()
    sds=hdf.select(SDS_NAME)
    data=sds.get()

    p = pyproj.Proj(proj='utm', zone=45, ellps='WGS84')
    x,y = p(longitude, latitude)

    def set_element(elements, x, y, data):
        # Set element with two coordinates.
        elements[x + (y * 10)] = data

    elements = []
    set_element(elements,x,y,data)

But I got error: only integer arrays with one element can be converted to an index 但是我得到了一个错误:只有具有一个元素的整数数组可以转换为索引

you can find the data: https://drive.google.com/open?id=0B2rkXkOkG7ExMElPRDd5YkNEeDQ 您可以找到数据: https : //drive.google.com/open?id=0B2rkXkOkG7ExMElPRDd5YkNEeDQ

I have created toy datasets for this problem as per requested. 我已根据要求为该问题创建了玩具数据集。 what I want is to get one single array from four (a,b,c,d) arrays. 我想要的是从四个(a,b,c,d)数组中获得一个单个数组。 whose dimension should be something like (406, 270) 其尺寸应类似于(406,270)

a = (np.random.rand(27405)).reshape(203,135)
b = (np.random.rand(27405)).reshape(203,135)
c = (np.random.rand(27405)).reshape(203,135)
d = (np.random.rand(27405)).reshape(203,135)
a_x = (np.random.uniform(10,145,27405)).reshape(203,135)
a_y = (np.random.uniform(204,407,27405)).reshape(203,135)
d_x = (np.random.uniform(150,280,27405)).reshape(203,135)
d_y = (np.random.uniform(204,407,27405)).reshape(203,135)
b_x = (np.random.uniform(150,280,27405)).reshape(203,135)
b_y = (np.random.uniform(0,202,27405)).reshape(203,135)
c_x = (np.random.uniform(10,145,27405)).reshape(203,135)
c_y = (np.random.uniform(0,202,27405)).reshape(203,135)

any help? 有什么帮助吗?

This should be a comment, yet the comment space is not enough for these questions. 这应该是评论,但评论空间不足以解决这些问题。 Therefore I am posting here: 因此,我在这里发布:

You say that you have 4 input arrays (a,b,c,d) which are somehow to be intergrated into an output array. 您说您有4个输入数组(a,b,c,d),它们以某种方式集成到一个输出数组中。 As far as is understood, two of these arrays contain positional information (x,y) such as longitude and latitude. 据了解,这些阵列中的两个包含位置信息(x,y),例如经度和纬度。 The only line in your code, where you combine several input arrays is here: 您的代码中唯一可合并多个输入数组的行在此处:

def set_element(elements, x, y, data):
        # Set element with two coordinates.
        elements[x + (y * 10)] = data

Here you have four input variables (elements, x, y, data) which I assume to be your input arrays (a,b,c,d). 在这里,您有四个输入变量(元素,x,y,数据),我假设它们是您的输入数组(a,b,c,d)。 In this operation yet you do not combine them, but you overwrite an element of elements (index: x + 10y) with a new value (data). 在此操作中,您尚未将它们组合在一起,但是您用新值(数据)覆盖了元素的元素(索引:x + 10y)。

Therefore, I do not understand your target output. 因此,我不理解您的目标输出。

When I was asking for toy data, I had something like this in mind: 当我询问玩具数据时,我想到的是这样的东西:

a = [[1,2]]
b = [[3,4]]
c = [[5,6]]
d = [[7,8]]

This would be such an easy example that you could easily say: 这将是一个简单的示例,您可以轻松地说:

What I want is this: 我想要的是:

res = [[[1,2],[3,4]],[[5,6],[7,8]]]

Then we could help you to find an answer. 然后,我们可以帮助您找到答案。

Please, thus, provide more information about the operation that you want to conduct either mathematically notated ( such as x = a +b*c +d) or with toy data so that we can deduce the function you ask for. 因此,请提供有关您要进行数学表示(例如x = a + b * c + d)或玩具数据的操作的更多信息,以便我们推断出您想要的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM