简体   繁体   English

如何在python中使用2d数组访问网格的单元格?

[英]How to access a cell of a grid using a 2d array in python?

I'm tryin to bilerp a cell into one value using python for a NetCDF data set. 我正在尝试使用python将NetCDF数据集的一个单元格变为一个值。 I'm new to python and I can't understand what I'm doing wrong here. 我是python的新手,我不明白自己在做什么错。 I have a 2d array and I give the four points by iterating through the dataset dataArr. 我有一个二维数组,并通过遍历数据集dataArr给出了四个点。 I use Lerp method for the calculation. 我使用Lerp方法进行计算。 Somehow when I pass the 3 values it seems to consider it as more than 3. What am I doing wrong here? 不知何故,当我传递3个值时,似乎认为它大于3。在这里我做错了什么? Thanks in adv. 感谢在广告中。

This is the error I get>> self.Lerp((dataArr[i][j]),(dataArr[i+1][j]),fraction) takes exactly 3 arguments (4 given) 这是我得到的错误>> self.Lerp((dataArr [i] [j]),(dataArr [i + 1] [j]),fraction)正好接受3个参数(给定4个)

def compute(self,varval):      
        vars=self.data.variables
        for var in vars:
            if var==varval:
                ntimes, ny, nx=vars[var].shape #inherit the method above.
        print(ntimes, ny, nx)
        #create the old computational grid.
        computational_grid=np.zeros((ny,nx),dtype=int) 
        fraction=.5 
        newnx,newny =(nx*fraction,ny*fraction)
        new_computational_grid=np.zeros((newny,newnx),dtype=int)
        phy_value_arr=self.get_data(varval)
        t=10 #send this t value with coords
        dataArr=self.data.variables['tos'][t]     
        for i in range(0,(ny-1),1):
            for j in range(0,(nx-1),1):
               a=self.Lerp((dataArr[i][j]),(dataArr[i+1][j]),fraction)
               b=self.Lerp((dataArr[i][j]),(dataArr[i+1][j]),fraction)
               self.tempY.append(self.Lerp(a,b,fraction))
       tempY.reshape(newnx,newny)
       pcolormesh(self.tempY)
       colorbar()

def Lerp( _a, _b, _t) :
      return _a+(_b-_a)*_t

You call Lerp like tihs: 您像tihs一样称呼Lerp:

self.Lerp(a,b,fraction)

By definition it will put self as the first argument. 根据定义,它将把self作为第一个参数。 Thus you will have four in total. 因此,您总共将有四个。 However, you define Lerp as 但是,您将Lerp定义为

def Lerp( _a, _b, _t) : #<- this takes only three arguments

I think it should be: 我认为应该是:

def Lerp(self, _a, _b, _t) : #<- this takes four, and self is first one

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

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