我现在陷入困境,不知道如何解决这个问题。 我想将此计算应用于列表/数据帧:
方程本身对我来说并不是真正的问题,我可以很容易地手动求解,但这与我拥有的数据量无关
因此,基本上,这是为了计算/近似一个新的温度值,该温度值位于距离正方形拐角一定距离的位置:
import pandas as pd
import numpy as np
import xarray as xr
import math
filepath = r'F:\Data\data.nc' # just the path to the file
obj= xr.open_dataset(filepath)
# This is where I get the coordinates for each of the corners of the square
# from the netcdf4 file
lat = 9.7398
lon = 51.2695
xlat = obj['XLAT'].values
xlon = obj['XLON'].values
p_1 = [xlat[0,0], xlon[0,0]]
p_2 = [xlat[0,1], xlon[0,1]]
p_3 = [xlat[1,0], xlon[1,0]]
p_4 = [xlat[1,1], xlon[1,1]]
p_rect = [p_1, p_2, p_3, p_4]
p_orig = [lat, lon]
#=================================================
# Calculates the distance between the points
# d = sqrt((x2-x1)^2 + (y2-y1)^2))
#=================================================
distance = []
for coord in p_rect:
distance.append(math.sqrt(math.pow(coord[0]-p_orig[0],2)+math.pow(coord[1]-p_orig[1],2)))
# to get the values for they key['WS'] for example:
a = obj['WS'].values[:,0,0,0] # Array of floats for the first values
b = obj['WS'].values[:,0,0,1] # Array of floats for the second values
c = obj['WS'].values[:,0,1,0] # Array of floats for the third values
d = obj['WS'].values[:,0,1,1] # Array of floats for the fourth values
从那时起,我不知道我应该如何继续,我应该怎么做:
df = pd.DataFrame()
df['a'] = a
df['b'] = b
df['c'] = c
df['d'] = d
然后以某种方式处理数据帧,在获得所需的值后删除abcd,或者我应该先处理列表,然后只将结果添加到数据帧中。我有点迷路了
到目前为止,我想到的唯一一件事是,如果我手动操作,它会是什么样子:
for i starting at 0 and ending if the end of the list [a, b, c d have the same length] is reached .
1/a[i]^2*distance[0] + 1/b[i]^2*distance[1] + 1/c[i]^2*distance[2] + 1/d[i]^2*distance[3]
v = ------------------------------------------------------------------------------------------
1/a[i]^2 + 1/b[i]^2 + 1/c[i]^2 + 1/d[i]^2
'''
这是我第一次(至少对我来说)对列表/数据帧进行如此复杂的计算。我希望你能帮我解决这个问题,或者至少让我朝着正确的方向前进
PS:以下是指向该文件的链接: LINK TO FILE
只需将计算矢量化即可。对于数据帧,您可以直接在列上运行整个算术运算,就像它们是生成另一列的标量一样。下面假设距离是一个由四个标量组成的列表,记住在Python中
^
并不意味着力量,而是我们**
或者使用Pandas系列binary operators的函数形式。以下是操作顺序(括号>;指数>;乘法/除法>;加法/减法):
相关问题 更多 >
编程相关推荐