简体   繁体   English

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

[英]Create 3D array from multiple 2D arrays

I have two monthly gridded data sets which I want to compare later. 我有两个每月的网格数据集,以后想比较。 The input looks like this for both data and that is also how I want the output. 对于数据来说,输入看起来都是这样,这也是我想要输出的方式。

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)

So both have the same spatial resolution of 144x72 but different length of time. 因此,两者具有相同的144x72空间分辨率,但时间长度不同。 As one of them has some missing months, I made sure that only the months are selected were both have data. 由于其中之一缺少月份,因此我确保只有选中的月份都具有数据。 So I created a two dimensional array where the data is stored according to their longitude and latitude value if both data sets contain this month. 因此,我创建了一个二维数组,如果两个数据集都包含本月,则根据数据的经度和纬度值存储数据。 In the end I want to have a three dimensional array for data1 and data2 of the same length. 最后,我想为长度相同的data1和data2提供一个三维数组。

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])

The script runs without an error, however, I only get a really long 1D array. 该脚本运行没有错误,但是,我只得到了一个很长的一维数组。

In[3]: 3Darray_data1
Out[3]: array([        nan,         nan,         nan, ...,  0.81707358,
        0.82471925,  0.83550251])

How can I arrange it to a three dimensional array? 如何将其排列成三维阵列?

For me I got it working with the following. 对我来说,我可以使用以下方法。 I defined the three dimensional array with the fixed dimension of the longitude and latitude and an undefined length of the time axis. 我定义了具有固定维度的经度和纬度以及未定义的时间轴长度的三维数组。

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

And then I appended two dimensional outputs along the time axis. 然后,我沿着时间轴附加了二维输出。

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

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

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