从多个2D阵列创建3D阵列

时间:2017-05-23 08:05:30

标签: python arrays numpy datagrid

我有两个月网格数据集,我想稍后比较。 输入对于两个数据都是这样的,这也是我想要输出的方式。

xgettext

因此两者具有相同的空间分辨率144x72但不同的时间长度。 由于其中一个人有一些缺失的月份,我确保只选择了几个月都有数据。所以我创建了一个二维数组,如果两个数据集包含这个月,则根据它们的经度和纬度值存储数据。最后,我希望为data1和data2提供相同长度的三维数组。

In[4]: data1.shape
Out[4]: (444, 72, 144)

In[5]: gfz.shape
Out[5]: (155, 72, 144)

In[6]: data1
Out[6]: 
array([[[ 0.98412287,  0.96739882,  0.91172796, ...,  1.12651634,
          1.0682013 ,  1.07681048],
        [ 1.47803092,  1.44721365,  1.49585509, ...,  1.58934438,
          1.66956687,  1.57198083],
        [ 0.68730044,  0.76112831,  0.78218687, ...,  0.92582172,
          1.07873237,  0.87490368],
        ..., 
        [ 1.00752461,  1.00758123,  0.99440521, ...,  0.94128627,
          0.88981551,  0.93984401],
        [ 1.03467119,  1.02640462,  0.91580886, ...,  0.88302392,
          0.99204206,  0.96396238],
        [ 0.8280431 ,  0.82936555,  0.82637453, ...,  0.92009377,
          0.77890259,  0.81065702]],

       ..., 
       [[-0.12173297, -0.06624345, -0.02809682, ..., -0.04522502,
         -0.11502996, -0.22779272],
        [-0.61080372, -0.61958522, -0.52239478, ..., -0.6775983 ,
         -0.79460669, -0.70022893],
        [-0.12011283, -0.10849079,  0.096185  , ..., -0.45782232,
         -0.39763898, -0.31247514],
        ..., 
        [ 0.90601307,  0.88580155,  0.90268403, ...,  0.86414611,
          0.87041426,  0.86274058],
        [ 1.46445823,  1.31938004,  1.37585044, ...,  1.51378822,
          1.48515761,  1.49078977],
        [ 0.29749078,  0.22273554,  0.27161494, ...,  0.43205476,
          0.43777165,  0.36340511]],

       [[ 0.41008961,  0.44208974,  0.40928891, ...,  0.45899671,
          0.39472976,  0.36803097],
        [-0.13514084, -0.17332518, -0.11183424, ..., -0.22284794,
         -0.2532815 , -0.15402752],
        [ 0.28614867,  0.33750001,  0.48767376, ...,  0.01886483,
          0.07220326,  0.17406547],
        ..., 
        [ 1.0551219 ,  1.09540403,  1.19031584, ...,  1.09203815,
          1.07658005,  1.08363533],
        [ 1.54310501,  1.49531853,  1.56107259, ...,  1.57243073,
          1.5867976 ,  1.57728028],
        [ 1.1034857 ,  0.98658448,  1.14141166, ...,  0.97744882,
          1.13562942,  1.08589089]],

       [[ 1.02020931,  0.99780071,  0.87209344, ...,  1.11072564,
          1.01270151,  0.9222675 ],
        [ 0.93467152,  0.81068456,  0.68190312, ...,  0.95696563,
          0.84669352,  0.84596157],
        [ 0.97022212,  0.94228816,  0.97413743, ...,  1.06613588,
          1.08708596,  1.04224277],
        ..., 
        [ 1.21519053,  1.23492992,  1.2802881 , ...,  1.33915019,
          1.32537413,  1.27963519],
        [ 1.32051706,  1.28170252,  1.36266208, ...,  1.29100537,
          1.38395023,  1.34622073],
        [ 0.86108029,  0.86364979,  0.88489276, ...,  0.81707358,
          0.82471925,  0.83550251]]], dtype=float32)

脚本运行没有错误,但是,我只得到一个非常长的1D数组。

3Darray_data1 =[]
3Darray_data2=[]
xy_data1=[[0 for i in range(len(lons_data1))] for j in range(len(lats_data1))]
xy_data2=[[0 for i in range(len(lons_data2))] for j in range(len(lats_data2))] 

# comparing the time steps 
for i in range(len(time_data1)):
    for j in range(len(time_data2)):
        if time_data1.year[i] == time_data2[j].year and time_data1[i].month==time_data2[j].month:

            # loop for data1 which writes the data into a 2D array
            for x in range(len(lats_data1)):
                for y in range(len(lons_data1)):
                    xy_data1[x][y]=data1[j,0,x,y]

            # append to get an array of arrays                   
            xy_data1 = np.squeeze(np.asarray(xy_data1))
            3Darray_data1 = np.append(3Darray_data1,[xy_data1])

            # loop for data2 which writes the data into a 2D array
            for x in range(len(lats_data2)):
                for y in range(len(lons_data2)):
                    xy_data2[x][y]=data2[i,x,y]

            # append to get an array of arrays                    
            xy_data2 = np.squeeze(np.asarray(xy_data2))
            3Darray_data2 = np.append(3Darray_data2,[xy_data2])

如何将其排列成三维数组?

1 个答案:

答案 0 :(得分:0)

对我来说,我得到了以下工作。 我用经度和纬度的固定维度以及时间轴的未定义长度定义了三维数组。

temp_data1 = np.zeros((0,len(lats_data1),len(lons_data1)))

然后我沿着时间轴添加了两个维度输出。

3Darray = np.append(3Darray,xy_data1[np.newaxis,:,:],axis=0)